|
2 | 2 |
|
3 | 3 | import org.junit.Test; |
4 | 4 |
|
| 5 | +import static fj.P.p; |
| 6 | +import static org.junit.Assert.assertEquals; |
| 7 | + |
5 | 8 | /** |
6 | 9 | * Created by MarkPerry on 18/12/2014. |
7 | 10 | */ |
8 | 11 | public class StateTest { |
9 | 12 |
|
10 | | - @Test |
11 | | - public void map() { |
| 13 | + @Test |
| 14 | + public void testBind() { |
| 15 | + assertEquals(p(2, "one"), state().run(1)); |
| 16 | + assertEquals(p(3, "two"), state().run(2)); |
| 17 | + assertEquals(p(4, "three"), state().run(3)); |
| 18 | + assertEquals(p(2, "?"), state().bind(state -> State.constant("?")).run(1)); |
| 19 | + assertEquals(p(3, "?"), state().bind(state -> State.constant("?")).run(2)); |
| 20 | + assertEquals(p(4, "?"), state().bind(state -> State.constant("?")).run(3)); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testFlatMap() { |
| 25 | + assertEquals(p(2, "one"), state().run(1)); |
| 26 | + assertEquals(p(3, "two"), state().run(2)); |
| 27 | + assertEquals(p(4, "three"), state().run(3)); |
| 28 | + assertEquals(p(2, "?"), state().flatMap(state -> State.constant("?")).run(1)); |
| 29 | + assertEquals(p(3, "?"), state().flatMap(state -> State.constant("?")).run(2)); |
| 30 | + assertEquals(p(4, "?"), state().flatMap(state -> State.constant("?")).run(3)); |
| 31 | + } |
12 | 32 |
|
13 | | - } |
| 33 | + private static final State<Integer, String> state() { |
| 34 | + return State.<Integer, String>unit(i -> p(i + 1, toLapine(i))); |
| 35 | + } |
14 | 36 |
|
| 37 | + private static String toLapine( |
| 38 | + final int i) { |
| 39 | + return i == 1 ? |
| 40 | + "one" : |
| 41 | + i == 2 ? |
| 42 | + "two" : |
| 43 | + i == 3 ? |
| 44 | + "three" : |
| 45 | + i == 4 ? |
| 46 | + "four" : "hrair"; |
| 47 | + } |
15 | 48 | } |
0 commit comments