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.
- checksums.yaml +7 -0
- data/.gemtest +0 -0
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +92 -0
- data/.rubocop_todo.yml +39 -0
- data/.yardopts +25 -0
- data/CONTRIBUTING.md +7 -0
- data/COPYING.md +3 -0
- data/Gemfile +17 -0
- data/{MIT-LICENSE → MIT-LICENSE.md} +2 -2
- data/README.md +361 -0
- data/RELEASE.md +1235 -0
- data/Rakefile +165 -123
- data/gemfiles/Gemfile.minitest.latest +8 -0
- data/gemfiles/Gemfile.rubocop +9 -0
- data/gemfiles/Gemfile.test-unit.latest +8 -0
- data/lib/mocha/any_instance_method.rb +12 -26
- data/lib/mocha/any_instance_receiver.rb +20 -0
- data/lib/mocha/api.rb +213 -0
- data/lib/mocha/argument_iterator.rb +17 -0
- data/lib/mocha/backtrace_filter.rb +15 -0
- data/lib/mocha/block_matchers.rb +33 -0
- data/lib/mocha/cardinality.rb +110 -0
- data/lib/mocha/central.rb +33 -22
- data/lib/mocha/change_state_side_effect.rb +17 -0
- data/lib/mocha/class_methods.rb +67 -0
- data/lib/mocha/configuration.rb +338 -0
- data/lib/mocha/default_name.rb +15 -0
- data/lib/mocha/default_receiver.rb +13 -0
- data/lib/mocha/deprecation.rb +19 -14
- data/lib/mocha/detection/minitest.rb +25 -0
- data/lib/mocha/detection/test_unit.rb +30 -0
- data/lib/mocha/error_with_filtered_backtrace.rb +15 -0
- data/lib/mocha/exception_raiser.rb +11 -10
- data/lib/mocha/expectation.rb +553 -168
- data/lib/mocha/expectation_error.rb +9 -14
- data/lib/mocha/expectation_error_factory.rb +37 -0
- data/lib/mocha/expectation_list.rb +30 -14
- data/lib/mocha/hooks.rb +55 -0
- data/lib/mocha/ignoring_warning.rb +20 -0
- data/lib/mocha/impersonating_any_instance_name.rb +13 -0
- data/lib/mocha/impersonating_name.rb +13 -0
- data/lib/mocha/in_state_ordering_constraint.rb +17 -0
- data/lib/mocha/inspect.rb +56 -22
- data/lib/mocha/instance_method.rb +17 -4
- data/lib/mocha/integration/assertion_counter.rb +15 -0
- data/lib/mocha/integration/minitest/adapter.rb +71 -0
- data/lib/mocha/integration/minitest.rb +29 -0
- data/lib/mocha/integration/monkey_patcher.rb +26 -0
- data/lib/mocha/integration/test_unit/adapter.rb +61 -0
- data/lib/mocha/integration/test_unit.rb +29 -0
- data/lib/mocha/integration.rb +5 -0
- data/lib/mocha/invocation.rb +76 -0
- data/lib/mocha/logger.rb +13 -0
- data/lib/mocha/macos_version.rb +7 -0
- data/lib/mocha/method_matcher.rb +8 -10
- data/lib/mocha/minitest.rb +7 -0
- data/lib/mocha/mock.rb +333 -108
- data/lib/mocha/mockery.rb +199 -0
- data/lib/mocha/name.rb +13 -0
- data/lib/mocha/not_initialized_error.rb +9 -0
- data/lib/mocha/object_methods.rb +183 -0
- data/lib/mocha/object_receiver.rb +20 -0
- data/lib/mocha/parameter_matchers/all_of.rb +38 -28
- data/lib/mocha/parameter_matchers/any_of.rb +44 -33
- data/lib/mocha/parameter_matchers/any_parameters.rb +33 -26
- data/lib/mocha/parameter_matchers/anything.rb +31 -22
- data/lib/mocha/parameter_matchers/base_methods.rb +64 -0
- data/lib/mocha/parameter_matchers/equals.rb +36 -25
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +65 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +48 -29
- data/lib/mocha/parameter_matchers/has_entry.rb +90 -42
- data/lib/mocha/parameter_matchers/has_key.rb +39 -26
- data/lib/mocha/parameter_matchers/has_keys.rb +59 -0
- data/lib/mocha/parameter_matchers/has_value.rb +39 -26
- data/lib/mocha/parameter_matchers/includes.rb +88 -23
- data/lib/mocha/parameter_matchers/instance_methods.rb +28 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +37 -26
- data/lib/mocha/parameter_matchers/is_a.rb +38 -26
- data/lib/mocha/parameter_matchers/kind_of.rb +39 -26
- data/lib/mocha/parameter_matchers/not.rb +37 -26
- data/lib/mocha/parameter_matchers/optionally.rb +52 -17
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +91 -0
- data/lib/mocha/parameter_matchers/regexp_matches.rb +37 -25
- data/lib/mocha/parameter_matchers/responds_with.rb +82 -0
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +55 -0
- data/lib/mocha/parameter_matchers.rb +12 -5
- data/lib/mocha/parameters_matcher.rb +28 -19
- data/lib/mocha/raised_exception.rb +13 -0
- data/lib/mocha/return_values.rb +13 -18
- data/lib/mocha/ruby_version.rb +7 -0
- data/lib/mocha/sequence.rb +23 -17
- data/lib/mocha/single_return_value.rb +8 -18
- data/lib/mocha/state_machine.rb +95 -0
- data/lib/mocha/stubbed_method.rb +96 -0
- data/lib/mocha/stubbing_error.rb +10 -0
- data/lib/mocha/test_unit.rb +7 -0
- data/lib/mocha/thrower.rb +15 -0
- data/lib/mocha/thrown_object.rb +14 -0
- data/lib/mocha/version.rb +5 -0
- data/lib/mocha/yield_parameters.rb +12 -20
- data/lib/mocha.rb +19 -17
- data/mise.toml +2 -0
- data/mocha.gemspec +40 -0
- metadata +130 -145
- data/COPYING +0 -3
- data/README +0 -35
- data/RELEASE +0 -188
- data/examples/misc.rb +0 -44
- data/examples/mocha.rb +0 -26
- data/examples/stubba.rb +0 -65
- data/lib/mocha/auto_verify.rb +0 -118
- data/lib/mocha/class_method.rb +0 -66
- data/lib/mocha/infinite_range.rb +0 -25
- data/lib/mocha/is_a.rb +0 -9
- data/lib/mocha/metaclass.rb +0 -7
- data/lib/mocha/missing_expectation.rb +0 -17
- data/lib/mocha/multiple_yields.rb +0 -20
- data/lib/mocha/no_yields.rb +0 -11
- data/lib/mocha/object.rb +0 -110
- data/lib/mocha/parameter_matchers/base.rb +0 -15
- data/lib/mocha/parameter_matchers/object.rb +0 -9
- data/lib/mocha/pretty_parameters.rb +0 -28
- data/lib/mocha/setup_and_teardown.rb +0 -23
- data/lib/mocha/single_yield.rb +0 -18
- data/lib/mocha/standalone.rb +0 -32
- data/lib/mocha/stub.rb +0 -18
- data/lib/mocha/test_case_adapter.rb +0 -49
- data/lib/mocha_standalone.rb +0 -2
- data/lib/stubba.rb +0 -2
- data/test/acceptance/expected_invocation_count_acceptance_test.rb +0 -187
- data/test/acceptance/mocha_acceptance_test.rb +0 -98
- data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +0 -44
- data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +0 -71
- data/test/acceptance/optional_parameters_acceptance_test.rb +0 -63
- data/test/acceptance/parameter_matcher_acceptance_test.rb +0 -117
- data/test/acceptance/partial_mocks_acceptance_test.rb +0 -40
- data/test/acceptance/sequence_acceptance_test.rb +0 -179
- data/test/acceptance/standalone_acceptance_test.rb +0 -131
- data/test/acceptance/stubba_acceptance_test.rb +0 -102
- data/test/active_record_test_case.rb +0 -36
- data/test/deprecation_disabler.rb +0 -15
- data/test/execution_point.rb +0 -34
- data/test/integration/mocha_test_result_integration_test.rb +0 -105
- data/test/integration/stubba_integration_test.rb +0 -89
- data/test/integration/stubba_test_result_integration_test.rb +0 -85
- data/test/method_definer.rb +0 -18
- data/test/test_helper.rb +0 -12
- data/test/test_runner.rb +0 -31
- data/test/unit/any_instance_method_test.rb +0 -126
- data/test/unit/array_inspect_test.rb +0 -16
- data/test/unit/auto_verify_test.rb +0 -129
- data/test/unit/central_test.rb +0 -124
- data/test/unit/class_method_test.rb +0 -200
- data/test/unit/date_time_inspect_test.rb +0 -21
- data/test/unit/expectation_error_test.rb +0 -24
- data/test/unit/expectation_list_test.rb +0 -75
- data/test/unit/expectation_raiser_test.rb +0 -28
- data/test/unit/expectation_test.rb +0 -483
- data/test/unit/hash_inspect_test.rb +0 -16
- data/test/unit/infinite_range_test.rb +0 -53
- data/test/unit/metaclass_test.rb +0 -22
- data/test/unit/method_matcher_test.rb +0 -23
- data/test/unit/missing_expectation_test.rb +0 -42
- data/test/unit/mock_test.rb +0 -323
- data/test/unit/multiple_yields_test.rb +0 -18
- data/test/unit/no_yield_test.rb +0 -18
- data/test/unit/object_inspect_test.rb +0 -37
- data/test/unit/object_test.rb +0 -165
- data/test/unit/parameter_matchers/all_of_test.rb +0 -26
- data/test/unit/parameter_matchers/any_of_test.rb +0 -26
- data/test/unit/parameter_matchers/anything_test.rb +0 -21
- data/test/unit/parameter_matchers/has_entries_test.rb +0 -30
- data/test/unit/parameter_matchers/has_entry_test.rb +0 -40
- data/test/unit/parameter_matchers/has_key_test.rb +0 -25
- data/test/unit/parameter_matchers/has_value_test.rb +0 -25
- data/test/unit/parameter_matchers/includes_test.rb +0 -25
- data/test/unit/parameter_matchers/instance_of_test.rb +0 -25
- data/test/unit/parameter_matchers/is_a_test.rb +0 -25
- data/test/unit/parameter_matchers/kind_of_test.rb +0 -25
- data/test/unit/parameter_matchers/not_test.rb +0 -26
- data/test/unit/parameter_matchers/regexp_matches_test.rb +0 -25
- data/test/unit/parameter_matchers/stub_matcher.rb +0 -23
- data/test/unit/parameters_matcher_test.rb +0 -121
- data/test/unit/return_values_test.rb +0 -63
- data/test/unit/sequence_test.rb +0 -104
- data/test/unit/setup_and_teardown_test.rb +0 -76
- data/test/unit/single_return_value_test.rb +0 -33
- data/test/unit/single_yield_test.rb +0 -18
- data/test/unit/string_inspect_test.rb +0 -11
- data/test/unit/stub_test.rb +0 -24
- data/test/unit/yield_parameters_test.rb +0 -93
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
module Mocha
|
|
3
4
|
class ExpectationList
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
@expectations = []
|
|
5
|
+
def initialize(expectations = [])
|
|
6
|
+
@expectations = expectations
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def add(expectation)
|
|
10
|
-
@expectations
|
|
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
|
|
19
|
-
|
|
22
|
+
def match(invocation, ignoring_order: false)
|
|
23
|
+
matching_expectations(invocation, ignoring_order: ignoring_order).first
|
|
20
24
|
end
|
|
21
25
|
|
|
22
|
-
def
|
|
23
|
-
|
|
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
|
|
29
|
-
|
|
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
|
-
|
|
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
|
data/lib/mocha/hooks.rb
ADDED
|
@@ -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,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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
12
|
-
|
|
13
|
-
inspect.gsub(/\"/, "'")
|
|
14
|
-
end
|
|
55
|
+
class Object
|
|
56
|
+
include Mocha::Inspect::ObjectMethods
|
|
15
57
|
end
|
|
16
58
|
|
|
17
59
|
class Array
|
|
18
|
-
|
|
19
|
-
"[#{collect { |member| member.mocha_inspect }.join(', ')}]"
|
|
20
|
-
end
|
|
60
|
+
include Mocha::Inspect::ArrayMethods
|
|
21
61
|
end
|
|
22
62
|
|
|
23
63
|
class Hash
|
|
24
|
-
|
|
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
|
-
|
|
31
|
-
"#{inspect} (#{to_f} secs)"
|
|
32
|
-
end
|
|
68
|
+
include Mocha::Inspect::TimeMethods
|
|
33
69
|
end
|
|
34
70
|
|
|
35
71
|
class Date
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
end
|
|
39
|
-
end
|
|
72
|
+
include Mocha::Inspect::DateMethods
|
|
73
|
+
end
|
|
@@ -1,8 +1,21 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
17
|
+
def original_method_owner
|
|
18
|
+
stubba_object.singleton_class
|
|
19
|
+
end
|
|
6
20
|
end
|
|
7
|
-
|
|
8
|
-
end
|
|
21
|
+
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
|