-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoYAML_test.cpp
104 lines (82 loc) · 1.99 KB
/
AutoYAML_test.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#define CATCH_CONFIG_MAIN
#include "catch2/catch.hpp"
#include "AutoYAML_example.h"
#include "AutoYAML_example.AutoYAML.h"
using namespace std::literals::chrono_literals;
namespace {
static std::string AutoYAML_example_no_default_YAML {
R"(
s: some string
b: true
i: 42
d: 42
e: E2
v:
- 1
- 2
- 3
l:
- 4
- 5
- 6
m:
1: 2
3: 4
5: 6
n:
i: 42
sec: 10)"
};
static std::string AutoYAML_example_default_YAML {
AutoYAML_example_no_default_YAML +
R"(
def: 123)"
};
}
namespace YAML {
template<typename Rep, typename Period>
struct convert<std::chrono::duration<Rep, Period>>
{
static Node encode(const std::chrono::duration<Rep, Period> &obj) {
return Node(obj.count());
}
static bool decode(Node const &node, std::chrono::duration<Rep, Period> &obj) {
obj = std::chrono::duration<Rep, Period> { node.as<std::size_t>() };
return true;
}
};
} // end namespace YAML
TEST_CASE("YAML files are correctly decoded", "[decode]")
{
auto node { YAML::Load(AutoYAML_example_no_default_YAML.substr(1)) };
auto example { node.as<AutoYAML_example>() };
CHECK(example.s == "some string");
CHECK(example.b == true);
CHECK(example.i == 42);
CHECK(example.d == 42.0);
CHECK(example.e == AutoYAML_example::E::E2);
CHECK(example.v == std::vector<int>{1, 2, 3});
CHECK(example.l == std::list<int>{4, 5, 6});
CHECK(example.m == std::map<int, int>{{1, 2}, {3, 4}, {5, 6}});
CHECK(example.n.i == 42);
CHECK(example.sec == 10s);
CHECK(example.def == 123);
}
TEST_CASE("C++ objects are correctly encoded", "[encode]")
{
AutoYAML_example example;
example.s = "some string";
example.b = true;
example.i = 42;
example.d = 42.0;
example.e = AutoYAML_example::E::E2;
example.v = std::vector<int>{1, 2, 3};
example.l = std::list<int>{4, 5, 6};
example.m = std::map<int, int>{{1, 2}, {3, 4}, {5, 6}};
example.n.i = 42;
example.sec = 10s;
YAML::Node node { example };
YAML::Emitter out;
out << node;
CHECK(out.c_str() == AutoYAML_example_default_YAML.substr(1));
}