diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/constraint/JavaDecimalConstraint.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/constraint/JavaDecimalConstraint.java index 5a816ceac..c41152eea 100644 --- a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/constraint/JavaDecimalConstraint.java +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/constraint/JavaDecimalConstraint.java @@ -28,92 +28,52 @@ @API(since = "0.6.8", status = Status.MAINTAINED) public final class JavaDecimalConstraint { @Nullable - private final BigDecimal positiveMin; + private final BigDecimal min; @Nullable - private final Boolean positiveMinInclusive; + private final Boolean minInclusive; @Nullable - private final BigDecimal positiveMax; + private final BigDecimal max; @Nullable - private final Boolean positiveMaxInclusive; - - @Nullable - private final BigDecimal negativeMin; - - @Nullable - private final Boolean negativeMinInclusive; - - @Nullable - private final BigDecimal negativeMax; - - @Nullable - private final Boolean negativeMaxInclusive; + private final Boolean maxInclusive; @Nullable private final Integer scale; public JavaDecimalConstraint( - @Nullable BigDecimal positiveMin, - @Nullable Boolean positiveMinInclusive, - @Nullable BigDecimal positiveMax, - @Nullable Boolean positiveMaxInclusive, - @Nullable BigDecimal negativeMin, - @Nullable Boolean negativeMinInclusive, - @Nullable BigDecimal negativeMax, - @Nullable Boolean negativeMaxInclusive, + @Nullable BigDecimal min, + @Nullable Boolean minInclusive, + @Nullable BigDecimal max, + @Nullable Boolean maxInclusive, @Nullable Integer scale ) { - this.positiveMin = positiveMin; - this.positiveMinInclusive = positiveMinInclusive; - this.positiveMax = positiveMax; - this.positiveMaxInclusive = positiveMaxInclusive; - this.negativeMin = negativeMin; - this.negativeMinInclusive = negativeMinInclusive; - this.negativeMax = negativeMax; - this.negativeMaxInclusive = negativeMaxInclusive; + this.min = min; + this.minInclusive = minInclusive; + this.max = max; + this.maxInclusive = maxInclusive; this.scale = scale; } @Nullable - public BigDecimal getPositiveMin() { - return positiveMin; - } - - @Nullable - public Boolean getPositiveMinInclusive() { - return positiveMinInclusive; - } - - @Nullable - public BigDecimal getPositiveMax() { - return positiveMax; - } - - @Nullable - public Boolean getPositiveMaxInclusive() { - return positiveMaxInclusive; - } - - @Nullable - public BigDecimal getNegativeMin() { - return negativeMin; + public BigDecimal getMin() { + return min; } @Nullable - public Boolean getNegativeMinInclusive() { - return negativeMinInclusive; + public Boolean getMinInclusive() { + return minInclusive; } @Nullable - public BigDecimal getNegativeMax() { - return negativeMax; + public BigDecimal getMax() { + return max; } @Nullable - public Boolean getNegativeMaxInclusive() { - return negativeMaxInclusive; + public Boolean getMaxInclusive() { + return maxInclusive; } @Nullable diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/generator/ValidateArbitraryGenerator.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/generator/ValidateArbitraryGenerator.java index 536ba72b2..afb667ee1 100644 --- a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/generator/ValidateArbitraryGenerator.java +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/generator/ValidateArbitraryGenerator.java @@ -133,52 +133,22 @@ public CombinableArbitrary generate(ArbitraryGeneratorContext context) { BigDecimal value = toBigDecimal(it); - if (value.compareTo(BigDecimal.ZERO) < 0) { - if (javaDecimalConstraint.getNegativeMin() != null) { - if (value.compareTo(javaDecimalConstraint.getNegativeMin()) == 0 - && Boolean.FALSE.equals(javaDecimalConstraint.getNegativeMinInclusive())) { - return false; - } - - if (value.compareTo(javaDecimalConstraint.getNegativeMin()) < 0) { - return false; - } + if (javaDecimalConstraint.getMin() != null) { + if (value.compareTo(javaDecimalConstraint.getMin()) == 0 + && Boolean.FALSE.equals(javaDecimalConstraint.getMinInclusive())) { + return false; } - if (javaDecimalConstraint.getNegativeMax() != null) { - if (value.compareTo(javaDecimalConstraint.getNegativeMax()) == 0 - && Boolean.FALSE.equals(javaDecimalConstraint.getNegativeMaxInclusive())) { - return false; - } - - if (value.compareTo(javaDecimalConstraint.getNegativeMax()) > 0) { - return false; - } - } + return value.compareTo(javaDecimalConstraint.getMin()) >= 0; } - if (value.compareTo(BigDecimal.ZERO) > 0) { - if (javaDecimalConstraint.getPositiveMin() != null) { - if (value.compareTo(javaDecimalConstraint.getPositiveMin()) == 0 - && Boolean.FALSE.equals(javaDecimalConstraint.getPositiveMinInclusive())) { - return false; - } - - if (value.compareTo(javaDecimalConstraint.getPositiveMin()) < 0) { - return false; - } + if (javaDecimalConstraint.getMax() != null) { + if (value.compareTo(javaDecimalConstraint.getMax()) == 0 + && Boolean.FALSE.equals(javaDecimalConstraint.getMaxInclusive())) { + return false; } - if (javaDecimalConstraint.getPositiveMax() != null) { - if (value.compareTo(javaDecimalConstraint.getPositiveMax()) == 0 - && Boolean.FALSE.equals(javaDecimalConstraint.getPositiveMaxInclusive())) { - return false; - } - - if (value.compareTo(javaDecimalConstraint.getPositiveMax()) > 0) { - return false; - } - } + return value.compareTo(javaDecimalConstraint.getMax()) <= 0; } return true; diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/jqwik/JqwikJavaArbitraryResolver.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/jqwik/JqwikJavaArbitraryResolver.java index d808da8c8..0ed57f4e3 100644 --- a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/jqwik/JqwikJavaArbitraryResolver.java +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/jqwik/JqwikJavaArbitraryResolver.java @@ -207,61 +207,40 @@ public Arbitrary floats( return floatArbitrary; } - BigDecimal positiveMin = constraint.getPositiveMin(); - BigDecimal positiveMax = constraint.getPositiveMax(); - Boolean positiveMinInclusive = constraint.getPositiveMinInclusive(); - Boolean positiveMaxInclusive = constraint.getPositiveMaxInclusive(); - BigDecimal negativeMin = constraint.getNegativeMin(); - BigDecimal negativeMax = constraint.getNegativeMax(); - Boolean negativeMinInclusive = constraint.getNegativeMinInclusive(); - Boolean negativeMaxInclusive = constraint.getNegativeMaxInclusive(); + BigDecimal min = constraint.getMin(); + Boolean minInclusive = constraint.getMinInclusive(); + BigDecimal max = constraint.getMax(); + Boolean maxInclusive = constraint.getMaxInclusive(); Integer scale = constraint.getScale(); - FloatArbitrary positiveArbitrary = null; - FloatArbitrary negativeArbitrary = null; - if (positiveMin != null) { - positiveArbitrary = Types.defaultIfNull(positiveArbitrary, () -> floatArbitrary); - if (positiveMinInclusive != null && positiveMinInclusive) { - positiveArbitrary = positiveArbitrary.greaterOrEqual(positiveMin.floatValue()); - } else { - positiveArbitrary = positiveArbitrary.greaterThan(positiveMin.floatValue()); - } - } - if (positiveMax != null) { - positiveArbitrary = Types.defaultIfNull(positiveArbitrary, () -> floatArbitrary); - if (positiveMaxInclusive != null && positiveMaxInclusive) { - positiveArbitrary = positiveArbitrary.lessOrEqual(positiveMax.floatValue()); - } else { - positiveArbitrary = positiveArbitrary.lessThan(positiveMax.floatValue()); - } - } - if (negativeMin != null) { - negativeArbitrary = Types.defaultIfNull(negativeArbitrary, () -> floatArbitrary); - if (negativeMinInclusive != null && negativeMinInclusive) { - negativeArbitrary = negativeArbitrary.greaterOrEqual(negativeMin.floatValue()); + FloatArbitrary arbitrary = null; + + if (min != null) { + arbitrary = Types.defaultIfNull(arbitrary, () -> floatArbitrary); + if (minInclusive == null || minInclusive) { + arbitrary = arbitrary.greaterOrEqual(min.floatValue()); } else { - negativeArbitrary = negativeArbitrary.greaterThan(negativeMin.floatValue()); + arbitrary = arbitrary.greaterThan(min.floatValue()); } } - if (negativeMax != null) { - negativeArbitrary = Types.defaultIfNull(negativeArbitrary, () -> floatArbitrary); - if (negativeMaxInclusive != null && negativeMaxInclusive) { - negativeArbitrary = negativeArbitrary.lessOrEqual(negativeMax.floatValue()); + if (max != null) { + arbitrary = Types.defaultIfNull(arbitrary, () -> floatArbitrary); + if (maxInclusive == null || maxInclusive) { + arbitrary = arbitrary.lessOrEqual(max.floatValue()); } else { - negativeArbitrary = negativeArbitrary.lessThan(negativeMax.floatValue()); + arbitrary = arbitrary.lessThan(max.floatValue()); } } - if (scale != null) { - if (positiveArbitrary != null) { - positiveArbitrary = positiveArbitrary.ofScale(scale); - } - if (negativeArbitrary != null) { - negativeArbitrary = negativeArbitrary.ofScale(scale); + if (arbitrary != null) { + arbitrary = arbitrary.ofScale(scale); } } - return resolveArbitrary(floatArbitrary, positiveArbitrary, negativeArbitrary); + if (arbitrary == null) { + return floatArbitrary; + } + return arbitrary; } @Override @@ -274,61 +253,40 @@ public Arbitrary doubles( return doubleArbitrary; } - BigDecimal positiveMin = constraint.getPositiveMin(); - BigDecimal positiveMax = constraint.getPositiveMax(); - Boolean positiveMinInclusive = constraint.getPositiveMinInclusive(); - Boolean positiveMaxInclusive = constraint.getPositiveMaxInclusive(); - BigDecimal negativeMin = constraint.getNegativeMin(); - BigDecimal negativeMax = constraint.getNegativeMax(); - Boolean negativeMinInclusive = constraint.getNegativeMinInclusive(); - Boolean negativeMaxInclusive = constraint.getNegativeMaxInclusive(); + BigDecimal min = constraint.getMin(); + Boolean minInclusive = constraint.getMinInclusive(); + BigDecimal max = constraint.getMax(); + Boolean maxInclusive = constraint.getMaxInclusive(); Integer scale = constraint.getScale(); - DoubleArbitrary positiveArbitrary = null; - DoubleArbitrary negativeArbitrary = null; - if (positiveMin != null) { - positiveArbitrary = Types.defaultIfNull(positiveArbitrary, () -> doubleArbitrary); - if (positiveMinInclusive != null && positiveMinInclusive) { - positiveArbitrary = positiveArbitrary.greaterOrEqual(positiveMin.doubleValue()); - } else { - positiveArbitrary = positiveArbitrary.greaterThan(positiveMin.doubleValue()); - } - } - if (positiveMax != null) { - positiveArbitrary = Types.defaultIfNull(positiveArbitrary, () -> doubleArbitrary); - if (positiveMaxInclusive != null && positiveMaxInclusive) { - positiveArbitrary = positiveArbitrary.lessOrEqual(positiveMax.doubleValue()); - } else { - positiveArbitrary = positiveArbitrary.lessThan(positiveMax.doubleValue()); - } - } - if (negativeMin != null) { - negativeArbitrary = Types.defaultIfNull(negativeArbitrary, () -> doubleArbitrary); - if (negativeMinInclusive != null && negativeMinInclusive) { - negativeArbitrary = negativeArbitrary.greaterOrEqual(negativeMin.floatValue()); + DoubleArbitrary arbitrary = null; + + if (min != null) { + arbitrary = Types.defaultIfNull(arbitrary, () -> doubleArbitrary); + if (minInclusive == null || minInclusive) { + arbitrary = arbitrary.greaterOrEqual(min.doubleValue()); } else { - negativeArbitrary = negativeArbitrary.greaterThan(negativeMin.floatValue()); + arbitrary = arbitrary.greaterThan(min.doubleValue()); } } - if (negativeMax != null) { - negativeArbitrary = Types.defaultIfNull(negativeArbitrary, () -> doubleArbitrary); - if (negativeMaxInclusive != null && negativeMaxInclusive) { - negativeArbitrary = negativeArbitrary.lessOrEqual(negativeMax.doubleValue()); + if (max != null) { + arbitrary = Types.defaultIfNull(arbitrary, () -> doubleArbitrary); + if (maxInclusive == null || maxInclusive) { + arbitrary = arbitrary.lessOrEqual(max.doubleValue()); } else { - negativeArbitrary = negativeArbitrary.lessThan(negativeMax.doubleValue()); + arbitrary = arbitrary.lessThan(max.doubleValue()); } } - if (scale != null) { - if (positiveArbitrary != null) { - positiveArbitrary = positiveArbitrary.ofScale(scale); - } - if (negativeArbitrary != null) { - negativeArbitrary = negativeArbitrary.ofScale(scale); + if (arbitrary != null) { + arbitrary = arbitrary.ofScale(scale); } } - return resolveArbitrary(doubleArbitrary, positiveArbitrary, negativeArbitrary); + if (arbitrary == null) { + return doubleArbitrary; + } + return arbitrary; } @Override @@ -450,62 +408,40 @@ public Arbitrary bigDecimals( return bigDecimalArbitrary; } - BigDecimal positiveMin = constraint.getPositiveMin(); - BigDecimal positiveMax = constraint.getPositiveMax(); - Boolean positiveMinInclusive = constraint.getPositiveMinInclusive(); - Boolean positiveMaxInclusive = constraint.getPositiveMaxInclusive(); - BigDecimal negativeMin = constraint.getNegativeMin(); - BigDecimal negativeMax = constraint.getNegativeMax(); - Boolean negativeMinInclusive = constraint.getNegativeMinInclusive(); - Boolean negativeMaxInclusive = constraint.getNegativeMaxInclusive(); + BigDecimal min = constraint.getMin(); + Boolean minInclusive = constraint.getMinInclusive(); + BigDecimal max = constraint.getMax(); + Boolean maxInclusive = constraint.getMaxInclusive(); Integer scale = constraint.getScale(); - BigDecimalArbitrary positiveArbitrary = null; - BigDecimalArbitrary negativeArbitrary = null; - if (positiveMin != null) { - positiveArbitrary = Types.defaultIfNull(positiveArbitrary, () -> bigDecimalArbitrary); - if (positiveMinInclusive != null && positiveMinInclusive) { - positiveArbitrary = positiveArbitrary.greaterOrEqual(positiveMin); - } else { - positiveArbitrary = positiveArbitrary.greaterThan(positiveMin); - } - } - if (positiveMax != null) { - positiveArbitrary = Types.defaultIfNull(positiveArbitrary, () -> bigDecimalArbitrary); - if (positiveMaxInclusive != null && positiveMaxInclusive) { - positiveArbitrary = positiveArbitrary.lessOrEqual(positiveMax); - } else { - positiveArbitrary = positiveArbitrary.lessThan(positiveMax); - } - } + BigDecimalArbitrary arbitrary = null; - if (negativeMin != null) { - negativeArbitrary = Types.defaultIfNull(negativeArbitrary, () -> bigDecimalArbitrary); - if (negativeMinInclusive != null && negativeMinInclusive) { - negativeArbitrary = negativeArbitrary.greaterOrEqual(negativeMin); + if (min != null) { + arbitrary = Types.defaultIfNull(arbitrary, () -> bigDecimalArbitrary); + if (minInclusive == null || minInclusive) { + arbitrary = arbitrary.greaterOrEqual(min); } else { - negativeArbitrary = negativeArbitrary.greaterThan(negativeMin); + arbitrary = arbitrary.greaterThan(min); } } - if (negativeMax != null) { - negativeArbitrary = Types.defaultIfNull(negativeArbitrary, () -> bigDecimalArbitrary); - if (negativeMaxInclusive != null && negativeMaxInclusive) { - negativeArbitrary = negativeArbitrary.lessOrEqual(negativeMax); + if (max != null) { + arbitrary = Types.defaultIfNull(arbitrary, () -> bigDecimalArbitrary); + if (maxInclusive == null || maxInclusive) { + arbitrary = arbitrary.lessOrEqual(max); } else { - negativeArbitrary = negativeArbitrary.lessThan(negativeMax); + arbitrary = arbitrary.lessThan(max); } } - if (scale != null) { - if (positiveArbitrary != null) { - positiveArbitrary = positiveArbitrary.ofScale(scale); - } - if (negativeArbitrary != null) { - negativeArbitrary = negativeArbitrary.ofScale(scale); + if (arbitrary != null) { + arbitrary = arbitrary.ofScale(scale); } } - return resolveArbitrary(bigDecimalArbitrary, positiveArbitrary, negativeArbitrary); + if (arbitrary == null) { + return bigDecimalArbitrary; + } + return arbitrary; } private static Arbitrary resolveArbitrary( diff --git a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/plugin/SimpleValueJqwikPlugin.java b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/plugin/SimpleValueJqwikPlugin.java index 85f9f146d..c58120bf2 100644 --- a/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/plugin/SimpleValueJqwikPlugin.java +++ b/fixture-monkey-api/src/main/java/com/navercorp/fixturemonkey/api/plugin/SimpleValueJqwikPlugin.java @@ -262,32 +262,15 @@ public JavaIntegerConstraint generateIntegerConstraint(ArbitraryGeneratorContext @Override public JavaDecimalConstraint generateDecimalConstraint(ArbitraryGeneratorContext context) { - BigDecimal positiveMin = ifNotNull(this.positiveMinNumberValue, BigDecimal::valueOf); - BigDecimal positiveMax = ifNotNull(this.positiveMaxNumberValue, BigDecimal::valueOf); - BigDecimal negativeMin = ifNotNull(this.negativeMinNumberValue, BigDecimal::valueOf); - BigDecimal negativeMax = ifNotNull(this.negativeMaxNumberValue, BigDecimal::valueOf); + BigDecimal min = negativeMinNumberValue != null + ? BigDecimal.valueOf(negativeMinNumberValue) : + BigDecimal.valueOf(defaultIfNull(positiveMinNumberValue, () -> DEFAULT_MIN_NUMBER_VALUE)); - if (positiveMin == null) { - negativeMin = defaultIfNull(negativeMin, () -> BigDecimal.valueOf(DEFAULT_MIN_NUMBER_VALUE)); - negativeMax = defaultIfNull(negativeMax, () -> BigDecimal.ZERO); - } - - if (negativeMax == null) { - positiveMin = defaultIfNull(positiveMin, () -> BigDecimal.ZERO); - positiveMax = defaultIfNull(positiveMax, () -> BigDecimal.valueOf(DEFAULT_MAX_NUMBER_VALUE)); - } + BigDecimal max = positiveMaxNumberValue != null + ? BigDecimal.valueOf(positiveMaxNumberValue) : + BigDecimal.valueOf(defaultIfNull(negativeMaxNumberValue, () -> DEFAULT_MAX_NUMBER_VALUE)); - return new JavaDecimalConstraint( - positiveMin, - true, - positiveMax, - true, - negativeMin, - true, - negativeMax, - true, - 2 - ); + return new JavaDecimalConstraint(min, true, max, true, 2); } @Override diff --git a/fixture-monkey-jakarta-validation/src/main/java/com/navercorp/fixturemonkey/jakarta/validation/introspector/JakartaValidationConstraintGenerator.java b/fixture-monkey-jakarta-validation/src/main/java/com/navercorp/fixturemonkey/jakarta/validation/introspector/JakartaValidationConstraintGenerator.java index cdbc76a86..b4181518e 100644 --- a/fixture-monkey-jakarta-validation/src/main/java/com/navercorp/fixturemonkey/jakarta/validation/introspector/JakartaValidationConstraintGenerator.java +++ b/fixture-monkey-jakarta-validation/src/main/java/com/navercorp/fixturemonkey/jakarta/validation/introspector/JakartaValidationConstraintGenerator.java @@ -292,180 +292,114 @@ public JavaIntegerConstraint generateIntegerConstraint(ArbitraryGeneratorContext @Override @Nullable public JavaDecimalConstraint generateDecimalConstraint(ArbitraryGeneratorContext context) { - BigDecimal positiveMin = null; - Boolean positiveMinInclusive = null; - BigDecimal positiveMax = null; - Boolean positiveMaxInclusive = null; - BigDecimal negativeMin = null; - Boolean negativeMinInclusive = null; - BigDecimal negativeMax = null; - boolean negativeMaxInclusive = false; + BigDecimal min = null; + Boolean minInclusive = null; + BigDecimal max = null; + Boolean maxInclusive = null; Integer scale = null; - Optional digits = context.findAnnotation(Digits.class); - if (digits.isPresent()) { - BigDecimal value = BigDecimal.ONE; - int integer = digits.get().integer(); - if (integer > 1) { - value = BigDecimal.TEN.pow(integer - 1); - } - positiveMax = value.multiply(BigDecimal.TEN).subtract(BigDecimal.ONE); - positiveMin = value; - negativeMax = positiveMin.negate(); - negativeMin = positiveMax.negate(); - positiveMinInclusive = false; - negativeMinInclusive = false; - scale = digits.get().fraction(); - } - Optional minAnnotation = context.findAnnotation(Min.class); if (minAnnotation.isPresent()) { - BigDecimal minValue = minAnnotation.map(Min::value).map(BigDecimal::valueOf).get(); - if (minValue.compareTo(BigDecimal.ZERO) >= 0) { - if (positiveMin == null) { - positiveMin = minValue; - } else { - positiveMin = positiveMin.min(minValue); - } - negativeMax = null; - negativeMin = null; - } else { - if (negativeMin == null) { - negativeMin = minValue; - } else { - negativeMin = negativeMin.min(minValue); - } - negativeMinInclusive = true; - } + min = BigDecimal.valueOf(minAnnotation.get().value()); + minInclusive = true; } Optional decimalMinAnnotation = context.findAnnotation(DecimalMin.class); if (decimalMinAnnotation.isPresent()) { - BigDecimal decimalMin = new BigDecimal( - decimalMinAnnotation - .get() - .value() - ); + BigDecimal newMin = new BigDecimal(decimalMinAnnotation.get().value()); - if (decimalMin.compareTo(BigDecimal.ZERO) >= 0) { - if (positiveMin == null) { - positiveMin = decimalMin; - } else { - positiveMin = positiveMin.min(decimalMin); - } - if (!decimalMinAnnotation.map(DecimalMin::inclusive).get()) { - positiveMinInclusive = false; - } - negativeMax = null; - negativeMin = null; - } else { - if (negativeMin == null) { - negativeMin = decimalMin; - } else { - negativeMin = negativeMin.min(negativeMin); - } - if (!decimalMinAnnotation.map(DecimalMin::inclusive).get()) { - negativeMinInclusive = false; - } + if (min == null || newMin.compareTo(min) > 0 + || (newMin.compareTo(min) == 0 && !decimalMinAnnotation.get().inclusive())) { + + min = newMin; + minInclusive = decimalMinAnnotation.get().inclusive(); } } Optional maxAnnotation = context.findAnnotation(Max.class); if (maxAnnotation.isPresent()) { - BigDecimal maxValue = maxAnnotation.map(Max::value).map(BigDecimal::valueOf).get(); - if (maxValue.compareTo(BigDecimal.ZERO) > 0) { - if (positiveMax == null) { - positiveMax = maxValue; - } else { - positiveMax = positiveMax.max(maxValue); - } - } else { - if (negativeMax == null) { - negativeMax = maxValue; - } else { - negativeMax = negativeMax.max(maxValue); - } - } + max = BigDecimal.valueOf(maxAnnotation.get().value()); + maxInclusive = true; } Optional decimalMaxAnnotation = context.findAnnotation(DecimalMax.class); if (decimalMaxAnnotation.isPresent()) { - BigDecimal decimalMax = new BigDecimal( - decimalMaxAnnotation - .get() - .value() - ); + BigDecimal newMax = new BigDecimal(decimalMaxAnnotation.get().value()); - if (decimalMax.compareTo(BigDecimal.ZERO) > 0) { - if (positiveMax == null) { - positiveMax = decimalMax; - } else { - positiveMax = positiveMax.max(decimalMax); - } - positiveMaxInclusive = decimalMaxAnnotation.map(DecimalMax::inclusive).get(); - } else { - if (negativeMax == null) { - negativeMax = decimalMax; - } else { - negativeMax = negativeMax.max(decimalMax); - } - negativeMaxInclusive = decimalMaxAnnotation.map(DecimalMax::inclusive).get(); + if (max == null || newMax.compareTo(max) < 0 + || (newMax.compareTo(max) == 0 && !decimalMaxAnnotation.get().inclusive())) { + + max = newMax; + maxInclusive = decimalMaxAnnotation.get().inclusive(); } + } - if (!decimalMaxAnnotation.map(DecimalMax::inclusive).get()) { - positiveMaxInclusive = false; + if (context.findAnnotation(Positive.class).isPresent()) { + if (min == null || BigDecimal.ZERO.compareTo(min) > 0 + || (BigDecimal.ZERO.compareTo(min) == 0 && minInclusive)) { + min = BigDecimal.ZERO; + minInclusive = false; } + } - if (positiveMax == null) { - positiveMax = decimalMax; - } else if (positiveMax.compareTo(decimalMax) > 0) { - positiveMax = decimalMax; + if (context.findAnnotation(PositiveOrZero.class).isPresent()) { + if (min == null || BigDecimal.ZERO.compareTo(min) > 0) { + min = BigDecimal.ZERO; + minInclusive = true; } } if (context.findAnnotation(Negative.class).isPresent()) { - if (negativeMax == null || negativeMax.compareTo(BigDecimal.ZERO) > 0) { - negativeMax = BigDecimal.ZERO; - negativeMaxInclusive = false; + if (max == null || BigDecimal.ZERO.compareTo(max) < 0 + || (BigDecimal.ZERO.compareTo(max) == 0 && maxInclusive)) { + max = BigDecimal.ZERO; + maxInclusive = false; } } if (context.findAnnotation(NegativeOrZero.class).isPresent()) { - if (negativeMax == null || negativeMax.compareTo(BigDecimal.ZERO) > 0) { - negativeMax = BigDecimal.ZERO; - negativeMaxInclusive = true; + if (max == null || BigDecimal.ZERO.compareTo(max) < 0) { + max = BigDecimal.ZERO; + maxInclusive = true; } } - if (context.findAnnotation(Positive.class).isPresent()) { - if (positiveMin == null || positiveMin.compareTo(BigDecimal.ZERO) < 0) { - positiveMin = BigDecimal.ZERO; - positiveMinInclusive = false; + Optional digitsAnn = context.findAnnotation(Digits.class); + if (digitsAnn.isPresent()) { + Digits digits = digitsAnn.get(); + int integerDigits = digits.integer(); + int fractionDigits = digits.fraction(); + + StringBuilder maxBuilder = new StringBuilder(); + for (int i = 0; i < integerDigits; i++) { + maxBuilder.append('9'); } - } + if (fractionDigits > 0) { + maxBuilder.append('.'); + for (int i = 0; i < fractionDigits; i++) { + maxBuilder.append('9'); + } + } + BigDecimal digitsMax = new BigDecimal(maxBuilder.toString()); + BigDecimal digitsMin = digitsMax.negate(); - if (context.findAnnotation(PositiveOrZero.class).isPresent()) { - if (positiveMin == null || positiveMin.compareTo(BigDecimal.ZERO) < 0) { - positiveMin = BigDecimal.ZERO; - positiveMinInclusive = true; + if (max == null || digitsMax.compareTo(max) < 0) { + max = digitsMax; + maxInclusive = true; + } + if (min == null || digitsMin.compareTo(min) > 0) { + min = digitsMin; + minInclusive = true; } + + scale = digits.fraction(); } - if (positiveMin == null && positiveMax == null && negativeMin == null && negativeMax == null && scale == null) { + if (min == null && max == null) { return null; } - return new JavaDecimalConstraint( - positiveMin, - positiveMinInclusive, - positiveMax, - positiveMaxInclusive, - negativeMin, - negativeMinInclusive, - negativeMax, - negativeMaxInclusive, - scale - ); + return new JavaDecimalConstraint(min, minInclusive, max, maxInclusive, scale); } @Override diff --git a/fixture-monkey-javax-validation/src/main/java/com/navercorp/fixturemonkey/javax/validation/introspector/JavaxValidationConstraintGenerator.java b/fixture-monkey-javax-validation/src/main/java/com/navercorp/fixturemonkey/javax/validation/introspector/JavaxValidationConstraintGenerator.java index 289267ad7..070c67aac 100644 --- a/fixture-monkey-javax-validation/src/main/java/com/navercorp/fixturemonkey/javax/validation/introspector/JavaxValidationConstraintGenerator.java +++ b/fixture-monkey-javax-validation/src/main/java/com/navercorp/fixturemonkey/javax/validation/introspector/JavaxValidationConstraintGenerator.java @@ -291,180 +291,114 @@ public JavaIntegerConstraint generateIntegerConstraint(ArbitraryGeneratorContext @Override @Nullable public JavaDecimalConstraint generateDecimalConstraint(ArbitraryGeneratorContext context) { - BigDecimal positiveMin = null; - Boolean positiveMinInclusive = null; - BigDecimal positiveMax = null; - Boolean positiveMaxInclusive = null; - BigDecimal negativeMin = null; - Boolean negativeMinInclusive = null; - BigDecimal negativeMax = null; - boolean negativeMaxInclusive = false; + BigDecimal min = null; + Boolean minInclusive = null; + BigDecimal max = null; + Boolean maxInclusive = null; Integer scale = null; - Optional digits = context.findAnnotation(Digits.class); - if (digits.isPresent()) { - BigDecimal value = BigDecimal.ONE; - int integer = digits.get().integer(); - if (integer > 1) { - value = BigDecimal.TEN.pow(integer - 1); - } - positiveMax = value.multiply(BigDecimal.TEN).subtract(BigDecimal.ONE); - positiveMin = value; - negativeMax = positiveMin.negate(); - negativeMin = positiveMax.negate(); - positiveMinInclusive = false; - negativeMinInclusive = false; - scale = digits.get().fraction(); - } - Optional minAnnotation = context.findAnnotation(Min.class); if (minAnnotation.isPresent()) { - BigDecimal minValue = minAnnotation.map(Min::value).map(BigDecimal::valueOf).get(); - if (minValue.compareTo(BigDecimal.ZERO) >= 0) { - if (positiveMin == null) { - positiveMin = minValue; - } else { - positiveMin = positiveMin.min(minValue); - } - negativeMax = null; - negativeMin = null; - } else { - if (negativeMin == null) { - negativeMin = minValue; - } else { - negativeMin = negativeMin.min(minValue); - } - negativeMinInclusive = true; - } + min = BigDecimal.valueOf(minAnnotation.get().value()); + minInclusive = true; } Optional decimalMinAnnotation = context.findAnnotation(DecimalMin.class); if (decimalMinAnnotation.isPresent()) { - BigDecimal decimalMin = new BigDecimal( - decimalMinAnnotation - .get() - .value() - ); + BigDecimal newMin = new BigDecimal(decimalMinAnnotation.get().value()); - if (decimalMin.compareTo(BigDecimal.ZERO) >= 0) { - if (positiveMin == null) { - positiveMin = decimalMin; - } else { - positiveMin = positiveMin.min(decimalMin); - } - if (!decimalMinAnnotation.map(DecimalMin::inclusive).get()) { - positiveMinInclusive = false; - } - negativeMax = null; - negativeMin = null; - } else { - if (negativeMin == null) { - negativeMin = decimalMin; - } else { - negativeMin = negativeMin.min(negativeMin); - } - if (!decimalMinAnnotation.map(DecimalMin::inclusive).get()) { - negativeMinInclusive = false; - } + if (min == null || newMin.compareTo(min) > 0 + || (newMin.compareTo(min) == 0 && !decimalMinAnnotation.get().inclusive())) { + + min = newMin; + minInclusive = decimalMinAnnotation.get().inclusive(); } } Optional maxAnnotation = context.findAnnotation(Max.class); if (maxAnnotation.isPresent()) { - BigDecimal maxValue = maxAnnotation.map(Max::value).map(BigDecimal::valueOf).get(); - if (maxValue.compareTo(BigDecimal.ZERO) > 0) { - if (positiveMax == null) { - positiveMax = maxValue; - } else { - positiveMax = positiveMax.max(maxValue); - } - } else { - if (negativeMax == null) { - negativeMax = maxValue; - } else { - negativeMax = negativeMax.max(maxValue); - } - } + max = BigDecimal.valueOf(maxAnnotation.get().value()); + maxInclusive = true; } Optional decimalMaxAnnotation = context.findAnnotation(DecimalMax.class); if (decimalMaxAnnotation.isPresent()) { - BigDecimal decimalMax = new BigDecimal( - decimalMaxAnnotation - .get() - .value() - ); + BigDecimal newMax = new BigDecimal(decimalMaxAnnotation.get().value()); - if (decimalMax.compareTo(BigDecimal.ZERO) > 0) { - if (positiveMax == null) { - positiveMax = decimalMax; - } else { - positiveMax = positiveMax.max(decimalMax); - } - positiveMaxInclusive = decimalMaxAnnotation.map(DecimalMax::inclusive).get(); - } else { - if (negativeMax == null) { - negativeMax = decimalMax; - } else { - negativeMax = negativeMax.max(decimalMax); - } - negativeMaxInclusive = decimalMaxAnnotation.map(DecimalMax::inclusive).get(); + if (max == null || newMax.compareTo(max) < 0 + || (newMax.compareTo(max) == 0 && !decimalMaxAnnotation.get().inclusive())) { + + max = newMax; + maxInclusive = decimalMaxAnnotation.get().inclusive(); } + } - if (!decimalMaxAnnotation.map(DecimalMax::inclusive).get()) { - positiveMaxInclusive = false; + if (context.findAnnotation(Positive.class).isPresent()) { + if (min == null || BigDecimal.ZERO.compareTo(min) > 0 + || (BigDecimal.ZERO.compareTo(min) == 0 && minInclusive)) { + min = BigDecimal.ZERO; + minInclusive = false; } + } - if (positiveMax == null) { - positiveMax = decimalMax; - } else if (positiveMax.compareTo(decimalMax) > 0) { - positiveMax = decimalMax; + if (context.findAnnotation(PositiveOrZero.class).isPresent()) { + if (min == null || BigDecimal.ZERO.compareTo(min) > 0) { + min = BigDecimal.ZERO; + minInclusive = true; } } if (context.findAnnotation(Negative.class).isPresent()) { - if (negativeMax == null || negativeMax.compareTo(BigDecimal.ZERO) > 0) { - negativeMax = BigDecimal.ZERO; - negativeMaxInclusive = false; + if (max == null || BigDecimal.ZERO.compareTo(max) < 0 + || (BigDecimal.ZERO.compareTo(max) == 0 && maxInclusive)) { + max = BigDecimal.ZERO; + maxInclusive = false; } } if (context.findAnnotation(NegativeOrZero.class).isPresent()) { - if (negativeMax == null || negativeMax.compareTo(BigDecimal.ZERO) > 0) { - negativeMax = BigDecimal.ZERO; - negativeMaxInclusive = true; + if (max == null || BigDecimal.ZERO.compareTo(max) < 0) { + max = BigDecimal.ZERO; + maxInclusive = true; } } - if (context.findAnnotation(Positive.class).isPresent()) { - if (positiveMin == null || positiveMin.compareTo(BigDecimal.ZERO) < 0) { - positiveMin = BigDecimal.ZERO; - positiveMinInclusive = false; + Optional digitsAnn = context.findAnnotation(Digits.class); + if (digitsAnn.isPresent()) { + Digits digits = digitsAnn.get(); + int integerDigits = digits.integer(); + int fractionDigits = digits.fraction(); + + StringBuilder maxBuilder = new StringBuilder(); + for (int i = 0; i < integerDigits; i++) { + maxBuilder.append('9'); } - } + if (fractionDigits > 0) { + maxBuilder.append('.'); + for (int i = 0; i < fractionDigits; i++) { + maxBuilder.append('9'); + } + } + BigDecimal digitsMax = new BigDecimal(maxBuilder.toString()); + BigDecimal digitsMin = digitsMax.negate(); - if (context.findAnnotation(PositiveOrZero.class).isPresent()) { - if (positiveMin == null || positiveMin.compareTo(BigDecimal.ZERO) < 0) { - positiveMin = BigDecimal.ZERO; - positiveMinInclusive = true; + if (max == null || digitsMax.compareTo(max) < 0) { + max = digitsMax; + maxInclusive = true; + } + if (min == null || digitsMin.compareTo(min) > 0) { + min = digitsMin; + minInclusive = true; } + + scale = digits.fraction(); } - if (positiveMin == null && positiveMax == null && negativeMin == null && negativeMax == null && scale == null) { + if (min == null && max == null) { return null; } - return new JavaDecimalConstraint( - positiveMin, - positiveMinInclusive, - positiveMax, - positiveMaxInclusive, - negativeMin, - negativeMinInclusive, - negativeMax, - negativeMaxInclusive, - scale - ); + return new JavaDecimalConstraint(min, minInclusive, max, maxInclusive, scale); } @Override diff --git a/fixture-monkey-kotest/src/main/kotlin/com/navercorp/fixturemonkey/kotest/KotestArbitraryGeneratorSet.kt b/fixture-monkey-kotest/src/main/kotlin/com/navercorp/fixturemonkey/kotest/KotestArbitraryGeneratorSet.kt index 924c8c679..5f7c5c19c 100644 --- a/fixture-monkey-kotest/src/main/kotlin/com/navercorp/fixturemonkey/kotest/KotestArbitraryGeneratorSet.kt +++ b/fixture-monkey-kotest/src/main/kotlin/com/navercorp/fixturemonkey/kotest/KotestArbitraryGeneratorSet.kt @@ -29,6 +29,7 @@ import io.kotest.property.arbitrary.bigInt import io.kotest.property.arbitrary.byte import io.kotest.property.arbitrary.char import io.kotest.property.arbitrary.choice +import io.kotest.property.arbitrary.constant import io.kotest.property.arbitrary.double import io.kotest.property.arbitrary.duration import io.kotest.property.arbitrary.filter @@ -46,9 +47,9 @@ import io.kotest.property.arbitrary.short import io.kotest.property.arbitrary.single import io.kotest.property.arbitrary.string import io.kotest.property.arbitrary.yearMonth +import io.kotest.property.arbitrary.zonedDateTime import io.kotest.property.arbitrary.zoneId import io.kotest.property.arbitrary.zoneOffset -import io.kotest.property.arbitrary.zonedDateTime import org.apiguardian.api.API import org.apiguardian.api.API.Status import java.math.BigDecimal @@ -166,46 +167,27 @@ class KotestJavaArbitraryGeneratorSet( return CombinableArbitrary.from { if (decimalConstraint != null) { val scale = decimalConstraint.scale - // TODO: waits for update in Kotest - // val positiveMaxInclusive = decimalConstraint.positiveMaxInclusive ?: false - // val positiveMinInclusive = decimalConstraint.positiveMinInclusive ?: false - // val negativeMaxInclusive = decimalConstraint.negativeMaxInclusive ?: false - // val negativeMinInclusive = decimalConstraint.negativeMinInclusive ?: false - - val positiveArb = if (decimalConstraint.positiveMin != null || decimalConstraint.positiveMax != null) { - val positiveMinSize = - decimalConstraint.positiveMin?.toDouble() ?: 0.0 - val positiveMaxSize = decimalConstraint.positiveMax?.toDouble() ?: Double.MAX_VALUE - Arb.double(min = positiveMinSize, max = positiveMaxSize).map { - if (scale != null) { - it.ofScale(scale) - } else { - it - } - } + var min = decimalConstraint.min?.toDouble() ?: -Double.MAX_VALUE + var max = decimalConstraint.max?.toDouble() ?: Double.MAX_VALUE + + if (min == max && + (decimalConstraint.minInclusive ?: true) && + (decimalConstraint.maxInclusive ?: true) + ) { + Arb.constant(min) } else { - null - } - - val negativeArb = if (decimalConstraint.negativeMin != null || decimalConstraint.negativeMax != null) { - val negativeMinSize = decimalConstraint.negativeMin?.toDouble() ?: -Double.MIN_VALUE - val negativeMaxSize = decimalConstraint.negativeMax?.toDouble() ?: -0.0 - Arb.double(min = negativeMinSize, max = negativeMaxSize).map { - if (scale != null) { - it.ofScale(scale) - } else { - it + if (decimalConstraint.minInclusive == false) { + min += Double.MIN_VALUE + } + if (decimalConstraint.maxInclusive ?: true) { + if (max != Double.MAX_VALUE) { + max += Double.MIN_VALUE } } - } else { - null - } - if (positiveArb != null && negativeArb != null) { - Arb.choice(negativeArb, positiveArb) - } else { - negativeArb ?: positiveArb!! - }.single() + Arb.double(min = min, max = max) + }.map { if (scale != null) it.ofScale(scale) else it } + .single() } else { Arb.double().single() } @@ -218,46 +200,27 @@ class KotestJavaArbitraryGeneratorSet( return CombinableArbitrary.from { if (decimalConstraint != null) { val scale = decimalConstraint.scale - // TODO: waits for update in Kotest - // val positiveMaxInclusive = decimalConstraint.positiveMaxInclusive ?: false - // val positiveMinInclusive = decimalConstraint.positiveMinInclusive ?: false - // val negativeMaxInclusive = decimalConstraint.negativeMaxInclusive ?: false - // val negativeMinInclusive = decimalConstraint.negativeMinInclusive ?: false - - val positiveArb = if (decimalConstraint.positiveMin != null || decimalConstraint.positiveMax != null) { - val positiveMinSize = - decimalConstraint.positiveMin?.toFloat() ?: 0.0f - val positiveMaxSize = decimalConstraint.positiveMax?.toFloat() ?: Float.MAX_VALUE - Arb.float(min = positiveMinSize, max = positiveMaxSize).map { - if (scale != null) { - it.ofScale(scale) - } else { - it - } - } + var min = decimalConstraint.min?.toFloat() ?: -Float.MAX_VALUE + var max = decimalConstraint.max?.toFloat() ?: Float.MAX_VALUE + + if (min == max && + (decimalConstraint.minInclusive ?: true) && + (decimalConstraint.maxInclusive ?: true) + ) { + Arb.constant(min) } else { - null - } - - val negativeArb = if (decimalConstraint.negativeMin != null || decimalConstraint.negativeMax != null) { - val negativeMinSize = decimalConstraint.negativeMin?.toFloat() ?: -Float.MIN_VALUE - val negativeMaxSize = decimalConstraint.negativeMax?.toFloat() ?: -0.0f - Arb.float(min = negativeMinSize, max = negativeMaxSize).map { - if (scale != null) { - it.ofScale(scale) - } else { - it + if (decimalConstraint.minInclusive == false) { + min += Float.MIN_VALUE + } + if (decimalConstraint.maxInclusive ?: true) { + if (max != Float.MAX_VALUE) { + max += Float.MIN_VALUE } } - } else { - null - } - if (positiveArb != null && negativeArb != null) { - Arb.choice(negativeArb, positiveArb) - } else { - negativeArb ?: positiveArb!! - }.single() + Arb.float(min = min, max = max) + }.map { if (scale != null) it.ofScale(scale) else it } + .single() } else { Arb.float().single() } @@ -366,46 +329,27 @@ class KotestJavaArbitraryGeneratorSet( return CombinableArbitrary.from { if (decimalConstraint != null) { val scale = decimalConstraint.scale - // TODO: waits for update in Kotest - // val positiveMaxInclusive = decimalConstraint.positiveMaxInclusive ?: false - // val positiveMinInclusive = decimalConstraint.positiveMinInclusive ?: false - // val negativeMaxInclusive = decimalConstraint.negativeMaxInclusive ?: false - // val negativeMinInclusive = decimalConstraint.negativeMinInclusive ?: false - - val positiveArb = if (decimalConstraint.positiveMin != null || decimalConstraint.positiveMax != null) { - val positiveMinSize = - decimalConstraint.positiveMin ?: BigDecimal.ZERO - val positiveMaxSize = decimalConstraint.positiveMax ?: BigDecimal.valueOf(Double.MAX_VALUE) - Arb.bigDecimal(min = positiveMinSize, max = positiveMaxSize).map { - if (scale != null) { - it.setScale(scale, RoundingMode.DOWN) - } else { - it - } - } + var min = decimalConstraint.min ?: BigDecimal.valueOf(-Double.MAX_VALUE) + var max = decimalConstraint.max ?: BigDecimal.valueOf(Double.MAX_VALUE) + + if (min.compareTo(max) == 0 && + (decimalConstraint.minInclusive ?: true) && + (decimalConstraint.maxInclusive ?: true) + ) { + Arb.constant(min) } else { - null - } - - val negativeArb = if (decimalConstraint.negativeMin != null || decimalConstraint.negativeMax != null) { - val negativeMinSize = decimalConstraint.negativeMin ?: -BigDecimal.valueOf(Double.MIN_VALUE) - val negativeMaxSize = decimalConstraint.negativeMax ?: -BigDecimal.ZERO - Arb.bigDecimal(min = negativeMinSize, max = negativeMaxSize).map { - if (scale != null) { - it.setScale(scale, RoundingMode.DOWN) - } else { - it + if (decimalConstraint.minInclusive == false) { + min = min.add(BigDecimal.valueOf(Double.MIN_VALUE)) + } + if (decimalConstraint.maxInclusive ?: true) { + if (max != BigDecimal.valueOf(Double.MAX_VALUE)) { + max = max.add(BigDecimal.valueOf(Double.MIN_VALUE)) } } - } else { - null - } - if (positiveArb != null && negativeArb != null) { - Arb.choice(negativeArb, positiveArb) - } else { - negativeArb ?: positiveArb!! - }.single() + Arb.bigDecimal(min = min, max = max) + }.map { if (scale != null) it.setScale(scale, RoundingMode.DOWN) else it } + .single() } else { Arb.bigDecimal().single() } diff --git a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotestInJunitTest.kt b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotestInJunitTest.kt index 5c3803740..cbe0e99c8 100644 --- a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotestInJunitTest.kt +++ b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/KotestInJunitTest.kt @@ -306,7 +306,44 @@ class KotestInJunitTest { val actual = SUT.giveMeOne().value - then(actual).matches { it in 10.0..99.0 || it in -100.0..-10.0 } + then(actual).matches { it in -99.0..99.0 } + } + + @RepeatedTest(TEST_COUNT) + fun sampleDoubleWithEqualMinMax() { + class DoubleObject( + @field:DecimalMin("10.0") @field:DecimalMax("10.0") + val value: Double + ) + + val actual = SUT.giveMeOne().value + then(actual).isEqualTo(10.0) + } + + @RepeatedTest(TEST_COUNT) + fun sampleDoubleWithMultipleConstraints() { + class DoubleObject( + @field:DecimalMax("11.5") @field:Max(10) + val value: Double + ) + + val actual = SUT.giveMeOne().value + then(actual).isLessThanOrEqualTo(10.0) + } + + @RepeatedTest(TEST_COUNT) + fun sampleDoubleWithPreciseExclusiveBounds() { + class DoubleObject( + @field:DecimalMin(value = "10.0", inclusive = false) + @field:DecimalMax(value = "10.1", inclusive = false) + val value: Double + ) + + val actual = SUT.giveMeOne().value + + then(actual) + .isGreaterThan(10.0) + .isLessThan(10.1) } @RepeatedTest(TEST_COUNT) @@ -387,7 +424,44 @@ class KotestInJunitTest { val actual = SUT.giveMeOne().value - then(actual).matches { it in 10.0..99.0 || it in -100.0..-10.0 } + then(actual).matches { it in -99.0..99.0 } + } + + @RepeatedTest(TEST_COUNT) + fun sampleFloatWithEqualMinMax() { + class DoubleObject( + @field:DecimalMin("10.0") @field:DecimalMax("10.0") + val value: Double + ) + + val actual = SUT.giveMeOne().value + then(actual).isEqualTo(10.0) + } + + @RepeatedTest(TEST_COUNT) + fun sampleFloatWithMultipleConstraints() { + class DoubleObject( + @field:DecimalMax("11.5") @field:Max(10) + val value: Double + ) + + val actual = SUT.giveMeOne().value + then(actual).isLessThanOrEqualTo(10.0) + } + + @RepeatedTest(TEST_COUNT) + fun sampleFloatWithPreciseExclusiveBounds() { + class FloatObject( + @field:DecimalMin(value = "10.0", inclusive = false) + @field:DecimalMax(value = "10.1", inclusive = false) + val value: Float + ) + + val actual = SUT.giveMeOne().value + + then(actual) + .isGreaterThan(10.0f) + .isLessThan(10.1f) } @RepeatedTest(TEST_COUNT) @@ -631,8 +705,7 @@ class KotestInJunitTest { val actual = SUT.giveMeOne().value then(actual).matches { - it in BigInteger.valueOf(10L)..BigInteger.valueOf(99L) || - it in BigInteger.valueOf(-99L)..BigInteger.valueOf(-10L) + it in BigInteger.valueOf(-99L)..BigInteger.valueOf(99L) } } @@ -715,11 +788,47 @@ class KotestInJunitTest { val actual = SUT.giveMeOne().value then(actual).matches { - it in BigDecimal.valueOf(10L)..BigDecimal.valueOf(99) || - it in BigDecimal.valueOf(-100)..BigDecimal.valueOf(-10) + it in BigDecimal.valueOf(-99)..BigDecimal.valueOf(99) } } + @RepeatedTest(TEST_COUNT) + fun sampleBigDecimalWithEqualMinMax() { + class BigDecimalObject( + @field:DecimalMin("10.00") @field:DecimalMax("10.00") + val value: BigDecimal + ) + + val actual = SUT.giveMeOne().value + then(actual).isEqualByComparingTo(BigDecimal("10.00")) + } + + @RepeatedTest(TEST_COUNT) + fun sampleBigDecimalWithMultipleConstraints() { + class BigDecimalObject( + @field:DecimalMax("11.5") @field:Max(10) + val value: BigDecimal + ) + + val actual = SUT.giveMeOne().value + then(actual).isLessThanOrEqualTo(BigDecimal.valueOf(10)) + } + + @RepeatedTest(TEST_COUNT) + fun sampleBigDecimalWithPreciseExclusiveBounds() { + class BigDecimalObject( + @field:DecimalMin(value = "10.0", inclusive = false) + @field:DecimalMax(value = "10.1", inclusive = false) + val value: BigDecimal + ) + + val actual = SUT.giveMeOne().value + + then(actual) + .isGreaterThan(BigDecimal("10.0")) + .isLessThan(BigDecimal("10.1")) + } + @RepeatedTest(TEST_COUNT) fun setPostConditionExtension() { class StringObject(val string: String) diff --git a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/SimpleValueJqwikPluginTest.kt b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/SimpleValueJqwikPluginTest.kt index 3ae622ffd..ce63a0e66 100644 --- a/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/SimpleValueJqwikPluginTest.kt +++ b/fixture-monkey-tests/kotlin-tests/src/test/kotlin/com/navercorp/fixturemonkey/tests/kotlin/SimpleValueJqwikPluginTest.kt @@ -223,4 +223,49 @@ class SimpleValueJqwikPluginTest { then(actual).isLessThanOrEqualTo(0); } + + @RepeatedTest(TEST_COUNT) + fun modifyPositiveDecimalNumberRange() { + val sut = FixtureMonkey.builder() + .plugin( + SimpleValueJqwikPlugin() + .minNumberValue(1) + .maxNumberValue(10) + ) + .build() + + val actual: Double = sut.giveMeOne() + + then(actual).isBetween(1.0, 10.0) + } + + @RepeatedTest(TEST_COUNT) + fun modifyNegativeDecimalNumberRange() { + val sut = FixtureMonkey.builder() + .plugin( + SimpleValueJqwikPlugin() + .minNumberValue(-10) + .maxNumberValue(-1) + ) + .build() + + val actual: Double = sut.giveMeOne() + + then(actual).isBetween(-10.0, -1.0) + } + + @RepeatedTest(TEST_COUNT) + fun modifyMixedDecimalNumberRange() { + val sut = FixtureMonkey.builder() + .plugin( + SimpleValueJqwikPlugin() + .minNumberValue(-5) + .maxNumberValue(5) + ) + .build() + + val actual: Double = sut.giveMeOne() + + then(actual).isBetween(-5.0, 5.0) + } }