mocha 0.5.6 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (192) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/.github/FUNDING.yml +1 -0
  4. data/.rubocop.yml +92 -0
  5. data/.rubocop_todo.yml +39 -0
  6. data/.yardopts +25 -0
  7. data/CONTRIBUTING.md +7 -0
  8. data/COPYING.md +3 -0
  9. data/Gemfile +17 -0
  10. data/{MIT-LICENSE → MIT-LICENSE.md} +2 -2
  11. data/README.md +361 -0
  12. data/RELEASE.md +1235 -0
  13. data/Rakefile +165 -123
  14. data/gemfiles/Gemfile.minitest.latest +8 -0
  15. data/gemfiles/Gemfile.rubocop +9 -0
  16. data/gemfiles/Gemfile.test-unit.latest +8 -0
  17. data/lib/mocha/any_instance_method.rb +12 -26
  18. data/lib/mocha/any_instance_receiver.rb +20 -0
  19. data/lib/mocha/api.rb +213 -0
  20. data/lib/mocha/argument_iterator.rb +17 -0
  21. data/lib/mocha/backtrace_filter.rb +15 -0
  22. data/lib/mocha/block_matchers.rb +33 -0
  23. data/lib/mocha/cardinality.rb +110 -0
  24. data/lib/mocha/central.rb +33 -22
  25. data/lib/mocha/change_state_side_effect.rb +17 -0
  26. data/lib/mocha/class_methods.rb +67 -0
  27. data/lib/mocha/configuration.rb +338 -0
  28. data/lib/mocha/default_name.rb +15 -0
  29. data/lib/mocha/default_receiver.rb +13 -0
  30. data/lib/mocha/deprecation.rb +19 -14
  31. data/lib/mocha/detection/minitest.rb +25 -0
  32. data/lib/mocha/detection/test_unit.rb +30 -0
  33. data/lib/mocha/error_with_filtered_backtrace.rb +15 -0
  34. data/lib/mocha/exception_raiser.rb +11 -10
  35. data/lib/mocha/expectation.rb +553 -168
  36. data/lib/mocha/expectation_error.rb +9 -14
  37. data/lib/mocha/expectation_error_factory.rb +37 -0
  38. data/lib/mocha/expectation_list.rb +30 -14
  39. data/lib/mocha/hooks.rb +55 -0
  40. data/lib/mocha/ignoring_warning.rb +20 -0
  41. data/lib/mocha/impersonating_any_instance_name.rb +13 -0
  42. data/lib/mocha/impersonating_name.rb +13 -0
  43. data/lib/mocha/in_state_ordering_constraint.rb +17 -0
  44. data/lib/mocha/inspect.rb +56 -22
  45. data/lib/mocha/instance_method.rb +17 -4
  46. data/lib/mocha/integration/assertion_counter.rb +15 -0
  47. data/lib/mocha/integration/minitest/adapter.rb +71 -0
  48. data/lib/mocha/integration/minitest.rb +29 -0
  49. data/lib/mocha/integration/monkey_patcher.rb +26 -0
  50. data/lib/mocha/integration/test_unit/adapter.rb +61 -0
  51. data/lib/mocha/integration/test_unit.rb +29 -0
  52. data/lib/mocha/integration.rb +5 -0
  53. data/lib/mocha/invocation.rb +76 -0
  54. data/lib/mocha/logger.rb +13 -0
  55. data/lib/mocha/macos_version.rb +7 -0
  56. data/lib/mocha/method_matcher.rb +8 -10
  57. data/lib/mocha/minitest.rb +7 -0
  58. data/lib/mocha/mock.rb +333 -108
  59. data/lib/mocha/mockery.rb +199 -0
  60. data/lib/mocha/name.rb +13 -0
  61. data/lib/mocha/not_initialized_error.rb +9 -0
  62. data/lib/mocha/object_methods.rb +183 -0
  63. data/lib/mocha/object_receiver.rb +20 -0
  64. data/lib/mocha/parameter_matchers/all_of.rb +38 -28
  65. data/lib/mocha/parameter_matchers/any_of.rb +44 -33
  66. data/lib/mocha/parameter_matchers/any_parameters.rb +33 -26
  67. data/lib/mocha/parameter_matchers/anything.rb +31 -22
  68. data/lib/mocha/parameter_matchers/base_methods.rb +64 -0
  69. data/lib/mocha/parameter_matchers/equals.rb +36 -25
  70. data/lib/mocha/parameter_matchers/equivalent_uri.rb +65 -0
  71. data/lib/mocha/parameter_matchers/has_entries.rb +48 -29
  72. data/lib/mocha/parameter_matchers/has_entry.rb +90 -42
  73. data/lib/mocha/parameter_matchers/has_key.rb +39 -26
  74. data/lib/mocha/parameter_matchers/has_keys.rb +59 -0
  75. data/lib/mocha/parameter_matchers/has_value.rb +39 -26
  76. data/lib/mocha/parameter_matchers/includes.rb +88 -23
  77. data/lib/mocha/parameter_matchers/instance_methods.rb +28 -0
  78. data/lib/mocha/parameter_matchers/instance_of.rb +37 -26
  79. data/lib/mocha/parameter_matchers/is_a.rb +38 -26
  80. data/lib/mocha/parameter_matchers/kind_of.rb +39 -26
  81. data/lib/mocha/parameter_matchers/not.rb +37 -26
  82. data/lib/mocha/parameter_matchers/optionally.rb +52 -17
  83. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +91 -0
  84. data/lib/mocha/parameter_matchers/regexp_matches.rb +37 -25
  85. data/lib/mocha/parameter_matchers/responds_with.rb +82 -0
  86. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +55 -0
  87. data/lib/mocha/parameter_matchers.rb +12 -5
  88. data/lib/mocha/parameters_matcher.rb +28 -19
  89. data/lib/mocha/raised_exception.rb +13 -0
  90. data/lib/mocha/return_values.rb +13 -18
  91. data/lib/mocha/ruby_version.rb +7 -0
  92. data/lib/mocha/sequence.rb +23 -17
  93. data/lib/mocha/single_return_value.rb +8 -18
  94. data/lib/mocha/state_machine.rb +95 -0
  95. data/lib/mocha/stubbed_method.rb +96 -0
  96. data/lib/mocha/stubbing_error.rb +10 -0
  97. data/lib/mocha/test_unit.rb +7 -0
  98. data/lib/mocha/thrower.rb +15 -0
  99. data/lib/mocha/thrown_object.rb +14 -0
  100. data/lib/mocha/version.rb +5 -0
  101. data/lib/mocha/yield_parameters.rb +12 -20
  102. data/lib/mocha.rb +19 -17
  103. data/mise.toml +2 -0
  104. data/mocha.gemspec +40 -0
  105. metadata +130 -145
  106. data/COPYING +0 -3
  107. data/README +0 -35
  108. data/RELEASE +0 -188
  109. data/examples/misc.rb +0 -44
  110. data/examples/mocha.rb +0 -26
  111. data/examples/stubba.rb +0 -65
  112. data/lib/mocha/auto_verify.rb +0 -118
  113. data/lib/mocha/class_method.rb +0 -66
  114. data/lib/mocha/infinite_range.rb +0 -25
  115. data/lib/mocha/is_a.rb +0 -9
  116. data/lib/mocha/metaclass.rb +0 -7
  117. data/lib/mocha/missing_expectation.rb +0 -17
  118. data/lib/mocha/multiple_yields.rb +0 -20
  119. data/lib/mocha/no_yields.rb +0 -11
  120. data/lib/mocha/object.rb +0 -110
  121. data/lib/mocha/parameter_matchers/base.rb +0 -15
  122. data/lib/mocha/parameter_matchers/object.rb +0 -9
  123. data/lib/mocha/pretty_parameters.rb +0 -28
  124. data/lib/mocha/setup_and_teardown.rb +0 -23
  125. data/lib/mocha/single_yield.rb +0 -18
  126. data/lib/mocha/standalone.rb +0 -32
  127. data/lib/mocha/stub.rb +0 -18
  128. data/lib/mocha/test_case_adapter.rb +0 -49
  129. data/lib/mocha_standalone.rb +0 -2
  130. data/lib/stubba.rb +0 -2
  131. data/test/acceptance/expected_invocation_count_acceptance_test.rb +0 -187
  132. data/test/acceptance/mocha_acceptance_test.rb +0 -98
  133. data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +0 -44
  134. data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +0 -71
  135. data/test/acceptance/optional_parameters_acceptance_test.rb +0 -63
  136. data/test/acceptance/parameter_matcher_acceptance_test.rb +0 -117
  137. data/test/acceptance/partial_mocks_acceptance_test.rb +0 -40
  138. data/test/acceptance/sequence_acceptance_test.rb +0 -179
  139. data/test/acceptance/standalone_acceptance_test.rb +0 -131
  140. data/test/acceptance/stubba_acceptance_test.rb +0 -102
  141. data/test/active_record_test_case.rb +0 -36
  142. data/test/deprecation_disabler.rb +0 -15
  143. data/test/execution_point.rb +0 -34
  144. data/test/integration/mocha_test_result_integration_test.rb +0 -105
  145. data/test/integration/stubba_integration_test.rb +0 -89
  146. data/test/integration/stubba_test_result_integration_test.rb +0 -85
  147. data/test/method_definer.rb +0 -18
  148. data/test/test_helper.rb +0 -12
  149. data/test/test_runner.rb +0 -31
  150. data/test/unit/any_instance_method_test.rb +0 -126
  151. data/test/unit/array_inspect_test.rb +0 -16
  152. data/test/unit/auto_verify_test.rb +0 -129
  153. data/test/unit/central_test.rb +0 -124
  154. data/test/unit/class_method_test.rb +0 -200
  155. data/test/unit/date_time_inspect_test.rb +0 -21
  156. data/test/unit/expectation_error_test.rb +0 -24
  157. data/test/unit/expectation_list_test.rb +0 -75
  158. data/test/unit/expectation_raiser_test.rb +0 -28
  159. data/test/unit/expectation_test.rb +0 -483
  160. data/test/unit/hash_inspect_test.rb +0 -16
  161. data/test/unit/infinite_range_test.rb +0 -53
  162. data/test/unit/metaclass_test.rb +0 -22
  163. data/test/unit/method_matcher_test.rb +0 -23
  164. data/test/unit/missing_expectation_test.rb +0 -42
  165. data/test/unit/mock_test.rb +0 -323
  166. data/test/unit/multiple_yields_test.rb +0 -18
  167. data/test/unit/no_yield_test.rb +0 -18
  168. data/test/unit/object_inspect_test.rb +0 -37
  169. data/test/unit/object_test.rb +0 -165
  170. data/test/unit/parameter_matchers/all_of_test.rb +0 -26
  171. data/test/unit/parameter_matchers/any_of_test.rb +0 -26
  172. data/test/unit/parameter_matchers/anything_test.rb +0 -21
  173. data/test/unit/parameter_matchers/has_entries_test.rb +0 -30
  174. data/test/unit/parameter_matchers/has_entry_test.rb +0 -40
  175. data/test/unit/parameter_matchers/has_key_test.rb +0 -25
  176. data/test/unit/parameter_matchers/has_value_test.rb +0 -25
  177. data/test/unit/parameter_matchers/includes_test.rb +0 -25
  178. data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
  179. data/test/unit/parameter_matchers/is_a_test.rb +0 -25
  180. data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
  181. data/test/unit/parameter_matchers/not_test.rb +0 -26
  182. data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -25
  183. data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
  184. data/test/unit/parameters_matcher_test.rb +0 -121
  185. data/test/unit/return_values_test.rb +0 -63
  186. data/test/unit/sequence_test.rb +0 -104
  187. data/test/unit/setup_and_teardown_test.rb +0 -76
  188. data/test/unit/single_return_value_test.rb +0 -33
  189. data/test/unit/single_yield_test.rb +0 -18
  190. data/test/unit/string_inspect_test.rb +0 -11
  191. data/test/unit/stub_test.rb +0 -24
  192. data/test/unit/yield_parameters_test.rb +0 -93
