mocha 0.10.5 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (262) hide show
  1. checksums.yaml +7 -0
  2. data/.github/FUNDING.yml +1 -0
  3. data/.rubocop.yml +61 -0
  4. data/.rubocop_todo.yml +27 -0
  5. data/.yardopts +24 -0
  6. data/CONTRIBUTING.md +7 -0
  7. data/COPYING.md +3 -0
  8. data/Gemfile +2 -2
  9. data/{MIT-LICENSE.rdoc → MIT-LICENSE.md} +1 -1
  10. data/README.md +363 -0
  11. data/{RELEASE.rdoc → RELEASE.md} +436 -35
  12. data/Rakefile +87 -87
  13. data/gemfiles/Gemfile.minitest.latest +2 -2
  14. data/gemfiles/Gemfile.test-unit.latest +6 -2
  15. data/init.rb +1 -3
  16. data/lib/mocha/any_instance_method.rb +12 -45
  17. data/lib/mocha/api.rb +199 -131
  18. data/lib/mocha/argument_iterator.rb +6 -10
  19. data/lib/mocha/backtrace_filter.rb +1 -5
  20. data/lib/mocha/block_matcher.rb +31 -0
  21. data/lib/mocha/cardinality.rb +77 -66
  22. data/lib/mocha/central.rb +27 -18
  23. data/lib/mocha/change_state_side_effect.rb +3 -7
  24. data/lib/mocha/class_methods.rb +62 -0
  25. data/lib/mocha/configuration.rb +399 -46
  26. data/lib/mocha/debug.rb +12 -0
  27. data/lib/mocha/deprecation.rb +11 -12
  28. data/lib/mocha/detection/mini_test.rb +23 -0
  29. data/lib/mocha/detection/test_unit.rb +27 -0
  30. data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
  31. data/lib/mocha/exception_raiser.rb +8 -10
  32. data/lib/mocha/expectation.rb +290 -151
  33. data/lib/mocha/expectation_error.rb +6 -13
  34. data/lib/mocha/expectation_error_factory.rb +35 -0
  35. data/lib/mocha/expectation_list.rb +22 -22
  36. data/lib/mocha/hooks.rb +42 -0
  37. data/lib/mocha/in_state_ordering_constraint.rb +3 -7
  38. data/lib/mocha/inspect.rb +35 -43
  39. data/lib/mocha/instance_method.rb +12 -21
  40. data/lib/mocha/integration/assertion_counter.rb +13 -0
  41. data/lib/mocha/integration/mini_test/adapter.rb +52 -0
  42. data/lib/mocha/integration/mini_test/exception_translation.rb +1 -7
  43. data/lib/mocha/integration/mini_test/nothing.rb +19 -0
  44. data/lib/mocha/integration/mini_test/version_13.rb +35 -25
  45. data/lib/mocha/integration/mini_test/version_140.rb +35 -26
  46. data/lib/mocha/integration/mini_test/version_141.rb +43 -34
  47. data/lib/mocha/integration/mini_test/version_142_to_172.rb +44 -35
  48. data/lib/mocha/integration/mini_test/version_200.rb +45 -36
  49. data/lib/mocha/integration/mini_test/version_201_to_222.rb +44 -35
  50. data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +70 -0
  51. data/lib/mocha/integration/mini_test/version_2112_to_320.rb +73 -0
  52. data/lib/mocha/integration/mini_test/version_230_to_2101.rb +68 -0
  53. data/lib/mocha/integration/mini_test.rb +51 -49
  54. data/lib/mocha/integration/monkey_patcher.rb +24 -0
  55. data/lib/mocha/integration/test_unit/adapter.rb +50 -0
  56. data/lib/mocha/integration/test_unit/gem_version_200.rb +39 -29
  57. data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +39 -29
  58. data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +39 -29
  59. data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +68 -0
  60. data/lib/mocha/integration/test_unit/nothing.rb +19 -0
  61. data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +39 -29
  62. data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +40 -30
  63. data/lib/mocha/integration/test_unit.rb +45 -51
  64. data/lib/mocha/integration.rb +6 -33
  65. data/lib/mocha/invocation.rb +77 -0
  66. data/lib/mocha/is_a.rb +0 -2
  67. data/lib/mocha/logger.rb +2 -6
  68. data/lib/mocha/macos_version.rb +5 -0
  69. data/lib/mocha/method_matcher.rb +6 -10
  70. data/lib/mocha/minitest.rb +8 -0
  71. data/lib/mocha/mock.rb +266 -79
  72. data/lib/mocha/mockery.rb +104 -106
  73. data/lib/mocha/names.rb +10 -20
  74. data/lib/mocha/not_initialized_error.rb +7 -0
  75. data/lib/mocha/object_methods.rb +169 -0
  76. data/lib/mocha/parameter_matchers/all_of.rb +18 -14
  77. data/lib/mocha/parameter_matchers/any_of.rb +19 -14
  78. data/lib/mocha/parameter_matchers/any_parameters.rb +14 -13
  79. data/lib/mocha/parameter_matchers/anything.rb +17 -14
  80. data/lib/mocha/parameter_matchers/base.rb +33 -31
  81. data/lib/mocha/parameter_matchers/equals.rb +18 -13
  82. data/lib/mocha/parameter_matchers/equivalent_uri.rb +58 -0
  83. data/lib/mocha/parameter_matchers/has_entries.rb +19 -14
  84. data/lib/mocha/parameter_matchers/has_entry.rb +58 -26
  85. data/lib/mocha/parameter_matchers/has_key.rb +18 -13
  86. data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
  87. data/lib/mocha/parameter_matchers/has_value.rb +18 -13
  88. data/lib/mocha/parameter_matchers/includes.rb +80 -19
  89. data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
  90. data/lib/mocha/parameter_matchers/instance_of.rb +18 -13
  91. data/lib/mocha/parameter_matchers/is_a.rb +20 -14
  92. data/lib/mocha/parameter_matchers/kind_of.rb +20 -13
  93. data/lib/mocha/parameter_matchers/not.rb +19 -14
  94. data/lib/mocha/parameter_matchers/optionally.rb +23 -17
  95. data/lib/mocha/parameter_matchers/regexp_matches.rb +16 -12
  96. data/lib/mocha/parameter_matchers/responds_with.rb +17 -11
  97. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +15 -9
  98. data/lib/mocha/parameter_matchers.rb +4 -5
  99. data/lib/mocha/parameters_matcher.rb +11 -14
  100. data/lib/mocha/raised_exception.rb +11 -0
  101. data/lib/mocha/receivers.rb +45 -0
  102. data/lib/mocha/return_values.rb +11 -15
  103. data/lib/mocha/ruby_version.rb +4 -0
  104. data/lib/mocha/sequence.rb +21 -17
  105. data/lib/mocha/setup.rb +14 -0
  106. data/lib/mocha/single_return_value.rb +5 -8
  107. data/lib/mocha/singleton_class.rb +9 -0
  108. data/lib/mocha/state_machine.rb +69 -67
  109. data/lib/mocha/stubbed_method.rb +125 -0
  110. data/lib/mocha/stubbing_error.rb +6 -14
  111. data/lib/mocha/test_unit.rb +8 -0
  112. data/lib/mocha/thrower.rb +6 -8
  113. data/lib/mocha/thrown_object.rb +12 -0
  114. data/lib/mocha/version.rb +1 -1
  115. data/lib/mocha/yield_parameters.rb +12 -22
  116. data/lib/mocha.rb +8 -3
  117. data/mocha.gemspec +43 -34
  118. data/yard-templates/default/layout/html/google_analytics.erb +8 -0
  119. data/yard-templates/default/layout/html/setup.rb +5 -0
  120. metadata +123 -268
  121. data/COPYING.rdoc +0 -3
  122. data/README.rdoc +0 -54
  123. data/examples/misc.rb +0 -43
  124. data/examples/mocha.rb +0 -25
  125. data/examples/stubba.rb +0 -64
  126. data/gemfiles/Gemfile.minitest.1.3.0 +0 -7
  127. data/gemfiles/Gemfile.minitest.1.4.0 +0 -7
  128. data/gemfiles/Gemfile.minitest.1.4.1 +0 -7
  129. data/gemfiles/Gemfile.minitest.1.4.2 +0 -7
  130. data/gemfiles/Gemfile.minitest.2.0.0 +0 -7
  131. data/gemfiles/Gemfile.minitest.2.0.1 +0 -7
  132. data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
  133. data/gemfiles/Gemfile.test-unit.2.0.0 +0 -8
  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 -98
  137. data/lib/mocha/integration/mini_test/assertion_counter.rb +0 -23
  138. data/lib/mocha/integration/mini_test/version_230_to_262.rb +0 -59
  139. data/lib/mocha/integration/test_unit/assertion_counter.rb +0 -23
  140. data/lib/mocha/integration/test_unit/gem_version_230_to_240.rb +0 -58
  141. data/lib/mocha/module_method.rb +0 -16
  142. data/lib/mocha/multiple_yields.rb +0 -20
  143. data/lib/mocha/no_yields.rb +0 -11
  144. data/lib/mocha/object.rb +0 -223
  145. data/lib/mocha/options.rb +0 -1
  146. data/lib/mocha/parameter_matchers/object.rb +0 -15
  147. data/lib/mocha/parameter_matchers/query_string.rb +0 -47
  148. data/lib/mocha/pretty_parameters.rb +0 -28
  149. data/lib/mocha/single_yield.rb +0 -18
  150. data/lib/mocha/standalone.rb +0 -1
  151. data/lib/mocha/unexpected_invocation.rb +0 -18
  152. data/lib/mocha_standalone.rb +0 -2
  153. data/lib/stubba.rb +0 -4
  154. data/test/acceptance/acceptance_test_helper.rb +0 -41
  155. data/test/acceptance/api_test.rb +0 -139
  156. data/test/acceptance/bug_18914_test.rb +0 -43
  157. data/test/acceptance/bug_21465_test.rb +0 -34
  158. data/test/acceptance/bug_21563_test.rb +0 -25
  159. data/test/acceptance/exception_rescue_test.rb +0 -55
  160. data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -55
  161. data/test/acceptance/expected_invocation_count_test.rb +0 -232
  162. data/test/acceptance/failure_messages_test.rb +0 -64
  163. data/test/acceptance/issue_65_test.rb +0 -63
  164. data/test/acceptance/issue_70_test.rb +0 -55
  165. data/test/acceptance/minitest_test.rb +0 -162
  166. data/test/acceptance/mocha_example_test.rb +0 -98
  167. data/test/acceptance/mocha_test_result_test.rb +0 -84
  168. data/test/acceptance/mock_test.rb +0 -100
  169. data/test/acceptance/mock_with_initializer_block_test.rb +0 -51
  170. data/test/acceptance/mocked_methods_dispatch_test.rb +0 -78
  171. data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -68
  172. data/test/acceptance/optional_parameters_test.rb +0 -70
  173. data/test/acceptance/parameter_matcher_test.rb +0 -300
  174. data/test/acceptance/partial_mocks_test.rb +0 -47
  175. data/test/acceptance/raise_exception_test.rb +0 -39
  176. data/test/acceptance/return_value_test.rb +0 -52
  177. data/test/acceptance/sequence_test.rb +0 -192
  178. data/test/acceptance/states_test.rb +0 -70
  179. data/test/acceptance/stub_any_instance_method_test.rb +0 -198
  180. data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -106
  181. data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -72
  182. data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -75
  183. data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -75
  184. data/test/acceptance/stub_everything_test.rb +0 -56
  185. data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -93
  186. data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -69
  187. data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -66
  188. data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -75
  189. data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -75
  190. data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -75
  191. data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -70
  192. data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -72
  193. data/test/acceptance/stub_module_method_test.rb +0 -163
  194. data/test/acceptance/stub_test.rb +0 -52
  195. data/test/acceptance/stubba_example_test.rb +0 -102
  196. data/test/acceptance/stubba_test.rb +0 -15
  197. data/test/acceptance/stubba_test_result_test.rb +0 -66
  198. data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
  199. data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -65
  200. data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -130
  201. data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
  202. data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -147
  203. data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -130
  204. data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
  205. data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -143
  206. data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -64
  207. data/test/acceptance/throw_test.rb +0 -45
  208. data/test/acceptance/unstubbing_test.rb +0 -151
  209. data/test/deprecation_disabler.rb +0 -15
  210. data/test/execution_point.rb +0 -36
  211. data/test/method_definer.rb +0 -24
  212. data/test/mini_test_result.rb +0 -83
  213. data/test/simple_counter.rb +0 -13
  214. data/test/test_helper.rb +0 -11
  215. data/test/test_runner.rb +0 -50
  216. data/test/unit/any_instance_method_test.rb +0 -136
  217. data/test/unit/array_inspect_test.rb +0 -16
  218. data/test/unit/backtrace_filter_test.rb +0 -19
  219. data/test/unit/cardinality_test.rb +0 -56
  220. data/test/unit/central_test.rb +0 -100
  221. data/test/unit/change_state_side_effect_test.rb +0 -41
  222. data/test/unit/class_method_test.rb +0 -260
  223. data/test/unit/configuration_test.rb +0 -38
  224. data/test/unit/date_time_inspect_test.rb +0 -21
  225. data/test/unit/exception_raiser_test.rb +0 -42
  226. data/test/unit/expectation_list_test.rb +0 -71
  227. data/test/unit/expectation_test.rb +0 -480
  228. data/test/unit/hash_inspect_test.rb +0 -16
  229. data/test/unit/in_state_ordering_constraint_test.rb +0 -43
  230. data/test/unit/method_matcher_test.rb +0 -23
  231. data/test/unit/mock_test.rb +0 -312
  232. data/test/unit/mockery_test.rb +0 -150
  233. data/test/unit/multiple_yields_test.rb +0 -18
  234. data/test/unit/no_yields_test.rb +0 -18
  235. data/test/unit/object_inspect_test.rb +0 -38
  236. data/test/unit/object_test.rb +0 -87
  237. data/test/unit/parameter_matchers/all_of_test.rb +0 -26
  238. data/test/unit/parameter_matchers/any_of_test.rb +0 -26
  239. data/test/unit/parameter_matchers/anything_test.rb +0 -21
  240. data/test/unit/parameter_matchers/equals_test.rb +0 -25
  241. data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
  242. data/test/unit/parameter_matchers/has_entry_test.rb +0 -96
  243. data/test/unit/parameter_matchers/has_key_test.rb +0 -55
  244. data/test/unit/parameter_matchers/has_value_test.rb +0 -57
  245. data/test/unit/parameter_matchers/includes_test.rb +0 -44
  246. data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
  247. data/test/unit/parameter_matchers/is_a_test.rb +0 -25
  248. data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
  249. data/test/unit/parameter_matchers/not_test.rb +0 -26
  250. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -46
  251. data/test/unit/parameter_matchers/responds_with_test.rb +0 -25
  252. data/test/unit/parameter_matchers/stub_matcher.rb +0 -27
  253. data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -25
  254. data/test/unit/parameters_matcher_test.rb +0 -121
  255. data/test/unit/return_values_test.rb +0 -63
  256. data/test/unit/sequence_test.rb +0 -104
  257. data/test/unit/single_return_value_test.rb +0 -14
  258. data/test/unit/single_yield_test.rb +0 -18
  259. data/test/unit/state_machine_test.rb +0 -98
  260. data/test/unit/string_inspect_test.rb +0 -11
  261. data/test/unit/thrower_test.rb +0 -20
  262. data/test/unit/yield_parameters_test.rb +0 -93
