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.junit.Test;
19  
20  import java.util.ArrayList;
21  import java.util.NoSuchElementException;
22  import java.util.Set;
23  import java.util.function.Function;
24  
25  import static io.atlassian.fugue.Option.none;
26  import static io.atlassian.fugue.Option.some;
27  import static org.hamcrest.Matchers.is;
28  import static org.hamcrest.Matchers.sameInstance;
29  import static org.junit.Assert.assertThat;
30  
31  public class OptionNoneTest {
32    private final Option<Integer> none = none();
33  
34    @Test(expected = NoSuchElementException.class) public void get() {
35      none.get();
36    }
37  
38    @Test public void isSet() {
39      assertThat(none.isDefined(), is(false));
40    }
41  
42    @Test public void getOrElse() {
43      assertThat(none.getOrElse(1), is(1));
44    }
45  
46    @Test public void getOrNull() {
47      assertThat(none.getOrNull(), is((Integer) null));
48    }
49  
50    @Test public void map() {
51      final Function<Integer, Integer> function = input -> {
52        throw new AssertionError("None.map should not call the function.");
53      };
54  
55      assertThat(none.map(function).isEmpty(), is(true));
56    }
57  
58    @Test(expected = NullPointerException.class) public void nullFunctionForMap() {
59      none.map(null);
60    }
61  
62    @Test(expected = NullPointerException.class) public void nullPredicateForFilter() {
63      none.filter(null);
64    }
65  
66    @Test public void filterTrueReturnsEmpty() {
67      assertThat(none.filter(x -> true).isEmpty(), is(true));
68    }
69  
70    @Test public void filterFalseReturnsEmpty() {
71      assertThat(none.filter(x -> false).isEmpty(), is(true));
72    }
73  
74    @Test public void existsTrueReturnsFalse() {
75      assertThat(none.exists(x -> true), is(false));
76    }
77  
78    @Test public void existsFalseReturnsFalse() {
79      assertThat(none.exists(x -> false), is(false));
80    }
81  
82    @Test public void toLeftReturnsRight() {
83      assertThat(none.toLeft(Suppliers.ofInstance("")).isRight(), is(true));
84    }
85  
86    @Test public void toRightReturnsLeft() {
87      assertThat(none.toRight(Suppliers.ofInstance("")).isLeft(), is(true));
88    }
89  
90    @Test public void superTypesPermittedOnFilter() {
91      final Option<ArrayList<?>> opt = none();
92      final Option<ArrayList<?>> nopt = opt.filter(x -> true);
93      assertThat(nopt, sameInstance(opt));
94    }
95  
96    @Test public void superTypesPermittedOnMap() {
97      final Option<ArrayList<?>> opt = none();
98      final Option<Set<?>> size = opt.map(list -> {
99        throw new AssertionError("This internal method should never get called.");
100     });
101     assertThat(size.isDefined(), is(false));
102   }
103 
104   @Test public void hashDoesNotThrowException() {
105     none.hashCode();
106   }
107 
108   // These tests are duplicated in TestEmptyIterator, but I've included them
109   // here to ensure
110   // that None itself complies with the API.
111   @Test public void iteratorHasNoNext() {
112     assertThat(none.iterator().hasNext(), is(false));
113   }
114 
115   @Test(expected = NoSuchElementException.class) public void iteratorNext() {
116     none.iterator().next();
117   }
118 
119   @Test(expected = UnsupportedOperationException.class) public void iteratorImmutable() {
120     none.iterator().remove();
121   }
122 
123   @Test public void foreach() {
124     assertThat(Count.countEach(none), is(0));
125   }
126 
127   @Test public void forallTrue() {
128     assertThat(none.forall(x -> true), is(true));
129   }
130 
131   @Test public void forallFalse() {
132     assertThat(none.forall(x -> false), is(true));
133   }
134 
135   @Test public void toStringTest() {
136     assertThat(none.toString(), is("none()"));
137   }
138 
139   @Test public void equalsItself() {
140     assertThat(none.equals(none), is(true));
141   }
142 
143   @Test public void notEqualsSome() {
144     assertThat(none.equals(some(5)), is(false));
145   }
146 
147   @Test public void notEqualsNull() {
148     assertThat(none.equals(null), is(false));
149   }
150 
151   static class MyException extends Exception {
152     private static final long serialVersionUID = -1056362494708225175L;
153   }
154 
155   @Test(expected = MyException.class) public void getOrThrow() throws MyException {
156     none.getOrThrow(MyException::new);
157   }
158 }