1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.atlassian.fugue;
17
18 import org.hamcrest.Matchers;
19 import org.junit.Test;
20
21 import java.io.IOException;
22 import java.util.NoSuchElementException;
23 import java.util.Optional;
24
25 import static io.atlassian.fugue.Either.left;
26 import static io.atlassian.fugue.Eithers.getOrThrow;
27 import static io.atlassian.fugue.Option.none;
28 import static io.atlassian.fugue.Option.some;
29 import static io.atlassian.fugue.UtilityFunctions.bool2String;
30 import static io.atlassian.fugue.UtilityFunctions.int2String;
31 import static java.lang.String.valueOf;
32 import static java.util.stream.Collectors.toList;
33 import static org.hamcrest.Matchers.contains;
34 import static org.hamcrest.Matchers.empty;
35 import static org.hamcrest.Matchers.is;
36 import static org.junit.Assert.assertThat;
37
38 public class EitherLeftTest {
39 private static final Boolean ORIGINAL_VALUE = true;
40 final Either<Boolean, Integer> either = left(ORIGINAL_VALUE);
41
42 @Test public void leftGet() {
43 assertThat(either.left().get(), is(ORIGINAL_VALUE));
44 }
45
46 @Test public void right() {
47 assertThat(either.right().isDefined(), is(false));
48 }
49
50 @Test public void isRight() {
51 assertThat(either.isRight(), is(false));
52 }
53
54 @Test public void isLeft() {
55 assertThat(either.isLeft(), is(true));
56 }
57
58 @Test(expected = NoSuchElementException.class) public void getRight() {
59 either.getRight();
60 }
61
62 @Test public void swapIsRight() {
63 assertThat(either.swap().isRight(), is(true));
64 }
65
66 @Test public void swapRightIsEitherLeft() {
67 assertThat(either.swap().right().get(), is(either.left().get()));
68 }
69
70 @Test public void swapRightIsOriginal() {
71 assertThat(either.swap().right().get(), is(ORIGINAL_VALUE));
72 }
73
74 @Test public void map() {
75 assertThat(either.fold(bool2String, int2String), is(valueOf(ORIGINAL_VALUE)));
76 }
77
78 @Test public void mapRight() {
79 assertThat(either.right().map(int2String).right().isEmpty(), is(true));
80 }
81
82 @Test public void mapLeft() {
83 assertThat(either.left().map(bool2String).left().get(), is(valueOf(ORIGINAL_VALUE)));
84 }
85
86 @Test public void toStringTest() {
87 assertThat(either.toString(), is("Either.Left(true)"));
88 }
89
90 @Test public void hashCodeTest() {
91 assertThat(either.hashCode(), is(~ORIGINAL_VALUE.hashCode()));
92 }
93
94 @Test public void equalsItself() {
95 assertThat(either.equals(either), is(true));
96 }
97
98 @Test public void notEqualsNull() {
99 assertThat(either.equals(null), is(false));
100 }
101
102 @Test public void leftPredicateMatches() {
103 assertThat(Eithers.<Boolean, Integer> isLeft().test(either), is(true));
104 }
105
106 @Test public void toOptionTest() {
107 assertThat(either.left().toOption(), is(some(ORIGINAL_VALUE)));
108 assertThat(either.right().toOption(), is(none()));
109 }
110
111 @Test public void toOptionalTest() {
112 assertThat(either.left().toOptional(), is(Optional.of(ORIGINAL_VALUE)));
113 assertThat(either.right().toOptional(), is(Optional.empty()));
114 }
115
116 @Test public void toStreamTest() {
117 assertThat(either.left().toStream().collect(toList()), contains(ORIGINAL_VALUE));
118 assertThat(either.right().toStream().collect(toList()), empty());
119 }
120
121 @Test(expected = IOException.class) public void throwsException() throws IOException {
122 final Either<IOException, String> either = left(new IOException());
123 getOrThrow(either);
124 }
125
126 @Test public void upcastLeftOnLeft() {
127 final Either<Integer, String> e = Either.left(1);
128 final Either<Number, String> result = Eithers.<Number, Integer, String> upcastLeft(e);
129 final Number expected = 1;
130 assertThat(result.left().get(), is(expected));
131 }
132
133 @Test public void upcastLeftOnRight() {
134 final Either<Integer, String> e = Either.right("a");
135 final Either<Number, String> result = Eithers.<Number, Integer, String> upcastLeft(e);
136 assertThat(result.getRight(), is("a"));
137 }
138
139 @Test public void flatMapLeftSubTypes() {
140 class ErrorType {}
141 class AnotherErrorType extends ErrorType {}
142
143 final AnotherErrorType anotherErrorType = new AnotherErrorType();
144 final Either<AnotherErrorType, Integer> l = Either.left(anotherErrorType);
145
146 final Either<AnotherErrorType, Integer> either = Either.<ErrorType, Integer> left(new ErrorType()).left().flatMap(input -> l);
147
148 final ErrorType errorType = either.left().get();
149
150 assertThat(errorType, Matchers.<ErrorType> is(anotherErrorType));
151 }
152
153 @Test public void flatMapLeftWithUpcastAndSubtypes() {
154 class ErrorType {}
155 class MyErrorType extends ErrorType {}
156 class AnotherErrorType extends ErrorType {}
157
158 final MyErrorType myErrorType = new MyErrorType();
159 final AnotherErrorType anotherErrorType = new AnotherErrorType();
160
161 final Either<MyErrorType, Integer> l = Either.left(myErrorType);
162 final Either<AnotherErrorType, Integer> l2 = Either.left(anotherErrorType);
163
164 final Either<AnotherErrorType, Integer> either = Eithers.<ErrorType, MyErrorType, Integer> upcastLeft(l).left().flatMap(input -> l2);
165
166 final ErrorType errorType = either.left().get();
167
168 assertThat(errorType, Matchers.<ErrorType> is(anotherErrorType));
169 }
170 }