forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTryTest.java
More file actions
50 lines (42 loc) · 1.39 KB
/
TryTest.java
File metadata and controls
50 lines (42 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package fj;
import fj.data.Validation;
import fj.function.Try0;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class TryTest {
@Test
public void testTrySuccess() {
F<Try0<Integer, TryException>, Validation<TryException, Integer>> f =
Try.f(Try0::f);
Validation<TryException, Integer> v = f.f(new AlwaysSucceed());
assertThat(v.isSuccess(), is(true));
assertThat(v.success(), is(99));
}
@Test
public void testTryFail() {
F<Try0<Integer, TryException>, Validation<TryException, Integer>> f =
Try.f(Try0::f);
Validation<TryException, Integer> v = f.f(new AlwaysFail());
assertThat(v.isFail(), is(true));
assertThat(v.fail(), is(new TryException()));
}
class AlwaysSucceed implements Try0<Integer, TryException> {
@Override
public Integer f() throws TryException {
return 99;
}
}
class AlwaysFail implements Try0<Integer, TryException> {
@Override
public Integer f() throws TryException {
throw new TryException();
}
}
class TryException extends Exception {
@Override
public boolean equals (Object obj) {
return (obj instanceof TryException);
}
}
}