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 type support for Faker::Types.rb_array #2771

Merged
merged 3 commits into from
Jun 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/default/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Faker::Types.complex_rb_hash(number: 2) #=> {user: {first: "bob", last: "marley"
# Random Array
Faker::Types.rb_array #=> ["a"]
Faker::Types.rb_array(len: 4) #=> ["a", 1, 2, "bob"]
Faker::Types.rb_array(len: 2, type: -> { Faker::Types.rb_string }) #=> ["cat", "foo"]

# Random Type (string, or integer)
Faker::Types.random_type #=> 1 or "a" or "bob"
Expand Down
5 changes: 3 additions & 2 deletions lib/faker/default/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ def complex_rb_hash(number: 1)
# @example
# Faker::Types.rb_array #=> ["a"]
# Faker::Types.rb_array(len: 4) #=> ["a", 1, 2, "bob"]
# Faker::Types.rb_array(len: 2, type: -> { Faker::Types.rb_string }) #=> ["cat", "foo"]
#
# @faker.version 1.8.6
def rb_array(len: 1)
def rb_array(len: 1, type: -> { random_type })
[].tap do |ar|
len.times do
ar.push random_type
ar.push type.is_a?(Proc) ? type.call : type
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/faker/default/test_faker_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def test_rb_array_returns_array
assert_instance_of Array, @tester.rb_array
end

def test_rb_array_returns_right_type_of_array
@tester.rb_array(len: 3, type: -> { @tester.rb_string }).each do |value|
assert_instance_of String, value
end
end

def test_array_has_the_right_array
assert_equal(3, @tester.rb_array(len: 3).length)
assert_empty @tester.rb_array(len: 0)
Expand Down