xref: /haiku/src/tools/cppunit/cppunit/TextOutputter.cpp (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
1 #include <cppunit/NotEqualException.h>
2 #include <cppunit/TestFailure.h>
3 #include <cppunit/SourceLine.h>
4 #include <cppunit/TestResultCollector.h>
5 #include <cppunit/TextOutputter.h>
6 
7 
8 namespace CppUnit
9 {
10 
11 
12 TextOutputter::TextOutputter( TestResultCollector *result,
13                               ostream &stream )
14     : m_result( result )
15     , m_stream( stream )
16 {
17 }
18 
19 
20 TextOutputter::~TextOutputter()
21 {
22 }
23 
24 
25 void
26 TextOutputter::write()
27 {
28   printHeader();
29   m_stream << endl;
30   printFailures();
31   m_stream << endl;
32 }
33 
34 
35 void
36 TextOutputter::printFailures()
37 {
38   TestResultCollector::TestFailures::const_iterator itFailure = m_result->failures().begin();
39   int failureNumber = 1;
40   while ( itFailure != m_result->failures().end() )
41   {
42     m_stream  <<  endl;
43     printFailure( *itFailure++, failureNumber++ );
44   }
45 }
46 
47 
48 void
49 TextOutputter::printFailure( TestFailure *failure,
50                              int failureNumber )
51 {
52   printFailureListMark( failureNumber );
53   m_stream << ' ';
54   printFailureTestName( failure );
55   m_stream << ' ';
56   printFailureType( failure );
57   m_stream << ' ';
58   printFailureLocation( failure->sourceLine() );
59   m_stream << endl;
60   printFailureDetail( failure->thrownException() );
61   m_stream << endl;
62 }
63 
64 
65 void
66 TextOutputter::printFailureListMark( int failureNumber )
67 {
68   m_stream << failureNumber << ")";
69 }
70 
71 
72 void
73 TextOutputter::printFailureTestName( TestFailure *failure )
74 {
75   m_stream << "test: " << failure->failedTestName();
76 }
77 
78 
79 void
80 TextOutputter::printFailureType( TestFailure *failure )
81 {
82   m_stream << "("
83            << (failure->isError() ? "E" : "F")
84            << ")";
85 }
86 
87 
88 void
89 TextOutputter::printFailureLocation( SourceLine sourceLine )
90 {
91   if ( !sourceLine.isValid() )
92     return;
93 
94   m_stream << "line: " << sourceLine.lineNumber()
95            << ' ' << sourceLine.fileName();
96 }
97 
98 
99 void
100 TextOutputter::printFailureDetail( Exception *thrownException )
101 {
102   if ( thrownException->isInstanceOf( NotEqualException::type() ) )
103   {
104     NotEqualException *e = (NotEqualException*)thrownException;
105     m_stream << "expected: " << e->expectedValue() << endl
106              << "but was:  " << e->actualValue();
107     if ( !e->additionalMessage().empty() )
108     {
109       m_stream  << endl;
110       m_stream  <<  "additional message:"  <<  endl
111                 <<  e->additionalMessage();
112     }
113   }
114   else
115   {
116     m_stream << " \"" << thrownException->what() << "\"";
117   }
118 }
119 
120 
121 void
122 TextOutputter::printHeader()
123 {
124   if ( m_result->wasSuccessful() )
125     m_stream << endl << "OK (" << m_result->runTests () << " tests)"
126              << endl;
127   else
128   {
129     m_stream << endl;
130     printFailureWarning();
131     printStatistics();
132   }
133 }
134 
135 
136 void
137 TextOutputter::printFailureWarning()
138 {
139   m_stream  << "!!!FAILURES!!!" << endl;
140 }
141 
142 
143 void
144 TextOutputter::printStatistics()
145 {
146   m_stream  << "Test Results:" << endl;
147 
148   m_stream  <<  "Run:  "  <<  m_result->runTests()
149             <<  "   Failures: "  <<  m_result->testFailures()
150             <<  "   Errors: "  <<  m_result->testErrors()
151             <<  endl;
152 }
153 
154 
155 } //  namespace CppUnit
156 
157