PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/fugue/ToEitherLeftOrRightTest.java

https://bitbucket.org/atlassian/fugue
Java | 89 lines | 55 code | 17 blank | 17 comment | 1 complexity | e63c6bbe1e9155b6e6dad7c92260208a MD5 | raw file
  1. /*
  2. Copyright 2011 Atlassian
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.atlassian.fugue;
  14. import org.junit.Test;
  15. import static com.atlassian.fugue.Eithers.toLeft;
  16. import static com.atlassian.fugue.Eithers.toRight;
  17. import static org.hamcrest.Matchers.is;
  18. import static org.junit.Assert.assertThat;
  19. public class ToEitherLeftOrRightTest {
  20. private static final String ORIGINAL_STRING = "abc";
  21. private static final int ORIGINAL_INT = 1;
  22. @Test public void testToLeftFunction() {
  23. assertThat(Eithers.<String, Integer> toLeft().apply(ORIGINAL_STRING).left().get(), is(ORIGINAL_STRING));
  24. }
  25. @Test public void testToLeftFunctionWithTypes() {
  26. assertThat(toLeft(String.class, Integer.class).apply(ORIGINAL_STRING).left().get(), is(ORIGINAL_STRING));
  27. }
  28. @Test public void testToRightFunction() {
  29. assertThat(Eithers.<String, Integer> toRight().apply(ORIGINAL_INT).right().get(), is(ORIGINAL_INT));
  30. }
  31. @Test public void testToRightFunctionWithTypes() {
  32. assertThat(toRight(String.class, Integer.class).apply(ORIGINAL_INT).right().get(), is(ORIGINAL_INT));
  33. }
  34. @Test public void testToLeftSupplier() {
  35. assertThat(Eithers.<String, Integer> toLeft(ORIGINAL_STRING).get().left().get(), is(ORIGINAL_STRING));
  36. }
  37. @Test public void testToLeftSupplierWithType() {
  38. assertThat(toLeft(ORIGINAL_STRING, Integer.class).get().left().get(), is(ORIGINAL_STRING));
  39. }
  40. @Test public void testToRightSupplier() {
  41. assertThat(Eithers.<String, Integer> toRight(ORIGINAL_INT).get().right().get(), is(ORIGINAL_INT));
  42. }
  43. @Test public void testToRightSupplierWithType() {
  44. assertThat(toRight(String.class, ORIGINAL_INT).get().right().get(), is(ORIGINAL_INT));
  45. }
  46. // The following tests are more to demonstrate why these toLeft/toRight
  47. // Function and Supplier can be useful.
  48. @Test public void toRightFunctionUsedInFold() {
  49. Either<String, Integer> either = divideByTwo(ORIGINAL_INT * 2).fold(toLeft(ORIGINAL_STRING, Integer.class), toRight(String.class, Integer.class));
  50. assertThat(either.right().get(), is(ORIGINAL_INT));
  51. }
  52. @Test public void toLeftSupplierUsedInFold() {
  53. Either<String, Integer> either = divideByTwo(ORIGINAL_INT).fold(toLeft(ORIGINAL_STRING, Integer.class), toRight(String.class, Integer.class));
  54. assertThat(either.left().get(), is(ORIGINAL_STRING));
  55. }
  56. @Test public void toLeftFunctionUsedInFold() {
  57. Either<String, Integer> either = divideByTwo(ORIGINAL_INT * 2).fold(toRight(Integer.class, ORIGINAL_STRING), toLeft(Integer.class, String.class))
  58. .swap();
  59. assertThat(either.right().get(), is(ORIGINAL_INT));
  60. }
  61. @Test public void toRightSupplierUsedInFold() {
  62. Either<String, Integer> either = divideByTwo(ORIGINAL_INT).fold(toRight(Integer.class, ORIGINAL_STRING), toLeft(Integer.class, String.class))
  63. .swap();
  64. assertThat(either.left().get(), is(ORIGINAL_STRING));
  65. }
  66. private Option<Integer> divideByTwo(final Integer i) {
  67. return i % 2 == 0 ? Option.some(i / 2) : Option.<Integer> none();
  68. }
  69. }