Correctly implement and specify singleton method extraction

This commit is contained in:
Markus Schirp 2012-08-29 13:32:44 +02:00
parent 789dac96ac
commit 565058f566
2 changed files with 54 additions and 2 deletions

View file

@ -28,8 +28,15 @@ module Mutant
# @api private
#
def self.singleton_methods(scope)
scope.singleton_class.instance_methods(false).reject do |method|
method.to_sym == :__class_init__
singleton_class = scope.singleton_class
names =
singleton_class.public_instance_methods(false) +
singleton_class.private_instance_methods(false) +
singleton_class.protected_instance_methods(false)
names.map(&:to_sym).sort.reject do |name|
name.to_sym == :__class_init__
end
end

View file

@ -0,0 +1,45 @@
require 'spec_helper'
describe Mutant::Matcher::Method::Singleton, '.each' do
subject { object.each(scope) { |item| yields << item } }
let(:each_arguments) { [scope] }
let(:object) { described_class }
let(:yields) { [] }
context 'when scope is a Class' do
let(:scope) do
ancestor = Class.new do
def self.ancestor_method
end
def self.name; 'SomeRandomClass'; end
end
Class.new(ancestor) do
def self.public_method; end
public_class_method :public_method
class << self
def protected_method; end
protected :protected_method
end
def self.private_method; end
private_class_method :private_method
end
end
it 'should yield instance method matchers' do
expected = [
Mutant::Matcher::Method::Singleton.new(scope, :public_method ),
Mutant::Matcher::Method::Singleton.new(scope, :protected_method),
Mutant::Matcher::Method::Singleton.new(scope, :private_method )
].sort_by(&:method_name)
expect { subject }.to change { yields.dup }.from([]).to(expected)
end
end
end