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
@@ -1,15 +1,8 @@
1
- require 'mocha/backtrace_filter'
2
-
3
1
  module Mocha
4
-
5
- class ExpectationError < Exception
6
-
7
- def initialize(message = nil, backtrace = [])
8
- super(message)
9
- filter = BacktraceFilter.new
10
- set_backtrace(filter.filtered(backtrace))
11
- end
12
-
13
- end
14
-
2
+ # Default exception class raised when an unexpected invocation or an unsatisfied expectation occurs.
3
+ #
4
+ # Authors of test libraries may use +Mocha::ExpectationErrorFactory+ to have Mocha raise a different exception.
5
+ #
6
+ # @see Mocha::ExpectationErrorFactory
7
+ class ExpectationError < Exception; end # rubocop:disable Lint/InheritException
15
8
  end
@@ -0,0 +1,35 @@
1
+ require 'mocha/backtrace_filter'
2
+ require 'mocha/expectation_error'
3
+
4
+ module Mocha
5
+ # This factory determines what class of exception should be raised when Mocha detects a test failure.
6
+ #
7
+ # This class should only be used by authors of test libraries and not by typical "users" of Mocha.
8
+ #
9
+ # For example, it is used by +Mocha::Integration::MiniTest::Adapter+ in order to have Mocha raise a +MiniTest::Assertion+ which can then be sensibly handled by +MiniTest::Unit::TestCase+.
10
+ #
11
+ # @see Mocha::Integration::MiniTest::Adapter
12
+ class ExpectationErrorFactory
13
+ class << self
14
+ # @!attribute exception_class
15
+ # Determines what class of exception should be raised when Mocha detects a test failure.
16
+ #
17
+ # This attribute may be set by authors of test libraries in order to have Mocha raise exceptions of a specific class when there is an unexpected invocation or an unsatisfied expectation.
18
+ #
19
+ # By default a +Mocha::ExpectationError+ will be raised.
20
+ #
21
+ # @return [Exception] class of exception to be raised when an expectation error occurs
22
+ # @see Mocha::ExpectationError
23
+ attr_accessor :exception_class
24
+
25
+ # @private
26
+ def build(message = nil, backtrace = [])
27
+ exception = exception_class.new(message)
28
+ filter = BacktraceFilter.new
29
+ exception.set_backtrace(filter.filtered(backtrace))
30
+ exception
31
+ end
32
+ end
33
+ self.exception_class = ExpectationError
34
+ end
35
+ end
@@ -1,11 +1,9 @@
1
- module Mocha # :nodoc:
2
-
1
+ module Mocha
3
2
  class ExpectationList
4
-
5
- def initialize
6
- @expectations = []
3
+ def initialize(expectations = [])
4
+ @expectations = expectations
7
5
  end
8
-
6
+
9
7
  def add(expectation)
10
8
  @expectations.unshift(expectation)
11
9
  expectation
@@ -14,31 +12,31 @@ module Mocha # :nodoc:
14
12
  def remove_all_matching_method(method_name)
15
13
  @expectations.reject! { |expectation| expectation.matches_method?(method_name) }
16
14
  end
17
-
15
+
18
16
  def matches_method?(method_name)
19
17
  @expectations.any? { |expectation| expectation.matches_method?(method_name) }
20
18
  end
21
-
22
- def match(method_name, *arguments)
23
- matching_expectations(method_name, *arguments).first
19
+
20
+ def match(invocation)
21
+ matching_expectations(invocation).first
24
22
  end
25
-
26
- def match_allowing_invocation(method_name, *arguments)
27
- matching_expectations(method_name, *arguments).detect { |e| e.invocations_allowed? }
23
+
24
+ def match_allowing_invocation(invocation)
25
+ matching_expectations(invocation).detect(&:invocations_allowed?)
28
26
  end
29
-
27
+
30
28
  def verified?(assertion_counter = nil)
31
29
  @expectations.all? { |expectation| expectation.verified?(assertion_counter) }
32
30
  end
33
-
31
+
34
32
  def to_a
35
33
  @expectations
36
34
  end
37
-
35
+
38
36
  def to_set
39
37
  @expectations.to_set
40
38
  end
41
-
39
+
42
40
  def length
43
41
  @expectations.length
44
42
  end
@@ -47,12 +45,14 @@ module Mocha # :nodoc:
47
45
  @expectations.any?
48
46
  end
49
47
 
48
+ def +(other)
49
+ self.class.new(to_a + other.to_a)
50
+ end
51
+
50
52
  private
51
-
52
- def matching_expectations(method_name, *arguments)
53
- @expectations.select { |e| e.match?(method_name, *arguments) }
53
+
54
+ def matching_expectations(invocation)
55
+ @expectations.select { |e| e.match?(invocation) }
54
56
  end
