diff options
Diffstat (limited to 'src/test/java/org/pacien/lemonad/validation/ValidationTest.java')
-rw-r--r-- | src/test/java/org/pacien/lemonad/validation/ValidationTest.java | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/test/java/org/pacien/lemonad/validation/ValidationTest.java b/src/test/java/org/pacien/lemonad/validation/ValidationTest.java new file mode 100644 index 0000000..665f501 --- /dev/null +++ b/src/test/java/org/pacien/lemonad/validation/ValidationTest.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 ValidationTest { | ||
36 | @Test void testValidResult() { | ||
37 | var subject = "subject"; | ||
38 | var validation = Validation.valid(subject); | ||
39 | assertTrue(validation.getErrors().isEmpty()); | ||
40 | assertTrue(validation.isValid()); | ||
41 | assertFalse(validation.isInvalid()); | ||
42 | validation.ifValid(innerSubject -> assertEquals(subject, innerSubject)); | ||
43 | validation.ifInvalid((__, ___) -> fail()); | ||
44 | } | ||
45 | |||
46 | @Test void testInvalidResult() { | ||
47 | var subject = "subject"; | ||
48 | var errors = List.of(0, 1); | ||
49 | var validation = Validation.invalid(subject, 0, 1); | ||
50 | assertEquals(errors, validation.getErrors()); | ||
51 | assertFalse(validation.isValid()); | ||
52 | assertTrue(validation.isInvalid()); | ||
53 | validation.ifValid(Assertions::fail); | ||
54 | validation.ifInvalid((innerSubject, innerErrors) -> { | ||
55 | assertEquals(subject, innerSubject); | ||
56 | assertEquals(errors, innerErrors); | ||
57 | }); | ||
58 | } | ||
59 | |||
60 | @Test void testFlatMap() { | ||
61 | Validation.valid("subject") | ||
62 | .ifInvalid((__, ___) -> fail()) | ||
63 | .flatMap(res -> Validation.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), Validation.merge(subject, Stream.of( | ||
70 | Validation.valid(subject), | ||
71 | Validation.invalid(subject, 0, 1), | ||
72 | Validation.invalid(subject, 2, 3)) | ||
73 | ).getErrors()); | ||
74 | } | ||
75 | } | ||