1 package io.atlassian.fugue;
2
3 import org.junit.Test;
4
5 import java.util.function.Function;
6
7 import static org.hamcrest.core.Is.is;
8 import static org.junit.Assert.assertThat;
9
10 public class ApplicativeFunction {
11
12 final Function<Integer, String> f = Object::toString;
13 final Function<Integer, Function<String, Boolean>> g = i -> s -> {
14 if (i == 1) {
15 return s.length() == 1;
16 } else {
17 return s.startsWith("2");
18 }
19 };
20
21 @Test public void functionApplication() {
22 assertThat(Functions.ap(f, g).apply(1), is(true));
23 }
24
25 @Test public void functionApplicationSwitching() {
26 assertThat(Functions.ap(f, g).apply(2), is(true));
27 }
28
29 @Test(expected = NullPointerException.class) public void functionApplicationNullContext() {
30 Functions.ap(f, null).apply(2);
31 }
32
33 @Test(expected = NullPointerException.class) public void functionApplicationNullStart() {
34 Functions.ap(null, g).apply(2);
35 }
36
37 @Test(expected = NullPointerException.class) public void functionApplicationAllNull() {
38 final Function<Integer, String> f = null;
39 final Function<Integer, Function<String, Boolean>> g = null;
40 Functions.ap(f, g).apply(2);
41 }
42 }