Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into java8
  • Loading branch information
mjureczko committed Jan 19, 2024
2 parents b6ef980 + 90dc937 commit b63bf6f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/ocadotechnology/gembus/test/Arranger.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.time.Instant;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
Expand Down Expand Up @@ -239,6 +240,30 @@ public static boolean someBoolean() {
return random.easyRandom.nextBoolean();
}

public static float someFloat() {
return random.easyRandom.nextFloat();
}

/**
* @param min inclusive
* @param max exclusive
*/
public static float someFloat(float min, float max) {
return (float) ThreadLocalRandom.current().nextDouble(min, max);
}

public static double someDouble() {
return random.easyRandom.nextDouble();
}

/**
* @param min inclusive
* @param max exclusive
*/
public static double someDouble(double min, double max) {
return ThreadLocalRandom.current().nextDouble(min, max);
}

public static <T> T someFrom(Collection<T> source) {
if (source.isEmpty()) {
throw new IllegalArgumentException("Cannot return element from empty collection.");
Expand Down
28 changes: 16 additions & 12 deletions src/main/kotlin/com/ocadotechnology/gembus/test/testUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ 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> some(overrides: Map<String, () -> Any>): T =
Arranger.some(T::class.java, overrides.mapValues { Supplier { it.value.invoke() } })
Arranger.some(T::class.java, overrides.mapValues { Supplier { it.value.invoke() } })

@Deprecated(
message = "This methods creates a lot of complexity resulting in lesser performance and increased memory consumption.",
replaceWith = ReplaceWith("some<T>(excludedFields)"),
level = DeprecationLevel.WARNING
message = "This methods creates a lot of complexity resulting in lesser performance and increased memory consumption.",
replaceWith = ReplaceWith("some<T>(excludedFields)"),
level = DeprecationLevel.WARNING
)
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> someObjects(numberOfObjects: Int, overrides: Map<String, () -> Any>) =
Arranger.someObjects(T::class.java, numberOfObjects, overrides.mapValues { Supplier { it.value.invoke() } })
.iterator().asSequence<T>()
Arranger.someObjects(T::class.java, numberOfObjects, overrides.mapValues { Supplier { it.value.invoke() } })
.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 @@ -71,14 +71,18 @@ fun someBoolean(): Boolean = Arranger.someBoolean()
fun someTwoDecimalPlacesBigDecimal(): BigDecimal = Arranger.somePriceLikeBigDecimal()
fun somePositiveInt(boundInclusive: Int): Int = Arranger.somePositiveInt(boundInclusive)
fun somePositiveLong(boundInclusive: Long): Long = Arranger.somePositiveLong(boundInclusive)
fun someFloat(): Float = Arranger.someFloat()
fun someFloat(min: Float, max: Float): Float = Arranger.someFloat(min, max)
fun someDouble(): Double = Arranger.someDouble()
fun someDouble(min: Double, max: Double): Double = Arranger.someDouble(min, max)

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)
Expand Down
57 changes: 51 additions & 6 deletions src/test/java/com/ocadotechnology/gembus/test/ArrangerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.ocadotechnology.gembus.test;

import com.ocadotechnology.gembus.ToTestNonPublic;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
Expand Down Expand Up @@ -56,12 +57,12 @@ void email() {
void priceLike() {


//when
final BigDecimal price = Arranger.somePriceLikeBigDecimal();
//when
final BigDecimal price = Arranger.somePriceLikeBigDecimal();

//then
assertTrue(price.compareTo(BigDecimal.ZERO) >= 0);
assertEquals(2, price.scale());
//then
assertTrue(price.compareTo(BigDecimal.ZERO) >= 0);
assertEquals(2, price.scale());

}

Expand Down Expand Up @@ -165,7 +166,7 @@ void someFromList() {
}

@Test
void someFromSetOfSizeOne(){
void someFromSetOfSizeOne() {
//given
final Set<Integer> listOfSizeOne = Collections.singleton(Arranger.someInteger());

Expand Down Expand Up @@ -377,6 +378,50 @@ public void should_generateLastName() {
assertThat(actual.length()).isGreaterThan(1);
assertThat(actual.charAt(0)).isUpperCase();
}

@Test
void should_generateFloatFromTheRangeDefinedByMinAndMax() {
//given
float min = -999.99f;
float max = 999.99f;

//when
float actualMin = 0.0f;
float actualMax = 0.0f;
for (int i = 0; i < 999; i++) {
double actual = Arranger.someFloat(min, max);
actualMin = (float) Math.min(actualMin, actual);
actualMax = (float) Math.max(actualMax, actual);
}

//then
assertThat(actualMin).isGreaterThanOrEqualTo(min);
assertThat(actualMin).isLessThan(-1.0f);
assertThat(actualMax).isLessThan(max);
assertThat(actualMax).isGreaterThan(1.0f);
}

@Test
void should_generateDoubleFromTheRangeDefinedByMinAndMax() {
//given
double min = -999.99;
double max = 999.99;

//when
double actualMin = 0.0;
double actualMax = 0.0;
for (int i = 0; i < 999; i++) {
double actual = Arranger.someDouble(min, max);
actualMin = Math.min(actualMin, actual);
actualMax = Math.max(actualMax, actual);
}

//then
assertThat(actualMin).isGreaterThanOrEqualTo(min);
assertThat(actualMin).isLessThan(-1.0);
assertThat(actualMax).isLessThan(max);
assertThat(actualMax).isGreaterThan(1.0);
}
}

class SomeClass {
Expand Down

0 comments on commit b63bf6f

Please sign in to comment.