~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to third-party/cppunit/cppunit/src/cppunit/CompilerOutputter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <cppunit/config/SourcePrefix.h>
2
 
#include <cppunit/Exception.h>
3
 
#include <cppunit/SourceLine.h>
4
 
#include <cppunit/TestFailure.h>
5
 
#include <cppunit/TestResultCollector.h>
6
 
#include <cppunit/CompilerOutputter.h>
7
 
#include <algorithm>
8
 
#include <cppunit/tools/StringTools.h>
9
 
 
10
 
 
11
 
CPPUNIT_NS_BEGIN
12
 
 
13
 
 
14
 
CompilerOutputter::CompilerOutputter( TestResultCollector *result,
15
 
                                      OStream &stream,
16
 
                                      const std::string &locationFormat )
17
 
    : m_result( result )
18
 
    , m_stream( stream )
19
 
    , m_locationFormat( locationFormat )
20
 
    , m_wrapColumn( CPPUNIT_WRAP_COLUMN )
21
 
{
22
 
}
23
 
 
24
 
 
25
 
CompilerOutputter::~CompilerOutputter()
26
 
{
27
 
}
28
 
 
29
 
 
30
 
void 
31
 
CompilerOutputter::setLocationFormat( const std::string &locationFormat )
32
 
{
33
 
  m_locationFormat = locationFormat;
34
 
}
35
 
 
36
 
 
37
 
CompilerOutputter *
38
 
CompilerOutputter::defaultOutputter( TestResultCollector *result,
39
 
                                     OStream &stream )
40
 
{
41
 
  return new CompilerOutputter( result, stream );
42
 
}
43
 
 
44
 
 
45
 
void 
46
 
CompilerOutputter::write()
47
 
{
48
 
  if ( m_result->wasSuccessful() )
49
 
    printSuccess();
50
 
  else
51
 
    printFailureReport();
52
 
}
53
 
 
54
 
 
55
 
void 
56
 
CompilerOutputter::printSuccess()
57
 
{
58
 
  m_stream  << "OK (" << m_result->runTests()  << ")\n";
59
 
}
60
 
 
61
 
 
62
 
void 
63
 
CompilerOutputter::printFailureReport()
64
 
{
65
 
  printFailuresList();
66
 
  printStatistics();
67
 
}
68
 
 
69
 
 
70
 
void 
71
 
CompilerOutputter::printFailuresList()
72
 
{
73
 
  for ( int index =0; index < m_result->testFailuresTotal(); ++index)
74
 
  {
75
 
    printFailureDetail( m_result->failures()[ index ] );
76
 
  }
77
 
}
78
 
 
79
 
 
80
 
void 
81
 
CompilerOutputter::printFailureDetail( TestFailure *failure )
82
 
{
83
 
  printFailureLocation( failure->sourceLine() );
84
 
  printFailureType( failure );
85
 
  printFailedTestName( failure );
86
 
  printFailureMessage( failure );
87
 
}
88
 
 
89
 
 
90
 
void 
91
 
CompilerOutputter::printFailureLocation( SourceLine sourceLine )
92
 
{
93
 
  if ( !sourceLine.isValid() )
94
 
  {
95
 
    m_stream  <<  "##Failure Location unknown## : ";
96
 
    return;
97
 
  }
98
 
 
99
 
  std::string location;
100
 
  for ( unsigned int index = 0; index < m_locationFormat.length(); ++index )
101
 
  {
102
 
    char c = m_locationFormat[ index ];
103
 
    if ( c == '%'  &&  ( index+1 < m_locationFormat.length() ) )
104
 
    {
105
 
      char command = m_locationFormat[index+1];
106
 
      if ( processLocationFormatCommand( command, sourceLine ) )
107
 
      {
108
 
        ++index;
109
 
        continue;
110
 
      }
111
 
    }
112
 
 
113
 
    m_stream  << c;
114
 
  }
115
 
}
116
 
 
117
 
 
118
 
bool 
119
 
CompilerOutputter::processLocationFormatCommand( char command, 
120
 
                                                 const SourceLine &sourceLine )
121
 
{
122
 
  switch ( command )
123
 
  {
124
 
  case 'p':
125
 
    m_stream  <<  sourceLine.fileName();
126
 
    return true;
127
 
  case 'l':
128
 
    m_stream  <<  sourceLine.lineNumber();
129
 
    return true;
130
 
  case 'f':
131
 
    m_stream  <<  extractBaseName( sourceLine.fileName() );
132
 
    return true;
133
 
  }
134
 
  
135
 
  return false;
136
 
}
137
 
 
138
 
 
139
 
std::string 
140
 
CompilerOutputter::extractBaseName( const std::string &fileName ) const
141
 
{
142
 
  int indexLastDirectorySeparator = fileName.find_last_of( '/' );
143
 
  
144
 
  if ( indexLastDirectorySeparator < 0 )
145
 
    indexLastDirectorySeparator = fileName.find_last_of( '\\' );
146
 
  
147
 
  if ( indexLastDirectorySeparator < 0 )
148
 
    return fileName;
149
 
 
150
 
  return fileName.substr( indexLastDirectorySeparator +1 );
151
 
}
152
 
 
153
 
 
154
 
void 
155
 
CompilerOutputter::printFailureType( TestFailure *failure )
156
 
{
157
 
  m_stream  <<  (failure->isError() ? "Error" : "Assertion");
158
 
}
159
 
 
160
 
 
161
 
void 
162
 
CompilerOutputter::printFailedTestName( TestFailure *failure )
163
 
{
164
 
  m_stream  <<  "\nTest name: "  <<  failure->failedTestName();
165
 
}
166
 
 
167
 
 
168
 
void 
169
 
CompilerOutputter::printFailureMessage( TestFailure *failure )
170
 
{
171
 
  m_stream  <<  "\n";
172
 
  Exception *thrownException = failure->thrownException();
173
 
  m_stream  << thrownException->message().shortDescription()  <<  "\n";
174
 
 
175
 
  std::string message = thrownException->message().details();
176
 
  if ( m_wrapColumn > 0 )
177
 
    message = StringTools::wrap( message, m_wrapColumn );
178
 
 
179
 
  m_stream  <<  message  <<  "\n";
180
 
}
181
 
 
182
 
 
183
 
void 
184
 
CompilerOutputter::printStatistics()
185
 
{
186
 
  m_stream  <<  "Failures !!!\n";
187
 
  m_stream  <<  "Run: "  <<  m_result->runTests()  << "   "
188
 
            <<  "Failure total: "  <<  m_result->testFailuresTotal()  << "   "
189
 
            <<  "Failures: "  <<  m_result->testFailures()  << "   "
190
 
            <<  "Errors: "  <<  m_result->testErrors()
191
 
            <<  "\n";
192
 
}
193
 
 
194
 
 
195
 
void 
196
 
CompilerOutputter::setWrapColumn( int wrapColumn )
197
 
{
198
 
  m_wrapColumn = wrapColumn;
199
 
}
200
 
 
201
 
 
202
 
void 
203
 
CompilerOutputter::setNoWrap()
204
 
{
205
 
  m_wrapColumn = 0;
206
 
}
207
 
 
208
 
 
209
 
int 
210
 
CompilerOutputter::wrapColumn() const
211
 
{
212
 
  return m_wrapColumn;
213
 
}
214
 
 
215
 
 
216
 
CPPUNIT_NS_END