mocha 1.8.0 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (274) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop.yml +3 -0
  4. data/.rubocop_todo.yml +7 -30
  5. data/.yardopts +1 -0
  6. data/CONTRIBUTING.md +1 -1
  7. data/README.md +56 -31
  8. data/RELEASE.md +166 -0
  9. data/Rakefile +15 -7
  10. data/init.rb +1 -3
  11. data/lib/mocha/any_instance_method.rb +11 -53
  12. data/lib/mocha/api.rb +119 -56
  13. data/lib/mocha/block_matcher.rb +31 -0
  14. data/lib/mocha/cardinality.rb +52 -34
  15. data/lib/mocha/class_methods.rb +17 -15
  16. data/lib/mocha/configuration.rb +351 -67
  17. data/lib/mocha/deprecation.rb +2 -1
  18. data/lib/mocha/detection/test_unit.rb +1 -3
  19. data/lib/mocha/exception_raiser.rb +2 -1
  20. data/lib/mocha/expectation.rb +120 -81
  21. data/lib/mocha/expectation_error.rb +1 -3
  22. data/lib/mocha/expectation_list.rb +6 -6
  23. data/lib/mocha/inspect.rb +30 -26
  24. data/lib/mocha/instance_method.rb +19 -2
  25. data/lib/mocha/integration/mini_test.rb +7 -0
  26. data/lib/mocha/integration/test_unit.rb +7 -0
  27. data/lib/mocha/integration.rb +1 -3
  28. data/lib/mocha/invocation.rb +77 -0
  29. data/lib/mocha/macos_version.rb +5 -0
  30. data/lib/mocha/minitest.rb +6 -1
  31. data/lib/mocha/mock.rb +80 -39
  32. data/lib/mocha/mockery.rb +34 -80
  33. data/lib/mocha/names.rb +1 -1
  34. data/lib/mocha/object_methods.rb +13 -19
  35. data/lib/mocha/parameter_matchers/all_of.rb +1 -1
  36. data/lib/mocha/parameter_matchers/any_of.rb +1 -1
  37. data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -9
  38. data/lib/mocha/parameter_matchers/has_entries.rb +2 -3
  39. data/lib/mocha/parameter_matchers/has_entry.rb +24 -16
  40. data/lib/mocha/parameter_matchers/has_key.rb +2 -3
  41. data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
  42. data/lib/mocha/parameter_matchers/has_value.rb +2 -3
  43. data/lib/mocha/parameter_matchers/includes.rb +2 -0
  44. data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
  45. data/lib/mocha/parameter_matchers/is_a.rb +2 -3
  46. data/lib/mocha/parameter_matchers/not.rb +2 -3
  47. data/lib/mocha/parameter_matchers.rb +2 -1
  48. data/lib/mocha/raised_exception.rb +11 -0
  49. data/lib/mocha/return_values.rb +3 -3
  50. data/lib/mocha/setup.rb +5 -0
  51. data/lib/mocha/single_return_value.rb +2 -1
  52. data/lib/mocha/singleton_class.rb +9 -0
  53. data/lib/mocha/state_machine.rb +31 -40
  54. data/lib/mocha/stubbed_method.rb +125 -0
  55. data/lib/mocha/test_unit.rb +6 -1
  56. data/lib/mocha/thrower.rb +2 -1
  57. data/lib/mocha/thrown_object.rb +12 -0
  58. data/lib/mocha/version.rb +1 -1
  59. data/lib/mocha/yield_parameters.rb +5 -11
  60. data/lib/mocha.rb +8 -0
  61. data/mocha.gemspec +14 -10
  62. metadata +16 -233
  63. data/bin/build-matrix +0 -83
  64. data/docs/CNAME +0 -1
  65. data/docs/Mocha/API.html +0 -1056
  66. data/docs/Mocha/ClassMethods.html +0 -267
  67. data/docs/Mocha/Configuration.html +0 -626
  68. data/docs/Mocha/Expectation.html +0 -2709
  69. data/docs/Mocha/ExpectationError.html +0 -157
  70. data/docs/Mocha/ExpectationErrorFactory.html +0 -269
  71. data/docs/Mocha/Hooks.html +0 -385
  72. data/docs/Mocha/Integration/MiniTest/Adapter.html +0 -165
  73. data/docs/Mocha/Integration/MiniTest.html +0 -123
  74. data/docs/Mocha/Integration/TestUnit/Adapter.html +0 -165
  75. data/docs/Mocha/Integration/TestUnit.html +0 -123
  76. data/docs/Mocha/Integration.html +0 -125
  77. data/docs/Mocha/Mock.html +0 -1197
  78. data/docs/Mocha/ObjectMethods.html +0 -712
  79. data/docs/Mocha/ParameterMatchers/AllOf.html +0 -154
  80. data/docs/Mocha/ParameterMatchers/AnyOf.html +0 -154
  81. data/docs/Mocha/ParameterMatchers/AnyParameters.html +0 -153
  82. data/docs/Mocha/ParameterMatchers/Anything.html +0 -153
  83. data/docs/Mocha/ParameterMatchers/Base.html +0 -448
  84. data/docs/Mocha/ParameterMatchers/Equals.html +0 -154
  85. data/docs/Mocha/ParameterMatchers/EquivalentUri.html +0 -153
  86. data/docs/Mocha/ParameterMatchers/HasEntries.html +0 -154
  87. data/docs/Mocha/ParameterMatchers/HasEntry.html +0 -154
  88. data/docs/Mocha/ParameterMatchers/HasKey.html +0 -154
  89. data/docs/Mocha/ParameterMatchers/HasValue.html +0 -154
  90. data/docs/Mocha/ParameterMatchers/Includes.html +0 -154
  91. data/docs/Mocha/ParameterMatchers/InstanceOf.html +0 -154
  92. data/docs/Mocha/ParameterMatchers/IsA.html +0 -153
  93. data/docs/Mocha/ParameterMatchers/KindOf.html +0 -154
  94. data/docs/Mocha/ParameterMatchers/Not.html +0 -154
  95. data/docs/Mocha/ParameterMatchers/Optionally.html +0 -153
  96. data/docs/Mocha/ParameterMatchers/RegexpMatches.html +0 -154
  97. data/docs/Mocha/ParameterMatchers/RespondsWith.html +0 -154
  98. data/docs/Mocha/ParameterMatchers/YamlEquivalent.html +0 -154
  99. data/docs/Mocha/ParameterMatchers.html +0 -3049
  100. data/docs/Mocha/Sequence.html +0 -149
  101. data/docs/Mocha/StateMachine/State.html +0 -141
  102. data/docs/Mocha/StateMachine/StatePredicate.html +0 -141
  103. data/docs/Mocha/StateMachine.html +0 -539
  104. data/docs/Mocha/StubbingError.html +0 -150
  105. data/docs/Mocha/UnexpectedInvocation.html +0 -140
  106. data/docs/Mocha.html +0 -127
  107. data/docs/_index.html +0 -537
  108. data/docs/class_list.html +0 -51
  109. data/docs/css/common.css +0 -1
  110. data/docs/css/full_list.css +0 -58
  111. data/docs/css/style.css +0 -496
  112. data/docs/file.COPYING.html +0 -81
  113. data/docs/file.MIT-LICENSE.html +0 -85
  114. data/docs/file.README.html +0 -418
  115. data/docs/file.RELEASE.html +0 -875
  116. data/docs/file_list.html +0 -71
  117. data/docs/frames.html +0 -17
  118. data/docs/index.html +0 -418
  119. data/docs/js/app.js +0 -292
  120. data/docs/js/full_list.js +0 -216
  121. data/docs/js/jquery.js +0 -4
  122. data/docs/method_list.html +0 -571
  123. data/docs/top-level-namespace.html +0 -118
  124. data/gemfiles/Gemfile.minitest.1.3.0 +0 -7
  125. data/gemfiles/Gemfile.minitest.1.4.0 +0 -7
  126. data/gemfiles/Gemfile.minitest.1.4.1 +0 -7
  127. data/gemfiles/Gemfile.minitest.1.4.2 +0 -7
  128. data/gemfiles/Gemfile.minitest.2.0.0 +0 -7
  129. data/gemfiles/Gemfile.minitest.2.0.1 +0 -7
  130. data/gemfiles/Gemfile.minitest.2.11.0 +0 -7
  131. data/gemfiles/Gemfile.minitest.2.11.2 +0 -7
  132. data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
  133. data/gemfiles/Gemfile.test-unit.2.0.0 +0 -7
  134. data/gemfiles/Gemfile.test-unit.2.0.1 +0 -7
  135. data/gemfiles/Gemfile.test-unit.2.0.3 +0 -7
  136. data/lib/mocha/class_method.rb +0 -113
  137. data/lib/mocha/mini_test.rb +0 -5
  138. data/lib/mocha/module_method.rb +0 -6
  139. data/lib/mocha/module_methods.rb +0 -10
  140. data/lib/mocha/multiple_yields.rb +0 -15
  141. data/lib/mocha/no_yields.rb +0 -5
  142. data/lib/mocha/parameter_matchers/object.rb +0 -15
  143. data/lib/mocha/pretty_parameters.rb +0 -24
  144. data/lib/mocha/single_yield.rb +0 -13
  145. data/lib/mocha/standalone.rb +0 -4
  146. data/lib/mocha/unexpected_invocation.rb +0 -24
  147. data/lib/mocha_standalone.rb +0 -4
  148. data/test/acceptance/acceptance_test_helper.rb +0 -35
  149. data/test/acceptance/bug_18914_test.rb +0 -38
  150. data/test/acceptance/bug_21465_test.rb +0 -32
  151. data/test/acceptance/bug_21563_test.rb +0 -23
  152. data/test/acceptance/exception_rescue_test.rb +0 -54
  153. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -56
  154. data/test/acceptance/expected_invocation_count_test.rb +0 -230
  155. data/test/acceptance/failure_messages_test.rb +0 -62
  156. data/test/acceptance/issue_272_test.rb +0 -51
  157. data/test/acceptance/issue_65_test.rb +0 -65
  158. data/test/acceptance/issue_70_test.rb +0 -54
  159. data/test/acceptance/mocha_example_test.rb +0 -97
  160. data/test/acceptance/mocha_test_result_test.rb +0 -84
  161. data/test/acceptance/mock_test.rb +0 -98
  162. data/test/acceptance/mock_with_initializer_block_test.rb +0 -56
  163. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -76
  164. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -67
  165. data/test/acceptance/optional_parameters_test.rb +0 -68
  166. data/test/acceptance/parameter_matcher_test.rb +0 -300
  167. data/test/acceptance/partial_mocks_test.rb +0 -45
  168. data/test/acceptance/prepend_test.rb +0 -87
  169. data/test/acceptance/prevent_use_of_mocha_outside_test_test.rb +0 -77
  170. data/test/acceptance/raise_exception_test.rb +0 -37
  171. data/test/acceptance/return_value_test.rb +0 -50
  172. data/test/acceptance/sequence_test.rb +0 -190
  173. data/test/acceptance/states_test.rb +0 -72
  174. data/test/acceptance/stub_any_instance_method_defined_on_superclass_test.rb +0 -65
  175. data/test/acceptance/stub_any_instance_method_test.rb +0 -282
  176. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -106
  177. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -79
  178. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -74
  179. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -145
  180. data/test/acceptance/stub_everything_test.rb +0 -54
  181. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -92
  182. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -68
  183. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -68
  184. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -137
  185. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -77
  186. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -76
  187. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -68
  188. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -71
  189. data/test/acceptance/stub_method_defined_on_module_and_aliased_test.rb +0 -39
  190. data/test/acceptance/stub_module_method_test.rb +0 -201
  191. data/test/acceptance/stub_test.rb +0 -50
  192. data/test/acceptance/stubba_example_test.rb +0 -94
  193. data/test/acceptance/stubba_test_result_test.rb +0 -72
  194. data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
  195. data/test/acceptance/stubbing_frozen_object_test.rb +0 -89
  196. data/test/acceptance/stubbing_method_accepting_block_parameter_test.rb +0 -53
  197. data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -63
  198. data/test/acceptance/stubbing_nil_test.rb +0 -60
  199. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -141
  200. data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
  201. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -145
  202. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -128
  203. data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
  204. data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -141
  205. data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -70
  206. data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +0 -34
  207. data/test/acceptance/throw_test.rb +0 -43
  208. data/test/acceptance/unexpected_invocation_test.rb +0 -24
  209. data/test/acceptance/unstubbing_test.rb +0 -195
  210. data/test/assertions.rb +0 -8
  211. data/test/deprecation_disabler.rb +0 -15
  212. data/test/execution_point.rb +0 -36
  213. data/test/integration/mini_test_test.rb +0 -8
  214. data/test/integration/shared_tests.rb +0 -176
  215. data/test/integration/test_unit_test.rb +0 -8
  216. data/test/method_definer.rb +0 -22
  217. data/test/mini_test_result.rb +0 -96
  218. data/test/minitest_result.rb +0 -48
  219. data/test/simple_counter.rb +0 -11
  220. data/test/test_helper.rb +0 -58
  221. data/test/test_runner.rb +0 -56
  222. data/test/test_unit_result.rb +0 -22
  223. data/test/unit/any_instance_method_test.rb +0 -150
  224. data/test/unit/array_inspect_test.rb +0 -14
  225. data/test/unit/backtrace_filter_test.rb +0 -17
  226. data/test/unit/cardinality_test.rb +0 -54
  227. data/test/unit/central_test.rb +0 -99
  228. data/test/unit/change_state_side_effect_test.rb +0 -37
  229. data/test/unit/class_method_test.rb +0 -254
  230. data/test/unit/class_methods_test.rb +0 -69
  231. data/test/unit/configuration_test.rb +0 -37
  232. data/test/unit/date_time_inspect_test.rb +0 -19
  233. data/test/unit/exception_raiser_test.rb +0 -40
  234. data/test/unit/expectation_list_test.rb +0 -80
  235. data/test/unit/expectation_test.rb +0 -492
  236. data/test/unit/hash_inspect_test.rb +0 -14
  237. data/test/unit/hooks_test.rb +0 -35
  238. data/test/unit/in_state_ordering_constraint_test.rb +0 -39
  239. data/test/unit/method_matcher_test.rb +0 -26
  240. data/test/unit/mock_test.rb +0 -363
  241. data/test/unit/mockery_test.rb +0 -174
  242. data/test/unit/module_methods_test.rb +0 -17
  243. data/test/unit/multiple_yields_test.rb +0 -16
  244. data/test/unit/no_yields_test.rb +0 -16
  245. data/test/unit/object_inspect_test.rb +0 -58
  246. data/test/unit/object_methods_test.rb +0 -62
  247. data/test/unit/parameter_matchers/all_of_test.rb +0 -24
  248. data/test/unit/parameter_matchers/any_of_test.rb +0 -24
  249. data/test/unit/parameter_matchers/anything_test.rb +0 -19
  250. data/test/unit/parameter_matchers/equals_test.rb +0 -23
  251. data/test/unit/parameter_matchers/equivalent_uri_test.rb +0 -50
  252. data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
  253. data/test/unit/parameter_matchers/has_entry_test.rb +0 -128
  254. data/test/unit/parameter_matchers/has_key_test.rb +0 -54
  255. data/test/unit/parameter_matchers/has_value_test.rb +0 -55
  256. data/test/unit/parameter_matchers/includes_test.rb +0 -106
  257. data/test/unit/parameter_matchers/instance_of_test.rb +0 -23
  258. data/test/unit/parameter_matchers/is_a_test.rb +0 -23
  259. data/test/unit/parameter_matchers/kind_of_test.rb +0 -23
  260. data/test/unit/parameter_matchers/not_test.rb +0 -24
  261. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -45
  262. data/test/unit/parameter_matchers/responds_with_test.rb +0 -38
  263. data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
  264. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -23
  265. data/test/unit/parameters_matcher_test.rb +0 -119
  266. data/test/unit/receivers_test.rb +0 -96
  267. data/test/unit/return_values_test.rb +0 -61
  268. data/test/unit/sequence_test.rb +0 -100
  269. data/test/unit/single_return_value_test.rb +0 -12
  270. data/test/unit/single_yield_test.rb +0 -16
  271. data/test/unit/state_machine_test.rb +0 -96
  272. data/test/unit/string_inspect_test.rb +0 -9
  273. data/test/unit/thrower_test.rb +0 -18
  274. data/test/unit/yield_parameters_test.rb +0 -91
