Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling the usage of open compounds in sentences #2109

Merged
merged 2 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/books/lovecraft.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ Faker::Books::Lovecraft.location #=> "Kingsport"

Faker::Books::Lovecraft.word #=> "furtive"

# Keyword arguments: word_count, random_words_to_add
# Keyword arguments: word_count, random_words_to_add, open_compounds_allowed
Faker::Books::Lovecraft.sentence #=> "Furtive antiquarian squamous dank cat loathsome amorphous lurk."
Faker::Books::Lovecraft.sentence(word_count: 3) #=> "Daemoniac antediluvian fainted squamous comprehension gambrel nameless singular."
Faker::Books::Lovecraft.sentence(word_count: 3, random_words_to_add: 1) #=> "Amorphous indescribable tenebrous."
Faker::Books::Lovecraft.sentence(word_count: 3, random_words_to_add: 0, open_compounds_allowed: true) #=> "Effulgence unmentionable gambrel."

# Keyword arguments: number, spaces_allowed
Faker::Books::Lovecraft.words #=> ["manuscript", "abnormal", "singular"]
Expand Down
4 changes: 3 additions & 1 deletion doc/default/hipster.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ Faker::Hipster.words(number: 4) #=> ["ugh", "cardigan", "poutine", "stumptown"]
Faker::Hipster.words(number: 4, supplemental: true) #=> ["iste", "seitan", "normcore", "provident"]
Faker::Hipster.words(number: 4, supplemental: true, spaces_allowed: true) #=> ["qui", "magni", "craft beer", "est"]

# Keyword arguments: word_count, supplemental, random_words_to_add
# Keyword arguments: word_count, supplemental, random_words_to_add, open_compounds_allowed
Faker::Hipster.sentence #=> "Park iphone leggings put a bird on it."
Faker::Hipster.sentence(word_count: 3) #=> "Pour-over swag godard."
Faker::Hipster.sentence(word_count: 3, supplemental: true) #=> "Beard laboriosam sequi celiac."
Faker::Hipster.sentence(word_count: 3, supplemental: false, random_words_to_add: 4) #=> "Bitters retro mustache aesthetic biodiesel 8-bit."
Faker::Hipster.sentence(word_count: 3, supplemental: true, random_words_to_add: 4) #=> "Occaecati deleniti messenger bag meh crucifix autem."
Faker::Hipster.sentence(word_count: 3, supplemental: true, random_words_to_add: 0, open_compounds_allowed: true) #=> "Kale chips nihil eos."
Faker::Hipster.sentence(word_count: 3, supplemental: true, random_words_to_add: 0, open_compounds_allowed: false) #=> "Dreamcatcher umami fixie."

# Keyword arguments: number, supplemental
Faker::Hipster.sentences #=> ["Godard pitchfork vinegar chillwave everyday 90's whatever.", "Pour-over artisan distillery street waistcoat.", "Salvia yr leggings franzen blue bottle."]
Expand Down
8 changes: 6 additions & 2 deletions lib/faker/books/lovecraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def tome
#
# @param word_count [Integer] The number of words to have in the sentence
# @param random_words_to_add [Integer]
# @param open_compounds_allowed [Boolean] If true, generated sentence can contain words having additional spaces
#
# @return [String]
#
Expand All @@ -80,15 +81,18 @@ def tome
# @example
# Faker::Books::Lovecraft.sentence(word_count: 3, random_words_to_add: 1)
# #=> "Amorphous indescribable tenebrous."
# @example
# Faker::Books::Lovecraft.sentence(word_count: 3, random_words_to_add: 0, open_compounds_allowed: true)
# #=> "Effulgence unmentionable gambrel."
#
# @faker.version 1.9.3
def sentence(legacy_word_count = NOT_GIVEN, legacy_random_words_to_add = NOT_GIVEN, word_count: 4, random_words_to_add: 6)
def sentence(legacy_word_count = NOT_GIVEN, legacy_random_words_to_add = NOT_GIVEN, word_count: 4, random_words_to_add: 6, open_compounds_allowed: true)
warn_for_deprecated_arguments do |keywords|
keywords << :word_count if legacy_word_count != NOT_GIVEN
keywords << :random_words_to_add if legacy_random_words_to_add != NOT_GIVEN
end

