floehopper-mocha 0.9.3.20081220175348

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. data/COPYING +3 -0
  2. data/MIT-LICENSE +7 -0
  3. data/README +35 -0
  4. data/RELEASE +257 -0
  5. data/Rakefile +199 -0
  6. data/examples/misc.rb +44 -0
  7. data/examples/mocha.rb +26 -0
  8. data/examples/stubba.rb +65 -0
  9. data/lib/mocha/any_instance_method.rb +54 -0
  10. data/lib/mocha/argument_iterator.rb +21 -0
  11. data/lib/mocha/backtrace_filter.rb +17 -0
  12. data/lib/mocha/cardinality.rb +95 -0
  13. data/lib/mocha/central.rb +27 -0
  14. data/lib/mocha/change_state_side_effect.rb +19 -0
  15. data/lib/mocha/class_method.rb +86 -0
  16. data/lib/mocha/configuration.rb +60 -0
  17. data/lib/mocha/deprecation.rb +22 -0
  18. data/lib/mocha/exception_raiser.rb +17 -0
  19. data/lib/mocha/expectation.rb +451 -0
  20. data/lib/mocha/expectation_error.rb +15 -0
  21. data/lib/mocha/expectation_list.rb +50 -0
  22. data/lib/mocha/in_state_ordering_constraint.rb +19 -0
  23. data/lib/mocha/inspect.rb +67 -0
  24. data/lib/mocha/instance_method.rb +16 -0
  25. data/lib/mocha/is_a.rb +9 -0
  26. data/lib/mocha/logger.rb +15 -0
  27. data/lib/mocha/metaclass.rb +13 -0
  28. data/lib/mocha/method_matcher.rb +21 -0
  29. data/lib/mocha/mini_test_adapter.rb +50 -0
  30. data/lib/mocha/mock.rb +200 -0
  31. data/lib/mocha/mockery.rb +181 -0
  32. data/lib/mocha/module_method.rb +16 -0
  33. data/lib/mocha/multiple_yields.rb +20 -0
  34. data/lib/mocha/names.rb +53 -0
  35. data/lib/mocha/no_yields.rb +11 -0
  36. data/lib/mocha/object.rb +187 -0
  37. data/lib/mocha/parameter_matchers/all_of.rb +42 -0
  38. data/lib/mocha/parameter_matchers/any_of.rb +47 -0
  39. data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
  40. data/lib/mocha/parameter_matchers/anything.rb +33 -0
  41. data/lib/mocha/parameter_matchers/base.rb +15 -0
  42. data/lib/mocha/parameter_matchers/equals.rb +42 -0
  43. data/lib/mocha/parameter_matchers/has_entries.rb +45 -0
  44. data/lib/mocha/parameter_matchers/has_entry.rb +56 -0
  45. data/lib/mocha/parameter_matchers/has_key.rb +42 -0
  46. data/lib/mocha/parameter_matchers/has_value.rb +42 -0
  47. data/lib/mocha/parameter_matchers/includes.rb +40 -0
  48. data/lib/mocha/parameter_matchers/instance_of.rb +42 -0
  49. data/lib/mocha/parameter_matchers/is_a.rb +42 -0
  50. data/lib/mocha/parameter_matchers/kind_of.rb +42 -0
  51. data/lib/mocha/parameter_matchers/not.rb +42 -0
  52. data/lib/mocha/parameter_matchers/object.rb +15 -0
  53. data/lib/mocha/parameter_matchers/optionally.rb +55 -0
  54. data/lib/mocha/parameter_matchers/regexp_matches.rb +43 -0
  55. data/lib/mocha/parameter_matchers/responds_with.rb +43 -0
  56. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +43 -0
  57. data/lib/mocha/parameter_matchers.rb +27 -0
  58. data/lib/mocha/parameters_matcher.rb +37 -0
  59. data/lib/mocha/pretty_parameters.rb +28 -0
  60. data/lib/mocha/return_values.rb +31 -0
  61. data/lib/mocha/sequence.rb +42 -0
  62. data/lib/mocha/single_return_value.rb +17 -0
  63. data/lib/mocha/single_yield.rb +18 -0
  64. data/lib/mocha/standalone.rb +166 -0
  65. data/lib/mocha/state_machine.rb +91 -0
  66. data/lib/mocha/stubbing_error.rb +16 -0
  67. data/lib/mocha/test_case_adapter.rb +103 -0
  68. data/lib/mocha/unexpected_invocation.rb +18 -0
  69. data/lib/mocha/yield_parameters.rb +31 -0
  70. data/lib/mocha.rb +47 -0
  71. data/lib/mocha_standalone.rb +2 -0
  72. data/lib/stubba.rb +4 -0
  73. data/test/acceptance/acceptance_test_helper.rb +38 -0
  74. data/test/acceptance/bug_18914_test.rb +43 -0
  75. data/test/acceptance/bug_21465_test.rb +34 -0
  76. data/test/acceptance/bug_21563_test.rb +25 -0
  77. data/test/acceptance/expected_invocation_count_test.rb +196 -0
  78. data/test/acceptance/failure_messages_test.rb +64 -0
  79. data/test/acceptance/minitest_test.rb +130 -0
  80. data/test/acceptance/mocha_example_test.rb +98 -0
  81. data/test/acceptance/mocha_test_result_test.rb +84 -0
  82. data/test/acceptance/mock_test.rb +100 -0
  83. data/test/acceptance/mock_with_initializer_block_test.rb +51 -0
  84. data/test/acceptance/mocked_methods_dispatch_test.rb +78 -0
  85. data/test/acceptance/optional_parameters_test.rb +70 -0
  86. data/test/acceptance/parameter_matcher_test.rb +209 -0
  87. data/test/acceptance/partial_mocks_test.rb +47 -0
  88. data/test/acceptance/return_value_test.rb +52 -0
  89. data/test/acceptance/sequence_test.rb +186 -0
  90. data/test/acceptance/standalone_test.rb +139 -0
  91. data/test/acceptance/states_test.rb +70 -0
  92. data/test/acceptance/stub_any_instance_method_test.rb +195 -0
  93. data/test/acceptance/stub_class_method_test.rb +203 -0
  94. data/test/acceptance/stub_everything_test.rb +56 -0
  95. data/test/acceptance/stub_instance_method_test.rb +203 -0
  96. data/test/acceptance/stub_module_method_test.rb +163 -0
  97. data/test/acceptance/stub_test.rb +52 -0
  98. data/test/acceptance/stubba_example_test.rb +102 -0
  99. data/test/acceptance/stubba_test.rb +15 -0
  100. data/test/acceptance/stubba_test_result_test.rb +66 -0
  101. data/test/acceptance/stubbing_error_backtrace_test.rb +64 -0
  102. data/test/acceptance/stubbing_method_unnecessarily_test.rb +65 -0
  103. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +130 -0
  104. data/test/acceptance/stubbing_non_existent_class_method_test.rb +157 -0
  105. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +147 -0
  106. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +130 -0
  107. data/test/acceptance/stubbing_non_public_class_method_test.rb +163 -0
  108. data/test/acceptance/stubbing_non_public_instance_method_test.rb +143 -0
  109. data/test/acceptance/stubbing_on_non_mock_object_test.rb +64 -0
  110. data/test/deprecation_disabler.rb +15 -0
  111. data/test/execution_point.rb +36 -0
  112. data/test/method_definer.rb +24 -0
  113. data/test/simple_counter.rb +13 -0
  114. data/test/test_helper.rb +11 -0
  115. data/test/test_runner.rb +33 -0
  116. data/test/unit/any_instance_method_test.rb +126 -0
  117. data/test/unit/array_inspect_test.rb +16 -0
  118. data/test/unit/backtrace_filter_test.rb +19 -0
  119. data/test/unit/cardinality_test.rb +56 -0
  120. data/test/unit/central_test.rb +65 -0
  121. data/test/unit/change_state_side_effect_test.rb +41 -0
  122. data/test/unit/class_method_test.rb +237 -0
  123. data/test/unit/date_time_inspect_test.rb +21 -0
  124. data/test/unit/exception_raiser_test.rb +42 -0
  125. data/test/unit/expectation_list_test.rb +57 -0
  126. data/test/unit/expectation_test.rb +459 -0
  127. data/test/unit/hash_inspect_test.rb +16 -0
  128. data/test/unit/in_state_ordering_constraint_test.rb +43 -0
  129. data/test/unit/metaclass_test.rb +22 -0
  130. data/test/unit/method_matcher_test.rb +23 -0
  131. data/test/unit/mock_test.rb +295 -0
  132. data/test/unit/mockery_test.rb +149 -0
  133. data/test/unit/multiple_yields_test.rb +18 -0
  134. data/test/unit/no_yields_test.rb +18 -0
  135. data/test/unit/object_inspect_test.rb +37 -0
  136. data/test/unit/object_test.rb +82 -0
  137. data/test/unit/parameter_matchers/all_of_test.rb +26 -0
  138. data/test/unit/parameter_matchers/any_of_test.rb +26 -0
  139. data/test/unit/parameter_matchers/anything_test.rb +21 -0
  140. data/test/unit/parameter_matchers/equals_test.rb +25 -0
  141. data/test/unit/parameter_matchers/has_entries_test.rb +51 -0
  142. data/test/unit/parameter_matchers/has_entry_test.rb +62 -0
  143. data/test/unit/parameter_matchers/has_key_test.rb +36 -0
  144. data/test/unit/parameter_matchers/has_value_test.rb +37 -0
  145. data/test/unit/parameter_matchers/includes_test.rb +25 -0
  146. data/test/unit/parameter_matchers/instance_of_test.rb +25 -0
  147. data/test/unit/parameter_matchers/is_a_test.rb +25 -0
  148. data/test/unit/parameter_matchers/kind_of_test.rb +25 -0
  149. data/test/unit/parameter_matchers/not_test.rb +26 -0
  150. data/test/unit/parameter_matchers/regexp_matches_test.rb +25 -0
  151. data/test/unit/parameter_matchers/responds_with_test.rb +25 -0
  152. data/test/unit/parameter_matchers/stub_matcher.rb +27 -0
  153. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +25 -0
  154. data/test/unit/parameters_matcher_test.rb +121 -0
  155. data/test/unit/return_values_test.rb +63 -0
  156. data/test/unit/sequence_test.rb +104 -0
  157. data/test/unit/single_return_value_test.rb +14 -0
  158. data/test/unit/single_yield_test.rb +18 -0
  159. data/test/unit/state_machine_test.rb +98 -0
  160. data/test/unit/string_inspect_test.rb +11 -0
  161. data/test/unit/yield_parameters_test.rb +93 -0
  162. metadata +226 -0
