This repository was archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathtestrunner.h
More file actions
196 lines (161 loc) · 5.8 KB
/
Copy pathtestrunner.h
File metadata and controls
196 lines (161 loc) · 5.8 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright Joakim Karlsson & Kim Gräsman 2010-2013.
// 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)
#ifndef IGLOO_TESTRUNNER_H
#define IGLOO_TESTRUNNER_H
#include <igloo/core/testresult.h>
#include <igloo/core/testresults.h>
#include <igloo/core/testlisteners/minimalprogresstestlistener.h>
#include <igloo/external/choices/choices.h>
namespace igloo {
namespace c = choices;
class TestRunner
{
public:
typedef std::list<BaseContextRunner*> ContextRunners;
static int RunAllTests(int argc = 0, const char *argv[] = 0)
{
choices::options opt = choices::parse_cmd(argc, argv);
if(c::has_option("version", opt))
{
std::cout << IGLOO_VERSION << std::endl;
return 0;
}
if(c::has_option("help", opt))
{
std::cout << "Usage: <igloo-executable> [--version] [--output=color|vs|xunit]" << std::endl;
std::cout << "Options:" << std::endl;
std::cout << " --version:\tPrint version of igloo and exit." << std::endl;
std::cout << " --output:\tFormat output of test results." << std::endl;
std::cout << "\t\t--output=color:\tColored output" << std::endl;
std::cout << "\t\t--output=vs:\tVisual studio friendly output." << std::endl;
std::cout << "\t\t--output=xunit:\tXUnit formatted output." << std::endl;
std::cout << "\t\t--output=default:\tDefault output format." << std::endl;
return 0;
}
std::auto_ptr<TestResultsOutput> output;
if(c::has_option("output", opt))
{
std::string val = c::option_value("output", opt);
if(val == "vs")
{
output = std::auto_ptr<TestResultsOutput>(new VisualStudioResultsOutput());
}
else if(val == "color")
{
output = std::auto_ptr<TestResultsOutput>(new ColoredConsoleTestResultsOutput());
}
else if(val == "xunit")
{
output = std::auto_ptr<TestResultsOutput>(new XUnitResultsOutput());
}
else if(val == "default")
{
output = std::auto_ptr<TestResultsOutput>(new DefaultTestResultsOutput());
}
else
{
std::cerr << "Unknown output: " << c::option_value("output", opt) << std::endl;
return 1;
}
}
else
{
output = std::auto_ptr<TestResultsOutput>(new DefaultTestResultsOutput());
}
TestRunner runner(*(output.get()));
MinimalProgressTestListener progressOutput;
runner.AddListener(&progressOutput);
return runner.Run();
}
TestRunner(const TestResultsOutput& output)
: output_(output)
{}
static bool is_only(const BaseContextRunner* runner)
{
return runner->IsContextMarkedAsOnly();
}
int Run(const ContextRunners& runners)
{
TestResults results;
listenerAggregator_.TestRunStarting();
bool only_has_been_found = std::find_if(runners.begin(), runners.end(), is_only) != runners.end();
for (ContextRunners::const_iterator it = runners.begin(); it != runners.end(); it++)
{
BaseContextRunner* contextRunner = *it;
if(!only_has_been_found || contextRunner->IsContextMarkedAsOnly()) {
if(!contextRunner->IsMarkedAsSkip()) {
contextRunner->Run(results, listenerAggregator_);
}
}
}
listenerAggregator_.TestRunEnded(results);
output_.PrintResult(results);
return results.NumberOfFailedTests();
}
int Run()
{
int numberOfFailedTests = Run(RegisteredRunners());
CleanUpRunners();
return numberOfFailedTests;
}
template <typename ContextRunnerType>
static void RegisterContext(const std::string& name, const char* fileName, int lineNumber)
{
if(!ContextIsRegistered(name, fileName, lineNumber))
{
ContextRunnerType* contextRunner = 0;
try
{
// Must add runner first...
contextRunner = new ContextRunnerType(name, fileName, lineNumber);
TestRunner::RegisteredRunners().push_back(contextRunner);
// ... and then instantiate context, because context ctor calls this method again,
// possibly for the same context, depending on inheritance chain.
contextRunner->InstantiateContext();
}
catch (...)
{
delete contextRunner;
throw;
}
}
}
void AddListener(TestListener* listener)
{
listenerAggregator_.AddListener(listener);
}
private:
static void CleanUpRunners()
{
while(!RegisteredRunners().empty())
{
delete RegisteredRunners().front();
RegisteredRunners().pop_front();
}
}
static bool ContextIsRegistered(const std::string& name, const char* fileName, int lineNumber)
{
for (ContextRunners::const_iterator it = RegisteredRunners().begin(); it != RegisteredRunners().end(); ++it)
{
if((*it)->ContextName() == name &&
(*it)->FileName() == fileName &&
(*it)->LineNumber() == lineNumber)
{
return true;
}
}
return false;
}
static TestRunner::ContextRunners& RegisteredRunners()
{
static TestRunner::ContextRunners contextRunners;
return contextRunners;
}
private:
const TestResultsOutput& output_;
TestListenerAggregator listenerAggregator_;
};
}
#endif // IGLOO_TESTRUNNER_H