Skip to content

Commit

Permalink
Add RandomChoiceBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mkutz committed May 21, 2024
1 parent 720dd96 commit 0eb4615
Show file tree
Hide file tree
Showing 17 changed files with 524 additions and 90 deletions.
29 changes: 27 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
plugins {
base
`stubit-style`
id("org.sonarqube") version "5.0.0.4638"
id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
id("info.solidsoft.pitest.aggregator")
id("com.diffplug.spotless") version "6.24.0"
}

repositories { mavenCentral() }
Expand All @@ -24,3 +23,29 @@ nexusPublishing {
}
}
}

spotless {
format("misc") {
target("**/*.md", "**/*.xml", "**/*.yml", "**/*.yaml", "**/*.html", "**/*.css", ".gitignore")
targetExclude("**/build/**/*", "**/.idea/**")
trimTrailingWhitespace()
endWithNewline()
indentWithSpaces(2)
}

java {
target("**/*.java")
targetExclude("**/build/**/*")
googleJavaFormat().reflowLongStrings()
removeUnusedImports()
indentWithSpaces(2)
}

kotlinGradle {
target("**/*.gradle.kts")
targetExclude("**/build/**/*.gradle.kts")
ktfmt().googleStyle()
}

freshmark { target("*.md") }
}
5 changes: 0 additions & 5 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
plugins { `kotlin-dsl` }

repositories { gradlePluginPortal() }

dependencies {
implementation("info.solidsoft.gradle.pitest:gradle-pitest-plugin:1.15.0")
implementation("com.diffplug.spotless:spotless-plugin-gradle:6.24.0")
}
29 changes: 0 additions & 29 deletions buildSrc/src/main/kotlin/stubit-style.gradle.kts

This file was deleted.

19 changes: 0 additions & 19 deletions buildSrc/src/main/kotlin/stubit-testing.gradle.kts

This file was deleted.

4 changes: 3 additions & 1 deletion manual/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
plugins {
id("org.asciidoctor.jvm.convert") version "4.0.2"
`stubit-style`
java
}

repositories { mavenCentral() }

dependencies {
testImplementation(project(":modules:http"))
testImplementation(project(":modules:random"))

testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
Expand Down
78 changes: 78 additions & 0 deletions manual/src/docs/asciidoc/chapters/03-random.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
= Random

The Random module contains a collection of builders and utility functions to generate random data.


== Random Integer

The link:{javadoc-url}/http/org/stubit/random/RandomIntBuilder.html[`RandomIntBuilder` class] allows to generate random Integer values within a defined min and max range (both inclusive).

NOTE:: It is not possible to set max to Integer.MAX_VALUE.
The maximum value is Integer.MAX_VALUE - 1.
This is due to the fact that the SecureRandom.nextInt(int) method is exclusive of the upper bound.


=== Min and Max

[source,java,indent=0]
----
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=anInt]
----


=== Positive Integer

[source,java,indent=0]
----
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=aPositiveInt]
----


=== Negative Integer


[source,java,indent=0]
----
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=aNegativeInt]
----


== Random Choice

The link:{javadoc-url}/http/org/stubit/random/RandomChoiceBuilder.html[`RandomChoiceBuilder` class] allows to make a random choice from the objects in a Collection, an Array, or the values of an Enum type.


=== From Collection

[source,java,indent=0]
----
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=from_list]
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=from_map]
----


=== From Array

[source,java,indent=0]
----
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=from_ellipsis]
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=from_array]
----


=== From Enum Type

[source,java,indent=0]
----
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=from_enum_values]
----


=== Excluding Choices

[source,java,indent=0]
----
include::../../../test/java/org/stubit/random/RandomDocTest.java[tag=save]
----
77 changes: 77 additions & 0 deletions manual/src/test/java/org/stubit/random/RandomDocTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.stubit.random;

import static org.assertj.core.api.Assertions.assertThat;
import static org.stubit.random.RandomChoiceBuilder.from;
import static org.stubit.random.RandomChoiceBuilder.fromValuesOf;
import static org.stubit.random.RandomIntBuilder.*;

import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;

