Lumiera  0.pre.03
»edit your freedom«
result-test.cpp
Go to the documentation of this file.
1 /*
2  Result(Test) - verify the either-result-or-failure intermediary wrapper
3 
4  Copyright (C)
5  2023, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
21 #include "lib/test/run.hpp"
22 #include "lib/test/test-helper.hpp"
23 #include "lib/result.hpp"
24 #include "lib/util.hpp"
25 
26 #include <cstdlib>
27 
28 
29 
30 namespace lib {
31 namespace test{
32 
33  using util::isSameObject;
34  using std::rand;
35 
36  namespace error = lumiera::error;
37  using error::LUMIERA_ERROR_FATAL;
38  using error::LUMIERA_ERROR_STATE;
39 
40 
41  namespace {
42  const Literal THE_END = "all dead and hero got the girl";
43 
44 
45  #define Type(_EXPR_) lib::test::showType<decltype(_EXPR_)>()
46  }
47 
48 
49 
50 
51 
52  /***********************************************************************************/
65  : public Test
66  {
67 
68  void
69  run (Arg)
70  {
71  auto happy = Result{THE_END};
72  CHECK (happy == THE_END);
73  CHECK (happy.isValid());
74  CHECK (bool(happy));
75 
76  happy.maybeThrow(); // still alive...
77 
78  CHECK (Type(happy) == "Result<Literal const&>"_expect);
79 
80  // Note type deduction: RValue moved into the Result...
81  auto sequel = Result{Literal{THE_END}};
82  CHECK (sequel.isValid());
83  CHECK (Type(sequel) == "Result<Literal>"_expect);
84 
85  CHECK ( isSameObject (happy.get<Literal const&>(), THE_END));
86  CHECK (not isSameObject (sequel.get<Literal const&>(), THE_END));
87 
88  // »Either Right« case : mark as failure
89  Result<double> facepalm{error::Fatal("zOMG")};
90  CHECK (not facepalm.isValid());
91 
92  VERIFY_ERROR (FATAL, (double)facepalm );
93  VERIFY_ERROR (FATAL, facepalm.get<double&>());
94  VERIFY_ERROR (FATAL, facepalm.maybeThrow() );
95 
96  CHECK (42.0 == facepalm.or_else([]{ return 42; }));
97  CHECK (42.0 == facepalm.value_or(210/5));
98 
99 
100  // a generic functor (template) to invoke
101  auto evil = [](auto it)
102  {
103  if (it % 2)
104  throw error::State{"conspiracy"};
105  else
106  return it;
107  };
108 
109  // Invoke failsafe and capture result....
110  auto seed = Result{evil, '*'}; // this invocation is successful
111  CHECK (Type(seed) == "Result<char>"_expect); // generic λ instantiated with <char>
112  CHECK (42 == seed); // int('*') == 42
113 
114  auto breed = Result{evil, 55ll}; // an odd number...
115  VERIFY_ERROR (STATE, breed.maybeThrow() );
116  CHECK (Type(breed) == "Result<llong>"_expect);
117 
118  auto dead = Result{[]{ throw 55; }};
119  auto deed = Result{[]{ /* :-) */ }};
120 
121  CHECK (Type(dead) == "Result<void>"_expect);
122  CHECK (Type(deed) == "Result<void>"_expect);
123 
124  CHECK (not dead.isValid());
125  CHECK ( deed.isValid());
126 
127  try { dead.maybeThrow(); } // can handle really *anything* thrown
128  catch(int oo)
129  { CHECK (oo == 55); }
130 
131  // can also invoke member function....
132  auto deaf = Result{&Literal::empty, &THE_END}; // Note: instance "this"-ptr as first argument
133  CHECK (deaf.isValid()); // no exception was thrown => state isValid()
134  CHECK (deaf == false); // yet invocation of THE_END.empty() yields false
135  CHECK (not deaf); // Warning: in this case, the conversion to payload type shadows
136  CHECK (Type(deaf) == "Result<bool>"_expect);
137  }
138  };
139 
140 
142  LAUNCHER (Result_test, "unit common");
143 
144 
145 }} // namespace lib::test
Representation of the result of some operation, EITHER a value or a failure.
Definition: result.hpp:97
Definition: run.hpp:40
inline string literal This is a marker type to indicate that
Definition: symbol.hpp:76
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
Intermediary value object to represent »either« an operation result or a failure. ...
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:190
Simplistic test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421