add Lorem#sentence(int wordCount, int randomWordsToAdd) (fixes #190) (#192)

This commit is contained in:
Pascal Schumacher
2017-02-02 22:13:49 +01:00
committed by Ricky Yim
parent 1ad1adc4ab
commit 33e09fcf0f
2 changed files with 49 additions and 6 deletions
+26 -4
View File
@@ -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).</p>
*
* 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<String> sentences(int sentenceCount) {
List<String> sentences = new ArrayList<String>(sentenceCount);
for (int i = 0; i < sentenceCount; i++) {
@@ -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}\\."));
}
}