aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/pacien/lemonad/attempt
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/pacien/lemonad/attempt')
-rw-r--r--src/test/java/org/pacien/lemonad/attempt/AttemptTest.java114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
new file mode 100644
index 0000000..1b165e8
--- /dev/null
+++ b/src/test/java/org/pacien/lemonad/attempt/AttemptTest.java
@@ -0,0 +1,114 @@
1/*
2 * lemonad - Some functional sweetness for Java
3 * Copyright (C) 2019 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19package org.pacien.lemonad.attempt;
20
21import org.junit.jupiter.api.Test;
22
23import java.util.NoSuchElementException;
24
25import static org.junit.jupiter.api.Assertions.assertEquals;
26import static org.junit.jupiter.api.Assertions.assertFalse;
27import static org.junit.jupiter.api.Assertions.assertThrows;
28import static org.junit.jupiter.api.Assertions.assertTrue;
29import static org.junit.jupiter.api.Assertions.fail;
30
31/**
32 * @author pacien
33 */
34class AttemptTest {
35 @Test void testSimpleSuccess() {
36 var result = "result";
37 var success = Attempt.success(result);
38 assertFalse(success.isFailure());
39 assertTrue(success.isSuccess());
40 assertThrows(NoSuchElementException.class, success::getError);
41 assertEquals(result, success.getResult());
42 success.ifFailure(__ -> fail());
43 success.ifSuccess(innerResult -> assertEquals(result, innerResult));
44 }
45
46 @Test void testSimpleFailure() {
47 var fault = 0;
48 var failure = Attempt.failure(fault);
49 assertTrue(failure.isFailure());
50 assertFalse(failure.isSuccess());
51 assertEquals(fault, failure.getError());
52 assertThrows(NoSuchElementException.class, failure::getResult);
53 failure.ifFailure(innerFault -> assertEquals(fault, innerFault));
54 failure.ifSuccess(__ -> fail());
55 }
56
57 @Test void testNormalAttempt() {
58 var result = "result";
59 var success = Attempt.attempt(() -> result);
60 assertFalse(success.isFailure());
61 assertTrue(success.isSuccess());
62 assertThrows(NoSuchElementException.class, success::getError);
63 assertEquals(result, success.getResult());
64 success.ifFailure(__ -> fail());
65 success.ifSuccess(innerResult -> assertEquals(result, innerResult));
66 }
67
68 @Test void testFailedAttempt() {
69 var exception = new Exception();
70 var failure = Attempt.attempt(() -> {
71 throw exception;
72 });
73 assertTrue(failure.isFailure());
74 assertFalse(failure.isSuccess());
75 assertEquals(exception, failure.getError());
76 assertThrows(NoSuchElementException.class, failure::getResult);
77 failure.ifFailure(innerFault -> assertEquals(exception, innerFault));
78 failure.ifSuccess(__ -> fail());
79 }
80
81 @Test void testTransformationFlow() {
82 var result0 = 0;
83 var result1 = "res";
84 var result2 = 0L;
85 var fault0 = 0;
86 var fault1 = 1;
87 var fault2 = 2;
88
89 Attempt.success(result0)
90 .mapFailure(__ -> fail())
91 .mapResult(res -> Attempt.success(result1))
92 .mapResult(res -> {
93 assertEquals(result1, res);
94 return Attempt.failure(fault0);
95 })
96 .ifSuccess(__ -> fail())
97 .mapResult(__ -> fail())
98 .mapFailure(f -> {
99 assertEquals(fault0, f);
100 return Attempt.failure(fault1);
101 })
102 .mapFailure(f -> {
103 assertEquals(fault1, f);
104 return Attempt.success(result2);
105 })
106 .ifFailure(__ -> fail())
107 .flatMap(attempt -> {
108 assertEquals(result2, attempt.getResult());
109 return Attempt.failure(fault2);
110 })
111 .ifSuccess(__ -> fail())
112 .ifFailure(f -> assertEquals(fault2, f));
113 }
114}