From b6e100b94b12f3f92ec025dc2363eaf7c0ee6662 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 10 Apr 2007 16:02:07 +0000 Subject: Well, we've got the basis of a workable unit test harness thing. There should be a few more add-ons to it, but it works just fine, and eventually it should cover command line options and creating logs, and possibly even provide output functionality so that output from tests can be logged and kept track of well. --- build.conf | 16 +++++++++++++ src/fstring.h | 24 +++++++++++++++++++ src/ssocket.cpp | 4 ++-- src/unit/fstring.cpp | 28 ++++++++++++++++++++++ src/unit/sfile.cpp | 27 +++++++++++++++++++++ src/unitsuite.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/unitsuite.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 src/unit/fstring.cpp create mode 100644 src/unit/sfile.cpp create mode 100644 src/unitsuite.cpp create mode 100644 src/unitsuite.h diff --git a/build.conf b/build.conf index 5c4dbb6..bc186f9 100644 --- a/build.conf +++ b/build.conf @@ -30,6 +30,22 @@ filesIn("src/tests") filter regexp("^src/tests/(.*)\\.cpp$", "tests/{re:1}"): set "LDFLAGS" += "-L. -lbu++", input "src/{target}.cpp" +directoriesIn("src/unit","unit/"): + rule "exe", + target file, + requires "libbu++.a", + set "CXXFLAGS" += "-Isrc", + set "LDFLAGS" += "-L. -lbu++", + input filesIn("{fulldir}") filter regexp("^.*\\.cpp$") + +filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"): + rule "exe", + target file, + requires "libbu++.a", + set "CXXFLAGS" += "-Isrc", + set "LDFLAGS" += "-L. -lbu++", + input "src/{target}.cpp" + "tests/plugin": set "LDFLAGS" += "-ldl" rule "exe": diff --git a/src/fstring.h b/src/fstring.h index 0184301..751beb8 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -364,6 +364,30 @@ namespace Bu } } + long find( const char *sText ) + { + long nTLen = strlen( sText ); + flatten(); + for( long j = 0; j < pFirst->nLength-nTLen; j++ ) + { + if( !strncmp( sText, pFirst->pData+j, nTLen ) ) + return j; + } + return -1; + } + + long rfind( const char *sText ) + { + long nTLen = strlen( sText ); + flatten(); + for( long j = pFirst->nLength-nTLen-1; j >= 0; j-- ) + { + if( !strncmp( sText, pFirst->pData+j, nTLen ) ) + return j; + } + return -1; + } + void archive( class Archive &ar ) { if( ar.isLoading() ) diff --git a/src/ssocket.cpp b/src/ssocket.cpp index bdaac24..85a2da0 100644 --- a/src/ssocket.cpp +++ b/src/ssocket.cpp @@ -1,9 +1,9 @@ #include "ssocket.h" -SSocket::SSocket() +Bu::SSocket::SSocket() { } -SSocket::~SSocket() +Bu::SSocket::~SSocket() { } diff --git a/src/unit/fstring.cpp b/src/unit/fstring.cpp new file mode 100644 index 0000000..72755eb --- /dev/null +++ b/src/unit/fstring.cpp @@ -0,0 +1,28 @@ +#include "fstring.h" +#include "unitsuite.h" + +class Unit : public Bu::UnitSuite +{ +public: + Unit() + { + setName("FString"); + addTest( Unit::test1 ); + } + + virtual ~Unit() + { + } + + void test1() + { + unitTest( 1 == 1 ); + unitTest( 1 == 0 ); + } +}; + +int main( int argc, char *argv[] ) +{ + return Unit().run( argc, argv ); +} + diff --git a/src/unit/sfile.cpp b/src/unit/sfile.cpp new file mode 100644 index 0000000..7b19942 --- /dev/null +++ b/src/unit/sfile.cpp @@ -0,0 +1,27 @@ +#include "unitsuite.h" + +class Unit : public Bu::UnitSuite +{ +public: + Unit() + { + setName("SFile"); + addTest( Unit::test ); + } + + virtual ~Unit() { } + + // + // Tests go here + // + void test() + { + unitTest( 1 == 1 ); + } +}; + +int main( int argc, char *argv[] ) +{ + return Unit().run( argc, argv ); +} + diff --git a/src/unitsuite.cpp b/src/unitsuite.cpp new file mode 100644 index 0000000..2a28eb5 --- /dev/null +++ b/src/unitsuite.cpp @@ -0,0 +1,67 @@ +#include "unitsuite.h" + +Bu::UnitSuite::UnitSuite() +{ +} + +Bu::UnitSuite::~UnitSuite() +{ +} + +int Bu::UnitSuite::run( int argc, char *argv[] ) +{ + for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ ) + { + printf("%s: ", i->sName.getStr() ); + fflush( stdout ); + try + { + (this->*(i->fTest))(); + printf("passed.\n"); + } + catch( Failed &e ) + { + if( e.bFile ) + { + printf("unitTest(%s) failed. (%s:%d)\n", + e.str.getStr(), + e.sFile.getStr(), + e.nLine + ); + } + else + { + printf("unitTest(%s) failed.\n", + e.str.getStr() + ); + } + } + catch( ... ) + { + printf("failed with external exception.\n"); + } + } + + return 0; +} + +void Bu::UnitSuite::add( Test fTest, Bu::FString sName ) +{ + TestInfo ti; + ti.sName = sName; + long index = ti.sName.rfind("::"); + if( index != -1 ) + { + FString tmp = sSuiteName; + tmp += ti.sName.getStr()+index; + ti.sName = tmp; + } + ti.fTest = fTest; + lTests.push_back( ti ); +} + +void Bu::UnitSuite::setName( const FString &sName ) +{ + sSuiteName = sName; +} + diff --git a/src/unitsuite.h b/src/unitsuite.h new file mode 100644 index 0000000..3502a1b --- /dev/null +++ b/src/unitsuite.h @@ -0,0 +1,60 @@ +#ifndef UNIT_SUITE_H +#define UNIT_SUITE_H + +#include +#include +#include "fstring.h" + +namespace Bu +{ + /** + * + */ + class UnitSuite + { + public: + UnitSuite(); + virtual ~UnitSuite(); + + int run( int argc=0, char *argv[]=NULL ); + + typedef void (UnitSuite::*Test)(); + + class Failed + { + public: + Failed() : str(""), bFile( false ) { } + Failed( const FString &s ) : str( s ), bFile( false ) { } + Failed( const FString &s, const FString &sFile, int nLine ) : + str( s ), sFile( sFile ), nLine( nLine ), bFile( true ) { } + + FString str; + FString sFile; + int nLine; + bool bFile; + }; + + protected: + void add( Test fTest, Bu::FString sName ); + void setName( const FString &sName ); + + private: + typedef struct TestInfo + { + FString sName; + Test fTest; + } TestInfo; + + typedef std::list TestList; + TestList lTests; + FString sSuiteName; + }; +} + +#define addTest( fn ) add( static_cast(&fn), #fn ) +#define unitTest( tst ) if( !(tst) ) \ +{ \ + throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \ +} + +#endif -- cgit v1.2.3