File: test_format_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 (78 lines) | stat: -rw-r--r-- 2,523 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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <toml11/parser.hpp>
#include <toml11/serializer.hpp>
#include <toml11/types.hpp>

TEST_CASE("testing multiline table")
{
    const auto table_fmt = [](toml::indent_char c, std::int32_t body, std::int32_t name) {
        toml::table_format_info fmt;
        fmt.fmt = toml::table_format::multiline;
        fmt.indent_type    = c;
        fmt.body_indent    = body;
        fmt.name_indent    = name;
        fmt.closing_indent = 0;
        return fmt;
    };

    toml::value v(toml::table{
            {"a", 42},
            {"b", 3.14},
            {"c", "foobar"},
        });

    v.as_table_fmt() = table_fmt(toml::indent_char::space, 0, 0);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));

    v.as_table_fmt() = table_fmt(toml::indent_char::space, 0, 2);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));

    v.as_table_fmt() = table_fmt(toml::indent_char::space, 2, 0);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));

    v.as_table_fmt() = table_fmt(toml::indent_char::space, 2, 2);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));


    v.as_table_fmt() = table_fmt(toml::indent_char::tab, 0, 0);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));

    v.as_table_fmt() = table_fmt(toml::indent_char::tab, 0, 2);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));

    v.as_table_fmt() = table_fmt(toml::indent_char::tab, 2, 0);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));

    v.as_table_fmt() = table_fmt(toml::indent_char::tab, 2, 2);
    CHECK_EQ(v, toml::parse_str(toml::format(v)));
}

TEST_CASE("testing multiline table")
{
    const auto table_fmt = [](toml::indent_char c, std::int32_t body, std::int32_t name) {
        toml::table_format_info fmt;
        fmt.fmt = toml::table_format::dotted;
        fmt.indent_type    = c;
        fmt.body_indent    = body;
        fmt.name_indent    = name;
        fmt.closing_indent = 0;
        return fmt;
    };

    {
        toml::value v(toml::table{});
        v["a"]["b"]["c"] = "foobar";
        CHECK_EQ(v, toml::parse_str(toml::format(v)));

        v.at("a").as_table_fmt() = table_fmt(toml::indent_char::space, 0, 0);
        CHECK_EQ(v, toml::parse_str(toml::format(v)));

        v.at("a").at("b").as_table_fmt() = table_fmt(toml::indent_char::space, 0, 0);
        CHECK_EQ(v, toml::parse_str(toml::format(v)));

        v.at("a").as_table_fmt().fmt = toml::table_format::multiline;
        CHECK_EQ(v, toml::parse_str(toml::format(v)));
    }
}