aboutsummaryrefslogtreecommitdiff
path: root/src/stable/unitsuite.cpp
blob: e508c6fe62031dc8c3871e646133e4cc461e1f35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/unitsuite.h"
#include "bu/file.h"
#include "bu/sio.h"
#include "bu/optparser.h"
#include <stdlib.h>
#include <time.h>

using namespace Bu;

#include <unistd.h>

Bu::UnitSuite::UnitSuite() :
	iOptions( 0 ),
	iNameWidth( 0 )
{
}

Bu::UnitSuite::UnitSuite( int iOptions ) :
	iOptions( iOptions ),
	iNameWidth( 0 )
{
}

Bu::UnitSuite::~UnitSuite()
{
}

int Bu::UnitSuite::run( int argc, char *argv[] )
{
	bool bCleanup = true;
	OptParser p;
	p.addOption( Bu::slot( this, &Bu::UnitSuite::onListCases ), 'l', "list",
			"List available test cases." );
	p.addOption( bCleanup, "no-cleanup", "Don't erase temp files.");
	p.setOverride( "no-cleanup", false );
	p.setNonOption( Bu::slot( this, &Bu::UnitSuite::onAddTest ) );
	p.addHelpOption();
	p.parse( argc, argv );

	if( !hSelTests.isEmpty() )
	{
		TestList lSub;
		for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ )
		{
			if( hSelTests.has( (*i).sName ) )
				lSub.append( *i );
		}

		lTests = lSub;
	}

	int iEPass = 0;
	int iEFail = 0;
	int iUPass = 0;
	int iUFail = 0;
	for( TestList::iterator i = lTests.begin(); i != lTests.end(); i++ )
	{
		sio << Fmt( iNameWidth+3, Fmt::Left ).fill('.') << i->sName
			<< sio.flush;
		try
		{
			iStepCount = -1;
			iProgress = 0;
			(this->*(i->fTest))();
			switch( i->eExpect )
			{
				case expectPass:
					sio << "pass." << sio.nl;
					iEPass++;
					break;

				case expectFail:
					sio << "unexpected pass." << sio.nl;
					iUPass++;
					break;
			}
		}
		catch( Failed &e )
		{
			switch( i->eExpect )
			{
				case expectPass:
					sio << "unexpected ";
					iUFail++;
					break;

				case expectFail:
					sio << "expected ";
					iEFail++;
					break;
			}
			if( e.bFile )
			{
				sio << "fail in unitTest(" << e.str << "). (" << e.sFile
					<< ":" << e.nLine << ")." << sio.nl;
			}
			else
			{
				sio << "fail in unitTest(" << e.str << ")." << sio.nl;
			}

			if( (iOptions & optStopOnError) )
				return 0;
		}
		catch( std::exception &e )
		{
			switch( i->eExpect )
			{
				case expectPass:
					sio << "unexpected ";
					iUFail++;
					break;

				case expectFail:
					sio << "expected ";
					iEFail++;
					break;
			}
			sio << "fail with unknown exception.  what: " << e.what() << sio.nl;

			if( (iOptions & optStopOnError) )
				return 0;
		}
		catch( ... )
		{
			switch( i->eExpect )
			{
				case expectPass:
					sio << "unexpected ";
					iUFail++;
					break;

				case expectFail:
					sio << "expected ";
					iEFail++;
					break;
			}
			sio << "fail with external exception." << sio.nl;

			if( (iOptions & optStopOnError) )
				return 0;
		}
	}

	sio << sio.nl
		<< "Report:" << sio.nl
		<< "\tTotal tests run:     " << lTests.getSize() << sio.nl
		<< "\tExpected passes:     " << iEPass << sio.nl
		<< "\tExpected failures:   " << iEFail << sio.nl
		<< "\tUnexpected passes:   " << iUPass << sio.nl
		<< "\tUnexpected failures: " << iUFail << sio.nl << sio.nl;
	if( iUPass == 0 && iUFail == 0 )
		sio << "\tNothing unexpected." << sio.nl << sio.nl;

	if( bCleanup )
	{
		for( StrList::iterator i = lFileCleanup.begin(); i; i++ )
		{
			unlink( (*i).getStr() );
		}
	}

	return 0;
}

Bu::File Bu::UnitSuite::tempFile( Bu::String &sFileName )
{
	Bu::File f = Bu::File::tempFile( sFileName );
	lFileCleanup.append( sFileName );
	return f;
}

void Bu::UnitSuite::add( Test fTest, const Bu::String &sName, Expect e )
{
	TestInfo ti;
	ti.sName = sName;
	ti.eExpect = e;
	long index = ti.sName.rfindIdx("::");
	if( index != -1 )
	{
		String tmp = sSuiteName;
		tmp += ti.sName.getStr()+index;
		ti.sName = tmp;
	}
	ti.fTest = fTest;
	lTests.append( ti );
	if( iNameWidth < ti.sName.getSize() )
		iNameWidth = ti.sName.getSize();
}

void Bu::UnitSuite::setName( const String &sName )
{
	sSuiteName = sName;
}

void Bu::UnitSuite::dispProgress()
{
	if( tLastUpdate == time( NULL ) )
		return;
	sio << Fmt(3) << (iProgress*100/iStepCount) << "%" << "\b\b\b\b"
		<< sio.flush;
	tLastUpdate = time( NULL );
}

void Bu::UnitSuite::setStepCount( int iSteps )
{
	iStepCount = iSteps;
	if( iStepCount < 0 )
		return;
	tLastUpdate = 0;
	dispProgress();
}

void Bu::UnitSuite::incProgress( int iAmnt )
{
	iProgress += iAmnt;
	if( iProgress < 0 )
		iProgress = 0;
	if( iProgress > iStepCount )
		iProgress = iStepCount;
	dispProgress();
}

void Bu::UnitSuite::setProgress( int iAmnt )
{
	iProgress = iAmnt;
	if( iProgress < 0 )
		iProgress = 0;
	if( iProgress > iStepCount )
		iProgress = iStepCount;
	dispProgress();
}

int Bu::UnitSuite::onListCases( StrArray )
{
	sio << "Test cases:" << sio.nl;
	for( TestList::iterator i = lTests.begin(); i; i++ )
	{
		sio << "\t- " << Fmt( iNameWidth, 10, Fmt::Left ) << (*i).sName << "  "
			<< (*i).eExpect << sio.nl;
	}
	sio << sio.nl;
	exit( 0 );
	return 0;
}

int Bu::UnitSuite::onAddTest( StrArray aParam )
{
	hSelTests.insert( aParam[0], true );
	return 0;
}

Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::UnitSuite::Expect &e )
{
	switch( e )
	{
		case Bu::UnitSuite::expectPass:
			return f << "expect pass";

		case Bu::UnitSuite::expectFail:
			return f << "expect fail";
	}

	return f << "**error**";
}