diff options
Diffstat (limited to '')
-rw-r--r-- | src/unitsuite.h | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/unitsuite.h b/src/unitsuite.h new file mode 100644 index 0000000..578b4cc --- /dev/null +++ b/src/unitsuite.h | |||
@@ -0,0 +1,96 @@ | |||
1 | #ifndef BU_UNIT_SUITE_H | ||
2 | #define BU_UNIT_SUITE_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | #include <list> | ||
6 | #include "fstring.h" | ||
7 | |||
8 | namespace Bu | ||
9 | { | ||
10 | /** | ||
11 | * Provides a unit testing framework. This is pretty easy to use, probably | ||
12 | * the best way to get started is to use ch to generate a template, or use | ||
13 | * the code below with appropriate tweaks: | ||
14 | *@code | ||
15 | * #include "unitsuite.h" | ||
16 | * | ||
17 | * class Unit : public Bu::UnitSuite | ||
18 | * { | ||
19 | * public: | ||
20 | * Unit() | ||
21 | * { | ||
22 | * setName("Example"); | ||
23 | * addTest( Unit::test ); | ||
24 | * } | ||
25 | * | ||
26 | * virtual ~Unit() { } | ||
27 | * | ||
28 | * // | ||
29 | * // Tests go here | ||
30 | * // | ||
31 | * void test() | ||
32 | * { | ||
33 | * unitTest( 1 == 1 ); | ||
34 | * } | ||
35 | * }; | ||
36 | * | ||
37 | * int main( int argc, char *argv[] ) | ||
38 | * { | ||
39 | * return Unit().run( argc, argv ); | ||
40 | * } | ||
41 | * | ||
42 | @endcode | ||
43 | * The main function can contain other things, but using this one exactly | ||
44 | * makes all of the test suites work exactly the same. Using the optional | ||
45 | * setName at the top of the constructor replaces the class name with the | ||
46 | * chosen name when printing out stats and info. | ||
47 | */ | ||
48 | class UnitSuite | ||
49 | { | ||
50 | public: | ||
51 | UnitSuite(); | ||
52 | virtual ~UnitSuite(); | ||
53 | |||
54 | int run( int argc=0, char *argv[]=NULL ); | ||
55 | |||
56 | typedef void (UnitSuite::*Test)(); | ||
57 | |||
58 | class Failed | ||
59 | { | ||
60 | public: | ||
61 | Failed() : str(""), bFile( false ) { } | ||
62 | Failed( const FString &s ) : str( s ), bFile( false ) { } | ||
63 | Failed( const FString &s, const FString &sFile, int nLine ) : | ||
64 | str( s ), sFile( sFile ), nLine( nLine ), bFile( true ) { } | ||
65 | |||
66 | FString str; | ||
67 | FString sFile; | ||
68 | int nLine; | ||
69 | bool bFile; | ||
70 | }; | ||
71 | |||
72 | protected: | ||
73 | void add( Test fTest, Bu::FString sName ); | ||
74 | void setName( const FString &sName ); | ||
75 | |||
76 | private: | ||
77 | typedef struct TestInfo | ||
78 | { | ||
79 | FString sName; | ||
80 | Test fTest; | ||
81 | } TestInfo; | ||
82 | |||
83 | typedef std::list<TestInfo> TestList; | ||
84 | TestList lTests; | ||
85 | FString sSuiteName; | ||
86 | }; | ||
87 | } | ||
88 | |||
89 | #define addTest( fn ) add( static_cast<Bu::UnitSuite::Test>(&fn), #fn ) | ||
90 | #define unitTest( tst ) if( !(tst) ) \ | ||
91 | { \ | ||
92 | throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \ | ||
93 | } | ||
94 | #define unitFailed( msg ) throw Bu::UnitSuite::Failed(msg, __FILE__, __LINE__); | ||
95 | |||
96 | #endif | ||