-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_parse_test.cpp
214 lines (187 loc) · 7.43 KB
/
config_parse_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <gtest/gtest.h>
#include <filesystem>
#include <string>
#include <tao/pegtl.hpp>
#include <tao/pegtl/contrib/parse_tree.hpp>
#include "flexi_cfg/config/actions.h"
#include "flexi_cfg/config/grammar.h"
#include "flexi_cfg/config/parser-internal.h"
#include "flexi_cfg/config/selector.h"
#include "flexi_cfg/logger.h"
#include "flexi_cfg/parser.h"
#include "flexi_cfg/reader.h"
namespace peg = TAO_PEGTL_NAMESPACE;
class InputString : public testing::TestWithParam<std::string> {};
TEST_P(InputString, ParseTree) {
auto parse = []() {
peg::memory_input in(GetParam(), "From content");
auto root = peg::parse_tree::parse<flexi_cfg::config::grammar, flexi_cfg::config::selector>(in);
};
EXPECT_NO_THROW(parse());
}
TEST_P(InputString, Parse) {
setLevel(flexi_cfg::logger::Severity::INFO);
auto parse = []() {
peg::memory_input in(GetParam(), "From content");
flexi_cfg::config::ActionData out;
return flexi_cfg::config::internal::parseCore<flexi_cfg::config::grammar,
flexi_cfg::config::action>(in, out);
};
bool ret{false};
EXPECT_NO_THROW(ret = parse());
EXPECT_TRUE(ret);
}
TEST_P(InputString, Reader) {
setLevel(flexi_cfg::logger::Severity::INFO);
flexi_cfg::Reader cfg({}, ""); // Nominally, we wouldn't do this, but we need a mechanism to
// capture the output of 'parse' from within the "try/catch" block
EXPECT_NO_THROW(cfg = flexi_cfg::Parser::parseFromString(GetParam(), "From String"));
EXPECT_TRUE(cfg.exists("test1.key1"));
EXPECT_EQ(cfg.getValue<std::string>("test1.key1"), "value");
EXPECT_EQ(cfg.getType("test1.key1"), flexi_cfg::config::types::Type::kString);
EXPECT_TRUE(cfg.exists("test1.key2"));
EXPECT_FLOAT_EQ(cfg.getValue<float>("test1.key2"), 1.342F);
EXPECT_EQ(cfg.getType("test1.key2"), flexi_cfg::config::types::Type::kNumber);
EXPECT_TRUE(cfg.exists("test1.key3"));
EXPECT_EQ(cfg.getValue<int>("test1.key3"), 10);
EXPECT_EQ(cfg.getType("test1.key3"), flexi_cfg::config::types::Type::kNumber);
EXPECT_TRUE(cfg.exists("test1.f"));
EXPECT_EQ(cfg.getValue<std::string>("test1.f"), "none");
EXPECT_EQ(cfg.getType("test1.f"), flexi_cfg::config::types::Type::kString);
EXPECT_TRUE(cfg.exists("test2.my_key"));
EXPECT_EQ(cfg.getValue<std::string>("test2.my_key"), "foo");
EXPECT_EQ(cfg.getType("test2.my_key"), flexi_cfg::config::types::Type::kString);
EXPECT_TRUE(cfg.exists("test2.n_key"));
EXPECT_EQ(cfg.getValue<bool>("test2.n_key"), true);
EXPECT_EQ(cfg.getType("test2.n_key"), flexi_cfg::config::types::Type::kBoolean);
EXPECT_TRUE(cfg.exists("test2.inner.list"));
EXPECT_EQ(cfg.getType("test2.inner.list"), flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.list"), std::vector({1, 2, 3, 4}));
EXPECT_TRUE(cfg.exists("test2.inner.emptyList"));
EXPECT_EQ(cfg.getType("test2.inner.emptyList"), flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.emptyList"), std::vector<int>({}));
EXPECT_TRUE(cfg.exists("test2.inner.listWithComment"));
EXPECT_EQ(cfg.getType("test2.inner.listWithComment"), flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.listWithComment"), std::vector({0, 2}));
EXPECT_TRUE(cfg.exists("test2.inner.listWithTrailingComment"));
EXPECT_EQ(cfg.getType("test2.inner.listWithTrailingComment"),
flexi_cfg::config::types::Type::kList);
EXPECT_EQ(cfg.getValue<std::vector<int>>("test2.inner.listWithTrailingComment"),
std::vector({0, 2}));
EXPECT_TRUE(cfg.exists("test2"));
EXPECT_EQ(cfg.getType("test2"), flexi_cfg::config::types::Type::kStruct);
EXPECT_TRUE(cfg.exists("test1"));
EXPECT_EQ(cfg.getType("test1"), flexi_cfg::config::types::Type::kStruct);
EXPECT_TRUE(cfg.exists("test2.inner"));
EXPECT_EQ(cfg.getType("test2.inner"), flexi_cfg::config::types::Type::kStruct);
// Coverage for override. The values below should match the value on the following line:
// a [override] = 2
// All of the following variables should match the override value:
for (const auto& key : {"a", "c", "d", "q.e", "g"}) {
EXPECT_TRUE(cfg.exists(key));
EXPECT_EQ(cfg.getValue<float>(key), 2);
EXPECT_EQ(cfg.getType(key), flexi_cfg::config::types::Type::kNumber);
}
// b [override] = 4
// All of the following variables should match the override value:
for (const auto& key : {"b", "e", "f"}) {
EXPECT_TRUE(cfg.exists(key));
EXPECT_EQ(cfg.getValue<float>(key), 4);
EXPECT_EQ(cfg.getType(key), flexi_cfg::config::types::Type::kNumber);
}
}
INSTANTIATE_TEST_SUITE_P(ConfigParse, InputString, testing::Values(std::string(R"(
struct test1 {
key1 = "value"
key2 = 1.342 # test comment here
key3 = 10
f = "none"
}
reference p as q {
$A = $(a)
}
a [override] = 2
b [override] = 4
struct test2 {
my_key = "foo"
n_key = true
struct inner {
list = [1, 2, 3, 4]
emptyList = []
listWithComment = [
# I don't matter
0, 2
]
listWithTrailingComment = [
0,
2# I don't matter
]
}
}
a = 1
b = $(a)
c = {{ $(a) }}
d = $(c)
e = {{ $(b) }}
f = $(e)
g = $(a)
proto p {
e = $A
}
)")));
/// File-based input
class FileInput : public testing::TestWithParam<std::filesystem::path> {};
namespace {
auto baseDir() -> const std::filesystem::path& {
static const std::filesystem::path base_dir = std::filesystem::path(EXAMPLE_DIR);
return base_dir;
}
auto filenameGenerator() -> std::vector<std::filesystem::path> {
std::vector<std::filesystem::path> files;
for (const auto& entry : std::filesystem::directory_iterator(baseDir())) {
if (entry.is_regular_file()) {
if (const auto& file = entry.path().filename().string();
file.starts_with("config_example") && file.ends_with(".cfg")) {
files.emplace_back(file);
}
}
}
std::ranges::sort(files);
return files;
}
} // namespace
TEST_P(FileInput, ParseTree) {
auto parse = []() {
peg::file_input in(baseDir() / GetParam());
auto root = peg::parse_tree::parse<flexi_cfg::config::grammar, flexi_cfg::config::selector>(in);
};
EXPECT_NO_THROW(parse());
}
TEST_P(FileInput, Parse) {
flexi_cfg::logger::setLevel(flexi_cfg::logger::Severity::WARN);
auto parse = []() {
peg::file_input in(baseDir() / GetParam());
flexi_cfg::config::ActionData out{baseDir()};
return flexi_cfg::config::internal::parseCore<flexi_cfg::config::grammar,
flexi_cfg::config::action>(in, out);
};
bool ret{false};
EXPECT_NO_THROW(ret = parse());
EXPECT_TRUE(ret);
}
TEST_P(FileInput, ConfigReaderParse) {
// This test creates a full path to the config files. This works because they are all top level
flexi_cfg::logger::setLevel(flexi_cfg::logger::Severity::WARN);
EXPECT_NO_THROW(flexi_cfg::Parser::parse(baseDir() / GetParam()));
}
TEST_P(FileInput, ConfigReaderParseRootDir) {
// This test calls parse with a relative path to the config file and specifies the root dir
flexi_cfg::logger::setLevel(flexi_cfg::logger::Severity::WARN);
EXPECT_NO_THROW(flexi_cfg::Parser::parse(GetParam(), baseDir()));
}
INSTANTIATE_TEST_SUITE_P(ConfigParse, FileInput, testing::ValuesIn(filenameGenerator()));
TEST(ConfigParse, ConfigRoot) {
setLevel(flexi_cfg::logger::Severity::DEBUG);
EXPECT_NO_THROW(flexi_cfg::Parser::parse(
std::filesystem::path("config_root/test/config_example_base.cfg"), baseDir()));
}