File: test_parse_table.cpp

package info (click to toggle)
toml11 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,464 kB
  • sloc: cpp: 38,446; makefile: 8; sh: 5
file content (165 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download
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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

#include <iostream>
#include "utility.hpp"
#include <toml11/parser.hpp>
#include <toml11/types.hpp>

#include <doctest/doctest.h>

TEST_CASE("testing a table")
{
    auto spec = toml::spec::v(1,0,0);
    {
        toml::detail::context<toml::type_config> ctx(spec);
        auto loc = toml::detail::make_temporary_location(R"(a = "foo")");

        toml::value table{toml::table()};
        const auto res = toml::detail::parse_table<toml::type_config>(loc, ctx, table);
        REQUIRE_UNARY(res.is_ok());

        REQUIRE_UNARY(table.is_table());

        CHECK_UNARY(table.as_table().at("a").is_string());
        CHECK_EQ   (table.as_table().at("a").as_string(), "foo");
        CHECK_UNARY(table.at("a").is_string());
        CHECK_EQ   (table.at("a").as_string(), "foo");

        CHECK_EQ(table.as_table_fmt().indent_type,    toml::indent_char::none);
        CHECK_EQ(table.as_table_fmt().name_indent,    0);
        CHECK_EQ(table.as_table_fmt().body_indent,    0);
        CHECK_EQ(table.as_table_fmt().closing_indent, 0);
    }

    {
        toml::detail::context<toml::type_config> ctx(spec);
        auto loc = toml::detail::make_temporary_location("  a = \"foo\"");

        toml::value table{toml::table()};
        const auto res = toml::detail::parse_table<toml::type_config>(loc, ctx, table);

        REQUIRE_UNARY(res.is_ok());

        REQUIRE_UNARY(table.is_table());

        CHECK_UNARY(table.as_table().at("a").is_string());
        CHECK_EQ   (table.as_table().at("a").as_string(), "foo");
        CHECK_UNARY(table.at("a").is_string());
        CHECK_EQ   (table.at("a").as_string(), "foo");

        CHECK_EQ(table.as_table_fmt().indent_type,    toml::indent_char::space);
        CHECK_EQ(table.as_table_fmt().name_indent,    0);
        CHECK_EQ(table.as_table_fmt().body_indent,    2);
        CHECK_EQ(table.as_table_fmt().closing_indent, 0);
    }
    {
        toml::detail::context<toml::type_config> ctx(spec);
        auto loc = toml::detail::make_temporary_location("\ta = \"foo\"");

        toml::value table{toml::table()};
        const auto res = toml::detail::parse_table<toml::type_config>(loc, ctx, table);
        REQUIRE_UNARY(res.is_ok());

        REQUIRE_UNARY(table.is_table());

        CHECK_UNARY(table.as_table().at("a").is_string());
        CHECK_EQ   (table.as_table().at("a").as_string(), "foo");
        CHECK_UNARY(table.at("a").is_string());
        CHECK_EQ   (table.at("a").as_string(), "foo");

        CHECK_EQ(table.as_table_fmt().indent_type,    toml::indent_char::tab);
        CHECK_EQ(table.as_table_fmt().name_indent,    0);
        CHECK_EQ(table.as_table_fmt().body_indent,    1);
        CHECK_EQ(table.as_table_fmt().closing_indent, 0);
    }

    {
        toml::detail::context<toml::type_config> ctx(spec);
        auto loc = toml::detail::make_temporary_location(R"(a = "foo"
b = "bar"
c.c1 = "baz"
c.c2 = "qux"
)");

        toml::value table{toml::table()};
        const auto res = toml::detail::parse_table<toml::type_config>(loc, ctx, table);
        REQUIRE_UNARY(res.is_ok());

        REQUIRE_UNARY(table.is_table());

        CHECK_UNARY(table.at("a").is_string());
        CHECK_EQ   (table.at("a").as_string(), "foo");
        CHECK_UNARY(table.at("b").is_string());
        CHECK_EQ   (table.at("b").as_string(), "bar");
        CHECK_UNARY(table.at("c").at("c1").is_string());
        CHECK_EQ   (table.at("c").at("c1").as_string(), "baz");
        CHECK_UNARY(table.at("c").at("c2").is_string());
        CHECK_EQ   (table.at("c").at("c2").as_string(), "qux");

        CHECK_EQ(table.as_table_fmt().indent_type,    toml::indent_char::none);
        CHECK_EQ(table.as_table_fmt().name_indent,    0);
        CHECK_EQ(table.as_table_fmt().body_indent,    0);
        CHECK_EQ(table.as_table_fmt().closing_indent, 0);
    }


    {
        toml::detail::context<toml::type_config> ctx(spec);
        auto loc = toml::detail::make_temporary_location(R"(a = "foo"
b = "bar"
c.c1 = "baz"
[next.table]
c.c2 = "qux"
)");

        toml::value table{toml::table()};
        const auto res = toml::detail::parse_table<toml::type_config>(loc, ctx, table);
        REQUIRE_UNARY(res.is_ok());

        REQUIRE_UNARY(table.is_table());

        CHECK_UNARY(table.at("a").is_string());
        CHECK_EQ   (table.at("a").as_string(), "foo");
        CHECK_UNARY(table.at("b").is_string());
        CHECK_EQ   (table.at("b").as_string(), "bar");
        CHECK_UNARY(table.at("c").at("c1").is_string());
        CHECK_EQ   (table.at("c").at("c1").as_string(), "baz");
        CHECK_UNARY( ! table.at("c").contains("c2"));

        CHECK_EQ(table.as_table_fmt().indent_type,    toml::indent_char::none);
        CHECK_EQ(table.as_table_fmt().name_indent,    0);
        CHECK_EQ(table.as_table_fmt().body_indent,    0);
        CHECK_EQ(table.as_table_fmt().closing_indent, 0);
    }

    {
        toml::detail::context<toml::type_config> ctx(spec);
        auto loc = toml::detail::make_temporary_location(R"(
    a = "foo"
    b = "bar"
    c.c1 = "baz"
  [next.table]
    c.c2 = "qux"
)");

        toml::value table{toml::table()};
        const auto res = toml::detail::parse_table<toml::type_config>(loc, ctx, table);
        REQUIRE_UNARY(res.is_ok());

        REQUIRE_UNARY(table.is_table());

        CHECK_UNARY(table.at("a").is_string());
        CHECK_EQ   (table.at("a").as_string(), "foo");
        CHECK_UNARY(table.at("b").is_string());
        CHECK_EQ   (table.at("b").as_string(), "bar");
        CHECK_UNARY(table.at("c").at("c1").is_string());
        CHECK_EQ   (table.at("c").at("c1").as_string(), "baz");
        CHECK_UNARY( ! table.at("c").contains("c2"));

        CHECK_EQ(table.as_table_fmt().indent_type,    toml::indent_char::space);
        CHECK_EQ(table.as_table_fmt().name_indent,    0);
        CHECK_EQ(table.as_table_fmt().body_indent,    4);
        CHECK_EQ(table.as_table_fmt().closing_indent, 0);

    }
}