mocha 0.10.5 → 0.11.0

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 (229) hide show
  1. data/COPYING.rdoc +1 -1
  2. data/README.rdoc +6 -4
  3. data/RELEASE.rdoc +12 -0
  4. data/Rakefile +29 -60
  5. data/doc/Mocha.html +112 -0
  6. data/doc/Mocha/API.html +898 -0
  7. data/doc/Mocha/ClassMethods.html +246 -0
  8. data/doc/Mocha/Configuration.html +471 -0
  9. data/doc/Mocha/Expectation.html +2570 -0
  10. data/doc/Mocha/Mock.html +830 -0
  11. data/doc/Mocha/ObjectMethods.html +668 -0
  12. data/doc/Mocha/ParameterMatchers.html +2715 -0
  13. data/doc/Mocha/ParameterMatchers/AllOf.html +137 -0
  14. data/doc/Mocha/ParameterMatchers/AnyOf.html +137 -0
  15. data/doc/Mocha/ParameterMatchers/AnyParameters.html +136 -0
  16. data/doc/Mocha/ParameterMatchers/Anything.html +136 -0
  17. data/doc/Mocha/ParameterMatchers/Base.html +419 -0
  18. data/doc/Mocha/ParameterMatchers/Equals.html +137 -0
  19. data/doc/Mocha/ParameterMatchers/HasEntries.html +137 -0
  20. data/doc/Mocha/ParameterMatchers/HasEntry.html +137 -0
  21. data/doc/Mocha/ParameterMatchers/HasKey.html +137 -0
  22. data/doc/Mocha/ParameterMatchers/HasValue.html +137 -0
  23. data/doc/Mocha/ParameterMatchers/Includes.html +137 -0
  24. data/doc/Mocha/ParameterMatchers/InstanceOf.html +137 -0
  25. data/doc/Mocha/ParameterMatchers/IsA.html +136 -0
  26. data/doc/Mocha/ParameterMatchers/KindOf.html +137 -0
  27. data/doc/Mocha/ParameterMatchers/Not.html +137 -0
  28. data/doc/Mocha/ParameterMatchers/Optionally.html +136 -0
  29. data/doc/Mocha/ParameterMatchers/QueryStringMatches.html +136 -0
  30. data/doc/Mocha/ParameterMatchers/RegexpMatches.html +137 -0
  31. data/doc/Mocha/ParameterMatchers/RespondsWith.html +137 -0
  32. data/doc/Mocha/ParameterMatchers/YamlEquivalent.html +137 -0
  33. data/doc/Mocha/Sequence.html +133 -0
  34. data/doc/Mocha/StateMachine.html +510 -0
  35. data/doc/Mocha/StateMachine/State.html +125 -0
  36. data/doc/Mocha/StateMachine/StatePredicate.html +125 -0
  37. data/doc/Mocha/StubbingError.html +134 -0
  38. data/doc/Mocha/UnexpectedInvocation.html +124 -0
  39. data/doc/_index.html +481 -0
  40. data/doc/class_list.html +47 -0
  41. data/doc/css/common.css +1 -0
  42. data/doc/css/full_list.css +55 -0
  43. data/doc/css/style.css +322 -0
  44. data/doc/file.COPYING.html +72 -0
  45. data/doc/file.MIT-LICENSE.html +86 -0
  46. data/doc/file.README.html +153 -0
  47. data/doc/file.RELEASE.html +933 -0
  48. data/doc/file.misc.html +108 -0
  49. data/doc/file.mocha.html +90 -0
  50. data/doc/file.stubba.html +129 -0
  51. data/doc/file_list.html +67 -0
  52. data/doc/frames.html +13 -0
  53. data/doc/index.html +153 -0
  54. data/doc/js/app.js +205 -0
  55. data/doc/js/full_list.js +173 -0
  56. data/doc/js/jquery.js +16 -0
  57. data/doc/method_list.html +510 -0
  58. data/doc/top-level-namespace.html +105 -0
  59. data/lib/mocha/any_instance_method.rb +16 -11
  60. data/lib/mocha/api.rb +120 -106
  61. data/lib/mocha/argument_iterator.rb +6 -6
  62. data/lib/mocha/backtrace_filter.rb +1 -1
  63. data/lib/mocha/cardinality.rb +21 -21
  64. data/lib/mocha/central.rb +8 -8
  65. data/lib/mocha/change_state_side_effect.rb +5 -5
  66. data/lib/mocha/class_method.rb +14 -19
  67. data/lib/mocha/configuration.rb +47 -37
  68. data/lib/mocha/deprecation.rb +8 -8
  69. data/lib/mocha/exception_raiser.rb +7 -7
  70. data/lib/mocha/expectation.rb +173 -77
  71. data/lib/mocha/expectation_error.rb +2 -2
  72. data/lib/mocha/expectation_list.rb +11 -11
  73. data/lib/mocha/in_state_ordering_constraint.rb +5 -5
  74. data/lib/mocha/inspect.rb +8 -8
  75. data/lib/mocha/instance_method.rb +1 -17
  76. data/lib/mocha/integration.rb +9 -9
  77. data/lib/mocha/integration/mini_test/assertion_counter.rb +9 -9
  78. data/lib/mocha/integration/mini_test/exception_translation.rb +6 -6
  79. data/lib/mocha/integration/mini_test/version_13.rb +6 -6
  80. data/lib/mocha/integration/mini_test/version_140.rb +6 -6
  81. data/lib/mocha/integration/mini_test/version_141.rb +6 -6
  82. data/lib/mocha/integration/mini_test/version_142_to_172.rb +7 -7
  83. data/lib/mocha/integration/mini_test/version_200.rb +7 -7
  84. data/lib/mocha/integration/test_unit/assertion_counter.rb +10 -10
  85. data/lib/mocha/integration/test_unit/gem_version_200.rb +6 -6
  86. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +6 -6
  87. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +6 -6
  88. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +6 -6
  89. data/lib/mocha/is_a.rb +1 -1
  90. data/lib/mocha/logger.rb +6 -6
  91. data/lib/mocha/method_matcher.rb +7 -7
  92. data/lib/mocha/mock.rb +85 -48
  93. data/lib/mocha/mockery.rb +47 -33
  94. data/lib/mocha/module_method.rb +2 -2
  95. data/lib/mocha/multiple_yields.rb +9 -9
  96. data/lib/mocha/names.rb +18 -18
  97. data/lib/mocha/no_yields.rb +7 -7
  98. data/lib/mocha/object.rb +104 -75
  99. data/lib/mocha/parameter_matchers.rb +3 -3
  100. data/lib/mocha/parameter_matchers/all_of.rb +22 -12
  101. data/lib/mocha/parameter_matchers/any_of.rb +23 -12
  102. data/lib/mocha/parameter_matchers/any_parameters.rb +15 -8
  103. data/lib/mocha/parameter_matchers/anything.rb +20 -11
  104. data/lib/mocha/parameter_matchers/base.rb +35 -27
  105. data/lib/mocha/parameter_matchers/equals.rb +23 -12
  106. data/lib/mocha/parameter_matchers/has_entries.rb +22 -12
  107. data/lib/mocha/parameter_matchers/has_entry.rb +31 -13
  108. data/lib/mocha/parameter_matchers/has_key.rb +21 -11
  109. data/lib/mocha/parameter_matchers/has_value.rb +21 -11
  110. data/lib/mocha/parameter_matchers/includes.rb +15 -5
  111. data/lib/mocha/parameter_matchers/instance_of.rb +23 -12
  112. data/lib/mocha/parameter_matchers/is_a.rb +23 -12
  113. data/lib/mocha/parameter_matchers/kind_of.rb +23 -12
  114. data/lib/mocha/parameter_matchers/not.rb +22 -12
  115. data/lib/mocha/parameter_matchers/object.rb +5 -3
  116. data/lib/mocha/parameter_matchers/optionally.rb +24 -12
  117. data/lib/mocha/parameter_matchers/query_string.rb +14 -3
  118. data/lib/mocha/parameter_matchers/regexp_matches.rb +21 -11
  119. data/lib/mocha/parameter_matchers/responds_with.rb +17 -6
  120. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +16 -6
  121. data/lib/mocha/parameters_matcher.rb +8 -8
  122. data/lib/mocha/pretty_parameters.rb +7 -7
  123. data/lib/mocha/return_values.rb +11 -11
  124. data/lib/mocha/sequence.rb +23 -14
  125. data/lib/mocha/single_return_value.rb +7 -7
  126. data/lib/mocha/single_yield.rb +9 -9
  127. data/lib/mocha/standalone.rb +1 -1
  128. data/lib/mocha/state_machine.rb +61 -46
  129. data/lib/mocha/stubbing_error.rb +8 -5
  130. data/lib/mocha/thrower.rb +2 -2
  131. data/lib/mocha/unexpected_invocation.rb +9 -6
  132. data/lib/mocha/version.rb +1 -1
  133. data/lib/mocha/yield_parameters.rb +10 -10
  134. data/lib/mocha_standalone.rb +1 -1
  135. data/mocha.gemspec +4 -7
  136. data/test/acceptance/acceptance_test_helper.rb +10 -10
  137. data/test/acceptance/api_test.rb +20 -20
  138. data/test/acceptance/bug_18914_test.rb +12 -12
  139. data/test/acceptance/bug_21465_test.rb +6 -6
  140. data/test/acceptance/bug_21563_test.rb +5 -5
  141. data/test/acceptance/exception_rescue_test.rb +1 -1
  142. data/test/acceptance/expected_invocation_count_test.rb +17 -17
  143. data/test/acceptance/failure_messages_test.rb +13 -13
  144. data/test/acceptance/minitest_test.rb +39 -39
  145. data/test/acceptance/mocha_example_test.rb +26 -26
  146. data/test/acceptance/mocha_test_result_test.rb +13 -13
  147. data/test/acceptance/mock_test.rb +5 -5
  148. data/test/acceptance/mock_with_initializer_block_test.rb +5 -5
  149. data/test/acceptance/mocked_methods_dispatch_test.rb +4 -4
  150. data/test/acceptance/multiple_expectations_failure_message_test.rb +2 -2
  151. data/test/acceptance/optional_parameters_test.rb +5 -5
  152. data/test/acceptance/parameter_matcher_test.rb +18 -18
  153. data/test/acceptance/partial_mocks_test.rb +9 -9
  154. data/test/acceptance/raise_exception_test.rb +1 -1
  155. data/test/acceptance/return_value_test.rb +5 -5
  156. data/test/acceptance/sequence_test.rb +29 -29
  157. data/test/acceptance/states_test.rb +17 -17
  158. data/test/acceptance/stub_any_instance_method_test.rb +13 -13
  159. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +7 -7
  160. data/test/acceptance/stub_everything_test.rb +5 -5
  161. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
  162. data/test/acceptance/stub_module_method_test.rb +17 -17
  163. data/test/acceptance/stub_test.rb +5 -5
  164. data/test/acceptance/stubba_example_test.rb +24 -24
  165. data/test/acceptance/stubba_test_result_test.rb +11 -11
  166. data/test/acceptance/stubbing_error_backtrace_test.rb +6 -6
  167. data/test/acceptance/stubbing_frozen_object_test.rb +88 -0
  168. data/test/acceptance/stubbing_method_unnecessarily_test.rb +9 -9
  169. data/test/acceptance/stubbing_nil_test.rb +59 -0
  170. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +13 -13
  171. data/test/acceptance/stubbing_non_existent_class_method_test.rb +15 -15
  172. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +14 -14
  173. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +13 -13
  174. data/test/acceptance/stubbing_non_public_class_method_test.rb +12 -12
  175. data/test/acceptance/stubbing_non_public_instance_method_test.rb +12 -12
  176. data/test/acceptance/stubbing_on_non_mock_object_test.rb +9 -9
  177. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +35 -0
  178. data/test/acceptance/throw_test.rb +1 -1
  179. data/test/deprecation_disabler.rb +1 -1
  180. data/test/execution_point.rb +7 -7
  181. data/test/method_definer.rb +1 -1
  182. data/test/mini_test_result.rb +21 -21
  183. data/test/simple_counter.rb +5 -5
  184. data/test/test_helper.rb +1 -1
  185. data/test/test_runner.rb +22 -24
  186. data/test/test_unit_result.rb +20 -0
  187. data/test/unit/any_instance_method_test.rb +28 -32
  188. data/test/unit/array_inspect_test.rb +3 -3
  189. data/test/unit/backtrace_filter_test.rb +3 -3
  190. data/test/unit/cardinality_test.rb +12 -12
  191. data/test/unit/central_test.rb +18 -18
  192. data/test/unit/change_state_side_effect_test.rb +12 -12
  193. data/test/unit/class_method_test.rb +33 -91
  194. data/test/unit/date_time_inspect_test.rb +4 -4
  195. data/test/unit/exception_raiser_test.rb +5 -5
  196. data/test/unit/expectation_list_test.rb +7 -7
  197. data/test/unit/expectation_test.rb +66 -66
  198. data/test/unit/hash_inspect_test.rb +4 -4
  199. data/test/unit/in_state_ordering_constraint_test.rb +13 -13
  200. data/test/unit/method_matcher_test.rb +3 -3
  201. data/test/unit/mock_test.rb +40 -40
  202. data/test/unit/mockery_test.rb +25 -25
  203. data/test/unit/multiple_yields_test.rb +2 -2
  204. data/test/unit/no_yields_test.rb +2 -2
  205. data/test/unit/object_inspect_test.rb +4 -4
  206. data/test/unit/object_test.rb +15 -15
  207. data/test/unit/parameter_matchers/all_of_test.rb +6 -6
  208. data/test/unit/parameter_matchers/any_of_test.rb +6 -6
  209. data/test/unit/parameter_matchers/anything_test.rb +5 -5
  210. data/test/unit/parameter_matchers/has_entries_test.rb +10 -10
  211. data/test/unit/parameter_matchers/has_entry_test.rb +13 -13
  212. data/test/unit/parameter_matchers/has_key_test.rb +11 -11
  213. data/test/unit/parameter_matchers/has_value_test.rb +12 -12
  214. data/test/unit/parameter_matchers/includes_test.rb +4 -4
  215. data/test/unit/parameter_matchers/not_test.rb +6 -6
  216. data/test/unit/parameter_matchers/regexp_matches_test.rb +9 -9
  217. data/test/unit/parameter_matchers/responds_with_test.rb +6 -6
  218. data/test/unit/parameter_matchers/stub_matcher.rb +4 -4
  219. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +6 -6
  220. data/test/unit/parameters_matcher_test.rb +16 -16
  221. data/test/unit/return_values_test.rb +5 -5
  222. data/test/unit/sequence_test.rb +10 -10
  223. data/test/unit/single_return_value_test.rb +3 -3
  224. data/test/unit/single_yield_test.rb +2 -2
  225. data/test/unit/state_machine_test.rb +19 -19
  226. data/test/unit/string_inspect_test.rb +2 -2
  227. data/test/unit/thrower_test.rb +1 -1
  228. data/test/unit/yield_parameters_test.rb +11 -11
  229. metadata +113 -101
