Skip to content

Commit

Permalink
fix: create BigDecimal as SpecialSpecimen
Browse files Browse the repository at this point in the history
Refs: #85
  • Loading branch information
akutschera authored and Nylle committed Mar 3, 2023
1 parent ffa787a commit 7821ee5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/github/nylle/javafixture/SpecimenType.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public boolean isSpecialType() {
if (asClass().equals(java.math.BigInteger.class)) {
return true;
}
if (asClass().equals(java.math.BigDecimal.class)) {
return true;
}
if (asClass().equals(java.io.File.class)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.github.nylle.javafixture.Context;
import com.github.nylle.javafixture.CustomizationContext;
import com.github.nylle.javafixture.ISpecimen;
import com.github.nylle.javafixture.PseudoRandom;
import com.github.nylle.javafixture.SpecimenType;

import java.io.File;
import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -44,6 +46,9 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati
if (type.asClass().equals(BigInteger.class)) {
return (T) createBigInteger();
}
if (type.asClass().equals(BigDecimal.class)) {
return (T) createBigDecimal();
}
try {
return (T) new URI("https://localhost/" + UUID.randomUUID());
} catch (URISyntaxException e) {
Expand All @@ -61,4 +66,12 @@ private BigInteger createBigInteger() {
}
return result;
}

private BigDecimal createBigDecimal() {
var bd = new BigDecimal(new PseudoRandom().nextLong(new Random().nextBoolean()));
if (context.getConfiguration().usePositiveNumbersOnly()) {
return bd.abs();
}
return bd;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.File;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.time.Duration;
Expand Down Expand Up @@ -262,13 +263,15 @@ void isSpecialType() {
assertThat(new SpecimenType<File>() {}.isSpecialType()).isTrue();
assertThat(new SpecimenType<URI>() {}.isSpecialType()).isTrue();
assertThat(new SpecimenType<BigInteger>() {}.isSpecialType()).isTrue();
assertThat(new SpecimenType<BigDecimal>() {}.isSpecialType()).isTrue();
}

@TestWithCases
@TestCase(class1 = String.class, bool2 = false)
@TestCase(class1 = File.class, bool2 = true)
@TestCase(class1 = URI.class, bool2 = true)
@TestCase(class1 = BigInteger.class, bool2 = true)
@TestCase(class1 = BigDecimal.class, bool2 = true)
void isSpecialTypeFromClass(Class<?> value, boolean expected) {
assertThat(SpecimenType.fromClass(value).isSpecialType()).isEqualTo(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.File;
import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.util.Map;
Expand Down Expand Up @@ -111,4 +112,22 @@ void createNonNegativeBigInteger() {
assertThat(actual).isNotNegative();
}

@Test
@DisplayName("create BigDecimal creates random number")
void createBigDecimal() {
var sut = new SpecialSpecimen<>(new SpecimenType<BigDecimal>() {}, context);
var actual = sut.create(noContext(), new Annotation[0]);

assertThat(actual).isInstanceOf(BigDecimal.class);
}

@Test
@DisplayName("create BigDecimal acreates non-negative random number when context demands it")
void createNonNegativeBigDecimal() {
var context = new Context(Configuration.configure().usePositiveNumbersOnly(true));
var sut = new SpecialSpecimen<>(new SpecimenType<BigDecimal>() {}, context);
var actual = sut.create(noContext(), new Annotation[0]);

assertThat(actual).isNotNegative();
}
}

0 comments on commit 7821ee5

Please sign in to comment.