aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/pacien/lemonad/validation
diff options
context:
space:
mode:
authorpacien2019-03-29 01:31:15 +0100
committerpacien2019-03-29 01:31:15 +0100
commitb6869c1b17c65594f65c3ad5e53461082e5c3088 (patch)
tree1b924d802852efbbc0519210fb947b095ddf48b4 /src/test/java/org/pacien/lemonad/validation
parenteadf4a9f3c7d539f1af08ee37928ad0f80f4e4d3 (diff)
downloadjava-lemonad-b6869c1b17c65594f65c3ad5e53461082e5c3088.tar.gz
initial impl
Diffstat (limited to 'src/test/java/org/pacien/lemonad/validation')
-rw-r--r--src/test/java/org/pacien/lemonad/validation/ValidationResultTest.java75
-rw-r--r--src/test/java/org/pacien/lemonad/validation/ValidatorTest.java61
2 files changed, 136 insertions, 0 deletions
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
19package org.pacien.lemonad.validation;
20
21import org.junit.jupiter.api.Assertions;
22import org.junit.jupiter.api.Test;
23
24import java.util.List;
25import java.util.stream.Stream;
26
27import static org.junit.jupiter.api.Assertions.assertEquals;
28import static org.junit.jupiter.api.Assertions.assertFalse;
29import static org.junit.jupiter.api.Assertions.assertTrue;
30import static org.junit.jupiter.api.Assertions.fail;
31
32/**
33 * @author pacien
34 */
35class 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
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.validation;
20
21import org.junit.jupiter.api.Test;
22
23import java.util.List;
24
25import static java.util.function.Predicate.not;
26import static org.junit.jupiter.api.Assertions.assertEquals;
27
28/**
29 * @author pacien
30 */
31class ValidatorTest {
32 @Test void testValidatorEnsuringPredicate() {
33 var emptyError = 0;
34 var validator = Validator.ensuringPredicate(not(String::isEmpty), emptyError);
35 assertEquals(List.of(emptyError), validator.validate("").getErrors());
36 assertEquals(List.of(), validator.validate("test").getErrors());
37 }
38
39 @Test void testValidatorValidatingAll() {
40 var emptyError = 0;
41 var tooLongError = 1;
42 var containsBadLetterError = 2;
43
44 var validator = Validator.validatingAll(
45 Validator.ensuringPredicate(not(String::isEmpty), emptyError),
46 Validator.ensuringPredicate((String str) -> str.length() < 10, tooLongError),
47 Validator.ensuringPredicate((String str) -> !str.contains("e"), containsBadLetterError));
48
49 assertEquals(List.of(emptyError), validator.validate("").getErrors());
50 assertEquals(List.of(tooLongError, containsBadLetterError), validator.validate("test test test").getErrors());
51 assertEquals(List.of(), validator.validate("potato").getErrors());
52 }
53
54 @Test void testValidatingField() {
55 var emptyError = 0;
56 var fieldValidator = Validator.ensuringPredicate((Integer len) -> len > 0, emptyError);
57 var validator = Validator.validatingField(String::length, fieldValidator);
58 assertEquals(List.of(emptyError), validator.validate("").getErrors());
59 assertEquals(List.of(), validator.validate("test").getErrors());
60 }
61}