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,12 +1,14 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
5
4
|
module ParameterMatchers
|
6
|
-
|
7
|
-
#
|
5
|
+
# Matches any parameters. This is used as the default for a newly built expectation.
|
6
|
+
#
|
7
|
+
# @return [AnyParameters] parameter matcher.
|
8
8
|
#
|
9
|
-
#
|
9
|
+
# @see Expectation#with
|
10
|
+
#
|
11
|
+
# @example Any parameters will match.
|
10
12
|
# object = mock()
|
11
13
|
# object.expects(:method_1).with(any_parameters)
|
12
14
|
# object.method_1(1, 2, 3, 4)
|
@@ -20,21 +22,20 @@ module Mocha
|
|
20
22
|
AnyParameters.new
|
21
23
|
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
+
# Parameter matcher which always matches whatever the parameters.
|
26
|
+
class AnyParameters < Base
|
27
|
+
# @private
|
25
28
|
def matches?(available_parameters)
|
26
|
-
|
29
|
+
until available_parameters.empty?
|
27
30
|
available_parameters.shift
|
28
31
|
end
|
29
|
-
|
32
|
+
true
|
30
33
|
end
|
31
34
|
|
35
|
+
# @private
|
32
36
|
def mocha_inspect
|
33
|
-
|
37
|
+
'any_parameters'
|
34
38
|
end
|
35
|
-
|
36
39
|
end
|
37
|
-
|
38
40
|
end
|
39
|
-
|
40
|
-
end
|
41
|
+
end
|
@@ -1,33 +1,36 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
5
4
|
module ParameterMatchers
|
6
|
-
|
7
|
-
# :call-seq: anything() -> parameter_matcher
|
8
|
-
#
|
9
5
|
# Matches any object.
|
6
|
+
#
|
7
|
+
# @return [Anything] parameter matcher.
|
8
|
+
#
|
9
|
+
# @see Expectation#with
|
10
|
+
#
|
11
|
+
# @example Any object will match.
|
10
12
|
# object = mock()
|
11
13
|
# object.expects(:method_1).with(anything)
|
12
14
|
# object.method_1('foo')
|
15
|
+
# object.method_1(789)
|
16
|
+
# object.method_1(:bar)
|
13
17
|
# # no error raised
|
14
18
|
def anything
|
15
19
|
Anything.new
|
16
20
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
21
|
+
|
22
|
+
# Parameter matcher which always matches a single parameter.
|
23
|
+
class Anything < Base
|
24
|
+
# @private
|
20
25
|
def matches?(available_parameters)
|
21
26
|
available_parameters.shift
|
22
|
-
|
27
|
+
true
|
23
28
|
end
|
24
|
-
|
29
|
+
|
30
|
+
# @private
|
25
31
|
def mocha_inspect
|
26
|
-
|
32
|
+
'anything'
|
27
33
|
end
|
28
|
-
|
29
34
|
end
|
30
|
-
|
31
35
|
end
|
32
|
-
|
33
|
-
end
|
36
|
+
end
|
@@ -1,63 +1,65 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
3
2
|
module ParameterMatchers
|
4
|
-
|
5
|
-
class Base
|
6
|
-
|
7
|
-
def to_matcher
|
3
|
+
# @abstract Subclass and implement +#matches?+ and +#mocha_inspect+ to define a custom matcher. Also add a suitably named instance method to {ParameterMatchers} to build an instance of the new matcher c.f. {#equals}.
|
4
|
+
class Base
|
5
|
+
# @private
|
6
|
+
def to_matcher
|
8
7
|
self
|
9
8
|
end
|
10
|
-
|
11
|
-
#
|
9
|
+
|
10
|
+
# A shorthand way of combining two matchers when both must match.
|
12
11
|
#
|
13
|
-
#
|
14
|
-
# all match.
|
12
|
+
# Returns a new {AllOf} parameter matcher combining two matchers using a logical AND.
|
15
13
|
#
|
16
|
-
#
|
17
|
-
# given matcher and the receiver.
|
14
|
+
# This shorthand will not work with an implicit equals match. Instead, an explicit {Equals} matcher should be used.
|
18
15
|
#
|
19
|
-
#
|
16
|
+
# @param [Base] other parameter matcher.
|
17
|
+
# @return [AllOf] parameter matcher.
|
18
|
+
#
|
19
|
+
# @see Expectation#with
|
20
|
+
#
|
21
|
+
# @example Alternative ways to combine matchers with a logical AND.
|
20
22
|
# object = mock()
|
21
23
|
# object.expects(:run).with(all_of(has_key(:foo), has_key(:bar)))
|
22
24
|
# object.run(:foo => 'foovalue', :bar => 'barvalue')
|
23
25
|
#
|
24
|
-
# #
|
26
|
+
# # is exactly equivalent to
|
27
|
+
#
|
25
28
|
# object.expects(:run).with(has_key(:foo) & has_key(:bar))
|
26
29
|
# object.run(:foo => 'foovalue', :bar => 'barvalue)
|
27
|
-
def &(
|
28
|
-
AllOf.new(self,
|
30
|
+
def &(other)
|
31
|
+
AllOf.new(self, other)
|
29
32
|
end
|
30
|
-
|
31
|
-
#
|
33
|
+
|
34
|
+
# A shorthand way of combining two matchers when at least one must match.
|
35
|
+
#
|
36
|
+
# Returns a new +AnyOf+ parameter matcher combining two matchers using a logical OR.
|
32
37
|
#
|
33
|
-
#
|
34
|
-
# one of which should pass.
|
38
|
+
# This shorthand will not work with an implicit equals match. Instead, an explicit {Equals} matcher should be used.
|
35
39
|
#
|
36
|
-
#
|
37
|
-
#
|
40
|
+
# @param [Base] other parameter matcher.
|
41
|
+
# @return [AnyOf] parameter matcher.
|
38
42
|
#
|
39
|
-
#
|
43
|
+
# @see Expectation#with
|
44
|
+
#
|
45
|
+
# @example Alternative ways to combine matchers with a logical OR.
|
40
46
|
# object = mock()
|
41
47
|
# object.expects(:run).with(any_of(has_key(:foo), has_key(:bar)))
|
42
48
|
# object.run(:foo => 'foovalue')
|
43
49
|
#
|
44
|
-
# #
|
50
|
+
# # is exactly equivalent to
|
51
|
+
#
|
45
52
|
# object.expects(:run).with(has_key(:foo) | has_key(:bar))
|
46
53
|
# object.run(:foo => 'foovalue')
|
47
54
|
#
|
48
|
-
#
|
49
|
-
# an explicit equals matcher should be used:
|
50
|
-
#
|
55
|
+
# @example Using an explicit {Equals} matcher in combination with {#|}.
|
51
56
|
# object.expects(:run).with(equals(1) | equals(2))
|
52
57
|
# object.run(1) # passes
|
53
58
|
# object.run(2) # passes
|
54
59
|
# object.run(3) # fails
|
55
|
-
def |(
|
56
|
-
AnyOf.new(self,
|
60
|
+
def |(other)
|
61
|
+
AnyOf.new(self, other)
|
57
62
|
end
|
58
|
-
|
59
63
|
end
|
60
|
-
|
61
64
|
end
|
62
|
-
|
63
65
|
end
|
@@ -1,42 +1,47 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
5
4
|
module ParameterMatchers
|
6
|
-
|
7
|
-
#
|
5
|
+
# Matches any +Object+ equalling +value+.
|
6
|
+
#
|
7
|
+
# @param [Object] value expected value.
|
8
|
+
# @return [Equals] parameter matcher.
|
9
|
+
#
|
10
|
+
# @see Expectation#with
|
11
|
+
# @see Object#==
|
8
12
|
#
|
9
|
-
#
|
13
|
+
# @example Actual parameter equals expected parameter.
|
10
14
|
# object = mock()
|
11
15
|
# object.expects(:method_1).with(equals(2))
|
12
16
|
# object.method_1(2)
|
13
17
|
# # no error raised
|
14
18
|
#
|
19
|
+
# @example Actual parameter does not equal expected parameter.
|
15
20
|
# object = mock()
|
16
21
|
# object.expects(:method_1).with(equals(2))
|
17
22
|
# object.method_1(3)
|
18
|
-
# # error raised, because method_1 was not called with Object
|
23
|
+
# # error raised, because method_1 was not called with an +Object+ that equals 2
|
19
24
|
def equals(value)
|
20
25
|
Equals.new(value)
|
21
26
|
end
|
22
27
|
|
23
|
-
|
24
|
-
|
28
|
+
# Parameter matcher which matches when actual parameter equals expected value.
|
29
|
+
class Equals < Base
|
30
|
+
# @private
|
25
31
|
def initialize(value)
|
26
32
|
@value = value
|
27
33
|
end
|
28
|
-
|
34
|
+
|
35
|
+
# @private
|
29
36
|
def matches?(available_parameters)
|
30
37
|
parameter = available_parameters.shift
|
31
38
|
parameter == @value
|
32
39
|
end
|
33
|
-
|
40
|
+
|
41
|
+
# @private
|
34
42
|
def mocha_inspect
|
35
43
|
@value.mocha_inspect
|
36
44
|
end
|
37
|
-
|
38
45
|
end
|
39
|
-
|
40
46
|
end
|
41
|
-
|
42
|
-
end
|
47
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'mocha/deprecation'
|
2
|
+
require 'mocha/parameter_matchers/base'
|
3
|
+
require 'uri'
|
4
|
+
require 'cgi'
|
5
|
+
|
6
|
+
module Mocha
|
7
|
+
module ParameterMatchers
|
8
|
+
# Matches a URI without regard to the ordering of parameters in the query string.
|
9
|
+
#
|
10
|
+
# @param [String] uri URI to match.
|
11
|
+
# @return [EquivalentUri] parameter matcher.
|
12
|
+
#
|
13
|
+
# @see Expectation#with
|
14
|
+
#
|
15
|
+
# @example Actual URI is equivalent.
|
16
|
+
# object = mock()
|
17
|
+
# object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
|
18
|
+
# object.method_1('http://example.com/foo?b=2&a=1')
|
19
|
+
# # no error raised
|
20
|
+
#
|
21
|
+
# @example Actual URI is not equivalent.
|
22
|
+
# object = mock()
|
23
|
+
# object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
|
24
|
+
# object.method_1('http://example.com/foo?a=1&b=3')
|
25
|
+
# # error raised, because the query parameters were different
|
26
|
+
def equivalent_uri(uri)
|
27
|
+
EquivalentUri.new(uri)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Parameter matcher which matches URIs with equivalent query strings.
|
31
|
+
class EquivalentUri < Base
|
32
|
+
# @private
|
33
|
+
def initialize(uri)
|
34
|
+
@uri = URI.parse(uri)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @private
|
38
|
+
def matches?(available_parameters)
|
39
|
+
actual = explode(URI.parse(available_parameters.shift))
|
40
|
+
expected = explode(@uri)
|
41
|
+
actual == expected
|
42
|
+
end
|
43
|
+
|
44
|
+
# @private
|
45
|
+
def mocha_inspect
|
46
|
+
"equivalent_uri(#{@uri.mocha_inspect})"
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
# @private
|
52
|
+
def explode(uri)
|
53
|
+
query_hash = CGI.parse(uri.query || '')
|
54
|
+
URI::Generic::COMPONENT.inject({}) { |h, k| h.merge(k => uri.__send__(k)) }.merge(:query => query_hash)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -3,43 +3,48 @@ require 'mocha/parameter_matchers/all_of'
|
|
3
3
|
require 'mocha/parameter_matchers/has_entry'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
|
7
6
|
module ParameterMatchers
|
8
|
-
|
9
|
-
# :call-seq: has_entries(entries) -> parameter_matcher
|
10
|
-
#
|
11
7
|
# Matches +Hash+ containing all +entries+.
|
8
|
+
#
|
9
|
+
# @param [Hash] entries expected +Hash+ entries.
|
10
|
+
# @return [HasEntries] parameter matcher.
|
11
|
+
#
|
12
|
+
# @see Expectation#with
|
13
|
+
#
|
14
|
+
# @example Actual parameter contains all expected entries.
|
12
15
|
# object = mock()
|
13
16
|
# object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
|
14
17
|
# object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)
|
15
18
|
# # no error raised
|
16
19
|
#
|
20
|
+
# @example Actual parameter does not contain all expected entries.
|
17
21
|
# object = mock()
|
18
22
|
# object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
|
19
23
|
# object.method_1('key_1' => 1, 'key_2' => 99)
|
20
24
|
# # error raised, because method_1 was not called with Hash containing entries: 'key_1' => 1, 'key_2' => 2
|
21
|
-
|
25
|
+
#
|
26
|
+
def has_entries(entries) # rubocop:disable Naming/PredicateName
|
22
27
|
HasEntries.new(entries)
|
23
28
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
29
|
+
|
30
|
+
# Parameter matcher which matches when actual parameter contains all expected +Hash+ entries.
|
31
|
+
class HasEntries < Base
|
32
|
+
# @private
|
27
33
|
def initialize(entries)
|
28
34
|
@entries = entries
|
29
35
|
end
|
30
|
-
|
36
|
+
|
37
|
+
# @private
|
31
38
|
def matches?(available_parameters)
|
32
39
|
parameter = available_parameters.shift
|
33
40
|
has_entry_matchers = @entries.map { |key, value| HasEntry.new(key, value) }
|
34
41
|
AllOf.new(*has_entry_matchers).matches?([parameter])
|
35
42
|
end
|
36
|
-
|
43
|
+
|
44
|
+
# @private
|
37
45
|
def mocha_inspect
|
38
46
|
"has_entries(#{@entries.mocha_inspect})"
|
39
47
|
end
|
40
|
-
|
41
48
|
end
|
42
|
-
|
43
49
|
end
|
44
|
-
|
45
|
-
end
|
50
|
+
end
|
@@ -1,66 +1,98 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
5
4
|
module ParameterMatchers
|
6
|
-
|
7
|
-
# :call-seq: has_entry(key, value) -> parameter_matcher
|
8
|
-
# has_entry(key => value) -> parameter_matcher
|
9
|
-
#
|
10
5
|
# Matches +Hash+ containing entry with +key+ and +value+.
|
6
|
+
#
|
7
|
+
# @overload def has_entry(key, value)
|
8
|
+
# @param [Object] key key for entry.
|
9
|
+
# @param [Object] value value for entry.
|
10
|
+
# @overload def has_entry(single_entry_hash)
|
11
|
+
# @param [Hash] single_entry_hash +Hash+ with single entry.
|
12
|
+
# @raise [ArgumentError] if +single_entry_hash+ does not contain exactly one entry.
|
13
|
+
#
|
14
|
+
# @return [HasEntry] parameter matcher.
|
15
|
+
#
|
16
|
+
# @see Expectation#with
|
17
|
+
#
|
18
|
+
# @example Actual parameter contains expected entry supplied as key and value.
|
11
19
|
# object = mock()
|
12
20
|
# object.expects(:method_1).with(has_entry('key_1', 1))
|
13
21
|
# object.method_1('key_1' => 1, 'key_2' => 2)
|
14
22
|
# # no error raised
|
15
23
|
#
|
24
|
+
# @example Actual parameter contains expected entry supplied as +Hash+ entry.
|
16
25
|
# object = mock()
|
17
26
|
# object.expects(:method_1).with(has_entry('key_1' => 1))
|
18
27
|
# object.method_1('key_1' => 1, 'key_2' => 2)
|
19
28
|
# # no error raised
|
20
29
|
#
|
30
|
+
# @example Actual parameter does not contain expected entry supplied as key and value.
|
21
31
|
# object = mock()
|
22
32
|
# object.expects(:method_1).with(has_entry('key_1', 1))
|
23
33
|
# object.method_1('key_1' => 2, 'key_2' => 1)
|
24
34
|
# # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
|
25
35
|
#
|
36
|
+
# @example Actual parameter does not contain expected entry supplied as +Hash+ entry.
|
37
|
+
#
|
26
38
|
# object = mock()
|
27
39
|
# object.expects(:method_1).with(has_entry('key_1' => 1))
|
28
40
|
# object.method_1('key_1' => 2, 'key_2' => 1)
|
29
41
|
# # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
42
|
+
#
|
43
|
+
def has_entry(*options) # rubocop:disable Naming/PredicateName
|
44
|
+
case options.length
|
45
|
+
when 0
|
46
|
+
raise ArgumentError, 'No arguments. Expecting at least one.'
|
47
|
+
when 1
|
48
|
+
key, value = parse_option(options[0])
|
49
|
+
when 2
|
50
|
+
key, value = options
|
51
|
+
else
|
52
|
+
raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).'
|
41
53
|
end
|
42
54
|
HasEntry.new(key, value)
|
43
55
|
end
|
44
|
-
|
45
|
-
|
46
|
-
|
56
|
+
|
57
|
+
# Parameter matcher which matches when actual parameter contains expected +Hash+ entry.
|
58
|
+
class HasEntry < Base
|
59
|
+
# @private
|
47
60
|
def initialize(key, value)
|
48
|
-
@key
|
61
|
+
@key = key
|
62
|
+
@value = value
|
49
63
|
end
|
50
|
-
|
64
|
+
|
65
|
+
# @private
|
51
66
|
def matches?(available_parameters)
|
52
67
|
parameter = available_parameters.shift
|
53
68
|
return false unless parameter.respond_to?(:keys) && parameter.respond_to?(:[])
|
54
69
|
matching_keys = parameter.keys.select { |key| @key.to_matcher.matches?([key]) }
|
55
70
|
matching_keys.any? { |key| @value.to_matcher.matches?([parameter[key]]) }
|
56
71
|
end
|
57
|
-
|
72
|
+
|
73
|
+
# @private
|
58
74
|
def mocha_inspect
|
59
75
|
"has_entry(#{@key.mocha_inspect} => #{@value.mocha_inspect})"
|
60
76
|
end
|
61
|
-
|
62
77
|
end
|
63
|
-
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
# @private
|
82
|
+
def parse_option(option)
|
83
|
+
case option
|
84
|
+
when Hash
|
85
|
+
case option.length
|
86
|
+
when 0
|
87
|
+
raise ArgumentError, 'Argument has no entries.'
|
88
|
+
when 1
|
89
|
+
option.first
|
90
|
+
else
|
91
|
+
raise ArgumentError, 'Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.'
|
92
|
+
end
|
93
|
+
else
|
94
|
+
raise ArgumentError, 'Argument is not a Hash.'
|
95
|
+
end
|
96
|
+
end
|
64
97
|
end
|
65
|
-
|
66
|
-
end
|
98
|
+
end
|
@@ -1,43 +1,48 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
5
4
|
module ParameterMatchers
|
6
|
-
|
7
|
-
# :call-seq: has_key(key) -> parameter_matcher
|
8
|
-
#
|
9
5
|
# Matches +Hash+ containing +key+.
|
6
|
+
#
|
7
|
+
# @param [Object] key expected key.
|
8
|
+
# @return [HasKey] parameter matcher.
|
9
|
+
#
|
10
|
+
# @see Expectation#with
|
11
|
+
#
|
12
|
+
# @example Actual parameter contains entry with expected key.
|
10
13
|
# object = mock()
|
11
14
|
# object.expects(:method_1).with(has_key('key_1'))
|
12
15
|
# object.method_1('key_1' => 1, 'key_2' => 2)
|
13
16
|
# # no error raised
|
14
17
|
#
|
18
|
+
# @example Actual parameter does not contain entry with expected key.
|
15
19
|
# object = mock()
|
16
20
|
# object.expects(:method_1).with(has_key('key_1'))
|
17
21
|
# object.method_1('key_2' => 2)
|
18
22
|
# # error raised, because method_1 was not called with Hash containing key: 'key_1'
|
19
|
-
|
23
|
+
#
|
24
|
+
def has_key(key) # rubocop:disable Naming/PredicateName
|
20
25
|
HasKey.new(key)
|
21
26
|
end
|
22
27
|
|
23
|
-
|
24
|
-
|
28
|
+
# Parameter matcher which matches when actual parameter contains +Hash+ entry with expected key.
|
29
|
+
class HasKey < Base
|
30
|
+
# @private
|
25
31
|
def initialize(key)
|
26
32
|
@key = key
|
27
33
|
end
|
28
|
-
|
34
|
+
|
35
|
+
# @private
|
29
36
|
def matches?(available_parameters)
|
30
37
|
parameter = available_parameters.shift
|
31
38
|
return false unless parameter.respond_to?(:keys)
|
32
39
|
parameter.keys.any? { |key| @key.to_matcher.matches?([key]) }
|
33
40
|
end
|
34
|
-
|
41
|
+
|
42
|
+
# @private
|
35
43
|
def mocha_inspect
|
36
44
|
"has_key(#{@key.mocha_inspect})"
|
37
45
|
end
|
38
|
-
|
39
46
|
end
|
40
|
-
|
41
47
|
end
|
42
|
-
|
43
|
-
end
|
48
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'mocha/parameter_matchers/base'
|
2
|
+
|
3
|
+
module Mocha
|
4
|
+
module ParameterMatchers
|
5
|
+
# Matches +Hash+ containing +keys+.
|
6
|
+
#
|
7
|
+
# @param [*Array<Object>] keys expected keys.
|
8
|
+
# @return [HasKeys] parameter matcher.
|
9
|
+
#
|
10
|
+
# @see Expectation#with
|
11
|
+
#
|
12
|
+
# @example Actual parameter contains entry with expected keys.
|
13
|
+
# object = mock()
|
14
|
+
# object.expects(:method_1).with(has_keys(:key_1, :key_2))
|
15
|
+
# object.method_1(:key_1 => 1, :key_2 => 2, :key_3 => 3)
|
16
|
+
# # no error raised
|
17
|
+
#
|
18
|
+
# @example Actual parameter does not contain all expected keys.
|
19
|
+
# object = mock()
|
20
|
+
# object.expects(:method_1).with(has_keys(:key_1, :key_2))
|
21
|
+
# object.method_1(:key_2 => 2)
|
22
|
+
# # error raised, because method_1 was not called with Hash containing key: :key_1
|
23
|
+
#
|
24
|
+
def has_keys(*keys) # rubocop:disable Naming/PredicateName
|
25
|
+
HasKeys.new(*keys)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Parameter matcher which matches when actual parameter contains +Hash+ with all expected keys.
|
29
|
+
class HasKeys < Base
|
30
|
+
# @private
|
31
|
+
def initialize(*keys)
|
32
|
+
raise ArgumentError, 'No arguments. Expecting at least one.' if keys.empty?
|
33
|
+
|
34
|
+
@keys = keys
|
35
|
+
end
|
36
|
+
|
37
|
+
# @private
|
38
|
+
def matches?(available_parameters)
|
39
|
+
parameter = available_parameters.shift
|
40
|
+
return false unless parameter.respond_to?(:keys)
|
41
|
+
|
42
|
+
@keys.map(&:to_matcher).all? do |matcher|
|
43
|
+
parameter.keys.any? { |key| matcher.matches?([key]) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# @private
|
48
|
+
def mocha_inspect
|
49
|
+
"has_keys(#{@keys.mocha_inspect(false)})"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -1,43 +1,48 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
5
4
|
module ParameterMatchers
|
6
|
-
|
7
|
-
# :call-seq: has_value(value) -> parameter_matcher
|
8
|
-
#
|
9
5
|
# Matches +Hash+ containing +value+.
|
6
|
+
#
|
7
|
+
# @param [Object] value expected value.
|
8
|
+
# @return [HasValue] parameter matcher.
|
9
|
+
#
|
10
|
+
# @see Expectation#with
|
11
|
+
#
|
12
|
+
# @example Actual parameter contains entry with expected value.
|
10
13
|
# object = mock()
|
11
14
|
# object.expects(:method_1).with(has_value(1))
|
12
15
|
# object.method_1('key_1' => 1, 'key_2' => 2)
|
13
16
|
# # no error raised
|
14
17
|
#
|
18
|
+
# @example Actual parameter does not contain entry with expected value.
|
15
19
|
# object = mock()
|
16
20
|
# object.expects(:method_1).with(has_value(1))
|
17
21
|
# object.method_1('key_2' => 2)
|
18
22
|
# # error raised, because method_1 was not called with Hash containing value: 1
|
19
|
-
|
23
|
+
#
|
24
|
+
def has_value(value) # rubocop:disable Naming/PredicateName
|
20
25
|
HasValue.new(value)
|
21
26
|
end
|
22
27
|
|
23
|
-
|
24
|
-
|
28
|
+
# Parameter matcher which matches when actual parameter contains +Hash+ entry with expected value.
|
29
|
+
class HasValue < Base
|
30
|
+
# @private
|
25
31
|
def initialize(value)
|
26
32
|
@value = value
|
27
33
|
end
|
28
|
-
|
34
|
+
|
35
|
+
# @private
|
29
36
|
def matches?(available_parameters)
|
30
37
|
parameter = available_parameters.shift
|
31
38
|
return false unless parameter.respond_to?(:values)
|
32
39
|
parameter.values.any? { |value| @value.to_matcher.matches?([value]) }
|
33
40
|
end
|
34
|
-
|
41
|
+
|
42
|
+
# @private
|
35
43
|
def mocha_inspect
|
36
44
|
"has_value(#{@value.mocha_inspect})"
|
37
45
|
end
|
38
|
-
|
39
46
|
end
|
40
|
-
|
41
47
|
end
|
42
|
-
|
43
|
-
end
|
48
|
+
end
|