1
1
package com .jnape .palatable .lambda .monad .transformer .builtin ;
2
2
3
- import com .jnape .palatable .lambda .adt .Maybe ;
4
3
import com .jnape .palatable .lambda .adt .Unit ;
5
4
import com .jnape .palatable .lambda .adt .hlist .Tuple2 ;
6
5
import com .jnape .palatable .lambda .functor .builtin .Identity ;
7
6
import com .jnape .palatable .traitor .annotations .TestTraits ;
8
7
import com .jnape .palatable .traitor .runners .Traits ;
9
8
import org .junit .Test ;
10
9
import org .junit .runner .RunWith ;
11
- import testsupport .traits .ApplicativeLaws ;
12
- import testsupport .traits .Equivalence ;
13
- import testsupport .traits .FunctorLaws ;
14
- import testsupport .traits .MonadLaws ;
15
- import testsupport .traits .MonadReaderLaws ;
16
- import testsupport .traits .MonadRecLaws ;
17
- import testsupport .traits .MonadWriterLaws ;
10
+ import testsupport .traits .*;
18
11
19
12
import java .util .ArrayList ;
20
13
import java .util .List ;
26
19
import static com .jnape .palatable .lambda .optics .functions .Set .set ;
27
20
import static com .jnape .palatable .lambda .optics .lenses .ListLens .elementAt ;
28
21
import static java .util .Arrays .asList ;
29
- import static org .junit .Assert .assertEquals ;
22
+ import static org .hamcrest .MatcherAssert .assertThat ;
23
+ import static testsupport .matchers .StateTMatcher .*;
30
24
import static testsupport .traits .Equivalence .equivalence ;
31
25
32
26
@ RunWith (Traits .class )
33
27
public class StateTTest {
34
28
35
29
@ TestTraits ({FunctorLaws .class ,
36
- ApplicativeLaws .class ,
37
- MonadLaws .class ,
38
- MonadRecLaws .class ,
39
- MonadReaderLaws .class ,
40
- MonadWriterLaws .class })
30
+ ApplicativeLaws .class ,
31
+ MonadLaws .class ,
32
+ MonadRecLaws .class ,
33
+ MonadReaderLaws .class ,
34
+ MonadWriterLaws .class })
41
35
public Equivalence <StateT <String , Identity <?>, Integer >> testReader () {
42
36
return equivalence (StateT .gets (s -> new Identity <>(s .length ())), s -> s .runStateT ("foo" ));
43
37
}
@@ -47,85 +41,80 @@ public void evalAndExec() {
47
41
StateT <String , Identity <?>, Integer > stateT =
48
42
StateT .stateT (str -> new Identity <>(tuple (str .length (), str + "_" )));
49
43
50
- assertEquals ( new Identity <>("__" ), stateT . execT ( "_" ));
51
- assertEquals ( new Identity <>(1 ), stateT . evalT ( "_" ));
44
+ assertThat ( stateT , whenExecuted ( "_" , new Identity <>("__" )));
45
+ assertThat ( stateT , whenEvaluated ( "_" , new Identity <>(1 )));
52
46
}
53
47
54
48
@ Test
55
49
public void mapStateT () {
56
50
StateT <String , Identity <?>, Integer > stateT =
57
51
StateT .stateT (str -> new Identity <>(tuple (str .length (), str + "_" )));
58
- assertEquals ( just ( tuple ( 4 , "ABC_" )),
59
- stateT .mapStateT (id -> id .<Identity <Tuple2 <Integer , String >>>coerce ()
60
- .runIdentity ()
61
- .into ((x , str ) -> just (tuple (x + 1 , str .toUpperCase ()))))
62
- .< Maybe < Tuple2 < Integer , String >>> runStateT ( "abc" ));
52
+
53
+ assertThat ( stateT .mapStateT (id -> id .<Identity <Tuple2 <Integer , String >>>coerce ()
54
+ .runIdentity ()
55
+ .into ((x , str ) -> just (tuple (x + 1 , str .toUpperCase ())))),
56
+ whenRun ( "abc" , just ( tuple ( 4 , "ABC_" )) ));
63
57
}
64
58
65
59
@ Test
66
60
public void zipping () {
67
- Tuple2 < Unit , List <String >> result = StateT .<List <String >, Identity <?>>modify (
61
+ StateT < List <String >, Identity <?>, Unit > result = StateT .<List <String >, Identity <?>>modify (
68
62
s -> new Identity <>(set (elementAt (s .size ()), just ("one" ), s )))
69
- .discardL (StateT .modify (s -> new Identity <>(set (elementAt (s .size ()), just ("two" ), s ))))
70
- .<Identity <Tuple2 <Unit , List <String >>>>runStateT (new ArrayList <>())
71
- .runIdentity ();
63
+ .discardL (StateT .modify (s -> new Identity <>(set (elementAt (s .size ()), just ("two" ), s ))));
72
64
73
- assertEquals ( tuple ( UNIT , asList ( "one" , "two" )) ,
74
- result );
65
+ assertThat ( result ,
66
+ whenRun ( new ArrayList <>(), new Identity <>( tuple ( UNIT , asList ( "one" , "two" )))) );
75
67
}
76
68
77
69
@ Test
78
70
public void withStateT () {
79
71
StateT <String , Identity <?>, Integer > stateT =
80
72
StateT .stateT (str -> new Identity <>(tuple (str .length (), str + "_" )));
81
- assertEquals ( new Identity <>(tuple ( 3 , "ABC_" )),
82
- stateT . withStateT ( str -> new Identity <>(str . toUpperCase ())). runStateT ( "abc" ));
73
+ assertThat ( stateT . withStateT ( str -> new Identity <>(str . toUpperCase () )),
74
+ whenRun ( "abc" , new Identity <>(tuple ( 3 , "ABC_" )) ));
83
75
}
84
76
85
77
@ Test
86
78
public void get () {
87
- assertEquals ( new Identity <>( tuple ( "state" , "state" )),
88
- StateT .< String , Identity <?>> get ( pureIdentity ()). runStateT ( "state" ));
79
+ assertThat ( StateT . get ( pureIdentity ( )),
80
+ whenRun ( "state" , new Identity <>( tuple ( "state" , "state" )) ));
89
81
}
90
82
91
83
@ Test
92
84
public void gets () {
93
- assertEquals ( new Identity <>(tuple ( 5 , "state" )),
94
- StateT .< String , Identity <?>, Integer > gets ( s -> new Identity <>(s . length ())). runStateT ( "state" ));
85
+ assertThat ( StateT . gets ( s -> new Identity <>(s . length () )),
86
+ whenRun ( "state" , new Identity <>(tuple ( 5 , "state" )) ));
95
87
}
96
88
97
89
@ Test
98
90
public void put () {
99
- assertEquals (new Identity <>(tuple (UNIT , 1 )), StateT .put (new Identity <>(1 )).runStateT (0 ));
91
+ assertThat (StateT .put (new Identity <>(1 )),
92
+ whenRun (0 , new Identity <>(tuple (UNIT , 1 ))));
100
93
}
101
94
102
95
@ Test
103
96
public void modify () {
104
- assertEquals ( new Identity <>(tuple ( UNIT , 1 )),
105
- StateT .< Integer , Identity <?>> modify ( x -> new Identity <>(x + 1 )). runStateT ( 0 ));
97
+ assertThat ( StateT . modify ( x -> new Identity <>(x + 1 )),
98
+ whenRun ( 0 , new Identity <>(tuple ( UNIT , 1 ))));
106
99
}
107
100
108
101
@ Test
109
102
public void stateT () {
110
- assertEquals (new Identity <>(tuple (0 , "_" )),
111
- StateT .<String , Identity <?>, Integer >stateT (new Identity <>(0 )).runStateT ("_" ));
112
- assertEquals (new Identity <>(tuple (1 , "_1" )),
113
- StateT .<String , Identity <?>, Integer >stateT (s -> new Identity <>(tuple (s .length (), s + "1" )))
114
- .runStateT ("_" ));
103
+ assertThat (StateT .stateT (new Identity <>(0 )),
104
+ whenRun ("_" , new Identity <>(tuple (0 , "_" ))));
105
+ assertThat (StateT .stateT (s -> new Identity <>(tuple (s .length (), s + "1" ))),
106
+ whenRun ("_" , new Identity <>(tuple (1 , "_1" ))));
115
107
}
116
108
117
109
@ Test
118
110
public void staticPure () {
119
- assertEquals (new Identity <>(tuple (1 , "foo" )),
120
- StateT .<String , Identity <?>>pureStateT (pureIdentity ())
121
- .<Integer , StateT <String , Identity <?>, Integer >>apply (1 )
122
- .<Identity <Tuple2 <Integer , String >>>runStateT ("foo" ));
111
+ assertThat (StateT .<String , Identity <?>>pureStateT (pureIdentity ()).apply (1 ),
112
+ whenRun ("foo" , new Identity <>(tuple (1 , "foo" ))));
123
113
}
124
114
125
115
@ Test
126
116
public void staticLift () {
127
- assertEquals (new Identity <>(tuple (1 , "foo" )),
128
- StateT .<String >liftStateT ().<Integer , Identity <?>, StateT <String , Identity <?>, Integer >>apply (new Identity <>(1 ))
129
- .<Identity <Tuple2 <Integer , String >>>runStateT ("foo" ));
117
+ assertThat (StateT .<String >liftStateT ().apply (new Identity <>(1 )),
118
+ whenRun ("foo" , new Identity <>(tuple (1 , "foo" ))));
130
119
}
131
120
}
0 commit comments