Skip to content

Commit

Permalink
type support for Faker::Types.rb_array (faker-ruby#2771)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruban-thilak authored Jun 19, 2023
1 parent 7ac1c37 commit 4558f1b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
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

0 comments on commit 4558f1b

Please sign in to comment.