|
26 | 26 | import java.util.ArrayList;
|
27 | 27 | import java.util.Objects;
|
28 | 28 | import java.util.Spliterator;
|
| 29 | +import org.junit.jupiter.api.Nested; |
29 | 30 | import org.junit.jupiter.api.Test;
|
30 | 31 |
|
31 | 32 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
| 33 | +import static org.junit.jupiter.api.Assertions.fail; |
32 | 34 |
|
33 | 35 | public class ValidationTest extends AbstractValueTest {
|
34 | 36 |
|
@@ -127,6 +129,103 @@ public void shouldNarrowInvalid() {
|
127 | 129 | assertThat(narrow.getError()).isEqualTo("vavr");
|
128 | 130 | }
|
129 | 131 |
|
| 132 | + @Nested |
| 133 | + public class CondTests { |
| 134 | + |
| 135 | + @Test |
| 136 | + public void shouldReturnValidIfTestTrue() { |
| 137 | + Validation<String, Integer> validation = Validation.cond(true, () -> 21, () -> "vavr"); |
| 138 | + assertThat(validation).isEqualTo(Validation.valid(21)); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void shouldReturnInvalidIfTestFalse() { |
| 143 | + Validation<String, Integer> validation = Validation.cond(false, () -> 21, () -> "vavr"); |
| 144 | + assertThat(validation).isEqualTo(Validation.invalid("vavr")); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void shouldNotEvaluateValidSupplierOnFalse() { |
| 149 | + Validation<String, Integer> validation = Validation.cond(false, () -> { |
| 150 | + fail("Should not be called"); |
| 151 | + return 21; |
| 152 | + }, () -> "vavr"); |
| 153 | + assertThat(validation).isEqualTo(Validation.invalid("vavr")); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void shouldNotEvaluateErrorSupplierOnTrue() { |
| 158 | + Validation<String, Integer> validation = Validation.cond(true, () -> 21, () -> { |
| 159 | + fail("Should not be called"); |
| 160 | + return "vavr"; |
| 161 | + }); |
| 162 | + assertThat(validation).isEqualTo(Validation.valid(21)); |
| 163 | + } |
| 164 | + |
| 165 | + private class Car { |
| 166 | + String name; |
| 167 | + |
| 168 | + Car(String name) { |
| 169 | + this.name = name; |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public boolean equals(Object o) { |
| 174 | + if (this == o) return true; |
| 175 | + if (!(o instanceof Car)) return false; |
| 176 | + Car other = (Car) o; |
| 177 | + return name.equals(other.name); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public int hashCode() { |
| 182 | + return name.hashCode(); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private class Hatchback extends Car { |
| 187 | + Hatchback(String name) { |
| 188 | + super(name); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private class Sedan extends Car { |
| 193 | + Sedan(String name) { |
| 194 | + super(name); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + public void shouldBeFineWithCovariantError() { |
| 200 | + Validation<Car, Integer> validation = Validation.cond(false, () -> 21, () -> new Hatchback("vavr")); |
| 201 | + assertThat(validation).isEqualTo(Validation.invalid(new Hatchback("vavr"))); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void shouldBeFineWithCovariantValid() { |
| 206 | + Validation<String, Car> validation = Validation.cond(true, () -> new Sedan("vavr"), () -> "vavr"); |
| 207 | + assertThat(validation).isEqualTo(Validation.valid(new Sedan("vavr"))); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void shouldMakeTheSameDecisionNoMatterHowItsCalled() { |
| 212 | + Validation<String, Integer> v1 = Validation.cond(true, () -> 21, () -> "vavr"); |
| 213 | + Validation<String, Integer> v2 = Validation.cond(true, 21, "vavr"); |
| 214 | + |
| 215 | + Validation<String, Integer> v3 = Validation.cond(false, () -> 21, () -> "vavr"); |
| 216 | + Validation<String, Integer> v4 = Validation.cond(false, 21, "vavr"); |
| 217 | + |
| 218 | + assertThat(List.of(v1, v2)).allMatch(e -> e.equals(Validation.valid(21))); |
| 219 | + assertThat(List.of(v3, v4)).allMatch(e -> e.equals(Validation.invalid("vavr"))); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void shouldThrowWhenProvidedWithNull() { |
| 224 | + assertThrows(NullPointerException.class, () -> Validation.cond(false, 1, null)); |
| 225 | + assertThrows(NullPointerException.class, () -> Validation.cond(false, null, 2)); |
| 226 | + } |
| 227 | + } |
| 228 | + |
130 | 229 | // -- Validation.sequence
|
131 | 230 |
|
132 | 231 | @Test
|
|
0 commit comments