33import com .jnape .palatable .traitor .annotations .TestTraits ;
44import com .jnape .palatable .traitor .framework .Subjects ;
55import com .jnape .palatable .traitor .runners .Traits ;
6+ import org .junit .Rule ;
67import org .junit .Test ;
8+ import org .junit .rules .ExpectedException ;
79import org .junit .runner .RunWith ;
810import testsupport .traits .ApplicativeLaws ;
911import testsupport .traits .FunctorLaws ;
1820import static com .jnape .palatable .lambda .adt .Maybe .nothing ;
1921import static com .jnape .palatable .lambda .adt .Try .failure ;
2022import static com .jnape .palatable .lambda .adt .Try .success ;
23+ import static com .jnape .palatable .lambda .adt .Try .trying ;
2124import static com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly .constantly ;
2225import static com .jnape .palatable .traitor .framework .Subjects .subjects ;
26+ import static org .hamcrest .CoreMatchers .equalTo ;
2327import static org .hamcrest .CoreMatchers .instanceOf ;
2428import static org .junit .Assert .assertEquals ;
2529import static org .junit .Assert .assertThat ;
2832@ RunWith (Traits .class )
2933public class TryTest {
3034
35+ @ Rule public ExpectedException thrown = ExpectedException .none ();
36+
3137 @ TestTraits ({FunctorLaws .class , ApplicativeLaws .class , MonadLaws .class , TraversableLaws .class })
3238 public Subjects <Try <Throwable , Integer >> testSubject () {
3339 return subjects (failure (new IllegalStateException ()), success (1 ));
@@ -108,6 +114,15 @@ public void recoverEnsuresSuccess() {
108114 assertEquals ((Integer ) 1 , Try .<RuntimeException , Integer >failure (new IllegalArgumentException ()).recover (constantly (1 )));
109115 }
110116
117+ @ Test
118+ public void orThrow () throws Throwable {
119+ assertEquals ((Integer ) 1 , trying (() -> 1 ).orThrow ());
120+
121+ Throwable expected = new Exception ("expected" );
122+ thrown .expect (equalTo (expected ));
123+ trying (() -> {throw expected ;}).orThrow ();
124+ }
125+
111126 @ Test
112127 public void toMaybe () {
113128 assertEquals (just ("foo" ), Try .success ("foo" ).toMaybe ());
@@ -131,8 +146,8 @@ public void toEitherWithLeftMappingFunction() {
131146 @ Test
132147 public void tryingCatchesAnyThrowableThrownDuringEvaluation () {
133148 IllegalStateException expected = new IllegalStateException ();
134- assertEquals (failure (expected ), Try . trying (() -> {throw expected ;}));
149+ assertEquals (failure (expected ), trying (() -> {throw expected ;}));
135150
136- assertEquals (success ("foo" ), Try . trying (() -> "foo" ));
151+ assertEquals (success ("foo" ), trying (() -> "foo" ));
137152 }
138153}
0 commit comments