data/lib/mocha/central.rb CHANGED
@@ -1,33 +1,42 @@
1
1
  module Mocha
2
-
3
2
  class Central
4
-
3
+ class Null < self
4
+ def initialize(&block)
5
+ super
6
+ @raise_not_initialized_error = block
7
+ end
8
+
9
+ def stub(*)
10
+ @raise_not_initialized_error.call
11
+ end
12
+
13
+ def unstub(*)
14
+ @raise_not_initialized_error.call
15
+ end
16
+ end
17
+
5
18
  attr_accessor :stubba_methods
6
-
19
+
7
20
  def initialize
8
21
  self.stubba_methods = []
9
22
  end
10
-
23
+
11
24
  def stub(method)
12
- unless stubba_methods.detect { |m| m.matches?(method) }
13
- method.stub
14
- stubba_methods.push(method)
15
- end
25
+ return if stubba_methods.detect { |m| m.matches?(method) }
26
+ method.stub
27
+ stubba_methods.push(method)
16
28
  end
17
-
29
+
18
30
  def unstub(method)
19
- if existing = stubba_methods.detect { |m| m.matches?(method) }
20
- existing.unstub
21
- stubba_methods.delete(existing)
22
- end
31
+ return unless (existing = stubba_methods.detect { |m| m.matches?(method) })
32
+ existing.unstub
33
+ stubba_methods.delete(existing)
23
34
  end
