-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_toml.cpp
More file actions
83 lines (72 loc) · 2.12 KB
/
test_toml.cpp
File metadata and controls
83 lines (72 loc) · 2.12 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
#include <gtest/gtest.h>
import std;
import mcpp.libs.toml;
namespace t = mcpp::libs::toml;
TEST(Toml, EmptyDocumentParses) {
auto d = t::parse("");
ASSERT_TRUE(d.has_value());
EXPECT_TRUE(d->root().empty());
}
TEST(Toml, SimpleKeyValue) {
auto d = t::parse("key = \"value\"\nnum = 42\nflag = true");
ASSERT_TRUE(d.has_value()) << d.error().message;
EXPECT_EQ(d->get_string("key").value_or(""), "value");
EXPECT_EQ(d->get_int("num").value_or(-1), 42);
EXPECT_EQ(d->get_bool("flag").value_or(false), true);
}
TEST(Toml, NestedTables) {
auto d = t::parse(R"(
[a.b]
x = "deep"
[a]
y = "shallow"
)");
ASSERT_TRUE(d.has_value()) << d.error().message;
EXPECT_EQ(d->get_string("a.b.x").value_or(""), "deep");
EXPECT_EQ(d->get_string("a.y").value_or(""), "shallow");
}
TEST(Toml, ArrayOfStrings) {
auto d = t::parse(R"(items = ["a", "b", "c"])");
ASSERT_TRUE(d.has_value());
auto arr = d->get_string_array("items");
ASSERT_TRUE(arr.has_value());
EXPECT_EQ(arr->size(), 3u);
EXPECT_EQ((*arr)[0], "a");
EXPECT_EQ((*arr)[2], "c");
}
TEST(Toml, ArrayAllowsTrailingComma) {
auto d = t::parse(R"(
items = [
"a",
"b",
]
)");
ASSERT_TRUE(d.has_value()) << d.error().message;
auto arr = d->get_string_array("items");
ASSERT_TRUE(arr.has_value());
EXPECT_EQ(*arr, std::vector<std::string>({"a", "b"}));
}
TEST(Toml, EscapedString) {
auto d = t::parse(R"(s = "a\tb\nc")");
ASSERT_TRUE(d.has_value());
EXPECT_EQ(d->get_string("s").value_or(""), std::string("a\tb\nc"));
}
TEST(Toml, RejectUnterminatedString) {
auto d = t::parse(R"(x = "no end)");
EXPECT_FALSE(d.has_value());
}
TEST(Toml, CommentsIgnored) {
auto d = t::parse(R"(
# top comment
x = 1 # trailing isn't supported but full-line is
y = 2
)");
ASSERT_TRUE(d.has_value()) << d.error().message;
EXPECT_EQ(d->get_int("x").value_or(-1), 1);
EXPECT_EQ(d->get_int("y").value_or(-1), 2);
}
TEST(Toml, EscapeStringHelper) {
EXPECT_EQ(t::escape_string("hello"), "\"hello\"");
EXPECT_EQ(t::escape_string("a\"b"), "\"a\\\"b\"");
EXPECT_EQ(t::escape_string("a\\b"), "\"a\\\\b\"");
}