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 Faker::Internet.uuid #1603

Merged
merged 2 commits into from May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/unreleased/default/internet.md
Expand Up @@ -79,4 +79,6 @@ Faker::Internet.slug('foo bar', '-') #=> "foo-bar"
# Optional argument: vendor=nil
Faker::Internet.user_agent #=> "Mozilla/5.0 (compatible; MSIE 9.0; AOL 9.7; AOLBuild 4343.19; Windows NT 6.1; WOW64; Trident/5.0; FunWebProducts)"
Faker::Internet.user_agent(:firefox) #=> "Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0"

Faker::Internet.uuid #=> "929ef6ef-b11f-38c9-111b-accd67a258b2"
```
8 changes: 8 additions & 0 deletions lib/faker/default/internet.rb
Expand Up @@ -192,6 +192,14 @@ def user_agent(vendor = nil)
sample(agents)
end

def uuid
# borrowed from: https://github.com/ruby/ruby/blob/d48783bb0236db505fe1205d1d9822309de53a36/lib/securerandom.rb#L250
ary = Faker::Config.random.bytes(16).unpack('NnnnnN')
ary[2] = (ary[2] & 0x0fff) | 0x4000
ary[3] = (ary[3] & 0x3fff) | 0x8000
'%08x-%04x-%04x-%04x-%04x%08x' % ary # rubocop:disable Style/FormatString
end

alias user_name username
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/faker/default/test_faker_internet.rb
Expand Up @@ -280,4 +280,10 @@ def test_user_agent_with_invalid_argument
assert @tester.user_agent(nil).match(/Mozilla|Opera/)
assert @tester.user_agent(1).match(/Mozilla|Opera/)
end

def test_uuid
uuid = @tester.uuid
assert_equal(36, uuid.size)
assert_match(/\A\h{8}-\h{4}-4\h{3}-\h{4}-\h{12}\z/, uuid)
end
end