@@ -9,6 +9,8 @@ require 'mocha/is_a'
9
9
  require 'mocha/in_state_ordering_constraint'
10
10
  require 'mocha/change_state_side_effect'
11
11
  require 'mocha/cardinality'
12
+ require 'mocha/configuration'
13
+ require 'mocha/block_matcher'
12
14
 
13
15
  module Mocha
14
16
  # Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}.
@@ -40,7 +42,7 @@ module Mocha
40
42
  # object.expected_method
41
43
  # # => verify fails
42
44
  def times(range)
43
- @cardinality = Cardinality.times(range)
45
+ @cardinality.times(range)
44
46
  self
45
47
  end
46
48
 
@@ -66,7 +68,7 @@ module Mocha
66
68
  # object.expected_method
67
69
  # # => verify fails
68
70
  def twice
69
- @cardinality = Cardinality.exactly(2)
71
+ @cardinality.exactly(2)
70
72
  self
71
73
  end
72
74
 
@@ -91,7 +93,7 @@ module Mocha
91
93
  # object.expects(:expected_method).once
92
94
  # # => verify fails
93
95
  def once
94
- @cardinality = Cardinality.exactly(1)
96
+ @cardinality.exactly(1)
95
97
  self
96
98
  end
97
99
 
@@ -108,7 +110,7 @@ module Mocha
108
110
  # object.expects(:expected_method).never
