43 void EitherTest::testBasicLeft ()
45 const auto& left = SomeEither_t::Left (1);
46 QCOMPARE (left.IsLeft (),
true);
47 QCOMPARE (left.IsRight (),
false);
48 QCOMPARE (left.GetLeft (), 1);
50 bool hadCaught =
false;
55 catch (
const std::exception&)
59 QCOMPARE (hadCaught,
true);
62 void EitherTest::testBasicRight ()
64 const auto& right = SomeEither_t::Right (
"foo");
65 QCOMPARE (right.IsLeft (),
false);
66 QCOMPARE (right.IsRight (),
true);
67 QCOMPARE (right.GetRight (), QString {
"foo" });
69 bool hadCaught =
false;
74 catch (
const std::exception&)
78 QCOMPARE (hadCaught,
true);
81 void EitherTest::testFMapLeft ()
83 const auto& left = SomeEither_t::Left (1);
84 const auto& fmapped =
Fmap (left, [] (
const QString& str) {
return str +
"_mapped"; });
85 QCOMPARE (fmapped, left);
88 void EitherTest::testFMapRight ()
90 const auto& right = SomeEither_t::Right (
"foo");
91 const auto& fmapped =
Fmap (right, [] (
const QString& str) {
return str +
"_mapped"; });
92 QCOMPARE (fmapped.GetRight (), QString {
"foo_mapped" });
95 void EitherTest::testFMapRightChangeType ()
97 const auto& right = SomeEither_t::Right (
"foo");
98 const auto& fmapped =
Fmap (right, [] (
const QString& str) {
return static_cast<long> (str.size ()); });
99 QCOMPARE (fmapped.GetRight (),
static_cast<long> (right.GetRight ().size ()));
102 void EitherTest::testPure ()
104 const auto& pure = Pure<Either, int> (QString {
"foo" });
105 QCOMPARE (pure, SomeEither_t::Right (
"foo"));
108 void EitherTest::testGSL ()
110 const auto& pure = Pure<Either, int> ([] (
const QString& s) {
return s +
"_pure"; });
111 const auto& app = pure * Pure<Either, int> (QString {
"foo" });
112 QCOMPARE (app, SomeEither_t::Right (
"foo_pure"));
115 void EitherTest::testGSLLeft ()
117 const auto& pure = Pure<Either, int> ([] (
const QString& s) {
return s +
"_pure"; });
118 const auto& value = SomeEither_t::Left (2);
119 const auto& app = pure * value;
120 QCOMPARE (app, value);
123 void EitherTest::testGSLCurry ()
125 const auto& summer = Pure<Either, int> (
Curry ([] (
const QString& a,
const QString& b) {
return a + b; }));
126 const auto& s1 = Pure<Either, int> (QString {
"foo" });
127 const auto& s2 = Pure<Either, int> (QString {
"bar" });
128 const auto& app = summer * s1 * s2;
129 QCOMPARE (app, SomeEither_t::Right (
"foobar"));
132 void EitherTest::testGSLCurryLeft ()
134 const auto& summer = Pure<Either, int> (
Curry ([] (
const QString& a,
const QString& b) {
return a + b; }));
135 const auto& s1 = SomeEither_t::Left (2);
136 const auto& s2 = Pure<Either, int> (QString {
"bar" });
137 const auto& app = summer * s1 * s2;
141 void EitherTest::testBind ()
143 const auto& res = Return<Either, int> (QString {
"foo" }) >>
144 [] (
const QString& right) {
return SomeEither_t::Right (right +
"_bound"); };
145 QCOMPARE (res, SomeEither_t::Right (
"foo_bound"));
148 void EitherTest::testBindLeft ()
150 const auto& value = SomeEither_t::Left (2);
151 const auto& res = value >>
152 [] (
const QString& right) {
return SomeEither_t::Right (right +
"_bound"); };
153 QCOMPARE (res, value);
FmapResult_t< T, F > Fmap(const T &t, const F &f)
CurryImpl< F > Curry(F f)