@@ -1,15 +1,10 @@
1
- module Mocha
2
-
3
- class ExpectationError < StandardError
4
-
5
- LIB_DIRECTORY = File.expand_path(File.join(File.dirname(__FILE__), "..")) + File::SEPARATOR
6
-
7
- def initialize(message = nil, backtrace = [], lib_directory = LIB_DIRECTORY)
8
- super(message)
9
- filtered_backtrace = backtrace.reject { |location| Regexp.new(lib_directory).match(File.expand_path(location)) }
10
- set_backtrace(filtered_backtrace)
11
- end
1
+ # frozen_string_literal: true
12
2
 
13
- end
14
-
15
- end
3
+ module Mocha
4
+ # Default exception class raised when an unexpected invocation or an unsatisfied expectation occurs.
5
+ #
6
+ # Authors of test libraries may use +Mocha::ExpectationErrorFactory+ to have Mocha raise a different exception.
7
+ #
8
+ # @see Mocha::ExpectationErrorFactory
9
+ class ExpectationError < Exception; end # rubocop:disable Lint/InheritException
10
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/backtrace_filter'
4
+ require 'mocha/expectation_error'
5
+
6
+ module Mocha
7
+ # This factory determines what class of exception should be raised when Mocha detects a test failure.
8
+ #
9
+ # This class should only be used by authors of test libraries and not by typical "users" of Mocha.
10
+ #
11
+ # 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+.
12
+ #
13
+ # @see Mocha::Integration::Minitest::Adapter
14
+ class ExpectationErrorFactory
15
+ class << self
16
+ # @!attribute exception_class
17
+ # Determines what class of exception should be raised when Mocha detects a test failure.
18
+ #
19
+ # 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.
20
+ #
21
+ # By default a +Mocha::ExpectationError+ will be raised.
22
+ #
23
+ # @return [Exception] class of exception to be raised when an expectation error occurs
24
+ # @see Mocha::ExpectationError
25
+ attr_accessor :exception_class
26
+
27
+ # @private
28
+ def build(message = nil, backtrace = [])
29
+ exception = exception_class.new(message)
30
+ filter = BacktraceFilter.new
31
+ exception.set_backtrace(filter.filtered(backtrace))
32
+ exception
33
+ end
34
+ end
35
+ self.exception_class = ExpectationError
36
+ end
37
+ end
@@ -1,32 +1,38 @@
1
- module Mocha # :nodoc:
1
+ # frozen_string_literal: true
2
2
 
