-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_o_matic.cpp
173 lines (149 loc) · 4.81 KB
/
test_o_matic.cpp
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 2008 Edd Dawson.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_o_matic.hpp"
#include <iostream>
#include <iomanip> // endl
#include <algorithm>
#include <cctype>
#ifdef __BORLANDC__
#pragma warn -8091 // "template argument InputIterator passed to 'find_if' is a output iterator: input iterator required"
#pragma warn -8013 // "Possible use of 'ns' before definition in function trim(const string &)"
#endif
namespace
{
using namespace std;
using namespace test_o_matic;
struct not_space { bool operator() (char ch) const { return !::isspace(ch); } };
string trim(const string &s)
{
not_space ns;
string::const_iterator open = find_if(s.begin(), s.end(), ns);
string::const_iterator close = find_if(s.rbegin(), s.rend(), ns).base();
return (open < close) ? string(open, close) : s;
}
std::ostream &result_colon(std::ostream &out, const std::string &okfail)
{
return out << okfail << ':' << setw(5 - okfail.size()) << ' ';
}
} // close anonymous namespace
namespace test_o_matic
{
simple_logger::simple_logger(std::ostream &out, bool verbose) :
successes(0),
failures(0),
complete(0),
aborted(0),
out(out),
verbose(verbose)
{
}
void simple_logger::record(const std::string &event, const strmap &dat)
{
strmap mp(dat);
const string res(mp["result"]);
const bool ok = (event == "pre_test") || (res == "ok");
if (!ok)
{
if (!headline.empty()) out << headline;
headline.clear();
}
if (event == "check")
{
if (verbose || !ok)
{
result_colon(out, res) << "'" << trim(mp["statement"]) << "'";
if (!mp["threw"].empty()) out << " threw " << mp["threw"];
out << ", line " << mp["line"] << endl;
}
++(ok ? successes : failures);
}
else if (event == "throws" || event == "try")
{
if (verbose || !ok)
result_colon(out, res) << "'" << trim(mp["statement"]) << "' threw " << mp["threw"] <<
(ok ? " as expected" : " unexpectedly") << ", line " << mp["line"] << endl;
++(ok ? successes : failures);
}
else if (event == "pre_test")
{
std::ostringstream oss;
(verbose ? out : static_cast<ostream &>(oss)) << '\n' << mp["name"] << ", " << mp["file"] << ':' << mp["line"] << endl;
if (!verbose) headline = oss.str();
}
else if (event == "post_test")
{
if (!ok) out << "8< ---- test cut short: " << mp["reason"] << " ----\n";
++(ok ? complete : aborted);
headline.clear();
}
}
int simple_logger::summary(std::ostream &out)
{
out << "\nsuccesses: " << successes << '\n';
out << "failures: " << failures << '\n';
if (aborted) out << "aborted: " << aborted << '\n';
out << std::endl;
return failures + aborted;
}
void runner::run(const test &t, logger &lgr)
{
try
{
lgr.record("pre_test", data("name", t.name)("line", t.line)("file", t.file)("time", t.time)("date", t.date));
t.function(lgr);
lgr.record("post_test", data("result", "ok"));
}
catch (const data &dat)
{
lgr.record("post_test", dat);
}
catch (...)
{
lgr.record("post_test", data("result", "aborted")("reason", "an uncaught exception ended the test"));
}
}
namespace
{
test *&root()
{
static test *root = 0;
return root;
}
}
test::test(const char *name, const char *file, std::size_t line, const char *date, const char *time, void (*function)(logger &)) :
name(name),
file(file),
line(line),
date(date),
time(time),
function(function),
next(0)
{
if (!root()) root() = this;
else
{
test *t = root();
while (t->next) t = t->next;
t->next = this;
}
}
test::~test()
{
if (root() == this) root() = next;
else
{
test *t = root();
while (t->next != this) t = t->next;
t->next = next;
}
}
const test *first_test() { return root(); }
void run_test(const test &tst, logger &lgr, runner &rnr) { rnr.run(tst, lgr); }
} // close namespace test_o_matic
test_o_matic::simple_logger &get_local_tom_logger()
{
static test_o_matic::simple_logger lgr(std::cout, false);
return lgr;
}