Skip to content

Commit

Permalink
Distinguish module defining mocha_inspect methods
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
floehopper committed Nov 3, 2019
1 parent ab619c8 commit 537a742
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
54 changes: 28 additions & 26 deletions lib/mocha/inspect.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
require 'date'

module Mocha
module ObjectMethods
def mocha_inspect
address = __id__ * 2
address += 0x100000000 if address < 0
inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%x', address)}>" : inspect
module Inspect
module ObjectMethods
def mocha_inspect
address = __id__ * 2
address += 0x100000000 if address < 0
inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%x', address)}>" : 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
3 changes: 2 additions & 1 deletion test/unit/object_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 537a742

Please sign in to comment.