@@ -7,41 +7,41 @@ require 'mocha/configuration'
7
7
  require 'mocha/stubbing_error'
8
8
 
9
9
  module Mocha
10
-
10
+
11
11
  class Mockery
12
-
12
+
13
13
  class << self
14
-
14
+
15
15
  def instance
16
16
  @instance ||= new
17
17
  end
18
-
18
+
19
19
  def reset_instance
20
20
  @instance = nil
21
21
  end
22
-
22
+
23
23
  end
24
-
24
+
25
25
  def named_mock(name, &block)
26
26
  add_mock(Mock.new(self, Name.new(name), &block))
27
27
  end
28
-
28
+
29
29
  def unnamed_mock(&block)
30
30
  add_mock(Mock.new(self, &block))
31
31
  end
32
-
32
+
33
33
  def mock_impersonating(object, &block)
34
34
  add_mock(Mock.new(self, ImpersonatingName.new(object), &block))
35
35
  end
36
-
36
+
37
37
  def mock_impersonating_any_instance_of(klass, &block)
38
38
  add_mock(Mock.new(self, ImpersonatingAnyInstanceName.new(klass), &block))
39
39
  end
40
-
40
+
41
41
  def new_state_machine(name)
42
42
  add_state_machine(StateMachine.new(name))
43
43
  end
