File: mockery.rb

package info (click to toggle)
ruby-mocha 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 12,324; javascript: 499; makefile: 14
file content (199 lines) | stat: -rw-r--r-- 5,381 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# frozen_string_literal: true

require 'mocha/central'
require 'mocha/mock'
require 'mocha/name'
require 'mocha/impersonating_name'
require 'mocha/impersonating_any_instance_name'
require 'mocha/object_receiver'
require 'mocha/any_instance_receiver'
require 'mocha/state_machine'
require 'mocha/logger'
require 'mocha/configuration'
require 'mocha/stubbing_error'
require 'mocha/not_initialized_error'
require 'mocha/expectation_error_factory'

module Mocha
  class Mockery
    class Null < self
      def self.build
        new(nil)
      end

      def add_mock(*)
        raise_not_initialized_error
      end

      def add_state_machine(*)
        raise_not_initialized_error
      end

      def stubba
        Central::Null.new(&method(:raise_not_initialized_error))
      end

      private

      def raise_not_initialized_error
        message = 'Mocha methods cannot be used outside the context of a test'
        raise NotInitializedError.new(message, caller)
      end
    end

    class << self
      def instance
        @instances.last || Null.build
      end

      def setup(assertion_counter)
        @instances ||= []
        mockery = new(assertion_counter)
        mockery.logger = instance.logger unless @instances.empty?
        @instances.push(mockery)
      end

      def verify
        instance.verify
      end

      def teardown(origin = nil)
        if @instances.nil?
          raise NotInitializedError, 'Mocha::Mockery.teardown called before Mocha::Mockery.setup'
        end

        instance.teardown(origin)
      ensure
        @instances.pop unless @instances.nil?
      end
    end

    def initialize(assertion_counter)
      @assertion_counter = assertion_counter
    end

    def named_mock(name)
      add_mock(Mock.new(self, @assertion_counter, Name.new(name)))
    end

    def unnamed_mock
      add_mock(Mock.new(self, @assertion_counter))
    end

    def mock_impersonating(object)
      add_mock(Mock.new(self, @assertion_counter, ImpersonatingName.new(object), ObjectReceiver.new(object)))
    end

    def mock_impersonating_any_instance_of(klass)
      add_mock(Mock.new(self, @assertion_counter, ImpersonatingAnyInstanceName.new(klass), AnyInstanceReceiver.new(klass)))
    end

    def new_state_machine(name)
      add_state_machine(StateMachine.new(name))
    end

    def verify
      unless mocks.all?(&:__verified__?)
        message = "not all expectations were satisfied\n#{mocha_inspect}"
        backtrace = if unsatisfied_expectations.empty?
                      caller
                    else
                      unsatisfied_expectations[0].backtrace
                    end
        raise ExpectationErrorFactory.build(message, backtrace)
      end
      expectations.reject(&:used?).each do |expectation|
        signature_proc = lambda { expectation.method_signature }
        check(:stubbing_method_unnecessarily, 'method unnecessarily', signature_proc, expectation.backtrace)
      end
    end

    def teardown(origin = nil)
      stubba.unstub_all
      mocks.each { |m| m.__expire__(origin) }
      reset
    end

    def stubba
      @stubba ||= Central.new
    end

    def mocks
      @mocks ||= []
    end

    def state_machines
      @state_machines ||= []
    end

    def sequences
      @sequences ||= []
    end

    def mocha_inspect
      lines = []
      lines << "unsatisfied expectations:\n- #{unsatisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if unsatisfied_expectations.any?
      lines << "satisfied expectations:\n- #{satisfied_expectations.map(&:mocha_inspect).join("\n- ")}\n" if satisfied_expectations.any?
      lines << "states:\n- #{state_machines.map(&:mocha_inspect).join("\n- ")}\n" if state_machines.any?
      lines.join
    end

    def on_stubbing(object, method)
      signature_proc = lambda { "#{object.mocha_inspect}.#{method}" }
      check(:stubbing_non_existent_method, 'non-existent method', signature_proc) do
        !(object.stubba_class.__method_exists__?(method) || object.stubba_respond_to?(method))
      end
      check(:stubbing_non_public_method, 'non-public method', signature_proc) do
        object.stubba_class.__method_exists__?(method, include_public_methods: false)
      end
      check(:stubbing_method_on_non_mock_object, 'method on non-mock object', signature_proc)
    end

    attr_writer :logger

    def logger
      @logger ||= Logger.new($stderr)
    end

    private

    def check(action, description, signature_proc, backtrace = caller)
      treatment = Mocha.configuration.send(action)
      return if (treatment == :allow) || (block_given? && !yield)

      method_signature = signature_proc.call
      message = "stubbing #{description}: #{method_signature}"
      raise StubbingError.new(message, backtrace) if treatment == :prevent

      logger.warn(message) if treatment == :warn
    end

    def expectations
      mocks.map { |mock| mock.__expectations__.to_a }.flatten
    end

    def unsatisfied_expectations
      expectations.reject(&:verified?)
    end

    def satisfied_expectations
      expectations.select(&:verified?)
    end

    def add_mock(mock)
      mocks << mock
      mock
    end

    def add_state_machine(state_machine)
      state_machines << state_machine
      state_machine
    end

    def reset
      @stubba = nil
      @mocks = nil
      @state_machines = nil
    end
  end
end