Skip to content
Open
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
5 changes: 4 additions & 1 deletion lib/faker/default/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def build_shallow_hash(width, options)
value = options[:value]

hash = {}
width.times do
# retry on duplicate
(width * 10).times do
break if hash.size == width

hash[eval(key)] = eval(value)
end
hash
Expand Down
9 changes: 9 additions & 0 deletions test/faker/default/test_faker_json.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# frozen_string_literal: true

require 'minitest/mock'
require_relative '../../test_helper'

class TestFakerJson < Test::Unit::TestCase
require 'json'

def test_shallow_json_keeps_width_when_keys_collide
Faker::Config.stub :random, Random.new(0) do
json = Faker::Json.shallow_json(width: 2, options: { key: 'Boolean.boolean', value: 'Boolean.boolean' })

assert_equal 2, JSON.parse(json).size
end
end

def test_simple_json_lenght
expected_result_length = JSON.parse(simple_json).flatten.length

Expand Down