@@ -0,0 +1,50 @@
1
+ module Mocha
2
+
3
+ module MiniTestCaseAdapter
4
+
5
+ class AssertionCounter
6
+ def initialize(test_case)
7
+ @test_case = test_case
8
+ end
9
+
10
+ def increment
11
+ @test_case._assertions += 1
12
+ end
13
+ end
14
+
15
+ def self.included(base)
16
+ base.class_eval do
17
+
18
+ alias_method :run_before_mocha_mini_test_adapter, :run
19
+
20
+ def run runner
21
+ assertion_counter = AssertionCounter.new(self)
22
+ result = '.'
23
+ begin
24
+ begin
25
+ @passed = nil
26
+ self.setup
27
+ self.__send__ self.name
28
+ mocha_verify(assertion_counter)
29
+ @passed = true
30
+ rescue Exception => e
31
+ @passed = false
32
+ result = runner.puke(self.class, self.name, e)
33
+ ensure
34
+ begin
35
+ self.teardown
36
+ rescue Exception => e
37
+ result = runner.puke(self.class, self.name, e)
38
+ end
39
+ end
40
+ ensure
41
+ mocha_teardown
42
+ end
43
+ result
44
+ end
45
+
46
+ end
47
+ end
48
+
49
+ end
50
+ end
data/lib/mocha/mock.rb ADDED
@@ -0,0 +1,200 @@
1
+ require 'mocha/expectation'
2
+ require 'mocha/expectation_list'
3
+ require 'mocha/metaclass'
4
+ require 'mocha/names'
5
+ require 'mocha/mockery'
6
+ require 'mocha/method_matcher'
7
+ require 'mocha/parameters_matcher'
8
+ require 'mocha/unexpected_invocation'
9
+ require 'mocha/argument_iterator'
10
+
11
+ module Mocha # :nodoc:
12
+
13
+ # Traditional mock object.
14
+ #
15
+ # Methods return an Expectation which can be further modified by methods on Expectation.
16
+ class Mock
17
+
18
+ # :call-seq: expects(method_name) -> expectation
19
+ # expects(method_names_vs_return_values) -> last expectation
20
+ #
21
+ # Adds an expectation that a method identified by +method_name+ Symbol/String must be called exactly once with any parameters.
22
+ # Returns the new expectation which can be further modified by methods on Expectation.
23
+ # object = mock()
24
+ # object.expects(:method1)
25
+ # object.method1
26
+ # # no error raised
27
+ #
28
+ # object = mock()
29
+ # object.expects(:method1)
30
+ # # error raised, because method1 not called exactly once
31
+ # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
32
+ # object = mock()
33
+ # object.expects(:method1 => :result1, :method2 => :result2)
34
+ #
35
+ # # exactly equivalent to
36
+ #
37
+ # object = mock()
38
+ # object.expects(:method1).returns(:result1)
39
+ # object.expects(:method2).returns(:result2)
40
+ #
41
+ # Aliased by <tt>\_\_expects\_\_</tt>
42
+ def expects(method_name_or_hash, backtrace = nil)
43
+ iterator = ArgumentIterator.new(method_name_or_hash)
44
+ iterator.each { |*args|
45
+ method_name = args.shift
46
+ ensure_method_not_already_defined(method_name)
47
+ expectation = Expectation.new(self, method_name, backtrace)
48
+ expectation.returns(args.shift) if args.length > 0
49
+ @expectations.add(expectation)
50
+ }
51
+ end
52
+
53
+ # :call-seq: stubs(method_name) -> expectation
54
+ # stubs(method_names_vs_return_values) -> last expectation
55
+ #
56
+ # Adds an expectation that a method identified by +method_name+ Symbol/String may be called any number of times with any parameters.
57
+ # Returns the new expectation which can be further modified by methods on Expectation.
58
+ # object = mock()
59
+ # object.stubs(:method1)
60
+ # object.method1
61
+ # object.method1
62
+ # # no error raised
63
+ # If +method_names_vs_return_values+ is a +Hash+, an expectation will be set up for each entry using the key as +method_name+ and value as +return_value+.
64
+ # object = mock()
65
+ # object.stubs(:method1 => :result1, :method2 => :result2)
66
+ #
67
+ # # exactly equivalent to
68
+ #
69
+ # object = mock()
70
+ # object.stubs(:method1).returns(:result1)
71
+ # object.stubs(:method2).returns(:result2)
72
+ #
73
+ # Aliased by <tt>\_\_stubs\_\_</tt>
74
+ def stubs(method_name_or_hash, backtrace = nil)
75
+ iterator = ArgumentIterator.new(method_name_or_hash)
76
+ iterator.each { |*args|
77
+ method_name = args.shift
78
+ ensure_method_not_already_defined(method_name)
79
+ expectation = Expectation.new(self, method_name, backtrace)
80
+ expectation.at_least(0)
81
+ expectation.returns(args.shift) if args.length > 0
82
+ @expectations.add(expectation)
83
+ }
84
+ end
85
+
86
+ # :call-seq: responds_like(responder) -> mock
87
+ #
88
+ # Constrains the +mock+ so that it can only expect or stub methods to which +responder+ responds. The constraint is only applied at method invocation time.
89
+ #
90
+ # A +NoMethodError+ will be raised if the +responder+ does not <tt>respond_to?</tt> a method invocation (even if the method has been expected or stubbed).
91
+ #
92
+ # The +mock+ will delegate its <tt>respond_to?</tt> method to the +responder+.
93
+ # class Sheep
94
+ # def chew(grass); end
95
+ # def self.number_of_legs; end
96
+ # end
97
+ #
98
+ # sheep = mock('sheep')
99
+ # sheep.expects(:chew)
100
+ # sheep.expects(:foo)
101
+ # sheep.respond_to?(:chew) # => true
102
+ # sheep.respond_to?(:foo) # => true
103
+ # sheep.chew
104
+ # sheep.foo
105
+ # # no error raised
106
+ #
107
+ # sheep = mock('sheep')
108
+ # sheep.responds_like(Sheep.new)
109
+ # sheep.expects(:chew)
110
+ # sheep.expects(:foo)
111
+ # sheep.respond_to?(:chew) # => true
112
+ # sheep.respond_to?(:foo) # => false
113
+ # sheep.chew
114
+ # sheep.foo # => raises NoMethodError exception
115
+ #
116
+ # sheep_class = mock('sheep_class')
117
+ # sheep_class.responds_like(Sheep)
118
+ # sheep_class.stubs(:number_of_legs).returns(4)
119
+ # sheep_class.expects(:foo)
120
+ # sheep_class.respond_to?(:number_of_legs) # => true
121
+ # sheep_class.respond_to?(:foo) # => false
122
+ # assert_equal 4, sheep_class.number_of_legs
123
+ # sheep_class.foo # => raises NoMethodError exception
124
+ #
125
+ # Aliased by +quacks_like+
126
+ def responds_like(object)
127
+ @responder = object
128
+ self
129
+ end
130
+
131
+ # :stopdoc:
132
+
133
+ def initialize(name = nil, &block)
134
+ @name = name || DefaultName.new(self)
135
+ @expectations = ExpectationList.new
136
+ @everything_stubbed = false
137
+ @responder = nil
138
+ instance_eval(&block) if block
139
+ end
140
+
141
+ attr_reader :everything_stubbed, :expectations
142
+
143
+ alias_method :__expects__, :expects
144
+
145
+ alias_method :__stubs__, :stubs
146
+
147
+ alias_method :quacks_like, :responds_like
148
+
149
+ def stub_everything
150
+ @everything_stubbed = true
151
+ end
152
+
153
+ def method_missing(symbol, *arguments, &block)
154
+ if @responder and not @responder.respond_to?(symbol)
155
+ raise NoMethodError, "undefined method `#{symbol}' for #{self.mocha_inspect} which responds like #{@responder.mocha_inspect}"
156
+ end
157
+ if matching_expectation_allowing_invocation = @expectations.match_allowing_invocation(symbol, *arguments)
158
+ matching_expectation_allowing_invocation.invoke(&block)
159
+ else
160
+ if (matching_expectation = @expectations.match(symbol, *arguments)) || (!matching_expectation && !@everything_stubbed)
161
+ message = UnexpectedInvocation.new(self, symbol, *arguments).to_s
162
+ message << Mockery.instance.mocha_inspect
163
+ raise ExpectationError.new(message, caller)
164
+ end
165
+ end
166
+ end
167
+
168
+ def respond_to?(symbol, include_private = false)
169
+ if @responder then
170
+ if @responder.method(:respond_to?).arity > 1
171
+ @responder.respond_to?(symbol, include_private)
172
+ else
173
+ @responder.respond_to?(symbol)
174
+ end
175
+ else
176
+ @expectations.matches_method?(symbol)
177
+ end
178
+ end
179
+
180
+ def __verified__?(assertion_counter = nil)
181
+ @expectations.verified?(assertion_counter)
182
+ end
183
+
184
+ def mocha_inspect
185
+ @name.mocha_inspect
186
+ end
187
+
188
+ def inspect
189
+ mocha_inspect
190
+ end
191
+
192
+ def ensure_method_not_already_defined(method_name)
193
+ self.__metaclass__.send(:undef_method, method_name) if self.__metaclass__.method_defined?(method_name)
194
+ end
195
+
196
+ # :startdoc:
197
+
198
+ end
199
+
200
+ end
@@ -0,0 +1,181 @@
1
+ require 'mocha/central'
2
+ require 'mocha/mock'
3
+ require 'mocha/names'
4
+ require 'mocha/state_machine'
5
+ require 'mocha/logger'
6
+ require 'mocha/configuration'
7
+ require 'mocha/stubbing_error'
8
+
9
+ module Mocha
10
+
11
+ class Mockery
12
+
13
+ class << self
14
+
15
+ def instance
16
+ @instance ||= new
17
+ end
18
+
19
+ def reset_instance
20
+ @instance = nil
21
+ end
22
+
23
+ end
24
+
25
+ def named_mock(name, &block)
26
+ add_mock(Mock.new(Name.new(name), &block))
27
+ end
28
+
29
+ def unnamed_mock(&block)
30
+ add_mock(Mock.new(&block))
31
+ end
32
+
33
+ def mock_impersonating(object, &block)
34
+ add_mock(Mock.new(ImpersonatingName.new(object), &block))
35
+ end
36
+
37
+ def mock_impersonating_any_instance_of(klass, &block)
38
+ add_mock(Mock.new(ImpersonatingAnyInstanceName.new(klass), &block))
39
+ end
40
+
41
+ def new_state_machine(name)
42
+ add_state_machine(StateMachine.new(name))
43
+ end
44
+
45
+ def verify(assertion_counter = nil)
46
+ unless mocks.all? { |mock| mock.__verified__?(assertion_counter) }
47
+ message = "not all expectations were satisfied\n#{mocha_inspect}"
48
+ if unsatisfied_expectations.empty?
49
+ backtrace = caller
50
+ else
51
+ backtrace = unsatisfied_expectations[0].backtrace
52
+ end
53
+ raise ExpectationError.new(message, backtrace)
54
+ end
55
+ expectations.each do |e|
56
+ unless Mocha::Configuration.allow?(:stubbing_method_unnecessarily)
57
+ unless e.used?
58
+ on_stubbing_method_unnecessarily(e)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def teardown
65
+ stubba.unstub_all
66
+ reset
67
+ end
68
+
69
+ def stubba
70
+ @stubba ||= Central.new
71
+ end
72
+
73
+ def mocks
74
+ @mocks ||= []
75
+ end
76
+
77
+ def state_machines
78
+ @state_machines ||= []
79
+ end
80
+
81
+ def mocha_inspect
82
+ message = ""
83
+ message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty?
84
+ message << "satisfied expectations:\n- #{satisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless satisfied_expectations.empty?
85
+ message << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty?
86
+ message
87
+ end
88
+
89
+ def on_stubbing(object, method)
90
+ method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
91
+ unless Mocha::Configuration.allow?(:stubbing_non_existent_method)
92
+ unless object.method_exists?(method, include_public_methods = true)
93
+ on_stubbing_non_existent_method(object, method)
94
+ end
95
+ end
96
+ unless Mocha::Configuration.allow?(:stubbing_non_public_method)
97
+ if object.method_exists?(method, include_public_methods = false)
98
+ on_stubbing_non_public_method(object, method)
99
+ end
100
+ end
101
+ unless Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object)
102
+ on_stubbing_method_on_non_mock_object(object, method)
103
+ end
104
+ end
105
+
106
+ def on_stubbing_non_existent_method(object, method)
107
+ if Mocha::Configuration.prevent?(:stubbing_non_existent_method)
108
+ raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{method}", caller)
109
+ end
110
+ if Mocha::Configuration.warn_when?(:stubbing_non_existent_method)
111
+ logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}"
112
+ end
113
+ end
114
+
115
+ def on_stubbing_non_public_method(object, method)
116
+ if Mocha::Configuration.prevent?(:stubbing_non_public_method)
117
+ raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{method}", caller)
118
+ end
119
+ if Mocha::Configuration.warn_when?(:stubbing_non_public_method)
120
+ logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}"
121
+ end
122
+ end
123
+
124
+ def on_stubbing_method_on_non_mock_object(object, method)
125
+ if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object)
126
+ raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{method}", caller)
127
+ end
128
+ if Mocha::Configuration.warn_when?(:stubbing_method_on_non_mock_object)
129
+ logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}"
130
+ end
131
+ end
132
+
133
+ def on_stubbing_method_unnecessarily(expectation)
134
+ if Mocha::Configuration.prevent?(:stubbing_method_unnecessarily)
135
+ raise StubbingError.new("stubbing method unnecessarily: #{expectation.method_signature}", expectation.backtrace)
136
+ end
137
+ if Mocha::Configuration.warn_when?(:stubbing_method_unnecessarily)
138
+ logger.warn "stubbing method unnecessarily: #{expectation.method_signature}"
139
+ end
140
+ end
141
+
142
+ attr_writer :logger
143
+
144
+ def logger
145
+ @logger ||= Logger.new($stderr)
146
+ end
147
+
148
+
149
+ private
150
+
151
+ def expectations
152
+ mocks.map { |mock| mock.expectations.to_a }.flatten
153
+ end
154
+
155
+ def unsatisfied_expectations
156
+ expectations.reject { |e| e.verified? }
157
+ end
158
+
159
+ def satisfied_expectations
160
+ expectations.select { |e| e.verified? }
161
+ end
162
+
163
+ def add_mock(mock)
164
+ mocks << mock
165
+ mock
166
+ end
167
+
168
+ def add_state_machine(state_machine)
169
+ state_machines << state_machine
170
+ state_machine
171
+ end
172
+
173
+ def reset
174
+ @stubba = nil
175
+ @mocks = nil
176
+ @state_machines = nil
177
+ end
178
+
179
+ end
180
+
181
+ end
@@ -0,0 +1,16 @@
1
+ require 'mocha/module_method'
2
+
3
+ module Mocha
4
+
5
+ class ModuleMethod < ClassMethod
6
+
7
+ def method_exists?(method)
8
+ return true if stubbee.public_methods(false).include?(method)
9
+ return true if stubbee.protected_methods(false).include?(method)
10
+ return true if stubbee.private_methods(false).include?(method)
11
+ return false
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,20 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class MultipleYields # :nodoc:
4
+
5
+ attr_reader :parameter_groups
6
+
7
+ def initialize(*parameter_groups)
8
+ @parameter_groups = parameter_groups
9
+ end
10
+
11
+ def each
12
+ @parameter_groups.each do |parameter_group|
13
+ yield(parameter_group)
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
@@ -0,0 +1,53 @@
1
+ module Mocha
2
+
3
+ class ImpersonatingName
4
+
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ def mocha_inspect
10
+ @object.mocha_inspect
11
+ end
12
+
13
+ end
14
+
15
+ class ImpersonatingAnyInstanceName
16
+
17
+ def initialize(klass)
18
+ @klass = klass
19
+ end
20
+
21
+ def mocha_inspect
22
+ "#<AnyInstance:#{@klass.mocha_inspect}>"
23
+ end
24
+
25
+ end
26
+
27
+ class Name
28
+
29
+ def initialize(name)
30
+ @name = name
31
+ end
32
+
33
+ def mocha_inspect
34
+ "#<Mock:#{@name}>"
35
+ end
36
+
37
+ end
38
+
39
+ class DefaultName
40
+
41
+ def initialize(mock)
42
+ @mock = mock
43
+ end
44
+
45
+ def mocha_inspect
46
+ address = @mock.__id__ * 2
47
+ address += 0x100000000 if address < 0
48
+ "#<Mock:0x#{'%x' % address}>"
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,11 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class NoYields # :nodoc:
4
+
5
+ def each
6
+ end
7
+
8
+ end
9
+
10
+ end
11
+