24
-
35
+
25
36
  def unstub_all
26
- while stubba_methods.any? do
37
+ while stubba_methods.any?
27
38
  unstub(stubba_methods.first)
28
39
  end
29
40
  end
30
-
31
41
  end
32
-
33
- end
42
+ end
@@ -1,19 +1,15 @@
1
1
  module Mocha
2
-
3
2
  class ChangeStateSideEffect
4
-
5
3
  def initialize(state)
6
4
  @state = state
7
5
  end
8
-
6
+
9
7
  def perform
10
8
  @state.activate
11
9
  end
12
-
10
+
13
11
  def mocha_inspect
14
12
  "then #{@state.mocha_inspect}"
15
13
  end
16
-
17
14
  end
18
-
19
- end
15
+ end
@@ -0,0 +1,62 @@
1
+ require 'mocha/mockery'
2
+ require 'mocha/any_instance_method'
3
+
4
+ module Mocha
5
+ # Methods added to all classes to allow mocking and stubbing on real (i.e. non-mock) objects.
6
+ module ClassMethods
7
+ # @private
8
+ class AnyInstance
9
+ def initialize(klass)
10
+ @stubba_object = klass
11
+ end
12
+
13
+ def mocha(instantiate = true)
14
+ if instantiate
15
+ @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
16
+ else
17
+ defined?(@mocha) ? @mocha : nil
18
+ end
19
+ end
20
+
21
+ def stubba_method
22
+ Mocha::AnyInstanceMethod
23
+ end
24
+
25
+ def stubba_class
26
+ @stubba_object
27
+ end
28
+
29
+ def respond_to?(method)
30
+ @stubba_object.allocate.respond_to?(method.to_sym)
31
+ end
32
+
33
+ attr_reader :stubba_object
34
+ end
35
+
36
+ # @return [Mock] a mock object which will detect calls to any instance of this class.
37
+ # @raise [StubbingError] if attempting to stub method which is not allowed.
38
+ #
39
+ # @example Return false to invocation of +Product#save+ for any instance of +Product+.
40
+ # Product.any_instance.stubs(:save).returns(false)
41
+ # product_1 = Product.new
42
+ # assert_equal false, product_1.save
43
+ # product_2 = Product.new
44
+ # assert_equal false, product_2.save
45
+ def any_instance
46
+ if frozen?
47
+ raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}.any_instance", caller)
48
+ end
49
+ @any_instance ||= AnyInstance.new(self)
50
+ end
51
+
52
+ # @private
53
+ # rubocop:disable Metrics/CyclomaticComplexity
54
+ def __method_visibility__(method, include_public_methods = true)
55
+ (include_public_methods && public_method_defined?(method) && :public) ||
56
+ (protected_method_defined?(method) && :protected) ||
57
+ (private_method_defined?(method) && :private)
58
+ end
59
+ # rubocop:enable Metrics/CyclomaticComplexity
60
+ alias_method :__method_exists__?, :__method_visibility__
61
+ end
62
+ end
@@ -1,79 +1,432 @@
1
- module Mocha # :nodoc:
2
-
3
- # Configuration settings
1
+ module Mocha
2
+ # Allows setting of configuration options. See {Configuration} for the available options.
3
+ #
4
+ # Typically the configuration is set globally in a +test_helper.rb+ or +spec_helper.rb+ file.
5
+ #
6
+ # @see Configuration
7
+ #
8
+ # @yieldparam configuration [Configuration] the configuration for modification
9
+ #
10
+ # @example Setting multiple configuration options
11
+ # Mocha.configure do |c|
12
+ # c.stubbing_method_unnecessarily = :prevent
13
+ # c.stubbing_method_on_non_mock_object = :warn
14
+ # c.stubbing_method_on_nil = :allow
15
+ # end
16
+ #
17
+ def self.configure
18
+ yield configuration
19
+ end
20
+
21
+ # @private
22
+ def self.configuration
23
+ Configuration.configuration
24
+ end
25
+
26
+ # This class provides a number of ways to configure the library.
27
+ #
28
+ # Typically the configuration is set globally in a +test_helper.rb+ or +spec_helper.rb+ file.
29
+ #
30
+ # @example Setting multiple configuration options
31
+ # Mocha.configure do |c|
32
+ # c.stubbing_method_unnecessarily = :prevent
33
+ # c.stubbing_method_on_non_mock_object = :warn
34
+ # c.stubbing_method_on_nil = :allow
35
+ # end
36
+ #
4
37
  class Configuration