3
+ module Mocha
3
4
  class ExpectationList
4
-
5
- def initialize
6
- @expectations = []
5
+ def initialize(expectations = [])
6
+ @expectations = expectations
7
7
  end
8
8
 
9
9
  def add(expectation)
10
- @expectations << expectation
10
+ @expectations.unshift(expectation)
11
11
  expectation
12
12
  end
13
13
 
14
+ def remove_all_matching_method(method_name)
15
+ @expectations.reject! { |expectation| expectation.matches_method?(method_name) }
16
+ end
17
+
14
18
  def matches_method?(method_name)
15
19
  @expectations.any? { |expectation| expectation.matches_method?(method_name) }
16
20
  end
17
21
 
18
- def similar(method_name)
19
- @expectations.select { |expectation| expectation.matches_method?(method_name) }
22
+ def match(invocation, ignoring_order: false)
23
+ matching_expectations(invocation, ignoring_order: ignoring_order).first
20
24
  end
21
25
 
22
- def detect(method_name, *arguments)
23
- expectations = @expectations.reverse.select { |e| e.match?(method_name, *arguments) }
24
- expectation = expectations.detect { |e| e.invocations_allowed? }
25
- expectation || expectations.first
26
+ def match_allowing_invocation(invocation)
27
+ matching_expectations(invocation).detect(&:invocations_allowed?)
26
28
  end