55
-
56
57
  end
57
-
58
58
  end
@@ -0,0 +1,42 @@
1
+ require 'mocha/mockery'
2
+
3
+ module Mocha
4
+ # Integration hooks for test library authors.
5
+ #
6
+ # The methods in this module should be called from test libraries wishing to integrate with Mocha.
7
+ #
8
+ # This module is provided as part of the +Mocha::API+ module and is therefore part of the public API, but should only be used by authors of test libraries and not by typical "users" of Mocha.
9
+ #
10
+ # Integration with Test::Unit and MiniTest are provided as part of Mocha, because they are (or were once) part of the Ruby standard library. Integration with other test libraries is not provided as *part* of Mocha, but is supported by means of the methods in this module.
11
+ #
12
+ # See the code in the +Adapter+ modules for examples of how to use the methods in this module. +Mocha::ExpectationErrorFactory+ may be used if you want +Mocha+ to raise a different type of exception.
13
+ #
14
+ # @see Mocha::Integration::TestUnit::Adapter
15
+ # @see Mocha::Integration::MiniTest::Adapter
16
+ # @see Mocha::ExpectationErrorFactory
17
+ # @see Mocha::API
18
+ module Hooks
19
+ # Prepares Mocha before a test (only for use by authors of test libraries).
20
+ #
21
+ # This method should be called before each individual test starts (including before any "setup" code).
22
+ def mocha_setup
23
+ Mockery.setup
24
+ end
25
+
26
+ # Verifies that all mock expectations have been met (only for use by authors of test libraries).
27
+ #
28
+ # This is equivalent to a series of "assertions".
29
+ #
30
+ # This method should be called at the end of each individual test, before it has been determined whether or not the test has passed.
31
+ def mocha_verify(assertion_counter = nil)
32
+ Mockery.verify(assertion_counter)
33
+ end
34
+
35
+ # Resets Mocha after a test (only for use by authors of test libraries).
36
+ #
37
+ # This method should be called after each individual test has finished (including after any "teardown" code).
38
+ def mocha_teardown
39
+ Mockery.teardown
40
+ end
41
+ end
42
+ end
@@ -1,19 +1,15 @@
1
1
  module Mocha
2
-
3
2
  class InStateOrderingConstraint
4
-
5
3
  def initialize(state_predicate)
6
4
  @state_predicate = state_predicate
7
5
  end
8
-
6
+
9
7
  def allows_invocation_now?
10
8
  @state_predicate.active?
11
9
  end
12
-
10
+
13
11
  def mocha_inspect
14
12
  "when #{@state_predicate.mocha_inspect}"
15
13
  end
16
-
17
14
  end
18
-
19
- end
15
+ end
data/lib/mocha/inspect.rb CHANGED
@@ -1,67 +1,59 @@
1
1
  require 'date'
2
2
 
3
3
  module Mocha
4
-
5
- module ObjectMethods
6
- def mocha_inspect
7
- address = self.__id__ * 2
8
- address += 0x100000000 if address < 0
9
- inspect =~ /#</ ? "#<#{self.class}:0x#{'%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
10
11
  end
