summaryrefslogtreecommitdiff
path: root/src/cppunit/CompilerOutputter.cpp
blob: 8196a5f87c9e699d7ec33bea7100786abbfae1b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <cppunit/config/SourcePrefix.h>
#include <cppunit/Exception.h>
#include <cppunit/SourceLine.h>
#include <cppunit/TestFailure.h>
#include <cppunit/TestResultCollector.h>
#include <cppunit/CompilerOutputter.h>
#include <algorithm>
#include <cppunit/tools/StringTools.h>


CPPUNIT_NS_BEGIN


CompilerOutputter::CompilerOutputter( TestResultCollector *result,
                                      OStream &stream,
                                      const std::string &locationFormat )
    : m_result( result )
    , m_stream( stream )
    , m_locationFormat( locationFormat )
    , m_wrapColumn( CPPUNIT_WRAP_COLUMN )
{
}


CompilerOutputter::~CompilerOutputter()
{
}


void 
CompilerOutputter::setLocationFormat( const std::string &locationFormat )
{
  m_locationFormat = locationFormat;
}


CompilerOutputter *
CompilerOutputter::defaultOutputter( TestResultCollector *result,
                                     OStream &stream )
{
  return new CompilerOutputter( result, stream );
}


void 
CompilerOutputter::write()
{
  if ( m_result->wasSuccessful() )
    printSuccess();
  else
    printFailureReport();
}


void 
CompilerOutputter::printSuccess()
{
  m_stream  << "OK (" << m_result->runTests()  << ")\n";
}


void 
CompilerOutputter::printFailureReport()
{
  printFailuresList();
  printStatistics();
}


void 
CompilerOutputter::printFailuresList()
{
  for ( int index =0; index < m_result->testFailuresTotal(); ++index)
  {
    printFailureDetail( m_result->failures()[ index ] );
  }
}


void 
CompilerOutputter::printFailureDetail( TestFailure *failure )
{
  printFailureLocation( failure->sourceLine() );
  printFailureType( failure );
  printFailedTestName( failure );
  printFailureMessage( failure );
}

 
void 
CompilerOutputter::printFailureLocation( SourceLine sourceLine )
{
  if ( !sourceLine.isValid() )
  {
    m_stream  <<  "##Failure Location unknown## : ";
    return;
  }

  std::string location;
  for ( unsigned int index = 0; index < m_locationFormat.length(); ++index )
  {
    char c = m_locationFormat[ index ];
    if ( c == '%'  &&  ( index+1 < m_locationFormat.length() ) )
    {
      char command = m_locationFormat[index+1];
      if ( processLocationFormatCommand( command, sourceLine ) )
      {
        ++index;
        continue;
      }
    }

    m_stream  << c;
  }
}


bool 
CompilerOutputter::processLocationFormatCommand( char command, 
                                                 const SourceLine &sourceLine )
{
  switch ( command )
  {
  case 'p':
    m_stream  <<  sourceLine.fileName();
    return true;
  case 'l':
    m_stream  <<  sourceLine.lineNumber();
    return true;
  case 'f':
    m_stream  <<  extractBaseName( sourceLine.fileName() );
    return true;
  }
  
  return false;
}


std::string 
CompilerOutputter::extractBaseName( const std::string &fileName ) const
{
  int indexLastDirectorySeparator = fileName.find_last_of( '/' );
  
  if ( indexLastDirectorySeparator < 0 )
    indexLastDirectorySeparator = fileName.find_last_of( '\\' );
  
  if ( indexLastDirectorySeparator < 0 )
    return fileName;

  return fileName.substr( indexLastDirectorySeparator +1 );
}


void 
CompilerOutputter::printFailureType( TestFailure *failure )
{
  m_stream  <<  (failure->isError() ? "Error" : "Assertion");
}


void 
CompilerOutputter::printFailedTestName( TestFailure *failure )
{
  m_stream  <<  "\nTest name: "  <<  failure->failedTestName();
}


void 
CompilerOutputter::printFailureMessage( TestFailure *failure )
{
  m_stream  <<  "\n";
  Exception *thrownException = failure->thrownException();
  m_stream  << thrownException->message().shortDescription()  <<  "\n";

  std::string message = thrownException->message().details();
  if ( m_wrapColumn > 0 )
    message = StringTools::wrap( message, m_wrapColumn );

  m_stream  <<  message  <<  "\n";
}


void 
CompilerOutputter::printStatistics()
{
  m_stream  <<  "Failures !!!\n";
  m_stream  <<  "Run: "  <<  m_result->runTests()  << "   "
            <<  "Failure total: "  <<  m_result->testFailuresTotal()  << "   "
            <<  "Failures: "  <<  m_result->testFailures()  << "   "
            <<  "Errors: "  <<  m_result->testErrors()
            <<  "\n";
}


void 
CompilerOutputter::setWrapColumn( int wrapColumn )
{
  m_wrapColumn = wrapColumn;
}


void 
CompilerOutputter::setNoWrap()
{
  m_wrapColumn = 0;
}


int 
CompilerOutputter::wrapColumn() const
{
  return m_wrapColumn;
}


CPPUNIT_NS_END