diff options
author | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2012-03-25 20:00:08 +0000 |
commit | 469bbcf0701e1eb8a6670c23145b0da87357e178 (patch) | |
tree | b5b062a16e46a6c5d3410b4e574cd0cc09057211 /src/stable/unitsuite.h | |
parent | ee1b79396076edc4e30aefb285fada03bb45e80d (diff) | |
download | libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.gz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.bz2 libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.tar.xz libbu++-469bbcf0701e1eb8a6670c23145b0da87357e178.zip |
Code is all reorganized. We're about ready to release. I should write up a
little explenation of the arrangement.
Diffstat (limited to 'src/stable/unitsuite.h')
-rw-r--r-- | src/stable/unitsuite.h | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/stable/unitsuite.h b/src/stable/unitsuite.h new file mode 100644 index 0000000..2250a4d --- /dev/null +++ b/src/stable/unitsuite.h | |||
@@ -0,0 +1,139 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_UNIT_SUITE_H | ||
9 | #define BU_UNIT_SUITE_H | ||
10 | |||
11 | #include <stdint.h> | ||
12 | #include "bu/list.h" | ||
13 | #include "bu/string.h" | ||
14 | #include "bu/file.h" | ||
15 | #include "bu/array.h" | ||
16 | |||
17 | namespace Bu | ||
18 | { | ||
19 | /** | ||
20 | * Provides a unit testing framework. This is pretty easy to use, probably | ||
21 | * the best way to get started is to use ch to generate a template, or use | ||
22 | * the code below with appropriate tweaks: | ||
23 | *@code | ||
24 | * #include "unitsuite.h" | ||
25 | * | ||
26 | * class Unit : public Bu::UnitSuite | ||
27 | * { | ||
28 | * public: | ||
29 | * Unit() | ||
30 | * { | ||
31 | * setName("Example"); | ||
32 | * addTest( Unit::test ); | ||
33 | * } | ||
34 | * | ||
35 | * virtual ~Unit() { } | ||
36 | * | ||
37 | * // | ||
38 | * // Tests go here | ||
39 | * // | ||
40 | * void test() | ||
41 | * { | ||
42 | * unitTest( 1 == 1 ); | ||
43 | * } | ||
44 | * }; | ||
45 | * | ||
46 | * int main( int argc, char *argv[] ) | ||
47 | * { | ||
48 | * return Unit().run( argc, argv ); | ||
49 | * } | ||
50 | * | ||
51 | @endcode | ||
52 | * The main function can contain other things, but using this one exactly | ||
53 | * makes all of the test suites work exactly the same. Using the optional | ||
54 | * setName at the top of the constructor replaces the class name with the | ||
55 | * chosen name when printing out stats and info. | ||
56 | */ | ||
57 | class UnitSuite | ||
58 | { | ||
59 | public: | ||
60 | UnitSuite(); | ||
61 | UnitSuite( int iOptions ); | ||
62 | virtual ~UnitSuite(); | ||
63 | |||
64 | int run( int argc=0, char *argv[]=NULL ); | ||
65 | |||
66 | Bu::File tempFile( Bu::String &sFileName ); | ||
67 | |||
68 | typedef void (UnitSuite::*Test)(); | ||
69 | |||
70 | class Failed | ||
71 | { | ||
72 | public: | ||
73 | Failed() : str(""), bFile( false ) { } | ||
74 | Failed( const String &s ) : str( s ), bFile( false ) { } | ||
75 | Failed( const String &s, const String &sFile, int nLine ) : | ||
76 | str( s ), sFile( sFile ), nLine( nLine ), bFile( true ) { } | ||
77 | |||
78 | String str; | ||
79 | String sFile; | ||
80 | int nLine; | ||
81 | bool bFile; | ||
82 | }; | ||
83 | |||
84 | enum | ||
85 | { | ||
86 | optStopOnError = 0x000001 | ||
87 | }; | ||
88 | |||
89 | enum Expect | ||
90 | { | ||
91 | expectPass, | ||
92 | expectFail | ||
93 | }; | ||
94 | |||
95 | protected: | ||
96 | void add( Test fTest, const Bu::String &sName, Expect e=expectPass ); | ||
97 | void setName( const String &sName ); | ||
98 | |||
99 | void dispProgress(); | ||
100 | void setStepCount( int iSteps ); | ||
101 | void incProgress( int iAmnt = 1 ); | ||
102 | void setProgress( int iAmnt ); | ||
103 | |||
104 | private: | ||
105 | int onListCases( Bu::Array<Bu::String> aParam ); | ||
106 | |||
107 | private: | ||
108 | typedef struct TestInfo | ||
109 | { | ||
110 | String sName; | ||
111 | Test fTest; | ||
112 | Expect eExpect; | ||
113 | } TestInfo; | ||
114 | |||
115 | typedef Bu::List<TestInfo> TestList; | ||
116 | TestList lTests; | ||
117 | String sSuiteName; | ||
118 | |||
119 | int iOptions; | ||
120 | |||
121 | typedef Bu::List<Bu::String> StrList; | ||
122 | StrList lFileCleanup; | ||
123 | int iNameWidth; | ||
124 | int iStepCount; | ||
125 | int iProgress; | ||
126 | time_t tLastUpdate; | ||
127 | }; | ||
128 | |||
129 | Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::UnitSuite::Expect &e ); | ||
130 | } | ||
131 | |||
132 | #define addTest( fn ) add( static_cast<Bu::UnitSuite::Test>(&fn), #fn ) | ||
133 | #define unitTest( tst ) if( !(tst) ) \ | ||
134 | { \ | ||
135 | throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \ | ||
136 | } else (void)0 | ||
137 | #define unitFailed( msg ) throw Bu::UnitSuite::Failed(msg, __FILE__, __LINE__) | ||
138 | |||
139 | #endif | ||