5
-
6
- DEFAULTS = { :stubbing_method_unnecessarily => :allow, :stubbing_method_on_non_mock_object => :allow, :stubbing_non_existent_method => :allow, :stubbing_non_public_method => :allow }
7
-
38
+ # @private
39
+ DEFAULTS = {
40
+ :stubbing_method_unnecessarily => :allow,
41
+ :stubbing_method_on_non_mock_object => :allow,
42
+ :stubbing_non_existent_method => :allow,
43
+ :stubbing_non_public_method => :allow,
44
+ :stubbing_method_on_nil => :prevent,
45
+ :display_matching_invocations_on_failure => false,
46
+ :reinstate_undocumented_behaviour_from_v1_9 => true
47
+ }.freeze
48
+
49
+ attr_reader :options
50
+ protected :options
51
+
52
+ # @private
53
+ def initialize(options = {})
54
+ @options = DEFAULTS.merge(options)
55
+ end
56
+
57
+ # @private
58
+ def initialize_copy(other)
59
+ @options = other.options.dup
60
+ end
61
+
62
+ # @private
63
+ def merge(other)
64
+ self.class.new(@options.merge(other.options))
65
+ end
66
+
67
+ # Configure whether stubbing methods unnecessarily is allowed.
68
+ #
69
+ # This is useful for identifying unused stubs. Unused stubs are often accidentally introduced when code is {http://martinfowler.com/bliki/DefinitionOfRefactoring.html refactored}.
70
+ #
71
+ # When +value+ is +:allow+, do nothing. This is the default.
72
+ # When +value+ is +:warn+, display a warning.
73
+ # When +value+ is +:prevent+, raise a {StubbingError}.
74
+ #
75
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
76
+ #
77
+ # @example Preventing unnecessary stubbing of a method
78
+ # Mocha.configure do |c|
79
+ # c.stubbing_method_unnecessarily = :prevent
80
+ # end
81
+ #
82
+ # example = mock('example')
83
+ # example.stubs(:unused_stub)
84
+ # # => Mocha::StubbingError: stubbing method unnecessarily:
85
+ # # => #<Mock:example>.unused_stub(any_parameters)
86
+ #
87
+ def stubbing_method_unnecessarily=(value)
88
+ @options[:stubbing_method_unnecessarily] = value
89
+ end
90
+
91
+ # @private
92
+ def stubbing_method_unnecessarily
93
+ @options[:stubbing_method_unnecessarily]
94
+ end
95
+
96
+ # Configure whether stubbing methods on non-mock objects is allowed.
97
+ #
98
+ # If you like the idea of {http://www.jmock.org/oopsla2004.pdf mocking roles not objects} and {http://www.mockobjects.com/2007/04/test-smell-mocking-concrete-classes.html you don't like stubbing concrete classes}, this is the setting for you. However, while this restriction makes a lot of sense in Java with its {http://java.sun.com/docs/books/tutorial/java/concepts/interface.html explicit interfaces}, it may be moot in Ruby where roles are probably best represented as Modules.
99
+ #
100
+ # When +value+ is +:allow+, do nothing. This is the default.
101
+ # When +value+ is +:warn+, display a warning.
102
+ # When +value+ is +:prevent+, raise a {StubbingError}.
103
+ #
104
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
105
+ #
106
+ # @example Preventing stubbing of a method on a non-mock object
107
+ # Mocha.configure do |c|
108
+ # c.stubbing_method_on_non_mock_object = :prevent
109
+ # end
110
+ #
111
+ # class Example
112
+ # def example_method; end
113
+ # end
114
+ #
115
+ # example = Example.new
116
+ # example.stubs(:example_method)
117
+ # # => Mocha::StubbingError: stubbing method on non-mock object:
118
+ # # => #<Example:0x593620>.example_method
119
+ #
120
+ def stubbing_method_on_non_mock_object=(value)
121
+ @options[:stubbing_method_on_non_mock_object] = value
122
+ end
123
+
124
+ # @private
125
+ def stubbing_method_on_non_mock_object
126
+ @options[:stubbing_method_on_non_mock_object]
127
+ end
128
+
129
+ # Configure whether stubbing of non-existent methods is allowed.
130
+ #
131
+ # This is useful if you want to ensure that methods you're mocking really exist. A common criticism of unit tests with mock objects is that such a test may (incorrectly) pass when an equivalent non-mocking test would (correctly) fail. While you should always have some integration tests, particularly for critical business functionality, this Mocha configuration setting should catch scenarios when mocked methods and real methods have become misaligned.
132
+ #
133
+ # When +value+ is +:allow+, do nothing. This is the default.
134
+ # When +value+ is +:warn+, display a warning.
135
+ # When +value+ is +:prevent+, raise a {StubbingError}.
136
+ #
137
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
138
+ #
139
+ # @example Preventing stubbing of a non-existent method
140
+ #
141
+ # Mocha.configure do |c|
142
+ # c.stubbing_non_existent_method = :prevent
143
+ # end
144
+ #
145
+ # class Example
146
+ # end
147
+ #
148
+ # example = Example.new
149
+ # example.stubs(:method_that_doesnt_exist)
150
+ # # => Mocha::StubbingError: stubbing non-existent method:
151
+ # # => #<Example:0x593760>.method_that_doesnt_exist
152
+ #
153
+ def stubbing_non_existent_method=(value)
154
+ @options[:stubbing_non_existent_method] = value
155
+ end
156
+
157
+ # @private
158
+ def stubbing_non_existent_method
159
+ @options[:stubbing_non_existent_method]
160
+ end
161
+
162
+ # Configure whether stubbing of non-public methods is allowed.
163
+ #
164
+ # Many people think that it's good practice only to mock public methods. This is one way to prevent your tests being too tightly coupled to the internal implementation of a class. Such tests tend to be very brittle and not much use when refactoring.
165
+ #
166
+ # When +value+ is +:allow+, do nothing. This is the default.
167
+ # When +value+ is +:warn+, display a warning.
168
+ # When +value+ is +:prevent+, raise a {StubbingError}.
169
+ #
170
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
171
+ #
172
+ # @example Preventing stubbing of a non-public method
173
+ # Mocha.configure do |c|
174
+ # c.stubbing_non_public_method = :prevent
175
+ # end
176
+ #
177
+ # class Example
178
+ # def internal_method; end
179
+ # private :internal_method
180
+ # end
181
+ #
182
+ # example = Example.new
183
+ # example.stubs(:internal_method)
184
+ # # => Mocha::StubbingError: stubbing non-public method:
185
+ # # => #<Example:0x593530>.internal_method
186
+ #
187
+ def stubbing_non_public_method=(value)
188
+ @options[:stubbing_non_public_method] = value
189
+ end
190
+
191
+ # @private
192
+ def stubbing_non_public_method
193
+ @options[:stubbing_non_public_method]
194
+ end
195
+
196
+ # Configure whether stubbing methods on the +nil+ object is allowed.
197
+ #
198
+ # This is usually done accidentally, but there might be rare cases where it is intended.
199
+ #
200
+ # This option only works for Ruby < v2.2.0. In later versions of Ruby +nil+ is frozen and so a {StubbingError} will be raised if you attempt to stub a method on +nil+.
201
+ #
202
+ # When +value+ is +:allow+, do nothing.
203
+ # When +value+ is +:warn+, display a warning.
204
+ # When +value+ is +:prevent+, raise a {StubbingError}. This is the default.
205
+ #
206
+ # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
207
+ #
208
+ def stubbing_method_on_nil=(value)
209
+ @options[:stubbing_method_on_nil] = value
210
+ end
211
+
212
+ # @private
213
+ def stubbing_method_on_nil
214
+ @options[:stubbing_method_on_nil]
215
+ end
216
+
217
+ # Display matching invocations alongside expectations on Mocha-related test failure.
218
+ #
219
+ # @param [Boolean] value +true+ to enable display of matching invocations; disabled by default.
220
+ #
221
+ # @example Enable display of matching invocations
222
+ # Mocha.configure do |c|
223
+ # c.display_matching_invocations_on_failure = true
224
+ # end
225
+ #
226
+ # foo = mock('foo')
227
+ # foo.expects(:bar)
228
+ # foo.stubs(:baz).returns('baz').raises(RuntimeError).throws(:tag, 'value')
229
+ #
230
+ # foo.baz(1, 2)
231
+ # assert_raises(RuntimeError) { foo.baz(3, 4) }
232
+ # assert_throws(:tag) { foo.baz(5, 6) }
233
+ #
234
+ # not all expectations were satisfied
235
+ # unsatisfied expectations:
236
+ # - expected exactly once, invoked never: #<Mock:foo>.bar
237
+ # satisfied expectations:
238
+ # - allowed any number of times, invoked 3 times: #<Mock:foo>.baz(any_parameters)
239
+ # - #<Mock:foo>.baz(1, 2) # => "baz"
240
+ # - #<Mock:foo>.baz(3, 4) # => raised RuntimeError
241
+ # - #<Mock:foo>.baz(5, 6) # => threw (:tag, "value")
242
+ def display_matching_invocations_on_failure=(value)
243
+ @options[:display_matching_invocations_on_failure] = value
244
+ end
245
+
246
+ # @private
247
+ def display_matching_invocations_on_failure?
248
+ @options[:display_matching_invocations_on_failure]
249
+ end
250
+
251
+ # Reinstate undocumented behaviour from v1.9
252
+ #
253
+ # Previously when {API#mock}, {API#stub}, or {API#stub_everything} were called with the first argument being a symbol, they built an *unnamed* mock object *and* expected or stubbed the method identified by the symbol argument; subsequent arguments were ignored.
254
+ # Now these methods build a *named* mock with the name specified by the symbol argument; *no* methods are expected or stubbed and subsequent arguments *are* taken into account.
255
+ #
256
+ # Previously if {Expectation#yields} or {Expectation#multiple_yields} was called on an expectation, but no block was given when the method was invoked, the instruction to yield was ignored.
257
+ # Now a +LocalJumpError+ is raised.
258
+ #
259
+ # Enabling this configuration option reinstates the previous behaviour, but displays a deprecation warning.
260
+ #
261
+ # @param [Boolean] value +true+ to reinstate undocumented behaviour; enabled by default.
262
+ #
263
+ # @example Reinstate undocumented behaviour for {API#mock}
264
+ # Mocha.configure do |c|
265
+ # c.reinstate_undocumented_behaviour_from_v1_9 = true
266
+ # end
267
+ #
268
+ # foo = mock(:bar)
269
+ # foo.inspect # => #<Mock>
270
+ #
271
+ # not all expectations were satisfied
272
+ # unsatisfied expectations:
273
+ # - expected exactly once, invoked never: #<Mock>.foo
274
+ #
275
+ # @example Reinstate undocumented behaviour for {API#stub}
276
+ # Mocha.configure do |c|
277
+ # c.reinstate_undocumented_behaviour_from_v1_9 = true
278
+ # end
279
+ #
280
+ # foo = stub(:bar)
281
+ # foo.inspect # => #<Mock>
282
+ # foo.bar # => nil
283
+ #
284
+ # @example Reinstate undocumented behaviour for {Expectation#yields}
285
+ # foo = mock('foo')
286
+ # foo.stubs(:my_method).yields(1, 2)
287
+ # foo.my_method # => raises LocalJumpError when no block is supplied
288
+ #
289
+ # Mocha.configure do |c|
290
+ # c.reinstate_undocumented_behaviour_from_v1_9 = true
291
+ # end
292
+ #
293
+ # foo = mock('foo')
294
+ # foo.stubs(:my_method).yields(1, 2)
295
+ # foo.my_method # => does *not* raise LocalJumpError when no block is supplied
296
+ #
297
+ def reinstate_undocumented_behaviour_from_v1_9=(value)
298
+ @options[:reinstate_undocumented_behaviour_from_v1_9] = value
299
+ end
300
+
301
+ # @private
302
+ def reinstate_undocumented_behaviour_from_v1_9?
303
+ @options[:reinstate_undocumented_behaviour_from_v1_9]
304
+ end
305
+
8
306
  class << self
