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.Either.left;
21  import static io.atlassian.fugue.Either.right;
22  import static io.atlassian.fugue.Eithers.cond;
23  import static io.atlassian.fugue.Eithers.merge;
24  import static io.atlassian.fugue.UtilityFunctions.addOne;
25  import static io.atlassian.fugue.UtilityFunctions.square;
26  import static org.hamcrest.Matchers.is;
27  import static org.junit.Assert.assertThat;
28  
29  public class EitherTest {
30    @Test(expected = NullPointerException.class) public void testNullLeft() {
31      Either.left(null);
32    }
33  
34    @Test public void leftCreation() {
35      final Either<Boolean, Integer> left = left(true);
36      assertThat(left.isLeft(), is(true));
37      assertThat(left.left().get(), is(true));
38    }
39  
40    @Test(expected = NullPointerException.class) public void testNullRight() {
41      right(null);
42    }
43  
44    @Test public void rightCreation() {
45      final Either<Boolean, Integer> right = right(1);
46      assertThat(right.isRight(), is(true));
47      assertThat(right.right().get(), is(1));
48    }
49  
50    @Test public void leftMerge() {
51      assertThat(merge(Either.<String, String> left("Ponies.")), is("Ponies."));
52    }
53  
54    @Test public void rightMerge() {
55      assertThat(merge(Either.<String, String> right("Unicorns.")), is("Unicorns."));
56    }
57  
58    @Test public void condTrue() {
59      assertThat(cond(true, 7, "Pegasus."), is(Either.<Integer, String> right("Pegasus.")));
60    }
61  
62    @Test public void condFalse() {
63      assertThat(cond(false, 7, "Pegasus."), is(Either.<Integer, String> left(7)));
64    }
65  
66    @Test public void bimapRight() {
67      assertThat(Either.<Integer, Integer> right(3).bimap(square, addOne), is(Either.<Integer, Integer> right(4)));
68    }
69  
70    @Test public void bimapLeft() {
71      assertThat(Either.<Integer, Integer> left(7).bimap(square, addOne), is(Either.<Integer, Integer> left(49)));
72    }
73  
74    @Test public void hashCodeMirrorItegerMin() {
75      assertThat(~(left(Integer.MIN_VALUE).hashCode()), is(right(Integer.MIN_VALUE).hashCode()));
76    }
77  
78    @Test public void hashCodeMirrorItegerMax() {
79      assertThat(left(Integer.MAX_VALUE).hashCode(), is(~(right(Integer.MAX_VALUE).hashCode())));
80    }
81  
82    @Test public void hashCodeMirrorItegerZero() {
83      assertThat(left(0).hashCode(), is(~(right(0).hashCode())));
84    }
85  }