Skip to content

Commit

Permalink
Simplify implementation of String#mocha_inspect.
Browse files Browse the repository at this point in the history
This is an alternative attempt at fixing the problem identified
and addressed in #215 regarding the "pretty printing" of single- and
double-quotes.

In this attempt, I asked myself why we *ever* needed to do the `gsub`
replacement of escaped double-quote characters with single-quote characters.
I couldn't come up with a reason other than to make the tests more readable.

Thus in this commit I've removed the custom implementation of
`String#mocha_inspect` so it falls back to the default `String#inspect`
implementation. As far as I can see the error messages are still sensibly
formatted and by using the `%{}` string literal construction, I think the
tests are still perfectly readable.

I hope that this is a simpler and more robust way to solve the problems
that @neonichu was seeing.
  • Loading branch information
floehopper authored and chrisroos committed Oct 24, 2016
1 parent 0cc039c commit f187d30
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 26 deletions.
10 changes: 0 additions & 10 deletions lib/mocha/inspect.rb
Expand Up @@ -10,12 +10,6 @@ def mocha_inspect
end
end

module StringMethods
def mocha_inspect
inspect.gsub(/\"/, "'")
end
end

module ArrayMethods
def mocha_inspect
"[#{collect { |member| member.mocha_inspect }.join(', ')}]"
Expand Down Expand Up @@ -46,10 +40,6 @@ class Object
include Mocha::ObjectMethods
end

class String
include Mocha::StringMethods
end

class Array
include Mocha::ArrayMethods
end
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/failure_messages_test.rb
Expand Up @@ -58,7 +58,7 @@ def test_should_display_string_when_expectation_was_on_string
test_result = run_as_test do
'Foo'.expects(:bar)
end
assert_match Regexp.new("'Foo'"), test_result.failures[0].message
assert_match Regexp.new(%{"Foo"}), test_result.failures[0].message
end

end
10 changes: 5 additions & 5 deletions test/acceptance/sequence_test.rb
Expand Up @@ -148,7 +148,7 @@ def test_should_include_sequence_in_failure_message
mock.first
end
assert_failed(test_result)
assert_match Regexp.new("in sequence 'one'"), test_result.failures.first.message
assert_match Regexp.new(%{in sequence "one"}), test_result.failures.first.message
end

def test_should_allow_expectations_to_be_in_more_than_one_sequence
Expand All @@ -166,8 +166,8 @@ def test_should_allow_expectations_to_be_in_more_than_one_sequence
mock.second
end
assert_failed(test_result)
assert_match Regexp.new("in sequence 'one'"), test_result.failures.first.message
assert_match Regexp.new("in sequence 'two'"), test_result.failures.first.message
assert_match Regexp.new(%{in sequence "one"}), test_result.failures.first.message
assert_match Regexp.new(%{in sequence "two"}), test_result.failures.first.message
end

def test_should_have_shortcut_for_expectations_to_be_in_more_than_one_sequence
Expand All @@ -185,8 +185,8 @@ def test_should_have_shortcut_for_expectations_to_be_in_more_than_one_sequence
mock.second
end
assert_failed(test_result)
assert_match Regexp.new("in sequence 'one'"), test_result.failures.first.message
assert_match Regexp.new("in sequence 'two'"), test_result.failures.first.message
assert_match Regexp.new(%{in sequence "one"}), test_result.failures.first.message
assert_match Regexp.new(%{in sequence "two"}), test_result.failures.first.message
end

end
2 changes: 1 addition & 1 deletion test/unit/array_inspect_test.rb
Expand Up @@ -10,7 +10,7 @@ def test_should_use_inspect

def test_should_use_mocha_inspect_on_each_item
array = [1, 2, "chris"]
assert_equal "[1, 2, 'chris']", array.mocha_inspect
assert_equal %{[1, 2, "chris"]}, array.mocha_inspect
end

end
2 changes: 1 addition & 1 deletion test/unit/expectation_test.rb
Expand Up @@ -343,7 +343,7 @@ def test_should_raise_error_with_message_indicating_which_method_was_expected_to
sequence_two = Sequence.new('two')
expectation = Expectation.new(mock, :expected_method).with(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]).in_sequence(sequence_one, sequence_two)
assert !expectation.verified?
assert_match "mock.expected_method(1, 2, {'a' => true}, {:b => false}, [1, 2, 3]); in sequence 'one'; in sequence 'two'", expectation.mocha_inspect
assert_match %{mock.expected_method(1, 2, {"a" => true}, {:b => false}, [1, 2, 3]); in sequence "one"; in sequence "two"}, expectation.mocha_inspect
end