109
111
  # # => verify succeeds
110
112
  def never
111
- @cardinality = Cardinality.exactly(0)
113
+ @cardinality.exactly(0)
112
114
  self
113
115
  end
114
116
 
@@ -128,7 +130,7 @@ module Mocha
128
130
  # object.expected_method
129
131
  # # => verify fails
130
132
  def at_least(minimum_number_of_times)
131
- @cardinality = Cardinality.at_least(minimum_number_of_times)
133
+ @cardinality.at_least(minimum_number_of_times)
132
134
  self
133
135
  end
134
136
 
@@ -147,7 +149,6 @@ module Mocha
147
149
  # # => verify fails
148
150
  def at_least_once
149
151
  at_least(1)
150
- self
151
152
  end
152
153
 
153
154
  # Modifies expectation so that the expected method must be called at most a +maximum_number_of_times+.
@@ -165,7 +166,7 @@ module Mocha
165
166
  # object.expects(:expected_method).at_most(2)
166
167
  # 3.times { object.expected_method } # => unexpected invocation
167
168
  def at_most(maximum_number_of_times)
168
- @cardinality = Cardinality.at_most(maximum_number_of_times)
169
+ @cardinality.at_most(maximum_number_of_times)
169
170
  self
170
171
  end
171
172
 
@@ -184,7 +185,6 @@ module Mocha
184
185
  # 2.times { object.expected_method } # => unexpected invocation
