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

Add user generator that accepts arguments (#1671) #1730

Merged
merged 11 commits into from Aug 20, 2021
6 changes: 5 additions & 1 deletion doc/default/internet.md
@@ -1,7 +1,11 @@
# Faker::Internet

```ruby
# Keyword arguments: name, separators, domain
# Keyword arguments: name, username, email, password, domain_name, user_agent, uuid etc...
Faker::Internet.user #=> { username: 'alexie', email: 'alexie@example.net' }
Faker::Internet.user('username', 'email', 'password') #=> { username: 'alexie', email: 'alexie@example.net', password: 'DtEf9P8wS31iMyC' }

# Keyword arguments: name, separators
Faker::Internet.email #=> "eliza@mann.net"
Faker::Internet.email(name: 'Nancy') #=> "nancy@terry.biz"
Faker::Internet.email(name: 'Janelle Santiago', separators: '+') #=> "janelle+santiago@becker.com"
Expand Down
35 changes: 12 additions & 23 deletions lib/faker/default/internet.rb
Expand Up @@ -531,32 +531,21 @@ def uuid
end

##
# Produces a random string of alphabetic characters, (no digits)
#
# @param length [Integer] The length of the string to generate
# @param padding [Boolean] Toggles if a final equal '=' will be added.
# @param urlsafe [Boolean] Toggles charset to '-' and '_' instead of '+' and '/'.
#
# @return [String]
#
# Produces a randomized hash of internet user details
# @example
# Faker::Internet.base64
# #=> "r_hbZ2DSD-ZACzZT"
# Faker::Internet.user #=> { username: 'alexie', email: 'alexie@example.net' }
#
# @example
# Faker::Internet.base64(length: 4, padding: true, urlsafe: false)
# #=> "x1/R="
# Faker::Internet.user('username', 'email', 'password') #=> { username: 'alexie', email: 'alexie@example.net', password: 'DtEf9P8wS31iMyC' }
#
# @faker.version 2.11.0
def base64(length: 16, padding: false, urlsafe: true)
char_range = [
Array('0'..'9'),
Array('A'..'Z'),
Array('a'..'z'),
urlsafe ? %w[- _] : %w[+ /]
].flatten
s = Array.new(length) { sample(char_range) }.join
s += '=' if padding
s
# @return [hash]
#
# @faker.version next
def user(*args)
ashishra0 marked this conversation as resolved.
Show resolved Hide resolved
user_hash = {}
args = %w[username email] if args.empty?
args.each { |arg| user_hash[:"#{arg}"] = send(arg) }
user_hash
end

alias user_name username
Expand Down
22 changes: 17 additions & 5 deletions test/faker/default/test_faker_internet.rb
Expand Up @@ -333,10 +333,22 @@ def test_uuid
assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid)
end

def test_base64
assert_match(/[[[:alnum:]]\-_]{16}/, @tester.base64)
assert_match(/[[[:alnum:]]\-_]{4}/, @tester.base64(length: 4))
assert_match(/[[[:alnum:]]\-_]{16}=/, @tester.base64(padding: true))
assert_match(/[[[:alnum:]]+\/]{16}/, @tester.base64(urlsafe: false))
def test_user_with_args
user = @tester.user('username', 'email', 'password')
assert user[:username].match(/[a-z]+((_|\.)[a-z]+)?/)
assert user[:email].match(/.+@.+\.\w+/)
assert user[:password].match(/\w{3}/)
end

def test_user_without_args
user = @tester.user
assert user[:username].match(/[a-z]+((_|\.)[a-z]+)?/)
assert user[:email].match(/.+@.+\.\w+/)
end

def test_user_with_invalid_args
assert_raises NoMethodError do
@tester.user('xyx')
end
end
end