Skip to content

Commit

Permalink
Provide wrapped option for #mocha_inspect on hashes & arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
floehopper committed Jun 20, 2021
1 parent 9c4ef13 commit d8f44bc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/mocha/inspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ def mocha_inspect
end

module ArrayMethods
def mocha_inspect
"[#{collect(&:mocha_inspect).join(', ')}]"
def mocha_inspect(wrapped = true)
unwrapped = collect(&:mocha_inspect).join(', ')
wrapped ? "[#{unwrapped}]" : unwrapped
end
end

module HashMethods
def mocha_inspect
"{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
def mocha_inspect(wrapped = true)
unwrapped = collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')
wrapped ? "{#{unwrapped}}" : unwrapped
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/unit/array_inspect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def test_should_use_inspect
assert_equal array.inspect, array.mocha_inspect
end

def test_should_return_unwrapped_array_when_wrapped_is_false
array = [1, 2]
assert_equal '1, 2', array.mocha_inspect(false)
end

def test_should_use_mocha_inspect_on_each_item
array = [1, 2, 'chris']
assert_equal %([1, 2, "chris"]), array.mocha_inspect
Expand Down
5 changes: 5 additions & 0 deletions test/unit/hash_inspect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ def test_should_keep_spacing_between_key_value
assert_equal '{:a => true}', hash.mocha_inspect
end

def test_should_return_unwrapped_hash_when_wrapped_is_false
hash = { :a => true }
assert_equal ':a => true', hash.mocha_inspect(false)
end

def test_should_use_mocha_inspect_on_each_item
hash = { :a => 'mocha' }
assert_equal %({:a => "mocha"}), hash.mocha_inspect
Expand Down

0 comments on commit d8f44bc

Please sign in to comment.