Loading...
Searching...
No Matches
cppunit_base.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Jens Wetterich <jens@wetterich-net.de>
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
16#ifndef CPPUNIT_BASE_H
17#define CPPUNIT_BASE_H
18#if __cplusplus >= 201103L || defined(DOXYGEN)
19#include <array>
20#include <cstdio>
21#include <cstring>
22#include <type_traits>
23#ifndef CPPUNIT_SUITE_CNT
27#define CPPUNIT_SUITE_CNT (10)
28#endif
32namespace riot {
36namespace testing {
42class test {
43private:
44 bool suc = true;
45public:
52 virtual bool run() = 0;
61 void fail() {
62 suc = false;
63 }
68 bool success() const {
69 return suc;
70 }
71};
84protected:
85 bool suc = true;
86 std::array<test*, CPPUNIT_SUITE_CNT> tests{};
87public:
91 virtual void set_up() {
92 }
96 virtual void tear_down() {
97 }
102 virtual const char* get_name() const {
103 return "";
104 };
108 virtual void run() {
109 printf("----\nStarting Test suite %s\n", get_name());
110 for (auto test : tests) {
111 if (test) {
112 suc = test->run() && suc;
113 }
114 }
115 printf("Suite %s completed: %s\n----\n", get_name(), suc ? "SUCCESS" : "FAILURE");
116 }
121 for (int i = 0; i < CPPUNIT_SUITE_CNT; i++) {
122 if (!tests[i]) {
123 tests[i] = test;
124 break;
125 }
126 }
127 }
128};
129}// namespace testing
130}// namespace riot
136#define RUN_SUITE(name) test_suite_##name.run();
137
144#define TEST_SUITE_F(suite, name) \
145 static_assert(sizeof(#suite) > 1, "test fixture class must not be empty"); \
146 static_assert(sizeof(#name) > 1, "test_suite name must not be empty"); \
147 class test_suite_##name : public suite { \
148 const char* get_name() const override { \
149 return #name; \
150 }; \
151 }; \
152 test_suite_##name test_suite_##name;
153
159#define TEST_SUITE(name) TEST_SUITE_F(::riot::testing::test_suite, name)
160
168#define TEST(suite, name) \
169 class Test_##name : public ::riot::testing::test { \
170 private: \
171 void test_body(); \
172 \
173 public: \
174 Test_##name() { \
175 test_suite_##suite.addTest(this); \
176 } \
177 bool run() { \
178 test_suite_##suite.set_up(); \
179 printf("Running test " #name "...\n"); \
180 test_body(); \
181 printf("Test " #name ": %s\n", success() ? "SUCCESS" : "FAILURE"); \
182 test_suite_##suite.tear_down(); \
183 return success(); \
184 }; \
185 }; \
186 void Test_##name::test_body()
187#else
188#error This library needs C++11 and newer
189#endif
190#endif
Test suite base class.
void addTest(test *test)
Run all tests in the suite.
virtual void tear_down()
method to run after each test
virtual const char * get_name() const
get the name of the test suite
virtual void run()
Run all tests in the suite.
std::array< test *, CPPUNIT_SUITE_CNT > tests
array of tests to run
bool suc
Indicates success of all tests after running the suite.
virtual void set_up()
method to run before each test
Test base class.
bool success() const
Check whether the test completed successfully.
void fail()
Indicate failure during test run.
virtual bool run()=0
Run the test.
#define printf(...)
A wrapper for the printf() function that passes arguments through unmodified, but fails to compile if...
Definition stdio.h:60
#define CPPUNIT_SUITE_CNT
Maximum amount of tests in one test suite.
RIOT C++ namespace.
Definition chrono.hpp:35