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

Faker::Internet.user_name can't handle UTF-8 arguments #1421

Merged
merged 7 commits into from Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion lib/faker/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def safe_email(name = nil)

def username(specifier = nil, separators = %w[. _])
with_locale(:en) do
return shuffle(specifier.scan(/\w+/)).join(sample(separators)).downcase if specifier.respond_to?(:scan)
return shuffle(specifier.scan(/[[:word:]]+/)).join(sample(separators)).downcase if specifier.respond_to?(:scan)

if specifier.is_a?(Integer)
# If specifier is Integer and has large value, Argument error exception is raised to overcome memory full error
Expand Down
4 changes: 4 additions & 0 deletions test/test_faker_internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def test_username_with_integer_arg
end
end

def test_username_with_utf_8_arg
assert @tester.username('Łucja').match('Łucja')
end
Copy link
Member

@vbrazo vbrazo Oct 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your solution looks good. What's killing you is the .downcase at the end of the shuffle method: return shuffle(specifier.scan(/[[:word:]]+/)).join(sample(separators)).downcase if specifier.respond_to?(:scan)

From: /faker-ruby/faker/test/test_faker_internet.rb @ line 53 TestFakerInternet#test_username_with_utf_8_arg:

    51: def test_username_with_utf_8_arg
    52:   require 'pry'
 => 53:   binding.pry
    54:   assert @tester.username('Łucja').match('Łucja')
    55: end

[1] pry(#<TestFakerInternet>)> @tester.username('Łucja')
=> "łucja"


def test_username_with_very_large_integer_arg
exception = assert_raises(ArgumentError) { @tester.username(10_000_000) }
assert_equal('Given argument is too large', exception.message)
Expand Down