ZeroErr
载入中...
搜索中...
未找到
unittest.h
浏览该文件的文档.
1#pragma once
2
3// libc++ offers compatibility-only transitive includes through C++20.
4// Suppress those while loading our explicit dependencies, then restore the
5// caller's setting. This does not change standard-library types or ABI.
6#pragma push_macro("_LIBCPP_REMOVE_TRANSITIVE_INCLUDES")
7#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
8#define _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
9#endif
10
11
13
15#include <iosfwd>
16#include <functional>
17#include "zeroerr/string.h"
18#include <vector>
19
21
22#define ZEROERR_CREATE_TEST_FUNC(function, name, ...) \
23 ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH \
24 static void function(zeroerr::TestContext*); \
25 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
26 zeroerr::TestCase(name, __FILE__, __LINE__, function, {__VA_ARGS__})); \
27 ZEROERR_SUPPRESS_COMMON_WARNINGS_POP; \
28 static void function(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
29
30#define TEST_CASE(...) \
31 ZEROERR_EXPAND(ZEROERR_CREATE_TEST_FUNC(ZEROERR_NAMEGEN(_zeroerr_testcase), __VA_ARGS__))
32
33#define ZEROERR_CREATE_SUB_CASE(name, ...) \
34 zeroerr::SubCase(name, __FILE__, __LINE__, _ZEROERR_TEST_CONTEXT, {__VA_ARGS__}) \
35 << [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT)) mutable
36
37// The caller supplies the lambda body, so no diagnostic pragma may follow
38// the lambda declarator (GCC rejects it before the opening brace).
39#define SUB_CASE(...) ZEROERR_EXPAND(ZEROERR_CREATE_SUB_CASE(__VA_ARGS__))
40
41#define ZEROERR_CREATE_TEST_CLASS(fixture, classname, funcname, name, ...) \
42 class classname : public fixture { \
43 public: \
44 void funcname(zeroerr::TestContext*); \
45 }; \
46 static void ZEROERR_CAT(call_, funcname)(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT) { \
47 classname instance; \
48 instance.funcname(_ZEROERR_TEST_CONTEXT); \
49 } \
50 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
51 zeroerr::TestCase(name, __FILE__, __LINE__, ZEROERR_CAT(call_, funcname), \
52 {__VA_ARGS__})); \
53 inline void classname::funcname(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
54
55#define TEST_CASE_FIXTURE(fixture, ...) \
56 ZEROERR_EXPAND(ZEROERR_CREATE_TEST_CLASS(fixture, ZEROERR_NAMEGEN(_zeroerr_class), \
57 ZEROERR_NAMEGEN(_zeroerr_test_method), __VA_ARGS__))
58
59
60#define ZEROERR_HAVE_SAME_OUTPUT _ZEROERR_TEST_CONTEXT->save_output();
61
62#ifndef ZEROERR_DISABLE_BDD
63#define SCENARIO(...) TEST_CASE("Scenario: " __VA_ARGS__)
64#define GIVEN(...) SUB_CASE("given: " __VA_ARGS__)
65#define WHEN(...) SUB_CASE("when: " __VA_ARGS__)
66#define THEN(...) SUB_CASE("then: " __VA_ARGS__)
67#endif
68
69namespace zeroerr {
70
71class IReporter;
72struct TestCase;
73class Decorator;
74
88public:
89 unsigned passed = 0;
90 unsigned warning = 0;
91 unsigned failed = 0;
92 unsigned skipped = 0;
93 unsigned passed_as = 0;
94 unsigned warning_as = 0;
95 unsigned failed_as = 0;
96 unsigned skipped_as = 0;
97
99
101
107 int add(TestContext& local);
108
112 void reset();
113
117 void save_output();
118
124 ~TestContext() = default;
125};
126
144struct UnitTest {
151 UnitTest& parseArgs(int argc, const char** argv);
152
157 int run();
158
165 bool run_filter(const TestCase& tc);
166
167 bool silent = false;
168 bool run_bench = false;
169 bool run_fuzz = false;
170 bool list_test_cases = false;
172 bool no_color = false;
173 bool log_to_report = false;
174 bool show_help = false;
175 bool invalid_args = false;
179 struct Filters* filters = nullptr;
180};
181
191struct TestCase {
194 unsigned line;
195 std::function<void(TestContext*)> func;
196 std::vector<TestCase*> subcases;
197 std::vector<Decorator*> decorators;
203 bool operator<(const TestCase& rhs) const;
204
213
223 std::function<void(TestContext*)> func, std::vector<Decorator*> decorators)
225};
226
227
233 std::vector<Decorator*> decorators);
234 ~SubCase() = default;
236 void operator<<(std::function<void(TestContext*)> op);
237};
238
239
240template <typename T>
242 void add(T&& obj) { objects.push_back(std::forward<T>(obj)); }
243 std::vector<T> objects;
244};
245
246
259public:
260 virtual ~IReporter() = default;
261
262 virtual zeroerr::String getName() const = 0;
263
264 // There are a list of events
265 virtual void testStart() = 0;
266 virtual void testCaseStart(const TestCase& tc, std::stringbuf& sb) = 0;
267 virtual void testCaseEnd(const TestCase& tc, std::stringbuf& sb, const TestContext& ctx,
268 int type) = 0;
269 virtual void subCaseStart(const TestCase& tc, std::stringbuf& sb) = 0;
270 virtual void subCaseEnd(const TestCase& tc, std::stringbuf& sb, const TestContext& ctx,
271 int type) = 0;
272 virtual void testEnd(const TestContext& tc) = 0;
273
279 static IReporter* create(const zeroerr::String& name, UnitTest& ut);
280
282
283protected:
285};
286
290enum TestType { test_case = 1, sub_case = 1 << 1, bench = 1 << 2, fuzz_test = 1 << 3 };
291
292namespace detail {
293
298struct regTest {
299 explicit regTest(const TestCase& tc, TestType type = test_case);
300};
301
307 explicit regReporter(IReporter*);
308};
309} // namespace detail
310
311
328public:
329 CombinationalTest(std::function<void()> func) : func(func) {}
330 std::function<void()> func;
331
332 template <typename T>
333 void operator()(T& arg) {
334 arg.reset();
335 for (size_t i = 0; i < arg.size(); ++i, ++arg) {
336 func();
337 }
338 }
339
340 template <typename T, typename... Args>
341 void operator()(T& arg, Args&... args) {
342 arg.reset();
343 for (size_t i = 0; i < arg.size(); ++i, ++arg) {
344 operator()(args...);
345 }
346 }
347};
348
349
353template <typename T>
354class TestArgs {
355public:
356 TestArgs(std::initializer_list<T> args) : args(args) {}
357 std::vector<T> args;
358
359 operator T() const { return args[index]; }
361 index++;
362 return *this;
363 }
364 size_t size() const { return args.size(); }
365 void reset() { index = 0; }
366
367private:
368 int index = 0;
369};
370
371
373public:
374 // Called when the test registered, return true can block the test registering
375 virtual bool onStartup(const TestCase&) { return false; }
376
377 // Called when the test executing, return true can block the test execution
378 virtual bool onExecution(const TestCase&) { return false; }
379
380 // Called on each assertion, return true can skip the assertion
381 virtual bool onAssertion() { return false; }
382
383 // Called when the test finished, return true means the test containing changes
384 virtual bool onFinish(const TestCase&, TestContext&) { return false; }
385};
386
387Decorator* skip(bool isSkip = true);
388Decorator* timeout(float timeout = 0.1f); // in seconds
389Decorator* may_fail(bool isMayFail = true);
390Decorator* should_fail(bool isShouldFail = true);
391
392} // namespace zeroerr
393
395
396#pragma pop_macro("_LIBCPP_REMOVE_TRANSITIVE_INCLUDES")
CombinationalTest is a class that is used to cross test a few lists of arguments. One example
Definition unittest.h:327
std::function< void()> func
Definition unittest.h:330
void operator()(T &arg, Args &... args)
Definition unittest.h:341
void operator()(T &arg)
Definition unittest.h:333
CombinationalTest(std::function< void()> func)
Definition unittest.h:329
Definition unittest.h:372
virtual bool onAssertion()
Definition unittest.h:381
virtual bool onStartup(const TestCase &)
Definition unittest.h:375
virtual bool onExecution(const TestCase &)
Definition unittest.h:378
virtual bool onFinish(const TestCase &, TestContext &)
Definition unittest.h:384
IReporter is an interface that is used to report the test results. You can create a new reporter by i...
Definition unittest.h:258
virtual zeroerr::String getName() const =0
virtual void subCaseStart(const TestCase &tc, std::stringbuf &sb)=0
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
static IReporter * create(const zeroerr::String &name, UnitTest &ut)
Create the reporter object with the given name.
Definition unittest.cpp:899
virtual void testEnd(const TestContext &tc)=0
UnitTest & ut
Definition unittest.h:284
IReporter(UnitTest &ut)
Definition unittest.h:281
virtual void testCaseEnd(const TestCase &tc, std::stringbuf &sb, const TestContext &ctx, int type)=0
Definition string.h:41
TestArgs is a class that is used to store the test arguments.
Definition unittest.h:354
TestArgs & operator++()
Definition unittest.h:360
size_t size() const
Definition unittest.h:364
std::vector< T > args
Definition unittest.h:357
TestArgs(std::initializer_list< T > args)
Definition unittest.h:356
void reset()
Definition unittest.h:365
TestContext is a class that holds the test results and reporter context. There are 8 different matric...
Definition unittest.h:87
void reset()
Reset the matrices to 0.
Definition unittest.cpp:70
unsigned skipped
Definition unittest.h:92
unsigned failed
Definition unittest.h:91
unsigned passed
Definition unittest.h:89
Duration duration
Definition unittest.h:98
void save_output()
Save the output of the test to the correct_output_path as a golden file.
Definition unittest.cpp:50
unsigned skipped_as
Definition unittest.h:96
unsigned passed_as
Definition unittest.h:93
unsigned failed_as
Definition unittest.h:95
IReporter & reporter
Definition unittest.h:100
unsigned warning
Definition unittest.h:90
unsigned warning_as
Definition unittest.h:94
TestContext(IReporter &reporter)
Construct a new Test Context object
Definition unittest.h:123
int add(TestContext &local)
Add the subtest results to the matrices.
Definition unittest.cpp:29
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_POP
Definition config.h:283
#define ZEROERR_SUPPRESS_COMMON_WARNINGS_PUSH
Definition config.h:235
Definition assert_text.cpp:5
Decorator * should_fail(bool isShouldFail)
Definition unittest.cpp:982
TestType
TestType is a enum describe the type of the test case.
Definition unittest.h:290
@ bench
Definition unittest.h:290
@ test_case
Definition unittest.h:290
@ fuzz_test
Definition unittest.h:290
@ sub_case
Definition unittest.h:290
Decorator * timeout(float timeout)
Definition unittest.cpp:932
Decorator * may_fail(bool isMayFail)
Definition unittest.cpp:976
Decorator * skip(bool isSkip)
Definition unittest.cpp:910
Definition time.h:3
Definition unittest.cpp:110
SubCase is a class that holds the subcase information.
Definition unittest.h:231
TestContext * context
Definition unittest.h:235
void operator<<(std::function< void(TestContext *)> op)
Definition unittest.cpp:87
~SubCase()=default
TestCase is a class that holds the test case information. There are several fields that are used to s...
Definition unittest.h:191
bool operator<(const TestCase &rhs) const
Compare the test cases.
Definition unittest.cpp:390
std::function< void(TestContext *)> func
Definition unittest.h:195
std::vector< Decorator * > decorators
Definition unittest.h:197
zeroerr::String file
Definition unittest.h:193
unsigned line
Definition unittest.h:194
zeroerr::String name
Definition unittest.h:192
std::vector< TestCase * > subcases
Definition unittest.h:196
TestCase(zeroerr::String name, zeroerr::String file, unsigned line, std::vector< Decorator * > decorators)
Construct a new Test Case object
Definition unittest.h:211
TestCase(zeroerr::String name, zeroerr::String file, unsigned line, std::function< void(TestContext *)> func, std::vector< Decorator * > decorators)
Construct a new Test Case object
Definition unittest.h:222
Definition unittest.h:241
std::vector< T > objects
Definition unittest.h:243
void add(T &&obj)
Definition unittest.h:242
UnitTest is a class that holds the test configuration. There are several options that can be set to c...
Definition unittest.h:144
bool show_help
Definition unittest.h:174
bool list_test_cases
Definition unittest.h:170
bool run_fuzz
Definition unittest.h:169
bool invalid_args
Definition unittest.h:175
bool run_bench
Definition unittest.h:168
bool log_to_report
Definition unittest.h:173
zeroerr::String list_format
Definition unittest.h:171
bool run_filter(const TestCase &tc)
Run the test with the given filter.
Definition unittest.cpp:288
zeroerr::String reporter_name
Definition unittest.h:177
bool no_color
Definition unittest.h:172
struct Filters * filters
Definition unittest.h:179
zeroerr::String binary
Definition unittest.h:178
zeroerr::String correct_output_path
Definition unittest.h:176
UnitTest & parseArgs(int argc, const char **argv)
Parse the arguments to configure the test.
Definition unittest.cpp:137
int run()
Run the test.
Definition unittest.cpp:318
bool silent
Definition unittest.h:167
regReporter is a class that is used to register the reporter. It will be used as global variables and...
Definition unittest.h:306
regTest is a class that is used to register the test case. It will be used as global variables and th...
Definition unittest.h:298