diff options
author | pacien | 2019-03-29 01:31:15 +0100 |
---|---|---|
committer | pacien | 2019-03-29 01:31:15 +0100 |
commit | b6869c1b17c65594f65c3ad5e53461082e5c3088 (patch) | |
tree | 1b924d802852efbbc0519210fb947b095ddf48b4 /src/test/java/org | |
parent | eadf4a9f3c7d539f1af08ee37928ad0f80f4e4d3 (diff) | |
download | java-lemonad-b6869c1b17c65594f65c3ad5e53461082e5c3088.tar.gz |
initial impl
Diffstat (limited to 'src/test/java/org')
3 files changed, 250 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 | |||
19 | package org.pacien.lemonad.attempt; | ||
20 | |||
21 | import org.junit.jupiter.api.Test; | ||
22 | |||
23 | import java.util.NoSuchElementException; | ||
24 | |||
25 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
26 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
27 | import static org.junit.jupiter.api.Assertions.assertThrows; | ||
28 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
29 | import static org.junit.jupiter.api.Assertions.fail; | ||
30 | |||
31 | /** | ||
32 | * @author pacien | ||
33 | */ | ||
34 | class 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 | } | ||
diff --git a/src/test/java/org/pacien/lemonad/validation/ValidationResultTest.java b/src/test/java/org/pacien/lemonad/validation/ValidationResultTest.java new file mode 100644 index 0000000..65b2cb3 --- /dev/null +++ b/src/test/java/org/pacien/lemonad/validation/ValidationResultTest.java | |||
@@ -0,0 +1,75 @@ | |||
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 | |||
19 | package org.pacien.lemonad.validation; | ||
20 | |||
21 | import org.junit.jupiter.api.Assertions; | ||
22 | import org.junit.jupiter.api.Test; | ||
23 | |||
24 | import java.util.List; | ||
25 | import java.util.stream.Stream; | ||
26 | |||
27 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
28 | import static org.junit.jupiter.api.Assertions.assertFalse; | ||
29 | import static org.junit.jupiter.api.Assertions.assertTrue; | ||
30 | import static org.junit.jupiter.api.Assertions.fail; | ||
31 | |||
32 | /** | ||
33 | * @author pacien | ||
34 | */ | ||
35 | class ValidationResultTest { | ||
36 | @Test void testValidResult() { | ||
37 | var subject = "subject"; | ||
38 | var validationResult = ValidationResult.valid(subject); | ||
39 | assertTrue(validationResult.getErrors().isEmpty()); | ||
40 | assertTrue(validationResult.isValid()); | ||
41 | assertFalse(validationResult.isInvalid()); | ||
42 | validationResult.ifValid(innerSubject -> assertEquals(subject, innerSubject)); | ||
43 | validationResult.ifInvalid((__, ___) -> fail()); | ||
44 | } | ||
45 | |||
46 | @Test void testInvalidResult() { | ||
47 | var subject = "subject"; | ||
48 | var errors = List.of(0, 1); | ||
49 | var validationResult = ValidationResult.invalid(subject, 0, 1); | ||
50 | assertEquals(errors, validationResult.getErrors()); | ||
51 | assertFalse(validationResult.isValid()); | ||
52 | assertTrue(validationResult.isInvalid()); | ||
53 | validationResult.ifValid(Assertions::fail); | ||
54 | validationResult.ifInvalid((innerSubject, innerErrors) -> { | ||
55 | assertEquals(subject, innerSubject); | ||
56 | assertEquals(errors, innerErrors); | ||
57 | }); | ||
58 | } | ||
59 | |||
60 | @Test void testFlatMap() { | ||
61 | ValidationResult.valid("subject") | ||
62 | .ifInvalid((__, ___) -> fail()) | ||
63 | .flatMap(res -> ValidationResult.invalid(res.getSubject(), 0)) | ||
64 | .ifValid(innerSubject -> fail()); | ||
65 | } | ||
66 | |||
67 | @Test void testMerge() { | ||
68 | var subject = "subject"; | ||
69 | assertEquals(List.of(0, 1, 2, 3), ValidationResult.merge(subject, Stream.of( | ||
70 | ValidationResult.valid(subject), | ||
71 | ValidationResult.invalid(subject, 0, 1), | ||
72 | ValidationResult.invalid(subject, 2, 3)) | ||
73 | ).getErrors()); | ||
74 | } | ||
75 | } | ||
diff --git a/src/test/java/org/pacien/lemonad/validation/ValidatorTest.java b/src/test/java/org/pacien/lemonad/validation/ValidatorTest.java new file mode 100644 index 0000000..55927b5 --- /dev/null +++ b/src/test/java/org/pacien/lemonad/validation/ValidatorTest.java | |||
@@ -0,0 +1,61 @@ | |||
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 | ||