27
29
 
28
- def verify(&block)
29
- @expectations.each { |expectation| expectation.verify(&block) }
30
+ def match_never_allowing_invocation(invocation)
31
+ matching_expectations(invocation).detect(&:invocations_never_allowed?)
32
+ end
33
+
34
+ def verified?(assertion_counter)
35
+ @expectations.all? { |expectation| expectation.verified?(assertion_counter) }
30
36
  end
31
37
 
32
38
  def to_a
@@ -41,6 +47,16 @@ module Mocha # :nodoc:
41
47
  @expectations.length
42
48
  end
43
49
 
44
- end
50
+ def any?
51
+ @expectations.any?
52
+ end
53
+
54
+ def +(other)
55
+ self.class.new(to_a + other.to_a)
56
+ end
45
57
 
58
+ def matching_expectations(invocation, ignoring_order: false)
59
+ @expectations.select { |e| e.match?(invocation, ignoring_order: ignoring_order) }
60
+ end
61
+ end
46
62
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/mockery'
4
+
5
+ module Mocha
6
+ # Integration hooks for test library authors.
7
+ #
8
+ # The methods in this module should be called from test libraries wishing to integrate with Mocha.
9
+ #
10
+ # 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.
11
+ #
12
+ # 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.
13
+ #
14
+ # 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.
15
+ #
16
+ # @see Mocha::Integration::TestUnit::Adapter
17
+ # @see Mocha::Integration::Minitest::Adapter
18
+ # @see Mocha::ExpectationErrorFactory
19
+ # @see Mocha::API
20
+ module Hooks
21
+ # @private
22
+ class NullAssertionCounter
23
+ def increment; end
24
+ end
25
+
26
+ # Prepares Mocha before a test (only for use by authors of test libraries).
27
+ #
28
+ # This method should be called before each individual test starts (including before any "setup" code).
29
+ def mocha_setup(assertion_counter = NullAssertionCounter.new)
30
+ Mockery.setup(assertion_counter)
31
+ end
32
+
33
+ # Verifies that all mock expectations have been met (only for use by authors of test libraries).
34
+ #
35
+ # This is equivalent to a series of "assertions".
36
+ #
37
+ # This method should be called at the end of each individual test, before it has been determined whether or not the test has passed.
38
+ def mocha_verify
39
+ Mockery.verify
40
+ end
41
+
42
+ # Resets Mocha after a test (only for use by authors of test libraries).
43
+ #
44
+ # This method should be called after each individual test has finished (including after any "teardown" code).
45
+ def mocha_teardown(origin = mocha_test_name)
46
+ Mockery.teardown(origin)
47
+ end
48
+
49
+ # Returns a string representing the unit test name, to be included in some Mocha
50
+ # to help track down potential bugs.
51
+ def mocha_test_name
52
+ nil
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,20 @@
1
+ module Mocha
2
+ # @private
3
+ module IgnoringWarning
4
+ def ignoring_warning(pattern, if_: true)
5
+ return yield unless if_
6
+
7
+ begin
8
+ original_warn = Warning.method(:warn)
9
+ Warning.singleton_class.define_method(:warn) do |message|
10
+ original_warn.call(message) unless message =~ pattern
11
+ end
12
+
13
+ yield
14
+ ensure
15
+ Warning.singleton_class.undef_method(:warn)
16
+ Warning.singleton_class.define_method(:warn, original_warn)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class ImpersonatingAnyInstanceName
5
+ def initialize(klass)
6
+ @klass = klass
7
+ end
8
+
9
+ def mocha_inspect
10
+ "#<AnyInstance:#{@klass.mocha_inspect}>"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class ImpersonatingName
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ def mocha_inspect
10
+ @object.mocha_inspect
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class InStateOrderingConstraint
5
+ def initialize(state_predicate)
6
+ @state_predicate = state_predicate
7
+ end
8
+
9
+ def allows_invocation_now?
10
+ @state_predicate.active?
11
+ end
12
+
13
+ def mocha_inspect
14
+ "when #{@state_predicate.mocha_inspect}"
15
+ end
16
+ end
17
+ end
data/lib/mocha/inspect.rb CHANGED
@@ -1,39 +1,73 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'date'
2
4
 
