-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_framework.hpp
More file actions
86 lines (73 loc) · 3.97 KB
/
Copy pathtest_framework.hpp
File metadata and controls
86 lines (73 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// A minimal, header-only, zero-dependency test harness.
//
// TEST_CASE("name") { ... } registers a test function that the runner in
// test_main.cpp discovers and executes automatically. CHECK records a
// failure and keeps running the test; REQUIRE aborts the current test case.
#pragma once
#include <exception>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
namespace testfw {
struct TestFailure : std::exception {
std::string message;
explicit TestFailure(std::string m) : message(std::move(m)) {}
const char* what() const noexcept override { return message.c_str(); }
};
struct TestCase {
std::string name;
std::function<void()> fn;
};
inline std::vector<TestCase>& registry() {
static std::vector<TestCase> tests;
return tests;
}
struct Registrar {
Registrar(std::string name, std::function<void()> fn) {
registry().push_back({std::move(name), std::move(fn)});
}
};
inline int& failure_count() {
static int count = 0;
return count;
}
} // namespace testfw
#define TESTFW_CONCAT_(a, b) a##b
#define TESTFW_CONCAT(a, b) TESTFW_CONCAT_(a, b)
#define TEST_CASE(name) \
static void TESTFW_CONCAT(testfw_fn_, __LINE__)(); \
static ::testfw::Registrar TESTFW_CONCAT(testfw_reg_, __LINE__)( \
name, TESTFW_CONCAT(testfw_fn_, __LINE__)); \
static void TESTFW_CONCAT(testfw_fn_, __LINE__)()
#define CHECK(expr) \
do { \
if (!(expr)) { \
std::cerr << " CHECK failed: " #expr " at " << __FILE__ << ":" << __LINE__ \
<< "\n"; \
++::testfw::failure_count(); \
} \
} while (0)
#define REQUIRE(expr) \
do { \
if (!(expr)) { \
std::ostringstream oss; \
oss << "REQUIRE failed: " #expr " at " << __FILE__ << ":" << __LINE__; \
throw ::testfw::TestFailure(oss.str()); \
} \
} while (0)
#define CHECK_THROWS_AS(expr, ExceptionType) \
do { \
bool testfw_threw = false; \
try { \
(void)(expr); \
} catch (const ExceptionType&) { \
testfw_threw = true; \
} \
if (!testfw_threw) { \
std::cerr << " CHECK_THROWS_AS failed: " #expr " did not throw " \
#ExceptionType " at " << __FILE__ << ":" << __LINE__ << "\n"; \
++::testfw::failure_count(); \
} \
} while (0)