diff --git a/lib/mutant/matcher/method/instance.rb b/lib/mutant/matcher/method/instance.rb index a9d9909e..f953461e 100644 --- a/lib/mutant/matcher/method/instance.rb +++ b/lib/mutant/matcher/method/instance.rb @@ -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] + # + 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] diff --git a/spec/unit/mutant/matcher/method/instance/class_methods/each_spec.rb b/spec/unit/mutant/matcher/method/instance/class_methods/each_spec.rb new file mode 100644 index 00000000..0b22a22d --- /dev/null +++ b/spec/unit/mutant/matcher/method/instance/class_methods/each_spec.rb @@ -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