class FakeConstraint
Expand Down
2 changes: 1 addition & 1 deletion test/unit/hash_inspect_test.rb
Expand Up @@ -10,7 +10,7 @@ def test_should_keep_spacing_between_key_value

def test_should_use_mocha_inspect_on_each_item
hash = {:a => 'mocha'}
assert_equal "{:a => 'mocha'}", hash.mocha_inspect
assert_equal %{{:a => "mocha"}}, hash.mocha_inspect
end

end
2 changes: 1 addition & 1 deletion test/unit/parameter_matchers/equals_test.rb
Expand Up @@ -19,7 +19,7 @@ def test_should_not_match_object_that_does_not_equal_value

def test_should_describe_matcher
matcher = equals('x')
assert_equal "'x'", matcher.mocha_inspect
assert_equal %{"x"}, matcher.mocha_inspect
end

end
4 changes: 2 additions & 2 deletions test/unit/parameter_matchers/has_entry_test.rb
Expand Up @@ -31,12 +31,12 @@ def test_should_not_match_hash_not_including_specified_entry

def test_should_describe_matcher_with_key_value_pair
matcher = has_entry(:key_1, 'value_1')
assert_equal "has_entry(:key_1 => 'value_1')", matcher.mocha_inspect
assert_equal %{has_entry(:key_1 => "value_1")}, matcher.mocha_inspect
end

def test_should_describe_matcher_with_entry
matcher = has_entry(:key_1 => 'value_1')
assert_equal "has_entry(:key_1 => 'value_1')", matcher.mocha_inspect
assert_equal %{has_entry(:key_1 => "value_1")}, matcher.mocha_inspect
end

def test_should_match_hash_including_specified_entry_with_nested_key_matcher
Expand Down
2 changes: 1 addition & 1 deletion test/unit/parameter_matchers/has_value_test.rb
Expand Up @@ -21,7 +21,7 @@ def test_should_not_match_hash_not_including_specified_value

def test_should_describe_matcher
matcher = has_value('value_1')
assert_equal "has_value('value_1')", matcher.mocha_inspect
assert_equal %{has_value("value_1")}, matcher.mocha_inspect
end

def test_should_match_hash_including_specified_value_with_nested_value_matcher
Expand Down
2 changes: 1 addition & 1 deletion test/unit/sequence_test.rb
Expand Up @@ -98,7 +98,7 @@ def test_should_describe_ordering_constraint_as_being_part_of_named_sequence
sequence = Sequence.new('wibble')
expectation = FakeExpectation.new
sequence.constrain_as_next_in_sequence(expectation)
assert_equal "in sequence 'wibble'", expectation.ordering_constraints[0].mocha_inspect
assert_equal %{in sequence "wibble"}, expectation.ordering_constraints[0].mocha_inspect
end

end
4 changes: 2 additions & 2 deletions test/unit/string_inspect_test.rb
Expand Up @@ -3,9 +3,9 @@

class StringInspectTest < Mocha::TestCase

def test_should_replace_escaped_quotes_with_single_quote
def test_should_use_default_inspect_method
string = "my_string"
assert_equal "'my_string'", string.mocha_inspect
assert_equal %{"my_string"}, string.mocha_inspect
end

end

0 comments on commit f187d30

Please sign in to comment.