blob: bc9e40666fdc76a9323579ab96ba3e39717856dc (
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
|
// This source code is released into the public domain.
#include <catch2/catch_test_macros.hpp>
import nihil.std;
import nihil.error;
import nihil.util;
namespace {
TEST_CASE("monad: co_await std::optional<> with value", "[nihil]")
{
auto get_value = [] -> std::optional<int> {
return 42;
};
auto try_get_value = [&get_value] -> std::optional<int> {
co_return co_await get_value();
};
auto o = try_get_value();
REQUIRE(o == 42);
}
TEST_CASE("monad: co_await std::optional<> without value", "[nihil]")
{
auto get_value = [] -> std::optional<int> {
return {};
};
auto try_get_value = [&get_value] -> std::optional<int> {
co_return co_await get_value();
};
auto o = try_get_value();
REQUIRE(!o.has_value());
}
TEST_CASE("monad: co_await std::expected<> with value", "[nihil]")
{
auto get_value = [] -> std::expected<int, std::string> {
return 42;
};
auto try_get_value = [&get_value] -> std::expected<int, std::string> {
co_return co_await get_value();
};
auto o = try_get_value();
REQUIRE(o == 42);
}
TEST_CASE("monad: co_await std::expected<> with error", "[nihil]")
{
auto get_value = [] -> std::expected<int, std::string> {
return std::unexpected("error");
};
auto try_get_value = [&get_value] -> std::expected<int, std::string> {
co_return co_await get_value();
};
auto o = try_get_value();
REQUIRE(!o);
REQUIRE(o.error() == "error");
}
} // anonymous namespace
|