Add specs for Mutant::Matcher::Method::Instance.each(scope)
This commit is contained in:
parent
46c9213991
commit
f24b190a2a
2 changed files with 58 additions and 2 deletions
|
@ -13,13 +13,29 @@ module Mutant
|
|||
# @api private
|
||||
#
|
||||
def self.each(scope)
|
||||
return to_enum unless block_given?
|
||||
return to_enum(:each, scope) unless block_given?
|
||||
return unless scope.kind_of?(Module)
|
||||
scope.instance_methods(false).map do |name|
|
||||
|
||||
instance_method_names(scope).map do |name|
|
||||
yield new(scope, name)
|
||||
end
|
||||
end
|
||||
|
||||
# Return instance methods names of scope
|
||||
#
|
||||
# @param [Class|Module] scope
|
||||
#
|
||||
# @return [Enumerable<Symbol>]
|
||||
#
|
||||
def self.instance_method_names(scope)
|
||||
names =
|
||||
scope.public_instance_methods(false) +
|
||||
scope.private_instance_methods(false) +
|
||||
scope.protected_instance_methods(false)
|
||||
|
||||
names.uniq.map(&:to_sym).sort
|
||||
end
|
||||
|
||||
# Return identification
|
||||
#
|
||||
# @return [String]
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Mutant::Matcher::Method::Instance, '.each' do
|
||||
subject { object.each(scope) { |item| yields << item } }
|
||||
|
||||
let(:object) { described_class }
|
||||
let(:yields) { [] }
|
||||
|
||||
context 'when scope is a Class' do
|
||||
let(:scope) do
|
||||
ancestor = Class.new do
|
||||
def ancestor_method
|
||||
end
|
||||
end
|
||||
|
||||
Class.new(ancestor) do
|
||||
def self.name; 'SomeRandomClass'; end
|
||||
|
||||
def public_method; end
|
||||
public :public_method
|
||||
|
||||
def protected_method; end
|
||||
protected :protected_method
|
||||
|
||||
def private_method; end
|
||||
private :private_method
|
||||
end
|
||||
end
|
||||
|
||||
it 'should yield instance method matchers' do
|
||||
expected = [
|
||||
Mutant::Matcher::Method::Instance.new(scope, :public_method ),
|
||||
Mutant::Matcher::Method::Instance.new(scope, :protected_method),
|
||||
Mutant::Matcher::Method::Instance.new(scope, :private_method )
|
||||
].sort_by(&:method_name)
|
||||
|
||||
expect { subject }.to change { yields.dup }.from([]).to(expected)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue