Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Object Boolean assert, null is valid #140

Merged
merged 2 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading