Skip to content

Commit

Permalink
Fix modifying the number option in SimpleValueJqwikPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
seongahjo committed Jan 16, 2025
1 parent 7a67237 commit dc8a925
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

package com.navercorp.fixturemonkey.api.plugin;

import static com.navercorp.fixturemonkey.api.type.Types.defaultIfNull;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

import javax.annotation.Nullable;

import org.apiguardian.api.API;
import org.apiguardian.api.API.Status;

Expand Down Expand Up @@ -69,10 +74,14 @@ public final class SimpleValueJqwikPlugin implements Plugin {

private long minStringLength = DEFAULT_MIN_STRING_LENGTH;
private long maxStringLength = DEFAULT_MAX_STRING_LENGTH;
private long positiveMinNumberValue = 0;
private long positiveMaxNumberValue = DEFAULT_MAX_NUMBER_VALUE;
private long negativeMinNumberValue = DEFAULT_MIN_NUMBER_VALUE;
private long negativeMaxNumberValue = -0;
@Nullable
private Long positiveMinNumberValue = null;
@Nullable
private Long positiveMaxNumberValue = null;
@Nullable
private Long negativeMinNumberValue = null;
@Nullable
private Long negativeMaxNumberValue = null;
private int minContainerSize = DEFAULT_MIN_CONTAINER_SIZE;
private int maxContainerSize = DEFAULT_MAX_CONTAINER_SIZE;
private long minusDaysFromToday = DEFAULT_MINUS_DAYS;
Expand Down Expand Up @@ -171,10 +180,14 @@ public StringArbitrary strings() {
private static class SimpleJavaConstraintGenerator implements JavaConstraintGenerator {
private final long stringMinLength;
private final long stringMaxLength;
private final long positiveMinNumberValue;
private final long positiveMaxNumberValue;
private final long negativeMinNumberValue;
private final long negativeMaxNumberValue;
@Nullable
private final Long positiveMinNumberValue;
@Nullable
private final Long positiveMaxNumberValue;
@Nullable
private final Long negativeMinNumberValue;
@Nullable
private final Long negativeMaxNumberValue;
private final int minContainerSize;
private final int maxContainerSize;
private final long minusDaysFromToday;
Expand All @@ -183,10 +196,10 @@ private static class SimpleJavaConstraintGenerator implements JavaConstraintGene
public SimpleJavaConstraintGenerator(
long stringMinLength,
long stringMaxLength,
long positiveMinNumberValue,
long positiveMaxNumberValue,
long negativeMinNumberValue,
long negativeMaxNumberValue,
@Nullable Long positiveMinNumberValue,
@Nullable Long positiveMaxNumberValue,
@Nullable Long negativeMinNumberValue,
@Nullable Long negativeMaxNumberValue,
int minContainerSize,
int maxContainerSize,
long minusDaysFromToday,
Expand Down Expand Up @@ -219,31 +232,59 @@ public JavaStringConstraint generateStringConstraint(ArbitraryGeneratorContext c

@Override
public JavaIntegerConstraint generateIntegerConstraint(ArbitraryGeneratorContext context) {
BigInteger positiveMin = BigInteger.valueOf(this.positiveMinNumberValue);
BigInteger positiveMax = BigInteger.valueOf(this.positiveMaxNumberValue);
BigInteger negativeMin = BigInteger.valueOf(this.negativeMinNumberValue);
BigInteger negativeMax = BigInteger.valueOf(this.negativeMaxNumberValue);
BigInteger positiveMin = ifNotNull(this.positiveMinNumberValue, BigInteger::valueOf);
BigInteger positiveMax = ifNotNull(this.positiveMaxNumberValue, BigInteger::valueOf);
BigInteger negativeMin = ifNotNull(this.negativeMinNumberValue, BigInteger::valueOf);
BigInteger negativeMax = ifNotNull(this.negativeMaxNumberValue, BigInteger::valueOf);

Class<?> type = Types.getActualType(context.getResolvedType());
if (positiveMin == null) {
negativeMin = defaultIfNull(negativeMin, () -> BigInteger.valueOf(DEFAULT_MIN_NUMBER_VALUE));
negativeMax = defaultIfNull(negativeMax, () -> BigInteger.ZERO);
}

if (negativeMax == null) {
positiveMin = defaultIfNull(positiveMin, () -> BigInteger.ZERO);
positiveMax = defaultIfNull(positiveMax, () -> BigInteger.valueOf(DEFAULT_MAX_NUMBER_VALUE));
}

Class<?> type = Types.getActualType(context.getResolvedType());
if (type == Byte.class || type == byte.class) {
positiveMax = positiveMax.min(BIG_INTEGER_MAX_BYTE);
negativeMin = negativeMin.max(BIG_INTEGER_MIN_BYTE);
positiveMax = positiveMax != null
? positiveMax.min(BIG_INTEGER_MAX_BYTE)
: BIG_INTEGER_MAX_BYTE;
negativeMin = negativeMin != null
? negativeMin.max(BIG_INTEGER_MIN_BYTE)
: BIG_INTEGER_MIN_BYTE;
}

return new JavaIntegerConstraint(positiveMin, positiveMax, negativeMin, negativeMax);
}

@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);

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));
}

return new JavaDecimalConstraint(
BigDecimal.valueOf(this.positiveMinNumberValue),
positiveMin,
true,
BigDecimal.valueOf(this.positiveMaxNumberValue),
positiveMax,
true,
BigDecimal.valueOf(this.negativeMinNumberValue),
negativeMin,
true,
BigDecimal.valueOf(this.negativeMaxNumberValue),
negativeMax,
true,
2
);
Expand All @@ -266,4 +307,8 @@ public JavaDateTimeConstraint generateDateTimeConstraint(ArbitraryGeneratorConte
);
}
}

private static <T, U> U ifNotNull(T value, Function<T, U> transformer) {
return value != null ? transformer.apply(value) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class SimpleValueJqwikPluginTest {

@RepeatedTest(TEST_COUNT)
fun sampleSetObject() {
class SetObject (val integers: Set<Integer>)
class SetObject(val integers: Set<Integer>)

val sut = FixtureMonkey.builder()
.plugin(KotlinPlugin())
Expand Down Expand Up @@ -202,4 +202,25 @@ class SimpleValueJqwikPluginTest {
then(actual).isBetween(Byte.MIN_VALUE, Byte.MAX_VALUE)
}

@RepeatedTest(TEST_COUNT)
fun setPositiveMinNumberReturnsPositive() {
val sut = FixtureMonkey.builder()
.plugin(SimpleValueJqwikPlugin().minNumberValue(0))
.build()

val actual: Long = sut.giveMeOne()

then(actual).isGreaterThanOrEqualTo(0);
}

@RepeatedTest(TEST_COUNT)
fun setNegativeMaxNumberReturnsNegative() {
val sut = FixtureMonkey.builder()
.plugin(SimpleValueJqwikPlugin().maxNumberValue(-0))
.build()

val actual: Long = sut.giveMeOne()

then(actual).isLessThanOrEqualTo(0);
}
}

0 comments on commit dc8a925

Please sign in to comment.