aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/tests/string.cc
blob: 19052cd26156417227218e32b6c03ee5812a80c2 (plain) (blame)
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
/*
 * This source code is released into the public domain.
 */

#include <list>
#include <string>
#include <vector>

#include <catch2/catch_test_macros.hpp>
#include <ucl.h>

import nihil.ucl;

TEST_CASE("ucl: string: invariants", "[ucl]")
{
	using namespace nihil::ucl;

	static_assert(std::same_as<std::string_view, string::value_type>);
	REQUIRE(string::ucl_type == object_type::string);
	REQUIRE(static_cast<::ucl_type>(string::ucl_type) == UCL_STRING);
}

TEST_CASE("ucl: string: default construct", "[ucl]")
{
	auto str = nihil::ucl::string();
	REQUIRE(str == "");
}

TEST_CASE("ucl: string: construct from string literal", "[ucl]")
{
	auto str = nihil::ucl::string("testing");
	REQUIRE(str == "testing");
}

TEST_CASE("ucl: string: construct from std::string", "[ucl]")
{
	auto str = nihil::ucl::string(std::string("testing"));
	REQUIRE(str == "testing");
}

TEST_CASE("ucl: string: construct from std::string_view", "[ucl]")
{
	auto str = nihil::ucl::string(std::string_view("testing"));
	REQUIRE(str == "testing");
}

TEST_CASE("ucl: string: construct from contiguous range", "[ucl]")
{
	auto s = std::vector{'t', 'e', 's', 't', 'i', 'n', 'g'};
	auto str = nihil::ucl::string(std::from_range, s);
	REQUIRE(str == "testing");
}

TEST_CASE("ucl: string: construct from non-contiguous range", "[ucl]")
{
	auto s = std::list{'t', 'e', 's', 't', 'i', 'n', 'g'};
	auto str = nihil::ucl::string(std::from_range, s);
	REQUIRE(str == "testing");
}

TEST_CASE("ucl: string: construct from contiguous iterator", "[ucl]")
{
	auto s = std::vector{'t', 'e', 's', 't', 'i', 'n', 'g'};
	auto str = nihil::ucl::string(std::ranges::begin(s),
				      std::ranges::end(s));
	REQUIRE(str == "testing");
}

TEST_CASE("ucl: string: construct from non-contiguous iterator", "[ucl]")
{
	auto s = std::list{'t', 'e', 's', 't', 'i', 'n', 'g'};
	auto str = nihil::ucl::string(std::ranges::begin(s),
				      std::ranges::end(s));
	REQUIRE(str == "testing");
}

TEST_CASE("ucl: string: operator==", "[ucl]")
{
	auto str = nihil::ucl::string("testing");

	REQUIRE(str == nihil::ucl::string("testing"));
	REQUIRE(str == std::string_view("testing"));
	REQUIRE(str == std::string("testing"));
	REQUIRE(str == "testing");

	REQUIRE(str != nihil::ucl::string("test"));
	REQUIRE(str != std::string_view("test"));
	REQUIRE(str != std::string("test"));
	REQUIRE(str != "test");
}

TEST_CASE("ucl: string: operator<=>", "[ucl]")
{
	auto str = nihil::ucl::string("testing");

	REQUIRE(str < nihil::ucl::string("zzz"));
	REQUIRE(str < std::string_view("zzz"));
	REQUIRE(str < std::string("zzz"));
	REQUIRE(str < "zzz");

	REQUIRE(str > nihil::ucl::string("aaa"));
	REQUIRE(str > std::string_view("aaa"));
	REQUIRE(str > std::string("aaa"));
	REQUIRE(str > "aaa");
}

TEST_CASE("ucl: string: parse", "[ucl]")
{
	using namespace std::literals;

	auto obj = nihil::ucl::parse("value = \"str\""sv);
	auto v = obj["value"];
	REQUIRE(v.key() == "value");
	REQUIRE(object_cast<nihil::ucl::string>(v) == "str");
}

TEST_CASE("ucl: string: emit", "[ucl]")
{
	auto ucl = nihil::ucl::parse("str = \"te\\\"st\";");

	auto output = std::string();
	emit(ucl, nihil::ucl::emitter::configuration,
	     std::back_inserter(output));

	REQUIRE(output == "str = \"te\\\"st\";\n");
}