aboutsummaryrefslogtreecommitdiffstats
path: root/nihil/tests/error.cc
blob: 31124e3bf3174453b5634888ea26a53514f637be (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
/*
 * This source code is released into the public domain.
 */

#include <cerrno>
#include <cstring>
#include <system_error>

#include <catch2/catch_test_macros.hpp>

import nihil;

TEST_CASE("error: invariants", "[nihil]")
{
	static_assert(std::destructible<nihil::error>);
	static_assert(std::default_initializable<nihil::error>);
	static_assert(std::move_constructible<nihil::error>);
	static_assert(std::copy_constructible<nihil::error>);
	static_assert(std::equality_comparable<nihil::error>);
	static_assert(std::totally_ordered<nihil::error>);
	static_assert(std::swappable<nihil::error>);
	static_assert(std::regular<nihil::error>);
}

TEST_CASE("error: construct from string", "[nihil]")
{
	using namespace nihil;

	auto e = error("an error");
	REQUIRE(e.str() == to_string(e));
	REQUIRE(to_string(e) == "an error");
	REQUIRE(std::format("{}", e) == to_string(e));
}

TEST_CASE("error: construct from std::error_condition", "[nihil]")
{
	using namespace nihil;

	auto code = std::make_error_condition(std::errc::invalid_argument);
	auto e = error(code);

	REQUIRE(!e.cause());
	REQUIRE(e.code().has_value() == false);
	REQUIRE(e.condition().has_value() == true);

	REQUIRE(e == std::errc::invalid_argument);
	REQUIRE(e != std::errc::no_such_file_or_directory);

	REQUIRE(e.str() == to_string(e));
	REQUIRE(to_string(e) == std::strerror(EINVAL));
	REQUIRE(std::format("{}", e) == to_string(e));
}

TEST_CASE("error: construct from std::errc", "[nihil]")
{
	using namespace nihil;

	auto e = error(std::errc::invalid_argument);

	REQUIRE(!e.cause());
	REQUIRE(e.code().has_value() == false);
	REQUIRE(e.condition().has_value() == true);

	REQUIRE(e == std::errc::invalid_argument);
	REQUIRE(e != std::errc::no_such_file_or_directory);

	REQUIRE(e.str() == to_string(e));
	REQUIRE(to_string(e) == std::strerror(EINVAL));
	REQUIRE(std::format("{}", e) == to_string(e));
}

TEST_CASE("error: compound error", "[nihil]")
{
	using namespace std::literals;
	using namespace nihil;

	auto e = error("cannot open file",
			error(std::errc::no_such_file_or_directory));

	REQUIRE(e.cause());
	REQUIRE(e.code().has_value() == false);
	REQUIRE(e.condition().has_value() == false);

	REQUIRE(*e.cause() == std::errc::no_such_file_or_directory);
	REQUIRE(e.str() == "cannot open file");
	REQUIRE(to_string(e) == ("cannot open file: "s +
				 std::strerror(ENOENT)));
	REQUIRE(std::format("{}", e) == to_string(e));
}

TEST_CASE("error: operator== with strings", "[nihil]")
{
	using namespace nihil;

	auto e1 = error("error");
	auto e2 = error("error");
	auto e3 = error("an error");

	REQUIRE(e1 == e2);
	REQUIRE(e1 != e3);
}

TEST_CASE("error: operator< with strings", "[nihil]")
{
	using namespace nihil;

	auto e1 = error("aaa");
	auto e2 = error("zzz");

	REQUIRE(e1 < e2);
}

TEST_CASE("error: operator== with a cause", "[nihil]")
{
	using namespace nihil;

	auto e1 = error("error", error("cause 1"));
	auto e2 = error("error", error("cause 2"));

	REQUIRE(e1 == e2);
}

TEST_CASE("error: operator== with error_conditions", "[nihil]")
{
	using namespace nihil;

	auto e1 = error(std::errc::invalid_argument);
	auto e2 = error(std::errc::invalid_argument);
	auto e3 = error(std::errc::permission_denied);

	REQUIRE(e1 == e2);
	REQUIRE(e1 != e3);
}

TEST_CASE("error: std::format with string", "[nihil]")
{
	using namespace nihil;

	auto err = error("an error");
	REQUIRE(std::format("{}", err) == "an error");
}

TEST_CASE("error: std::format with std::errc", "[nihil]")
{
	using namespace nihil;

	auto err = error(std::errc::invalid_argument);
	REQUIRE(std::format("{}", err) == std::strerror(EINVAL));
}

TEST_CASE("error: std::format with cause", "[nihil]")
{
	using namespace nihil;

	auto err = error("an error", std::errc::invalid_argument);
	REQUIRE(std::format("{}", err) == "an error: Invalid argument");
}

TEST_CASE("error: throw and catch", "[nihil]")
{
	using namespace std::literals;
	using namespace nihil;

	try {
		throw error("oh no", error(std::errc::invalid_argument));
	} catch (std::exception const &exc) {
		REQUIRE(exc.what() == "oh no: Invalid argument"s);
	}
}