testing: minor refactoring so better error messages are shown

This commit is contained in:
Daniel Marjamäki 2008-12-01 18:52:03 +00:00
parent e968b42c38
commit bbd21613f7
2 changed files with 23 additions and 6 deletions

View File

@ -77,11 +77,27 @@ bool TestFixture::runTest(const char testname[])
return true;
}
void TestFixture::assertFail(const char *filename, int linenr)
{
errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl;
void TestFixture::assertEquals(const char *filename, int linenr, const std::string &expected, const std::string &actual)
{
if ( expected != actual )
{
errmsg << "Assertion failed in " << filename << " at line " << linenr << std::endl
<< "Expected:" << std::endl
<< expected << std::endl
<< "Actual:" << std::endl
<< actual << std::endl;
}
}
void TestFixture::assertEquals(const char *filename, int linenr, unsigned int expected, unsigned int actual)
{
std::ostringstream ostr1;
ostr1 << expected;
std::ostringstream ostr2;
ostr2 << actual;
assertEquals( filename, linenr, ostr1.str(), ostr2.str() );
}
void TestFixture::printTests()
{
const std::list<TestFixture *> &tests = TestRegistry::theInstance().tests();

View File

@ -37,12 +37,13 @@ protected:
{ }
bool runTest(const char testname[]);
void assertFail(const char *filename, int linenr);
void assertEquals(const char *filename, int linenr, const std::string &expected, const std::string &actual);
void assertEquals(const char *filename, int linenr, unsigned int expected, unsigned int actual);
public:
virtual void reportErr( const std::string &errmsg);
virtual void reportOut( const std::string &outmsg);
virtual void reportOut( const std::string &outmsg);
TestFixture(const std::string &_name);
virtual ~TestFixture() { }
@ -53,6 +54,6 @@ public:
#define TEST_CASE( NAME ) if ( runTest(#NAME) ) NAME ();
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) if (EXPECTED!=ACTUAL) assertFail(__FILE__, __LINE__);
#define ASSERT_EQUALS( EXPECTED , ACTUAL ) assertEquals(__FILE__, __LINE__, EXPECTED, ACTUAL);
#define REGISTER_TEST( CLASSNAME ) namespace { CLASSNAME instance; }