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.hamcrest.MatcherAssert;
19  import org.junit.Test;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.function.BiFunction;
28  import java.util.function.Function;
29  import java.util.function.Predicate;
30  
31  import static io.atlassian.fugue.Eithers.getOrThrow;
32  import static io.atlassian.fugue.Iterables.filter;
33  import static io.atlassian.fugue.Iterables.isEmpty;
34  import static io.atlassian.fugue.Iterables.size;
35  import static io.atlassian.fugue.Option.fromOptional;
36  import static io.atlassian.fugue.Option.none;
37  import static io.atlassian.fugue.Option.option;
38  import static io.atlassian.fugue.Option.some;
39  import static io.atlassian.fugue.Options.ap;
40  import static io.atlassian.fugue.Options.filterNone;
41  import static io.atlassian.fugue.Options.find;
42  import static io.atlassian.fugue.Options.flatten;
43  import static io.atlassian.fugue.Options.lift;
44  import static org.hamcrest.Matchers.equalTo;
45  import static org.hamcrest.Matchers.is;
46  import static org.hamcrest.Matchers.sameInstance;
47  import static org.junit.Assert.assertThat;
48  
49  public class OptionStaticsTest {
50    static final Integer NULL = null;
51  
52    @Test public void getNull() {
53      assertThat(option(null), is(sameInstance(none())));
54    }
55  
56    @Test public void get() {
57      assertThat(option("Winter.").get(), is("Winter."));
58    }
59  
60    @Test(expected = InvocationTargetException.class) public void nonInstantiable() throws Exception {
61      getOrThrow(UtilityFunctions.<Options> defaultCtor().apply(Options.class));
62    }
63  
64    @Test public void identity() {
65      assertThat(none(), is(sameInstance(none())));
66    }
67  
68    private List<Option<Integer>> twoOptions() {
69      return Arrays.asList(option(NULL), option(2), option(NULL), option(4));
70    }
71  
72    private List<Option<Integer>> fourNones() {
73      return Arrays.asList(option(NULL), option(NULL), option(NULL), option(NULL));
74    }
75  
76    @Test public void upcastSome() {
77      final Option<Integer> some = some(1);
78      final Option<Number> result = Options.<Number, Integer> upcast(some);
79      final Number expected = 1;
80      assertThat(result.get(), is(expected));
81    }
82  
83    @Test public void upcastNone() {
84      final Option<Integer> none = Option.none();
85      final Option<Number> result = Options.<Number, Integer> upcast(none);
86      assertThat(result, is(sameInstance(Option.<Number> none())));
87    }
88  
89    @Test public void liftToString() {
90      assertThat(lift(UtilityFunctions.bool2String).apply(some(true)), is(some(String.valueOf(true))));
91    }
92  
93    @Test public void liftNone() {
94      assertThat(lift(UtilityFunctions.bool2String).apply(Option.<Boolean> none()), is(sameInstance(Option.<String> none())));
95    }
96  
97    @Test public void liftFunction() {
98      assertThat(liftBool2StringFunction().apply(Option.some(true)), is(Option.some(String.valueOf(true))));
99    }
100 
101   @Test public void liftFunctionNone() {
102     final Function<Option<Boolean>, Option<String>> liftedBool2String = liftBool2StringFunction();
103     assertThat(liftedBool2String.apply(Option.<Boolean> none()), is(sameInstance(Option.<String> none())));
104   }
105 
106   private Function<Option<Boolean>, Option<String>> liftBool2StringFunction() {
107     return Options.<Boolean, String> lift().apply(UtilityFunctions.bool2String);
108   }
109 
110   @Test public void apTest() {
111     assertThat(ap(Option.some(false), Option.some(UtilityFunctions.bool2String)), is(Option.some(String.valueOf(false))));
112   }
113 
114   @Test public void apNone() {
115     assertThat(ap(Option.<Boolean> none(), Option.some(UtilityFunctions.bool2String)), is(sameInstance(Option.<String> none())));
116   }
117 
118   @Test public void apNoneFunction() {
119     assertThat(ap(Option.<Boolean> none(), Option.<Function<Boolean, Integer>> none()), is(sameInstance(Option.<Integer> none())));
120   }
121 
122   @Test public void lift2() {
123     final BiFunction<Option<String>, Option<Integer>, Option<Option<Character>>> liftedCharAt = Options.lift2(UtilityFunctions.charAt);
124     Option<Option<Character>> b = Option.some(Option.some('b'));
125     assertThat(liftedCharAt.apply(some("abc"), Option.some(1)), is(b));
126   }
127 
128   @Test public void lift2FirstNone() {
129     final BiFunction<Option<String>, Option<Integer>, Option<Option<Character>>> liftedCharAt = Options.lift2(UtilityFunctions.charAt);
130     assertThat(liftedCharAt.apply(Option.<String> none(), Option.some(1)), is(sameInstance(Option.<Option<Character>> none())));
131   }
132 
133   @Test public void lift2SecondNone() {
134     final BiFunction<Option<String>, Option<Integer>, Option<Option<Character>>> liftedCharAt = Options.lift2(UtilityFunctions.charAt);
135     assertThat(liftedCharAt.apply(some("abc"), Option.<Integer> none()), is(sameInstance(Option.<Option<Character>> none())));
136   }
137 
138   @Test public void lift2Function() {
139     final BiFunction<Option<String>, Option<Integer>, Option<Option<Character>>> liftedCharAt = liftCharAtFunction();
140     Option<Option<Character>> b = some(some('b'));
141     assertThat(liftedCharAt.apply(some("abc"), some(1)), is(b));
142   }
143 
144   @Test public void liftPredicate() {
145     final Predicate<Option<Integer>> lifted = lift(Predicate.isEqual(3));
146     assertThat(lifted.test(some(3)), is(true));
147     assertThat(lifted.test(some(2)), is(false));
148     assertThat(lifted.test(Option.<Integer> none()), is(false));
149   }
150 
151   private BiFunction<Option<String>, Option<Integer>, Option<Option<Character>>> liftCharAtFunction() {
152     return Options.<String, Integer, Option<Character>> lift2().apply(UtilityFunctions.charAt);
153   }
154 
155   @Test public void findFindsFirst() {
156     assertThat(find(twoOptions()).get(), is(2));
157   }
158 
159   @Test public void findFindsOneSingleton() {
160     assertThat(find(Collections.singletonList(option(3))).get(), is(3));
161   }
162 
163   @Test public void findFindsNone() {
164     assertThat(find(fourNones()).isDefined(), is(false));
165   }
166 
167   @Test public void findFindsNoneSingleton() {
168     assertThat(find(Collections.singletonList(option(NULL))).isDefined(), is(false));
169   }
170 
171   @Test public void filterFindsTwo() {
172     final Iterable<Option<Integer>> filtered = filterNone(twoOptions());
173     assertThat(size(filtered), is(2));
174     final Iterator<Option<Integer>> it = filtered.iterator();
175     assertThat(it.next().get(), is(2));
176     assertThat(it.next().get(), is(4));
177     assertThat(it.hasNext(), is(false));
178   }
179 
180   @Test public void flattenFindsTwo() {
181     final Iterable<Integer> flattened = flatten(twoOptions());
182     assertThat(size(flattened), is(2));
183     final Iterator<Integer> it = flattened.iterator();
184     assertThat(it.next(), is(2));
185     assertThat(it.next(), is(4));
186     assertThat(it.hasNext(), is(false));
187   }
188 
189   @Test public void filterFindsNone() {
190     assertThat(isEmpty().test(filterNone(fourNones())), is(true));
191   }
192 
193   @Test public void filterNones() {
194     final List<Option<Integer>> list = Arrays.asList(some(1), none(Integer.class), some(2));
195     MatcherAssert.assertThat(size(filterNone(list)), is(equalTo(2)));
196   }
197 
198   @Test public void someDefined() {
199     MatcherAssert.assertThat(filter(Collections.singletonList(some(3)), Maybe::isDefined).iterator().hasNext(), is(true));
200   }
201 
202   @Test public void noneNotDefined() {
203     // throw new RuntimeException();
204     MatcherAssert.assertThat(filter(Collections.singletonList(none(int.class)), Maybe::isDefined).iterator().hasNext(), is(false));
205   }
206 
207   @Test public void fromPresentOptional() {
208     assertThat(fromOptional(Optional.of("value")), is(some("value")));
209   }
210 
211   @Test public void fromEmptyOptional() {
212     assertThat(fromOptional(Optional.empty()), is(none()));
213   }
214 
215 }