LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
eithertest.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "eithertest.h"
31 #include <QtTest>
32 #include <either.h>
33 #include <curry.h>
34 #include <void.h>
35 
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
43 
44  void EitherTest::testBasicLeft ()
45  {
46  const auto& left = SomeEither_t::Left (1);
47  QCOMPARE (left.IsLeft (), true);
48  QCOMPARE (left.IsRight (), false);
49  QCOMPARE (left.GetLeft (), 1);
50 
51  bool hadCaught = false;
52  try
53  {
54  left.GetRight ();
55  }
56  catch (const std::exception&)
57  {
58  hadCaught = true;
59  }
60  QCOMPARE (hadCaught, true);
61  }
62 
63  void EitherTest::testBasicRight ()
64  {
65  const auto& right = SomeEither_t::Right ("foo");
66  QCOMPARE (right.IsLeft (), false);
67  QCOMPARE (right.IsRight (), true);
68  QCOMPARE (right.GetRight (), QString { "foo" });
69 
70  bool hadCaught = false;
71  try
72  {
73  right.GetLeft ();
74  }
75  catch (const std::exception&)
76  {
77  hadCaught = true;
78  }
79  QCOMPARE (hadCaught, true);
80  }
81 
82  void EitherTest::testFMapLeft ()
83  {
84  const auto& left = SomeEither_t::Left (1);
85  const auto& fmapped = Fmap (left, [] (const QString& str) { return str + "_mapped"; });
86  QCOMPARE (fmapped, left);
87  }
88 
89  void EitherTest::testFMapRight ()
90  {
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" });
94  }
95 
96  void EitherTest::testFMapRightChangeType ()
97  {
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 ()));
101  }
102 
103  void EitherTest::testPure ()
104  {
105  const auto& pure = Pure<Either, int> (QString { "foo" });
106  QCOMPARE (pure, SomeEither_t::Right ("foo"));
107  }
108 
109  void EitherTest::testGSL ()
110  {
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"));
114  }
115 
116  void EitherTest::testGSLLeft ()
117  {
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);
122  }
123 
124  void EitherTest::testGSLCurry ()
125  {
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"));
131  }
132 
133  void EitherTest::testGSLCurryLeft ()
134  {
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;
139  QCOMPARE (app, s1);
140  }
141 
142  void EitherTest::testBind ()
143  {
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"));
147  }
148 
149  void EitherTest::testBindLeft ()
150  {
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);
155  }
156 
158  {
159  NoDefaultCtor () = delete;
160  NoDefaultCtor (const QString&)
161  {
162  }
163 
164  bool operator== (const NoDefaultCtor&) const
165  {
166  return true;
167  }
168  };
169 
170  void EitherTest::testBindLeftNotConstructed ()
171  {
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);
177  }
178 }
179 }
static Either Right(const R &r)
Definition: either.h:145
bool operator==(const NoDefaultCtor &) const
Definition: eithertest.cpp:164
static Either Left(const L &l)
Definition: either.h:140
CurryImpl< F > Curry(F f)
Definition: curry.h:104
FmapResult_t< T, F > Fmap(const T &functor, const F &function)
Apply the function f to the elements in functor.
Definition: functor.h:147