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.Arrays;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import static io.atlassian.fugue.Eithers.filterRight;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertFalse;
28  
29  public class FilterRightTest {
30    @Test public void leftOnlyFiltersRightToEmpty() {
31      final List<Either<Integer, String>> it = Arrays.asList(Either.<Integer, String> left(1), Either.<Integer, String> left(333),
32        Either.<Integer, String> left(22));
33  
34      assertFalse(filterRight(it).iterator().hasNext());
35    }
36  
37    @Test public void rightOnlyFiltersRightToSameContents() {
38      final List<Either<Integer, String>> it = Arrays.asList(Either.<Integer, String> right("one"), Either.<Integer, String> right("three"),
39        Either.<Integer, String> right("2"));
40  
41      final Iterator<String> rights = filterRight(it).iterator();
42      for (final Either<Integer, String> i : it) {
43        // calling get on a Maybe is ill advised, but this is a test - if get
44        // returns null it's a sign that something
45        // bigger has gone wrong
46        assertEquals(i.right().get(), rights.next());
47      }
48    }
49  
50    @Test public void mixedEithersFiltersRightToExpectedContents() {
51      final List<Either<Integer, String>> it = Arrays.asList(Either.<Integer, String> left(1), Either.<Integer, String> right("three"),
52        Either.<Integer, String> right("fore"), Either.<Integer, String> left(22));
53  
54      final Iterator<String> iterator = filterRight(it).iterator();
55      assertEquals("three", iterator.next());
56      assertEquals("fore", iterator.next());
57      assertFalse(iterator.hasNext());
58    }
59  
60    @Test public void emptyIterableFiltersRightToEmptyIterable() {
61      assertFalse(filterRight(Collections.<Either<Integer, String>> emptyList()).iterator().hasNext());
62    }
63  }