44 void EitherTest::testBasicLeft ()
46 const auto& left = SomeEither_t::Left (1);
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 ()
65 const auto& right = SomeEither_t::Right (
"foo");
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 ()
84 const auto& left = SomeEither_t::Left (1);
85 const auto& fmapped =
Fmap (left, [] (
const QString& str) {
return str +
"_mapped"; });
86 QCOMPARE (fmapped, left);
89 void EitherTest::testFMapRight ()
91 const auto& right = SomeEither_t::Right (
"foo");
92 const auto& fmapped =
Fmap (right, [] (
const QString& str) {
return str +
"_mapped"; });
93 QCOMPARE (fmapped.GetRight (), QString {
"foo_mapped" });
96 void EitherTest::testFMapRightChangeType ()
98 const auto& right = SomeEither_t::Right (
"foo");
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" });
106 QCOMPARE (pure, SomeEither_t::Right (
"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" });
113 QCOMPARE (app, SomeEither_t::Right (
"foo_pure"));
116 void EitherTest::testGSLLeft ()
118 const auto& pure = Pure<Either, int> ([] (
const QString& s) {
return s +
"_pure"; });
119 const auto& value = SomeEither_t::Left (2);
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;
130 QCOMPARE (app, SomeEither_t::Right (
"foobar"));
133 void EitherTest::testGSLCurryLeft ()
135 const auto& summer = Pure<Either, int> (
Curry ([] (
const QString& a,
const QString& b) {
return a + b; }));
136 const auto& s1 = SomeEither_t::Left (2);
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" }) >>
145 [] (
const QString& right) {
return SomeEither_t::Right (right +
"_bound"); };
146 QCOMPARE (res, SomeEither_t::Right (
"foo_bound"));
149 void EitherTest::testBindLeft ()
151 const auto& value = SomeEither_t::Left (2);
152 const auto& res = value >>
153 [] (
const QString& right) {
return SomeEither_t::Right (right +
"_bound"); };
154 QCOMPARE (res, value);
170 void EitherTest::testBindLeftNotConstructed ()
172 const auto& value = Either<NoDefaultCtor, Void>::Right ({});
173 const auto& expected = Either<NoDefaultCtor, int>::Right (5);
174 const auto res = value >>
175 [&expected] (
const auto&) {
return expected; };
176 QCOMPARE (res, expected);
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.
bool operator==(const ANBoolFieldValue &left, const ANBoolFieldValue &right)
Compares two fields with boolean values.