185
186
  def at_most_once
186
187
  at_most(1)
187
- self
188
188
  end
189
189
 
190
190
  # Modifies expectation so that the expected method must be called with +expected_parameters+.
@@ -223,7 +223,49 @@ module Mocha
223
223
  self
224
224
  end
225
225
 
226
- # Modifies expectation so that when the expected method is called, it yields with the specified +parameters+.
226
+ # Modifies expectation so that the expected method must be called with a block.
227
+ #
228
+ # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
229
+ #
230
+ # @example Expected method must be called with a block.
231
+ # object = mock()
232
+ # object.expects(:expected_method).with_block_given
233
+ # object.expected_method { 1 + 1 }
234
+ # # => verify succeeds
235
+ #
236
+ # object = mock()
237
+ # object.expects(:expected_method).with_block_given
238
+ # object.expected_method
239
+ # # => verify fails
240
+ def with_block_given
241
+ @block_matcher = BlockMatchers::BlockGiven.new
242
+ self
243
+ end
244
+
245
+ # Modifies expectation so that the expected method must be called without a block.
246
+ #
247
+ # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
248
+ #
249
+ # @example Expected method must be called without a block.
250
+ # object = mock()
251
+ # object.expects(:expected_method).with_no_block_given
252
+ # object.expected_method
253
+ # # => verify succeeds
254
+ #
255
+ # object = mock()
256
+ # object.expects(:expected_method).with_block_given
257
+ # object.expected_method { 1 + 1 }
258
+ # # => verify fails
259
+ def with_no_block_given
260
+ @block_matcher = BlockMatchers::NoBlockGiven.new
261
+ self
262
+ end
263
+
264
+ # Modifies expectation so that when the expected method is called, it yields to the block with the specified +parameters+.
265
+ #
266
+ # If no +parameters+ are specified, it yields to the block without any parameters.
267
+ #
268
+ # If no block is provided, the method will still attempt to yield resulting in a +LocalJumpError+. Note that this is what would happen if a "real" (non-mock) method implementation tried to yield to a non-existent block.
227
269
  #
228
270
  # May be called multiple times on the same expectation for consecutive invocations.
229
271
  #
@@ -231,51 +273,58 @@ module Mocha
231
273
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
232
274
  # @see #then
233
275
  #
276
+ # @example Yield when expected method is invoked.
277
+ # benchmark = mock()
278
+ # benchmark.expects(:measure).yields
279
+ # yielded = false
280
+ # benchmark.measure { yielded = true }
281
+ # yielded # => true
282
+ #
234
283
  # @example Yield parameters when expected method is invoked.
235
- # object = mock()
236
- # object.expects(:expected_method).yields('result')
237
- # yielded_value = nil
238
- # object.expected_method { |value| yielded_value = value }
239
- # yielded_value # => 'result'
284
+ # fibonacci = mock()
285
+ # fibonacci.expects(:next_pair).yields(0, 1)
286
+ # sum = 0
287
+ # fibonacci.next_pair { |first, second| sum = first + second }
288
+ # sum # => 1
240
289
  #
