diff --git a/modules/random/src/main/java/org/stubit/random/RandomChar.java b/modules/random/src/main/java/org/stubit/random/RandomChar.java deleted file mode 100644 index e984a5b..0000000 --- a/modules/random/src/main/java/org/stubit/random/RandomChar.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.stubit.random; - -import static org.stubit.random.RandomChoice.anyOf; - -/** - * Randomly select a character from a given {@link Alphabet} - * - * @see Alphabet - * @see DigitSystem - */ -public class RandomChar { - - private RandomChar() {} - - /** - * @return a randomly selected letter from {@link Alphabet#BASIC_LATIN}. - */ - public static char aLatinLetter() { - return aLetterFrom(Alphabet.BASIC_LATIN); - } - - /** - * @return a randomly selected digit from {@link DigitSystem#ARABIC}. - */ - public static char anArabicDigit() { - return aDigitFrom(DigitSystem.ARABIC); - } - - /** - * @param alphabet the {@link Alphabet} to select a character from - * @return a randomly selected character from the provided {@link Alphabet}. - */ - public static char aLetterFrom(Alphabet alphabet) { - return anyOf(alphabet.letters()); - } - - /** - * @param digitSystem the {@link DigitSystem} to select a character from - * @return a randomly selected character from the provided {@link DigitSystem}. - */ - public static char aDigitFrom(DigitSystem digitSystem) { - return anyOf(digitSystem.digits()); - } -} diff --git a/modules/random/src/main/java/org/stubit/random/RandomString.java b/modules/random/src/main/java/org/stubit/random/RandomString.java index 7c7f012..7654df3 100644 --- a/modules/random/src/main/java/org/stubit/random/RandomString.java +++ b/modules/random/src/main/java/org/stubit/random/RandomString.java @@ -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. */ @@ -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 diff --git a/modules/random/src/test/java/org/stubit/random/RandomStringTest.java b/modules/random/src/test/java/org/stubit/random/RandomStringTest.java index daa65d5..b762d1e 100644 --- a/modules/random/src/test/java/org/stubit/random/RandomStringTest.java +++ b/modules/random/src/test/java/org/stubit/random/RandomStringTest.java @@ -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]"); } }