ZeroErr
Loading...
Searching...
No Matches
unittest.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <chrono>
6#include <functional>
7#include <string>
8#include <vector>
9
11
12#define ZEROERR_CREATE_TEST_FUNC(function, name, ...) \
13 static void function(zeroerr::TestContext*); \
14 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
15 {name, __FILE__, __LINE__, function, {__VA_ARGS__}}); \
16 static void function(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
17
18#define TEST_CASE(name, ...) \
19 ZEROERR_CREATE_TEST_FUNC(ZEROERR_NAMEGEN(_zeroerr_testcase), name, __VA_ARGS__)
20
21#define SUB_CASE(name, ...) \
22 zeroerr::SubCase(name, __FILE__, __LINE__, _ZEROERR_TEST_CONTEXT, {__VA_ARGS__}) \
23 << [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT)) mutable
24
25#define ZEROERR_CREATE_TEST_CLASS(fixture, classname, funcname, name, ...) \
26 class classname : public fixture { \
27 public: \
28 void funcname(zeroerr::TestContext*); \
29 }; \
30 static void ZEROERR_CAT(call_, funcname)(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT) { \
31 classname instance; \
32 instance.funcname(_ZEROERR_TEST_CONTEXT); \
33 } \
34 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
35 {name, __FILE__, __LINE__, ZEROERR_CAT(call_, funcname), {__VA_ARGS__}}); \
36 inline void classname::funcname(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
37
38#define TEST_CASE_FIXTURE(fixture, name, ...) \
39 ZEROERR_CREATE_TEST_CLASS(fixture, ZEROERR_NAMEGEN(_zeroerr_class), \
40 ZEROERR_NAMEGEN(_zeroerr_test_method), name, __VA_ARGS__)
41
42
43#define ZEROERR_HAVE_SAME_OUTPUT _ZEROERR_TEST_CONTEXT->save_output();
44
45#ifndef ZEROERR_DISABLE_BDD
46#define SCENARIO(...) TEST_CASE("Scenario: " __VA_ARGS__)
47#define GIVEN(...) SUB_CASE("given: " __VA_ARGS__)
48#define WHEN(...) SUB_CASE("when: " __VA_ARGS__)
49#define THEN(...) SUB_CASE("then: " __VA_ARGS__)
50#endif
51
52namespace zeroerr {
53
54class IReporter;
55struct TestCase;
56class Decorator;
57
71public:
72 unsigned passed = 0;
73 unsigned warning = 0;
74 unsigned failed = 0;
75 unsigned skipped = 0;
76 unsigned passed_as = 0;
77 unsigned warning_as = 0;
78 unsigned failed_as = 0;
79 unsigned skipped_as = 0;
80
81 std::chrono::duration<double> duration = std::chrono::duration<double>::zero();
82
84
90 int add(TestContext& local);
91
95 void reset();
96
100 void save_output();
101
107 ~TestContext() = default;
108};
109
124struct UnitTest {
131 UnitTest& parseArgs(int argc, const char** argv);
132
137 int run();
138
145 bool run_filter(const TestCase& tc);
146
147 bool silent = false;
148 bool run_bench = false;
149 bool run_fuzz = false;
150 bool list_test_cases = false;
151 bool no_color = false;
152 bool log_to_report = false;
154 std::string reporter_name = "console";
155 std::string binary;
157};
158
168struct TestCase {
169 std::string name;
170 std::string file;
171 unsigned line;
172 std::function<void(TestContext*)> func;
173 std::vector<TestCase*> subcases;
174 std::vector<Decorator*> decorators;
180 bool operator<(const TestCase& rhs) const;
181
188 TestCase(std::string name, std::string file, unsigned line, std::vector<Decorator*> decorators)
190
199 TestCase(std::string name, std::string file, unsigned line,
200 std::function<void(TestContext*)> func, std::vector<Decorator*> decorators)
202};
203
204
209 SubCase(std::string name, std::string file, unsigned line, TestContext* context,
210 std::vector<Decorator*> decorators);
211 ~SubCase() = default;
213 void operator<<(std::function<void(TestContext*)> op);
214};
215
216
217template <typename T>
219 void add(T&& obj) { objects.push_back(std::forward<T>(obj)); }
220 std::vector<T> objects;
221};
222
223
236public:
237 virtual ~IReporter() = default;
238
239 virtual std::string getName() const = 0;
240
241 // There are a list of events
242 virtual void testStart() = 0;
243 virtual void testCaseStart(const TestCase& tc, std::stringbuf& sb) = 0;
244 virtual void testCaseEnd(const TestCase& tc, std::stringbuf& sb, const TestContext& ctx,
245 int type) = 0;
246 virtual void subCaseStart(const TestCase& tc, std::stringbuf& sb) = 0;
247 virtual void subCaseEnd(const TestCase& tc, std::stringbuf& sb, const TestContext& ctx,
248 int type) = 0;
249 virtual void testEnd(const TestContext& tc) = 0;
250
256 static IReporter* create(const std::string& name, UnitTest& ut);
257
259
260protected:
262};
263
267enum TestType { test_case = 1, sub_case = 1 << 1, bench = 1 << 2, fuzz_test = 1 << 3 };
268
269namespace detail {
270
275struct regTest {
276 explicit regTest(const TestCase& tc, TestType type = test_case);
277};
278
284 explicit regReporter(IReporter*);
285};
286} // namespace detail
287
288
305public:
306 CombinationalTest(std::function<void()> func) : func(func) {}
307 std::function<void()> func;
308
309 template <typename T>
310 void operator()(T& arg) {
311 arg.reset();
312 for (size_t i = 0; i < arg.size(); ++i, ++arg) {
313 func();
314 }
315 }
316
317 template <typename T, typename... Args>
318 void operator()(T& arg, Args&... args) {
319 arg.reset();
320 for (size_t i = 0; i < arg.size(); ++i, ++arg) {
321 operator()(args...);
322 }
323 }
324};
325
326
330template <typename T>
331class TestArgs {
332public:
333 TestArgs(std::initializer_list<T> args) : args(args) {}
334 std::vector<T> args;
335
336 operator T() const { return args[index]; }
338 index++;
339 return *this;
340 }
341 size_t size() const { return args.size(); }
342 void reset() { index = 0; }
343
344private:
345 int index = 0;
346};
347
348
350public:
351 // Called when the test registered, return true can block the test registering
352 virtual bool onStartup(const TestCase&) { return false; }
353
354 // Called when the test executing, return true can block the test execution
355 virtual bool onExecution(const TestCase&) { return false; }
356
357 // Called on each assertion, return true can skip the assertion
358 virtual bool onAssertion() { return false; }
359
360 // Called when the test finished, return true means the test containing errors
361 virtual bool onFinish(const TestCase&, const TestContext&) { return false; }
362};
363
364Decorator* skip(bool isSkip = true);
365Decorator* timeout(float timeout = 0.1f); // in seconds
366Decorator* may_fail(bool isMayFail = true);
367Decorator* should_fail(bool isShouldFail = true);
368
369} // namespace zeroerr
370
CombinationalTest is a class that is used to cross test a few lists of arguments. One example.
Definition unittest.h:304
std::function< void()> func
Definition unittest.h:307
void operator()(T &arg, Args &... args)
Definition unittest.h:318
void operator()(T &arg)
Definition unittest.h:310
CombinationalTest(std::function< void()> func)
Definition unittest.h:306
Definition unittest.h:349
virtual bool onAssertion()
Definition unittest.h:358
virtual bool onStartup(const TestCase &)
Definition unittest.h:352
virtual bool onExecution(const TestCase &)
Definition unittest.h:355
virtual bool onFinish(const TestCase &, const TestContext &)
Definition unittest.h:361
IReporter is an interface that is used to report the test results. You can create a new reporter by i...
Definition unittest.h:235
virtual void subCaseStart(const TestCase &tc, std::stringbuf &sb)=0
static IReporter * create(const std::string &name, UnitTest &ut)
Create the reporter object with the given name.
Definition unittest.cpp:817
virtual ~IReporter()=default
virtual void testStart()=0
virtual void testCaseStart(const TestCase &tc, std::stringbuf &sb)=0
virtual void subCaseEnd(const TestCase &tc, std::stringbuf &sb, const TestContext &ctx, int type)=0
virtual void testEnd(const TestContext &tc)=0
virtual std::string getName() const =0
UnitTest & ut
Definition unittest.h:261
IReporter(UnitTest &ut)
Definition unittest.h:258
virtual void testCaseEnd(const TestCase &tc, std::stringbuf &sb, const TestContext &ctx, int type)=0
TestArgs is a class that is used to store the test arguments.
Definition unittest.h:331
TestArgs & operator++()
Definition unittest.h:337
size_t size() const
Definition unittest.h:341
std::vector< T > args
Definition unittest.h:334
TestArgs(std::initializer_list< T > args)
Definition unittest.h:333
void reset()
Definition unittest.h:342
TestContext is a class that holds the test results and reporter context. There are 8 different matric...
Definition unittest.h:70
void reset()
Reset the matrices to 0.
Definition unittest.cpp:65
unsigned skipped
Definition unittest.h:75
unsigned failed
Definition unittest.h:74
unsigned passed
Definition unittest.h:72
void save_output()
Save the output of the test to the correct_output_path as a golden file.
Definition unittest.cpp:45
std::chrono::duration< double > duration
Definition unittest.h:81
unsigned skipped_as
Definition unittest.h:79
unsigned passed_as
Definition unittest.h:76
unsigned failed_as
Definition unittest.h:78
IReporter & reporter
Definition unittest.h:83
unsigned warning
Definition unittest.h:73
unsigned warning_as
Definition unittest.h:77
TestContext(IReporter &reporter)
Construct a new Test Context object.
Definition unittest.h:106
int add(TestContext &local)
Add the subtest results to the matrices.
Definition unittest.cpp:24
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:265
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:218
Definition benchmark.cpp:17
Decorator * should_fail(bool isShouldFail)
Definition unittest.cpp:874
TestType
TestType is a enum describe the type of the test case.
Definition unittest.h:267
@ bench
Definition unittest.h:267
@ test_case
Definition unittest.h:267
@ fuzz_test
Definition unittest.h:267
@ sub_case
Definition unittest.h:267
Decorator * timeout(float timeout)
Definition unittest.cpp:850
Decorator * may_fail(bool isMayFail)
Definition unittest.cpp:868
Decorator * skip(bool isSkip)
Definition unittest.cpp:828
Definition unittest.cpp:105
SubCase is a class that holds the subcase information.
Definition unittest.h:208
TestContext * context
Definition unittest.h:212
void operator<<(std::function< void(TestContext *)> op)
Definition unittest.cpp:82
~SubCase()=default
TestCase is a class that holds the test case information. There are several fields that are used to s...
Definition unittest.h:168
bool operator<(const TestCase &rhs) const
Compare the test cases.
Definition unittest.cpp:309
std::function< void(TestContext *)> func
Definition unittest.h:172
std::vector< Decorator * > decorators
Definition unittest.h:174
TestCase(std::string name, std::string file, unsigned line, std::function< void(TestContext *)> func, std::vector< Decorator * > decorators)
Construct a new Test Case object.
Definition unittest.h:199
std::string file
Definition unittest.h:170
unsigned line
Definition unittest.h:171
std::vector< TestCase * > subcases
Definition unittest.h:173
TestCase(std::string name, std::string file, unsigned line, std::vector< Decorator * > decorators)
Construct a new Test Case object.
Definition unittest.h:188
std::string name
Definition unittest.h:169
Definition unittest.h:218
std::vector< T > objects
Definition unittest.h:220
void add(T &&obj)
Definition unittest.h:219
UnitTest is a class that holds the test configuration. There are several options that can be set to c...
Definition unittest.h:124
std::string binary
Definition unittest.h:155
bool list_test_cases
Definition unittest.h:150
bool run_fuzz
Definition unittest.h:149
bool run_bench
Definition unittest.h:148
bool log_to_report
Definition unittest.h:152
bool run_filter(const TestCase &tc)
Run the test with the given filter.
Definition unittest.cpp:226
std::string correct_output_path
Definition unittest.h:153
bool no_color
Definition unittest.h:151
struct Filters * filters
Definition unittest.h:156
UnitTest & parseArgs(int argc, const char **argv)
Parse the arguments to configure the test.
Definition unittest.cpp:110
std::string reporter_name
Definition unittest.h:154
int run()
Run the test.
Definition unittest.cpp:253
bool silent
Definition unittest.h:147
regReporter is a class that is used to register the reporter. It will be used as global variables and...
Definition unittest.h:283
regTest is a class that is used to register the test case. It will be used as global variables and th...
Definition unittest.h:275