11
- end
12
-
13
- module StringMethods
14
- def mocha_inspect
15
- inspect.gsub(/\"/, "'")
16
- end
17
- end
18
-
19
- module ArrayMethods
20
- def mocha_inspect
21
- "[#{collect { |member| member.mocha_inspect }.join(', ')}]"
12
+
13
+ module ArrayMethods
14
+ def mocha_inspect(wrapped = true)
15
+ unwrapped = collect(&:mocha_inspect).join(', ')
16
+ wrapped ? "[#{unwrapped}]" : unwrapped
17
+ end
22
18
  end
23
- end
24
-
25
- module HashMethods
26
- def mocha_inspect
27
- "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
19
+
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
28
25
  end
29
- end
30
-
31
- module TimeMethods
32
- def mocha_inspect
33
- "#{inspect} (#{to_f} secs)"
26
+
27
+ module TimeMethods
28
+ def mocha_inspect
29
+ "#{inspect} (#{to_f} secs)"
30
+ end
34
31
  end
35
- end
36
-
37
- module DateMethods
38
- def mocha_inspect
39
- to_s
32
+
33
+ module DateMethods
34
+ def mocha_inspect
35
+ to_s
36
+ end
40
37
  end
41
38
  end
42
-
43
39
  end
44
40
 
45
41
  class Object
46
- include Mocha::ObjectMethods
47
- end
48
-
49
- class String
50
- include Mocha::StringMethods
42
+ include Mocha::Inspect::ObjectMethods
51
43
  end
52
44
 
53
45
  class Array
54
- include Mocha::ArrayMethods
46
+ include Mocha::Inspect::ArrayMethods
55
47
  end
56
48
 
57
49
  class Hash
58
- include Mocha::HashMethods
50
+ include Mocha::Inspect::HashMethods
59
51
  end
60
52
 
61
53
  class Time
62
- include Mocha::TimeMethods
54
+ include Mocha::Inspect::TimeMethods
63
55
  end
64
56
 
65
57
  class Date
66
- include Mocha::DateMethods
67
- end
58
+ include Mocha::Inspect::DateMethods
59
+ end
@@ -1,32 +1,23 @@
1
- require 'mocha/class_method'
1
+ require 'mocha/stubbed_method'
2
2
 
3
3
  module Mocha
4
+ class InstanceMethod < StubbedMethod
5
+ private
4
6
 
5
- class InstanceMethod < ClassMethod
6
-
7
- def hide_original_method
8
- super if singleton_method?(method)
7
+ def mock_owner
8
+ stubbee
9
9
  end
10
10
 
11
- def restore_original_method
12
- super if singleton_method?(hidden_method)
11
+ def method_body(method)
12
+ PRE_RUBY_V19 ? proc { |*args, &block| method.call(*args, &block) } : method
13
13
  end
14
14
 
15
- def method_exists?(method)
16
- return true if stubbee.public_methods(false).include?(method)
17
- return true if stubbee.protected_methods(false).include?(method)
18
- return true if stubbee.private_methods(false).include?(method)
19
- return false
15
+ def stubbee_method(method_name)
16
+ stubbee._method(method_name)
20
17
  end
21
18
 
22
- def singleton_method?(method)
23
- __metaclass__ = stubbee.__metaclass__
24
- return true if __metaclass__.public_instance_methods(false).include?(method)
25
- return true if __metaclass__.protected_instance_methods(false).include?(method)
26
- return true if __metaclass__.private_instance_methods(false).include?(method)
27
- return false
19
+ def original_method_owner
20
+ stubbee.singleton_class
28
21
  end
29
-
30
22
  end
31
-
32
- end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Mocha
2
+ module Integration
3
+ class AssertionCounter
4
+ def initialize(test_case)
5
+ @test_case = test_case
6
+ end
7
+
8
+ def increment
9
+ @test_case.assert(true)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,52 @@
1
+ require 'mocha/api'
2
+ require 'mocha/integration/assertion_counter'
3
+ require 'mocha/expectation_error_factory'
4
+
5
+ module Mocha
6
+ module Integration
7
+ module MiniTest
8
+ # Integrates Mocha into recent versions of MiniTest.
9
+ #
10
+ # See the source code for an example of how to integrate Mocha into a test library.
11
+ module Adapter
12
+ include Mocha::API
13
+
14
+ # @private
15
+ def self.applicable_to?(mini_test_version)
16
+ Gem::Requirement.new('>= 3.3.0').satisfied_by?(mini_test_version)
17
+ end
18
+
19
+ # @private
20
+ def self.description
21
+ 'adapter for MiniTest gem >= v3.3.0'
22
+ end
23
+
24
+ # @private
25
+ def self.included(_mod)
26
+ Mocha::ExpectationErrorFactory.exception_class = ::MiniTest::Assertion
27
+ end
28
+
29
+ # @private
30
+ def before_setup
31
+ mocha_setup
32
+ super
33
+ end
34
+
35
+ # @private
36
+ def before_teardown
37
+ return unless passed?
38
+ assertion_counter = Integration::AssertionCounter.new(self)
39
+ mocha_verify(assertion_counter)
40
+ ensure
41
+ super
42
+ end
43
+
44
+ # @private
45
+ def after_teardown
46
+ super
47
+ mocha_teardown
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,20 +1,14 @@
1
1
  require 'mocha/expectation_error'
2
2
 
3
3
  module Mocha
4
-
5
4
  module Integration
6
-
7
5
  module MiniTest
8
-
9
6
  def self.translate(exception)
10
- return exception unless exception.kind_of?(::Mocha::ExpectationError)
7
+ return exception unless exception.is_a?(::Mocha::ExpectationError)
11
8
  translated_exception = ::MiniTest::Assertion.new(exception.message)
12
9
  translated_exception.set_backtrace(exception.backtrace)
13
10
  translated_exception
14
11
  end
15
-
16
12
  end
17
-
18
13
  end
19
-
20
14
  end
@@ -0,0 +1,19 @@
1
+ module Mocha
2
+ module Integration
3
+ module MiniTest
4
+ module Nothing
5
+ def self.applicable_to?(_test_unit_version, _ruby_version = nil)
6
+ true
7
+ end
8
+
9
+ def self.description
10
+ 'nothing (no MiniTest integration available)'
11
+ end
12
+
13
+ def self.included(_mod)
14
+ raise 'No MiniTest integration available'
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,44 +1,54 @@
1
- require 'mocha/integration/mini_test/assertion_counter'
1
+ require 'mocha/integration/assertion_counter'
2
+ require 'mocha/integration/monkey_patcher'
3
+ require 'mocha/integration/mini_test/exception_translation'
2
4
 
3
5
  module Mocha
4
-
5
6
  module Integration
6
-
7
7
  module MiniTest
8
-
9
8
  module Version13
9
+ def self.applicable_to?(mini_test_version)
10
+ Gem::Requirement.new('>= 1.3.0', '<= 1.3.1').satisfied_by?(mini_test_version)
11
+ end
12
+
13
+ def self.description
14
+ 'monkey patch for MiniTest gem v1.3'
15
+ end
16
+
10
17
  def self.included(mod)
11
- $stderr.puts "Monkey patching MiniTest v1.3" if $mocha_options['debug']
18
+ MonkeyPatcher.apply(mod, RunMethodPatch)
12
19
  end
13
- def run runner
14
- assertion_counter = AssertionCounter.new(self)
15
- result = '.'
16
- begin
20
+
21
+ module RunMethodPatch
22
+ # rubocop:disable all
23
+ def run runner
24
+ assertion_counter = AssertionCounter.new(self)
25
+ result = '.'
17
26
  begin
18
- @passed = nil
19
- self.setup
20
- self.__send__ self.name
21
- mocha_verify(assertion_counter)
22
- @passed = true
23
- rescue Exception => e
24
- @passed = false
25
- result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
26
- ensure
27
27
  begin
28
- self.teardown
28
+ @passed = nil
29
+ mocha_setup
30
+ self.setup
31
+ self.__send__ self.name
32
+ mocha_verify(assertion_counter)
33
+ @passed = true
29
34
  rescue Exception => e
35
+ @passed = false
30
36
  result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
37
+ ensure
38
+ begin
39
+ self.teardown
40
+ rescue Exception => e
41
+ result = runner.puke(self.class, self.name, Mocha::Integration::MiniTest.translate(e))
42
+ end
31
43
  end
44
+ ensure
45
+ mocha_teardown
32
46
  end
33
- ensure
34
- mocha_teardown
47
+ result
35
48
  end
36
- result
49
+ # rubocop:enable all
37
50
  end
38
51
  end
39
-
40
52
  end
41
-
42
53
  end
43
-
44
54
  end
@@ -1,45 +1,54 @@
1
- require 'mocha/integration/mini_test/assertion_counter'
2
- require 'mocha/expectation_error'
1
+ require 'mocha/integration/assertion_counter'
2
+ require 'mocha/integration/monkey_patcher'
3
+ require 'mocha/integration/mini_test/exception_translation'
3
4
 
4
5
  module Mocha
5
-
6
6
  module Integration
7
-
8
7
  module MiniTest
9
-
10
8
  module Version140
9
+ def self.applicable_to?(mini_test_version)
10
+ Gem::Requirement.new('1.4.0').satisfied_by?(mini_test_version)
11
+ end
12
+
13
+ def self.description
14
+ 'monkey patch for MiniTest gem v1.4.0'
15
+ end
16
+
11
17
  def self.included(mod)
12
- $stderr.puts "Monkey patching MiniTest v1.4.0" if $mocha_options['debug']
18
+ MonkeyPatcher.apply(mod, RunMethodPatch)
13
19
  end
14
- def run runner
15
- assertion_counter = AssertionCounter.new(self)
16
- result = '.'
17
- begin
20
+
21
+ module RunMethodPatch
22
+ # rubocop:disable all
23
+ def run runner
24
+ assertion_counter = AssertionCounter.new(self)
25
+ result = '.'
18
26
  begin
19
- @passed = nil
20
- self.setup
21
- self.__send__ self.__name__
22
- mocha_verify(assertion_counter)
23
- @passed = true
24
- rescue Exception => e
25
- @passed = false
26
- result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
27
- ensure
28
27
  begin
29
- self.teardown
28
+ @passed = nil
29
+ mocha_setup
30
+ self.setup
31
+ self.__send__ self.__name__
32
+ mocha_verify(assertion_counter)
33
+ @passed = true
30
34
  rescue Exception => e
35
+ @passed = false
31
36
  result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
37
+ ensure
38
+ begin
39
+ self.teardown
40
+ rescue Exception => e
41
+ result = runner.puke(self.class, self.__name__, Mocha::Integration::MiniTest.translate(e))
42
+ end
32
43
  end
44
+ ensure
45
+ mocha_teardown
33
46
  end
34
- ensure
35
- mocha_teardown
47
+ result
36
48
  end
37
- result
49
+ # rubocop:enable all
38
50
  end
39
51
  end
40
-
41
52
  end
42
-
43
53
  end
44
-
45
54
  end