Sha256: 5ae69202bdf0fd138e5342923d10b1ff9e58b8986904a269fb063fe0cda695c0

Contents?: true

Size: 1.69 KB

Versions: 54

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Mutant
  class Matcher
    class Method
      # Matcher for singleton methods
      class Singleton < self

        # New singleton method matcher
        #
        # @param [Class, Module] scope
        # @param [Symbol] method_name
        #
        # @return [Matcher::Method::Singleton]
        def self.new(scope, method_name)
          super(scope, method_name, Evaluator)
        end

        # Singleton method evaluator
        class Evaluator < Evaluator
          SUBJECT_CLASS    = Subject::Method::Singleton
          RECEIVER_INDEX   = 0
          NAME_INDEX       = 1
          RECEIVER_WARNING = 'Can only match :defs on :self or :const got %p unable to match'

        private

          def match?(node)
            n_defs?(node) && line?(node) && name?(node) && receiver?(node)
          end

          def line?(node)
            node
              .location
              .line
              .equal?(source_line)
          end

          def name?(node)
            node.children.fetch(NAME_INDEX).equal?(method_name)
          end

          def receiver?(node)
            receiver = node.children.fetch(RECEIVER_INDEX)
            case receiver.type
            when :self
              true
            when :const
              receiver_name?(receiver)
            else
              env.warn(RECEIVER_WARNING % receiver.type)
              nil
            end
          end

          def receiver_name?(node)
            name = node.children.fetch(NAME_INDEX)
            name.to_s.eql?(context.unqualified_name)
          end

        end # Evaluator

        private_constant(*constants(false))
      end # Singleton
    end # Method
  end # Matcher
end # Mutant

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
mutant-0.11.13 lib/mutant/matcher/method/singleton.rb
mutant-0.11.12 lib/mutant/matcher/method/singleton.rb
mutant-0.11.11 lib/mutant/matcher/method/singleton.rb
mutant-0.11.10 lib/mutant/matcher/method/singleton.rb
mutant-0.11.9 lib/mutant/matcher/method/singleton.rb
mutant-0.11.8 lib/mutant/matcher/method/singleton.rb
mutant-0.11.7 lib/mutant/matcher/method/singleton.rb
mutant-0.11.6 lib/mutant/matcher/method/singleton.rb
mutant-0.11.5 lib/mutant/matcher/method/singleton.rb
mutant-0.11.4 lib/mutant/matcher/method/singleton.rb
mutant-0.11.3 lib/mutant/matcher/method/singleton.rb
mutant-0.11.2 lib/mutant/matcher/method/singleton.rb
mutant-0.11.1 lib/mutant/matcher/method/singleton.rb
mutant-0.11.0 lib/mutant/matcher/method/singleton.rb
mutant-0.10.35 lib/mutant/matcher/method/singleton.rb
mutant-0.10.34 lib/mutant/matcher/method/singleton.rb
mutant-0.10.33 lib/mutant/matcher/method/singleton.rb
mutant-0.10.32 lib/mutant/matcher/method/singleton.rb
mutant-0.10.31 lib/mutant/matcher/method/singleton.rb
mutant-0.10.30 lib/mutant/matcher/method/singleton.rb