diff --git a/src/main/java/com/github/javafaker/Lorem.java b/src/main/java/com/github/javafaker/Lorem.java index fb1966e9..fe1268d5 100644 --- a/src/main/java/com/github/javafaker/Lorem.java +++ b/src/main/java/com/github/javafaker/Lorem.java @@ -74,14 +74,36 @@ public class Lorem { return faker.fakeValuesService().resolve("lorem.words", this, faker); } - public String sentence(int wordCount) { - return capitalize(join(words(wordCount + faker.random().nextInt(6)), " ") + "."); - } - + /** + * Create a sentence with a random number of words within the range 4..10. + * @return a random sentence + */ public String sentence() { return sentence(3); } + /** + * Create a sentence with a random number of words within the range (wordCount+1)..(wordCount+6). + * @param wordCount + * @return a random sentence + */ + public String sentence(int wordCount) { + return sentence(wordCount, 6); + } + + /** + * Create a sentence with a random number of words within the range (wordCount+1)..(wordCount+randomWordsToAdd).

+ * + * Set {@code randomWordsToAdd} to 0 to generate sentences with a fixed number of words. + * @param wordCount + * @param randomWordsToAdd + * @return a random sentence + */ + public String sentence(int wordCount, int randomWordsToAdd) { + int numberOfWordsToAdd = randomWordsToAdd == 0 ? 0 : faker.random().nextInt(randomWordsToAdd); + return capitalize(join(words(wordCount + numberOfWordsToAdd), " ") + "."); + } + public List sentences(int sentenceCount) { List sentences = new ArrayList(sentenceCount); for (int i = 0; i < sentenceCount; i++) { diff --git a/src/test/java/com/github/javafaker/LoremTest.java b/src/test/java/com/github/javafaker/LoremTest.java index b8011c56..d35ad4de 100644 --- a/src/test/java/com/github/javafaker/LoremTest.java +++ b/src/test/java/com/github/javafaker/LoremTest.java @@ -1,7 +1,5 @@ package com.github.javafaker; -import org.junit.Test; - import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression; import static org.hamcrest.Matchers.isEmptyOrNullString; import static org.hamcrest.Matchers.isEmptyString; @@ -9,6 +7,8 @@ import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; +import org.junit.Test; + public class LoremTest extends AbstractFakerTest { @Test public void shouldCreateFixedLengthString() { @@ -72,4 +72,25 @@ public class LoremTest extends AbstractFakerTest { public void testCharactersMinimumMaximumLengthIncludeUppercase() { assertThat(faker.lorem().characters(1, 10), matchesRegularExpression("[a-zA-Z\\d]{1,10}")); } + + @Test + public void testSentence() { + assertThat(faker.lorem().sentence(), matchesRegularExpression("(\\w+\\s?){4,10}\\.")); + } + + @Test + public void testSentenceWithWordCount() { + assertThat(faker.lorem().sentence(10), matchesRegularExpression("(\\w+\\s?){11,17}\\.")); + } + + @Test + public void testSentenceWithWordCountAndRandomWordsToAdd() { + assertThat(faker.lorem().sentence(10, 10), matchesRegularExpression("(\\w+\\s?){10,20}\\.")); + } + + @Test + public void testSentenceFixedNumberOfWords() { + assertThat(faker.lorem().sentence(10, 0), matchesRegularExpression("(\\w+\\s?){10}\\.")); + } + }