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 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 applyDefinedRight() {
190 final Either<Integer, Function<String, String>> func = right(reverse);
191 assertThat(r.right().ap(func).right().get(), is("!aayeh"));
192 }
193
194 @Test public void applyDefinedLeft() {
195 final Either<Integer, Function<String, String>> func = left(36);
196 assertThat(r.right().ap(func).left().get(), is(36));
197 }
198
199 @Test public void applyNotDefinedRight() {
200 final Either<Integer, Function<String, String>> func = right(reverse);
201 assertThat(l.right().ap(func).left().get(), is(12));
202 }
203
204 @Test public void applyNotDefinedLeft() {
205 final Either<Integer, Function<String, String>> func = left(36);
206 assertThat(l.right().ap(func).left().get(), is(36));
207 }
208
209 static class MyException extends Exception {
210 private static final long serialVersionUID = -1056362494708225175L;
211 }
212
213 @Test public void getOrThrowRight() throws MyException {
214 assertThat(r.right().<MyException> getOrThrow(MyException::new), is("heyaa!"));
215 }
216
217 @Test(expected = MyException.class) public void getOrThrowLeft() throws MyException {
218 l.right().<MyException> getOrThrow(MyException::new);
219 }
220 }