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