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/pacien/lemonad/validation/ValidatorTest.java | |
parent | eadf4a9f3c7d539f1af08ee37928ad0f80f4e4d3 (diff) | |
download | java-lemonad-b6869c1b17c65594f65c3ad5e53461082e5c3088.tar.gz |
initial impl
Diffstat (limited to 'src/test/java/org/pacien/lemonad/validation/ValidatorTest.java')
-rw-r--r-- | src/test/java/org/pacien/lemonad/validation/ValidatorTest.java | 61 |
1 files changed, 61 insertions, 0 deletions
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 | |||
19 | package org.pacien.lemonad.validation; | ||
20 | |||
21 | import org.junit.jupiter.api.Test; | ||
22 | |||
23 | import java.util.List; | ||
24 | |||
25 | import static java.util.function.Predicate.not; | ||
26 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
27 | |||
28 | /** | ||
29 | * @author pacien | ||
30 | */ | ||
31 | class 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 | } | ||