-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_exception_test.cpp
385 lines (350 loc) · 14.9 KB
/
config_exception_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <fmt/format.h>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include <filesystem>
#include <string>
#include <string_view>
#include <tao/pegtl.hpp>
#include <tao/pegtl/contrib/parse_tree.hpp>
#include "flexi_cfg/config/actions.h"
#include "flexi_cfg/config/exceptions.h"
#include "flexi_cfg/config/grammar.h"
#include "flexi_cfg/config/parser-internal.h"
#include "flexi_cfg/logger.h"
#include "flexi_cfg/parser.h"
#include "flexi_cfg/reader.h"
namespace {
template <typename INPUT>
auto parse(INPUT& input, flexi_cfg::config::ActionData out = flexi_cfg::config::ActionData()) {
setLevel(flexi_cfg::logger::Severity::WARN);
return flexi_cfg::config::internal::parseCore<peg::must<flexi_cfg::config::grammar>,
flexi_cfg::config::action>(input, out);
}
auto parse(std::filesystem::path& input) {
peg::file_input cfg_file(input);
return parse(cfg_file, flexi_cfg::config::ActionData{input.parent_path()});
}
} // namespace
TEST(ConfigException, ParseError) {
{
const std::vector parse_error = {
"struct test1 { \n\
key = # Missing value produces a parse_error \n\
}",
"foo.bar = 1 # Can't mix flat keys with struct in same file. \n\
struct test1 { \n\
bar = 0 \n\
}",
"struct test1 { \n\
bar = 0 \n\
} \n\
foo.bar = 1 # Can't mix flat keys with struct in same file.",
"include foo.cfg # File does not exist",
"include_relative foo.cfg # File does not exist",
"include ${abc # Missing end curly brace",
"include_relative ${abc # Missing end curly brace"};
for (const auto& input : parse_error) {
peg::memory_input in_cfg(input, "From content");
EXPECT_THROW(parse(in_cfg), peg::parse_error) << "Input file: " << in_cfg.source();
}
}
}
TEST(ConfigException, MismatchedKey) {
{
const std::string_view mismatched_key =
"\n\
struct test1 { \n\
key = \"value\" \n\
} # This end key should match the 'struct' key, and will produce an exception.";
peg::memory_input in_cfg(mismatched_key, "No trailing newline");
EXPECT_THROW(parse(in_cfg), peg::parse_error) << "Input file: " << in_cfg.source();
}
}
TEST(ConfigException, DuplicateKeyException) {
{
const std::string_view duplicate_in_struct =
"struct test1 { \n\
key1 = \"value\" \n\
key2 = 0x10 \n\
key1 = -4 # Duplicate key. This should produce an exception. \n\
}\n";
peg::memory_input in_cfg(duplicate_in_struct, "key1 defined twice");
EXPECT_THROW(parse(in_cfg), flexi_cfg::config::DuplicateKeyException)
<< "Input file: " << in_cfg.source();
}
{
const std::string_view ref_proto_failure =
"proto proto_foo { \n\
bar = 0 \n\
baz = $BAZ \n\
} \n\
\n\
reference proto_foo as test2 { \n\
$BAZ = \"baz\" \n\
+bar = 0 \n\
}\n";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(ref_proto_failure, "ref_proto_failure"),
flexi_cfg::config::DuplicateKeyException);
}
#if 0 // See FULLPAIR action
{
const std::string_view duplicate_full_pair =
"this.is.a.key = 10 \n\
another.key = -1.2 \n\
this.is.a.key = \"again\" \n";
peg::memory_input in_cfg(duplicate_full_pair, "this.is.a.key defined twice");
EXPECT_THROW(parse(in_cfg), flexi_cfg::config::DuplicateKeyException);
}
#endif
{
const std::string_view var_add_duplicate =
"proto proto_foo { \n\
bar = 0 \n\
baz = $BAZ \n\
} \n\
\n\
reference proto_foo as test2 { \n\
+bar = -1.2 \n\
$BAZ = \"baz\" \n\
+bar = 0 \n\
}\n";
peg::memory_input in_cfg(var_add_duplicate, "test2.bar defined twice");
EXPECT_THROW(parse(in_cfg), flexi_cfg::config::DuplicateKeyException);
}
{
const std::string_view proto_pair_duplicate =
"proto proto_foo { \n\
baz = $BAZ \n\
bar = 0 \n\
baz = $(duplicate.key) \n\
}\n";
peg::memory_input in_cfg(proto_pair_duplicate, "proto_foo.baz defined twice");
EXPECT_THROW(parse(in_cfg), flexi_cfg::config::DuplicateKeyException);
}
{
const auto in_file = std::filesystem::path(EXAMPLE_DIR) / "invalid" / "config_malformed3.cfg";
EXPECT_THROW(flexi_cfg::Parser::parse(in_file), flexi_cfg::config::DuplicateKeyException);
}
}
namespace {
auto structLikes() -> const std::vector<std::string_view>& {
static const std::vector<std::string_view> struct_likes = {
"struct fizz { \n\
buzz = \"buzz\" \n\
}",
"reference bar as fizz { \n\
$BUZZ = \"buzz\" \n\
}",
"proto fizz { \n\
buzz = $BUZZ \n\
}"};
return struct_likes;
}
} // namespace
class DuplicateKeys
: public testing::TestWithParam<std::tuple<std::string_view, std::string_view>> {};
TEST_P(DuplicateKeys, Exception) {
const std::string_view cfg_base =
"struct foo {{ \n\
bar = 0 \n\
baz = -1.2 \n\
\n\
{} \n\
\n\
{} \n\
}}\n";
const std::string cfg = fmt::vformat(
cfg_base, fmt::make_format_args(std::get<0>(GetParam()), std::get<1>(GetParam())));
// std::cout << "Test config: \n" << cfg << std::endl;
peg::memory_input in_cfg(cfg, "duplicate 'fizz'");
EXPECT_THROW(parse(in_cfg), flexi_cfg::config::DuplicateKeyException) << "Input config:\n" << cfg;
}
INSTANTIATE_TEST_SUITE_P(ConfigException, DuplicateKeys,
testing::Combine(testing::ValuesIn(structLikes()),
testing::ValuesIn(structLikes())));
TEST(ConfigException, InvalidKeyException) {
{
const std::string_view invalid_key_reference =
// Here we have 'key' referencing a key 'test1.key2' that doesn't exist.
"struct test1 { \n\
key = $(test1.key2) \n\
}\n";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(invalid_key_reference, "invalid key reference"),
flexi_cfg::config::InvalidKeyException);
}
{
// This can also occur if the key exists but is of the wrong type
const std::string_view not_a_struct =
// Here we have 'key' referencing a key 'test1.key3.bar' where 'test.key3' doesn't exist.
"struct test1 { \n\
key = $(test1.key3.bar) \n\
}\n";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(not_a_struct, "not a struct"),
flexi_cfg::config::InvalidKeyException);
}
}
TEST(ConfigException, InvalidTypeException) {
{
// This can also occur if the key exists but is of the wrong type
const std::string_view key_wrong_type =
// Here we have 'key' referencing a key 'test1.key3.bar' where 'test.key3' doesn't exist.
"struct test1 { \n\
key = $(test1.key3.bar) \n\
key3 = 0 \n\
}\n";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(key_wrong_type, "key wrong type"),
flexi_cfg::config::InvalidTypeException);
}
{
const std::string_view non_numeric_in_expression =
"struct foo { \n\
key1 = \"not a number\" \n\
key2 = {{ 0.5 * $(foo.key1) }} \n\
}\n";
EXPECT_THROW(
flexi_cfg::Parser::parseFromString(non_numeric_in_expression, "non numeric in expression"),
flexi_cfg::config::InvalidTypeException);
}
}
TEST(ConfigException, UndefinedReferenceVarException) {
{
const std::string_view proto_var_doesnt_exist =
"proto foo_proto { \n\
key1 = $KEY1 \n\
key2 = $KEY2 \n\
} \n\
\n\
reference foo_proto as foo { \n\
$KEY1 = 0 \n\
#$KEY2 undefined \n\
}\n";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(proto_var_doesnt_exist, "$KEY2 is undefined"),
flexi_cfg::config::UndefinedReferenceVarException);
}
{
const std::string_view extra_proto_var =
"proto foo_proto { \n\
key1 = $KEY1 \n\
key2 = $KEY2 \n\
} \n\
\n\
reference foo_proto as foo { \n\
$KEY1 = 0 \n\
$KEY2 = \"defined\" \n\
$EXTRA_KEY = 0 # not useful, but not an error \n\
}\n";
EXPECT_NO_THROW(flexi_cfg::Parser::parseFromString(extra_proto_var, "$EXTRA_KEY is unused"));
}
{
const auto in_file = std::filesystem::path(EXAMPLE_DIR) / "invalid" / "config_malformed2.cfg";
EXPECT_THROW(flexi_cfg::Parser::parse(in_file),
flexi_cfg::config::UndefinedReferenceVarException);
}
}
TEST(ConfigException, UndefinedProtoException) {
{
const std::string_view undefined_proto =
"proto foo_proto { \n\
key1 = $KEY1 \n\
key2 = $KEY2 \n\
} \n\
\n\
reference bar_proto as bar { \n\
$KEY1 = 0 \n\
$KEY2 = \"defined\" \n\
}\n";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(undefined_proto, "bar_proto not defined"),
flexi_cfg::config::UndefinedProtoException);
}
}
TEST(ConfigException, DuplicateOverrideException) {
{
const std::string_view duplicate_override =
"struct foo { \n\
key1 [override] = -3 \n\
key4 = false \n\
} \n\
\n\
struct foo { \n\
key1 = 0 \n\
key2 = 1.2 \n\
} \n\
\n\
struct foo { \n\
key1 [override] = 10 \n\
key3 = \"string\" \n\
}\n";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(duplicate_override, "duplicate override"),
flexi_cfg::config::DuplicateOverrideException);
}
}
TEST(ConfigException, InvalidOverrideException) {
// There are two flavors of InvalidOverrideException:
// 1) Attempting to override a key that doesn't exist elsewhere in the config
// 2) Attempting to override a key with a type that is different from the original
{ // Type 1
const std::string_view invalid_override = R"(
my_key = 4
mykey [override] = 0 # Mis-spelled key
)";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(invalid_override, "invalid override"),
flexi_cfg::config::InvalidOverrideException);
}
{ // Type 2
const std::string_view invalid_override_type = R"(
my_key = 4
my_key [override] = "string" # Type mismatch
)";
EXPECT_THROW(flexi_cfg::Parser::parseFromString(invalid_override_type, "invalid override"),
flexi_cfg::config::InvalidOverrideException);
}
}
class CyclicReference : public testing::TestWithParam<std::string> {};
TEST_P(CyclicReference, Exception) {
const auto in_file = std::filesystem::path(EXAMPLE_DIR) / "invalid" / GetParam();
EXPECT_THROW(flexi_cfg::Parser::parse(in_file), flexi_cfg::config::CyclicReferenceException)
<< "Input file: " << in_file;
}
INSTANTIATE_TEST_SUITE_P(ConfigException, CyclicReference,
testing::Values("config_cyclic1.cfg", "config_cyclic2.cfg"));
class InvalidConfig : public testing::TestWithParam<std::string> {};
TEST_P(InvalidConfig, Exception) {
const auto in_file = std::filesystem::path(EXAMPLE_DIR) / "invalid" / GetParam();
EXPECT_THROW(flexi_cfg::Parser::parse(in_file), flexi_cfg::config::InvalidConfigException)
<< "Input file: " << in_file;
}
INSTANTIATE_TEST_SUITE_P(ConfigException, InvalidConfig, testing::Values("config_malformed4.cfg"));
TEST(ConfigException, EmptyConfig) {
{
const auto in_file = std::filesystem::path(EXAMPLE_DIR) / "invalid" / "config_empty.cfg";
peg::file_input in_cfg(in_file);
EXPECT_THROW(parse(in_cfg), peg::parse_error);
}
}
TEST(IncludeTests, DuplicateInclude) {
auto in_cfg = std::filesystem::path(EXAMPLE_DIR) / "nested/dupe_include.cfg";
EXPECT_THAT([&]() { parse(in_cfg); },
Throws<peg::parse_error>(testing::Property(
&peg::parse_error::message, testing::HasSubstr("duplicate includes"))));
}
TEST(IncludeTests, DuplicateIncludeOnce) {
auto in_cfg = std::filesystem::path(EXAMPLE_DIR) / "nested/dupe_include2.cfg";
EXPECT_NO_THROW(parse(in_cfg));
}
TEST(IncludeTests, NonOptionalMissingIncludeThrows) {
auto in_cfg = std::filesystem::path(EXAMPLE_DIR) / "optional/optional_config_non_optional.cfg";
EXPECT_THAT([&]() { parse(in_cfg); },
testing::ThrowsMessage<peg::parse_error>(testing::HasSubstr("Missing include file")));
}
TEST(IncludeTests, OptionalMissingIncludeIsFine) {
auto in_cfg = std::filesystem::path(EXAMPLE_DIR) / "optional/optional_config.cfg";
EXPECT_NO_THROW(parse(in_cfg));
}
TEST(IncludeTests, BothOptionalMissingIncludeIsFine) {
auto in_cfg = std::filesystem::path(EXAMPLE_DIR) / "optional/optional_config2.cfg";
EXPECT_NO_THROW(parse(in_cfg));
}
TEST(IncludeTests, OptionalMissingAllIncludesIsFine) {
auto in_cfg = std::filesystem::path(EXAMPLE_DIR) / "optional/optional_config3.cfg";
EXPECT_NO_THROW(parse(in_cfg));
}