summaryrefslogtreecommitdiff
path: root/src/unitsuite.cpp
blob: 711b9b56c1b77bf85393d68247c93dd7cbcf0bda (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
/*
 * Copyright (C) 2007-2010 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"

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()
{
}

// Argument handling is coming soon, I promise.
int Bu::UnitSuite::run( int /*argc*/, char * /*argv */ [] )
{
	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
		{
			(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;

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

	return 0;
}

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

void Bu::UnitSuite::add( Test fTest, const Bu::FString &sName, Expect e )
{
	TestInfo ti;
	ti.sName = sName;
	ti.eExpect = e;
	long index = ti.sName.rfindIdx("::");
	if( index != -1 )
	{
		FString 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 FString &sName )
{
	sSuiteName = sName;
}