From 7ded9823fb54b81cbd73a133fd1e1fe036f31b04 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 13 Apr 2019 21:24:40 +0200 Subject: refactor validation --- .../pacien/lemonad/validation/ValidationTest.java | 39 +++++++++----- .../pacien/lemonad/validation/ValidatorTest.java | 61 ---------------------- 2 files changed, 26 insertions(+), 74 deletions(-) delete mode 100644 src/test/java/org/pacien/lemonad/validation/ValidatorTest.java (limited to 'src/test/java/org/pacien') 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; import org.pacien.lemonad.attempt.Attempt; import java.util.List; -import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -36,7 +35,7 @@ import static org.junit.jupiter.api.Assertions.fail; class ValidationTest { @Test void testValidResult() { var subject = "subject"; - var validation = Validation.valid(subject); + var validation = Validation.of(subject); assertTrue(validation.getErrors().isEmpty()); assertTrue(validation.isValid()); assertFalse(validation.isInvalid()); @@ -48,7 +47,7 @@ class ValidationTest { @Test void testInvalidResult() { var subject = "subject"; var errors = List.of(0, 1); - var validation = Validation.invalid(subject, 0, 1); + var validation = Validation.of(subject, 0, 1); assertEquals(errors, validation.getErrors()); assertFalse(validation.isValid()); assertTrue(validation.isInvalid()); @@ -61,18 +60,32 @@ class ValidationTest { } @Test void testFlatMap() { - Validation.valid("subject") - .ifInvalid((__, ___) -> fail()) - .flatMap(res -> Validation.invalid(res.getSubject(), 0)) - .ifValid(innerSubject -> fail()); + Validation + .of("subject") + .ifInvalid((__, ___) -> fail()) + .flatMap(res -> Validation.of(res.getSubject(), 0)) + .ifValid(innerSubject -> fail()); } @Test void testMerge() { - var subject = "subject"; - assertEquals(List.of(0, 1, 2, 3), Validation.merge(subject, Stream.of( - Validation.valid(subject), - Validation.invalid(subject, 0, 1), - Validation.invalid(subject, 2, 3)) - ).getErrors()); + var validation = Validation + .of(12345, 0) + .merge(s -> Validation.of(s, 1)) + .merge((Integer s) -> Integer.toString(s), (String s) -> Validation.of(s, 2)) + .merge(Validation.of(0L, List.of(3))) + .merge(List.of(4)); + + assertEquals(Validation.of(12345, 0, 1, 2, 3, 4), validation); + } + + @Test void testValidate() { + var validation = Validation + .of("subject") + .validate(String::isEmpty, 0) + .validate(String::length, len -> len > 0, 1) + .validate(subject -> List.of(2, 3)) + .validate(subject -> subject.charAt(0), firstChar -> firstChar == 's' ? List.of() : List.of(4)); + + assertEquals(Validation.of("subject", 0, 2, 3), validation); } } 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 @@ -/* - * lemonad - Some functional sweetness for Java - * Copyright (C) 2019 Pacien TRAN-GIRARD - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package org.pacien.lemonad.validation; - -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static java.util.function.Predicate.not; -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author pacien - */ -class ValidatorTest { - @Test void testValidatorEnsuringPredicate() { - var emptyError = 0; - var validator = Validator.ensuringPredicate(not(String::isEmpty), emptyError); - assertEquals(List.of(emptyError), validator.validate("").getErrors()); - assertEquals(List.of(), validator.validate("test").getErrors()); - } - - @Test void testValidatorValidatingAll() { - var emptyError = 0; - var tooLongError = 1; - var containsBadLetterError = 2; - - var validator = Validator.validatingAll( - Validator.ensuringPredicate(not(String::isEmpty), emptyError), - Validator.ensuringPredicate((String str) -> str.length() < 10, tooLongError), - Validator.ensuringPredicate((String str) -> !str.contains("e"), containsBadLetterError)); - - assertEquals(List.of(emptyError), validator.validate("").getErrors()); - assertEquals(List.of(tooLongError, containsBadLetterError), validator.validate("test test test").getErrors()); - assertEquals(List.of(), validator.validate("potato").getErrors()); - } - - @Test void testValidatingField() { - var emptyError = 0; - var fieldValidator = Validator.ensuringPredicate((Integer len) -> len > 0, emptyError); - var validator = Validator.validatingField(String::length, fieldValidator); - assertEquals(List.of(emptyError), validator.validate("").getErrors()); - assertEquals(List.of(), validator.validate("test").getErrors()); - } -} -- cgit v1.2.3