aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org')
-rw-r--r--src/test/java/org/pacien/lemonad/attempt/AttemptTest.java80
1 files changed, 43 insertions, 37 deletions
diff --git a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
index bb367dc..706a81d 100644
--- a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
+++ b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
@@ -44,13 +44,13 @@ class AttemptTest {
44 } 44 }
45 45
46 @Test void testSimpleFailure() { 46 @Test void testSimpleFailure() {
47 var fault = 0; 47 var error = 0;
48 var failure = Attempt.failure(fault); 48 var failure = Attempt.failure(error);
49 assertTrue(failure.isFailure()); 49 assertTrue(failure.isFailure());
50 assertFalse(failure.isSuccess()); 50 assertFalse(failure.isSuccess());
51 assertEquals(fault, failure.getError()); 51 assertEquals(error, failure.getError());
52 assertThrows(NoSuchElementException.class, failure::getResult); 52 assertThrows(NoSuchElementException.class, failure::getResult);
53 failure.ifFailure(innerFault -> assertEquals(fault, innerFault)); 53 failure.ifFailure(innerError -> assertEquals(error, innerError));
54 failure.ifSuccess(__ -> fail()); 54 failure.ifSuccess(__ -> fail());
55 } 55 }
56 56
@@ -74,46 +74,52 @@ class AttemptTest {
74 assertFalse(failure.isSuccess()); 74 assertFalse(failure.isSuccess());
75 assertEquals(exception, failure.getError()); 75 assertEquals(exception, failure.getError());
76 assertThrows(NoSuchElementException.class, failure::getResult); 76 assertThrows(NoSuchElementException.class, failure::getResult);
77 failure.ifFailure(innerFault -> assertEquals(exception, innerFault)); 77 failure.ifFailure(innerError -> assertEquals(exception, innerError));
78 failure.ifSuccess(__ -> fail()); 78 failure.ifSuccess(__ -> fail());
79 } 79 }
80 80
81 @Test void testTransformationFlow() { 81 @Test void testTransformationFlow() {
82 var result0 = 0; 82 var result0 = 0;
83 var result1 = "res"; 83 var result1 = "res";
84 var result2 = true; 84 var result2 = "result";
85 var result3 = 0L; 85 var result3 = 0L;
86 var fault0 = 0L; 86 var error0 = 0;
87 var fault1 = 0; 87 var error1 = 0L;
88 var fault2 = "fail"; 88 var error2 = "fail";
89 var fault3 = false; 89 var error3 = false;
90 90
91 Attempt.success(result0) 91 Attempt.<Integer, Integer>success(result0)
92 .mapError(__ -> fail()) 92 .mapError((Integer err) -> {
93 .mapResult(res -> Attempt.success(result1)) 93 fail();
94 .mapResult(res -> { 94 return Attempt.failure(err);
95 assertEquals(result1, res); 95 })
96 return Attempt.failure(fault0); 96 .mapResult((Integer res) -> Attempt.success(result1))
97 }) 97 .mapResult((String res) -> {
98 .ifSuccess(__ -> fail()) 98 assertEquals(result1, res);
99 .mapResult(__ -> fail()) 99 return Attempt.<String, Integer>failure(error0);
100 .mapError(f -> { 100 })
101 assertEquals(fault0, f); 101 .ifSuccess((String __) -> fail())
102 return Attempt.failure(fault1); 102 .mapResult((String res) -> {
103 }) 103 fail();
104 .mapError(f -> { 104 return Attempt.success(res);
105 assertEquals(fault1, f); 105 })
106 return Attempt.success(result2); 106 .mapError((Integer err) -> {
107 }) 107 assertEquals(error0, err);
108 .ifFailure(__ -> fail()) 108 return Attempt.failure(error1);
109 .flatMap(attempt -> { 109 })
110 assertEquals(result2, attempt.getResult()); 110 .mapError((Long err) -> {
111 return Attempt.failure(fault2); 111 assertEquals(error1, err);
112 }) 112 return Attempt.<String, Long>success(result2);
113 .ifSuccess(__ -> fail()) 113 })
114 .ifFailure(f -> assertEquals(fault2, f)) 114 .ifFailure((Long err) -> fail())
115 .map(__ -> Attempt.failure(fault3), __ -> Attempt.success(result3)) 115 .flatMap((Attempt<? super String, ? super Long> attempt) -> {
116 .ifSuccess(result -> assertEquals(result3, result)) 116 assertEquals(result2, attempt.getResult());
117 .ifFailure(__ -> fail()); 117 return Attempt.<String, String>failure(error2);
118 })
119 .ifSuccess(__ -> fail())
120 .ifFailure(f -> assertEquals(error2, f))
121 .map((String __) -> Attempt.failure(error3), (String __) -> Attempt.success(result3))
122 .ifSuccess((Long result) -> assertEquals(result3, result))
123 .ifFailure((Boolean __) -> fail());
118 } 124 }
119} 125}