44
-
44
+
45
45
  def verify(assertion_counter = nil)
46
46
  unless mocks.all? { |mock| mock.__verified__?(assertion_counter) }
47
47
  message = "not all expectations were satisfied\n#{mocha_inspect}"
@@ -60,24 +60,24 @@ module Mocha
60
60
  end
61
61
  end
62
62
  end
63
-
63
+
64
64
  def teardown
65
65
  stubba.unstub_all
66
66
  reset
67
67
  end
68
-
68
+
69
69
  def stubba
70
70
  @stubba ||= Central.new
71
71
  end
72
-
72
+
73
73
  def mocks
74
74
  @mocks ||= []
75
75
  end
76
-
76
+
77
77
  def state_machines
78
78
  @state_machines ||= []
79
79
  end
80
-
80
+
81
81
  def mocha_inspect
82
82
  message = ""
83
83
  message << "unsatisfied expectations:\n- #{unsatisfied_expectations.map { |e| e.mocha_inspect }.join("\n- ")}\n" unless unsatisfied_expectations.empty?
@@ -85,7 +85,7 @@ module Mocha
85
85
  message << "states:\n- #{state_machines.map { |sm| sm.mocha_inspect }.join("\n- ")}" unless state_machines.empty?
86
86
  message
87
87
  end
