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(...) \
19 ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH \
20 ZEROERR_CREATE_TEST_FUNC(ZEROERR_NAMEGEN(_zeroerr_testcase), __VA_ARGS__) \
21 ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
22
23#define ZEROERR_CREATE_SUB_CASE(name, ...) \
24 zeroerr::SubCase(name, __FILE__, __LINE__, _ZEROERR_TEST_CONTEXT, {__VA_ARGS__}) \
25 << [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT)) mutable
26
27#define SUB_CASE(...) \
28 ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH \
29 ZEROERR_CREATE_SUB_CASE(__VA_ARGS__) \
30 ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
31
32#define ZEROERR_CREATE_TEST_CLASS(fixture, classname, funcname, name, ...) \
33 class classname : public fixture { \
34 public: \
35 void funcname(zeroerr::TestContext*); \
36 }; \
37 static void ZEROERR_CAT(call_, funcname)(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT) { \
38 classname instance; \
39 instance.funcname(_ZEROERR_TEST_CONTEXT); \
40 } \
41 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
42 {name, __FILE__, __LINE__, ZEROERR_CAT(call_, funcname), {__VA_ARGS__}}); \
43 inline void classname::funcname(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
44
45#define TEST_CASE_FIXTURE(fixture, ...) \
46 ZEROERR_CREATE_TEST_CLASS(fixture, ZEROERR_NAMEGEN(_zeroerr_class), \
47 ZEROERR_NAMEGEN(_zeroerr_test_method), __VA_ARGS__)
48
49
50#define ZEROERR_HAVE_SAME_OUTPUT _ZEROERR_TEST_CONTEXT->save_output();
51
52#ifndef ZEROERR_DISABLE_BDD
53#define SCENARIO(...) TEST_CASE("Scenario: " __VA_ARGS__)
54#define GIVEN(...) SUB_CASE("given: " __VA_ARGS__)
55#define WHEN(...) SUB_CASE("when: " __VA_ARGS__)
56#define THEN(...) SUB_CASE("then: " __VA_ARGS__)
57#endif
58
59namespace zeroerr {
60
61class IReporter;
62struct TestCase;
63class Decorator;
64
78public:
79 unsigned passed = 0;
80 unsigned warning = 0;
81 unsigned failed = 0;
82 unsigned skipped = 0;
83 unsigned passed_as = 0;
84 unsigned warning_as = 0;
85 unsigned failed_as = 0;
86 unsigned skipped_as = 0;
87
88 std::chrono::duration<double> duration = std::chrono::duration<double>::zero();
89
91
97 int add(TestContext& local);
98
102 void reset();
103
107 void save_output();
108
114 ~TestContext() = default;
115};
116
131struct UnitTest {
138 UnitTest& parseArgs(int argc, const char** argv);
139
144 int run();
145
152 bool run_filter(const TestCase& tc);
153
154 bool silent = false;
155 bool run_bench = false;
156 bool run_fuzz = false;
157 bool list_test_cases = false;
158 bool no_color = false;
159 bool log_to_report = false;
161 std::string reporter_name = "console";
162 std::string binary;
164};
165
175struct TestCase {
176 std::string name;
177 std::string file;
178 unsigned line;
179 std::function<void(TestContext*)> func;
180 std::vector<TestCase*> subcases;
181 std::vector<Decorator*> decorators;
187 bool operator<(const TestCase& rhs) const;
188
195 TestCase(std::string name, std::string file, unsigned line, std::vector<Decorator*> decorators)
197
206 TestCase(std::string name, std::string file, unsigned line,
207 std::function<void(TestContext*)> func, std::vector<Decorator*> decorators)
209};
210
211
216 SubCase(std::string name, std::string file, unsigned line, TestContext* context,
217 std::vector<Decorator*> decorators);
218 ~SubCase() = default;
220 void operator<<(std::function<void(TestContext*)> op);
221};
222
223
224template <typename T>
226 void add(T&& obj) { objects.push_back(std::forward<T>(obj)); }
227 std::vector<T> objects;
228};
229
230
243public:
244 virtual ~IReporter() = default;
245
246 virtual std::string getName() const = 0;
247
248 // There are a list of events
249 virtual void testStart() = 0;
250 virtual void testCaseStart(const TestCase& tc, std::stringbuf& sb) = 0;
251 virtual void testCaseEnd(const TestCase& tc, std::stringbuf& sb, const TestContext& ctx,
252 int type) = 0;
253 virtual void subCaseStart(const TestCase& tc, std::stringbuf& sb) = 0;
254 virtual void subCaseEnd(const TestCase& tc, std::stringbuf& sb, const TestContext& ctx,
255 int type) = 0;
256 virtual void testEnd(const TestContext& tc) = 0;
257
263 static IReporter* create(const std::string& name, UnitTest& ut);
264
266
267protected:
269};
270
274enum TestType { test_case = 1, sub_case = 1 << 1, bench = 1 << 2, fuzz_test = 1 << 3 };
275
276namespace detail {
277
282struct regTest {
283 explicit regTest(const TestCase& tc, TestType type = test_case);
284};
285
291 explicit regReporter(IReporter*);
292};
293} // namespace detail
294
295
312public:
313 CombinationalTest(std::function<void()> func) : func(func) {}
314 std::function<void()> func;
315
316 template <typename T>
317 void operator()(T& arg) {
318 arg.reset();
319 for (size_t i = 0; i < arg.size(); ++i, ++arg) {
320 func();
321 }
322 }
323
324 template <typename T, typename... Args>
325 void operator()(T& arg, Args&... args) {
326 arg.reset();
327 for (size_t i = 0; i < arg.size(); ++i, ++arg) {
328 operator()(args...);
329 }
330 }
331};
332
333
337template <typename T>
338class TestArgs {
339public:
340 TestArgs(std::initializer_list<T> args) : args(args) {}
341 std::vector<T> args;
342
343 operator T() const { return args[index]; }
345 index++;
346 return *this;
347 }
348 size_t size() const { return args.size(); }
349 void reset() { index = 0; }
350
351private:
352 int index = 0;
353};
354
355
357public:
358 // Called when the test registered, return true can block the test registering
359 virtual bool onStartup(const TestCase&) { return false; }
360
361 // Called when the test executing, return true can block the test execution
362 virtual bool onExecution(const TestCase&) { return false; }
363
364 // Called on each assertion, return true can skip the assertion
365 virtual bool onAssertion() { return false; }
366
367 // Called when the test finished, return true means the test containing changes
368 virtual bool onFinish(const TestCase&, TestContext&) { return false; }
369};
370
371Decorator* skip(bool isSkip = true);
372Decorator* timeout(float timeout = 0.1f); // in seconds
373Decorator* may_fail(bool isMayFail = true);
374Decorator* should_fail(bool isShouldFail = true);
375
376} // namespace zeroerr
377
CombinationalTest is a class that is used to cross test a few lists of arguments. One example.
Definition unittest.h:311
std::function< void()> func
Definition unittest.h:314
void operator()(T &arg, Args &... args)
Definition unittest.h:325
void operator()(T &arg)
Definition unittest.h:317
CombinationalTest(std::function< void()> func)
Definition unittest.h:313
Definition unittest.h:356
virtual bool onAssertion()
Definition unittest.h:365
virtual bool onStartup(const TestCase &)
Definition unittest.h:359
virtual bool onExecution(const TestCase &)
Definition unittest.h:362
virtual bool onFinish(const TestCase &, TestContext &)
Definition unittest.h:368
IReporter is an interface that is used to report the test results. You can create a new reporter by i...
Definition unittest.h:242
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:820
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:268
IReporter(UnitTest &ut)
Definition unittest.h:265
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:338
TestArgs & operator++()
Definition unittest.h:344
size_t size() const
Definition unittest.h:348
std::vector< T > args
Definition unittest.h:341
TestArgs(std::initializer_list< T > args)
Definition unittest.h:340
void reset()
Definition unittest.h:349
TestContext is a class that holds the test results and reporter context. There are 8 different matric...
Definition unittest.h:77
void reset()
Reset the matrices to 0.
Definition unittest.cpp:65
unsigned skipped
Definition unittest.h:82
unsigned failed
Definition unittest.h:81
unsigned passed
Definition unittest.h:79
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:88
unsigned skipped_as
Definition unittest.h:86
unsigned passed_as
Definition unittest.h:83
unsigned failed_as
Definition unittest.h:85
IReporter & reporter
Definition unittest.h:90
unsigned warning
Definition unittest.h:80
unsigned warning_as
Definition unittest.h:84
TestContext(IReporter &reporter)
Construct a new Test Context object.
Definition unittest.h:113
int add(TestContext &local)
Add the subtest results to the matrices.
Definition unittest.cpp:24
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:268
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:220
Definition benchmark.cpp:17
Decorator * should_fail(bool isShouldFail)
Definition unittest.cpp:892
TestType
TestType is a enum describe the type of the test case.
Definition unittest.h:274
@ bench
Definition unittest.h:274
@ test_case
Definition unittest.h:274
@ fuzz_test
Definition unittest.h:274
@ sub_case
Definition unittest.h:274
Decorator * timeout(float timeout)
Definition unittest.cpp:853
Decorator * may_fail(bool isMayFail)
Definition unittest.cpp:886
Decorator * skip(bool isSkip)
Definition unittest.cpp:831
Definition unittest.cpp:105
SubCase is a class that holds the subcase information.
Definition unittest.h:215
TestContext * context
Definition unittest.h:219
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:175
bool operator<(const TestCase &rhs) const
Compare the test cases.
Definition unittest.cpp:311
std::function< void(TestContext *)> func
Definition unittest.h:179
std::vector< Decorator * > decorators
Definition unittest.h:181
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:206
std::string file
Definition unittest.h:177
unsigned line
Definition unittest.h:178
std::vector< TestCase * > subcases
Definition unittest.h:180
TestCase(std::string name, std::string file, unsigned line, std::vector< Decorator * > decorators)
Construct a new Test Case object.
Definition unittest.h:195
std::string name
Definition unittest.h:176
Definition unittest.h:225
std::vector< T > objects
Definition unittest.h:227
void add(T &&obj)
Definition unittest.h:226
UnitTest is a class that holds the test configuration. There are several options that can be set to c...
Definition unittest.h:131
std::string binary
Definition unittest.h:162
bool list_test_cases
Definition unittest.h:157
bool run_fuzz
Definition unittest.h:156
bool run_bench
Definition unittest.h:155
bool log_to_report
Definition unittest.h:159
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:160
bool no_color
Definition unittest.h:158
struct Filters * filters
Definition unittest.h:163
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:161
int run()
Run the test.
Definition unittest.cpp:256
bool silent
Definition unittest.h:154
regReporter is a class that is used to register the reporter. It will be used as global variables and...
Definition unittest.h:290
regTest is a class that is used to register the test case. It will be used as global variables and th...
Definition unittest.h:282