1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 EitherLeftProjectionTest {
32 private final Either<String, Integer> l = left("heyaa!");
33 private final Either<String, Integer> r = right(12);
34 final Supplier<String> boo = () -> "boo!";
35
36 Function<String, Either<String, Integer>> reverseToEither = from -> left(reverse.apply(from));
37
38 @Test public void isDefined() {
39 assertThat(l.left().isDefined(), is(true));
40 }
41
42 @Test public void isNotDefined() {
43 assertThat(r.left().isDefined(), is(false));
44 }
45
46 @Test public void isEmpty() {
47 assertThat(r.left().isEmpty(), is(true));
48 }
49
50 @Test public void isNotEmpty() {
51 assertThat(l.left().isEmpty(), is(false));
52 }
53
54 @Test public void either() {
55 assertThat(l.left().either(), is(l));
56 assertThat(r.left().either(), is(r));
57 }
58
59 @Test public void iteratorNotEmpty() {
60 assertThat(l.left().iterator().next(), is("heyaa!"));
61 }
62
63 @Test public void iteratorEmpty() {
64 assertThat(r.left().iterator().hasNext(), is(false));
65 }
66
67 @Test public void getOrNullDefined() {
68 assertThat(l.left().getOrNull(), is("heyaa!"));
69 }
70
71 @Test public void getOrNullEmpty() {
72 assertThat(r.left().getOrNull(), nullValue());
73 }
74
75 @Test public void getOrErrorDefined() {
76 assertThat(l.left().getOrError(boo), is("heyaa!"));
77 }
78
79 @Test(expected = AssertionError.class) public void getOrErrorEmpty() {
80 r.left().getOrError(boo);
81 }
82
83 @Test public void getOrElseDefined() {
84 assertThat(l.left().getOrElse("foo"), is("heyaa!"));
85 }
86
87 @Test public void getOrElseEmpty() {
88 assertThat(r.left().getOrElse("foo"), is("foo"));
89 }
90
91 @Test public void getOrElseSupplierDefined() {
92 assertThat(l.left().getOr(boo), is("heyaa!"));
93 }
94
95 @Test public void getOrElseSupplierEmpty() {
96 assertThat(r.left().getOr(boo), is("boo!"));
97 }
98
99 @Test public void existsDefinedTrue() {
100 assertThat(l.left().exists(x -> true), is(true));
101 }
102
103 @Test public void existsDefinedFalse() {
104 assertThat(l.left().exists(x -> false), is(false));
105 }
106
107 @Test public void existsNotDefinedTrue() {
108 assertThat(r.left().exists(x -> true), is(false));
109 }
110
111 @Test public void existsNotDefinedFalse() {
112 assertThat(r.left().exists(x -> false), is(false));
113 }
114
115 @Test public void forallDefinedTrue() {
116 assertThat(l.left().forall(x -> true), is(true));
117 }
118
119 @Test public void forallDefinedFalse() {
120 assertThat(l.left().forall(x -> false), is(false));
121 }
122
123 @Test public void forallNotDefinedTrue() {
124 assertThat(r.left().forall(x -> true), is(true));
125 }
126
127 @Test public void forallNotDefinedFalse() {
128 assertThat(r.left().forall(x -> false), is(true));
129 }
130
131 @Test public void foreachDefined() {
132 assertThat(Count.countEach(l.left()), is(1));
133 }
134
135 @Test public void foreachNotDefined() {
136 assertThat(Count.countEach(r.left()), is(0));
137 }
138
139 @Test public void onDefined() {
140 assertThat(l.left().on(toStringFunction()), is("heyaa!"));
141 }
142
143 @Test public void onNotDefined() {
144 assertThat(r.left().on(toStringFunction()), is("12"));
145 }
146
147 @Test public void mapDefined() {
148 assertThat(l.left().map(reverse).left().get(), is("!aayeh"));
149 }
150
151 @Test public void mapNotDefined() {
152 assertThat(r.left().map(reverse).right().get(), is(12));
153 }
154
155 @Test public void flatMapDefined() {
156 assertThat(l.left().flatMap(reverseToEither).left().get(), is("!aayeh"));
157 }
158
159 @Test public void flatMapNotDefined() {
160 assertThat(r.left().flatMap(reverseToEither).right().get(), is(12));
161 }
162
163 @Test public void sequenceDefined() {
164 final Either<String, Integer> e = left("bar");
165 assertThat(l.left().sequence(e).left().get(), is("bar"));
166 }
167
168 @Test public void sequenceNotDefined() {
169 final Either<String, Integer> e = left("bar");
170 assertThat(r.left().sequence(e).right().get(), is(12));
171 }
172
173 @Test public void filterDefinedTrue() {
174 final Option<Either<String, Object>> filtered = l.left().filter(x -> true);
175 assertThat(filtered.isDefined(), is(true));
176 assertThat(filtered.get().left().isDefined(), is(true));
177 }
178
179 @Test public void filterDefinedFalse() {
180 final Option<Either<String, Object>> filtered = l.left().filter(x -> false);
181 assertThat(filtered.isDefined(), is(false));
182 }
183
184 @Test public void filterNotDefined() {
185 final Option<Either<String, Object>> filtered = r.left().filter(x -> true);
186 assertThat(filtered.isDefined(), is(false));
187 }
188
189 @Test public void applyDefinedLeft() {
190 final Either<Function<String, String>, Integer> func = left(reverse);
191 assertThat(l.left().ap(func).left().get(), is("!aayeh"));
192 }
193
194 @Test public void applyDefinedRight() {
195 final Either<Function<String, String>, Integer> func = right(36);
196 assertThat(l.left().ap(func).right().get(), is(36));
197 }
198
199 @Test public void applyNotDefinedLeft() {
200 final Either<Function<String, String>, Integer> func = left(reverse);
201 assertThat(r.left().ap(func).right().get(), is(12));
202 }
203
204 @Test public void applyNotDefinedRight() {
205 final Either<Function<String, String>, Integer> func = right(36);
206 assertThat(r.left().ap(func).right().get(), is(36));
207 }
208
209 static class MyException extends Exception {
210 private static final long serialVersionUID = -1056362494708225175L;
211 }
212
213 @Test public void getOrThrowLeft() throws MyException {
214 assertThat(l.left().getOrThrow(MyException::new), is("heyaa!"));
215 }
216
217 @Test(expected = MyException.class) public void getOrThrowRight() throws MyException {
218 r.left().getOrThrow(MyException::new);
219 }
220 }