File: test_parse_array.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 (142 lines) | stat: -rw-r--r-- 5,547 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
#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 an array")
{
    toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
    const auto make_format = [](toml::array_format ty, toml::indent_char ic, std::int32_t i, std::int32_t c) {
        toml::array_format_info fmt;
        fmt.fmt = ty;
        fmt.indent_type = ic;
        fmt.body_indent = i;
        fmt.closing_indent = c;
        return fmt;
    };

    toml11_test_parse_success<toml::value_t::array>("[]",       (toml::array{}),     comments(), make_format(toml::array_format::oneline, toml::indent_char::none, 4, 0), ctx);
    toml11_test_parse_success<toml::value_t::array>("[1]",      (toml::array{1}),    comments(), make_format(toml::array_format::oneline, toml::indent_char::none, 4, 0), ctx);
    toml11_test_parse_success<toml::value_t::array>("[1, 2]",   (toml::array{1, 2}), comments(), make_format(toml::array_format::oneline, toml::indent_char::none, 4, 0), ctx);
    toml11_test_parse_success<toml::value_t::array>("[1, 2, ]", (toml::array{1, 2}), comments(), make_format(toml::array_format::oneline, toml::indent_char::none, 4, 0), ctx);

    toml11_test_parse_success<toml::value_t::array>("[\n  1,\n  2,\n]",   (toml::array{1, 2}), comments(), make_format(toml::array_format::multiline, toml::indent_char::space, 2, 0), ctx);
    toml11_test_parse_success<toml::value_t::array>("[\n  1,\n  2,\n  ]", (toml::array{1, 2}), comments(), make_format(toml::array_format::multiline, toml::indent_char::space, 2, 2), ctx);
    toml11_test_parse_success<toml::value_t::array>("[\n\t1,\n\t2,\n]",   (toml::array{1, 2}), comments(), make_format(toml::array_format::multiline, toml::indent_char::tab,   1, 0), ctx);
    toml11_test_parse_success<toml::value_t::array>("[\n\t1,\n\t2,\n\t]", (toml::array{1, 2}), comments(), make_format(toml::array_format::multiline, toml::indent_char::tab,   1, 1), ctx);

    // comment 3 is before comma, so is a comment for `2`.
    toml11_test_parse_success<toml::value_t::array>(R"([
    1, # comment 1
    2, # comment 2
# comment 3
  # comment 4
# comment 5
])",
        (toml::array{
             toml::value(1, comments("# comment 1")),
             toml::value(2, comments("# comment 2")),
         }), comments(), make_format(toml::array_format::multiline, toml::indent_char::space, 4, 0), ctx);

    // comment 3 is before comma, so is a comment for `2`.
    toml11_test_parse_success<toml::value_t::array>(R"([
    1, # comment 1
    2, # comment 2
# comment 3
  # comment 4
# comment 5
  ])",
        (toml::array{
             toml::value(1, comments("# comment 1")),
             toml::value(2, comments("# comment 2")),
         }), comments(), make_format(toml::array_format::multiline, toml::indent_char::space, 4, 2), ctx);

    // comment 3 is before comma, so is a comment for `2`.
    toml11_test_parse_success<toml::value_t::array>(R"([1 # comment 1
, 2 # comment 2
# comment 3
, # comment 4
# comment 5 (free comment)
])",
        (toml::array{
             toml::value(1, comments("# comment 1")),
             toml::value(2, comments("# comment 2", "# comment 3", "# comment 4")),
         }), comments(), make_format(toml::array_format::multiline, toml::indent_char::none, 4, 0), ctx);


    toml11_test_parse_success<toml::value_t::array>(R"([1 # comment 1
, # comment 2
# comment 3
# ditto

# comment 4
2 # comment 5
# comment 6
, 3
# comment 7
])",
        (toml::array{
             toml::value(1, comments("# comment 1", "# comment 2")),
             toml::value(2, comments("# comment 4", "# comment 5", "# comment 6")),
             toml::value(3, comments("# comment 7")),
         }), comments(), make_format(toml::array_format::multiline, toml::indent_char::none, 4, 0), ctx);
}

TEST_CASE("testing invalid arrays")
{
    using namespace toml::detail;
    {
        toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
        auto loc = toml::detail::make_temporary_location("[ 1, hoge ]");
        const auto res = parse_array(loc, ctx);
        CHECK_UNARY(res.is_err());

        for(const auto& e : ctx.errors())
        {
            std::cerr << toml::format_error(e) << std::endl;
        }
    }

    {
        toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
        auto loc = toml::detail::make_temporary_location("[ 1, 2");
        const auto res = parse_array(loc, ctx);
        CHECK_UNARY(res.is_err());
        ctx.report_error(res.unwrap_err());

        for(const auto& e : ctx.errors())
        {
            std::cerr << toml::format_error(e) << std::endl;
        }
    }

    {
        toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
        auto loc = toml::detail::make_temporary_location("[");
        const auto res = parse_array(loc, ctx);
        CHECK_UNARY(res.is_err());
        ctx.report_error(res.unwrap_err());

        for(const auto& e : ctx.errors())
        {
            std::cerr << toml::format_error(e) << std::endl;
        }
    }

    {
        toml::detail::context<toml::type_config> ctx(toml::spec::v(1,0,0));
        auto loc = toml::detail::make_temporary_location("[ 1, 2, 3\n a = b");
        const auto res = parse_array(loc, ctx);
        CHECK_UNARY(res.is_err());
        ctx.report_error(res.unwrap_err());

        for(const auto& e : ctx.errors())
        {
            std::cerr << toml::format_error(e) << std::endl;
        }
    }
}