241
290
  # @example Yield different parameters on different invocations of the expected method.
242
- # object = mock()
243
- # object.stubs(:expected_method).yields(1).then.yields(2)
244
- # yielded_values_from_first_invocation = []
245
- # yielded_values_from_second_invocation = []
246
- # object.expected_method { |value| yielded_values_from_first_invocation << value } # first invocation
247
- # object.expected_method { |value| yielded_values_from_second_invocation << value } # second invocation
248
- # yielded_values_from_first_invocation # => [1]
249
- # yielded_values_from_second_invocation # => [2]
291
+ # fibonacci = mock()
292
+ # fibonacci.expects(:next_pair).yields(0, 1).then.yields(1, 1)
293
+ # sum = 0
294
+ # fibonacci.next_pair { |first, second| sum = first + second }
295
+ # sum # => 1
296
+ # fibonacci.next_pair { |first, second| sum = first + second }
297
+ # sum # => 2
250
298
  def yields(*parameters)
251
- @yield_parameters.add(*parameters)
252
- self
299
+ multiple_yields(parameters)
253
300
  end
254
301
 
255
302
  # Modifies expectation so that when the expected method is called, it yields multiple times per invocation with the specified +parameter_groups+.
256
303
  #
257
- # @param [*Array<Array>] parameter_groups each element of +parameter_groups+ should iself be an +Array+ representing the parameters to be passed to the block for a single yield.
304
+ # If no block is provided, the method will still attempt to yield resulting in a +LocalJumpError+. Note that this is what would happen if a "real" (non-mock) method implementation tried to yield to a non-existent block.
305
+ #
306
+ # @param [*Array<Array>] parameter_groups each element of +parameter_groups+ should iself be an +Array+ representing the parameters to be passed to the block for a single yield. Any element of +parameter_groups+ that is not an +Array+ is wrapped in an +Array+.
258
307
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
259
308
  # @see #then
260
309
  #
261
- # @example When the +expected_method+ is called, the stub will invoke the block twice, the first time it passes +'result_1'+, +'result_2'+ as the parameters, and the second time it passes 'result_3' as the parameters.
262
- # object = mock()
263
- # object.expects(:expected_method).multiple_yields(['result_1', 'result_2'], ['result_3'])
264
- # yielded_values = []
265
- # object.expected_method { |*values| yielded_values << values }
266
- # yielded_values # => [['result_1', 'result_2'], ['result_3]]
267
- #
268
- # @example Yield different groups of parameters on different invocations of the expected method.
269
- # object = mock()
270
- # object.stubs(:expected_method).multiple_yields([1, 2], [3]).then.multiple_yields([4], [5, 6])
271
- # yielded_values_from_first_invocation = []
272
- # yielded_values_from_second_invocation = []
273
- # object.expected_method { |*values| yielded_values_from_first_invocation << values } # first invocation
274
- # object.expected_method { |*values| yielded_values_from_second_invocation << values } # second invocation
275
- # yielded_values_from_first_invocation # => [[1, 2], [3]]
276
- # yielded_values_from_second_invocation # => [[4], [5, 6]]
310
+ # @example When +foreach+ is called, the stub will invoke the block twice, the first time it passes ['row1_col1', 'row1_col2'] as the parameters, and the second time it passes ['row2_col1', ''] as the parameters.
311
+ # csv = mock()
312
+ # csv.expects(:foreach).with("path/to/file.csv").multiple_yields(['row1_col1', 'row1_col2'], ['row2_col1', ''])
313
+ # rows = []
314
+ # csv.foreach { |row| rows << row }
315
+ # rows # => [['row1_col1', 'row1_col2'], ['row2_col1', '']]
316
+ #
317
+ # @example Yield different groups of parameters on different invocations of the expected method. Simulating a situation where the CSV file at 'path/to/file.csv' has been modified between the two calls to +foreach+.
318
+ # csv = mock()
319
+ # csv.stubs(:foreach).with("path/to/file.csv").multiple_yields(['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']).then.multiple_yields(['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2'])
320
+ # rows_from_first_invocation = []
321
+ # rows_from_second_invocation = []
322
+ # csv.foreach { |row| rows_from_first_invocation << row } # first invocation
323
+ # csv.foreach { |row| rows_from_second_invocation << row } # second invocation
324
+ # rows_from_first_invocation # => [['old_row1_col1', 'old_row1_col2'], ['old_row2_col1', '']]
325
+ # rows_from_second_invocation # => [['new_row1_col1', ''], ['new_row2_col1', 'new_row2_col2']]
277
326
  def multiple_yields(*parameter_groups)
278
- @yield_parameters.multiple_add(*parameter_groups)
327
+ @yield_parameters.add(*parameter_groups)
279
328
  self
280
329
  end
281
330
 
@@ -407,9 +456,9 @@ module Mocha
407
456
 
408
457
  # @overload def then
409
458
  # Used as syntactic sugar to improve readability. It has no effect on state of the expectation.
410
- # @overload def then(state_machine.is(state_name))
411
- # Used to change the +state_machine+ to the state specified by +state_name+ when the expected invocation occurs.
412
- # @param [StateMachine::State] state_machine.is(state_name) provides a mechanism to change the +state_machine+ into the state specified by +state_name+ when the expected method is invoked.
459
+ # @overload def then(state)
460
+ # Used to change the +state_machine+ to the specified state when the expected invocation occurs.
461
+ # @param [StateMachine::State] state state_machine.is(state_name) provides a mechanism to change the +state_machine+ into the state specified by +state_name+ when the expected method is invoked.
413
462
  #
