forked from ComputationalBioLab/UnitTest-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitTestCustomUtilities.hpp
209 lines (189 loc) · 5.62 KB
/
UnitTestCustomUtilities.hpp
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
// Copyright (C) 2012- Dr Martin Buist & Dr Alberto Corrias
// Contacts: martin.buist _at_ nus.edu.sg; alberto _at_ nus.edu.sg
//
// See the 'Contributors' file for a list of those who have contributed
// to this work.
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
//
#ifndef UNITTESTCUSTOMUTILITIES_HPP_
#define UNITTESTCUSTOMUTILITIES_HPP_
#include <algorithm>
#include <string>
#include <vector>
#include "UnitTest++.h"
#include "TestReporterStdout.h"
namespace UnitTest
{
/**
* \brief Runs all the tests it finds in the project.
* it calls RunAllTests() within UnitTests++
*
* Behaviours:
* it runs all the test it finds. See documentation of UnitTest::RunAllTests()
* Intended use:
* int return_code = RunAllTheTests();
* Parameters:
* None
* Return:
* \return the return code of UnitTest::RunAllTests()
*
*/
inline int RunAllTheTests()
{
return UnitTest::RunAllTests();
}
#define COVERAGE_IGNORE
/**
* \brief Runs only one test specified by the name.
* It runs nothing if the tets does not exists.
*
* Behaviours:
*
* it runs one test in isolation.
*
* Intended use (assuming you have TEST(name_of_my_test) among your tests):
*
* int return_code = RunOneTest("name_of_my_test");
*
* Parameters:
* \param TestName the name of the test you wish to run
* Return:
* \return the return code of UnitTest::RunTestsIf()
*
*/
inline int RunOneTest(std::string TestName)
{
const UnitTest::TestList& allTests(UnitTest::Test::GetTestList());
UnitTest::TestList selectedTests;
UnitTest::Test* p = allTests.GetHead();
while (p) {
if (p->m_details.testName == TestName) {
selectedTests.Add(p);
}
UnitTest::Test* q = p;
p = p->next;
q->next = NULL;
}
UnitTest::TestReporterStdout reporter;
UnitTest::TestRunner runner(reporter);
return runner.RunTestsIf(selectedTests, nullptr, UnitTest::True(), 0);
}
/**
* \brief Runs only one test suite (all the tests in the suite) specified by the name.
* It runs nothing if the suite does not exists.
*
* Behaviours:
* it runs one suite in isolation
* Intended use:
* int return_code = RunOneSuite("name_of_my_suite");
*
* Parameters:
* \param SuiteName the name of the suite you wish to run
* Return:
* \return the return code of UnitTest::RunTestsIf()
*
*/
inline int RunOneSuite(std::string SuiteName)
{
const UnitTest::TestList& allTests(UnitTest::Test::GetTestList());
UnitTest::TestList selectedTests;
UnitTest::Test* p = allTests.GetHead();
while (p) {
if (p->m_details.suiteName == SuiteName) {
selectedTests.Add(p);
}
UnitTest::Test* q = p;
p = p->next;
q->next = NULL;
}
UnitTest::TestReporterStdout reporter;
UnitTest::TestRunner runner(reporter);
return runner.RunTestsIf(selectedTests, nullptr, UnitTest::True(), 0 );
}
/**
* \brief Runs multiple tests specified by the names.
* It runs nothing if all of the tests do not exist.
*
* Behaviours:
*
* it runs multiple tests in isolation.
*
* Intended use (assuming you have TEST(name_of_my_test) among your tests):
*
* int return_code = RunOneTest(name_of_my_tests);
*
* Parameters:
* \param TestName A vector containing the names of the tests you wish to run
* Return:
* \return the return code of UnitTest::RunTestsIf()
*
*/
inline int RunMultipleTests(std::vector<std::string> test_names)
{
const UnitTest::TestList& allTests(UnitTest::Test::GetTestList());
UnitTest::TestList selectedTests;
UnitTest::Test* p = allTests.GetHead();
while (p) {
auto is_desired_test = std::find(begin(test_names), end(test_names),
p->m_details.testName) != end(test_names);
if (is_desired_test) {
selectedTests.Add(p);
}
UnitTest::Test* q = p;
p = p->next;
q->next = NULL;
}
UnitTest::TestReporterStdout reporter;
UnitTest::TestRunner runner(reporter);
return runner.RunTestsIf(selectedTests, nullptr, UnitTest::True(), 0);
}
/**
* Runs multiple test suites (all the tests in the suites) specified by the
* names.
* It runs nothing if none of the suites exists.
*
* Behaviours:
* it runs multiple suites
* Intended use:
* int return_code = RunMultipleSuites(names_of_my_suites);
*
* Parameters:
* \param suite_names The names of the suites you wish to run
* Return:
* \return the return code of UnitTest::RunTestsIf()
*
*/
inline int RunMultipleSuites(std::vector<std::string> suite_names)
{
const UnitTest::TestList& allTests(UnitTest::Test::GetTestList());
UnitTest::TestList selectedTests;
UnitTest::Test* p = allTests.GetHead();
while (p) {
auto is_desired_suite = std::find(begin(suite_names), end(suite_names),
p->m_details.suiteName) != end(suite_names);
if (is_desired_suite) {
selectedTests.Add(p);
}
UnitTest::Test* q = p;
p = p->next;
q->next = NULL;
}
UnitTest::TestReporterStdout reporter;
UnitTest::TestRunner runner(reporter);
return runner.RunTestsIf(selectedTests, nullptr, UnitTest::True(), 0 );
}
#undef COVERAGE_IGNORE
} // namespace UnitTest
#endif