Skip to content

Commit

Permalink
Merge pull request #26 from ocadotechnology/test-arranger-17
Browse files Browse the repository at this point in the history
test-arranger-17:
  • Loading branch information
mjureczko authored Nov 5, 2021
2 parents dcd732e + fd61153 commit d138da7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ocadotechnology.gembus</groupId>
<artifactId>test-arranger</artifactId>
<version>1.4-SNAPSHOT</version>
<version>1.4</version>
<packaging>jar</packaging>
<name>test-arranger</name>
<description>A tool for arranging test data with pseudo-random values.</description>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/ocadotechnology/gembus/test/Arranger.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package com.ocadotechnology.gembus.test;

import org.jeasy.random.randomizers.EmailRandomizer;
import org.jeasy.random.randomizers.FirstNameRandomizer;
import org.jeasy.random.randomizers.LastNameRandomizer;
import org.jeasy.random.randomizers.RegularExpressionRandomizer;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand All @@ -33,6 +36,8 @@ public class Arranger {
private static EnhancedRandom simplifiedRandom = ArrangersConfigurer.instance().simplifiedRandom();
private static final EmailRandomizer emailRandomizer = new EmailRandomizer();
private static final MarkovStringRandomizer stringRandomizer = new MarkovStringRandomizer(ArrangersConfigurer.STRING_MIN_LENGTH, ArrangersConfigurer.STRING_MAX_LENGTH);
private static final FirstNameRandomizer firstNameRandomizer = new FirstNameRandomizer();
private static final LastNameRandomizer lastNameRandomizer = new LastNameRandomizer();

/**
* @see org.jeasy.random.EasyRandom#nextObject
Expand Down Expand Up @@ -227,6 +232,18 @@ public static BigDecimal somePriceLikeBigDecimal(BigDecimal min, BigDecimal max)
return new BigDecimal(valueInCents).divide(centsRatio).add(min);
}

public static String someFirstName() {
return firstNameRandomizer.getRandomValue();
}

public static String someLastName() {
return lastNameRandomizer.getRandomValue();
}

public static String someStringLike(String regex) {
return new RegularExpressionRandomizer(regex).getRandomValue();
}

static class CannotSatisfyPredicateException extends RuntimeException {
public CannotSatisfyPredicateException(String type) {
super("Cannot satisfy provided predicate when generating data of type " + type);
Expand Down
15 changes: 9 additions & 6 deletions src/main/kotlin/com/ocadotechnology/gembus/test/testUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ inline fun <reified T> some(adjustment: T.() -> Unit): T {
}

inline fun <reified T> some(vararg excludedFields: String) =
Arranger.some(T::class.java, *excludedFields)!!
Arranger.some(T::class.java, *excludedFields)!!

inline fun <reified T> someSimplified(vararg excludedFields: String): T =
Arranger.someSimplified(T::class.java, *excludedFields)!!
Arranger.someSimplified(T::class.java, *excludedFields)!!

inline fun <reified T> someObjects(numberOfObjects: Int, vararg excludedFields: String) =
Arranger.someObjects(T::class.java, numberOfObjects, *excludedFields).iterator().asSequence<T>()
Arranger.someObjects(T::class.java, numberOfObjects, *excludedFields).iterator().asSequence<T>()

inline fun <reified T> someMatching(crossinline predicate: (T) -> Boolean, vararg excludedFields: String) =
Arranger.someMatching<T>(T::class.java, Predicate { predicate.invoke(it) }, *excludedFields)!!
Arranger.someMatching<T>(T::class.java, Predicate { predicate.invoke(it) }, *excludedFields)!!

fun someInt(): Int = Arranger.someInteger()
fun someInt(minValue: Int, maxValue: Int): Int = Arranger.someInteger(minValue, maxValue)
Expand All @@ -57,12 +57,15 @@ fun somePositiveLong(boundInclusive: Long): Long = Arranger.somePositiveLong(bou
fun someEmail(): String = Arranger.someEmail()

fun <T> someMatchingOrNull(array: Array<T>, predicate: (T) -> Boolean): T? =
Arranger.someMatching(array, predicate)
Arranger.someMatching(array, predicate)

fun <T> someMatching(array: Array<T>, predicate: (T) -> Boolean) =
someMatchingOrNull(array, predicate) ?: throw RuntimeException("No match found.")
someMatchingOrNull(array, predicate) ?: throw RuntimeException("No match found.")

fun someGivenOrLater(given: LocalDate): LocalDate = Arranger.someGivenOrLater(given)
fun someGivenOrEarlier(given: LocalDate): LocalDate = Arranger.someGivenOrEarlier(given)
fun someInstant(): Instant = Arranger.someInstant()
fun <T> someFrom(source: Collection<T>): T = Arranger.someFrom(source)
fun someFirstName(): String = Arranger.someFirstName()
fun someLastName(): String = Arranger.someLastName()
fun someStringLike(regex: String): String = Arranger.someStringLike(regex)
21 changes: 21 additions & 0 deletions src/test/java/com/ocadotechnology/gembus/test/ArrangerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,27 @@ public void shouldGenerateIntFromRangeOverNegativeNumbers() {
.isGreaterThanOrEqualTo(min);
}
}

@Test
public void should_generateFirstName() {
//when
String actual = Arranger.someFirstName();

//then
assertThat(actual.length()).isGreaterThan(1);
assertThat(actual.charAt(0)).isUpperCase();
assertThat(actual.substring(1)).isEqualTo(actual.substring(1).toLowerCase(Locale.ROOT));
}

@Test
public void should_generateLastName() {
//when
String actual = Arranger.someLastName();

//then
assertThat(actual.length()).isGreaterThan(1);
assertThat(actual.charAt(0)).isUpperCase();
}
}

class SomeClass {
Expand Down

0 comments on commit d138da7

Please sign in to comment.