11 #define ZEROERR_CREATE_TEST_FUNC(function, name) \
12 static void function(zeroerr::TestContext*); \
13 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
14 {name, __FILE__, __LINE__, function}); \
15 static void function(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
17 #define TEST_CASE(name) ZEROERR_CREATE_TEST_FUNC(ZEROERR_NAMEGEN(_zeroerr_testcase), name)
19 #define SUB_CASE(name) \
20 zeroerr::SubCase(name, __FILE__, __LINE__, _ZEROERR_TEST_CONTEXT) \
21 << [=](ZEROERR_UNUSED(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT)) mutable
23 #define ZEROERR_CREATE_TEST_CLASS(fixture, classname, funcname, name) \
24 class classname : public fixture { \
26 void funcname(zeroerr::TestContext*); \
28 static void ZEROERR_CAT(call_, funcname)(zeroerr::TestContext * _ZEROERR_TEST_CONTEXT) { \
30 instance.funcname(_ZEROERR_TEST_CONTEXT); \
32 static zeroerr::detail::regTest ZEROERR_NAMEGEN(_zeroerr_reg)( \
33 {name, __FILE__, __LINE__, ZEROERR_CAT(call_, funcname)}); \
34 inline void classname::funcname(ZEROERR_UNUSED(zeroerr::TestContext* _ZEROERR_TEST_CONTEXT))
36 #define TEST_CASE_FIXTURE(fixture, name) \
37 ZEROERR_CREATE_TEST_CLASS(fixture, ZEROERR_NAMEGEN(_zeroerr_class), \
38 ZEROERR_NAMEGEN(_zeroerr_test_method), name)
41 #define ZEROERR_HAVE_SAME_OUTPUT _ZEROERR_TEST_CONTEXT->save_output();
43 #ifndef ZEROERR_DISABLE_BDD
44 #define SCENARIO(...) TEST_CASE("Scenario: " __VA_ARGS__)
45 #define GIVEN(...) SUB_CASE("given: " __VA_ARGS__)
46 #define WHEN(...) SUB_CASE("when: " __VA_ARGS__)
47 #define THEN(...) SUB_CASE("then: " __VA_ARGS__)
204 template <
typename T>
206 void add(T&& obj) {
objects.push_back(std::forward<T>(obj)); }
296 template <
typename T>
299 for (
size_t i = 0; i < arg.size(); ++i, ++arg) {
304 template <
typename T,
typename... Args>
307 for (
size_t i = 0; i < arg.size(); ++i, ++arg) {
317 template <
typename T>
323 operator T()
const {
return args[index]; }
CombinationalTest is a class that is used to cross test a few lists of arguments. One example.
Definition: unittest.h:291
std::function< void()> func
Definition: unittest.h:294
void operator()(T &arg, Args &... args)
Definition: unittest.h:305
void operator()(T &arg)
Definition: unittest.h:297
CombinationalTest(std::function< void()> func)
Definition: unittest.h:293
IReporter is an interface that is used to report the test results. You can create a new reporter by i...
Definition: unittest.h:222
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:782
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:248
IReporter(UnitTest &ut)
Definition: unittest.h:245
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:318
size_t size() const
Definition: unittest.h:328
std::vector< T > args
Definition: unittest.h:321
TestArgs(std::initializer_list< T > args)
Definition: unittest.h:320
TestArgs & operator++()
Definition: unittest.h:324
void reset()
Definition: unittest.h:329
TestContext is a class that holds the test results and reporter context. There are 8 different matric...
Definition: unittest.h:67
void reset()
Reset the matrices to 0.
Definition: unittest.cpp:65
unsigned skipped
Definition: unittest.h:69
unsigned failed
Definition: unittest.h:69
unsigned passed
Definition: unittest.h:69
void save_output()
Save the output of the test to the correct_output_path as a golden file.
Definition: unittest.cpp:45
unsigned skipped_as
Definition: unittest.h:70
unsigned passed_as
Definition: unittest.h:70
unsigned failed_as
Definition: unittest.h:70
IReporter & reporter
Definition: unittest.h:72
unsigned warning
Definition: unittest.h:69
unsigned warning_as
Definition: unittest.h:70
TestContext(IReporter &reporter)
Construct a new Test Context object.
Definition: unittest.h:95
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
TestType
TestType is a enum describe the type of the test case.
Definition: unittest.h:254
@ bench
Definition: unittest.h:254
@ test_case
Definition: unittest.h:254
@ fuzz_test
Definition: unittest.h:254
@ sub_case
Definition: unittest.h:254
Definition: unittest.cpp:104
SubCase is a class that holds the subcase information.
Definition: unittest.h:196
TestContext * context
Definition: unittest.h:199
void operator<<(std::function< void(TestContext *)> op)
Definition: unittest.cpp:81
SubCase(std::string name, std::string file, unsigned line, TestContext *context)
Definition: unittest.cpp:78
TestCase is a class that holds the test case information. There are several fields that are used to s...
Definition: unittest.h:157
bool operator<(const TestCase &rhs) const
Compare the test cases.
Definition: unittest.cpp:281
std::function< void(TestContext *)> func
Definition: unittest.h:161
std::string file
Definition: unittest.h:159
TestCase(std::string name, std::string file, unsigned line)
Construct a new Test Case object.
Definition: unittest.h:177
unsigned line
Definition: unittest.h:160
std::vector< TestCase * > subcases
Definition: unittest.h:162
TestCase(std::string name, std::string file, unsigned line, std::function< void(TestContext *)> func)
Construct a new Test Case object.
Definition: unittest.h:187
std::string name
Definition: unittest.h:158
Definition: unittest.h:205
std::vector< T > objects
Definition: unittest.h:207
void add(T &&obj)
Definition: unittest.h:206
UnitTest is a class that holds the test configuration. There are several options that can be set to c...
Definition: unittest.h:113
std::string binary
Definition: unittest.h:144
bool list_test_cases
Definition: unittest.h:139
bool run_fuzz
Definition: unittest.h:138
bool run_bench
Definition: unittest.h:137
bool log_to_report
Definition: unittest.h:141
bool run_filter(const TestCase &tc)
Run the test with the given filter.
Definition: unittest.cpp:225
std::string correct_output_path
Definition: unittest.h:142
bool no_color
Definition: unittest.h:140
struct Filters * filters
Definition: unittest.h:145
UnitTest & parseArgs(int argc, const char **argv)
Parse the arguments to configure the test.
Definition: unittest.cpp:109
std::string reporter_name
Definition: unittest.h:143
int run()
Run the test.
Definition: unittest.cpp:238
bool silent
Definition: unittest.h:136
regReporter is a class that is used to register the reporter. It will be used as global variables and...
Definition: unittest.h:270
regReporter(IReporter *)
Definition: unittest.cpp:315
regTest is a class that is used to register the test case. It will be used as global variables and th...
Definition: unittest.h:262
regTest(const TestCase &tc, TestType type=test_case)
Definition: unittest.cpp:308