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.function.Predicate;
21  
22  import static io.atlassian.fugue.Either.left;
23  import static io.atlassian.fugue.Either.right;
24  import static org.hamcrest.MatcherAssert.assertThat;
25  import static org.hamcrest.Matchers.equalTo;
26  import static org.hamcrest.Matchers.is;
27  
28  public class EitherVarianceTest {
29    private final Either<String, Integer> l = left("heyaa!");
30    private final Either<String, Integer> r = right(12);
31  
32    @Test public void filterLeft() {
33      final Option<Either<String, Object>> filtered = l.left().filter(x -> true);
34      assertThat(filtered.isDefined(), is(true));
35      assertThat(filtered.get().left().isDefined(), is(true));
36    }
37  
38    @Test public void filterRight() {
39      final Option<Either<Object, Integer>> filtered = r.right().filter(x -> true);
40      assertThat(filtered.isDefined(), is(true));
41      assertThat(filtered.get().right().isDefined(), is(true));
42    }
43  
44    @Test public void forAll() {
45      final Predicate<Number> alwaysTrue = x -> true;
46      assertThat(r.right().forall(alwaysTrue), equalTo(true));
47    }
48  
49    @Test public void exist() {
50      final Predicate<CharSequence> alwaysTrue = x -> true;
51      assertThat(l.left().exists(alwaysTrue), equalTo(true));
52    }
53  
54    @Test public void forEach() {
55      final Count<Number> e = new Count<>();
56      r.right().forEach(e);
57      assertThat(e.count(), equalTo(1));
58    }
59  }