words(number: word_count + rand(random_words_to_add.to_i).to_i, spaces_allowed: true).join(' ').capitalize + '.'
words(number: word_count + rand(random_words_to_add.to_i).to_i, spaces_allowed: open_compounds_allowed).join(' ').capitalize + '.'
end

##
Expand Down
9 changes: 6 additions & 3 deletions lib/faker/default/hipster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def words(legacy_number = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, legacy_spa
#
# @param word_count [Integer] Specifies the number of words in the sentence
# @param supplemental [Boolean] Specifies if the words are supplemental
# @param random_words_to_add [Boolean] Specifies the number of random words to add
# @param random_words_to_add [Integer] Specifies the number of random words to add
# @param open_compounds_allowed [Boolean] Specifies if the generated sentence can contain words having additional spaces
# @return [String]
#
# @example
Expand All @@ -68,18 +69,20 @@ def words(legacy_number = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, legacy_spa
# Faker::Hipster.sentence(word_count: 3, supplemental: true) #=> "Beard laboriosam sequi celiac."
# Faker::Hipster.sentence(word_count: 3, supplemental: false, random_words_to_add: 4) #=> "Bitters retro mustache aesthetic biodiesel 8-bit."
# Faker::Hipster.sentence(word_count: 3, supplemental: true, random_words_to_add: 4) #=> "Occaecati deleniti messenger bag meh crucifix autem."
# Faker::Hipster.sentence(word_count: 3, supplemental: true, random_words_to_add: 0, open_compounds_allowed: true) #=> "Kale chips nihil eos."
# Faker::Hipster.sentence(word_count: 3, supplemental: true, random_words_to_add: 0, open_compounds_allowed: false) #=> "Dreamcatcher umami fixie."
#
# @faker.version 1.6.0
# rubocop:disable Metrics/ParameterLists
def sentence(legacy_word_count = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, legacy_random_words_to_add = NOT_GIVEN, word_count: 4, supplemental: false, random_words_to_add: 6)
def sentence(legacy_word_count = NOT_GIVEN, legacy_supplemental = NOT_GIVEN, legacy_random_words_to_add = NOT_GIVEN, word_count: 4, supplemental: false, random_words_to_add: 6, open_compounds_allowed: true)
# rubocop:enable Metrics/ParameterLists
warn_for_deprecated_arguments do |keywords|
keywords << :word_count if legacy_word_count != NOT_GIVEN
keywords << :supplemental if legacy_supplemental != NOT_GIVEN
keywords << :random_words_to_add if legacy_random_words_to_add != NOT_GIVEN
end

words(number: word_count + rand(random_words_to_add.to_i).to_i, supplemental: supplemental, spaces_allowed: true).join(' ').capitalize + '.'
words(number: word_count + rand(random_words_to_add.to_i).to_i, supplemental: supplemental, spaces_allowed: open_compounds_allowed).join(' ').capitalize + '.'
end

##
Expand Down
15 changes: 15 additions & 0 deletions test/faker/books/test_lovecraft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ def test_words_with_large_count_params
assert(array.length == 250 || array.length == 500)
end

def test_sentence_with_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: true)
assert(sentence.split.length >= 5)
end
end

# Sentence should not contain any open compounds
def test_sentence_without_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: false)
assert(sentence.split.length == 5)
end
end

def test_paragraph_char_count
paragraph = @tester.paragraph_by_chars
assert(paragraph.length == 256)
Expand Down
15 changes: 15 additions & 0 deletions test/faker/default/test_faker_hipster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ def test_words_with_large_count_params
assert(array.length == 250 || array.length == 500)
end

def test_sentence_with_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: true)
assert(sentence.split.length >= 5)
end
end

# Sentence should not contain any open compounds
def test_sentence_without_open_compounds_allowed
1000.times do
sentence = @tester.sentence(word_count: 5, random_words_to_add: 0, open_compounds_allowed: false)
assert(sentence.split.length == 5)
end
end

def test_paragraph_char_count
paragraph = @tester.paragraph_by_chars(characters: 256)
assert(paragraph.length == 256)
Expand Down