comparison ext/UnitTest++/src/tests/TestXmlTestReporter.cpp @ 37:0d325e9d5953

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