mocha 0.10.5 → 1.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/FUNDING.yml +1 -0
- data/.rubocop.yml +61 -0
- data/.rubocop_todo.yml +27 -0
- data/.yardopts +24 -0
- data/CONTRIBUTING.md +7 -0
- data/COPYING.md +3 -0
- data/Gemfile +2 -2
- data/{MIT-LICENSE.rdoc → MIT-LICENSE.md} +1 -1
- data/README.md +363 -0
- data/{RELEASE.rdoc → RELEASE.md} +436 -35
- data/Rakefile +87 -87
- data/gemfiles/Gemfile.minitest.latest +2 -2
- data/gemfiles/Gemfile.test-unit.latest +6 -2
- data/init.rb +1 -3
- data/lib/mocha/any_instance_method.rb +12 -45
- data/lib/mocha/api.rb +199 -131
- data/lib/mocha/argument_iterator.rb +6 -10
- data/lib/mocha/backtrace_filter.rb +1 -5
- data/lib/mocha/block_matcher.rb +31 -0
- data/lib/mocha/cardinality.rb +77 -66
- data/lib/mocha/central.rb +27 -18
- data/lib/mocha/change_state_side_effect.rb +3 -7
- data/lib/mocha/class_methods.rb +62 -0
- data/lib/mocha/configuration.rb +399 -46
- data/lib/mocha/debug.rb +12 -0
- data/lib/mocha/deprecation.rb +11 -12
- data/lib/mocha/detection/mini_test.rb +23 -0
- data/lib/mocha/detection/test_unit.rb +27 -0
- data/lib/mocha/error_with_filtered_backtrace.rb +13 -0
- data/lib/mocha/exception_raiser.rb +8 -10
- data/lib/mocha/expectation.rb +290 -151
- data/lib/mocha/expectation_error.rb +6 -13
- data/lib/mocha/expectation_error_factory.rb +35 -0
- data/lib/mocha/expectation_list.rb +22 -22
- data/lib/mocha/hooks.rb +42 -0
- data/lib/mocha/in_state_ordering_constraint.rb +3 -7
- data/lib/mocha/inspect.rb +35 -43
- data/lib/mocha/instance_method.rb +12 -21
- data/lib/mocha/integration/assertion_counter.rb +13 -0
- data/lib/mocha/integration/mini_test/adapter.rb +52 -0
- data/lib/mocha/integration/mini_test/exception_translation.rb +1 -7
- data/lib/mocha/integration/mini_test/nothing.rb +19 -0
- data/lib/mocha/integration/mini_test/version_13.rb +35 -25
- data/lib/mocha/integration/mini_test/version_140.rb +35 -26
- data/lib/mocha/integration/mini_test/version_141.rb +43 -34
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +44 -35
- data/lib/mocha/integration/mini_test/version_200.rb +45 -36
- data/lib/mocha/integration/mini_test/version_201_to_222.rb +44 -35
- data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +70 -0
- data/lib/mocha/integration/mini_test/version_2112_to_320.rb +73 -0
- data/lib/mocha/integration/mini_test/version_230_to_2101.rb +68 -0
- data/lib/mocha/integration/mini_test.rb +51 -49
- data/lib/mocha/integration/monkey_patcher.rb +24 -0
- data/lib/mocha/integration/test_unit/adapter.rb +50 -0
- data/lib/mocha/integration/test_unit/gem_version_200.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +39 -29
- data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +68 -0
- data/lib/mocha/integration/test_unit/nothing.rb +19 -0
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +39 -29
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +40 -30
- data/lib/mocha/integration/test_unit.rb +45 -51
- data/lib/mocha/integration.rb +6 -33
- data/lib/mocha/invocation.rb +77 -0
- data/lib/mocha/is_a.rb +0 -2
- data/lib/mocha/logger.rb +2 -6
- data/lib/mocha/macos_version.rb +5 -0
- data/lib/mocha/method_matcher.rb +6 -10
- data/lib/mocha/minitest.rb +8 -0
- data/lib/mocha/mock.rb +266 -79
- data/lib/mocha/mockery.rb +104 -106
- data/lib/mocha/names.rb +10 -20
- data/lib/mocha/not_initialized_error.rb +7 -0
- data/lib/mocha/object_methods.rb +169 -0
- data/lib/mocha/parameter_matchers/all_of.rb +18 -14
- data/lib/mocha/parameter_matchers/any_of.rb +19 -14
- data/lib/mocha/parameter_matchers/any_parameters.rb +14 -13
- data/lib/mocha/parameter_matchers/anything.rb +17 -14
- data/lib/mocha/parameter_matchers/base.rb +33 -31
- data/lib/mocha/parameter_matchers/equals.rb +18 -13
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +58 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +19 -14
- data/lib/mocha/parameter_matchers/has_entry.rb +58 -26
- data/lib/mocha/parameter_matchers/has_key.rb +18 -13
- data/lib/mocha/parameter_matchers/has_keys.rb +53 -0
- data/lib/mocha/parameter_matchers/has_value.rb +18 -13
- data/lib/mocha/parameter_matchers/includes.rb +80 -19
- data/lib/mocha/parameter_matchers/instance_methods.rb +18 -0
- data/lib/mocha/parameter_matchers/instance_of.rb +18 -13
- data/lib/mocha/parameter_matchers/is_a.rb +20 -14
- data/lib/mocha/parameter_matchers/kind_of.rb +20 -13
- data/lib/mocha/parameter_matchers/not.rb +19 -14
- data/lib/mocha/parameter_matchers/optionally.rb +23 -17
- data/lib/mocha/parameter_matchers/regexp_matches.rb +16 -12
- data/lib/mocha/parameter_matchers/responds_with.rb +17 -11
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +15 -9
- data/lib/mocha/parameter_matchers.rb +4 -5
- data/lib/mocha/parameters_matcher.rb +11 -14
- data/lib/mocha/raised_exception.rb +11 -0
- data/lib/mocha/receivers.rb +45 -0
- data/lib/mocha/return_values.rb +11 -15
- data/lib/mocha/ruby_version.rb +4 -0
- data/lib/mocha/sequence.rb +21 -17
- data/lib/mocha/setup.rb +14 -0
- data/lib/mocha/single_return_value.rb +5 -8
- data/lib/mocha/singleton_class.rb +9 -0
- data/lib/mocha/state_machine.rb +69 -67
- data/lib/mocha/stubbed_method.rb +125 -0
- data/lib/mocha/stubbing_error.rb +6 -14
- data/lib/mocha/test_unit.rb +8 -0
- data/lib/mocha/thrower.rb +6 -8
- data/lib/mocha/thrown_object.rb +12 -0
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +12 -22
- data/lib/mocha.rb +8 -3
- data/mocha.gemspec +43 -34
- data/yard-templates/default/layout/html/google_analytics.erb +8 -0
- data/yard-templates/default/layout/html/setup.rb +5 -0
- metadata +123 -268
- data/COPYING.rdoc +0 -3
- data/README.rdoc +0 -54
- data/examples/misc.rb +0 -43
- data/examples/mocha.rb +0 -25
- data/examples/stubba.rb +0 -64
- data/gemfiles/Gemfile.minitest.1.3.0 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.0 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.1 +0 -7
- data/gemfiles/Gemfile.minitest.1.4.2 +0 -7
- data/gemfiles/Gemfile.minitest.2.0.0 +0 -7
- data/gemfiles/Gemfile.minitest.2.0.1 +0 -7
- data/gemfiles/Gemfile.minitest.2.3.0 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.0 +0 -8
- data/gemfiles/Gemfile.test-unit.2.0.1 +0 -7
- data/gemfiles/Gemfile.test-unit.2.0.3 +0 -7
- data/lib/mocha/class_method.rb +0 -98
- data/lib/mocha/integration/mini_test/assertion_counter.rb +0 -23
- data/lib/mocha/integration/mini_test/version_230_to_262.rb +0 -59
- data/lib/mocha/integration/test_unit/assertion_counter.rb +0 -23
- data/lib/mocha/integration/test_unit/gem_version_230_to_240.rb +0 -58
- data/lib/mocha/module_method.rb +0 -16
- data/lib/mocha/multiple_yields.rb +0 -20
- data/lib/mocha/no_yields.rb +0 -11
- data/lib/mocha/object.rb +0 -223
- data/lib/mocha/options.rb +0 -1
- data/lib/mocha/parameter_matchers/object.rb +0 -15
- data/lib/mocha/parameter_matchers/query_string.rb +0 -47
- data/lib/mocha/pretty_parameters.rb +0 -28
- data/lib/mocha/single_yield.rb +0 -18
- data/lib/mocha/standalone.rb +0 -1
- data/lib/mocha/unexpected_invocation.rb +0 -18
- data/lib/mocha_standalone.rb +0 -2
- data/lib/stubba.rb +0 -4
- data/test/acceptance/acceptance_test_helper.rb +0 -41
- data/test/acceptance/api_test.rb +0 -139
- data/test/acceptance/bug_18914_test.rb +0 -43
- data/test/acceptance/bug_21465_test.rb +0 -34
- data/test/acceptance/bug_21563_test.rb +0 -25
- data/test/acceptance/exception_rescue_test.rb +0 -55
- data/test/acceptance/expectations_on_multiple_methods_test.rb +0 -55
- data/test/acceptance/expected_invocation_count_test.rb +0 -232
- data/test/acceptance/failure_messages_test.rb +0 -64
- data/test/acceptance/issue_65_test.rb +0 -63
- data/test/acceptance/issue_70_test.rb +0 -55
- data/test/acceptance/minitest_test.rb +0 -162
- data/test/acceptance/mocha_example_test.rb +0 -98
- data/test/acceptance/mocha_test_result_test.rb +0 -84
- data/test/acceptance/mock_test.rb +0 -100
- data/test/acceptance/mock_with_initializer_block_test.rb +0 -51
- data/test/acceptance/mocked_methods_dispatch_test.rb +0 -78
- data/test/acceptance/multiple_expectations_failure_message_test.rb +0 -68
- data/test/acceptance/optional_parameters_test.rb +0 -70
- data/test/acceptance/parameter_matcher_test.rb +0 -300
- data/test/acceptance/partial_mocks_test.rb +0 -47
- data/test/acceptance/raise_exception_test.rb +0 -39
- data/test/acceptance/return_value_test.rb +0 -52
- data/test/acceptance/sequence_test.rb +0 -192
- data/test/acceptance/states_test.rb +0 -70
- data/test/acceptance/stub_any_instance_method_test.rb +0 -198
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +0 -106
- data/test/acceptance/stub_class_method_defined_on_class_test.rb +0 -72
- data/test/acceptance/stub_class_method_defined_on_module_test.rb +0 -75
- data/test/acceptance/stub_class_method_defined_on_superclass_test.rb +0 -75
- data/test/acceptance/stub_everything_test.rb +0 -56
- data/test/acceptance/stub_instance_method_defined_on_active_record_association_proxy_test.rb +0 -93
- data/test/acceptance/stub_instance_method_defined_on_class_and_aliased_test.rb +0 -69
- data/test/acceptance/stub_instance_method_defined_on_class_test.rb +0 -66
- data/test/acceptance/stub_instance_method_defined_on_kernel_module_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_module_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_object_class_test.rb +0 -75
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +0 -70
- data/test/acceptance/stub_instance_method_defined_on_superclass_test.rb +0 -72
- data/test/acceptance/stub_module_method_test.rb +0 -163
- data/test/acceptance/stub_test.rb +0 -52
- data/test/acceptance/stubba_example_test.rb +0 -102
- data/test/acceptance/stubba_test.rb +0 -15
- data/test/acceptance/stubba_test_result_test.rb +0 -66
- data/test/acceptance/stubbing_error_backtrace_test.rb +0 -64
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +0 -65
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +0 -130
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +0 -157
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +0 -147
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +0 -130
- data/test/acceptance/stubbing_non_public_class_method_test.rb +0 -163
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +0 -143
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +0 -64
- data/test/acceptance/throw_test.rb +0 -45
- data/test/acceptance/unstubbing_test.rb +0 -151
- data/test/deprecation_disabler.rb +0 -15
- data/test/execution_point.rb +0 -36
- data/test/method_definer.rb +0 -24
- data/test/mini_test_result.rb +0 -83
- data/test/simple_counter.rb +0 -13
- data/test/test_helper.rb +0 -11
- data/test/test_runner.rb +0 -50
- data/test/unit/any_instance_method_test.rb +0 -136
- data/test/unit/array_inspect_test.rb +0 -16
- data/test/unit/backtrace_filter_test.rb +0 -19
- data/test/unit/cardinality_test.rb +0 -56
- data/test/unit/central_test.rb +0 -100
- data/test/unit/change_state_side_effect_test.rb +0 -41
- data/test/unit/class_method_test.rb +0 -260
- data/test/unit/configuration_test.rb +0 -38
- data/test/unit/date_time_inspect_test.rb +0 -21
- data/test/unit/exception_raiser_test.rb +0 -42
- data/test/unit/expectation_list_test.rb +0 -71
- data/test/unit/expectation_test.rb +0 -480
- data/test/unit/hash_inspect_test.rb +0 -16
- data/test/unit/in_state_ordering_constraint_test.rb +0 -43
- data/test/unit/method_matcher_test.rb +0 -23
- data/test/unit/mock_test.rb +0 -312
- data/test/unit/mockery_test.rb +0 -150
- data/test/unit/multiple_yields_test.rb +0 -18
- data/test/unit/no_yields_test.rb +0 -18
- data/test/unit/object_inspect_test.rb +0 -38
- data/test/unit/object_test.rb +0 -87
- 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/equals_test.rb +0 -25
- data/test/unit/parameter_matchers/has_entries_test.rb +0 -51
- data/test/unit/parameter_matchers/has_entry_test.rb +0 -96
- data/test/unit/parameter_matchers/has_key_test.rb +0 -55
- data/test/unit/parameter_matchers/has_value_test.rb +0 -57
- data/test/unit/parameter_matchers/includes_test.rb +0 -44
- 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 -46
- data/test/unit/parameter_matchers/responds_with_test.rb +0 -25
- data/test/unit/parameter_matchers/stub_matcher.rb +0 -27
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +0 -25
- 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/single_return_value_test.rb +0 -14
- data/test/unit/single_yield_test.rb +0 -18
- data/test/unit/state_machine_test.rb +0 -98
- data/test/unit/string_inspect_test.rb +0 -11
- data/test/unit/thrower_test.rb +0 -20
- 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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
2
|
-
|
1
|
+
module Mocha
|
3
2
|
class ExpectationList
|
4
|
-
|
5
|
-
|
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(
|
23
|
-
matching_expectations(
|
19
|
+
|
20
|
+
def match(invocation)
|
21
|
+
matching_expectations(invocation).first
|
24
22
|
end
|
25
|
-
|
26
|
-
def match_allowing_invocation(
|
27
|
-
matching_expectations(
|
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(
|
53
|
-
@expectations.select { |e| e.match?(
|
53
|
+
|
54
|
+
def matching_expectations(invocation)
|
55
|
+
@expectations.select { |e| e.match?(invocation) }
|
54
56
|
end
|
55
|
-
|
56
57
|
end
|
57
|
-
|
58
58
|
end
|
data/lib/mocha/hooks.rb
ADDED
@@ -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
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
|
27
|
+
module TimeMethods
|
28
|
+
def mocha_inspect
|
29
|
+
"#{inspect} (#{to_f} secs)"
|
30
|
+
end
|
34
31
|
end
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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/
|
1
|
+
require 'mocha/stubbed_method'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
+
class InstanceMethod < StubbedMethod
|
5
|
+
private
|
4
6
|
|
5
|
-
|
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
|
12
|
-
|
11
|
+
def method_body(method)
|
12
|
+
PRE_RUBY_V19 ? proc { |*args, &block| method.call(*args, &block) } : method
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
|
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
|
23
|
-
|
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,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.
|
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/
|
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
|
-
|
18
|
+
MonkeyPatcher.apply(mod, RunMethodPatch)
|
12
19
|
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
|
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
|
-
|
34
|
-
mocha_teardown
|
47
|
+
result
|
35
48
|
end
|
36
|
-
|
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/
|
2
|
-
require 'mocha/
|
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
|
-
|
18
|
+
MonkeyPatcher.apply(mod, RunMethodPatch)
|
13
19
|
end
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
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
|
-
|
35
|
-
mocha_teardown
|
47
|
+
result
|
36
48
|
end
|
37
|
-
|
49
|
+
# rubocop:enable all
|
38
50
|
end
|
39
51
|
end
|
40
|
-
|
41
52
|
end
|
42
|
-
|
43
53
|
end
|
44
|
-
|
45
54
|
end
|