Skip to content

Commit

Permalink
Add user generator that accepts arguments (#1671) (#1730)
Browse files Browse the repository at this point in the history
* Add user generator that accepts arguments (#1671)
  The new user class method will now allow you to do this

  ```
  user = Faker::Internet.user('name', 'email', password')
  => {'name' => 'John Doe', 'email' => 'fake_email@test.com', 'password' => 'pass123' }
  ```

* Add user generator that accepts arguments (#1671)
  The new user class method will now allow you to do this

  ```
  user = Faker::Internet.user('name', 'email', password')
  => {'name' => 'John Doe', 'email' => 'fake_email@test.com', 'password' => 'pass123' }
  ```

* add yard docs for user generator

* Add user generator that accepts arguments (#1671)
  The new user class method will now allow you to do this

  ```
  user = Faker::Internet.user('name', 'email', password')
  => {'name' => 'John Doe', 'email' => 'fake_email@test.com', 'password' => 'pass123' }
  ```

* add yard docs for user generator

* reduce hash tags

Co-authored-by: Stephen A. Wilson <stephen-356@hotmail.com>
  • Loading branch information
ashishra0 and Zeragamba committed Aug 20, 2021
1 parent 45bdf35 commit dbe1362
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
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)
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

0 comments on commit dbe1362

Please sign in to comment.