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
|
/*
* This source code is released into the public domain.
*/
#include <format>
#include <sstream>
#include <catch2/catch_test_macros.hpp>
import nihil;
import nihil.ucl;
TEST_CASE("ucl: emit to std::ostream", "[ucl]")
{
using namespace std::literals;
auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv);
REQUIRE(obj);
auto strm = std::ostringstream();
strm << *obj;
// The ostream emitter produces JSON.
REQUIRE(strm.str() == std::format("{:j}", *obj));
}
TEST_CASE("ucl: emit JSON with std::format", "[ucl]")
{
using namespace std::literals;
auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv);
REQUIRE(obj);
auto str = std::format("{:j}", *obj);
REQUIRE(str ==
"{\n"
" \"int\": [\n"
" 1,\n"
" 42,\n"
" 666\n"
" ]\n"
"}");
// Make sure JSON is the default format.
auto str2 = std::format("{}", *obj);
REQUIRE(str == str2);
}
TEST_CASE("ucl: emit compact JSON with std::format", "[ucl]")
{
using namespace std::literals;
auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv);
REQUIRE(obj);
auto str = std::format("{:J}", *obj);
REQUIRE(str == "{\"int\":[1,42,666]}");
}
TEST_CASE("ucl: emit configuration with std::format", "[ucl]")
{
using namespace std::literals;
auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv);
REQUIRE(obj);
auto str = std::format("{:c}", *obj);
REQUIRE(str ==
"int [\n"
" 1,\n"
" 42,\n"
" 666,\n"
"]\n");
}
TEST_CASE("ucl: emit YAML with std::format", "[ucl]")
{
using namespace std::literals;
auto obj = nihil::ucl::parse("int = [1, 42, 666]"sv);
REQUIRE(obj);
auto str = std::format("{:y}", *obj);
REQUIRE(str ==
"int: [\n"
" 1,\n"
" 42,\n"
" 666\n"
"]");
}
|