?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "UnitTest++/Config.h"
  2. #ifndef UNITTEST_NO_DEFERRED_REPORTER
  3.  
  4. #include "UnitTest++/UnitTestPP.h"
  5. #include "UnitTest++/XmlTestReporter.h"
  6.  
  7. #include <sstream>
  8.  
  9. using namespace UnitTest;
  10. using std::ostringstream;
  11.  
  12. namespace
  13. {
  14.  
  15. #ifndef UNITTEST_MEMORYOUTSTREAM_IS_STD_OSTRINGSTREAM
  16.  
  17. // Overload to let MemoryOutStream accept std::string
  18.    MemoryOutStream& operator<<(MemoryOutStream& s, const std::string& value)
  19.    {
  20.       s << value.c_str();
  21.       return s;
  22.    }
  23.  
  24. #endif
  25.  
  26.    struct XmlTestReporterFixture
  27.    {
  28.       XmlTestReporterFixture()
  29.          : reporter(output)
  30.       {}
  31.  
  32.       ostringstream output;
  33.       XmlTestReporter reporter;
  34.    };
  35.  
  36.    TEST_FIXTURE(XmlTestReporterFixture, MultipleCharactersAreEscaped)
  37.    {
  38.       TestDetails const details("TestName", "suite", "filename.h", 4321);
  39.  
  40.       reporter.ReportTestStart(details);
  41.       reporter.ReportFailure(details, "\"\"\'\'&&<<>>");
  42.       reporter.ReportTestFinish(details, 0.1f);
  43.       reporter.ReportSummary(1, 2, 3, 0.1f);
  44.  
  45.       char const* expected =
  46.          "<?xml version=\"1.0\"?>"
  47.          "<unittest-results tests=\"1\" failedtests=\"2\" failures=\"3\" time=\"0.1\">"
  48.          "<test suite=\"suite\" name=\"TestName\" time=\"0.1\">"
  49.          "<failure message=\"filename.h(4321) : "
  50.          "&quot;&quot;&apos;&apos;&amp;&amp;&lt;&lt;&gt;&gt;\"/>"
  51.          "</test>"
  52.          "</unittest-results>";
  53.  
  54.       CHECK_EQUAL(expected, output.str().c_str());
  55.    }
  56.  
  57.    TEST_FIXTURE(XmlTestReporterFixture, OutputIsCachedUntilReportSummaryIsCalled)
  58.    {
  59.       TestDetails const details("", "", "", 0);
  60.  
  61.       reporter.ReportTestStart(details);
  62.       reporter.ReportFailure(details, "message");
  63.       reporter.ReportTestFinish(details, 1.0F);
  64.       CHECK(output.str().empty());
  65.  
  66.       reporter.ReportSummary(1, 1, 1, 1.0f);
  67.       CHECK(!output.str().empty());
  68.    }
  69.  
  70.    TEST_FIXTURE(XmlTestReporterFixture, EmptyReportSummaryFormat)
  71.    {
  72.       reporter.ReportSummary(0, 0, 0, 0.1f);
  73.  
  74.       const char *expected =
  75.          "<?xml version=\"1.0\"?>"
  76.          "<unittest-results tests=\"0\" failedtests=\"0\" failures=\"0\" time=\"0.1\">"
  77.          "</unittest-results>";
  78.  
  79.       CHECK_EQUAL(expected, output.str().c_str());
  80.    }
  81.  
  82.    TEST_FIXTURE(XmlTestReporterFixture, SingleSuccessfulTestReportSummaryFormat)
  83.    {
  84.       TestDetails const details("TestName", "DefaultSuite", "", 0);
  85.  
  86.       reporter.ReportTestStart(details);
  87.       reporter.ReportSummary(1, 0, 0, 0.1f);
  88.  
  89.       const char *expected =
  90.          "<?xml version=\"1.0\"?>"
  91.          "<unittest-results tests=\"1\" failedtests=\"0\" failures=\"0\" time=\"0.1\">"
  92.          "<test suite=\"DefaultSuite\" name=\"TestName\" time=\"0\"/>"
  93.          "</unittest-results>";
  94.  
  95.       CHECK_EQUAL(expected, output.str().c_str());
  96.    }
  97.  
  98.    TEST_FIXTURE(XmlTestReporterFixture, SingleFailedTestReportSummaryFormat)
  99.    {
  100.       TestDetails const details("A Test", "suite", "A File", 4321);
  101.  
  102.       reporter.ReportTestStart(details);
  103.       reporter.ReportFailure(details, "A Failure");
  104.       reporter.ReportSummary(1, 1, 1, 0.1f);
  105.  
  106.       const char *expected =
  107.          "<?xml version=\"1.0\"?>"
  108.          "<unittest-results tests=\"1\" failedtests=\"1\" failures=\"1\" time=\"0.1\">"
  109.          "<test suite=\"suite\" name=\"A Test\" time=\"0\">"
  110.          "<failure message=\"A File(4321) : A Failure\"/>"
  111.          "</test>"
  112.          "</unittest-results>";
  113.  
  114.       CHECK_EQUAL(expected, output.str().c_str());
  115.    }
  116.  
  117.    TEST_FIXTURE(XmlTestReporterFixture, FailureMessageIsXMLEscaped)
  118.    {
  119.       TestDetails const details("TestName", "suite", "filename.h", 4321);
  120.  
  121.       reporter.ReportTestStart(details);
  122.       reporter.ReportFailure(details, "\"\'&<>");
  123.       reporter.ReportTestFinish(details, 0.1f);
  124.       reporter.ReportSummary(1, 1, 1, 0.1f);
  125.  
  126.       char const* expected =
  127.          "<?xml version=\"1.0\"?>"
  128.          "<unittest-results tests=\"1\" failedtests=\"1\" failures=\"1\" time=\"0.1\">"
  129.          "<test suite=\"suite\" name=\"TestName\" time=\"0.1\">"
  130.          "<failure message=\"filename.h(4321) : &quot;&apos;&amp;&lt;&gt;\"/>"
  131.          "</test>"
  132.          "</unittest-results>";
  133.  
  134.       CHECK_EQUAL(expected, output.str().c_str());
  135.    }
  136.  
  137.    TEST_FIXTURE(XmlTestReporterFixture, OneFailureAndOneSuccess)
  138.    {
  139.       TestDetails const failedDetails("FailedTest", "suite", "fail.h", 1);
  140.       reporter.ReportTestStart(failedDetails);
  141.       reporter.ReportFailure(failedDetails, "expected 1 but was 2");
  142.       reporter.ReportTestFinish(failedDetails, 0.1f);
  143.  
  144.       TestDetails const succeededDetails("SucceededTest", "suite", "", 0);
  145.       reporter.ReportTestStart(succeededDetails);
  146.       reporter.ReportTestFinish(succeededDetails, 1.0f);
  147.       reporter.ReportSummary(2, 1, 1, 1.1f);
  148.  
  149.       char const* expected =
  150.          "<?xml version=\"1.0\"?>"
  151.          "<unittest-results tests=\"2\" failedtests=\"1\" failures=\"1\" time=\"1.1\">"
  152.          "<test suite=\"suite\" name=\"FailedTest\" time=\"0.1\">"
  153.          "<failure message=\"fail.h(1) : expected 1 but was 2\"/>"
  154.          "</test>"
  155.          "<test suite=\"suite\" name=\"SucceededTest\" time=\"1\"/>"
  156.          "</unittest-results>";
  157.  
  158.       CHECK_EQUAL(expected, output.str().c_str());
  159.    }
  160.  
  161.    TEST_FIXTURE(XmlTestReporterFixture, MultipleFailures)
  162.    {
  163.       TestDetails const failedDetails1("FailedTest", "suite", "fail.h", 1);
  164.       TestDetails const failedDetails2("FailedTest", "suite", "fail.h", 31);
  165.  
  166.       reporter.ReportTestStart(failedDetails1);
  167.       reporter.ReportFailure(failedDetails1, "expected 1 but was 2");
  168.       reporter.ReportFailure(failedDetails2, "expected one but was two");
  169.       reporter.ReportTestFinish(failedDetails1, 0.1f);
  170.  
  171.       reporter.ReportSummary(1, 1, 2, 1.1f);
  172.  
  173.       char const* expected =
  174.          "<?xml version=\"1.0\"?>"
  175.          "<unittest-results tests=\"1\" failedtests=\"1\" failures=\"2\" time=\"1.1\">"
  176.          "<test suite=\"suite\" name=\"FailedTest\" time=\"0.1\">"
  177.          "<failure message=\"fail.h(1) : expected 1 but was 2\"/>"
  178.          "<failure message=\"fail.h(31) : expected one but was two\"/>"
  179.          "</test>"
  180.          "</unittest-results>";
  181.  
  182.       CHECK_EQUAL(expected, output.str().c_str());
  183.    }
  184.  
  185. }
  186.  
  187. #endif
  188.