Skip to content

Commit b6a8e42

Browse files
author
Geophilus Durairaj
authored
Add exclude_words filter to Faker::Lorem.word generator (#2761)
* Add exclude_words keyword to Lorem Ipsum word * Reuse words() method and reduce word generation in tests
1 parent 5c7e44d commit b6a8e42

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

doc/default/lorem.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Faker::Lorem
22

33
```ruby
4+
# Keyword arguments: exclude_words (prevent specific words from being produced)
45
Faker::Lorem.word #=> "repellendus"
6+
Faker::Lorem.word(exclude_words: 'error') #=> "nisi"
7+
Faker::Lorem.word(exclude_words: 'id, error') #=> "et"
8+
Faker::Lorem.word(exclude_words: ['id', 'error']) #=> "consequatur"
59

610
# Keyword arguments: number, supplemental (words from a supplementary list of Lorem-like words)
711
Faker::Lorem.words #=> ["dolores", "adipisci", "nesciunt"]

lib/faker/default/lorem.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ class << self
1010
#
1111
# @example
1212
# Faker::Lorem.word #=> "soluto"
13+
# Faker::Lorem.word(exclude_words: 'error') #=> "nisi"
14+
# Faker::Lorem.word(exclude_words: 'id, error') #=> "et"
15+
# Faker::Lorem.word(exclude_words: ['id', 'error']) #=> "consequatur"
1316
#
1417
# @faker.version 2.1.3
15-
def word
16-
sample(translate('faker.lorem.words'))
18+
def word(exclude_words: nil)
19+
words(number: 1, exclude_words: exclude_words).first
1720
end
1821

1922
##

test/faker/default/test_faker_lorem.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,28 @@ def test_supplemental_words
4848

4949
# Faker::Lorem.word generates random word from standard wordlist
5050
def test_word
51-
@tester = Faker::Lorem
5251
@standard_wordlist = I18n.translate('faker.lorem.words')
5352

5453
100.times { assert_includes @standard_wordlist, @tester.word }
5554
end
5655

56+
def test_excluded_words_on_word
57+
excluded_words_array = @tester.words(number: 2)
58+
w = @tester.word(exclude_words: excluded_words_array)
59+
60+
assert_not_equal w, excluded_words_array[0]
61+
assert_not_equal w, excluded_words_array[1]
62+
end
63+
64+
def test_excluded_words_as_string_on_word
65+
excluded_words_array = @tester.words(number: 2)
66+
excluded_word_string = excluded_words_array.join(', ')
67+
w = @tester.word(exclude_words: excluded_word_string)
68+
69+
assert_not_equal w, excluded_words_array[0]
70+
assert_not_equal w, excluded_words_array[1]
71+
end
72+
5773
def test_exact_sentence_word_count
5874
assert_equal 2, @tester.sentence(word_count: 2, supplemental: false, random_words_to_add: 0).split.length
5975
end

0 commit comments

Comments
 (0)