Skip to content

Commit

Permalink
Merge pull request #140 from avaje/feature/tests-primitive-positive
Browse files Browse the repository at this point in the history
Fix Object Boolean assert, null is valid
  • Loading branch information
rbygrave authored Aug 9, 2023
2 parents 794e9b7 + 78366e2 commit 5ab4477
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
20 changes: 20 additions & 0 deletions blackbox-test/src/test/java/example/avaje/bool/ABoolFalse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package example.avaje.bool;

import io.avaje.validation.constraints.AssertFalse;
import io.avaje.validation.constraints.Valid;

@Valid
public record ABoolFalse(

@AssertFalse boolean primitive,
@AssertFalse Boolean object
) {

@AssertFalse public boolean primitiveMethod() {
return primitive;
}

@AssertFalse public Boolean objectMethod() {
return object;
}
}
38 changes: 38 additions & 0 deletions blackbox-test/src/test/java/example/avaje/bool/ABoolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package example.avaje.bool;

import io.avaje.validation.ConstraintViolation;
import io.avaje.validation.Validator;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class ABoolTest {

Validator validator = Validator.builder().build();

@Test
void valid() {
validator.validate(new ABoolTrue(true, null));
validator.validate(new ABoolTrue(true, true));
validator.validate(new ABoolFalse(false, null));
validator.validate(new ABoolFalse(false, false));
}

@Test
void invalidTrue() {
var violations = new ArrayList<>(validator.check(new ABoolTrue(false, false)));
violations.stream()
.map(ConstraintViolation::message)
.forEach(msg -> assertThat(msg).isEqualTo("must be true"));
}

@Test
void invalidFalse() {
var violations = new ArrayList<>(validator.check(new ABoolFalse(true, true)));
violations.stream()
.map(ConstraintViolation::message)
.forEach(msg -> assertThat(msg).isEqualTo("must be false"));
}
}
19 changes: 19 additions & 0 deletions blackbox-test/src/test/java/example/avaje/bool/ABoolTrue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package example.avaje.bool;

import io.avaje.validation.constraints.AssertTrue;
import io.avaje.validation.constraints.Valid;

@Valid
public record ABoolTrue(
@AssertTrue boolean primitive,
@AssertTrue Boolean object
) {

@AssertTrue public boolean primitiveMethod() {
return primitive;
}

@AssertTrue public Boolean objectMethod() {
return object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ private static final class AssertBooleanAdapter extends PrimitiveAdapter<Boolean
}

@Override
public boolean isValid(Boolean type) {
return !assertBool && type == null || type != null && assertBool == type.booleanValue();
public boolean isValid(Boolean value) {
return value == null || assertBool == value.booleanValue();
}

@Override
public boolean isValid(boolean type) {
return assertBool == type;
public boolean isValid(boolean value) {
return assertBool == value;
}
}

Expand Down

0 comments on commit 5ab4477

Please sign in to comment.