88
-
88
+
89
89
  def on_stubbing(object, method)
90
90
  method = RUBY_VERSION < '1.9' ? method.to_s : method.to_sym
91
91
  unless Mocha::Configuration.allow?(:stubbing_non_existent_method)
@@ -98,11 +98,16 @@ module Mocha
98
98
  on_stubbing_non_public_method(object, method)
99
99
  end
100
100
  end
101
+ unless Mocha::Configuration.allow?(:stubbing_method_on_nil)
102
+ if object.nil?
103
+ on_stubbing_method_on_nil(object, method)
104
+ end
105
+ end
101
106
  unless Mocha::Configuration.allow?(:stubbing_method_on_non_mock_object)
102
107
  on_stubbing_method_on_non_mock_object(object, method)
103
108
  end
104
109
  end
105
-
110
+
106
111
  def on_stubbing_non_existent_method(object, method)
107
112
  if Mocha::Configuration.prevent?(:stubbing_non_existent_method)
108
113
  raise StubbingError.new("stubbing non-existent method: #{object.mocha_inspect}.#{method}", caller)
@@ -111,7 +116,7 @@ module Mocha
111
116
  logger.warn "stubbing non-existent method: #{object.mocha_inspect}.#{method}"
112
117
  end
113
118
  end
114
-
119
+
115
120
  def on_stubbing_non_public_method(object, method)
