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