-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtest.hpp
88 lines (73 loc) · 2.15 KB
/
test.hpp
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
87
88
#pragma once
#include <libcore/fmt/fmt.hpp>
#include <libcore/fmt/log.hpp>
#include <libcore/result.hpp>
struct Test
{
const char *name;
core::Result<void> (*func)();
bool expect_failure;
const char *expected_failure_msg;
};
struct TestGroupResult
{
size_t count;
size_t failed;
TestGroupResult() : count(0), failed(0) {}
void add_result(core::Result<void> &&result)
{
count++;
if (!result)
{
failed++;
}
}
};
struct TestGroup
{
bool is_end;
const char *name;
size_t count;
union
{
Test *tests;
TestGroup *childs;
};
TestGroup() : is_end(false) {}
TestGroupResult execute(int depth)
{
TestGroupResult result;
log::log$("Executing test group: {}", name);
if (is_end)
{
for (size_t i = 0; i < count; i++)
{
auto test_result = tests[i].func();
if (!test_result)
{
log::err$(" * [FAILED] Test {} with error: {}", tests[i].name, test_result.error());
}
log::log$(" * [OK] Test {} ", tests[i].name);
result.add_result(core::move(test_result));
}
log::log$("Test group {} finished with {} failed tests out of {}", name, result.failed, result.count);
return result;
}
for (size_t i = 0; i < count; i++)
{
auto child_result = childs[i].execute(depth + 1);
result.count += child_result.count;
result.failed += child_result.failed;
if (child_result.failed > 0)
{
log::err$(" - [FAILED] Test group {} with {} failed tests out of {}", childs[i].name, child_result.failed, child_result.count);
}
else
{
log::log$(" - [OK] Test group {} finished with {} failed tests out of {}", childs[i].name, child_result.failed, child_result.count);
}
}
log::log$("Test group {} finished with {} failed tests out of {}", name, result.failed, result.count);
return result;
}
};