116
121
  if Mocha::Configuration.prevent?(:stubbing_non_public_method)
117
122
  raise StubbingError.new("stubbing non-public method: #{object.mocha_inspect}.#{method}", caller)
@@ -120,7 +125,16 @@ module Mocha
120
125
  logger.warn "stubbing non-public method: #{object.mocha_inspect}.#{method}"
121
126
  end
122
127
  end
123
-
128
+
129
+ def on_stubbing_method_on_nil(object, method)
130
+ if Mocha::Configuration.prevent?(:stubbing_method_on_nil)
131
+ raise StubbingError.new("stubbing method on nil: #{object.mocha_inspect}.#{method}", caller)
132
+ end
133
+ if Mocha::Configuration.warn_when?(:stubbing_method_on_nil)
134
+ logger.warn "stubbing method on nil: #{object.mocha_inspect}.#{method}"
135
+ end
136
+ end
137
+
124
138
  def on_stubbing_method_on_non_mock_object(object, method)
125
139
  if Mocha::Configuration.prevent?(:stubbing_method_on_non_mock_object)
126
140
  raise StubbingError.new("stubbing method on non-mock object: #{object.mocha_inspect}.#{method}", caller)
@@ -129,7 +143,7 @@ module Mocha
129
143
  logger.warn "stubbing method on non-mock object: #{object.mocha_inspect}.#{method}"
130
144
  end
131
145
  end
132
-
146
+
133
147
  def on_stubbing_method_unnecessarily(expectation)
134
148
  if Mocha::Configuration.prevent?(:stubbing_method_unnecessarily)
135
149
  raise StubbingError.new("stubbing method unnecessarily: #{expectation.method_signature}", expectation.backtrace)
@@ -138,44 +152,44 @@ module Mocha
138
152
  logger.warn "stubbing method unnecessarily: #{expectation.method_signature}"
139
153
  end
140
154
  end
141
-
155
+
142
156
  attr_writer :logger
143
-
157
+
144
158
  def logger
145
159
  @logger ||= Logger.new($stderr)
146
160
  end
147
-
148
-
161
+
162
+
149
163
  private
150
-
164
+
151
165
  def expectations
152
166
  mocks.map { |mock| mock.__expectations__.to_a }.flatten
153
167
  end
154
-
168
+
155
169
  def unsatisfied_expectations
156
170
  expectations.reject { |e| e.verified? }
157
171
  end
158
-
172
+
159
173
  def satisfied_expectations
160
174
  expectations.select { |e| e.verified? }
161
175
  end
162
-
176
+
163
177
  def add_mock(mock)
164
178
  mocks << mock
165
179
  mock
166
180
  end
167
-
181
+
168
182
  def add_state_machine(state_machine)
169
183
  state_machines << state_machine
170
184
  state_machine
171
185
  end
172
-
186
+
173
187
  def reset
174
188
  @stubba = nil
175
189
  @mocks = nil
176
190
  @state_machines = nil
177
191
  end
178
-
192
+
179
193
  end
180
-
194
+
181
195
  end
@@ -12,5 +12,5 @@ module Mocha
12
12
  end
13
13
 
14
14
  end
15
-
16
- end
15
+
16
+ end
@@ -1,20 +1,20 @@
1
- module Mocha # :nodoc:
2
-
3
- class MultipleYields # :nodoc:
4
-
1
+ module Mocha
2
+
3
+ class MultipleYields
4
+
5
5
  attr_reader :parameter_groups
6
-
6
+
7
7
  def initialize(*parameter_groups)
8
8
  @parameter_groups = parameter_groups
9
9
  end
10
-
10
+
11
11
  def each