3
- class Object
4
- def mocha_inspect
5
- address = self.__id__ * 2
6
- address += 0x100000000 if address < 0
7
- inspect =~ /#</ ? "#<#{self.class}:0x#{'%x' % address}>" : inspect
5
+ module Mocha
6
+ module Inspect
7
+ module ObjectMethods
8
+ def mocha_inspect
9
+ address = __id__ * 2
10
+ address += 0x100000000 if address < 0
11
+ inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%<address>x', address: address)}>" : inspect
12
+ end
13
+ end
14
+
15
+ module ArrayMethods
16
+ def mocha_inspect(wrapped: true)
17
+ unwrapped = collect(&:mocha_inspect).join(', ')
18
+ wrapped ? "[#{unwrapped}]" : unwrapped
19
+ end
20
+ end
21
+
22
+ module HashMethods
23
+ def mocha_inspect
24
+ unwrapped = collect do |key, value|
25
+ case key
26
+ when Symbol
27
+ "#{key}: #{value.mocha_inspect}"
28
+ else
29
+ "#{key.mocha_inspect} => #{value.mocha_inspect}"
30
+ end
31
+ end.join(', ')
32
+
33
+ if Hash.ruby2_keywords_hash?(self)
34
+ empty? ? '**{}' : unwrapped
35
+ else
36
+ "{#{unwrapped}}"
37
+ end
38
+ end
39
+ end
40
+
41
+ module TimeMethods
42
+ def mocha_inspect
43
+ "#{inspect} (#{to_f} secs)"
44
+ end
45
+ end
46
+
47
+ module DateMethods
48
+ def mocha_inspect
49
+ to_s
50
+ end
51
+ end
8
52
  end