414
463
  # @see API#states
415
464
  # @see StateMachine
@@ -435,17 +484,14 @@ module Mocha
435
484
  # radio.expects(:select_channel).with('BBC World Service').when(power.is('on'))
436
485
  # radio.expects(:adjust_volume).with(-5).when(power.is('on'))
437
486
  # radio.expects(:switch_off).then(power.is('off'))
438
- def then(*parameters)
439
- if parameters.length == 1
440
- state = parameters.first
441
- add_side_effect(ChangeStateSideEffect.new(state))
442
- end
487
+ def then(state = nil)
488
+ add_side_effect(ChangeStateSideEffect.new(state)) if state
443
489
  self
444
490
  end
445
491
 
446
- # Constrains the expectation to occur only when the +state_machine+ is in the state specified by +state_name+.
492
+ # Constrains the expectation to occur only when the +state_machine+ is in the state specified by +state_predicate+.
447
493
  #
448
- # @param [StateMachine::StatePredicate] state_machine.is(state_name) provides a mechanism to determine whether the +state_machine+ is in the state specified by +state_name+ when the expected method is invoked.
494
+ # @param [StateMachine::StatePredicate] state_predicate +state_machine.is(state_name)+ provides a mechanism to determine whether the +state_machine+ is in the state specified by +state_predicate+ when the expected method is invoked.
449
495
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
450
496
  #
451
497
  # @see API#states
@@ -477,7 +523,8 @@ module Mocha
477
523
  #
478
524
  # An expected method can appear in multiple sequences.
479
525
  #
480
- # @param [*Array<Sequence>] sequences sequences in which expected method should appear.
526
+ # @param [Sequence] sequence sequence in which expected method should appear.
527
+ # @param [*Array<Sequence>] sequences more sequences in which expected method should appear.
481
528
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
482
529
  #
483
530
  # @see API#sequence
@@ -489,8 +536,8 @@ module Mocha
489
536
  # egg.expects(:crack).in_sequence(breakfast)
490
537
  # egg.expects(:fry).in_sequence(breakfast)
491
538
  # egg.expects(:eat).in_sequence(breakfast)
492
- def in_sequence(*sequences)
493
- sequences.each { |sequence| add_in_sequence_ordering_constraint(sequence) }
539
+ def in_sequence(sequence, *sequences)
540
+ sequences.unshift(sequence).each { |seq| add_in_sequence_ordering_constraint(seq) }
494
541
  self
495
542
  end
496
543
 
@@ -502,10 +549,10 @@ module Mocha
502
549
  @mock = mock
503
550
  @method_matcher = MethodMatcher.new(expected_method_name.to_sym)
504
551
  @parameters_matcher = ParametersMatcher.new
552
+ @block_matcher = BlockMatchers::OptionalBlock.new
505
553
  @ordering_constraints = []
506
554
  @side_effects = []
507
- @cardinality = Cardinality.exactly(1)
508
- @invocation_count = 0
555
+ @cardinality = Cardinality.new.exactly(1)
509
556
  @return_values = ReturnValues.new
510
557
  @yield_parameters = YieldParameters.new
511
558
  @backtrace = backtrace || caller
@@ -542,41 +589,36 @@ module Mocha
542
589
  end
543
590
 
544
591
  # @private
545
- def match?(actual_method_name, *actual_parameters)
546
- @method_matcher.match?(actual_method_name) && @parameters_matcher.match?(actual_parameters) && in_correct_order?
592
+ def match?(invocation)
593
+ @method_matcher.match?(invocation.method_name) && @parameters_matcher.match?(invocation.arguments) && @block_matcher.match?(invocation.block) && in_correct_order?
547
594
  end
548
595
 
549
596
  # @private
550
597
  def invocations_allowed?
551
- @cardinality.invocations_allowed?(@invocation_count)
598
+ @cardinality.invocations_allowed?
552
599
  end
553
600
 
554
601
  # @private
555
602
  def satisfied?
556
- @cardinality.satisfied?(@invocation_count)
603
+ @cardinality.satisfied?
557
604
  end
558
605
 
559
606
  # @private
560
- def invoke
561
- @invocation_count += 1
607
+ def invoke(invocation)
562
608
  perform_side_effects
563
- if block_given?
564
- @yield_parameters.next_invocation.each do |yield_parameters|
565
- yield(*yield_parameters)
566
- end
567
- end
568
- @return_values.next
609
+ @cardinality << invocation
610
+ invocation.call(@yield_parameters, @return_values)
569
611
  end
570
612
 
571
613
  # @private
572
614
  def verified?(assertion_counter = nil)
573
615
  assertion_counter.increment if assertion_counter && @cardinality.needs_verifying?
574
- @cardinality.verified?(@invocation_count)
616
+ @cardinality.verified?
575
617
  end
576
618
 
577
619
  # @private
578
620
  def used?
579
- @cardinality.used?(@invocation_count)
621
+ @cardinality.used?
580
622
  end
581
623
 
582
624
  # @private
@@ -588,22 +630,19 @@ module Mocha
588
630
 
589
631
  # @private
590
632
  def mocha_inspect
591
- message = "#{@cardinality.mocha_inspect}, "
592
- message << case @invocation_count
593
- when 0 then 'not yet invoked'
594
- when 1 then 'invoked once'
595
- when 2 then 'invoked twice'
596
- else "invoked #{@invocation_count} times"
597
- end
598
- message << ': '
599
- message << method_signature
633
+ message = "#{@cardinality.anticipated_times}, #{@cardinality.invoked_times}: #{method_signature}"
600
634
  message << "; #{@ordering_constraints.map(&:mocha_inspect).join('; ')}" unless @ordering_constraints.empty?
