Skip to content

Commit 7821ee5

Browse files
akutscheraNylle
authored andcommitted
fix: create BigDecimal as SpecialSpecimen
Refs: #85
1 parent ffa787a commit 7821ee5

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/main/java/com/github/nylle/javafixture/SpecimenType.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ public boolean isSpecialType() {
165165
if (asClass().equals(java.math.BigInteger.class)) {
166166
return true;
167167
}
168+
if (asClass().equals(java.math.BigDecimal.class)) {
169+
return true;
170+
}
168171
if (asClass().equals(java.io.File.class)) {
169172
return true;
170173
}

src/main/java/com/github/nylle/javafixture/specimen/SpecialSpecimen.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import com.github.nylle.javafixture.Context;
44
import com.github.nylle.javafixture.CustomizationContext;
55
import com.github.nylle.javafixture.ISpecimen;
6+
import com.github.nylle.javafixture.PseudoRandom;
67
import com.github.nylle.javafixture.SpecimenType;
78

89
import java.io.File;
910
import java.lang.annotation.Annotation;
11+
import java.math.BigDecimal;
1012
import java.math.BigInteger;
1113
import java.net.URI;
1214
import java.net.URISyntaxException;
@@ -44,6 +46,9 @@ public T create(CustomizationContext customizationContext, Annotation[] annotati
4446
if (type.asClass().equals(BigInteger.class)) {
4547
return (T) createBigInteger();
4648
}
49+
if (type.asClass().equals(BigDecimal.class)) {
50+
return (T) createBigDecimal();
51+
}
4752
try {
4853
return (T) new URI("https://localhost/" + UUID.randomUUID());
4954
} catch (URISyntaxException e) {
@@ -61,4 +66,12 @@ private BigInteger createBigInteger() {
6166
}
6267
return result;
6368
}
69+
70+
private BigDecimal createBigDecimal() {
71+
var bd = new BigDecimal(new PseudoRandom().nextLong(new Random().nextBoolean()));
72+
if (context.getConfiguration().usePositiveNumbersOnly()) {
73+
return bd.abs();
74+
}
75+
return bd;
76+
}
6477
}

src/test/java/com/github/nylle/javafixture/SpecimenTypeTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.io.File;
1717
import java.lang.reflect.ParameterizedType;
1818
import java.lang.reflect.Type;
19+
import java.math.BigDecimal;
1920
import java.math.BigInteger;
2021
import java.net.URI;
2122
import java.time.Duration;
@@ -262,13 +263,15 @@ void isSpecialType() {
262263
assertThat(new SpecimenType<File>() {}.isSpecialType()).isTrue();
263264
assertThat(new SpecimenType<URI>() {}.isSpecialType()).isTrue();
264265
assertThat(new SpecimenType<BigInteger>() {}.isSpecialType()).isTrue();
266+
assertThat(new SpecimenType<BigDecimal>() {}.isSpecialType()).isTrue();
265267
}
266268

267269
@TestWithCases
268270
@TestCase(class1 = String.class, bool2 = false)
269271
@TestCase(class1 = File.class, bool2 = true)
270272
@TestCase(class1 = URI.class, bool2 = true)
271273
@TestCase(class1 = BigInteger.class, bool2 = true)
274+
@TestCase(class1 = BigDecimal.class, bool2 = true)
272275
void isSpecialTypeFromClass(Class<?> value, boolean expected) {
273276
assertThat(SpecimenType.fromClass(value).isSpecialType()).isEqualTo(expected);
274277
}

src/test/java/com/github/nylle/javafixture/specimen/SpecialSpecimenTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import java.io.File;
1313
import java.lang.annotation.Annotation;
14+
import java.math.BigDecimal;
1415
import java.math.BigInteger;
1516
import java.net.URI;
1617
import java.util.Map;
@@ -111,4 +112,22 @@ void createNonNegativeBigInteger() {
111112
assertThat(actual).isNotNegative();
112113
}
113114

115+
@Test
116+
@DisplayName("create BigDecimal creates random number")
117+
void createBigDecimal() {
118+
var sut = new SpecialSpecimen<>(new SpecimenType<BigDecimal>() {}, context);
119+
var actual = sut.create(noContext(), new Annotation[0]);
120+
121+
assertThat(actual).isInstanceOf(BigDecimal.class);
122+
}
123+
124+
@Test
125+
@DisplayName("create BigDecimal acreates non-negative random number when context demands it")
126+
void createNonNegativeBigDecimal() {
127+
var context = new Context(Configuration.configure().usePositiveNumbersOnly(true));
128+
var sut = new SpecialSpecimen<>(new SpecimenType<BigDecimal>() {}, context);
129+
var actual = sut.create(noContext(), new Annotation[0]);
130+
131+
assertThat(actual).isNotNegative();
132+
}
114133
}

0 commit comments

Comments
 (0)