9
53
  end
10
54
 
11
- class String
12
- def mocha_inspect
13
- inspect.gsub(/\"/, "'")
14
- end
55
+ class Object
56
+ include Mocha::Inspect::ObjectMethods
15
57
  end
16
58
 
17
59
  class Array
18
- def mocha_inspect
19
- "[#{collect { |member| member.mocha_inspect }.join(', ')}]"
20
- end
60
+ include Mocha::Inspect::ArrayMethods
21
61
  end
22
62
 
23
63
  class Hash
24
- def mocha_inspect
25
- "{#{collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')}}"
26
- end
64
+ include Mocha::Inspect::HashMethods
27
65
  end
28
66
 
29
67
  class Time
30
- def mocha_inspect
31
- "#{inspect} (#{to_f} secs)"
32
- end
68
+ include Mocha::Inspect::TimeMethods
33
69
  end
34
70
 
35
71
  class Date
36
- def mocha_inspect
37
- to_s
38
- end
39
- end
72
+ include Mocha::Inspect::DateMethods
73
+ end
@@ -1,8 +1,21 @@
1
- require 'mocha/class_method'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/stubbed_method'
2
4
 
3
5
  module Mocha
6
+ class InstanceMethod < StubbedMethod
7
+ private
8
+
9
+ def stubbee
10
+ stubba_object
11
+ end
12
+
13
+ def stubbee_method(method_name)
14
+ stubba_object._method(method_name)
15
+ end
4
16
 
5
- class InstanceMethod < ClassMethod
17
+ def original_method_owner
18
+ stubba_object.singleton_class
19
+ end
6
20
  end
7
-
8
- end
21
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ module Integration
5
+ class AssertionCounter
6
+ def initialize(test_case)
7
+ @test_case = test_case
8
+ end
9
+
10
+ def increment
11
+ @test_case.assert(true)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/api'
4
+ require 'mocha/integration/assertion_counter'
5
+ require 'mocha/expectation_error_factory'
6
+
7
+ module Mocha
8
+ module Integration
9
+ # Contains {Adapter} that integrates Mocha into recent versions of Minitest.
10
+ module Minitest
11
+ # Integrates Mocha into recent versions of Minitest.
12
+ #
13
+ # See the source code for an example of how to integrate Mocha into a test library.
14
+ module Adapter
15
+ include Mocha::API
16
+
17
+ # @private
18
+ def self.applicable_to?(minitest_version)
19
+ Gem::Requirement.new('>= 3.3.0').satisfied_by?(minitest_version)
20
+ end
21
+
22
+ # @private
23
+ def self.description
24
+ 'adapter for Minitest gem >= v3.3.0'
25
+ end
26
+
27
+ # @private
28
+ def self.included(_mod)
29
+ Mocha::ExpectationErrorFactory.exception_class = ::Minitest::Assertion
30
+ end
31
+
32
+ # @private
33
+ def before_setup
34
+ assertion_counter = Integration::AssertionCounter.new(self)
35
+ mocha_setup(assertion_counter)
36
+ super
37
+ end
38
+
39
+ # @private
40
+ def before_teardown
41
+ return unless passed?
42
+
43
+ mocha_verify
44
+ ensure
45
+ super
46
+ end
47
+
48
+ # @private
49
+ def after_teardown
50
+ super
51
+ mocha_teardown
52
+ end
53
+
54
+ # @private
55
+ def mocha_test_name
56
+ if respond_to?(:name)
57
+ test_name = name
58
+ elsif respond_to?(:__name__) # Older minitest
59
+ test_name = __name__
60
+ end
61
+
62
+ if test_name
63
+ "#{self.class.name}##{test_name}"
64
+ else
65
+ self.class.name
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/detection/minitest'
4
+ require 'mocha/integration/minitest/adapter'
5
+
6
+ module Mocha
7
+ module Integration
8
+ module Minitest
9
+ def self.activate # rubocop:disable Naming/PredicateMethod
10
+ target = Detection::Minitest.testcase
11
+ return false unless target
12
+
13
+ minitest_version = Gem::Version.new(Detection::Minitest.version)
14
+ warn "Detected Minitest version: #{minitest_version}" if $DEBUG
15
+
16
+ unless Minitest::Adapter.applicable_to?(minitest_version)
17
+ raise 'Versions of minitest earlier than v3.3.0 are not supported.'
18
+ end
19
+
20
+ unless target < Minitest::Adapter
21
+ warn "Applying #{Minitest::Adapter.description}" if $DEBUG
22
+ target.send(:include, Minitest::Adapter)
23
+ end
24
+
25
+ true
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/api'
4
+
5
+ module Mocha
6
+ module Integration
7
+ module MonkeyPatcher
8
+ def self.apply(mod, run_method_patch)
9
+ if mod < Mocha::API
10
+ warn "Mocha::API already included in #{mod}" if $DEBUG
11
+ else
12
+ mod.send(:include, Mocha::API)
13
+ end
14
+ if mod.method_defined?(:run_before_mocha)
15
+ warn "#{mod}#run_before_mocha method already defined" if $DEBUG
16
+ elsif mod.method_defined?(:run)
17
+ mod.send(:alias_method, :run_before_mocha, :run)
18
+ mod.send(:remove_method, :run)
19
+ mod.send(:include, run_method_patch)
20
+ else
21
+ raise "Unable to monkey-patch #{mod}, because it does not define a `#run` method"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/api'
4
+ require 'mocha/integration/assertion_counter'
5
+ require 'mocha/expectation_error'
6
+
7
+ module Mocha
8
+ module Integration
9
+ # Contains {Adapter} that integrates Mocha into recent versions of Test::Unit.
10
+ module TestUnit
11
+ # Integrates Mocha into recent versions of Test::Unit.
12
+ #
13
+ # See the source code for an example of how to integrate Mocha into a test library.
14
+ module Adapter
15
+ include Mocha::API
16
+
17
+ # @private
18
+ def self.applicable_to?(test_unit_version)
19
+ Gem::Requirement.new('>= 2.5.1').satisfied_by?(test_unit_version)
20
+ end
21
+
22
+ # @private
23
+ def self.description
24
+ 'adapter for Test::Unit gem >= v2.5.1'
25
+ end
26
+
27
+ # @private
28
+ def self.included(mod)
29
+ mod.setup before: :prepend do
30
+ assertion_counter = Integration::AssertionCounter.new(self)
31
+ mocha_setup(assertion_counter)
32
+ end
33
+
34
+ mod.exception_handler(:handle_mocha_expectation_error)
35
+
36
+ mod.cleanup after: :append do
37
+ mocha_verify
38
+ end
39
+
40
+ mod.teardown :mocha_teardown, after: :append
41
+ end
42
+
43
+ private
44
+
45
+ # @private
46
+ def mocha_test_name
47
+ name
48
+ end
49
+
50
+ # @private
51
+ def handle_mocha_expectation_error(exception) # rubocop:disable Naming/PredicateMethod
52
+ return false unless exception.is_a?(Mocha::ExpectationError)
53
+
54
+ problem_occurred
55
+ add_failure(exception.message, exception.backtrace)
56
+ true
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/detection/test_unit'
4
+ require 'mocha/integration/test_unit/adapter'
5
+
6
+ module Mocha
7
+ module Integration
8
+ module TestUnit
9
+ def self.activate # rubocop:disable Naming/PredicateMethod
10
+ target = Detection::TestUnit.testcase
11
+ return false unless target
12
+
13
+ test_unit_version = Gem::Version.new(Detection::TestUnit.version)
14
+ warn "Detected Test::Unit version: #{test_unit_version}" if $DEBUG
15
+
16
+ unless TestUnit::Adapter.applicable_to?(test_unit_version)
17
+ raise 'Versions of test-unit earlier than v2.5.1 are not supported.'
18
+ end
19
+
20
+ unless target < TestUnit::Adapter
21
+ warn "Applying #{TestUnit::Adapter.description}" if $DEBUG
22
+ target.send(:include, TestUnit::Adapter)
23
+ end
24
+
25
+ true
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ module Mocha
2
+ # Contains adapters that provide built-in support for +Minitest+ and +Test::Unit+.
3
+ module Integration
4
+ end
5
+ end