aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.ucl/integer.test.cc
blob: 68567e974daafb723a76f6b81e578f558659a9f8 (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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// This source code is released into the public domain.

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

import nihil.std;
import nihil.ucl;

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

	static_assert(std::same_as<std::int64_t, integer::contained_type>);
	REQUIRE(integer::ucl_type == object_type::integer);
	REQUIRE(static_cast<::ucl_type>(integer::ucl_type) == UCL_INT);

	static_assert(std::destructible<integer>);
	static_assert(std::default_initializable<integer>);
	static_assert(std::move_constructible<integer>);
	static_assert(std::copy_constructible<integer>);
	static_assert(std::equality_comparable<integer>);
	static_assert(std::totally_ordered<integer>);
	static_assert(std::swappable<integer>);
}

TEST_CASE("ucl: integer: constructor", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("default") {
		auto i = integer();
		REQUIRE(i == 0);
	}

	SECTION("with value") {
		auto i = integer(42);
		REQUIRE(i == 42);
	}
}

TEST_CASE("ucl: integer: literal", "[ucl]")
{
	SECTION("with namespace nihil::ucl::literals") {
		using namespace nihil::ucl::literals;

		auto i = 42_ucl;
		REQUIRE(i.type() == nihil::ucl::object_type::integer);
		REQUIRE(i == 42);
	}

	SECTION("with namespace nihil::literals") {
		using namespace nihil::literals;

		auto i = 42_ucl;
		REQUIRE(i.type() == nihil::ucl::object_type::integer);
		REQUIRE(i == 42);
	}
}

TEST_CASE("ucl: integer: construct from UCL object", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("ref, correct type") {
		auto *uobj = ::ucl_object_fromint(42);

		auto i = integer(ref, uobj);
		REQUIRE(i == 42);

		::ucl_object_unref(uobj);
	}

	SECTION("noref, correct type") {
		auto *uobj = ::ucl_object_fromint(42);

		auto i = integer(noref, uobj);
		REQUIRE(i == 42);
	}

	SECTION("ref, wrong type") {
		auto *uobj = ::ucl_object_frombool(true);

		REQUIRE_THROWS_AS(integer(ref, uobj), type_mismatch);

		::ucl_object_unref(uobj);
	}

	SECTION("noref, wrong type") {
		auto *uobj = ::ucl_object_frombool(true);

		REQUIRE_THROWS_AS(integer(noref, uobj), type_mismatch);

		::ucl_object_unref(uobj);
	}
}

TEST_CASE("ucl: integer: make_integer", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("default value") {
		auto i = make_integer().value();
		REQUIRE(i == 0);
	}

	SECTION("explicit value") {
		auto i = make_integer(42).value();
		REQUIRE(i == 42);
	}
}

TEST_CASE("ucl: integer: swap", "[ucl]")
{
	// do not add using namespace nihil::ucl
	
	auto i1 = nihil::ucl::integer(1);
	auto i2 = nihil::ucl::integer(2);

	swap(i1, i2);

	REQUIRE(i1 == 2);
	REQUIRE(i2 == 1);
}

TEST_CASE("ucl: integer: value()", "[ucl]")
{
	using namespace nihil::ucl;

	auto i = 42_ucl;
	REQUIRE(i.value() == 42);
}

TEST_CASE("ucl: integer: key()", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("parsed with key") {
		auto obj = parse("an_int = 42").value();
		auto i = object_cast<integer>(obj["an_int"]).value();
		REQUIRE(i.key() == "an_int");
	}

	SECTION("bare integer, no key") {
		auto i = 42_ucl;
		REQUIRE(i.key().empty() == true);
	}
}

TEST_CASE("ucl: integer: comparison", "[ucl]")
{
	using namespace nihil::ucl;

	auto i = 42_ucl;

	SECTION("operator==") {
		REQUIRE(i == 42);
		REQUIRE(i == 42_ucl);
	}

	SECTION("operator!=") {
		REQUIRE(i != 1);
		REQUIRE(i != 1_ucl);
	}

	SECTION("operator<") {
		REQUIRE(i < 43);
		REQUIRE(i < 43_ucl);
	}

	SECTION("operator>") {
		REQUIRE(i > 1);
		REQUIRE(i > 1_ucl);
	}
}

TEST_CASE("ucl: integer: parse", "[ucl]")
{
	using namespace nihil::ucl;

	auto obj = parse("value = 42").value();

	auto v = obj["value"];
	REQUIRE(v.key() == "value");
	REQUIRE(object_cast<integer>(v) == 42);
}

TEST_CASE("ucl: integer: parse and emit", "[ucl]")
{
	using namespace nihil::ucl;

	auto ucl = parse("int = 42;").value();

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

	REQUIRE(output == "int = 42;\n");
}

TEST_CASE("ucl: integer: format", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("bare integer") {
		auto str = std::format("{}", 42_ucl);
		REQUIRE(str == "42");
	}

	SECTION("parsed integer") {
		auto obj = parse("int = 42;").value();
		auto i = object_cast<integer>(obj["int"]).value();

		auto str = std::format("{}", i);
		REQUIRE(str == "42");
	}

	SECTION("with format string") {
		auto str = std::format("{:-05}", 42_ucl);
		REQUIRE(str == "00042");
	}
}

TEST_CASE("ucl: integer: print to ostream", "[ucl]")
{
	using namespace nihil::ucl;

	SECTION("bare integer") {
		auto strm = std::ostringstream();
		strm << 42_ucl;

		REQUIRE(strm.str() == "42");
	}

	SECTION("parsed integer") {
		auto obj = parse("int = 42;").value();
		auto i = object_cast<integer>(obj["int"]).value();

		auto strm = std::ostringstream();
		strm << i;

		REQUIRE(strm.str() == "42");
	}
}
} // anonymous namespace