From 537a74229e6dd238728454c9df9b1fa81892209c Mon Sep 17 00:00:00 2001 From: James Mead Date: Sun, 3 Nov 2019 19:00:14 +0000 Subject: [PATCH] Distinguish module defining mocha_inspect methods Previously this Mocha::ObjectMethods module was re-opening the module defined in `lib/mocha/object_methods.rb`. I think this was potentially confusing and so I've moved the modules containing the mocha_inspect method definitions to into a new Mocha::Inspect namespace to make things clearer. Note that I've had to amend a couple of tests in ObjectMethodsTest, because they had previously been relying on the Mocha::ObjectMethods being included elsewhere. --- lib/mocha/inspect.rb | 54 +++++++++++++++++--------------- test/unit/object_methods_test.rb | 3 +- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/lib/mocha/inspect.rb b/lib/mocha/inspect.rb index 478651ee4..51975c5f6 100755 --- a/lib/mocha/inspect.rb +++ b/lib/mocha/inspect.rb @@ -1,55 +1,57 @@ require 'date' module Mocha - module ObjectMethods - def mocha_inspect - address = __id__ * 2 - address += 0x100000000 if address < 0 - inspect =~ /#" : inspect + module Inspect + module ObjectMethods + def mocha_inspect + address = __id__ * 2 + address += 0x100000000 if address < 0 + inspect =~ /#" : inspect + end end - end - module ArrayMethods - def mocha_inspect - "[#{collect(&:mocha_inspect).join(', ')}]" + module ArrayMethods + def mocha_inspect + "[#{collect(&:mocha_inspect).join(', ')}]" + end end - end - module HashMethods - def mocha_inspect - "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}" + module HashMethods + def mocha_inspect + "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}" + end end - end - module TimeMethods - def mocha_inspect - "#{inspect} (#{to_f} secs)" + module TimeMethods + def mocha_inspect + "#{inspect} (#{to_f} secs)" + end end - end - module DateMethods - def mocha_inspect - to_s + module DateMethods + def mocha_inspect + to_s + end end end end class Object - include Mocha::ObjectMethods + include Mocha::Inspect::ObjectMethods end class Array - include Mocha::ArrayMethods + include Mocha::Inspect::ArrayMethods end class Hash - include Mocha::HashMethods + include Mocha::Inspect::HashMethods end class Time - include Mocha::TimeMethods + include Mocha::Inspect::TimeMethods end class Date - include Mocha::DateMethods + include Mocha::Inspect::DateMethods end diff --git a/test/unit/object_methods_test.rb b/test/unit/object_methods_test.rb index be15771ba..570bbcd4e 100644 --- a/test/unit/object_methods_test.rb +++ b/test/unit/object_methods_test.rb @@ -44,7 +44,7 @@ def test_should_reset_mocha end def test_should_use_stubba_instance_method_for_object - assert_equal Mocha::InstanceMethod, Object.new.stubba_method + assert_equal Mocha::InstanceMethod, @object.stubba_method end def test_should_stub_self_for_object @@ -57,6 +57,7 @@ def test_nobody_expects_the_spanish_inquisition def test_should_alias_object_method klass = Class.new { def self.method_x; end } + klass.extend(Mocha::ObjectMethods) assert_equal klass._method(:method_x), klass.method(:method_x) end end