12
12
  @parameter_groups.each do |parameter_group|
13
13
  yield(parameter_group)
14
14
  end
15
15
  end
16
-
16
+
17
17
  end
18
-
18
+
19
19
  end
20
-
20
+
@@ -1,53 +1,53 @@
1
1
  module Mocha
2
-
2
+
3
3
  class ImpersonatingName
4
-
4
+
5
5
  def initialize(object)
6
6
  @object = object
7
7
  end
8
-
8
+
9
9
  def mocha_inspect
10
10
  @object.mocha_inspect
11
11
  end
12
-
12
+
13
13
  end
14
-
14
+
15
15
  class ImpersonatingAnyInstanceName
16
-
16
+
17
17
  def initialize(klass)
18
18
  @klass = klass
19
19
  end
20
-
20
+
21
21
  def mocha_inspect
22
22
  "#<AnyInstance:#{@klass.mocha_inspect}>"
23
23
  end
24
-
24
+
25
25
  end
26
-
26
+
27
27
  class Name
28
-
28
+
29
29
  def initialize(name)
30
30
  @name = name
31
31
  end
32
-
32
+
33
33
  def mocha_inspect
34
34
  "#<Mock:#{@name}>"
35
35
  end
36
-
36
+
37
37
  end
38
-
38
+
39
39
  class DefaultName
40
-
40
+
41
41
  def initialize(mock)
42
42
  @mock = mock
43
43
  end
44
-
44
+
45
45
  def mocha_inspect
46
46
  address = @mock.__id__ * 2
47
47
  address += 0x100000000 if address < 0
48
48
  "#<Mock:0x#{'%x' % address}>"
49
49
  end
50
-
50
+
51
51
  end
52
-
53
- end
52
+
53
+ end
@@ -1,11 +1,11 @@
1
- module Mocha # :nodoc:
2
-
3
- class NoYields # :nodoc:
4
-
1
+ module Mocha
2
+
3
+ class NoYields
4
+
5
5
  def each
6
6
  end
7
-
7
+
8
8
  end
9
-
9
+
10
10
  end
11
-
11
+
@@ -6,42 +6,50 @@ require 'mocha/any_instance_method'
6
6
  require 'mocha/argument_iterator'
7
7
 
8
8
  module Mocha
9
-
10
- # Methods added to all objects to allow mocking and stubbing on real objects.
9
+
10
+ # Methods added to all objects to allow mocking and stubbing on real (i.e. non-mock) objects.
11
11
  #
12
- # Methods return a Mocha::Expectation which can be further modified by methods on Mocha::Expectation.
12
+ # Both {#expects} and {#stubs} return an {Expectation} which can be further modified by methods on {Expectation}.
13
13
  module ObjectMethods
14
-
15
- def mocha # :nodoc:
14
+
15
+ # @private
16
+ def mocha
16
17
  @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
17
18
  end
18
-
19
- def reset_mocha # :nodoc:
19
+
20
+ # @private
21
+ def reset_mocha
20
22
  @mocha = nil
21
23
  end
22
-
23
- def stubba_method # :nodoc:
24
+
25
+ # @private
26
+ def stubba_method
24
27
  Mocha::InstanceMethod
25
28
  end
26
-
27
- def stubba_object # :nodoc:
29
+
30
+ # @private
31
+ def stubba_object
28
32
  self
29
33
  end
30
-
31
- # :call-seq: expects(method_name) -> expectation
32
- # expects(method_names_vs_return_values) -> last expectation
34
+
35
+ # Adds an expectation that the specified method must be called exactly once with any parameters.
36
+ #
37
+ # The original implementation of the method is replaced during the test and then restored at the end of the test.
38
+ #
39
+ # @param [Symbol,String] method_name name of expected method
40
+ # @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {#expects} were called multiple times.
33
41
  #
34
- # Adds an expectation that a method identified by +method_name+ Symbol must be called exactly once with any parameters.
35
- # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
42
+ # @overload def expects(method_name)
43
+ # @overload def expects(expected_methods_vs_return_values)
44
+ # @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
45
+ # @raise [StubbingError] if attempting to stub method which is not allowed.
46
+ #
47
+ # @example Setting up an expectation on a non-mock object.
36
48
  # product = Product.new
37
49
  # product.expects(:save).returns(true)
38
50
  # assert_equal true, product.save
39
51
  #
40
- # The original implementation of <tt>Product#save</tt> is replaced temporarily.
41
- #
42
- # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
43
- #
44
- # 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+.
52
+ # @example Setting up multiple expectations on a non-mock object.
45
53
  # product = Product.new
