Loading...
Searching...
No Matches
ErrorFormatter.h
Go to the documentation of this file.
#ifndef __cxxtest__ErrorFormatter_h__
#define __cxxtest__ErrorFormatter_h__
//
// The ErrorFormatter is a TestListener that
// prints reports of the errors to an output
// stream. Since we cannot rely ou the standard
// iostreams, this header defines a base class
// analogout to std::ostream.
//
#include <cxxtest/TestRunner.h>
#include <cxxtest/TestListener.h>
#include <cxxtest/TestTracker.h>
#include <cxxtest/ValueTraits.h>
namespace CxxTest
{
class OutputStream
{
public:
virtual ~OutputStream() {}
virtual void flush() {};
virtual OutputStream &operator<<( unsigned /*number*/ ) { return *this; }
virtual OutputStream &operator<<( const char * /*string*/ ) { return *this; }
typedef void (*Manipulator)( OutputStream & );
virtual OutputStream &operator<<( Manipulator m ) { m( *this ); return *this; }
static void endl( OutputStream &o ) { (o << "\n").flush(); }
};
class ErrorFormatter : public TestListener
{
public:
ErrorFormatter( OutputStream *o, const char *preLine = ":", const char *postLine = "" ) :
_dotting( true ),
_reported( false ),
_o(o),
_preLine(preLine),
_postLine(postLine)
{
}
int run()
{
TestRunner::runAllTests( *this );
return tracker().failedTests();
}
void enterWorld( const WorldDescription & /*desc*/ )
{
(*_o) << "Running " << totalTests;
_o->flush();
_dotting = true;
_reported = false;
}
static void totalTests( OutputStream &o )
{
char s[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
const WorldDescription &wd = tracker().world();
o << wd.strTotalTests( s ) << (wd.numTotalTests() == 1 ? " test" : " tests");
}
void enterSuite( const SuiteDescription & )
{
_reported = false;
}
void enterTest( const TestDescription & )
{
_reported = false;
}
void leaveTest( const TestDescription & )
{
if ( !tracker().testFailed() ) {
((*_o) << ".").flush();
_dotting = true;
}
}
void leaveWorld( const WorldDescription &desc )
{
if ( !tracker().failedTests() ) {
(*_o) << "OK!" << endl;
return;
}
newLine();
(*_o) << "Failed " << tracker().failedTests() << " of " << totalTests << endl;
unsigned numPassed = desc.numTotalTests() - tracker().failedTests();
(*_o) << "Success rate: " << (numPassed * 100 / desc.numTotalTests()) << "%" << endl;
}
void trace( const char *file, unsigned line, const char *expression )
{
stop( file, line ) << "Trace: " <<
expression << endl;
}
void warning( const char *file, unsigned line, const char *expression )
{
stop( file, line ) << "Warning: " <<
expression << endl;
}
void failedTest( const char *file, unsigned line, const char *expression )
{
stop( file, line ) << "Error: Test failed: " <<
expression << endl;
}
void failedAssert( const char *file, unsigned line, const char *expression )
{
stop( file, line ) << "Error: Assertion failed: " <<
expression << endl;
}
void failedAssertEquals( const char *file, unsigned line,
const char *xStr, const char *yStr,
const char *x, const char *y )
{
stop( file, line ) << "Error: Expected (" <<
xStr << " == " << yStr << "), found (" <<
x << " != " << y << ")" << endl;
}
void failedAssertSameData( const char *file, unsigned line,
const char *xStr, const char *yStr,
const char *sizeStr, const void *x,
const void *y, unsigned size )
{
stop( file, line ) << "Error: Expected " << sizeStr << " (" << size << ") bytes to be equal at (" <<
xStr << ") and (" << yStr << "), found:" << endl;
dump( x, size );
(*_o) << " differs from" << endl;
dump( y, size );
}
void failedAssertDelta( const char *file, unsigned line,
const char *xStr, const char *yStr, const char *dStr,
const char *x, const char *y, const char *d )
{
stop( file, line ) << "Error: Expected (" <<
xStr << " == " << yStr << ") up to " << dStr << " (" << d << "), found (" <<
x << " != " << y << ")" << endl;
}
void failedAssertDiffers( const char *file, unsigned line,
const char *xStr, const char *yStr,
const char *value )
{
stop( file, line ) << "Error: Expected (" <<
xStr << " != " << yStr << "), found (" <<
value << ")" << endl;
}
void failedAssertLessThan( const char *file, unsigned line,
const char *xStr, const char *yStr,
const char *x, const char *y )
{
stop( file, line ) << "Error: Expected (" <<
xStr << " < " << yStr << "), found (" <<
x << " >= " << y << ")" << endl;
}
void failedAssertLessThanEquals( const char *file, unsigned line,
const char *xStr, const char *yStr,
const char *x, const char *y )
{
stop( file, line ) << "Error: Expected (" <<
xStr << " <= " << yStr << "), found (" <<
x << " > " << y << ")" << endl;
}
void failedAssertRelation( const char *file, unsigned line,
const char *relation, const char *xStr, const char *yStr,
const char *x, const char *y )
{
stop( file, line ) << "Error: Expected " << relation << "( " <<
xStr << ", " << yStr << " ), found !" << relation << "( " << x << ", " << y << " )" << endl;
}
void failedAssertPredicate( const char *file, unsigned line,
const char *predicate, const char *xStr, const char *x )
{
stop( file, line ) << "Error: Expected " << predicate << "( " <<
xStr << " ), found !" << predicate << "( " << x << " )" << endl;
}
void failedAssertThrows( const char *file, unsigned line,
const char *expression, const char *type,
bool otherThrown )
{
stop( file, line ) << "Error: Expected (" << expression << ") to throw (" <<
type << ") but it " << (otherThrown ? "threw something else" : "didn't throw") <<
endl;
}
void failedAssertThrowsNot( const char *file, unsigned line, const char *expression )
{
stop( file, line ) << "Error: Expected (" << expression << ") not to throw, but it did" <<
endl;
}
protected:
OutputStream *outputStream() const
{
return _o;
}
private:
ErrorFormatter( const ErrorFormatter & );
ErrorFormatter &operator=( const ErrorFormatter & );
OutputStream &stop( const char *file, unsigned line )
{
newLine();
reportTest();
return (*_o) << file << _preLine << line << _postLine << ": ";
}
void newLine( void )
{
if ( _dotting ) {
(*_o) << endl;
_dotting = false;
}
}
void reportTest( void )
{
if( _reported )
return;
(*_o) << "In " << tracker().suite().suiteName() << "::" << tracker().test().testName() << ":" << endl;
_reported = true;
}
void dump( const void *buffer, unsigned size )
{
if ( !buffer )
dumpNull();
else
dumpBuffer( buffer, size );
}
void dumpNull()
{
(*_o) << " (null)" << endl;
}
void dumpBuffer( const void *buffer, unsigned size )
{
unsigned dumpSize = size;
if ( maxDumpSize() && dumpSize > maxDumpSize() )
dumpSize = maxDumpSize();
const unsigned char *p = (const unsigned char *)buffer;
(*_o) << " { ";
for ( unsigned i = 0; i < dumpSize; ++ i )
(*_o) << byteToHex( *p++ ) << " ";
if ( dumpSize < size )
(*_o) << "... ";
(*_o) << "}" << endl;
}
static void endl( OutputStream &o )
{
OutputStream::endl( o );
}
bool _dotting;
bool _reported;
OutputStream *_o;
const char *_preLine;
const char *_postLine;
};
};
#endif // __cxxtest__ErrorFormatter_h__
TestListener.h
TestRunner.h
TestTracker.h
ValueTraits.h
size
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition cf_ops.cc:600
m
int m
Definition cfEzgcd.cc:128
i
int i
Definition cfEzgcd.cc:132
x
Variable x
Definition cfModGcd.cc:4090
p
int p
Definition cfModGcd.cc:4086
false
return false
Definition cfModGcd.cc:85
CxxTest::ErrorFormatter::failedAssertRelation
void failedAssertRelation(const char *file, unsigned line, const char *relation, const char *xStr, const char *yStr, const char *x, const char *y)
Definition ErrorFormatter.h:177
CxxTest::ErrorFormatter::stop
OutputStream & stop(const char *file, unsigned line)
Definition ErrorFormatter.h:217
CxxTest::ErrorFormatter::warning
void warning(const char *file, unsigned line, const char *expression)
Definition ErrorFormatter.h:102
CxxTest::ErrorFormatter::failedAssertThrows
void failedAssertThrows(const char *file, unsigned line, const char *expression, const char *type, bool otherThrown)
Definition ErrorFormatter.h:192
CxxTest::ErrorFormatter::newLine
void newLine(void)
Definition ErrorFormatter.h:224
CxxTest::ErrorFormatter::operator=
ErrorFormatter & operator=(const ErrorFormatter &)
CxxTest::ErrorFormatter::_o
OutputStream * _o
Definition ErrorFormatter.h:275
CxxTest::ErrorFormatter::enterTest
void enterTest(const TestDescription &)
Definition ErrorFormatter.h:71
CxxTest::ErrorFormatter::trace
void trace(const char *file, unsigned line, const char *expression)
Definition ErrorFormatter.h:96
CxxTest::ErrorFormatter::leaveTest
void leaveTest(const TestDescription &)
Definition ErrorFormatter.h:76
CxxTest::ErrorFormatter::failedAssertDelta
void failedAssertDelta(const char *file, unsigned line, const char *xStr, const char *yStr, const char *dStr, const char *x, const char *y, const char *d)
Definition ErrorFormatter.h:141
CxxTest::ErrorFormatter::failedAssertLessThan
void failedAssertLessThan(const char *file, unsigned line, const char *xStr, const char *yStr, const char *x, const char *y)
Definition ErrorFormatter.h:159
CxxTest::ErrorFormatter::outputStream
OutputStream * outputStream() const
Definition ErrorFormatter.h:208
CxxTest::ErrorFormatter::failedTest
void failedTest(const char *file, unsigned line, const char *expression)
Definition ErrorFormatter.h:108
CxxTest::ErrorFormatter::leaveWorld
void leaveWorld(const WorldDescription &desc)
Definition ErrorFormatter.h:84
CxxTest::ErrorFormatter::_preLine
const char * _preLine
Definition ErrorFormatter.h:276
CxxTest::ErrorFormatter::enterSuite
void enterSuite(const SuiteDescription &)
Definition ErrorFormatter.h:66
CxxTest::ErrorFormatter::dumpNull
void dumpNull()
Definition ErrorFormatter.h:248
CxxTest::ErrorFormatter::failedAssertSameData
void failedAssertSameData(const char *file, unsigned line, const char *xStr, const char *yStr, const char *sizeStr, const void *x, const void *y, unsigned size)
Definition ErrorFormatter.h:129
CxxTest::ErrorFormatter::endl
static void endl(OutputStream &o)
Definition ErrorFormatter.h:268
CxxTest::ErrorFormatter::_reported
bool _reported
Definition ErrorFormatter.h:274
CxxTest::ErrorFormatter::run
int run()
Definition ErrorFormatter.h:45
CxxTest::ErrorFormatter::failedAssertEquals
void failedAssertEquals(const char *file, unsigned line, const char *xStr, const char *yStr, const char *x, const char *y)
Definition ErrorFormatter.h:120
CxxTest::ErrorFormatter::failedAssertPredicate
void failedAssertPredicate(const char *file, unsigned line, const char *predicate, const char *xStr, const char *x)
Definition ErrorFormatter.h:185
CxxTest::ErrorFormatter::totalTests
static void totalTests(OutputStream &o)
Definition ErrorFormatter.h:59
CxxTest::ErrorFormatter::dumpBuffer
void dumpBuffer(const void *buffer, unsigned size)
Definition ErrorFormatter.h:253
CxxTest::ErrorFormatter::ErrorFormatter
ErrorFormatter(OutputStream *o, const char *preLine=":", const char *postLine="")
Definition ErrorFormatter.h:36
CxxTest::ErrorFormatter::failedAssert
void failedAssert(const char *file, unsigned line, const char *expression)
Definition ErrorFormatter.h:114
CxxTest::ErrorFormatter::_postLine
const char * _postLine
Definition ErrorFormatter.h:277
CxxTest::ErrorFormatter::reportTest
void reportTest(void)
Definition ErrorFormatter.h:232
CxxTest::ErrorFormatter::failedAssertThrowsNot
void failedAssertThrowsNot(const char *file, unsigned line, const char *expression)
Definition ErrorFormatter.h:201
CxxTest::ErrorFormatter::failedAssertLessThanEquals
void failedAssertLessThanEquals(const char *file, unsigned line, const char *xStr, const char *yStr, const char *x, const char *y)
Definition ErrorFormatter.h:168
CxxTest::ErrorFormatter::dump
void dump(const void *buffer, unsigned size)
Definition ErrorFormatter.h:240
CxxTest::ErrorFormatter::enterWorld
void enterWorld(const WorldDescription &)
Definition ErrorFormatter.h:51
CxxTest::ErrorFormatter::failedAssertDiffers
void failedAssertDiffers(const char *file, unsigned line, const char *xStr, const char *yStr, const char *value)
Definition ErrorFormatter.h:150
CxxTest::ErrorFormatter::_dotting
bool _dotting
Definition ErrorFormatter.h:273
CxxTest::ErrorFormatter::ErrorFormatter
ErrorFormatter(const ErrorFormatter &)
CxxTest::OutputStream
Definition ErrorFormatter.h:20
CxxTest::OutputStream::flush
virtual void flush()
Definition ErrorFormatter.h:23
CxxTest::OutputStream::operator<<
virtual OutputStream & operator<<(unsigned)
Definition ErrorFormatter.h:24
CxxTest::OutputStream::operator<<
virtual OutputStream & operator<<(const char *)
Definition ErrorFormatter.h:25
CxxTest::OutputStream::~OutputStream
virtual ~OutputStream()
Definition ErrorFormatter.h:22
CxxTest::OutputStream::endl
static void endl(OutputStream &o)
Definition ErrorFormatter.h:30
CxxTest::OutputStream::operator<<
virtual OutputStream & operator<<(Manipulator m)
Definition ErrorFormatter.h:29
CxxTest::OutputStream::Manipulator
void(* Manipulator)(OutputStream &)
Definition ErrorFormatter.h:27
CxxTest::SuiteDescription
Definition Descriptions.h:32
CxxTest::SuiteDescription::suiteName
virtual const char * suiteName() const =0
CxxTest::TestDescription
Definition Descriptions.h:16
CxxTest::TestDescription::testName
virtual const char * testName() const =0
CxxTest::TestListener::TestListener
TestListener()
Definition TestListener.h:20
CxxTest::TestRunner::runAllTests
static void runAllTests(TestListener &listener)
Definition TestRunner.h:20
CxxTest::TestTracker::suite
const SuiteDescription & suite() const
Definition TestTracker.h:29
CxxTest::TestTracker::world
const WorldDescription & world() const
Definition TestTracker.h:30
CxxTest::TestTracker::failedTests
unsigned failedTests() const
Definition TestTracker.h:37
CxxTest::TestTracker::test
const TestDescription & test() const
Definition TestTracker.h:28
CxxTest::WorldDescription
Definition Descriptions.h:54
CxxTest::WorldDescription::MAX_STRLEN_TOTAL_TESTS
@ MAX_STRLEN_TOTAL_TESTS
Definition Descriptions.h:62
CxxTest::WorldDescription::strTotalTests
char * strTotalTests(char *) const
Definition Descriptions.cpp:16
CxxTest::WorldDescription::numTotalTests
virtual unsigned numTotalTests(void) const =0
y
const CanonicalForm int const CFList const Variable & y
Definition facAbsFact.cc:53
CxxTest
Definition Descriptions.cpp:7
CxxTest::maxDumpSize
unsigned maxDumpSize()
Definition TestSuite.cpp:43
CxxTest::byteToHex
const char * byteToHex(unsigned char byte)
Definition ValueTraits.cpp:21
CxxTest::s
char * s
Definition ValueTraits.h:143
CxxTest::tracker
TestTracker & tracker()
Definition TestTracker.h:111