Add specs for singleton methods matcher

Also cleanup some dead code
This commit is contained in:
Markus Schirp 2013-04-27 18:21:38 +02:00
parent ecb5be355f
commit 1ea05148f1
2 changed files with 95 additions and 40 deletions

View file

@ -24,8 +24,6 @@ module Mutant
self
end
private
# Return method matcher class
#
# @return [Class:Matcher::Method]
@ -36,6 +34,22 @@ module Mutant
self.class::MATCHER
end
# Return methods
#
# @return [Enumerable<Method, UnboundMethod>]
#
# @api private
#
def methods
candidate_names.each_with_object([]) do |name, methods|
method = access(name)
methods << method if method.owner == candidate_scope
end
end
memoize :methods
private
# Emit matches for method
#
# @param [UnboundMethod, Method] method
@ -50,20 +64,6 @@ module Mutant
end
end
# Return methods
#
# @return [Enumerable<Method, UnboundMethod>]
#
# @api private
#
def methods
candidates.each_with_object([]) do |name, methods|
method = access(name)
methods << method if method.owner == scope
end
end
memoize :methods
# Return candidate names
#
# @param [Object] object
@ -72,8 +72,8 @@ module Mutant
#
# @api private
#
def candidates
object = self.scope
def candidate_names
object = candidate_scope
names =
object.public_instance_methods(false) +
object.private_instance_methods(false) +
@ -81,9 +81,19 @@ module Mutant
names.sort
end
# Return candidate scope
#
# @return [Class, Module]
#
# @api private
#
abstract_method :candidate_scope
class Singleton < self
MATCHER = Matcher::Method::Singleton
private
# Return method for name
#
# @param [Symbol] method_name
@ -96,27 +106,24 @@ module Mutant
scope.method(method_name)
end
private
# Return singleton methods defined on scope
# Return candidate scope
#
# @param [Class|Module] scope
#
# @return [Enumerable<Symbol>]
# @return [Class]
#
# @api private
#
def method_names
singleton_class = scope.singleton_class
candidate_names.sort.reject do |name|
name.to_sym == :__class_init__
end
def candidate_scope
scope.singleton_class
end
memoize :candidate_scope, :freezer => :noop
end
class Instance < self
MATCHER = Matcher::Method::Instance
private
# Return method for name
#
# @param [Symbol] method_name
@ -129,21 +136,16 @@ module Mutant
scope.instance_method(method_name)
end
private
# Return instance methods names of scope
#
# @param [Class|Module] scope
#
# @return [Enumerable<Symbol>]
# Return candidate scope
#
# @return [Class, Module]
#
# @api private
#
def method_names
scope = self.scope
return [] unless scope.kind_of?(Module)
candidate_names.sort
def candidate_scope
scope
end
end
end
end

View file

@ -0,0 +1,53 @@
require 'spec_helper'
describe Mutant::Matcher::Methods::Singleton, '#each' do
let(:object) { described_class.new(Foo) }
subject { object.each { |matcher| yields << matcher } }
let(:yields) { [] }
module Bar
def method_d
end
def method_e
end
end
class Foo
extend Bar
def self.method_a
end
def self.method_b
end
class << self; protected :method_b; end
def self.method_c
end
private_class_method :method_c
end
let(:subject_a) { mock('Subject A') }
let(:subject_b) { mock('Subject B') }
let(:subject_c) { mock('Subject C') }
let(:subjects) { [subject_a, subject_b, subject_c] }
before do
matcher = Mutant::Matcher::Method::Singleton
matcher.stub(:new).with(Foo, Foo.method(:method_a)).and_return([subject_a])
matcher.stub(:new).with(Foo, Foo.method(:method_b)).and_return([subject_b])
matcher.stub(:new).with(Foo, Foo.method(:method_c)).and_return([subject_c])
end
it 'should yield expected subjects' do
subject
yields.should eql(subjects)
end
it_should_behave_like 'an #each method'
end