Permalink
Browse files

As per Jay Fields recommendations [1] and with further impetus from a…

… talk at Ruby Manor, any methods added to core classes are now added by including a module. This means that Mocha is a better citizen of the Ruby world and it's behaviour is more easily extended. [1] http://blog.jayfields.com/2008/07/ruby-underuse-of-modules.html & http://blog.jayfields.com/2008/07/ruby-redefine-method-behavior.html
  • Loading branch information...
1 parent 36f20a4 commit 3251107c3ad9cc241ac1b4191a55a59c9e59187f @floehopper floehopper committed Nov 22, 2008
Showing with 200 additions and 138 deletions.
  1. +48 −20 lib/mocha/inspect.rb
  2. +9 −3 lib/mocha/metaclass.rb
  3. +118 −102 lib/mocha/object.rb
  4. +9 −3 lib/mocha/parameter_matchers/object.rb
  5. +16 −10 test/method_definer.rb
View
@@ -1,39 +1,67 @@
require 'date'
-class Object
- def mocha_inspect
- address = self.__id__ * 2
- address += 0x100000000 if address < 0
- inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect
+module Mocha
+
+ module ObjectMethods
+ def mocha_inspect
+ address = self.__id__ * 2
+ address += 0x100000000 if address < 0
+ inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect
+ end
+ end
+
+ module StringMethods
+ def mocha_inspect
+ inspect.gsub(/\"/, "'")
+ end
+ end
+
+ module ArrayMethods
+ def mocha_inspect
+ "[#{collect { |member| member.mocha_inspect }.join(', ')}]"
+ end
+ end
+
+ module HashMethods
+ def mocha_inspect
+ "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
+ end
end
+
+ module TimeMethods
+ def mocha_inspect
+ "#{inspect} (#{to_f} secs)"
+ end
+ end
+
+ module DateMethods
+ def mocha_inspect
+ to_s
+ end
+ end
+
+end
+
+class Object
+ include Mocha::ObjectMethods
end
class String
- def mocha_inspect
- inspect.gsub(/\"/, "'")
- end
+ include Mocha::StringMethods
end
class Array
- def mocha_inspect
- "[#{collect { |member| member.mocha_inspect }.join(', ')}]"
- end
+ include Mocha::ArrayMethods
end
class Hash
- def mocha_inspect
- "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
- end
+ include Mocha::HashMethods
end
class Time
- def mocha_inspect
- "#{inspect} (#{to_f} secs)"
- end
+ include Mocha::TimeMethods
end
class Date
- def mocha_inspect
- to_s
- end
+ include Mocha::DateMethods
end
View
@@ -1,7 +1,13 @@
-class Object
+module Mocha
- def __metaclass__
- class << self; self; end
+ module ObjectMethods
+ def __metaclass__
+ class << self; self; end
+ end
end
+
+end
+class Object
+ include Mocha::ObjectMethods
end
View
@@ -4,131 +4,147 @@
require 'mocha/module_method'
require 'mocha/any_instance_method'
-# Methods added all objects to allow mocking and stubbing on real objects.
-#
-# Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
-class Object
+module Mocha
- def mocha # :nodoc:
- @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
- end
+ # Methods added all objects to allow mocking and stubbing on real objects.
+ #
+ # Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
+ module ObjectMethods
- def reset_mocha # :nodoc:
- @mocha = nil
- end
+ def mocha # :nodoc:
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
+ end
- def stubba_method # :nodoc:
- Mocha::InstanceMethod
- end
+ def reset_mocha # :nodoc:
+ @mocha = nil
+ end
- def stubba_object # :nodoc:
- self
- end
+ def stubba_method # :nodoc:
+ Mocha::InstanceMethod
+ end
- # :call-seq: expects(symbol) -> expectation
- #
- # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
- # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
- # product = Product.new
- # product.expects(:save).returns(true)
- # assert_equal false, product.save
- #
- # The original implementation of <tt>Product#save</tt> is replaced temporarily.
- #
- # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
- def expects(symbol)
- mockery = Mocha::Mockery.instance
- mockery.on_stubbing(self, symbol)
- method = stubba_method.new(stubba_object, symbol)
- mockery.stubba.stub(method)
- mocha.expects(symbol, caller)
- end
+ def stubba_object # :nodoc:
+ self
+ end
- # :call-seq: stubs(symbol) -> expectation
- #
- # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
- # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
- # product = Product.new
- # product.stubs(:save).returns(true)
- # assert_equal false, product.save
- #
- # The original implementation of <tt>Product#save</tt> is replaced temporarily.
- #
- # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
- def stubs(symbol)
- mockery = Mocha::Mockery.instance
- mockery.on_stubbing(self, symbol)
- method = stubba_method.new(stubba_object, symbol)
- mockery.stubba.stub(method)
- mocha.stubs(symbol, caller)
- end
+ # :call-seq: expects(symbol) -> expectation
+ #
+ # Adds an expectation that a method identified by +symbol+ must be called exactly once with any parameters.
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
+ # product = Product.new
+ # product.expects(:save).returns(true)
+ # assert_equal false, product.save
+ #
+ # The original implementation of <tt>Product#save</tt> is replaced temporarily.
+ #
+ # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
+ def expects(symbol)
+ mockery = Mocha::Mockery.instance
+ mockery.on_stubbing(self, symbol)
+ method = stubba_method.new(stubba_object, symbol)
+ mockery.stubba.stub(method)
+ mocha.expects(symbol, caller)
+ end
- def method_exists?(method, include_public_methods = true)
- if include_public_methods
- return true if public_methods(include_superclass_methods = true).include?(method)
- return true if respond_to?(method.to_sym)
+ # :call-seq: stubs(symbol) -> expectation
+ #
+ # Adds an expectation that a method identified by +symbol+ may be called any number of times with any parameters.
+ # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
+ # product = Product.new
+ # product.stubs(:save).returns(true)
+ # assert_equal false, product.save
+ #
+ # The original implementation of <tt>Product#save</tt> is replaced temporarily.
+ #
+ # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
+ def stubs(symbol)
+ mockery = Mocha::Mockery.instance
+ mockery.on_stubbing(self, symbol)
+ method = stubba_method.new(stubba_object, symbol)
+ mockery.stubba.stub(method)
+ mocha.stubs(symbol, caller)
end
- return true if protected_methods(include_superclass_methods = true).include?(method)
- return true if private_methods(include_superclass_methods = true).include?(method)
- return false
- end
-end
-
-class Module # :nodoc:
+ def method_exists?(method, include_public_methods = true) # :nodoc:
+ if include_public_methods
+ return true if public_methods(include_superclass_methods = true).include?(method)
+ return true if respond_to?(method.to_sym)
+ end
+ return true if protected_methods(include_superclass_methods = true).include?(method)
+ return true if private_methods(include_superclass_methods = true).include?(method)
+ return false
+ end
- def stubba_method
- Mocha::ModuleMethod
end
-
-end
-
-class Class
- def stubba_method # :nodoc:
- Mocha::ClassMethod
- end
-
- class AnyInstance # :nodoc:
+ module ModuleMethods # :nodoc:
- def initialize(klass)
- @stubba_object = klass
+ def stubba_method
+ Mocha::ModuleMethod
end
- def mocha
- @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
+ end
+
+ # Methods added all classes to allow mocking and stubbing on real objects.
+ module ClassMethods
+
+ def stubba_method # :nodoc:
+ Mocha::ClassMethod
end
- def stubba_method
- Mocha::AnyInstanceMethod
- end
+ class AnyInstance # :nodoc:
- def stubba_object
- @stubba_object
- end
+ def initialize(klass)
+ @stubba_object = klass
+ end
- def method_exists?(method, include_public_methods = true)
- if include_public_methods
- return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
+ def mocha
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
+ end
+
+ def stubba_method
+ Mocha::AnyInstanceMethod
end
- return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
- return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
- return false
- end
- end
+ def stubba_object
+ @stubba_object
+ end
+
+ def method_exists?(method, include_public_methods = true)
+ if include_public_methods
+ return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
+ end
+ return true if @stubba_object.protected_instance_methods(include_superclass_methods = true).include?(method)
+ return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
+ return false
+ end
+
+ end
+
+ # :call-seq: any_instance -> mock object
+ #
+ # Returns a mock object which will detect calls to any instance of this class.
+ # Product.any_instance.stubs(:save).returns(false)
+ # product_1 = Product.new
+ # assert_equal false, product_1.save
+ # product_2 = Product.new
+ # assert_equal false, product_2.save
+ def any_instance
+ @any_instance ||= AnyInstance.new(self)
+ end
- # :call-seq: any_instance -> mock object
- #
- # Returns a mock object which will detect calls to any instance of this class.
- # Product.any_instance.stubs(:save).returns(false)
- # product_1 = Product.new
- # assert_equal false, product_1.save
- # product_2 = Product.new
- # assert_equal false, product_2.save
- def any_instance
- @any_instance ||= AnyInstance.new(self)
end
end
+class Object # :nodoc:
+ include Mocha::ObjectMethods
+end
+
+class Module # :nodoc:
+ include Mocha::ModuleMethods
+end
+
+class Class # :nodoc:
+ include Mocha::ClassMethods
+end
@@ -1,9 +1,15 @@
require 'mocha/parameter_matchers/equals'
-class Object
+module Mocha
- def to_matcher # :nodoc:
- Mocha::ParameterMatchers::Equals.new(self)
+ module ObjectMethods
+ def to_matcher # :nodoc:
+ Mocha::ParameterMatchers::Equals.new(self)
+ end
end
end
+
+class Object
+ include Mocha::ObjectMethods
+end
Oops, something went wrong.

0 comments on commit 3251107

Please sign in to comment.