class RandomDocTest {

@Test
void randomInt_examples() {
// tag::anInt[]
int someInt = anInt().build();
assertThat(someInt).isBetween(Integer.MIN_VALUE, Integer.MAX_VALUE - 1);

int someIntBetween42And4711 = anInt().min(42).max(4711).build();
assertThat(someIntBetween42And4711).isBetween(42, 4711);

int someIntLessThan4711 = anInt().max(4711).build();
assertThat(someIntLessThan4711).isLessThanOrEqualTo(4711);

int someIntGreaterThanMinus10 = anInt().min(-42).build();
assertThat(someIntGreaterThanMinus10).isGreaterThanOrEqualTo(-42);
// end::anInt[]

// tag::aPositiveInt[]
int somePositiveInt = aPositiveInt().build();
assertThat(somePositiveInt).isPositive().isNotZero().isLessThan(Integer.MAX_VALUE);
// end::aPositiveInt[]

// tag::aNegativeInt[]
int someNegativeInt = aNegativeInt().build();
assertThat(someNegativeInt).isNegative().isNotZero().isGreaterThanOrEqualTo(Integer.MIN_VALUE);
// end::aNegativeInt[]
}

@Test
void anyOf_examples() {
// tag::from_ellipsis[]
assertThat(from("a", "b", "c").chooseAny()).isIn("a", "b", "c");
// end::from_ellipsis[]

// tag::from_array[]
String[] choiceArray = {"a", "b", "c"};
assertThat(from(choiceArray).chooseAny()).isIn((Object[]) choiceArray);
// end::from_array[]

// tag::from_list[]
var choicesList = List.of("a", "b", "c");
assertThat(from(choicesList).chooseAny()).isIn(choicesList);
// end::from_list[]

// tag::from_map[]
var choicesMap = Map.of("a", 1, "b", 2, "c", 3);
assertThat(from(choicesMap).chooseAny()).isIn(choicesMap);
// end::from_map[]

// tag::from_enum_values[]
enum Color {
RED,
GREEN,
BLUE
}
assertThat(fromValuesOf(Color.class).chooseAny()).isIn(Color.RED, Color.GREEN, Color.BLUE);
// end::from_enum_values[]

// tag::save[]
var allChoices = List.of("a", "b", "c");
var excludedChoice = "a";
assertThat(from(allChoices).save(excludedChoice).chooseAny()).isNotEqualTo(excludedChoice);
// end::save[]
}
}
13 changes: 10 additions & 3 deletions modules/http/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
plugins {
`stubit-module`
`stubit-publish`
`stubit-style`
`stubit-testing`
id("info.solidsoft.pitest") version "1.15.0"
}

repositories { mavenCentral() }
Expand All @@ -11,7 +10,15 @@ dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.junit.jupiter:junit-jupiter-params")
testImplementation("org.assertj:assertj-core:3.25.3")

testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

testImplementation("org.assertj:assertj-core:3.25.3")
tasks.withType<Test> { useJUnitPlatform() }

pitest {
junit5PluginVersion.set("1.0.0")
outputFormats.addAll("XML", "HTML")
timestampedReports.set(false)
}
13 changes: 10 additions & 3 deletions modules/random/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
plugins {
`stubit-module`
`stubit-publish`
`stubit-style`
`stubit-testing`
id("info.solidsoft.pitest") version "1.15.0"
}

repositories { mavenCentral() }
Expand All @@ -11,7 +10,15 @@ dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.junit.jupiter:junit-jupiter-params")
testImplementation("org.assertj:assertj-core:3.25.3")

testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

testImplementation("org.assertj:assertj-core:3.25.3")
tasks.withType<Test> { useJUnitPlatform() }

pitest {
junit5PluginVersion.set("1.0.0")
outputFormats.addAll("XML", "HTML")
timestampedReports.set(false)
}
11 changes: 11 additions & 0 deletions modules/random/src/main/java/org/stubit/random/RandomChoice.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,15 @@ public static <T> T anyOf(Collection<T> choices) {
public static <K, V> Entry<K, V> anyOf(Map<K, V> choices) {
return anyOf(choices.entrySet());
}

/**
* Randomly selects an element from the values of the provided choices {@link Enum} class.
*
* @param enumType the {@link Enum} type
* @return a randomly selected {@link Enum} constant from the provided choices enumType
* @param <T> the type of the choices
*/
public static <T extends Enum<?>> T random(Class<? extends T> enumType) {
return anyOf(enumType.getEnumConstants());
}
}
Loading

0 comments on commit 0eb4615

Please sign in to comment.