lib/muack/mock.rb in muack-1.6.0 vs lib/muack/mock.rb in muack-1.7.0
- old
+ new
@@ -129,41 +129,23 @@
remove_method(defi.original_method)
end
end
end
- if ::Class.instance_method(:method_defined?).arity == 1 # Ruby 2.5-
- def self.direct_method_defined? mod, msg
- mod.public_instance_methods(false).include?(msg) ||
- mod.protected_instance_methods(false).include?(msg) ||
- mod.private_instance_methods(false).include?(msg)
- end
+ def self.direct_method_defined? mod, msg
+ mod.method_defined?(msg, false) || # this doesn't cover private method
+ mod.private_method_defined?(msg, false)
+ end
- def self.method_visibility mod, msg
- if mod.public_instance_methods(false).include?(msg)
- :public
- elsif mod.protected_instance_methods(false).include?(msg)
- :protected
- elsif mod.private_instance_methods(false).include?(msg)
- :private
- end
+ def self.method_visibility mod, msg, inherit=false
+ if mod.public_method_defined?(msg, inherit)
+ :public
+ elsif mod.protected_method_defined?(msg, inherit)
+ :protected
+ elsif mod.private_method_defined?(msg, inherit)
+ :private
end
- else # Ruby 2.6+
- def self.direct_method_defined? mod, msg
- mod.method_defined?(msg, false) || # this doesn't cover private method
- mod.private_method_defined?(msg, false)
- end
-
- def self.method_visibility mod, msg
- if mod.public_method_defined?(msg, false)
- :public
- elsif mod.protected_method_defined?(msg, false)
- :protected
- elsif mod.private_method_defined?(msg, false)
- :private
- end
- end
end
def self.prepare_target singleton_class, msg
if singleton_class == singleton_class.ancestors.first # no prepended mod
singleton_class
@@ -189,15 +171,19 @@
end
end
def self.store_original_method mod, defi
if visibility = method_visibility(mod, defi.msg)
+ # Defined in itself, we need to make alias
original_method = find_new_name(mod, defi.msg)
mod.__send__(:alias_method, original_method, defi.msg)
defi.original_method = original_method
defi.visibility = visibility
- else
+ elsif visibility = method_visibility(mod, defi.msg, true)
+ # Defined in parent, we can just override
+ defi.visibility = visibility
+ else # Not defined, pick a reasonable default
defi.visibility = :public
end
end
def self.find_new_name mod, message, level=0
@@ -290,15 +276,15 @@
def __mock_super_with_kargs? object, method_name
super_method =
::Kernel.instance_method(:method).bind(object).call(method_name).
super_method
- if super_method.owner == ::Class && super_method.name == :new
+ if super_method.owner.kind_of?(::Class) && super_method.name == :new
initialize_method = ::Class.instance_method(:instance_method).
bind(object).call(:initialize)
__mock_block_with_kargs?(initialize_method)
else
- super_method && __mock_block_with_kargs?(super_method)
+ __mock_block_with_kargs?(super_method)
end
end
def __mock_block_with_kargs? block
# there's no Symbol#start_with? in older Ruby