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
8using std::endl;
9using std::ostream;
10
11namespace CppUnit
12{
13
14
15TextOutputter::TextOutputter( TestResultCollector *result,
16                              ostream &stream )
17    : m_result( result )
18    , m_stream( stream )
19{
20}
21
22
23TextOutputter::~TextOutputter()
24{
25}
26
27
28void
29TextOutputter::write()
30{
31  printHeader();
32  m_stream << endl;
33  printFailures();
34  m_stream << endl;
35}
36
37
38void
39TextOutputter::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
51void
52TextOutputter::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
68void
69TextOutputter::printFailureListMark( int failureNumber )
70{
71  m_stream << failureNumber << ")";
72}
73
74
75void
76TextOutputter::printFailureTestName( TestFailure *failure )
77{
78  m_stream << "test: " << failure->failedTestName();
79}
80
81
82void
83TextOutputter::printFailureType( TestFailure *failure )
84{
85  m_stream << "("
86           << (failure->isError() ? "E" : "F")
87           << ")";
88}
89
90
91void
92TextOutputter::printFailureLocation( SourceLine sourceLine )
93{
94  if ( !sourceLine.isValid() )
95    return;
96
97  m_stream << "line: " << sourceLine.lineNumber()
98           << ' ' << sourceLine.fileName();
99}
100
101
102void
103TextOutputter::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
124void
125TextOutputter::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
139void
140TextOutputter::printFailureWarning()
141{
142  m_stream  << "!!!FAILURES!!!" << endl;
143}
144
145
146void
147TextOutputter::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