junit.cpp
4.6 KB
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
/***************************************************************************
* Copyright (C) 2005 by Jeff Ferr *
* root@sat *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "Stdafx.h"
#include "junit.h"
namespace jcommon {
bool verbose = true;
bool line_fmt = false;
void Tester::Summary()
{
os << "Tests [Ok-Fail-Error]: [" << n_test.n_ok() << '-'
<< n_test.n_fail() << '-' << n_test.n_err() << "]\n";
}
void Tester::Visit(Test& t)
{
try {
t();
n_test.add_ok();
Write(t);
} catch (AssertionError& e) {
n_test.add_fail();
Write(t, e);
} catch (RuntimeException& e) {
n_test.add_err();
Write(t, e);
} catch (...) {
n_test.add_err();
Write(t, 0);
}
}
void Tester::Visit(Suite& t)
{
if (verbose)
os << "****** " << t.GetName() << " ******" << std::endl;
accu.push(n_test);
}
void Tester::Visit(Suite& , int)
{
res_cnt r(accu.top());
accu.pop();
if (n_test.n_err() != r.n_err())
n_suite.add_err();
else if (n_test.n_fail() != r.n_fail())
n_suite.add_fail();
else
n_suite.add_ok();
}
void Tester::Write(Test& t)
{
if (verbose)
Disp(t, "OK");
}
void Tester::Disp(Test& t, const std::string& status)
{
os << status << ": " << t.GetName() << std::endl;
}
void Tester::Write(Test& t, AssertionError& e)
{
if (line_fmt)
os << e.GetFile() << ':' << e.GetLine() << ':';
Disp(t, "FAIL");
os << e << '\n';
}
void Tester::Write(Test& t, RuntimeException& e)
{
Disp(t, "ERROR");
os << " : [" << typeid(e).name() << "] " << e.what() << '\n';
}
void Tester::Write(Test& t, int )
{
Disp(t, "ERROR");
os << " : " << "unknown exception" << '\n';
}
void Test::Visit(Visitor* v)
{
v->Visit(*this);
}
TestCase::TestCase(Test* t)
: cnt(new size_t(1)), tst(t)
{
}
TestCase::TestCase(const TestCase& t)
: cnt(t.cnt), tst(t.tst)
{
(*cnt)++;
}
void TestCase::dec_cnt()
{
if (--(*cnt) == 0) {
delete cnt;
delete tst;
}
}
TestCase::~TestCase()
{
dec_cnt();
}
TestCase& TestCase::operator=(const TestCase& t)
{
++*(t.cnt);
dec_cnt();
cnt = t.cnt;
tst = t.tst;
return *this;
}
Suite& Suite::Main()
{
static Suite instance("top");
return instance;
}
Test* Suite::GetChild(const std::string& id)
{
std::vector<std::string>::iterator p = std::find(ids.begin(), ids.end(), id);
if (p != ids.end())
return &(static_cast<Test&>(tests[p - ids.begin()]));
return 0;
}
std::vector<std::string> vectorize(const std::string& str, char c)
{
std::vector<std::string> res;
std::string::const_iterator s, p;
for (s = str.begin(); s != str.end(); ) {
p = find(s, str.end(), c);
res.push_back(std::string(s, p));
s = (p == str.end()) ? p : p + 1;
}
return res;
}
Test* Suite::Find(const std::string& id)
{
std::vector<std::string> ss(vectorize(id, '.'));
Test* tp = this;
for (std::vector<std::string>::iterator p = ss.begin(); p != ss.end(); ++p)
if (!(tp = tp->GetChild(*p)))
break;
return tp;
}
void Suite::Add(const std::string& id, const TestCase& t)
{
ids.push_back(id);
tests.push_back(t);
}
void Suite::Visit(Visitor* v)
{
v->Visit(*this);
for_each(tests.begin(), tests.end(),
std::bind2nd(std::mem_fun_ref(&TestCase::Visit), v));
v->Visit(*this, 0);
}
void AssertionError::Out(std::ostream& os) const
{
os << msg << std::string(" [assertion failed]");
}
}