aboutsummaryrefslogtreecommitdiff
path: root/src/stable/unitsuite.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/unitsuite.cpp')
-rw-r--r--src/stable/unitsuite.cpp394
1 files changed, 197 insertions, 197 deletions
diff --git a/src/stable/unitsuite.cpp b/src/stable/unitsuite.cpp
index b6057d0..e0606e6 100644
--- a/src/stable/unitsuite.cpp
+++ b/src/stable/unitsuite.cpp
@@ -17,14 +17,14 @@ using namespace Bu;
17#include <unistd.h> 17#include <unistd.h>
18 18
19Bu::UnitSuite::UnitSuite() : 19Bu::UnitSuite::UnitSuite() :
20 iOptions( 0 ), 20 iOptions( 0 ),
21 iNameWidth( 0 ) 21 iNameWidth( 0 )
22{ 22{
23} 23}
24 24
25Bu::UnitSuite::UnitSuite( int iOptions ) : 25Bu::UnitSuite::UnitSuite( int iOptions ) :
26 iOptions( iOptions ), 26 iOptions( iOptions ),
27 iNameWidth( 0 ) 27 iNameWidth( 0 )
28{ 28{
29} 29}
30 30
@@ -34,240 +34,240 @@ Bu::UnitSuite::~UnitSuite()
34 34
35int Bu::UnitSuite::run( int argc, char *argv[] ) 35int Bu::UnitSuite::run( int argc, char *argv[] )
36{ 36{
37 bool bCleanup = true; 37 bool bCleanup = true;
38 OptParser p; 38 OptParser p;
39 p.addOption( Bu::slot( this, &Bu::UnitSuite::onListCases ), 'l', "list", 39 p.addOption( Bu::slot( this, &Bu::UnitSuite::onListCases ), 'l', "list",
40 "List available test cases." ); 40 "List available test cases." );
41 p.addOption( bCleanup, "no-cleanup", "Don't erase temp files."); 41 p.addOption( bCleanup, "no-cleanup", "Don't erase temp files.");
42 p.setOverride( "no-cleanup", false ); 42 p.setOverride( "no-cleanup", false );
43 p.setNonOption( Bu::slot( this, &Bu::UnitSuite::onAddTest ) ); 43 p.setNonOption( Bu::slot( this, &Bu::UnitSuite::onAddTest ) );
44 p.addHelpOption(); 44 p.addHelpOption();
45 p.parse( argc, argv ); 45 p.parse( argc, argv );
46 46
47 if( !hSelTests.isEmpty() ) 47 if( !hSelTests.isEmpty() )
48 { 48 {
49 TestList lSub; 49 TestList lSub;
50 for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ ) 50 for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ )
51 { 51 {
52 if( hSelTests.has( (*i).sName ) ) 52 if( hSelTests.has( (*i).sName ) )
53 lSub.append( *i ); 53 lSub.append( *i );
54 } 54 }
55 55
56 lTests = lSub; 56 lTests = lSub;
57 } 57 }
58 58
59 int iEPass = 0; 59 int iEPass = 0;
60 int iEFail = 0; 60 int iEFail = 0;
61 int iUPass = 0; 61 int iUPass = 0;
62 int iUFail = 0; 62 int iUFail = 0;
63 for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ ) 63 for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ )
64 { 64 {
65 sio << Fmt( iNameWidth+3, Fmt::Left ).fill('.') << i->sName 65 sio << Fmt( iNameWidth+3, Fmt::Left ).fill('.') << i->sName
66 << sio.flush; 66 << sio.flush;
67 try 67 try
68 { 68 {
69 iStepCount = -1; 69 iStepCount = -1;
70 iProgress = 0; 70 iProgress = 0;
71 (this->*(i->fTest))(); 71 (this->*(i->fTest))();
72 switch( i->eExpect ) 72 switch( i->eExpect )
73 { 73 {
74 case expectPass: 74 case expectPass:
75 sio << "pass." << sio.nl; 75 sio << "pass." << sio.nl;
76 iEPass++; 76 iEPass++;
77 break; 77 break;
78 78
79 case expectFail: 79 case expectFail:
80 sio << "unexpected pass." << sio.nl; 80 sio << "unexpected pass." << sio.nl;
81 iUPass++; 81 iUPass++;
82 break; 82 break;
83 } 83 }
84 } 84 }
85 catch( Failed &e ) 85 catch( Failed &e )
86 { 86 {
87 switch( i->eExpect ) 87 switch( i->eExpect )
88 { 88 {
89 case expectPass: 89 case expectPass:
90 sio << "unexpected "; 90 sio << "unexpected ";
91 iUFail++; 91 iUFail++;
92 break; 92 break;
93 93
94 case expectFail: 94 case expectFail:
95 sio << "expected "; 95 sio << "expected ";
96 iEFail++; 96 iEFail++;
97 break; 97 break;
98 } 98 }
99 if( e.bFile ) 99 if( e.bFile )
100 { 100 {
101 sio << "fail in unitTest(" << e.str << "). (" << e.sFile 101 sio << "fail in unitTest(" << e.str << "). (" << e.sFile
102 << ":" << e.nLine << ")." << sio.nl; 102 << ":" << e.nLine << ")." << sio.nl;
103 } 103 }
104 else 104 else
105 { 105 {
106 sio << "fail in unitTest(" << e.str << ")." << sio.nl; 106 sio << "fail in unitTest(" << e.str << ")." << sio.nl;
107 } 107 }
108 108
109 if( (iOptions & optStopOnError) ) 109 if( (iOptions & optStopOnError) )
110 return 0; 110 return 0;
111 } 111 }
112 catch( std::exception &e ) 112 catch( std::exception &e )
113 { 113 {
114 switch( i->eExpect ) 114 switch( i->eExpect )
115 { 115 {
116 case expectPass: 116 case expectPass:
117 sio << "unexpected "; 117 sio << "unexpected ";
118 iUFail++; 118 iUFail++;
119 break; 119 break;
120 120
121 case expectFail: 121 case expectFail:
122 sio << "expected "; 122 sio << "expected ";
123 iEFail++; 123 iEFail++;
124 break; 124 break;
125 } 125 }
126 sio << "fail with unknown exception. what: " << e.what() << sio.nl; 126 sio << "fail with unknown exception. what: " << e.what() << sio.nl;
127 127
128 if( (iOptions & optStopOnError) ) 128 if( (iOptions & optStopOnError) )
129 return 0; 129 return 0;
130 } 130 }
131 catch( ... ) 131 catch( ... )
132 { 132 {
133 switch( i->eExpect ) 133 switch( i->eExpect )
134 { 134 {
135 case expectPass: 135 case expectPass:
136 sio << "unexpected "; 136 sio << "unexpected ";
137 iUFail++; 137 iUFail++;
138 break; 138 break;
139 139
140 case expectFail: 140 case expectFail:
141 sio << "expected "; 141 sio << "expected ";
142 iEFail++; 142 iEFail++;
143 break; 143 break;
144 } 144 }
145 sio << "fail with external exception." << sio.nl; 145 sio << "fail with external exception." << sio.nl;
146 146
147 if( (iOptions & optStopOnError) ) 147 if( (iOptions & optStopOnError) )
148 return 0; 148 return 0;
149 } 149 }
150 } 150 }
151 151
152 sio << sio.nl 152 sio << sio.nl
153 << "Report:" << sio.nl 153 << "Report:" << sio.nl
154 << "\tTotal tests run: " << lTests.getSize() << sio.nl 154 << "\tTotal tests run: " << lTests.getSize() << sio.nl
155 << "\tExpected passes: " << iEPass << sio.nl 155 << "\tExpected passes: " << iEPass << sio.nl
156 << "\tExpected failures: " << iEFail << sio.nl 156 << "\tExpected failures: " << iEFail << sio.nl
157 << "\tUnexpected passes: " << iUPass << sio.nl 157 << "\tUnexpected passes: " << iUPass << sio.nl
158 << "\tUnexpected failures: " << iUFail << sio.nl << sio.nl; 158 << "\tUnexpected failures: " << iUFail << sio.nl << sio.nl;
159 if( iUPass == 0 && iUFail == 0 ) 159 if( iUPass == 0 && iUFail == 0 )
160 sio << "\tNothing unexpected." << sio.nl << sio.nl; 160 sio << "\tNothing unexpected." << sio.nl << sio.nl;
161 161
162 if( bCleanup ) 162 if( bCleanup )
163 { 163 {
164 for( StrList::iterator i = lFileCleanup.begin(); i; i++ ) 164 for( StrList::iterator i = lFileCleanup.begin(); i; i++ )
165 { 165 {
166 unlink( (*i).getStr() ); 166 unlink( (*i).getStr() );
167 } 167 }
168 } 168 }
169 169
170 return 0; 170 return 0;
171} 171}
172 172
173Bu::File Bu::UnitSuite::tempFile( Bu::String &sFileName ) 173Bu::File Bu::UnitSuite::tempFile( Bu::String &sFileName )
174{ 174{
175 Bu::File f = Bu::File::tempFile( sFileName ); 175 Bu::File f = Bu::File::tempFile( sFileName );
176 lFileCleanup.append( sFileName ); 176 lFileCleanup.append( sFileName );
177 return f; 177 return f;
178} 178}
179 179
180void Bu::UnitSuite::add( Test fTest, const Bu::String &sName, Expect e ) 180void Bu::UnitSuite::add( Test fTest, const Bu::String &sName, Expect e )
181{ 181{
182 TestInfo ti; 182 TestInfo ti;
183 ti.sName = sName; 183 ti.sName = sName;
184 ti.eExpect = e; 184 ti.eExpect = e;
185 long index = ti.sName.rfindIdx("::"); 185 long index = ti.sName.rfindIdx("::");
186 if( index != -1 ) 186 if( index != -1 )
187 { 187 {
188 String tmp = sSuiteName; 188 String tmp = sSuiteName;
189 tmp += ti.sName.getStr()+index; 189 tmp += ti.sName.getStr()+index;
190 ti.sName = tmp; 190 ti.sName = tmp;
191 } 191 }
192 ti.fTest = fTest; 192 ti.fTest = fTest;
193 lTests.append( ti ); 193 lTests.append( ti );
194 if( iNameWidth < ti.sName.getSize() ) 194 if( iNameWidth < ti.sName.getSize() )
195 iNameWidth = ti.sName.getSize(); 195 iNameWidth = ti.sName.getSize();
196} 196}
197 197
198void Bu::UnitSuite::setName( const String &sName ) 198void Bu::UnitSuite::setName( const String &sName )
199{ 199{
200 sSuiteName = sName; 200 sSuiteName = sName;
201} 201}
202 202
203void Bu::UnitSuite::dispProgress() 203void Bu::UnitSuite::dispProgress()
204{ 204{
205 if( tLastUpdate == time( NULL ) ) 205 if( tLastUpdate == time( NULL ) )
206 return; 206 return;
207 sio << Fmt(3) << (iProgress*100/iStepCount) << "%" << "\b\b\b\b" 207 sio << Fmt(3) << (iProgress*100/iStepCount) << "%" << "\b\b\b\b"
208 << sio.flush; 208 << sio.flush;
209 tLastUpdate = time( NULL ); 209 tLastUpdate = time( NULL );
210} 210}
211 211
212void Bu::UnitSuite::setStepCount( int iSteps ) 212void Bu::UnitSuite::setStepCount( int iSteps )
213{ 213{
214 iStepCount = iSteps; 214 iStepCount = iSteps;
215 if( iStepCount < 0 ) 215 if( iStepCount < 0 )
216 return; 216 return;
217 tLastUpdate = 0; 217 tLastUpdate = 0;
218 dispProgress(); 218 dispProgress();
219} 219}
220 220
221void Bu::UnitSuite::incProgress( int iAmnt ) 221void Bu::UnitSuite::incProgress( int iAmnt )
222{ 222{
223 iProgress += iAmnt; 223 iProgress += iAmnt;
224 if( iProgress < 0 ) 224 if( iProgress < 0 )
225 iProgress = 0; 225 iProgress = 0;
226 if( iProgress > iStepCount ) 226 if( iProgress > iStepCount )
227 iProgress = iStepCount; 227 iProgress = iStepCount;
228 dispProgress(); 228 dispProgress();
229} 229}
230 230
231void Bu::UnitSuite::setProgress( int iAmnt ) 231void Bu::UnitSuite::setProgress( int iAmnt )
232{ 232{
233 iProgress = iAmnt; 233 iProgress = iAmnt;
234 if( iProgress < 0 ) 234 if( iProgress < 0 )
235 iProgress = 0; 235 iProgress = 0;
236 if( iProgress > iStepCount ) 236 if( iProgress > iStepCount )
237 iProgress = iStepCount; 237 iProgress = iStepCount;
238 dispProgress(); 238 dispProgress();
239} 239}
240 240
241int Bu::UnitSuite::onListCases( StrArray ) 241int Bu::UnitSuite::onListCases( StrArray )
242{ 242{
243 sio << "Test cases:" << sio.nl; 243 sio << "Test cases:" << sio.nl;
244 for( TestList::iterator i = lTests.begin(); i; i++ ) 244 for( TestList::iterator i = lTests.begin(); i; i++ )
245 { 245 {
246 sio << "\t- " << Fmt( iNameWidth, 10, Fmt::Left ) << (*i).sName << " " 246 sio << "\t- " << Fmt( iNameWidth, 10, Fmt::Left ) << (*i).sName << " "
247 << (*i).eExpect << sio.nl; 247 << (*i).eExpect << sio.nl;
248 } 248 }
249 sio << sio.nl; 249 sio << sio.nl;
250 exit( 0 ); 250 exit( 0 );
251 return 0; 251 return 0;
252} 252}
253 253
254int Bu::UnitSuite::onAddTest( StrArray aParam ) 254int Bu::UnitSuite::onAddTest( StrArray aParam )
255{ 255{
256 hSelTests.insert( aParam[0], true ); 256 hSelTests.insert( aParam[0], true );
257 return 0; 257 return 0;
258} 258}
259 259
260Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::UnitSuite::Expect &e ) 260Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::UnitSuite::Expect &e )
261{ 261{
262 switch( e ) 262 switch( e )
263 { 263 {
264 case Bu::UnitSuite::expectPass: 264 case Bu::UnitSuite::expectPass:
265 return f << "expect pass"; 265 return f << "expect pass";
266 266
267 case Bu::UnitSuite::expectFail: 267 case Bu::UnitSuite::expectFail:
268 return f << "expect fail"; 268 return f << "expect fail";
269 } 269 }
270 270
271 return f << "**error**"; 271 return f << "**error**";
272} 272}
273 273