635
+ if Mocha.configuration.display_matching_invocations_on_failure?
636
+ message << @cardinality.actual_invocations
637
+ end
601
638
  message
602
639
  end
603
640
 
604
641
  # @private
605
642
  def method_signature
606
- "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
643
+ signature = "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
644
+ signature << " #{@block_matcher.mocha_inspect}" if @block_matcher.mocha_inspect
645
+ signature
607
646
  end
608
647
  end
609
648
  end
@@ -4,7 +4,5 @@ module Mocha
4
4
  # Authors of test libraries may use +Mocha::ExpectationErrorFactory+ to have Mocha raise a different exception.
5
5
  #
6
6
  # @see Mocha::ExpectationErrorFactory
7
- # rubocop:disable Lint/InheritException
8
- class ExpectationError < Exception; end
9
- # rubocop:enable Lint/InheritException
7
+ class ExpectationError < Exception; end # rubocop:disable Lint/InheritException
10
8
  end
@@ -17,12 +17,12 @@ module Mocha
17
17
  @expectations.any? { |expectation| expectation.matches_method?(method_name) }
18
18
  end
19
19
 
20
- def match(method_name, *arguments)
21
- matching_expectations(method_name, *arguments).first
20
+ def match(invocation)
21
+ matching_expectations(invocation).first
22
22
  end
23
23
 
24
- def match_allowing_invocation(method_name, *arguments)
25
- matching_expectations(method_name, *arguments).detect(&:invocations_allowed?)
24
+ def match_allowing_invocation(invocation)
25
+ matching_expectations(invocation).detect(&:invocations_allowed?)
26
26
  end
27
27
 
28
28
  def verified?(assertion_counter = nil)
@@ -51,8 +51,8 @@ module Mocha
51
51
 
52
52
  private
53
53
 
54
- def matching_expectations(method_name, *arguments)
55
- @expectations.select { |e| e.match?(method_name, *arguments) }
54
+ def matching_expectations(invocation)
55
+ @expectations.select { |e| e.match?(invocation) }
56
56
  end
57
57
  end
58
58
  end
data/lib/mocha/inspect.rb CHANGED
@@ -1,55 +1,59 @@
1
1
  require 'date'
2
2
 
3
3
  module Mocha
4
- module ObjectMethods
5
- def mocha_inspect
6
- address = __id__ * 2
7
- address += 0x100000000 if address < 0
8
- inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%x', address)}>" : inspect
4
+ module Inspect
5
+ module ObjectMethods
6
+ def mocha_inspect
7
+ address = __id__ * 2
8
+ address += 0x100000000 if address < 0
9
+ inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%x', address)}>" : inspect
10
+ end
9
11
  end
10
- end
11
12
 
12
- module ArrayMethods
13
- def mocha_inspect
14
- "[#{collect(&:mocha_inspect).join(', ')}]"
13
+ module ArrayMethods
14
+ def mocha_inspect(wrapped = true)
15
+ unwrapped = collect(&:mocha_inspect).join(', ')
16
+ wrapped ? "[#{unwrapped}]" : unwrapped
17
+ end
15
18
  end
16
- end
17
19
 
18
- module HashMethods
19
- def mocha_inspect
20
- "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
20
+ module HashMethods
21
+ def mocha_inspect(wrapped = true)
22
+ unwrapped = collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')
23
+ wrapped ? "{#{unwrapped}}" : unwrapped
24
+ end
21
25
  end
22
- end
23
26
 
24
- module TimeMethods
25
- def mocha_inspect
26
- "#{inspect} (#{to_f} secs)"
27
+ module TimeMethods
28
+ def mocha_inspect
29
+ "#{inspect} (#{to_f} secs)"
30
+ end
27
31
  end
28
- end
29
32
 
30
- module DateMethods
31
- def mocha_inspect
32
- to_s
33
+ module DateMethods
34
+ def mocha_inspect
35
+ to_s
36
+ end
33
37
  end
34
38
  end
35
39
  end
36
40
 
37
41
  class Object
38
- include Mocha::ObjectMethods
42
+ include Mocha::Inspect::ObjectMethods
39
43
  end
40
44
 
41
45
  class Array
42
- include Mocha::ArrayMethods
46
+ include Mocha::Inspect::ArrayMethods
43
47
  end
44
48
 
45
49
  class Hash
46
- include Mocha::HashMethods
50
+ include Mocha::Inspect::HashMethods
47
51
  end
48
52
 
49
53
  class Time
50
- include Mocha::TimeMethods
54
+ include Mocha::Inspect::TimeMethods
51
55
  end
52
56
 
53
57
  class Date
54
- include Mocha::DateMethods
58
+ include Mocha::Inspect::DateMethods
55
59
  end
@@ -1,6 +1,23 @@
1
- require 'mocha/class_method'
1
+ require 'mocha/stubbed_method'
2
2
 
3
3
  module Mocha
4
- class InstanceMethod < ClassMethod
4
+ class InstanceMethod < StubbedMethod
5
+ private
6
+
7
+ def mock_owner
8
+ stubbee
9
+ end
10
+
11
+ def method_body(method)
12
+ PRE_RUBY_V19 ? proc { |*args, &block| method.call(*args, &block) } : method
13
+ end
14
+
15
+ def stubbee_method(method_name)
16
+ stubbee._method(method_name)
17
+ end
18
+
19
+ def original_method_owner
20
+ stubbee.singleton_class
21
+ end
5
22
  end
6
23
  end
@@ -14,6 +14,8 @@ require 'mocha/integration/mini_test/version_2110_to_2111'
14
14
  require 'mocha/integration/mini_test/version_2112_to_320'