46
54
  # product.expects(:valid? => true, :save => true)
47
55
  #
@@ -50,13 +58,18 @@ module Mocha
50
58
  # product = Product.new
51
59
  # product.expects(:valid?).returns(true)
52
60
  # product.expects(:save).returns(true)
53
- def expects(method_name_or_hash)
54
- if method_name_or_hash.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/i
61
+ #
62
+ # @see Mock#expects
63
+ def expects(expected_methods_vs_return_values)
64
+ if expected_methods_vs_return_values.to_s =~ /the[^a-z]*spanish[^a-z]*inquisition/i
55
65
  raise Mocha::ExpectationError.new('NOBODY EXPECTS THE SPANISH INQUISITION!')
56
66
  end
67
+ if frozen?
68
+ raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
69
+ end
57
70
  expectation = nil
58
71
  mockery = Mocha::Mockery.instance
59
- iterator = ArgumentIterator.new(method_name_or_hash)
72
+ iterator = ArgumentIterator.new(expected_methods_vs_return_values)
60
73
  iterator.each { |*args|
61
74
  method_name = args.shift
62
75
  mockery.on_stubbing(self, method_name)
@@ -67,21 +80,23 @@ module Mocha
67
80
  }
68
81
  expectation
69
82
  end
70
-
71
- # :call-seq: stubs(method_name) -> expectation
72
- # stubs(method_names_vs_return_values) -> last expectation
83
+
84
+ # Adds an expectation that the specified method may be called any number of times with any parameters.
73
85
  #
74
- # Adds an expectation that a method identified by +method_name+ Symbol may be called any number of times with any parameters.
75
- # Returns the new expectation which can be further modified by methods on Mocha::Expectation.
86
+ # @param [Symbol,String] method_name name of stubbed method
87
+ # @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {#stubs} were called multiple times.
88
+ #
89
+ # @overload def stubs(method_name)
90
+ # @overload def stubs(stubbed_methods_vs_return_values)
91
+ # @return [Expectation] last-built expectation which can be further modified by methods on {Expectation}.
92
+ # @raise [StubbingError] if attempting to stub method which is not allowed.
93
+ #
94
+ # @example Setting up a stubbed methods on a non-mock object.
76
95
  # product = Product.new
77
96
  # product.stubs(:save).returns(true)
78
97
  # assert_equal true, product.save
79
98
  #
80
- # The original implementation of <tt>Product#save</tt> is replaced temporarily.
81
- #
82
- # The original implementation of <tt>Product#save</tt> is restored at the end of the test.
83
- #
84
- # 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+.
99
+ # @example Setting up multiple stubbed methods on a non-mock object.
85
100
  # product = Product.new
86
101
  # product.stubs(:valid? => true, :save => true)
87
102
  #
@@ -90,10 +105,15 @@ module Mocha
90
105
  # product = Product.new
91
106
  # product.stubs(:valid?).returns(true)
92
107
  # product.stubs(:save).returns(true)
93
- def stubs(method_name_or_hash)
108
+ #
109
+ # @see Mock#stubs
110
+ def stubs(stubbed_methods_vs_return_values)
111
+ if frozen?
112
+ raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
113
+ end
94
114
  expectation = nil
95
115
  mockery = Mocha::Mockery.instance
96
- iterator = ArgumentIterator.new(method_name_or_hash)
116
+ iterator = ArgumentIterator.new(stubbed_methods_vs_return_values)
97
117
  iterator.each { |*args|
98
118
  method_name = args.shift
99
119
  mockery.on_stubbing(self, method_name)
@@ -104,26 +124,24 @@ module Mocha
104
124
  }
105
125
  expectation
106
126
  end
107
-
108
- # :call-seq: unstub(*method_names)
127
+
128
+ # Removes the specified stubbed methods (added by calls to {#expects} or {#stubs}) and all expectations associated with them.
129
+ #
130
+ # Restores the original behaviour of the methods before they were stubbed.
109
131
  #
110
- # Removes the method stub added by calls to #expects or #stubs.
111
- # Restores the original behaviour of the method before it was stubbed.
132
+ # WARNING: If you {#unstub} a method which still has unsatisfied expectations, you may be removing the only way those expectations can be satisfied. Use {#unstub} with care.
133
+ #
134
+ # @param [Array<Symbol>] method_names names of methods to unstub.
135
+ #
136
+ # @example Stubbing and unstubbing a method on a real (non-mock) object.
112
137
  # multiplier = Multiplier.new
113
138
  # multiplier.double(2) # => 4
114
- # multiplier.stubs(:double).raises
139
+ # multiplier.stubs(:double).raises # new behaviour defined
115
140
  # multiplier.double(2) # => raises exception
