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.Predicate;
22 import java.util.function.Supplier;
23
24 import static io.atlassian.fugue.Option.some;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.hamcrest.Matchers.notNullValue;
28
29 public class OptionVarianceTest {
30
31 class Grand {}
32
33 class Parent extends Grand {}
34
35 class Child extends Parent {}
36
37 @Test public void flatMap() {
38 final Option<Parent> some = some(new Parent());
39 final Function<Grand, Option<Child>> f = p -> some(new Child());
40 final Option<Parent> mapped = some.<Parent> flatMap(f);
41 assertThat(mapped.get(), notNullValue());
42 }
43
44 @Test public void map() {
45 final Option<Parent> some = some(new Parent());
46 final Function<Grand, Child> f = p -> new Child();
47 final Option<Parent> mapped = some.<Parent> map(f);
48 assertThat(mapped.get(), notNullValue());
49 }
50
51 @Test public void orElse() {
52 final Option<Parent> some = some(new Parent());
53 final Supplier<Option<Child>> f = () -> some(new Child());
54 final Option<Parent> mapped = some.orElse(f);
55 assertThat(mapped.get(), notNullValue());
56 }
57
58 @Test public void or() {
59 final Option<Parent> some = some(new Parent());
60 final Option<Child> opt = some(new Child());
61 final Option<Parent> mapped = some.orElse(opt);
62 assertThat(mapped.get(), notNullValue());
63 }
64
65 @Test public void getOrElseSupplier() {
66 final Option<Parent> some = some(new Parent());
67 final Supplier<Child> f = Child::new;
68 final Parent mapped = some.getOr(f);
69 assertThat(mapped, notNullValue());
70 }
71
72 @Test public void getOrElse() {
73 final Option<Parent> some = some(new Parent());
74 final Option<Child> opt = some(new Child());
75 final Option<Parent> mapped = some.orElse(opt);
76 assertThat(mapped.get(), notNullValue());
77 }
78
79 @Test public void forAll() {
80 final Option<Parent> some = some(new Parent());
81 final Predicate<Grand> alwaysTrue = x -> true;
82 assertThat(some.forall(alwaysTrue), equalTo(true));
83 }
84
85 @Test public void exist() {
86 final Option<Parent> some = some(new Parent());
87 final Predicate<Grand> alwaysTrue = x -> true;
88 assertThat(some.exists(alwaysTrue), equalTo(true));
89 }
90
91 @Test public void forEach() {
92 final Maybe<Child> some = some(new Child());
93 Count<Grand> e = new Count<>();
94 some.forEach(e);
95 assertThat(e.count(), equalTo(1));
96 }
97 }