Skip to content

Commit 0812ef0

Browse files
authored
fix: Faker::Internet.username should not generate duplicated punctuation (#2970)
* `Faker::Internet.username` should not generate duplicated punctuation * test: Reject duplicated punctuation
1 parent 37a7ea2 commit 0812ef0

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/faker/default/internet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def username(specifier: nil, separators: %w[. _])
6565
with_locale(:en) do
6666
case specifier
6767
when ::String
68-
names = specifier&.gsub("'", '')&.split
68+
names = specifier.gsub("'", '').scan(/[[:word:]]+/)
6969
shuffled_names = shuffle(names)
7070

7171
return shuffled_names.join(sample(separators)).downcase

test/faker/default/test_faker_internet.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ def test_email_with_apostrophes
4444
end
4545
end
4646

47+
def test_email_with_abbreviations
48+
name = 'Mr. Faker'
49+
50+
deterministically_verify -> { @tester.email(name: name) } do |email|
51+
assert_email_regex 'Mr', 'Faker', email
52+
end
53+
end
54+
55+
def test_email_against_name_generator
56+
deterministically_verify -> { @tester.email(name: Faker::Name.unique.name) } do |email|
57+
# reject emails with duplicated punctuation
58+
59+
refute_match(/[\p{Punct}]{2,}/, email)
60+
end
61+
end
62+
4763
def test_email_with_separators
4864
deterministically_verify -> { @tester.email(name: 'jane doe', separators: '+') } do |result|
4965
name, domain = result.split('@')

0 commit comments

Comments
 (0)