File tree Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include " tl/expected.hpp"
4+
5+ // //////////////////////////////////////////////////////////////////////
6+
7+ namespace stout {
8+
9+ // //////////////////////////////////////////////////////////////////////
10+
11+ // Define aliases for 'tl::expected', 'tl::unexpected' and
12+ // 'tl::make_unexpected' that are used to represent error-or-value types
13+ // returned by code throughout stout. Errors are std::strings, and the
14+ // error-or-value type is similar to C++23's std::expected.
15+ // You can read more about 'tl::expected' library using the link below:
16+ // https://github.com/TartanLlama/expected
17+
18+ template <typename T, typename Error = std::string>
19+ using expected = tl::expected<T, Error>;
20+
21+ template <typename Error = std::string>
22+ using unexpected = tl::unexpected<Error>;
23+
24+ template <typename Error>
25+ auto make_unexpected = tl::make_unexpected<Error>;
26+
27+ // //////////////////////////////////////////////////////////////////////
28+
29+ } // namespace stout
30+
31+ // //////////////////////////////////////////////////////////////////////
Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ cc_test(
3939cc_test (
4040 name = "stout" ,
4141 srcs = [
42+ "expected_tests.cc" ,
4243 "stringify_tests.cc" ,
4344 "temporary_directory_test_tests.cc" ,
4445 ],
Original file line number Diff line number Diff line change 1+ #include " gtest/gtest.h"
2+ #include " stout/expected.h"
3+
4+ stout::expected<int > divide (const int dividend, const int divisor) {
5+ if (divisor == 0 ) {
6+ return stout::make_unexpected<std::string>(" divide by zero" );
7+ }
8+
9+ return dividend / divisor;
10+ }
11+
12+ TEST (Expected, SucceedResult) {
13+ const stout::expected<int > result = divide (6 , 3 );
14+
15+ EXPECT_EQ (result, stout::expected<int >(2 ));
16+ }
17+
18+ TEST (Expected, DivideByZero) {
19+ const stout::expected<int > result = divide (6 , 0 );
20+
21+ EXPECT_EQ (result, stout::make_unexpected<std::string>(" divide by zero" ));
22+ }
23+
24+ TEST (Expected, MakeUnexpected) {
25+ auto error = []() {
26+ return stout::make_unexpected<std::string>(" error" );
27+ };
28+ const stout::unexpected<std::string> result = error ();
29+
30+ EXPECT_EQ (" error" , result.value ());
31+ }
32+
33+ TEST (Expected, Unexpected) {
34+ stout::unexpected<std::string> unexpected{" error" };
35+
36+ EXPECT_EQ (" error" , unexpected.value ());
37+ }
You can’t perform that action at this time.
0 commit comments