9
-
10
- # :call-seq: allow(action, &block)
307
+ # Allow the specified +action+.
11
308
  #
12
- # Allow the specified <tt>action</tt> (as a symbol).
13
- # The <tt>actions</tt> currently available are <tt>:stubbing_method_unnecessarily, :stubbing_method_on_non_mock_object, :stubbing_non_existent_method, :stubbing_non_public_method</tt>.
14
- # If given a block, the configuration for the action will only be changed for the duration of the block, and will then be restored to the previous value.
309
+ # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
310
+ # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
311
+ # @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:allow+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:allow+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
312
+ # * {#stubbing_method_unnecessarily=}
313
+ # * {#stubbing_method_on_non_mock_object=}
314
+ # * {#stubbing_non_existent_method=}
315
+ # * {#stubbing_non_public_method=}
316
+ # * {#stubbing_method_on_nil=}
15
317
  def allow(action, &block)
318
+ if block_given?
319
+ Deprecation.warning("Use Mocha::Configuration.override(#{action}: :allow) with the same block")
320
+ else
321
+ Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :allow }")
322
+ end
16
323
  change_config action, :allow, &block
17
324
  end
18
-
19
- def allow?(action) # :nodoc:
20
- configuration[action] == :allow
325
+
326
+ # @private
327
+ def allow?(action)
328
+ configuration.allow?(action)
21
329
  end