116
- # multiplier.unstub(:double)
141
+ # multiplier.unstub(:double) # original behaviour restored
117
142
  # multiplier.double(2) # => 4
118
143
  #
119
- # The original implementation of <tt>Multiplier#double</tt> is replaced temporarily.
120
- #
121
- # The original implementation of <tt>Multiplier#double</tt> is restored when #unstub is called.
122
- #
123
- # WARNING: If you #unstub a method which still has unsatisfied expectations, you may be removing
124
- # the only way those expectations can be satisfied. Use #unstub with care.
125
- #
126
- # If multiple +method_names+ are supplied, each method is unstubbed.
144
+ # @example Unstubbing multiple methods on a real (non-mock) object.
127
145
  # multiplier.unstub(:double, :triple)
128
146
  #
129
147
  # # exactly equivalent to
@@ -137,8 +155,9 @@ module Mocha
137
155
  mockery.stubba.unstub(method)
138
156
  end
139
157
  end
140
-
141
- def method_exists?(method, include_public_methods = true) # :nodoc:
158
+
159
+ # @private
160
+ def method_exists?(method, include_public_methods = true)
142
161
  if include_public_methods
143
162
  return true if public_methods(include_superclass_methods = true).include?(method)
144
163
  return true if respond_to?(method.to_sym)
@@ -147,30 +166,33 @@ module Mocha
147
166
  return true if private_methods(include_superclass_methods = true).include?(method)
148
167
  return false
149
168
  end
150
-
169
+
151
170
  end
152
-
153
- module ModuleMethods # :nodoc:
154
-
171
+
172
+ # @private
173
+ module ModuleMethods
174
+
155
175
  def stubba_method
156
176
  Mocha::ModuleMethod
157
177
  end
158
-
178
+
159
179
  end
160
-
161
- # Methods added all classes to allow mocking and stubbing on real objects.
180
+
181
+ # Methods added to all classes to allow mocking and stubbing on real (i.e. non-mock) objects.
162
182
  module ClassMethods
163
-
164
- def stubba_method # :nodoc:
183
+
184
+ # @private
185
+ def stubba_method
165
186
  Mocha::ClassMethod
166
187
  end
167
188
 
168
- class AnyInstance # :nodoc:
169
-
189
+ # @private
190
+ class AnyInstance
191
+
170
192
  def initialize(klass)
171
193
  @stubba_object = klass
172
194
  end
173
-
195
+
174
196
  def mocha
175
197
  @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
176
198
  end
@@ -178,11 +200,11 @@ module Mocha
178
200
  def stubba_method
179
201
  Mocha::AnyInstanceMethod
180
202
  end
181
-
203
+
182
204
  def stubba_object
183
205
  @stubba_object
184
206
  end
185
-
207
+
186
208
  def method_exists?(method, include_public_methods = true)
187
209
  if include_public_methods
188
210
  return true if @stubba_object.public_instance_methods(include_superclass_methods = true).include?(method)
@@ -191,33 +213,40 @@ module Mocha
191
213
  return true if @stubba_object.private_instance_methods(include_superclass_methods = true).include?(method)
192
214
  return false
193
215
  end
194
-
216
+
195
217
  end
196
-
197
- # :call-seq: any_instance -> mock object
218
+
219
+ # @return [Mock] a mock object which will detect calls to any instance of this class.
220
+ # @raise [StubbingError] if attempting to stub method which is not allowed.
198
221
  #
199
- # Returns a mock object which will detect calls to any instance of this class.
222
+ # @example Return false to invocation of +Product#save+ for any instance of +Product+.
200
223
  # Product.any_instance.stubs(:save).returns(false)
201
224
  # product_1 = Product.new
202
225
  # assert_equal false, product_1.save
203
226
  # product_2 = Product.new
204
227
  # assert_equal false, product_2.save
205
228
  def any_instance
229
+ if frozen?
230
+ raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}.any_instance", caller)
231
+ end
206
232
  @any_instance ||= AnyInstance.new(self)
207
233
  end
208
-
234
+
209
235
  end
210
-
236
+
211
237
  end
212
238
 
213
- class Object # :nodoc:
239
+ # @private
240
+ class Object
214
241
  include Mocha::ObjectMethods
215
242
  end
216
243
 
217
- class Module # :nodoc:
244
+ # @private
245
+ class Module
218
246
  include Mocha::ModuleMethods
219
247
  end
220
248
 
221
- class Class # :nodoc:
249
+ # @private
250
+ class Class
222
251
  include Mocha::ClassMethods
223
252
  end