44 void EitherTest::testBasicLeft ()
47 QCOMPARE (left.IsLeft (),
true);
48 QCOMPARE (left.IsRight (),
false);
49 QCOMPARE (left.GetLeft (), 1);
51 bool hadCaught =
false;
56 catch (
const std::exception&)
60 QCOMPARE (hadCaught,
true);
63 void EitherTest::testBasicRight ()
66 QCOMPARE (right.IsLeft (),
false);
67 QCOMPARE (right.IsRight (),
true);
68 QCOMPARE (right.GetRight (), QString {
"foo" });
70 bool hadCaught =
false;
75 catch (
const std::exception&)
79 QCOMPARE (hadCaught,
true);
82 void EitherTest::testFMapLeft ()
85 const auto& fmapped =
Fmap (left, [] (
const QString& str) {
return str +
"_mapped"; });
86 QCOMPARE (fmapped, left);
89 void EitherTest::testFMapRight ()
92 const auto& fmapped =
Fmap (right, [] (
const QString& str) {
return str +
"_mapped"; });
93 QCOMPARE (fmapped.GetRight (), QString {
"foo_mapped" });
96 void EitherTest::testFMapRightChangeType ()
99 const auto& fmapped =
Fmap (right, [] (
const QString& str) {
return static_cast<long> (str.size ()); });
100 QCOMPARE (fmapped.GetRight (),
static_cast<long> (right.GetRight ().size ()));
103 void EitherTest::testPure ()
105 const auto& pure = Pure<Either, int> (QString {
"foo" });
109 void EitherTest::testGSL ()
111 const auto& pure = Pure<Either, int> ([] (
const QString& s) {
return s +
"_pure"; });
112 const auto& app = pure * Pure<Either, int> (QString {
"foo" });
116 void EitherTest::testGSLLeft ()
118 const auto& pure = Pure<Either, int> ([] (
const QString& s) {
return s +
"_pure"; });
120 const auto& app = pure * value;
121 QCOMPARE (app, value);
124 void EitherTest::testGSLCurry ()
126 const auto& summer = Pure<Either, int> (
Curry ([] (
const QString& a,
const QString& b) {
return a + b; }));
127 const auto& s1 = Pure<Either, int> (QString {
"foo" });
128 const auto& s2 = Pure<Either, int> (QString {
"bar" });
129 const auto& app = summer * s1 * s2;
133 void EitherTest::testGSLCurryLeft ()
135 const auto& summer = Pure<Either, int> (
Curry ([] (
const QString& a,
const QString& b) {
return a + b; }));
137 const auto& s2 = Pure<Either, int> (QString {
"bar" });
138 const auto& app = summer * s1 * s2;
142 void EitherTest::testBind ()
144 const auto& res = Return<Either, int> (QString {
"foo" }) >>
149 void EitherTest::testBindLeft ()
152 const auto& res = value >>
154 QCOMPARE (res, value);
170 void EitherTest::testBindLeftNotConstructed ()
174 const auto res = value >>
175 [&expected] (
const auto&) {
return expected; };
176 QCOMPARE (res, expected);
static Either Right(const R &r)
bool operator==(const NoDefaultCtor &) const
static Either Left(const L &l)
NoDefaultCtor(const QString &)
CurryImpl< F > Curry(F f)
FmapResult_t< T, F > Fmap(const T &functor, const F &function)
Apply the function f to the elements in functor.