|
4 | 4 | // or copy at https://opensource.org/licenses/ISC |
5 | 5 |
|
6 | 6 | #include <fn/and_then.hpp> |
| 7 | +#include <fn/discard.hpp> |
7 | 8 | #include <fn/fail.hpp> |
8 | 9 | #include <fn/filter.hpp> |
9 | 10 | #include <fn/inspect.hpp> |
@@ -114,10 +115,19 @@ TEST_CASE("Minimal expected", "[expected][and_then]") |
114 | 115 | REQUIRE(value.error().what == "Less than 42"); |
115 | 116 | // example-expected-filter-error |
116 | 117 | } |
| 118 | + { |
| 119 | + // example-expected-discard |
| 120 | + fn::expected<int, Error> ex{42}; |
| 121 | + |
| 122 | + // Observe for side-effects only |
| 123 | + ex | fn::inspect([](int v) noexcept { REQUIRE(v == 42); }) |
| 124 | + | fn::discard(); |
| 125 | + // example-expected-discard |
| 126 | + } |
117 | 127 | // clang-format on |
118 | 128 | } |
119 | 129 |
|
120 | | -TEST_CASE("Demo expected", "[expected][pack][and_then][transform_error][transform][inspect][inspect_error][" |
| 130 | +TEST_CASE("Demo expected", "[expected][pack][and_then][discard][transform_error][transform][inspect][inspect_error][" |
121 | 131 | "recover][fail][filter][immovable]") |
122 | 132 | { |
123 | 133 | constexpr auto fn1 = [](char const *str, double &peek) { |
@@ -219,6 +229,16 @@ TEST_CASE("Demo expected", "[expected][pack][and_then][transform_error][transfor |
219 | 229 | auto const p4 = fn3("42", "12"); |
220 | 230 | CHECK(p4.has_value()); |
221 | 231 | CHECK(p4.value() == 42 * 12); |
| 232 | + |
| 233 | + fn::expected<int, Error>{42} // |
| 234 | + | fn::inspect([](int v) noexcept { REQUIRE(v == 42); }) // |
| 235 | + | fn::inspect_error([](Error) noexcept { CHECK(false); }) // |
| 236 | + | fn::discard(); |
| 237 | + |
| 238 | + fn::expected<int, Error>{std::unexpected<Error>{"discarded"}} // |
| 239 | + | fn::inspect([](int) noexcept { CHECK(false); }) // |
| 240 | + | fn::inspect_error([](Error e) noexcept { REQUIRE(e.what == "discarded"); }) // |
| 241 | + | fn::discard(); |
222 | 242 | } |
223 | 243 |
|
224 | 244 | TEST_CASE("Minimal optional", "[optional][and_then]") |
@@ -250,28 +270,37 @@ TEST_CASE("Minimal optional", "[optional][and_then]") |
250 | 270 | } |
251 | 271 | { |
252 | 272 | // example-optional-filter-value |
253 | | - fn::optional<int> ex{42}; |
| 273 | + fn::optional<int> op{42}; |
254 | 274 |
|
255 | | - auto value = ex |
| 275 | + auto value = op |
256 | 276 | | fn::filter([](auto &&i) { return i >= 42; }); // Filter out values less than 42 |
257 | 277 |
|
258 | 278 | REQUIRE(value.value() == 42); |
259 | 279 | // example-optional-filter-value |
260 | 280 | } |
261 | 281 | { |
262 | 282 | // example-optional-filter-empty |
263 | | - fn::optional<int> ex{12}; |
| 283 | + fn::optional<int> op{12}; |
264 | 284 |
|
265 | | - auto value = ex |
| 285 | + auto value = op |
266 | 286 | | fn::filter([](auto &&i) { return i >= 42; }); |
267 | 287 |
|
268 | 288 | REQUIRE(not value.has_value()); |
269 | 289 | // example-optional-filter-empty |
270 | 290 | } |
| 291 | + { |
| 292 | + // example-optional-discard |
| 293 | + fn::optional<int> op{42}; |
| 294 | + |
| 295 | + // Observe for side-effects only |
| 296 | + op | fn::inspect([](int v) noexcept { REQUIRE(v == 42); }) |
| 297 | + | fn::discard(); |
| 298 | + // example-optional-discard |
| 299 | + } |
271 | 300 | // clang-format on |
272 | 301 | } |
273 | 302 |
|
274 | | -TEST_CASE("Demo optional", "[optional][pack][and_then][or_else][inspect][transform][fail][filter][recover]") |
| 303 | +TEST_CASE("Demo optional", "[optional][pack][and_then][discard][or_else][inspect][transform][fail][filter][recover]") |
275 | 304 | { |
276 | 305 | constexpr auto fn1 = [](char const *str, int &peek) { |
277 | 306 | using namespace fn; |
@@ -359,6 +388,10 @@ TEST_CASE("Demo optional", "[optional][pack][and_then][or_else][inspect][transfo |
359 | 388 | auto const p4 = fn3("42", "12"); |
360 | 389 | CHECK(p4.has_value()); |
361 | 390 | CHECK(p4.value() == 42 * 12); |
| 391 | + |
| 392 | + fn::optional<int>{42} // |
| 393 | + | fn::inspect([](int v) noexcept { REQUIRE(v == 42); }) // |
| 394 | + | fn::discard(); |
362 | 395 | } |
363 | 396 |
|
364 | 397 | TEST_CASE("Demo choice and graded monad", "[choice][and_then][inspect][transform][graded]") |
|
0 commit comments