1414import static com .jnape .palatable .lambda .io .IO .io ;
1515import static org .hamcrest .Matchers .equalTo ;
1616
17- public class StateTMatcher <S , M extends MonadRec <?, M >, A , MA extends MonadRec < A , M >, MS extends MonadRec < S , M >, MTS extends MonadRec < Tuple2 < A , S >, M > > extends TypeSafeMatcher <StateT <S , M , A >> {
17+ public class StateTMatcher <S , M extends MonadRec <?, M >, A > extends TypeSafeMatcher <StateT <S , M , A >> {
1818 private final S initialState ;
1919
20- private final Either <Matcher <? super MTS >, These <Matcher <? super MA > , Matcher <? super MS >>> matcher ;
20+ private final Either <Matcher <? super MonadRec < Tuple2 < A , S >, M >>, These <Matcher <? super MonadRec < A , M >> , Matcher <? super MonadRec < S , M > >>> matcher ;
2121
22- private StateTMatcher (S initialState , These <Matcher <? super MA > , Matcher <? super MS >> matchers ) {
22+ private StateTMatcher (S initialState , These <Matcher <? super MonadRec < A , M >> , Matcher <? super MonadRec < S , M > >> matchers ) {
2323 this .initialState = initialState ;
2424 this .matcher = right (matchers );
2525 }
2626
27- private StateTMatcher (S initialState , Matcher <? super MTS > matcher ) {
27+ private StateTMatcher (S initialState , Matcher <? super MonadRec < Tuple2 < A , S >, M > > matcher ) {
2828 this .initialState = initialState ;
2929 this .matcher = left (matcher );
3030 }
@@ -44,9 +44,9 @@ public void describeTo(Description description) {
4444 theseMatchers -> theseMatchers .match (a -> io (() -> a .describeTo (description .appendText ("Value matching " ))),
4545 b -> io (() -> b .describeTo (description .appendText ("State matching " ))),
4646 ab -> io (() -> {
47- description .appendText ("Value matching: " );
47+ description .appendText ("Value run matching: " );
4848 ab ._1 ().describeTo (description );
49- description .appendText (" and state matching: " );
49+ description .appendText (", then state run matching: " );
5050 ab ._2 ().describeTo (description );
5151 })))
5252 .unsafePerformIO ();
@@ -69,46 +69,60 @@ protected void describeMismatchSafely(StateT<S, M, A> item, Description mismatch
6969 b .describeMismatch (ran .fmap (Tuple2 ::_2 ), mismatchDescription );
7070 }),
7171 ab -> io (() -> {
72- mismatchDescription .appendText ("value matching: " );
72+ mismatchDescription .appendText ("value run matching: " );
7373 ab ._1 ().describeMismatch (ran .fmap (Tuple2 ::_1 ), mismatchDescription );
74- mismatchDescription .appendText (" and state matching: " );
74+ mismatchDescription .appendText (", then state run matching: " );
7575 ab ._2 ().describeMismatch (ran .fmap (Tuple2 ::_2 ), mismatchDescription );
7676 })))
7777 .unsafePerformIO ();
7878 }
7979
80- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec <A , M >, MS extends MonadRec < S , M >, MTS extends MonadRec < Tuple2 <A , S >, M >> StateTMatcher <S , M , A , MA , MS , MTS > whenRunWith (S initialState , Matcher <? super MTS > bothMatcher ) {
81- return new StateTMatcher <>(initialState , bothMatcher );
80+ public static <S , M extends MonadRec <?, M >, A , MAS extends MonadRec <Tuple2 <A , S >, M >> StateTMatcher <S , M , A > whenRunWith (S initialState , Matcher <? super MAS > bothMatcher ) {
81+ return new StateTMatcher <S , M , A >(initialState , extendMatcher ( bothMatcher ) );
8282 }
8383
84- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec < A , M >, MS extends MonadRec < S , M >, MTS extends MonadRec <Tuple2 <A , S >, M >> StateTMatcher < S , M , A , MA , MS , MTS > whenRun ( S initialState , MTS both ) {
84+ public static <S , M extends MonadRec <?, M >, A > StateTMatcher < S , M , A > whenRun ( S initialState , MonadRec <Tuple2 <A , S >, M > both ) {
8585 return whenRunWith (initialState , equalTo (both ));
8686 }
8787
88- // Note: This constructor will run both matchers, which can run effects twice
89- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec <A , M >, MS extends MonadRec <S , M >, MTS extends MonadRec < Tuple2 < A , S >, M >> StateTMatcher <S , M , A , MA , MS , MTS > whenRunWith (S initialState , Matcher <? super MA > valueMatcher , Matcher <? super MS > stateMatcher ) {
90- return new StateTMatcher <>(initialState , These .both (valueMatcher , stateMatcher ));
88+ // Note: This constructor will run both matchers, which will run effects twice
89+ public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec <A , M >, MS extends MonadRec <S , M >> StateTMatcher <S , M , A > whenRunWithBoth (S initialState , Matcher <? super MA > valueMatcher , Matcher <? super MS > stateMatcher ) {
90+ return new StateTMatcher <S , M , A >(initialState , These .both (extendMatcher ( valueMatcher ), extendMatcher ( stateMatcher ) ));
9191 }
9292
93- // Note: This constructor will run both matchers, which can run effects twice
93+ // Note: This constructor will run both matchers, which will run effects twice
9494 @ SuppressWarnings ("unused" )
95- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec <A , M >, MS extends MonadRec <S , M >, MTS extends MonadRec < Tuple2 < A , S >, M >> StateTMatcher <S , M , A , MA , MS , MTS > whenRun (S initialState , MA value , MS state ) {
96- return whenRunWith (initialState , equalTo (value ), equalTo (state ));
95+ public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec <A , M >, MS extends MonadRec <S , M >> StateTMatcher <S , M , A > whenRunBoth (S initialState , MonadRec < A , M > value , MonadRec < S , M > state ) {
96+ return whenRunWithBoth (initialState , equalTo (value ), equalTo (state ));
9797 }
9898
99- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec < A , M >, MS extends MonadRec <S , M >, MTS extends MonadRec < Tuple2 < A , S >, M >> StateTMatcher <S , M , A , MA , MS , MTS > whenExecutedWith (S initialState , Matcher <? super MS > stateMatcher ) {
100- return new StateTMatcher <>(initialState , These .b (stateMatcher ));
99+ public static <S , M extends MonadRec <?, M >, A , MS extends MonadRec <S , M >> StateTMatcher <S , M , A > whenExecutedWith (S initialState , Matcher <? super MS > stateMatcher ) {
100+ return new StateTMatcher <S , M , A >(initialState , These .b (extendMatcher ( stateMatcher ) ));
101101 }
102102
103- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec < A , M >, MS extends MonadRec < S , M >, MTS extends MonadRec < Tuple2 < A , S >, M >> StateTMatcher <S , M , A , MA , MS , MTS > whenExecuted (S initialState , MS state ) {
103+ public static <S , M extends MonadRec <?, M >, A > StateTMatcher <S , M , A > whenExecuted (S initialState , MonadRec < S , M > state ) {
104104 return whenExecutedWith (initialState , equalTo (state ));
105105 }
106106
107- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec <A , M >, MS extends MonadRec < S , M >, MTS extends MonadRec < Tuple2 < A , S >, M >> StateTMatcher <S , M , A , MA , MS , MTS > whenEvaluatedWith (S initialState , Matcher <? super MA > valueMatcher ) {
108- return new StateTMatcher <>(initialState , These .a (valueMatcher ));
107+ public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec <A , M >> StateTMatcher <S , M , A > whenEvaluatedWith (S initialState , Matcher <? super MA > valueMatcher ) {
108+ return new StateTMatcher <S , M , A >(initialState , These .a (extendMatcher ( valueMatcher ) ));
109109 }
110110
111- public static <S , M extends MonadRec <?, M >, A , MA extends MonadRec < A , M >, MS extends MonadRec < S , M >, MTS extends MonadRec < Tuple2 < A , S >, M >> StateTMatcher <S , M , A , MA , MS , MTS > whenEvaluated (S initialState , MA value ) {
111+ public static <S , M extends MonadRec <?, M >, A > StateTMatcher <S , M , A > whenEvaluated (S initialState , MonadRec < A , M > value ) {
112112 return whenEvaluatedWith (initialState , equalTo (value ));
113113 }
114+
115+ private static <X , M extends MonadRec <?, M >, MX extends MonadRec <X , M >> Matcher <MonadRec <X , M >> extendMatcher (Matcher <? super MX > matcher ) {
116+ return new TypeSafeMatcher <MonadRec <X , M >>() {
117+ @ Override
118+ protected boolean matchesSafely (MonadRec <X , M > item ) {
119+ return matcher .matches (item );
120+ }
121+
122+ @ Override
123+ public void describeTo (Description description ) {
124+ matcher .describeTo (description );
125+ }
126+ };
127+ }
114128}
0 commit comments