File: central.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 (46 lines) | stat: -rw-r--r-- 866 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
# frozen_string_literal: true

module Mocha
  class Central
    class Null < self
      def initialize(&block)
        super
        @raise_not_initialized_error = block
      end

      def stub(*)
        @raise_not_initialized_error.call
      end

      def unstub(*)
        @raise_not_initialized_error.call
      end
    end

    attr_accessor :stubba_methods

    def initialize
      self.stubba_methods = []
    end

    def stub(method)
      return if stubba_methods.detect { |m| m.matches?(method) }

      method.stub
      stubba_methods.push(method)
    end

    def unstub(method)
      return unless (existing = stubba_methods.detect { |m| m.matches?(method) })

      existing.unstub
      stubba_methods.delete(existing)
    end

    def unstub_all
      while stubba_methods.any?
        unstub(stubba_methods.first)
      end
    end
  end
end