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 static io.atlassian.fugue.Eithers.toLeft;
21  import static io.atlassian.fugue.Eithers.toRight;
22  import static org.hamcrest.Matchers.is;
23  import static org.junit.Assert.assertThat;
24  
25  public class ToEitherLeftOrRightTest {
26    private static final String ORIGINAL_STRING = "abc";
27    private static final int ORIGINAL_INT = 1;
28  
29    @Test public void testToLeftFunction() {
30      assertThat(Eithers.<String, Integer> toLeft().apply(ORIGINAL_STRING).left().get(), is(ORIGINAL_STRING));
31    }
32  
33    @Test public void testToLeftFunctionWithTypes() {
34      assertThat(toLeft(String.class, Integer.class).apply(ORIGINAL_STRING).left().get(), is(ORIGINAL_STRING));
35    }
36  
37    @Test public void testToRightFunction() {
38      assertThat(Eithers.<String, Integer> toRight().apply(ORIGINAL_INT).right().get(), is(ORIGINAL_INT));
39    }
40  
41    @Test public void testToRightFunctionWithTypes() {
42      assertThat(toRight(String.class, Integer.class).apply(ORIGINAL_INT).right().get(), is(ORIGINAL_INT));
43    }
44  
45    @Test public void testToLeftSupplier() {
46      assertThat(Eithers.<String, Integer> toLeft(ORIGINAL_STRING).get().left().get(), is(ORIGINAL_STRING));
47    }
48  
49    @Test public void testToLeftSupplierWithType() {
50      assertThat(toLeft(ORIGINAL_STRING, Integer.class).get().left().get(), is(ORIGINAL_STRING));
51    }
52  
53    @Test public void testToRightSupplier() {
54      assertThat(Eithers.<String, Integer> toRight(ORIGINAL_INT).get().right().get(), is(ORIGINAL_INT));
55    }
56  
57    @Test public void testToRightSupplierWithType() {
58      assertThat(toRight(String.class, ORIGINAL_INT).get().right().get(), is(ORIGINAL_INT));
59    }
60  
61    // The following tests are more to demonstrate why these toLeft/toRight
62    // Function and Supplier can be useful.
63  
64    @Test public void toRightFunctionUsedInFold() {
65      final Either<String, Integer> either = divideByTwo(ORIGINAL_INT * 2).fold(toLeft(ORIGINAL_STRING, Integer.class),
66        toRight(String.class, Integer.class));
67      assertThat(either.right().get(), is(ORIGINAL_INT));
68    }
69  
70    @Test public void toLeftSupplierUsedInFold() {
71      final Either<String, Integer> either = divideByTwo(ORIGINAL_INT).fold(toLeft(ORIGINAL_STRING, Integer.class),
72        toRight(String.class, Integer.class));
73      assertThat(either.left().get(), is(ORIGINAL_STRING));
74    }
75  
76    @Test public void toLeftFunctionUsedInFold() {
77      final Either<String, Integer> either = divideByTwo(ORIGINAL_INT * 2).fold(toRight(Integer.class, ORIGINAL_STRING),
78        toLeft(Integer.class, String.class)).swap();
79      assertThat(either.right().get(), is(ORIGINAL_INT));
80    }
81  
82    @Test public void toRightSupplierUsedInFold() {
83      final Either<String, Integer> either = divideByTwo(ORIGINAL_INT).fold(toRight(Integer.class, ORIGINAL_STRING),
84        toLeft(Integer.class, String.class)).swap();
85      assertThat(either.left().get(), is(ORIGINAL_STRING));
86    }
87  
88    private Option<Integer> divideByTwo(final Integer i) {
89      return i % 2 == 0 ? Option.some(i / 2) : Option.<Integer> none();
90    }
91  }