Permalink
Browse files

Replace custom Object#__metaclass__ method with a dependency on the "…

…metaid" gem by WhyTheLuckyStiff.
  • Loading branch information...
1 parent 2d09f17 commit f0749d6d291164cc9280aa8ba16f33d652d45fe1 @floehopper committed Jul 13, 2011
View
@@ -1,64 +1,64 @@
-require 'mocha/metaclass'
+require 'metaid'
module Mocha
class ClassMethod
-
+
attr_reader :stubbee, :method
-
+
def initialize(stubbee, method)
@stubbee = stubbee
@method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
end
-
+
def stub
hide_original_method
define_new_method
end
-
+
def unstub
remove_new_method
restore_original_method
stubbee.reset_mocha
end
-
+
def mock
stubbee.mocha
end
-
+
def hide_original_method
if method_exists?(method)
begin
- stubbee.__metaclass__.send(:alias_method, hidden_method, method)
+ stubbee.metaclass.send(:alias_method, hidden_method, method)
rescue NameError
# deal with nasties like ActiveRecord::Associations::AssociationProxy
end
end
end
-
+
def define_new_method
- stubbee.__metaclass__.class_eval(%{
+ stubbee.metaclass.class_eval(%{
def #{method}(*args, &block)
mocha.method_missing(:#{method}, *args, &block)
end
}, __FILE__, __LINE__)
end
-
+
def remove_new_method
- stubbee.__metaclass__.send(:remove_method, method)
+ stubbee.metaclass.send(:remove_method, method)
end
-
+
def restore_original_method
if method_exists?(hidden_method)
begin
- stubbee.__metaclass__.send(:alias_method, method, hidden_method)
- stubbee.__metaclass__.send(:remove_method, hidden_method)
+ stubbee.metaclass.send(:alias_method, method, hidden_method)
+ stubbee.metaclass.send(:remove_method, hidden_method)
rescue NameError
# deal with nasties like ActiveRecord::Associations::AssociationProxy
end
end
end
-
+
def hidden_method
if RUBY_VERSION < '1.9'
method_name = method.to_s.gsub(/\W/) { |s| "_substituted_character_#{s[0]}_" }
@@ -67,25 +67,25 @@ def hidden_method
end
hidden_method = "__stubba__#{method_name}__stubba__"
RUBY_VERSION < '1.9' ? hidden_method.to_s : hidden_method.to_sym
- end
-
+ end
+
def matches?(other)
return false unless (other.class == self.class)
(stubbee.object_id == other.stubbee.object_id) and (method == other.method)
end
-
+
alias_method :==, :eql?
-
+
def to_s
"#{stubbee}.#{method}"
end
-
+
def method_exists?(method)
symbol = method.to_sym
- metaclass = stubbee.__metaclass__
+ metaclass = stubbee.metaclass
metaclass.public_method_defined?(symbol) || metaclass.protected_method_defined?(symbol) || metaclass.private_method_defined?(symbol)
end
end
-
+
end
@@ -20,7 +20,7 @@ def method_exists?(method)
end
def singleton_method?(method)
- metaclass = stubbee.__metaclass__
+ metaclass = stubbee.metaclass
return true if metaclass.public_instance_methods(false).include?(method)
return true if metaclass.protected_instance_methods(false).include?(method)
return true if metaclass.private_instance_methods(false).include?(method)
View
@@ -1,13 +0,0 @@
-module Mocha
-
- module ObjectMethods
- def __metaclass__
- class << self; self; end
- end
- end
-
-end
-
-class Object
- include Mocha::ObjectMethods
-end
View
@@ -1,19 +1,19 @@
+require 'metaid'
require 'mocha/expectation'
require 'mocha/expectation_list'
-require 'mocha/metaclass'
require 'mocha/names'
require 'mocha/method_matcher'
require 'mocha/parameters_matcher'
require 'mocha/unexpected_invocation'
require 'mocha/argument_iterator'
module Mocha # :nodoc:
-
+
# Traditional mock object.
#
# Methods return an Expectation which can be further modified by methods on Expectation.
class Mock
-
+
# :call-seq: expects(method_name) -> expectation
# expects(method_names_vs_return_values) -> last expectation
#
@@ -48,7 +48,7 @@ def expects(method_name_or_hash, backtrace = nil)
@expectations.add(expectation)
}
end
-
+
# :call-seq: stubs(method_name) -> expectation
# stubs(method_names_vs_return_values) -> last expectation
#
@@ -81,7 +81,7 @@ def stubs(method_name_or_hash, backtrace = nil)
@expectations.add(expectation)
}
end
-
+
# :call-seq: responds_like(responder) -> mock
#
# Constrains the +mock+ so that it can only expect or stub methods to which +responder+ responds. The constraint is only applied at method invocation time.
@@ -126,9 +126,9 @@ def responds_like(object)
@responder = object
self
end
-
+
# :stopdoc:
-
+
def initialize(name = nil, &block)
@name = name || DefaultName.new(self)
@expectations = ExpectationList.new
@@ -142,13 +142,13 @@ def initialize(name = nil, &block)
alias_method :__expects__, :expects
alias_method :__stubs__, :stubs
-
+
alias_method :quacks_like, :responds_like
def stub_everything
@everything_stubbed = true
end
-
+
def method_missing(symbol, *arguments, &block)
if @responder and not @responder.respond_to?(symbol)
raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
@@ -165,7 +165,7 @@ def method_missing(symbol, *arguments, &block)
end
end
end
-
+
def respond_to?(symbol, include_private = false)
if @responder then
if @responder.method(:respond_to?).arity > 1
@@ -177,21 +177,21 @@ def respond_to?(symbol, include_private = false)
@everything_stubbed || @expectations.matches_method?(symbol)
end
end
-
+
def __verified__?(assertion_counter = nil)
@expectations.verified?(assertion_counter)
end
-
+
def mocha_inspect
@name.mocha_inspect
end
-
+
def inspect
mocha_inspect
end
-
+
def ensure_method_not_already_defined(method_name)
- self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
+ self.metaclass.send(:undef_method, method_name) if self.metaclass.method_defined?(method_name)
end
# :startdoc:
View
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
s.rubyforge_project = "mocha"
s.summary = "Mocking and stubbing library"
+ s.add_dependency("metaid", "~> 1.0")
if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3
View
@@ -26,7 +26,7 @@ class InstanceSnapshot
attr_reader :methods
def initialize(instance)
- ancestors = [instance.__metaclass__] + instance.class.ancestors
+ ancestors = [instance.metaclass] + instance.class.ancestors
@methods = Set.new(ancestors.map do |ancestor|
ancestor.public_instance_methods(false).map { |method| Method.new(ancestor, method, :public) } +
ancestor.protected_instance_methods(false).map { |method| Method.new(ancestor, method, :protected) } +
@@ -51,15 +51,15 @@ class ClassSnapshot
def initialize(klass)
@methods = Set.new(
- klass.__metaclass__.ancestors.map do |meta_ancestor|
+ klass.metaclass.ancestors.map do |meta_ancestor|
meta_ancestor.public_instance_methods(false).map { |method| Method.new(meta_ancestor, method, :public) } +
meta_ancestor.protected_instance_methods(false).map { |method| Method.new(meta_ancestor, method, :protected) } +
meta_ancestor.private_instance_methods(false).map { |method| Method.new(meta_ancestor, method, :private) }
end.flatten +
klass.ancestors.map do |ancestor|
- (ancestor.__metaclass__.public_instance_methods(false) - ancestor.__metaclass__.ancestors.map { |a| a.public_instance_methods(false) }.flatten - (ancestor.ancestors - [ancestor]).map { |a| a.public_methods(false) }).flatten.map { |method| Method.new(ancestor, method, :public) } +
- (ancestor.__metaclass__.protected_instance_methods(false) - ancestor.__metaclass__.ancestors.map { |a| a.protected_instance_methods(false) }.flatten - (ancestor.ancestors - [ancestor]).map { |a| a.protected_methods(false) }).flatten.map { |method| Method.new(ancestor, method, :protected) } +
- (ancestor.__metaclass__.private_instance_methods(false) - ancestor.__metaclass__.ancestors.map { |a| a.private_instance_methods(false) }.flatten - (ancestor.ancestors - [ancestor]).map { |a| a.private_methods(false) }).flatten.map { |method| Method.new(ancestor, method, :private) }
+ (ancestor.metaclass.public_instance_methods(false) - ancestor.metaclass.ancestors.map { |a| a.public_instance_methods(false) }.flatten - (ancestor.ancestors - [ancestor]).map { |a| a.public_methods(false) }).flatten.map { |method| Method.new(ancestor, method, :public) } +
+ (ancestor.metaclass.protected_instance_methods(false) - ancestor.metaclass.ancestors.map { |a| a.protected_instance_methods(false) }.flatten - (ancestor.ancestors - [ancestor]).map { |a| a.protected_methods(false) }).flatten.map { |method| Method.new(ancestor, method, :protected) } +
+ (ancestor.metaclass.private_instance_methods(false) - ancestor.metaclass.ancestors.map { |a| a.private_instance_methods(false) }.flatten - (ancestor.ancestors - [ancestor]).map { |a| a.private_methods(false) }).flatten.map { |method| Method.new(ancestor, method, :private) }
end.flatten
)
end
View
@@ -1,10 +1,10 @@
-require 'mocha/metaclass'
+require 'metaid'
module Mocha
-
+
module ObjectMethods
def define_instance_method(method_symbol, &block)
- __metaclass__.send(:define_method, method_symbol, block)
+ metaclass.send(:define_method, method_symbol, block)
end
def replace_instance_method(method_symbol, &block)
@@ -13,10 +13,10 @@ def replace_instance_method(method_symbol, &block)
end
def define_instance_accessor(*symbols)
- symbols.each { |symbol| __metaclass__.send(:attr_accessor, symbol) }
+ symbols.each { |symbol| metaclass.send(:attr_accessor, symbol) }
end
end
-
+
end
class Object
@@ -1,22 +0,0 @@
-require File.expand_path('../../test_helper', __FILE__)
-require 'mocha/metaclass'
-
-class MetaclassTest < Test::Unit::TestCase
-
- def test_should_return_objects_singleton_class
- object = Object.new
- assert_raises(NoMethodError) { object.success? }
-
- object = Object.new
- assert object.__metaclass__.ancestors.include?(Object)
- assert object.__metaclass__.ancestors.include?(Kernel)
- assert object.__metaclass__.is_a?(Class)
-
- object.__metaclass__.class_eval { def success?; true; end }
- assert object.success?
-
- object = Object.new
- assert_raises(NoMethodError) { object.success? }
- end
-
-end

0 comments on commit f0749d6

Please sign in to comment.