aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/pacien/lemonad/validation
diff options
context:
space:
mode:
authorpacien2019-04-13 21:24:40 +0200
committerpacien2019-04-13 21:24:40 +0200
commit7ded9823fb54b81cbd73a133fd1e1fe036f31b04 (patch)
treed6f05d089507290d9cc47f24b6799e81ba31a5d8 /src/test/java/org/pacien/lemonad/validation
parent80f7acccde921fb387a17d2a5712babf3001b974 (diff)
downloadjava-lemonad-7ded9823fb54b81cbd73a133fd1e1fe036f31b04.tar.gz
refactor validation
Diffstat (limited to 'src/test/java/org/pacien/lemonad/validation')
-rw-r--r--src/test/java/org/pacien/lemonad/validation/ValidationTest.java39
-rw-r--r--src/test/java/org/pacien/lemonad/validation/ValidatorTest.java61
2 files changed, 26 insertions, 74 deletions
diff --git a/src/test/java/org/pacien/lemonad/validation/ValidationTest.java b/src/test/java/org/pacien/lemonad/validation/ValidationTest.java
index fed74a3..c19c694 100644
--- a/src/test/java/org/pacien/lemonad/validation/ValidationTest.java
+++ b/src/test/java/org/pacien/lemonad/validation/ValidationTest.java
@@ -23,7 +23,6 @@ import org.junit.jupiter.api.Test;
23import org.pacien.lemonad.attempt.Attempt; 23import org.pacien.lemonad.attempt.Attempt;
24 24
25import java.util.List; 25import java.util.List;
26import java.util.stream.Stream;
27 26
28import static org.junit.jupiter.api.Assertions.assertEquals; 27import static org.junit.jupiter.api.Assertions.assertEquals;
29import static org.junit.jupiter.api.Assertions.assertFalse; 28import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -36,7 +35,7 @@ import static org.junit.jupiter.api.Assertions.fail;
36class ValidationTest { 35class ValidationTest {
37 @Test void testValidResult() { 36 @Test void testValidResult() {
38 var subject = "subject"; 37 var subject = "subject";
39 var validation = Validation.valid(subject); 38 var validation = Validation.of(subject);
40 assertTrue(validation.getErrors().isEmpty()); 39 assertTrue(validation.getErrors().isEmpty());
41 assertTrue(validation.isValid()); 40 assertTrue(validation.isValid());
42 assertFalse(validation.isInvalid()); 41 assertFalse(validation.isInvalid());
@@ -48,7 +47,7 @@ class ValidationTest {
48 @Test void testInvalidResult() { 47 @Test void testInvalidResult() {
49 var subject = "subject"; 48 var subject = "subject";
50 var errors = List.of(0, 1); 49 var errors = List.of(0, 1);
51 var validation = Validation.invalid(subject, 0, 1); 50 var validation = Validation.of(subject, 0, 1);
52 assertEquals(errors, validation.getErrors()); 51 assertEquals(errors, validation.getErrors());
53 assertFalse(validation.isValid()); 52 assertFalse(validation.isValid());
54 assertTrue(validation.isInvalid()); 53 assertTrue(validation.isInvalid());
@@ -61,18 +60,32 @@ class ValidationTest {
61 } 60 }
62 61
63 @Test void testFlatMap() { 62 @Test void testFlatMap() {
64 Validation.valid("subject") 63 Validation
65 .ifInvalid((__, ___) -> fail()) 64 .of("subject")
66 .flatMap(res -> Validation.invalid(res.getSubject(), 0)) 65 .ifInvalid((__, ___) -> fail())
67 .ifValid(innerSubject -> fail()); 66 .flatMap(res -> Validation.of(res.getSubject(), 0))
67 .ifValid(innerSubject -> fail());
68 } 68 }
69 69
70 @Test void testMerge() { 70 @Test void testMerge() {
71 var subject = "subject"; 71 var validation = Validation
72 assertEquals(List.of(0, 1, 2, 3), Validation.merge(subject, Stream.of( 72 .of(12345, 0)
73 Validation.valid(subject), 73 .merge(s -> Validation.of(s, 1))
74 Validation.invalid(subject, 0, 1), 74 .merge((Integer s) -> Integer.toString(s), (String s) -> Validation.of(s, 2))
75 Validation.invalid(subject, 2, 3)) 75 .merge(Validation.of(0L, List.of(3)))
76 ).getErrors()); 76 .merge(List.of(4));
77
78 assertEquals(Validation.of(12345, 0, 1, 2, 3, 4), validation);
79 }
80
81 @Test void testValidate() {
82 var validation = Validation
83 .of("subject")
84 .validate(String::isEmpty, 0)
85 .validate(String::length, len -> len > 0, 1)
86 .validate(subject -> List.of(2, 3))
87 .validate(subject -> subject.charAt(0), firstChar -> firstChar == 's' ? List.of() : List.of(4));
88
89 assertEquals(Validation.of("subject", 0, 2, 3), validation);
77 } 90 }
78} 91}
diff --git a/src/test/java/org/pacien/lemonad/validation/ValidatorTest.java b/src/test/java/org/pacien/lemonad/validation/ValidatorTest.java
deleted file mode 100644
index 55927b5..0000000
--- a/src/test/java/org/pacien/lemonad/validation/ValidatorTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
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}