11package com .jnape .palatable .lambda .monad .transformer .builtin ;
22
3- import com .jnape .palatable .lambda .adt .Maybe ;
43import com .jnape .palatable .lambda .adt .Unit ;
54import com .jnape .palatable .lambda .adt .hlist .Tuple2 ;
65import com .jnape .palatable .lambda .functor .builtin .Identity ;
76import com .jnape .palatable .traitor .annotations .TestTraits ;
87import com .jnape .palatable .traitor .runners .Traits ;
98import org .junit .Test ;
109import 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 .*;
1811
1912import java .util .ArrayList ;
2013import java .util .List ;
2619import static com .jnape .palatable .lambda .optics .functions .Set .set ;
2720import static com .jnape .palatable .lambda .optics .lenses .ListLens .elementAt ;
2821import 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 .*;
3024import static testsupport .traits .Equivalence .equivalence ;
3125
3226@ RunWith (Traits .class )
3327public class StateTTest {
3428
3529 @ 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 })
4135 public Equivalence <StateT <String , Identity <?>, Integer >> testReader () {
4236 return equivalence (StateT .gets (s -> new Identity <>(s .length ())), s -> s .runStateT ("foo" ));
4337 }
@@ -47,85 +41,80 @@ public void evalAndExec() {
4741 StateT <String , Identity <?>, Integer > stateT =
4842 StateT .stateT (str -> new Identity <>(tuple (str .length (), str + "_" )));
4943
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 )));
5246 }
5347
5448 @ Test
5549 public void mapStateT () {
5650 StateT <String , Identity <?>, Integer > stateT =
5751 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_" )) ));
6357 }
6458
6559 @ Test
6660 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 (
6862 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 ))));
7264
73- assertEquals ( tuple ( UNIT , asList ( "one" , "two" )) ,
74- result );
65+ assertThat ( result ,
66+ whenRun ( new ArrayList <>(), new Identity <>( tuple ( UNIT , asList ( "one" , "two" )))) );
7567 }
7668
7769 @ Test
7870 public void withStateT () {
7971 StateT <String , Identity <?>, Integer > stateT =
8072 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_" )) ));
8375 }
8476
8577 @ Test
8678 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" )) ));
8981 }
9082
9183 @ Test
9284 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" )) ));
9587 }
9688
9789 @ Test
9890 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 ))));
10093 }
10194
10295 @ Test
10396 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 ))));
10699 }
107100
108101 @ Test
109102 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" ))));
115107 }
116108
117109 @ Test
118110 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" ))));
123113 }
124114
125115 @ Test
126116 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" ))));
130119 }
131120}
0 commit comments