3
3
import com .jnape .palatable .traitor .annotations .TestTraits ;
4
4
import com .jnape .palatable .traitor .framework .Subjects ;
5
5
import com .jnape .palatable .traitor .runners .Traits ;
6
+ import org .junit .Rule ;
6
7
import org .junit .Test ;
8
+ import org .junit .rules .ExpectedException ;
7
9
import org .junit .runner .RunWith ;
8
10
import testsupport .traits .ApplicativeLaws ;
9
11
import testsupport .traits .FunctorLaws ;
18
20
import static com .jnape .palatable .lambda .adt .Maybe .nothing ;
19
21
import static com .jnape .palatable .lambda .adt .Try .failure ;
20
22
import static com .jnape .palatable .lambda .adt .Try .success ;
23
+ import static com .jnape .palatable .lambda .adt .Try .trying ;
21
24
import static com .jnape .palatable .lambda .functions .builtin .fn1 .Constantly .constantly ;
22
25
import static com .jnape .palatable .traitor .framework .Subjects .subjects ;
26
+ import static org .hamcrest .CoreMatchers .equalTo ;
23
27
import static org .hamcrest .CoreMatchers .instanceOf ;
24
28
import static org .junit .Assert .assertEquals ;
25
29
import static org .junit .Assert .assertThat ;
28
32
@ RunWith (Traits .class )
29
33
public class TryTest {
30
34
35
+ @ Rule public ExpectedException thrown = ExpectedException .none ();
36
+
31
37
@ TestTraits ({FunctorLaws .class , ApplicativeLaws .class , MonadLaws .class , TraversableLaws .class })
32
38
public Subjects <Try <Throwable , Integer >> testSubject () {
33
39
return subjects (failure (new IllegalStateException ()), success (1 ));
@@ -108,6 +114,15 @@ public void recoverEnsuresSuccess() {
108
114
assertEquals ((Integer ) 1 , Try .<RuntimeException , Integer >failure (new IllegalArgumentException ()).recover (constantly (1 )));
109
115
}
110
116
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
+
111
126
@ Test
112
127
public void toMaybe () {
113
128
assertEquals (just ("foo" ), Try .success ("foo" ).toMaybe ());
@@ -131,8 +146,8 @@ public void toEitherWithLeftMappingFunction() {
131
146
@ Test
132
147
public void tryingCatchesAnyThrowableThrownDuringEvaluation () {
133
148
IllegalStateException expected = new IllegalStateException ();
134
- assertEquals (failure (expected ), Try . trying (() -> {throw expected ;}));
149
+ assertEquals (failure (expected ), trying (() -> {throw expected ;}));
135
150
136
- assertEquals (success ("foo" ), Try . trying (() -> "foo" ));
151
+ assertEquals (success ("foo" ), trying (() -> "foo" ));
137
152
}
138
153
}
0 commit comments