Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mkutz committed Jul 10, 2024
1 parent a2e817e commit 4fb9deb
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 75 deletions.
44 changes: 0 additions & 44 deletions modules/random/src/main/java/org/stubit/random/RandomChar.java

This file was deleted.

89 changes: 69 additions & 20 deletions modules/random/src/main/java/org/stubit/random/RandomString.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,86 @@
package org.stubit.random;

import static org.stubit.random.RandomChoice.anyOf;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/** Generates random strings. */
public class RandomString {

private RandomString() {}

/**
* @param character the character the string should start with
* @param string the character the string should start with
* @return a new {@link Builder}
*/
public static Builder aStringStartingWith(char character) {
return new Builder(character);
public static Builder aStringStartingWith(String string) {
return new Builder(string);
}

/**
* @param string the character the string should start with
* @return a new {@link Builder}
* @return a randomly selected letter from {@link Alphabet#BASIC_LATIN}.
*/
public static Builder aStringStartingWith(String string) {
return new Builder(string);
public static String aLatinLetter() {
return aLetterFrom(Alphabet.BASIC_LATIN);
}

/**
* @param number the number of letters to generate
* @return a number of letters from {@link Alphabet#BASIC_LATIN}.
*/
public static String latinLetters(int number) {
return lettersFrom(number, Alphabet.BASIC_LATIN);
}

/**
* @return a randomly selected digit from {@link DigitSystem#ARABIC}.
*/
public static String anArabicDigit() {
return aDigitFrom(DigitSystem.ARABIC);
}

/**
* @param number the number of digits to generate
* @return a number of digits from {@link DigitSystem#ARABIC}.
*/
public static String arabicDigits(int number) {
return digitsFrom(number, DigitSystem.ARABIC);
}

/**
* @param alphabet the {@link Alphabet} to select a character from
* @return a randomly selected character from the provided {@link Alphabet}.
*/
public static String aLetterFrom(Alphabet alphabet) {
return anyOf(alphabet.letters()).toString();
}

/**
* @param alphabet the {@link Alphabet} to select a character from
* @return a randomly selected character from the provided {@link Alphabet}.
*/
public static String lettersFrom(int number, Alphabet alphabet) {
return Stream.generate(() -> aLetterFrom(alphabet)).limit(number).collect(Collectors.joining());
}

/**
* @param digitSystem the {@link DigitSystem} to select a character from
* @return a randomly selected character from the provided {@link DigitSystem}.
*/
public static String aDigitFrom(DigitSystem digitSystem) {
return anyOf(digitSystem.digits()).toString();
}

/**
* @param number the number of digits to generate
* @param digitSystem the {@link DigitSystem} to select the digits from
* @return a number of randomly selected digits from the provided {@link DigitSystem}.
*/
public static String digitsFrom(int number, DigitSystem digitSystem) {
return Stream.generate(() -> aDigitFrom(digitSystem))
.limit(number)
.collect(Collectors.joining());
}

/** Builds a random string. */
Expand All @@ -30,19 +92,6 @@ private Builder(String startString) {
stringBuilder = new StringBuilder(startString);
}

private Builder(char startCharacter) {
stringBuilder = new StringBuilder().append(startCharacter);
}

/**
* @param character the character to append
* @return this
*/
public Builder followedBy(Character character) {
stringBuilder.append(character);
return this;
}

/**
* @param string the string to append
* @return this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,95 @@
package org.stubit.random;

import static org.assertj.core.api.Assertions.assertThat;
import static org.stubit.random.RandomChar.aLatinLetter;
import static org.stubit.random.RandomChar.anArabicDigit;
import static org.stubit.random.RandomString.aStringStartingWith;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.stubit.random.RandomInt.anIntBetween;

import org.junit.jupiter.api.Test;

class RandomStringTest {

@Test
void followedBy_mixed() {
void aLetterFrom() {
assertThat(RandomString.aLetterFrom(Alphabet.GERMAN)).matches("[a-zA-ZöäüßÄÖÜẞ]");
}

@Test
void lettersFrom() {
int number = anIntBetween(1, 10);
assertThat(RandomString.lettersFrom(number, Alphabet.GERMAN))
.matches("[a-zA-ZöäüßÄÖÜẞ]+")
.hasSize(number);
}

@Test
void lettersFrom_zero() {
assertThat(RandomString.lettersFrom(0, Alphabet.FRENCH)).isEmpty();
}

@Test
void lettersFrom_negative() {
assertThatThrownBy(() -> RandomString.lettersFrom(-1, Alphabet.FRENCH))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
void aLatinLetter() {
assertThat(RandomString.aLatinLetter()).matches("[a-zA-Z]");
}

@Test
void latinLetters() {
int number = anIntBetween(1, 10);
assertThat(RandomString.latinLetters(number)).matches("[a-zA-Z]+").hasSize(number);
}

@Test
void latinLetters_zero() {
assertThat(RandomString.latinLetters(0)).isEmpty();
}

@Test
void latinLetters_negative() {
assertThatThrownBy(() -> RandomString.latinLetters(-1))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
void anArabicDigit() {
assertThat(RandomString.anArabicDigit()).matches("[0-9]");
}

@Test
void arabicDigits() {
int number = anIntBetween(1, 10);
assertThat(RandomString.arabicDigits(number)).matches("[0-9]+").hasSize(number);
}

@Test
void arabicDigits_zero() {
assertThat(RandomString.arabicDigits(0)).isEmpty();
}

@Test
void arabicDigits_negative() {
assertThatThrownBy(() -> RandomString.arabicDigits(-1))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
void aStringStartingWith() {
var string = RandomString.aStringStartingWith(RandomString.aLatinLetter()).build();

assertThat(string).matches("[a-zA-Z]");
}

@Test
void followedBy() {
var string =
aStringStartingWith(aLatinLetter())
.followedBy('-')
.followedBy(anArabicDigit())
.followedBy("üäößø")
RandomString.aStringStartingWith(RandomString.aLatinLetter())
.followedBy(RandomString.anArabicDigit())
.build();

System.out.println(string);

assertThat(string).matches("[a-zA-Z]-[0-9]üäößø");
assertThat(string).matches("[a-zA-Z][0-9]");
}
}

0 comments on commit 4fb9deb

Please sign in to comment.