22
-
23
- # :call-seq: warn_when(action, &block)
330
+
331
+ # Warn if the specified +action+ is attempted.
24
332
  #
25
- # Warn if the specified <tt>action</tt> (as a symbol) is attempted.
26
- # The <tt>actions</tt> currently available are <tt>:stubbing_method_unnecessarily, :stubbing_method_on_non_mock_object, :stubbing_non_existent_method, :stubbing_non_public_method</tt>.
27
- # If given a block, the configuration for the action will only be changed for the duration of the block, and will then be restored to the previous value.
333
+ # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
334
+ # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
335
+ # @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:warn+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:warn+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
336
+ # * {#stubbing_method_unnecessarily=}
337
+ # * {#stubbing_method_on_non_mock_object=}
338
+ # * {#stubbing_non_existent_method=}
339
+ # * {#stubbing_non_public_method=}
340
+ # * {#stubbing_method_on_nil=}
28
341
  def warn_when(action, &block)
342
+ if block_given?
343
+ Deprecation.warning("Use Mocha::Configuration.override(#{action}: :warn) with the same block")
344
+ else
345
+ Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :warn }")
346
+ end
29
347
  change_config action, :warn, &block
30
348
  end
31
-
32
- def warn_when?(action) # :nodoc:
33
- configuration[action] == :warn
349
+
350
+ # @private
351
+ def warn_when?(action)
352
+ configuration.warn_when?(action)
34
353
  end
