Debian Codegraph @

tests/CppUTest/TestOutputTest.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/TestOutput.h"
30 #include "CppUTest/TestResult.h"
31 #include "CppUTest/PlatformSpecificFunctions.h"
32
33 static long millisTime;
34
35 extern "C" {
36
37 static long MockGetPlatformSpecificTimeInMillis()
38 {
39 return millisTime;
40 }
41
42 }
43
44 TEST_GROUP(TestOutput)
45 {
46 TestOutput* printer;
47 StringBufferTestOutput* mock;
48 UtestShell* tst;
49 TestFailure *f;
50 TestFailure *f2;
51 TestFailure *f3;
52 TestResult* result;
53
54 void setup()
55 {
56 mock = new StringBufferTestOutput();
57 printer = mock;
58 tst = new UtestShell("group", "test", "file", 10);
59 f = new TestFailure(tst, "failfile", 20, "message");
60 f2 = new TestFailure(tst, "file", 20, "message");
61 f3 = new TestFailure(tst, "file", 2, "message");
62 result = new TestResult(*mock);
63 result->setTotalExecutionTime(10);
64 millisTime = 0;
65 UT_PTR_SET(GetPlatformSpecificTimeInMillis, MockGetPlatformSpecificTimeInMillis);
66 TestOutput::setWorkingEnvironment(TestOutput::eclipse);
67
68 }
69 void teardown()
70 {
71 TestOutput::setWorkingEnvironment(TestOutput::detectEnvironment);
72 delete printer;
73 delete tst;
74 delete f;
75 delete f2;
76 delete f3;
77 delete result;
78 }
79
80 void runOneTest()
81 {
82 result->countTest();
83 result->countRun();
84 }
85 };
86
87 TEST(TestOutput, PrintConstCharStar)
88 {
89 printer->print("hello");
90 printer->print("hello\n");
91 STRCMP_EQUAL("hellohello\n", mock->getOutput().asCharString());
92 }
93
94 TEST(TestOutput, PrintLong)
95 {
96 long number = 1234;
97 printer->print(number);
98 STRCMP_EQUAL("1234", mock->getOutput().asCharString());
99 }
100
101 TEST(TestOutput, PrintSize)
102 {
103 size_t ten = 10;
104 printer->print(ten);
105 STRCMP_EQUAL("10", mock->getOutput().asCharString());
106 }
107
108
109 TEST(TestOutput, PrintDouble)
110 {
111 printer->printDouble(12.34);
112 STRCMP_EQUAL("12.34", mock->getOutput().asCharString());
113 }
114
115 TEST(TestOutput, StreamOperators)
116 {
117 *printer << "n=" << 1234;
118 STRCMP_EQUAL("n=1234", mock->getOutput().asCharString());
119 }
120
121 TEST(TestOutput, PrintTestEnded)
122 {
123 printer->printCurrentTestEnded(*result);
124 STRCMP_EQUAL(".", mock->getOutput().asCharString());
125 }
126
127 TEST(TestOutput, PrintTestALot)
128 {
129 for (int i = 0; i < 60; ++i) {
130 printer->printCurrentTestEnded(*result);
131 }
132 STRCMP_EQUAL("..................................................\n..........", mock->getOutput().asCharString());
133 }
134
135 TEST(TestOutput, PrintTestALotAndSimulateRepeatRun)
136 {
137 for (int i = 0; i < 60; ++i) {
138 runOneTest();
139 printer->printCurrentTestEnded(*result);
140 }
141
142 printer->printTestsEnded(*result);
143
144 for (int i = 0; i < 60; ++i) {
145 runOneTest();
146 printer->printCurrentTestEnded(*result);
147 }
148 STRCMP_EQUAL("..................................................\n.........." \
149 "\nOK (60 tests, 60 ran, 0 checks, 0 ignored, 0 filtered out, 10 ms)\n\n" \
150 "..................................................\n..........", mock->getOutput().asCharString());
151 }
152
153 TEST(TestOutput, SetProgressIndicator)
154 {
155 printer->setProgressIndicator(".");
156 printer->printCurrentTestEnded(*result);
157 printer->setProgressIndicator("!");
158 printer->printCurrentTestEnded(*result);
159 printer->setProgressIndicator(".");
160 printer->printCurrentTestEnded(*result);
161
162 STRCMP_EQUAL(".!.", mock->getOutput().asCharString());
163 }
164
165 TEST(TestOutput, PrintTestVerboseStarted)
166 {
167 mock->verbose(TestOutput::level_verbose);
168 printer->printCurrentTestStarted(*tst);
169 STRCMP_EQUAL("TEST(group, test)", mock->getOutput().asCharString());
170 }
171
172 TEST(TestOutput, PrintTestVerboseEnded)
173 {
174 mock->verbose(TestOutput::level_verbose);
175 result->currentTestStarted(tst);
176 millisTime = 5;
177 result->currentTestEnded(tst);
178 STRCMP_EQUAL("TEST(group, test) - 5 ms\n", mock->getOutput().asCharString());
179 }
180
181 TEST(TestOutput, printColorWithSuccess)
182 {
183 mock->color();
184 runOneTest();
185 printer->printTestsEnded(*result);
186 STRCMP_EQUAL("\n\033[32;1mOK (1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out, 10 ms)\033[m\n\n",
187 mock->getOutput().asCharString());
188 }
189
190 TEST(TestOutput, printColorWithFailures)
191 {
192 mock->color();
193 runOneTest();
194 result->addFailure(*f);
195 printer->flush();
196 printer->printTestsEnded(*result);
197 STRCMP_EQUAL("\n\033[31;1mErrors (1 failures, 1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out, 10 ms)"
198 "\033[m\n\n", mock->getOutput().asCharString());
199 }
200
201 TEST(TestOutput, PrintTestRun)
202 {
203 printer->printTestRun(2, 3);
204 STRCMP_EQUAL("Test run 2 of 3\n", mock->getOutput().asCharString());
205 }
206
207 TEST(TestOutput, PrintTestRunOnlyOne)
208 {
209 printer->printTestRun(1, 1);
210 STRCMP_EQUAL("", mock->getOutput().asCharString());
211 }
212
213 TEST(TestOutput, PrintWithFailureInSameFile)
214 {
215 printer->printFailure(*f2);
216 STRCMP_EQUAL("\nfile:20: error: Failure in TEST(group, test)\n\tmessage\n\n", mock->getOutput().asCharString());
217 }
218
219 TEST(TestOutput, PrintFailureWithFailInDifferentFile)
220 {
221 printer->printFailure(*f);
222 const char* expected =
223 "\nfile:10: error: Failure in TEST(group, test)"
224 "\nfailfile:20: error:\n\tmessage\n\n";
225 STRCMP_EQUAL(expected, mock->getOutput().asCharString());
226 }
227
228 TEST(TestOutput, PrintFailureWithFailInHelper)
229 {
230 printer->printFailure(*f3);
231 const char* expected =
232 "\nfile:10: error: Failure in TEST(group, test)"
233 "\nfile:2: error:\n\tmessage\n\n";
234 STRCMP_EQUAL(expected, mock->getOutput().asCharString());
235 }
236
237 TEST(TestOutput, PrintInVisualStudioFormat)
238 {
239 TestOutput::setWorkingEnvironment(TestOutput::visualStudio);
240 printer->printFailure(*f3);
241 const char* expected =
242 "\nfile(10): error: Failure in TEST(group, test)"
243 "\nfile(2): error:\n\tmessage\n\n";
244 STRCMP_EQUAL(expected, mock->getOutput().asCharString());
245 }
246
247 TEST(TestOutput, PrintTestStarts)
248 {
249 printer->printTestsStarted();
250 STRCMP_EQUAL("", mock->getOutput().asCharString());
251 }
252
253 TEST(TestOutput, printTestsEnded)
254 {
255 result->countTest();
256 result->countCheck();
257 result->countIgnored();
258 result->countIgnored();
259 result->countRun();
260 result->countRun();
261 result->countRun();
262 printer->printTestsEnded(*result);
263 STRCMP_EQUAL("\nOK (1 tests, 3 ran, 1 checks, 2 ignored, 0 filtered out, 10 ms)\n\n", mock->getOutput().asCharString());
264 }
265
266 TEST(TestOutput, printTestsEndedWithFailures)
267 {
268 result->addFailure(*f);
269 printer->flush();
270 printer->printTestsEnded(*result);
271 STRCMP_EQUAL("\nErrors (1 failures, 0 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 10 ms)\n\n", mock->getOutput().asCharString());
272 }
273
274 TEST(TestOutput, printTestsEndedWithNoTestsRunOrIgnored)
275 {
276 result->countTest();
277 printer->flush();
278 printer->printTestsEnded(*result);
279 STRCMP_EQUAL("\nErrors (ran nothing, 1 tests, 0 ran, 0 checks, 0 ignored, 0 filtered out, 10 ms)\n"
280 "Note: test run failed because no tests were run or ignored. Assuming something went wrong. "
281 "This often happens because of linking errors or typos in test filter.\n\n",
282 mock->getOutput().asCharString());
283 }
284
285 class CompositeTestOutputTestStringBufferTestOutput : public StringBufferTestOutput
286 {
287 public:
288 virtual void printTestsStarted()
289 {
290 output += "Test Start\n";
291 }
292
293 virtual void printTestsEnded(const TestResult& result)
294 {
295 output += StringFromFormat("Test End %d\n", (int) result.getTestCount());
296 }
297
298 void printCurrentGroupStarted(const UtestShell& test)
299 {
300 output += StringFromFormat("Group %s Start\n", test.getGroup().asCharString());
301 }
302
303 void printCurrentGroupEnded(const TestResult& res)
304 {
305 output += StringFromFormat("Group End %d\n", (int) res.getTestCount());
306 }
307
308 virtual void printCurrentTestStarted(const UtestShell&)
309 {
310 output += "s";
311 }
312
313 void flush()
314 {
315 output += "flush";
316 }
317
318 virtual bool isVerbose()
319 {
320 return verbose_ == level_verbose || verbose_ == level_veryVerbose;
321 }
322
323 virtual bool isColor()
324 {
325 return color_;
326 }
327
328 virtual const char* getProgressIndicator()
329 {
330 return progressIndication_;
331 }
332 };
333
334 TEST_GROUP(CompositeTestOutput)
335 {
336 CompositeTestOutputTestStringBufferTestOutput* output1;
337 CompositeTestOutputTestStringBufferTestOutput* output2;
338 CompositeTestOutput compositeOutput;
339 TestResult* result;
340 UtestShell* test;
341
342 void setup()
343 {
344 output1 = new CompositeTestOutputTestStringBufferTestOutput;
345 output2 = new CompositeTestOutputTestStringBufferTestOutput;
346 compositeOutput.setOutputOne(output1);
347 compositeOutput.setOutputTwo(output2);
348 result = new TestResult(compositeOutput);
349 test = new UtestShell("Group", "Name", "file", 10);
350 }
351
352 void teardown()
353 {
354 delete test;
355 delete result;
356 }
357 };
358
359 TEST(CompositeTestOutput, TestStartedAndEnded)
360 {
361 compositeOutput.printTestsStarted();
362 compositeOutput.printTestsEnded(*result);
363 STRCMP_EQUAL("Test Start\nTest End 0\n", output1->getOutput().asCharString());
364 STRCMP_EQUAL("Test Start\nTest End 0\n", output2->getOutput().asCharString());
365 }
366
367 TEST(CompositeTestOutput, CurrentTestStartedAndEnded)
368 {
369 compositeOutput.printCurrentTestStarted(*test);
370 compositeOutput.printCurrentTestEnded(*result);
371 STRCMP_EQUAL("s.", output1->getOutput().asCharString());
372 STRCMP_EQUAL("s.", output2->getOutput().asCharString());
373 }
374
375 TEST(CompositeTestOutput, CurrentGroupStartedAndEnded)
376 {
377 compositeOutput.printCurrentGroupStarted(*test);
378 compositeOutput.printCurrentGroupEnded(*result);
379 STRCMP_EQUAL("Group Group Start\nGroup End 0\n", output1->getOutput().asCharString());
380 STRCMP_EQUAL("Group Group Start\nGroup End 0\n", output2->getOutput().asCharString());
381 }
382
383 TEST(CompositeTestOutput, PrintBuffer)
384 {
385 compositeOutput.printBuffer("Boo");
386 STRCMP_EQUAL("Boo", output1->getOutput().asCharString());
387 STRCMP_EQUAL("Boo", output2->getOutput().asCharString());
388 }
389
390 TEST(CompositeTestOutput, printChar)
391 {
392 compositeOutput.print("Boo");
393 STRCMP_EQUAL("Boo", output1->getOutput().asCharString());
394 STRCMP_EQUAL("Boo", output2->getOutput().asCharString());
395 }
396
397 TEST(CompositeTestOutput, printLong)
398 {
399 long ten = 10;
400 compositeOutput.print(ten);
401 STRCMP_EQUAL("10", output1->getOutput().asCharString());
402 STRCMP_EQUAL("10", output2->getOutput().asCharString());
403 }
404
405 TEST(CompositeTestOutput, PrintSize)
406 {
407 size_t ten = 10;
408 compositeOutput.print(ten);
409 STRCMP_EQUAL("10", output1->getOutput().asCharString());
410 STRCMP_EQUAL("10", output2->getOutput().asCharString());
411 }
412
413 TEST(CompositeTestOutput, printDouble)
414 {
415 compositeOutput.printDouble(1.01);
416 STRCMP_EQUAL("1.01", output1->getOutput().asCharString());
417 STRCMP_EQUAL("1.01", output2->getOutput().asCharString());
418 }
419
420 TEST(CompositeTestOutput, verbose)
421 {
422 compositeOutput.verbose(TestOutput::level_verbose);
423 CHECK(output1->isVerbose());
424 CHECK(output2->isVerbose());
425 }
426
427 TEST(CompositeTestOutput, color)
428 {
429 compositeOutput.color();
430 CHECK(output1->isColor());
431 CHECK(output2->isColor());
432 }
433
434 TEST(CompositeTestOutput, PrintTestFailure)
435 {
436 TestOutput::WorkingEnvironment previousEnvironment = TestOutput::getWorkingEnvironment();
437 TestOutput::setWorkingEnvironment(TestOutput::eclipse);
438 TestFailure failure(test, "file", 10, "failed");
439 compositeOutput.printFailure(failure);
440 STRCMP_EQUAL("\nfile:10: error: Failure in TEST(Group, Name)\n\tfailed\n\n", output1->getOutput().asCharString());
441 STRCMP_EQUAL("\nfile:10: error: Failure in TEST(Group, Name)\n\tfailed\n\n", output2->getOutput().asCharString());
442 TestOutput::setWorkingEnvironment(previousEnvironment);
443 }
444
445 TEST(CompositeTestOutput, PrintTestRun)
446 {
447 compositeOutput.printTestRun(1, 2);
448 STRCMP_EQUAL("Test run 1 of 2\n", output1->getOutput().asCharString());
449 STRCMP_EQUAL("Test run 1 of 2\n", output2->getOutput().asCharString());
450 }
451
452 TEST(CompositeTestOutput, setProgressIndicator)
453 {
454 compositeOutput.setProgressIndicator("?");
455 STRCMP_EQUAL("?", output1->getProgressIndicator());
456 STRCMP_EQUAL("?", output2->getProgressIndicator());
457 }
458
459 TEST(CompositeTestOutput, flush)
460 {
461 compositeOutput.flush();
462 STRCMP_EQUAL("flush", output1->getOutput().asCharString());
463 STRCMP_EQUAL("flush", output2->getOutput().asCharString());
464 }
465
466 TEST(CompositeTestOutput, deletePreviousInstanceWhenSettingNew)
467 {
468 compositeOutput.setOutputOne(new CompositeTestOutput);
469 compositeOutput.setOutputTwo(new CompositeTestOutput);
470
471 // CHECK NO MEMORY LEAKS
472 }
473