File: explicit_needed.rb

package info (click to toggle)
ruby-flexmock 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: ruby: 7,572; makefile: 6
file content (45 lines) | stat: -rw-r--r-- 1,213 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

class FlexMock

  # Expectations on mocks with a base class can only be defined on
  # methods supported by the base class. Attempting to add an stub to
  # a method not defined on the base class will cause the expectation
  # to be wrapped in an ExplicitNeeded wrapper. The wrapper will throw
  # an exception unless the explicitly method is immediately called on
  # the expectation.
  #
  class ExplicitNeeded
    def initialize(expectation, method_name, base_class)
      @expectation = expectation
      @explicit = false
      @method_name = method_name
      @base_class = base_class
    end

    def explicitly
      @explicit = true
      self
    end

    def explicit?
      @explicit
    end

    def mock=(m)
      @expectation.mock = m
    end

    WHITELIST = [:with_signature_matching]

    def method_missing(sym, *args, **kw, &block)
      if explicit? || WHITELIST.include?(sym)
        @expectation.send(sym, *args, **kw, &block)
      else
        fail NoMethodError, "Cannot stub methods not defined by the base class\n" +
          "   Method:     #{@method_name}\n" +
          "   Base Class: #{@base_class}\n" +
          "   (Use 'explicitly' to override)"
      end
    end
  end
end