aboutsummaryrefslogtreecommitdiffstats
path: root/nihil/tests/error.cc
blob: 7b079fd6ee0ea093a8a389220e35ffb68ae999af (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
/*
 * 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]")
{
	auto e = nihil::error("an error");
	REQUIRE(e.str() == e.what());
	REQUIRE(e.what() == "an error");
	REQUIRE(std::format("{}", e) == e.what());
}

TEST_CASE("error: construct from std::error_condition", "[nihil]")
{
	auto code = std::make_error_condition(std::errc::invalid_argument);
	auto e = nihil::error(code);

	REQUIRE(e.cause().has_value() == false);
	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() == e.what());
	REQUIRE(e.what() == std::strerror(EINVAL));
	REQUIRE(std::format("{}", e) == e.what());
}

TEST_CASE("error: construct from std::errc", "[nihil]")
{
	auto e = nihil::error(std::errc::invalid_argument);

	REQUIRE(e.cause().has_value() == false);
	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() == e.what());
	REQUIRE(e.what() == std::strerror(EINVAL));
	REQUIRE(std::format("{}", e) == e.what());
}

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

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

	REQUIRE(e.cause().has_value() == true);
	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(e.what() == ("cannot open file: "s + std::strerror(ENOENT)));
	REQUIRE(std::format("{}", e) == e.what());
}

TEST_CASE("error: operator== with strings", "[nihil]")
{
	auto e1 = nihil::error("error");
	auto e2 = nihil::error("error");
	auto e3 = nihil::error("an error");

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

TEST_CASE("error: operator< with strings", "[nihil]")
{
	auto e1 = nihil::error("aaa");
	auto e2 = nihil::error("zzz");

	REQUIRE(e1 < e2);
}

TEST_CASE("error: operator== with a cause", "[nihil]")
{
	auto e1 = nihil::error("error", nihil::error("cause 1"));
	auto e2 = nihil::error("error", nihil::error("cause 2"));

	REQUIRE(e1 == e2);
}

TEST_CASE("error: operator== with error_conditions", "[nihil]")
{
	auto e1 = nihil::error(std::errc::invalid_argument);
	auto e2 = nihil::error(std::errc::invalid_argument);
	auto e3 = nihil::error(std::errc::permission_denied);

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

TEST_CASE("error: std::format with string", "[nihil]")
{
	auto err = nihil::error("an error");
	REQUIRE(std::format("{}", err) == "an error");
}

TEST_CASE("error: std::format with std::errc", "[nihil]")
{
	auto err = nihil::error(std::errc::invalid_argument);
	REQUIRE(std::format("{}", err) == std::strerror(EINVAL));
}

TEST_CASE("error: std::format with cause", "[nihil]")
{
	auto err = nihil::error("an error", std::errc::invalid_argument);
	REQUIRE(std::format("{}", err) == "an error: Invalid argument");
}