Skip to content

Commit

Permalink
fix: Faker::Internet.username should not generate duplicated punctu…
Browse files Browse the repository at this point in the history
…ation (#2970)

* `Faker::Internet.username` should not generate duplicated punctuation

* test: Reject duplicated punctuation
  • Loading branch information
thdaraujo authored Jun 20, 2024
1 parent 37a7ea2 commit 0812ef0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/faker/default/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def username(specifier: nil, separators: %w[. _])
with_locale(:en) do
case specifier
when ::String
names = specifier&.gsub("'", '')&.split
names = specifier.gsub("'", '').scan(/[[:word:]]+/)
shuffled_names = shuffle(names)

return shuffled_names.join(sample(separators)).downcase
Expand Down
16 changes: 16 additions & 0 deletions test/faker/default/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ def test_email_with_apostrophes
end
end

def test_email_with_abbreviations
name = 'Mr. Faker'

deterministically_verify -> { @tester.email(name: name) } do |email|
assert_email_regex 'Mr', 'Faker', email
end
end

def test_email_against_name_generator
deterministically_verify -> { @tester.email(name: Faker::Name.unique.name) } do |email|
# reject emails with duplicated punctuation
# e.g.: "[email protected]"
refute_match(/[\p{Punct}]{2,}/, email)
end
end

def test_email_with_separators
deterministically_verify -> { @tester.email(name: 'jane doe', separators: '+') } do |result|
name, domain = result.split('@')
Expand Down

0 comments on commit 0812ef0

Please sign in to comment.