1
1
package com .jnape .palatable .lambda .functor .builtin ;
2
2
3
- import com .jnape .palatable .lambda .adt .Unit ;
4
- import com .jnape .palatable .lambda .adt .hlist .Tuple2 ;
5
- import com .jnape .palatable .lambda .functions .Fn1 ;
6
3
import com .jnape .palatable .traitor .annotations .TestTraits ;
7
4
import com .jnape .palatable .traitor .runners .Traits ;
8
5
import org .junit .Test ;
17
14
18
15
import static com .jnape .palatable .lambda .adt .Unit .UNIT ;
19
16
import static com .jnape .palatable .lambda .adt .hlist .HList .tuple ;
17
+ import static com .jnape .palatable .lambda .functions .builtin .fn1 .Id .id ;
20
18
import static com .jnape .palatable .lambda .functions .builtin .fn2 .Into .into ;
21
- import static org .junit .Assert .assertEquals ;
19
+ import static org .junit .Assert .assertThat ;
20
+ import static testsupport .matchers .StateMatcher .whenEvaluated ;
21
+ import static testsupport .matchers .StateMatcher .whenExecuted ;
22
+ import static testsupport .matchers .StateMatcher .whenRun ;
22
23
import static testsupport .traits .Equivalence .equivalence ;
23
24
24
25
@ RunWith (Traits .class )
@@ -36,71 +37,65 @@ public Equivalence<State<String, Integer>> testSubject() {
36
37
37
38
@ Test
38
39
public void eval () {
39
- State <Integer , Unit > state = State .put (0 );
40
- assertEquals (state .run (1 )._1 (), state .eval (1 ));
40
+ assertThat (State .gets (id ()), whenEvaluated (1 , 1 ));
41
41
}
42
42
43
43
@ Test
44
44
public void exec () {
45
- State <Integer , Unit > state = State .put (0 );
46
- assertEquals (state .run (1 )._2 (), state .exec (1 ));
45
+ assertThat (State .modify (x -> x + 1 ), whenExecuted (1 , 2 ));
47
46
}
48
47
49
48
@ Test
50
49
public void get () {
51
- assertEquals ( tuple ( 1 , 1 ), State .< Integer > get (). run ( 1 ));
50
+ assertThat ( State . get (), whenRun ( 1 , 1 , 1 ));
52
51
}
53
52
54
53
@ Test
55
54
public void put () {
56
- assertEquals ( tuple ( UNIT , 1 ), State .put (1 ). run ( 1 ));
55
+ assertThat ( State .put (1 ), whenRun ( 1 , UNIT , 1 ));
57
56
}
58
57
59
58
@ Test
60
59
public void gets () {
61
- assertEquals ( tuple ( 0 , "0" ), State .< String , Integer > gets (Integer ::parseInt ). run ( "0" ));
60
+ assertThat ( State .gets (Integer ::parseInt ), whenRun ( "0" , 0 , "0" ));
62
61
}
63
62
64
63
@ Test
65
64
public void modify () {
66
- assertEquals ( tuple ( UNIT , 1 ), State .< Integer > modify (x -> x + 1 ). run ( 0 ));
65
+ assertThat ( State .modify (x -> x + 1 ), whenRun ( 0 , UNIT , 1 ));
67
66
}
68
67
69
68
@ Test
70
69
public void state () {
71
- assertEquals ( tuple ( 1 , UNIT ), State .< Unit , Integer > state ( 1 ). run ( UNIT ));
72
- assertEquals ( tuple ( 1 , - 1 ), State .< Integer , Integer > state (x -> tuple (x + 1 , x - 1 )). run ( 0 ));
70
+ assertThat ( State . state ( 1 ), whenRun ( UNIT , 1 , UNIT ));
71
+ assertThat ( State .state (x -> tuple (x + 1 , x - 1 )), whenRun ( 0 , 1 , - 1 ));
73
72
}
74
73
75
74
@ Test
76
75
public void stateAccumulation () {
77
- State < Integer , Integer > counter = State .<Integer >get ().flatMap (i -> State .put (i + 1 ).discardL (State .state (i )));
78
- assertEquals ( tuple ( 0 , 1 ), counter . run ( 0 ));
76
+ assertThat ( State .<Integer >get ().flatMap (i -> State .put (i + 1 ).discardL (State .state (i ))),
77
+ whenRun ( 0 , 0 , 1 ));
79
78
}
80
79
81
80
@ Test
82
81
public void zipOrdering () {
83
- Tuple2 <Integer , String > result = State .<String , Integer >state (s -> tuple (0 , s + "1" ))
84
- .zip (State .<String , Fn1 <? super Integer , ? extends Integer >>state (s -> tuple (x -> x + 1 , s + "2" )))
85
- .run ("_" );
86
- assertEquals (tuple (1 , "_12" ), result );
82
+ assertThat (State .<String , Integer >state (s -> tuple (0 , s + "1" ))
83
+ .zip (State .state (s -> tuple (x -> x + 1 , s + "2" ))),
84
+ whenRun ("_" , 1 , "_12" ));
87
85
}
88
86
89
87
@ Test
90
88
public void withState () {
91
- State <Integer , Integer > modified = State .<Integer >get ().withState (x -> x + 1 );
92
- assertEquals (tuple (1 , 1 ), modified .run (0 ));
89
+ assertThat (State .<Integer >get ().withState (x -> x + 1 ), whenRun (0 , 1 , 1 ));
93
90
}
94
91
95
92
@ Test
96
93
public void mapState () {
97
- State <Integer , Integer > modified = State .<Integer >get ().mapState (into ((a , s ) -> tuple (a + 1 , s + 2 )));
98
- assertEquals (tuple (1 , 2 ), modified .run (0 ));
94
+ assertThat (State .<Integer >get ().mapState (into ((a , s ) -> tuple (a + 1 , s + 2 ))), whenRun (0 , 1 , 2 ));
99
95
}
100
96
101
97
@ Test
102
98
public void staticPure () {
103
- State <String , Integer > state = State .<String >pureState ().apply (1 );
104
- assertEquals (tuple (1 , "foo" ), state .run ("foo" ));
99
+ assertThat (State .<String >pureState ().apply (1 ), whenRun ("foo" , 1 , "foo" ));
105
100
}
106
101
}
0 commit comments