File: test_result.cpp

package info (click to toggle)
toml11 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,464 kB
  • sloc: cpp: 38,446; makefile: 8; sh: 5
file content (148 lines) | stat: -rw-r--r-- 3,985 bytes parent folder | download
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
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <toml11/result.hpp>

TEST_CASE("testing constructor")
{
    {
        auto s = toml::ok(42);
        toml::result<int, std::string> result(s);
        CHECK(!!result);
        CHECK(result.is_ok());
        CHECK(!result.is_err());
        CHECK(result.unwrap() == 42);
    }
    {
        const auto s = toml::ok(42);
        toml::result<int, std::string> result(s);
        CHECK(!!result);
        CHECK(result.is_ok());
        CHECK(!result.is_err());
        CHECK(result.unwrap() == 42);
    }
    {
        toml::result<int, std::string> result(toml::ok(42));
        CHECK(!!result);
        CHECK(result.is_ok());
        CHECK(!result.is_err());
        CHECK(result.unwrap() == 42);
    }

    {
        auto f = toml::err("foobar");
        toml::result<int, std::string> result(f);
        CHECK(!result);
        CHECK(!result.is_ok());
        CHECK(result.is_err());
        CHECK(result.unwrap_err() == "foobar");
    }
    {
        const auto f = toml::err("foobar");
        toml::result<int, std::string> result(f);
        CHECK(!result);
        CHECK(!result.is_ok());
        CHECK(result.is_err());
        CHECK(result.unwrap_err() == "foobar");
    }
    {
        toml::result<int, std::string> result(toml::err("foobar"));
        CHECK(!result);
        CHECK(!result.is_ok());
        CHECK(result.is_err());
        CHECK(result.unwrap_err() == "foobar");
    }
}

TEST_CASE("testing assignment op")
{
    {
        toml::result<int, std::string> result(toml::err("foobar"));
        result = toml::ok(42);
        CHECK(!!result);
        CHECK(result.is_ok());
        CHECK(!result.is_err());
        CHECK(result.unwrap() == 42);
    }
    {
        toml::result<int, std::string> result(toml::err("foobar"));
        auto s = toml::ok(42);
        result = s;
        CHECK(!!result);
        CHECK(result.is_ok());
        CHECK(!result.is_err());
        CHECK(result.unwrap() == 42);
    }
    {
        toml::result<int, std::string> result(toml::err("foobar"));
        const auto s = toml::ok(42);
        result = s;
        CHECK(!!result);
        CHECK(result.is_ok());
        CHECK(!result.is_err());
        CHECK(result.unwrap() == 42);
    }
    {
        toml::result<int, std::string> result(toml::err("foobar"));
        result = toml::err("hoge");
        CHECK(!result);
        CHECK(!result.is_ok());
        CHECK(result.is_err());
        CHECK(result.unwrap_err() == "hoge");
    }
    {
        toml::result<int, std::string> result(toml::err("foobar"));
        auto f = toml::err("hoge");
        result = f;
        CHECK(!result);
        CHECK(!result.is_ok());
        CHECK(result.is_err());
        CHECK(result.unwrap_err() == "hoge");
    }
    {
        toml::result<int, std::string> result(toml::err("foobar"));
        const auto f = toml::err("hoge");
        result = f;
        CHECK(!result);
        CHECK(!result.is_ok());
        CHECK(result.is_err());
        CHECK(result.unwrap_err() == "hoge");
    }
}

TEST_CASE("testing result<reference_wrapper>")
{
    {
        int a = 42;

        toml::result<std::reference_wrapper<int>, std::string> result(toml::ok(std::ref(a)));
        CHECK_UNARY(result);
        CHECK_UNARY(result.is_ok());
        CHECK_UNARY_FALSE(result.is_err());

        CHECK_EQ(result.unwrap(), 42);
        CHECK_EQ(a, 42);

        result.unwrap() = 6 * 9;

        CHECK_EQ(result.unwrap(), 6*9);
        CHECK_EQ(a, 6*9);
    }

    {
        std::string b = "foo";

        toml::result<int, std::reference_wrapper<std::string>> result(toml::err(std::ref(b)));
        CHECK_UNARY_FALSE(result);
        CHECK_UNARY_FALSE(result.is_ok());
        CHECK_UNARY(result.is_err());

        CHECK_EQ(result.unwrap_err(), "foo");
        CHECK_EQ(b, "foo");

        result.unwrap_err() = "foobar";

        CHECK_EQ(result.unwrap_err(), "foobar");
        CHECK_EQ(b, "foobar");
    }
}