15
15
  require 'mocha/integration/mini_test/adapter'
16
16
 
17
+ require 'mocha/deprecation'
18
+
17
19
  module Mocha
18
20
  module Integration
19
21
  module MiniTest
@@ -39,6 +41,11 @@ module Mocha
39
41
 
40
42
  target = Detection::MiniTest.testcase
41
43
  unless target < integration_module
44
+ unless integration_module == MiniTest::Adapter
45
+ Deprecation.warning(
46
+ 'Versions of minitest earlier than v3.3.0 will not be supported in future versions of Mocha.'
47
+ )
48
+ end
42
49
  Debug.puts "Applying #{integration_module.description}"
43
50
  target.send(:include, integration_module)
44
51
  end
@@ -11,6 +11,8 @@ require 'mocha/integration/test_unit/gem_version_203_to_220'
11
11
  require 'mocha/integration/test_unit/gem_version_230_to_250'
12
12
  require 'mocha/integration/test_unit/adapter'
13
13
 
14
+ require 'mocha/deprecation'
15
+
14
16
  module Mocha
15
17
  module Integration
16
18
  module TestUnit
@@ -34,6 +36,11 @@ module Mocha
34
36
  ].detect { |m| m.applicable_to?(test_unit_version, ruby_version) }
35
37
 
36
38
  unless ::Test::Unit::TestCase < integration_module
39
+ unless integration_module == TestUnit::Adapter
40
+ Deprecation.warning(
41
+ 'Versions of test-unit earlier than v2.5.1 will not be supported in future versions of Mocha.'
42
+ )
43
+ end
37
44
  Debug.puts "Applying #{integration_module.description}"
38
45
  ::Test::Unit::TestCase.send(:include, integration_module)
39
46
  end
@@ -1,4 +1,3 @@
1
- require 'mocha/deprecation'
2
1
  require 'mocha/integration/test_unit'
3
2
  require 'mocha/integration/mini_test'
4
3
 
@@ -6,8 +5,7 @@ module Mocha
6
5
  module Integration
7
6
  def self.activate
8
7
  return unless [Integration::TestUnit, Integration::MiniTest].map(&:activate).none?
9
- Deprecation.warning("Test::Unit or MiniTest must be loaded *before* `require 'mocha/setup'`.")
10
- Deprecation.warning("If you're integrating with a test library other than Test::Unit or MiniTest, you should use `require 'mocha/api'` instead of `require 'mocha/setup'`.")
8
+ raise "Test::Unit or Minitest must be loaded *before* `require 'mocha/setup'`."
11
9
  end
12
10
  end
13
11
  end
@@ -0,0 +1,77 @@
1
+ require 'mocha/parameters_matcher'
2
+ require 'mocha/raised_exception'
3
+ require 'mocha/return_values'
4
+ require 'mocha/thrown_object'
5
+ require 'mocha/yield_parameters'
6
+ require 'mocha/configuration'
7
+ require 'mocha/deprecation'
8
+
9
+ module Mocha
10
+ class Invocation
11
+ attr_reader :method_name, :block
12
+
13
+ def initialize(mock, method_name, *arguments, &block)
14
+ @mock = mock
15
+ @method_name = method_name
16
+ @arguments = arguments
17
+ @block = block
18
+ @yields = []
19
+ @result = nil
20
+ end
21
+
22
+ def call(yield_parameters = YieldParameters.new, return_values = ReturnValues.new)
23
+ yield_parameters.next_invocation.each do |yield_args|
24
+ @yields << ParametersMatcher.new(yield_args)
25
+ if @block
26
+ @block.call(*yield_args)
27
+ else
28
+ raise LocalJumpError unless Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
29
+ yield_args_description = ParametersMatcher.new(yield_args).mocha_inspect
30
+ Deprecation.warning(
31
+ "Stubbed method was instructed to yield #{yield_args_description}, but no block was given by invocation: #{call_description}.",
32
+ ' This will raise a LocalJumpError in the future.',
33
+ ' Use Expectation#with_block_given to constrain this expectation to match invocations supplying a block.',
34
+ ' And, if necessary, add another expectation to match invocations not supplying a block.'
35
+ )
36
+ end
37
+ end
38
+ return_values.next(self)
39
+ end
40
+
41
+ def returned(value)
42
+ @result = value
43
+ end
44
+
45
+ def raised(exception)
46
+ @result = RaisedException.new(exception)
47
+ end
48
+
49
+ def threw(tag, value)
50
+ @result = ThrownObject.new(tag, value)
51
+ end
52
+
53
+ def arguments
54
+ @arguments.dup
55
+ end
56
+
57
+ def call_description
58
+ description = "#{@mock.mocha_inspect}.#{@method_name}#{ParametersMatcher.new(@arguments).mocha_inspect}"
59
+ description << ' { ... }' unless @block.nil?
60
+ description
61
+ end
62
+
63
+ def short_call_description
64
+ "#{@method_name}(#{@arguments.join(', ')})"
65
+ end
66
+
67
+ def result_description
68
+ desc = "# => #{@result.mocha_inspect}"
69
+ desc << " after yielding #{@yields.map(&:mocha_inspect).join(', then ')}" if @yields.any?
70
+ desc
71
+ end
72
+
73
+ def full_description
74
+ "\n - #{call_description} #{result_description}"
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,5 @@
1
+ module Mocha
2
+ MACOS = /darwin/.match(RUBY_PLATFORM)
3
+ MACOS_VERSION = MACOS && /darwin(\d+)/.match(RUBY_PLATFORM)[1].to_i
4
+ MACOS_MOJAVE_VERSION = 18
5
+ end