1 |
|
2 |
#include <iostream> |
3 |
#include <iomanip> |
4 |
|
5 |
#include "CppUnitTest/TextTestResult.h" |
6 |
#include "CppUnitTest/CppUnitException.h" |
7 |
#include "CppUnitTest/estring.h" |
8 |
|
9 |
USING_NAMESPACE_CPPUNITTEST |
10 |
|
11 |
void TextTestResult::addError (Test *test, CppUnitException *e) |
12 |
{ |
13 |
TestResult::addError (test, e); |
14 |
std::cout << " Error: "; |
15 |
std::cout << e->what() << std::endl; |
16 |
} |
17 |
|
18 |
void TextTestResult::addFailure (Test *test, CppUnitException *e) |
19 |
{ |
20 |
TestResult::addFailure (test, e); |
21 |
std::cout << " Failure" << std::endl; |
22 |
|
23 |
} |
24 |
|
25 |
void TextTestResult::startTest (Test *test) |
26 |
{ |
27 |
TestResult::startTest (test); |
28 |
std::cout << " Test: " << test->name(); |
29 |
} |
30 |
|
31 |
|
32 |
|
33 |
|
34 |
void TextTestResult::printErrors (std::ostream& stream) |
35 |
{ |
36 |
if (testErrors () != 0) { |
37 |
|
38 |
if (testErrors () == 1) |
39 |
stream << "There was " << testErrors () << " error: " << std::endl; |
40 |
else |
41 |
stream << "There were " << testErrors () << " errors: " << std::endl; |
42 |
|
43 |
int i = 1; |
44 |
|
45 |
for (std::vector<TestFailure *>::iterator it = errors ().begin (); it != errors ().end (); ++it) { |
46 |
TestFailure *failure = *it; |
47 |
CppUnitException *e = failure->thrownException (); |
48 |
|
49 |
stream << i |
50 |
<< ") " |
51 |
<< (*failure).failedTest()->name() << " " |
52 |
<< "line: " << (e ? estring (e->lineNumber ()) : std::string("")) << " " |
53 |
<< (e ? e->fileName () : std::string("")) << " " |
54 |
<< "\"" << failure->thrownException ()->what () << "\"" |
55 |
<< std::endl; |
56 |
i++; |
57 |
} |
58 |
} |
59 |
|
60 |
} |
61 |
|
62 |
void TextTestResult::printFailures (std::ostream& stream) |
63 |
{ |
64 |
if (testFailures () != 0) { |
65 |
if (testFailures () == 1) |
66 |
stream << "There was " << testFailures () << " failure: " << std::endl; |
67 |
else |
68 |
stream << "There were " << testFailures () << " failures: " << std::endl; |
69 |
|
70 |
int i = 1; |
71 |
|
72 |
for (std::vector<TestFailure *>::iterator it = failures ().begin (); it != failures ().end (); ++it) { |
73 |
TestFailure *failure = *it; |
74 |
CppUnitException *e = failure->thrownException (); |
75 |
|
76 |
stream << i |
77 |
<< ") " |
78 |
<< (*failure).failedTest()->name() << " " |
79 |
<< "line: " << (e ? estring (e->lineNumber ()) : std::string("")) << " " |
80 |
<< (e ? e->fileName () : std::string("")) << " " |
81 |
<< "\"" << failure->thrownException ()->what () << "\"" |
82 |
<< std::endl; |
83 |
i++; |
84 |
} |
85 |
} |
86 |
|
87 |
} |
88 |
|
89 |
|
90 |
void TextTestResult::print (std::ostream& stream) |
91 |
{ |
92 |
printHeader (stream); |
93 |
printErrors (stream); |
94 |
printFailures (stream); |
95 |
|
96 |
} |
97 |
|
98 |
|
99 |
void TextTestResult::printHeader (std::ostream& stream) |
100 |
{ |
101 |
if (wasSuccessful ()) |
102 |
std::cout << std::endl << "OK (" << runTests () << " tests)" << std::endl; |
103 |
else |
104 |
std::cout << std::endl |
105 |
<< "!!!FAILURES!!!" << std::endl |
106 |
<< "Test Results:" << std::endl |
107 |
<< "Run: " |
108 |
<< runTests () |
109 |
<< " Failures: " |
110 |
<< testFailures () |
111 |
<< " Errors: " |
112 |
<< testErrors () |
113 |
<< std::endl; |
114 |
|
115 |
} |