File: matcher_delegator.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (61 lines) | stat: -rw-r--r-- 1,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module RSpec
  module Matchers
    # Provides a base class with as little methods as possible, so that
    # most methods can be delegated via `method_missing`.
    #
    # On Ruby 2.0+ BasicObject could be used for this purpose, but it
    # introduce some extra complexity with constant resolution, so the
    # BlankSlate pattern was prefered.
    # @private
    class BaseDelegator
      kept_methods = [
        # Methods that raise warnings if removed.
        :__id__, :__send__, :object_id,

        # Methods that are explicitly undefined in some subclasses.
        :==, :===,

        # Methods we keep on purpose.
        :class, :respond_to?, :__method__, :method, :dup,
        :clone, :initialize_dup, :initialize_copy, :initialize_clone,
      ]
      instance_methods.each do |method|
        unless kept_methods.include?(method.to_sym)
          undef_method(method)
        end
      end
    end

    # Provides the necessary plumbing to wrap a matcher with a decorator.
    # @private
    class MatcherDelegator < BaseDelegator
      include Composable
      attr_reader :base_matcher

      def initialize(base_matcher)
        @base_matcher = base_matcher
      end

      def method_missing(*args, &block)
        base_matcher.__send__(*args, &block)
      end

      if ::RUBY_VERSION.to_f > 1.8
        def respond_to_missing?(name, include_all=false)
          super || base_matcher.respond_to?(name, include_all)
        end
      else
        # :nocov:
        def respond_to?(name, include_all=false)
          super || base_matcher.respond_to?(name, include_all)
        end
        # :nocov:
      end

      def initialize_copy(other)
        @base_matcher = @base_matcher.clone
        super
      end
    end
  end
end