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.NoSuchElementException;
21 import java.util.Optional;
22 import java.util.function.Function;
23 import java.util.stream.Stream;
24
25 import static io.atlassian.fugue.Option.defined;
26 import static io.atlassian.fugue.Option.none;
27 import static io.atlassian.fugue.Option.some;
28 import static io.atlassian.fugue.Suppliers.ofInstance;
29 import static io.atlassian.fugue.UtilityFunctions.toStringFunction;
30 import static java.util.stream.Collectors.toList;
31 import static org.hamcrest.MatcherAssert.assertThat;
32 import static org.hamcrest.Matchers.contains;
33 import static org.hamcrest.Matchers.empty;
34 import static org.hamcrest.Matchers.emptyIterable;
35 import static org.hamcrest.Matchers.equalTo;
36 import static org.hamcrest.Matchers.is;
37 import static org.hamcrest.Matchers.not;
38 import static org.hamcrest.Matchers.notNullValue;
39
40 public class OptionTest {
41 @Test public void foldOnNoneReturnsValueFromSupplier() {
42 assertThat(none().fold(ofInstance("a"), toStringFunction()), is(equalTo("a")));
43 }
44
45 @Test public void foldOnSomeReturnsValueAfterFunctionIsApplied() {
46 assertThat(some(1).fold(ofInstance(0), increment()), is(equalTo(2)));
47 }
48
49 @Test public void isDefinedIsTrueForSome() {
50 assertThat(some("a").isDefined(), is(true));
51 }
52
53 @Test public void isDefinedIsFalseForNone() {
54 assertThat(none().isDefined(), is(false));
55 }
56
57 @Test public void getOnSomeReturnsValue() {
58 assertThat(some(1).get(), is(equalTo(1)));
59 }
60
61 @Test(expected = NoSuchElementException.class) public void getOnNoneThrowsException() {
62 none().get();
63 }
64
65 @Test public void getOrElseOnSomeReturnsValue() {
66 assertThat(some(1).getOrElse(0), is(equalTo(1)));
67 }
68
69 @Test public void getOrElseOnNoneReturnsElseValue() {
70 assertThat(none(Integer.class).getOrElse(0), is(equalTo(0)));
71 }
72
73 @Test public void getOrElseOnNoneReturnsValueFromSupplier() {
74 assertThat(none(Integer.class).getOr(ofInstance(0)), is(equalTo(0)));
75 }
76
77 @Test public void getElseOnNoneReturnsValueFromSupplier() {
78 assertThat(none(Integer.class).getOr(ofInstance(0)), is(equalTo(0)));
79 }
80
81 @Test public void getElseOnNoneReturnsValueFromLambda() {
82 assertThat(none(Integer.class).getOr(() -> 0), is(equalTo(0)));
83 }
84
85 @Test public void iteratorOverSomeContainsOnlyValue() {
86 assertThat(some(1), contains(1));
87 }
88
89 @Test public void noneIsEmptyIterable() {
90 assertThat(none(), is(emptyIterable()));
91 }
92
93 @Test public void mapAppliesFunctionToSomeValue() {
94 assertThat(some(1).map(increment()), is(equalTo(some(2))));
95 }
96
97 @Test public void mapOverNoneDoesNothing() {
98 assertThat(none(Integer.class).map(increment()), is(equalTo(none(Integer.class))));
99 }
100
101 @Test public void flatMapAppliesFunctionToSomeValue() {
102 assertThat(some(1).flatMap(liftedIncrement()), is(equalTo(some(2))));
103 }
104
105 @Test public void flatMapOverNoneDoesNothing() {
106 assertThat(none(Integer.class).flatMap(liftedIncrement()), is(equalTo(none(Integer.class))));
107 }
108
109 @Test public void equalSomesAreEqual() {
110 assertThat(some(2), is(some(2)));
111 }
112
113 @Test public void nonEqualSomesAreNotEqual() {
114 assertThat(some(1), is(not(some(2))));
115 }
116
117 @Test public void hashCodesFromEqualSomesAreEqual() {
118 assertThat(some(1).hashCode(), is(some(1).hashCode()));
119 }
120
121 @Test public void noneSomeEquality() {
122 assertThat(none().equals(some("")), is(false));
123 }
124
125 @Test public void someNoneEquality() {
126 assertThat(some("").equals(none(String.class)), is(false));
127 }
128
129 @Test public void someSomeEquality() {
130 assertThat(some("something"), is(some("something")));
131 }
132
133 @Test public void noneNoneEquality() {
134 assertThat(none(), is(equalTo(none())));
135 }
136
137 @Test public void someOrElseReturnsOriginal() {
138 assertThat(some(1).orElse(some(2)), is(equalTo(some(1))));
139 }
140
141 @Test public void noneOrElseReturnsOrElse() {
142 assertThat(none(int.class).orElse(some(2)), is(equalTo(some(2))));
143 }
144
145 @Test public void someOrElseSupplierReturnsOriginal() {
146 assertThat(some(1).orElse(ofInstance(some(2))), is(equalTo(some(1))));
147 }
148
149 @Test public void noneOrElseSupplierReturnsOrElse() {
150 assertThat(none(int.class).orElse(ofInstance(some(2))), is(equalTo(some(2))));
151 }
152
153 @Test public void definedOnSome() {
154 assertThat(defined().test(some("a")), is(true));
155 }
156
157 @Test public void definedOnNone() {
158 assertThat(defined().test(none()), is(false));
159 }
160
161 @Test public void someToOptional() {
162 assertThat(some("value").toOptional(), is(Optional.of("value")));
163 }
164
165 @Test(expected = NullPointerException.class) public void someWithNullToOptional() {
166 some("null").map(Functions.constant(null)).toOptional();
167 }
168
169 @Test public void noneToOptional() {
170 assertThat(none().toOptional(), is(Optional.empty()));
171 }
172
173 @Test public void someToStream() {
174 final Stream<String> stream = some("value").toStream();
175 assertThat(stream, notNullValue());
176 assertThat(stream.collect(toList()), contains("value"));
177 }
178
179 @Test public void someWithNullToStream() {
180 final String nullString = null;
181 final Stream<String> stream = some("null").map(Functions.constant(nullString)).toStream();
182 assertThat(stream, notNullValue());
183 assertThat(stream.collect(toList()), contains(nullString));
184 }
185
186 @Test public void noneToStream() {
187 final Stream<String> stream = Option.<String> none().toStream();
188 assertThat(stream, notNullValue());
189 assertThat(stream.collect(toList()), empty());
190 }
191
192
193
194
195
196 private Function<Integer, Option<Integer>> liftedIncrement() {
197 return Functions.compose(Functions.<Integer> nullToOption(), increment());
198 }
199
200 private Function<Integer, Integer> increment() {
201 return i -> i + 1;
202 }
203 }