35
-
36
- # :call-seq: prevent(action, &block)
354
+
355
+ # Raise a {StubbingError} if the specified +action+ is attempted.
37
356
  #
38
- # Raise a StubbingError if the specified <tt>action</tt> (as a symbol) is attempted.
39
- # The <tt>actions</tt> currently available are <tt>:stubbing_method_unnecessarily, :stubbing_method_on_non_mock_object, :stubbing_non_existent_method, :stubbing_non_public_method</tt>.
40
- # If given a block, the configuration for the action will only be changed for the duration of the block, and will then be restored to the previous value.
357
+ # @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
358
+ # @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
359
+ # @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:prevent+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:prevent+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
360
+ # * {#stubbing_method_unnecessarily=}
361
+ # * {#stubbing_method_on_non_mock_object=}
362
+ # * {#stubbing_non_existent_method=}
363
+ # * {#stubbing_non_public_method=}
364
+ # * {#stubbing_method_on_nil=}
41
365
  def prevent(action, &block)
366
+ if block_given?
367
+ Deprecation.warning("Use Mocha::Configuration.override(#{action}: :prevent) with the same block")
368
+ else
369
+ Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :prevent }")
370
+ end
42
371
  change_config action, :prevent, &block
43
372
  end
44
-
45
- def prevent?(action) # :nodoc:
46
- configuration[action] == :prevent
373
+
374
+ # @private
375
+ def prevent?(action)
376
+ configuration.prevent?(action)
47
377
  end
