View Javadoc
1   /*
2      Copyright 2011 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue;
17  
18  import org.junit.Test;
19  
20  import java.util.function.Function;
21  import java.util.function.Supplier;
22  
23  import static io.atlassian.fugue.Either.left;
24  import static io.atlassian.fugue.Either.right;
25  import static io.atlassian.fugue.UtilityFunctions.reverse;
26  import static io.atlassian.fugue.UtilityFunctions.toStringFunction;
27  import static org.hamcrest.Matchers.is;
28  import static org.hamcrest.Matchers.nullValue;
29  import static org.junit.Assert.assertThat;
30  
31  public class EitherRightProjectionTest {
32    private final Either<Integer, String> r = right("heyaa!");
33    private final Either<Integer, String> l = left(12);
34    final Supplier<String> boo = () -> "boo!";
35  
36    static Function<String, Either<Integer, String>> reverseToEither = from -> right(reverse.apply(from));
37  
38    @Test public void isDefined() {
39      assertThat(r.right().isDefined(), is(true));
40    }
41  
42    @Test public void isNotDefined() {
43      assertThat(l.right().isDefined(), is(false));
44    }
45  
46    @Test public void isEmpty() {
47      assertThat(l.right().isEmpty(), is(true));
48    }
49  
50    @Test public void isNotEmpty() {
51      assertThat(r.right().isEmpty(), is(false));
52    }
53  
54    @Test public void either() {
55      assertThat(r.right().either(), is(r));
56      assertThat(l.right().either(), is(l));
57    }
58  
59    @Test public void iteratorNotEmpty() {
60      assertThat(r.right().iterator().next(), is("heyaa!"));
61    }
62  
63    @Test public void iteratorEmpty() {
64      assertThat(l.right().iterator().hasNext(), is(false));
65    }
66  
67    @Test public void getOrNullDefined() {
68      assertThat(r.right().getOrNull(), is("heyaa!"));
69    }
70  
71    @Test public void getOrNullEmpty() {
72      assertThat(l.right().getOrNull(), nullValue());
73    }
74  
75    @Test public void getOrErrorDefined() {
76      assertThat(r.right().getOrError(boo), is("heyaa!"));
77    }
78  
79    @Test(expected = AssertionError.class) public void getOrErrorEmpty() {
80      l.right().getOrError(boo);
81    }
82  
83    @Test public void getOrElseDefined() {
84      assertThat(r.right().getOrElse("foo"), is("heyaa!"));
85    }
86  
87    @Test public void getOrElseEmpty() {
88      assertThat(l.right().getOrElse("foo"), is("foo"));
89    }
90  
91    @Test public void getOrElseSupplierDefined() {
92      assertThat(r.right().getOr(boo), is("heyaa!"));
93    }
94  
95    @Test public void getOrElseSupplierEmpty() {
96      assertThat(l.right().getOr(boo), is("boo!"));
97    }
98  
99    @Test public void existsDefinedTrue() {
100     assertThat(r.right().exists(x -> true), is(true));
101   }
102 
103   @Test public void existsDefinedFalse() {
104     assertThat(r.right().exists(x -> false), is(false));
105   }
106 
107   @Test public void existsNotDefinedTrue() {
108     assertThat(l.right().exists(x -> true), is(false));
109   }
110 
111   @Test public void existsNotDefinedFalse() {
112     assertThat(l.right().exists(x -> false), is(false));
113   }
114 
115   @Test public void forallDefinedTrue() {
116     assertThat(r.right().forall(x -> true), is(true));
117   }
118 
119   @Test public void forallDefinedFalse() {
120     assertThat(r.right().forall(x -> false), is(false));
121   }
122 
123   @Test public void forallNotDefinedTrue() {
124     assertThat(l.right().forall(x -> true), is(true));
125   }
126 
127   @Test public void forallNotDefinedFalse() {
128     assertThat(l.right().forall(x -> false), is(true));
129   }
130 
131   @Test public void foreachDefined() {
132     assertThat(Count.countEach(r.right()), is(1));
133   }
134 
135   @Test public void foreachNotDefined() {
136     assertThat(Count.countEach(l.right()), is(0));
137   }
138 
139   @Test public void onDefined() {
140     assertThat(r.right().on(toStringFunction()), is("heyaa!"));
141   }
142 
143   @Test public void onNotDefined() {
144     assertThat(l.right().on(toStringFunction()), is("12"));
145   }
146 
147   @Test public void mapDefined() {
148     assertThat(r.right().map(reverse).right().get(), is("!aayeh"));
149   }
150 
151   @Test public void mapNotDefined() {
152     assertThat(l.right().map(reverse).left().get(), is(12));
153   }
154 
155   @Test public void flatMapDefined() {
156     assertThat(r.right().flatMap(reverseToEither).right().get(), is("!aayeh"));
157   }
158 
159   @Test public void flatMapNotDefined() {
160     assertThat(l.right().flatMap(reverseToEither).left().get(), is(12));
161   }
162 
163   @Test public void sequenceDefined() {
164     final Either<Integer, String> e = right("bar");
165     assertThat(r.right().sequence(e).right().get(), is("bar"));
166   }
167 
168   @Test public void sequenceNotDefined() {
169     final Either<Integer, String> e = right("bar");
170     assertThat(l.right().sequence(e).left().get(), is(12));
171   }
172 
173   @Test public void filterDefinedTrue() {
174     final Option<Either<Integer, String>> filtered = r.right().filter(x -> true);
175     assertThat(filtered.isDefined(), is(true));
176     assertThat(filtered.get().right().isDefined(), is(true));
177   }
178 
179   @Test public void filterDefinedFalse() {
180     final Option<Either<Integer, String>> filtered = r.right().filter(x -> false);
181     assertThat(filtered.isDefined(), is(false));
182   }
183 
184   @Test public void filterNotDefined() {
185     final Option<Either<Integer, String>> filtered = l.right().filter(x -> true);
186     assertThat(filtered.isDefined(), is(false));
187   }
188 
189   @Test public void filterOrElseWithUnsatisfiedHandlerDefinedTrue() {
190     Either<Integer, String> filtered = r.right().filterOrElse(x -> true, () -> 300);
191     assertThat(filtered, is(r));
192   }
193 
194   @Test public void filterOrElseWithUnsatisfiedHandlerDefinedFalse() {
195     Either<Integer, String> filtered = r.right().filterOrElse(x -> false, () -> 300);
196     assertThat(filtered.right().isDefined(), is(false));
197     assertThat(filtered.left().get(), is(300));
198   }
199 
200   @Test public void filterOrElseWithUnsatisfiedHandlerNotDefined() {
201     Either<Integer, String> filtered = l.right().filterOrElse(x -> false, () -> 300);
202     assertThat(filtered, is(l));
203   }
204 
205   @Test public void applyDefinedRight() {
206     final Either<Integer, Function<String, String>> func = right(reverse);
207     assertThat(r.right().ap(func).right().get(), is("!aayeh"));
208   }
209 
210   @Test public void applyDefinedLeft() {
211     final Either<Integer, Function<String, String>> func = left(36);
212     assertThat(r.right().ap(func).left().get(), is(36));
213   }
214 
215   @Test public void applyNotDefinedRight() {
216     final Either<Integer, Function<String, String>> func = right(reverse);
217     assertThat(l.right().ap(func).left().get(), is(12));
218   }
219 
220   @Test public void applyNotDefinedLeft() {
221     final Either<Integer, Function<String, String>> func = left(36);
222     assertThat(l.right().ap(func).left().get(), is(36));
223   }
224 
225   static class MyException extends Exception {
226     private static final long serialVersionUID = -1056362494708225175L;
227   }
228 
229   @Test public void getOrThrowRight() throws MyException {
230     assertThat(r.right().<MyException> getOrThrow(MyException::new), is("heyaa!"));
231   }
232 
233   @Test(expected = MyException.class) public void getOrThrowLeft() throws MyException {
234     l.right().<MyException> getOrThrow(MyException::new);
235   }
236 }