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