48
-
49
- def reset_configuration # :nodoc:
378
+
379
+ # @private
380
+ def reset_configuration
50
381
  @configuration = nil
51
382
  end
52
-
53
- private
54
-
55
- def configuration # :nodoc:
56
- @configuration ||= DEFAULTS.dup
383
+
384
+ # Temporarily modify {Configuration} options.
385
+ #
386
+ # The supplied +temporary_options+ will override the current configuration for the duration of the supplied block.
387
+ # The configuration will be returned to its original state when the block returns.
388
+ #
389
+ # @param [Hash] temporary_options the configuration options to apply for the duration of the block.
390
+ # @yield block during which the configuration change will be in force.
391
+ #
392
+ # @example Temporarily allow stubbing of +nil+
393
+ # Mocha::Configuration.override(stubbing_method_on_nil: :allow) do
394
+ # nil.stubs(:foo)
395
+ # end
396
+ def override(temporary_options)
397
+ original_configuration = configuration
398
+ @configuration = configuration.merge(new(temporary_options))
399
+ yield
400
+ ensure
401
+ @configuration = original_configuration
57
402
  end
58
403
 
59
- def change_config(action, new_value, &block) # :nodoc:
404
+ # @private
405
+ def configuration
406
+ @configuration ||= new
407
+ end
408
+
409
+ private
410
+
411
+ # @private
412
+ def change_config(action, new_value, &block)
60
413
  if block_given?
61
414
  temporarily_change_config action, new_value, &block
62
415
  else
63
- configuration[action] = new_value
416
+ configuration.send("#{action}=".to_sym, new_value)
64
417
  end
65
418
  end
66
419
 
67
- def temporarily_change_config(action, new_value, &block) # :nodoc:
68
- original_value = configuration[action]
69
- configuration[action] = new_value
420
+ # @private
421
+ def temporarily_change_config(action, new_value)
422
+ original_configuration = configuration
423
+ new_configuration = configuration.dup
424
+ new_configuration.send("#{action}=".to_sym, new_value)
425
+ @configuration = new_configuration
70
426
  yield
71
427
  ensure
72
- configuration[action] = original_value
428
+ @configuration = original_configuration
73
429
  end
74
-
75
430
  end
76
-
77
431
  end
78
-
79
432
  end
@@ -0,0 +1,12 @@
1
+ module Mocha
2
+ module Debug
3
+ OPTIONS = (ENV['MOCHA_OPTIONS'] || '').split(',').inject({}) do |hash, key|
4
+ hash[key] = true
5
+ hash
6
+ end.freeze
7
+
8
+ def self.puts(message)
9
+ warn(message) if OPTIONS['debug']
10
+ end
11
+ end
12
+ end
@@ -1,22 +1,21 @@
1
+ require 'mocha/backtrace_filter'
2
+
1
3
  module Mocha
2
-
3
4
  class Deprecation
4
-
5
5
  class << self
6
-
7
6
  attr_accessor :mode, :messages
8
-
9
- def warning(message)
7
+
8
+ def warning(*messages)
9
+ message = messages.join
10
10
  @messages << message
11
- $stderr.puts "Mocha deprecation warning: #{message}" unless mode == :disabled
12
- $stderr.puts caller.join("\n ") if mode == :debug
11
+ return if mode == :disabled
12
+ filter = BacktraceFilter.new
13
+ location = filter.filtered(caller)[0]
14
+ warn "Mocha deprecation warning at #{location}: #{message}"
13
15
  end
14
-
15
16
  end
16
-
17
+
17
18
  self.mode = :enabled
18
19
  self.messages = []
19
-
20
20
  end
21
-
22
- end
21
+ end