File: configuration.rb

package info (click to toggle)
ruby-mocha 0.11.3-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,300 kB
  • sloc: ruby: 9,935; makefile: 2
file content (89 lines) | stat: -rw-r--r-- 2,858 bytes parent folder | download | duplicates (3)
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
module Mocha

  # Configuration settings.
  class Configuration

    DEFAULTS = {
      :stubbing_method_unnecessarily => :allow,
      :stubbing_method_on_non_mock_object => :allow,
      :stubbing_non_existent_method => :allow,
      :stubbing_non_public_method => :allow,
      :stubbing_method_on_nil => :prevent,
    }

    class << self

      # Allow the specified +action+.
      #
      # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
      # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
      def allow(action, &block)
        change_config action, :allow, &block
      end

      # @private
      def allow?(action)
        configuration[action] == :allow
      end

      # Warn if the specified +action+ is attempted.
      #
      # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
      # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
      def warn_when(action, &block)
        change_config action, :warn, &block
      end

      # @private
      def warn_when?(action)
        configuration[action] == :warn
      end

      # Raise a {StubbingError} if if the specified +action+ is attempted.
      #
      # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
      # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
      def prevent(action, &block)
        change_config action, :prevent, &block
      end

      # @private
      def prevent?(action)
        configuration[action] == :prevent
      end

      # @private
      def reset_configuration
        @configuration = nil
      end

      private

      # @private
      def configuration
        @configuration ||= DEFAULTS.dup
      end

      # @private
      def change_config(action, new_value, &block)
        if block_given?
          temporarily_change_config action, new_value, &block
        else
          configuration[action] = new_value
        end
      end

      # @private
      def temporarily_change_config(action, new_value, &block)
        original_value = configuration[action]
        configuration[action] = new_value
        yield
      ensure
        configuration[action] = original_value
      end

    end

  end

end