diff options
Diffstat (limited to '')
-rw-r--r-- | src/unit/fstring.cpp | 28 | ||||
-rw-r--r-- | src/unit/sfile.cpp | 27 | ||||
-rw-r--r-- | src/unitsuite.cpp | 67 | ||||
-rw-r--r-- | src/unitsuite.h | 60 |
4 files changed, 182 insertions, 0 deletions
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 @@ | |||
1 | #include "fstring.h" | ||
2 | #include "unitsuite.h" | ||
3 | |||
4 | class Unit : public Bu::UnitSuite | ||
5 | { | ||
6 | public: | ||
7 | Unit() | ||
8 | { | ||
9 | setName("FString"); | ||
10 | addTest( Unit::test1 ); | ||
11 | } | ||
12 | |||
13 | virtual ~Unit() | ||
14 | { | ||
15 | } | ||
16 | |||
17 | void test1() | ||
18 | { | ||
19 | unitTest( 1 == 1 ); | ||
20 | unitTest( 1 == 0 ); | ||
21 | } | ||
22 | }; | ||
23 | |||
24 | int main( int argc, char *argv[] ) | ||
25 | { | ||
26 | return Unit().run( argc, argv ); | ||
27 | } | ||
28 | |||
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 @@ | |||
1 | #include "unitsuite.h" | ||
2 | |||
3 | class Unit : public Bu::UnitSuite | ||
4 | { | ||
5 | public: | ||
6 | Unit() | ||
7 | { | ||
8 | setName("SFile"); | ||
9 | addTest( Unit::test ); | ||
10 | } | ||
11 | |||
12 | virtual ~Unit() { } | ||
13 | |||
14 | // | ||
15 | // Tests go here | ||
16 | // | ||
17 | void test() | ||
18 | { | ||
19 | unitTest( 1 == 1 ); | ||
20 | } | ||
21 | }; | ||
22 | |||
23 | int main( int argc, char *argv[] ) | ||
24 | { | ||
25 | return Unit().run( argc, argv ); | ||
26 | } | ||
27 | |||
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 @@ | |||
1 | #include "unitsuite.h" | ||
2 | |||
3 | Bu::UnitSuite::UnitSuite() | ||
4 | { | ||
5 | } | ||
6 | |||
7 | Bu::UnitSuite::~UnitSuite() | ||
8 | { | ||
9 | } | ||
10 | |||
11 | int Bu::UnitSuite::run( int argc, char *argv[] ) | ||
12 | { | ||
13 | for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ ) | ||
14 | { | ||
15 | printf("%s: ", i->sName.getStr() ); | ||
16 | fflush( stdout ); | ||
17 | try | ||
18 | { | ||
19 | (this->*(i->fTest))(); | ||
20 | printf("passed.\n"); | ||
21 | } | ||
22 | catch( Failed &e ) | ||
23 | { | ||
24 | if( e.bFile ) | ||
25 | { | ||
26 | printf("unitTest(%s) failed. (%s:%d)\n", | ||
27 | e.str.getStr(), | ||
28 | e.sFile.getStr(), | ||
29 | e.nLine | ||
30 | ); | ||
31 | } | ||
32 | else | ||
33 | { | ||
34 | printf("unitTest(%s) failed.\n", | ||
35 | e.str.getStr() | ||
36 | ); | ||
37 | } | ||
38 | } | ||
39 | catch( ... ) | ||
40 | { | ||
41 | printf("failed with external exception.\n"); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | void Bu::UnitSuite::add( Test fTest, Bu::FString sName ) | ||
49 | { | ||
50 | TestInfo ti; | ||
51 | ti.sName = sName; | ||
52 | long index = ti.sName.rfind("::"); | ||
53 | if( index != -1 ) | ||
54 | { | ||
55 | FString tmp = sSuiteName; | ||
56 | tmp += ti.sName.getStr()+index; | ||
57 | ti.sName = tmp; | ||
58 | } | ||
59 | ti.fTest = fTest; | ||
60 | lTests.push_back( ti ); | ||
61 | } | ||
62 | |||
63 | void Bu::UnitSuite::setName( const FString &sName ) | ||
64 | { | ||
65 | sSuiteName = sName; | ||
66 | } | ||
67 | |||
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 @@ | |||
1 | #ifndef UNIT_SUITE_H | ||
2 | #define UNIT_SUITE_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | #include <list> | ||
6 | #include "fstring.h" | ||
7 | |||
8 | namespace Bu | ||
9 | { | ||
10 | /** | ||
11 | * | ||
12 | */ | ||
13 | class UnitSuite | ||
14 | { | ||
15 | public: | ||
16 | UnitSuite(); | ||
17 | virtual ~UnitSuite(); | ||
18 | |||
19 | int run( int argc=0, char *argv[]=NULL ); | ||
20 | |||
21 | typedef void (UnitSuite::*Test)(); | ||
22 | |||
23 | class Failed | ||
24 | { | ||
25 | public: | ||
26 | Failed() : str(""), bFile( false ) { } | ||
27 | Failed( const FString &s ) : str( s ), bFile( false ) { } | ||
28 | Failed( const FString &s, const FString &sFile, int nLine ) : | ||
29 | str( s ), sFile( sFile ), nLine( nLine ), bFile( true ) { } | ||
30 | |||
31 | FString str; | ||
32 | FString sFile; | ||
33 | int nLine; | ||
34 | bool bFile; | ||
35 | }; | ||
36 | |||
37 | protected: | ||
38 | void add( Test fTest, Bu::FString sName ); | ||
39 | void setName( const FString &sName ); | ||
40 | |||
41 | private: | ||
42 | typedef struct TestInfo | ||
43 | { | ||
44 | FString sName; | ||
45 | Test fTest; | ||
46 | } TestInfo; | ||
47 | |||
48 | typedef std::list<TestInfo> TestList; | ||
49 | TestList lTests; | ||
50 | FString sSuiteName; | ||
51 | }; | ||
52 | } | ||
53 | |||
54 | #define addTest( fn ) add( static_cast<Bu::UnitSuite::Test>(&fn), #fn ) | ||
55 | #define unitTest( tst ) if( !(tst) ) \ | ||
56 | { \ | ||
57 | throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \ | ||
58 | } | ||
59 | |||
60 | #endif | ||