rippled
Expected_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github0.com/ripple/rippled
4  Copyright (c) 2012-2016 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/Expected.h>
21 #include <ripple/beast/unit_test.h>
22 #include <ripple/protocol/TER.h>
23 #include <array>
24 #include <cstdint>
25 
26 namespace ripple {
27 namespace test {
28 
29 struct Expected_test : beast::unit_test::suite
30 {
31  void
32  run() override
33  {
34  // Test non-error const construction.
35  {
36  auto const expected = []() -> Expected<std::string, TER> {
37  return "Valid value";
38  }();
39  BEAST_EXPECT(expected);
40  BEAST_EXPECT(expected.has_value());
41  BEAST_EXPECT(expected.value() == "Valid value");
42  BEAST_EXPECT(*expected == "Valid value");
43  BEAST_EXPECT(expected->at(0) == 'V');
44 
45  bool throwOccurred = false;
46  try
47  {
48  // There's no error, so should throw.
49  [[maybe_unused]] TER const t = expected.error();
50  }
51  catch (std::runtime_error const& e)
52  {
53  BEAST_EXPECT(e.what() == std::string("bad expected access"));
54  throwOccurred = true;
55  }
56  BEAST_EXPECT(throwOccurred);
57  }
58  // Test non-error non-const construction.
59  {
60  auto expected = []() -> Expected<std::string, TER> {
61  return "Valid value";
62  }();
63  BEAST_EXPECT(expected);
64  BEAST_EXPECT(expected.has_value());
65  BEAST_EXPECT(expected.value() == "Valid value");
66  BEAST_EXPECT(*expected == "Valid value");
67  BEAST_EXPECT(expected->at(0) == 'V');
68  std::string mv = std::move(*expected);
69  BEAST_EXPECT(mv == "Valid value");
70 
71  bool throwOccurred = false;
72  try
73  {
74  // There's no error, so should throw.
75  [[maybe_unused]] TER const t = expected.error();
76  }
77  catch (std::runtime_error const& e)
78  {
79  BEAST_EXPECT(e.what() == std::string("bad expected access"));
80  throwOccurred = true;
81  }
82  BEAST_EXPECT(throwOccurred);
83  }
84  // Test error construction from rvalue.
85  {
86  auto const expected = []() -> Expected<std::string, TER> {
87  return Unexpected(telLOCAL_ERROR);
88  }();
89  BEAST_EXPECT(!expected);
90  BEAST_EXPECT(!expected.has_value());
91  BEAST_EXPECT(expected.error() == telLOCAL_ERROR);
92 
93  bool throwOccurred = false;
94  try
95  {
96  // There's no result, so should throw.
97  [[maybe_unused]] std::string const s = *expected;
98  }
99  catch (std::runtime_error const& e)
100  {
101  BEAST_EXPECT(e.what() == std::string("bad expected access"));
102  throwOccurred = true;
103  }
104  BEAST_EXPECT(throwOccurred);
105  }
106  // Test error construction from lvalue.
107  {
108  auto const err(telLOCAL_ERROR);
109  auto expected = [&err]() -> Expected<std::string, TER> {
110  return Unexpected(err);
111  }();
112  BEAST_EXPECT(!expected);
113  BEAST_EXPECT(!expected.has_value());
114  BEAST_EXPECT(expected.error() == telLOCAL_ERROR);
115 
116  bool throwOccurred = false;
117  try
118  {
119  // There's no result, so should throw.
120  [[maybe_unused]] std::size_t const s = expected->size();
121  }
122  catch (std::runtime_error const& e)
123  {
124  BEAST_EXPECT(e.what() == std::string("bad expected access"));
125  throwOccurred = true;
126  }
127  BEAST_EXPECT(throwOccurred);
128  }
129  // Test error construction from const char*.
130  {
131  auto const expected = []() -> Expected<int, char const*> {
132  return Unexpected("Not what is expected!");
133  }();
134  BEAST_EXPECT(!expected);
135  BEAST_EXPECT(!expected.has_value());
136  BEAST_EXPECT(
137  expected.error() == std::string("Not what is expected!"));
138  }
139  // Test error construction of string from const char*.
140  {
141  auto expected = []() -> Expected<int, std::string> {
142  return Unexpected("Not what is expected!");
143  }();
144  BEAST_EXPECT(!expected);
145  BEAST_EXPECT(!expected.has_value());
146  BEAST_EXPECT(expected.error() == "Not what is expected!");
147  std::string const s(std::move(expected.error()));
148  BEAST_EXPECT(s == "Not what is expected!");
149  }
150  // Test non-error const construction of Expected<void, T>.
151  {
152  auto const expected = []() -> Expected<void, std::string> {
153  return {};
154  }();
155  BEAST_EXPECT(expected);
156  bool throwOccurred = false;
157  try
158  {
159  // There's no error, so should throw.
160  [[maybe_unused]] std::size_t const s = expected.error().size();
161  }
162  catch (std::runtime_error const& e)
163  {
164  BEAST_EXPECT(e.what() == std::string("bad expected access"));
165  throwOccurred = true;
166  }
167  BEAST_EXPECT(throwOccurred);
168  }
169  // Test non-error non-const construction of Expected<void, T>.
170  {
171  auto expected = []() -> Expected<void, std::string> {
172  return {};
173  }();
174  BEAST_EXPECT(expected);
175  bool throwOccurred = false;
176  try
177  {
178  // There's no error, so should throw.
179  [[maybe_unused]] std::size_t const s = expected.error().size();
180  }
181  catch (std::runtime_error const& e)
182  {
183  BEAST_EXPECT(e.what() == std::string("bad expected access"));
184  throwOccurred = true;
185  }
186  BEAST_EXPECT(throwOccurred);
187  }
188  // Test error const construction of Expected<void, T>.
189  {
190  auto const expected = []() -> Expected<void, std::string> {
191  return Unexpected("Not what is expected!");
192  }();
193  BEAST_EXPECT(!expected);
194  BEAST_EXPECT(expected.error() == "Not what is expected!");
195  }
196  // Test error non-const construction of Expected<void, T>.
197  {
198  auto expected = []() -> Expected<void, std::string> {
199  return Unexpected("Not what is expected!");
200  }();
201  BEAST_EXPECT(!expected);
202  BEAST_EXPECT(expected.error() == "Not what is expected!");
203  std::string const s(std::move(expected.error()));
204  BEAST_EXPECT(s == "Not what is expected!");
205  }
206  }
207 };
208 
209 BEAST_DEFINE_TESTSUITE(Expected, ripple_basics, ripple);
210 
211 } // namespace test
212 } // namespace ripple
std::string
STL class.
ripple::telLOCAL_ERROR
@ telLOCAL_ERROR
Definition: TER.h:51
ripple::Unexpected
Unexpected(E(&)[N]) -> Unexpected< E const * >
ripple::test::Expected_test::run
void run() override
Definition: Expected_test.cpp:32
ripple::Expected
Definition: Expected.h:129
ripple::TERSubset< CanCvtToTER >
array
cstdint
std::runtime_error
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::Expected_test
Definition: Expected_test.cpp:29
std::size_t
std::runtime_error::what
T what(T... args)
ripple::test::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(DeliverMin, app, ripple)