LeechCraft  0.6.70-6645-gcd10d7e
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 
36 
37 namespace LeechCraft
38 {
39 namespace Util
40 {
42 
43  void EitherTest::testBasicLeft ()
44  {
45  const auto& left = SomeEither_t::Left (1);
46  QCOMPARE (left.IsLeft (), true);
47  QCOMPARE (left.IsRight (), false);
48  QCOMPARE (left.GetLeft (), 1);
49 
50  bool hadCaught = false;
51  try
52  {
53  left.GetRight ();
54  }
55  catch (const std::exception&)
56  {
57  hadCaught = true;
58  }
59  QCOMPARE (hadCaught, true);
60  }
61 
62  void EitherTest::testBasicRight ()
63  {
64  const auto& right = SomeEither_t::Right ("foo");
65  QCOMPARE (right.IsLeft (), false);
66  QCOMPARE (right.IsRight (), true);
67  QCOMPARE (right.GetRight (), QString { "foo" });
68 
69  bool hadCaught = false;
70  try
71  {
72  right.GetLeft ();
73  }
74  catch (const std::exception&)
75  {
76  hadCaught = true;
77  }
78  QCOMPARE (hadCaught, true);
79  }
80 
81  void EitherTest::testFMapLeft ()
82  {
83  const auto& left = SomeEither_t::Left (1);
84  const auto& fmapped = Fmap (left, [] (const QString& str) { return str + "_mapped"; });
85  QCOMPARE (fmapped, left);
86  }
87 
88  void EitherTest::testFMapRight ()
89  {
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" });
93  }
94 
95  void EitherTest::testFMapRightChangeType ()
96  {
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 ()));
100  }
101 
102  void EitherTest::testPure ()
103  {
104  const auto& pure = Pure<Either, int> (QString { "foo" });
105  QCOMPARE (pure, SomeEither_t::Right ("foo"));
106  }
107 
108  void EitherTest::testGSL ()
109  {
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"));
113  }
114 
115  void EitherTest::testGSLLeft ()
116  {
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);
121  }
122 
123  void EitherTest::testGSLCurry ()
124  {
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"));
130  }
131 
132  void EitherTest::testGSLCurryLeft ()
133  {
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;
138  QCOMPARE (app, s1);
139  }
140 
141  void EitherTest::testBind ()
142  {
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"));
146  }
147 
148  void EitherTest::testBindLeft ()
149  {
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);
154  }
155 }
156 }
FmapResult_t< T, F > Fmap(const T &t, const F &f)
Definition: functor.h:46
CurryImpl< F > Curry(F f)
Definition: curry.h:105