mocha 0.10.5 → 0.11.0
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING.rdoc +1 -1
- data/README.rdoc +6 -4
- data/RELEASE.rdoc +12 -0
- data/Rakefile +29 -60
- data/doc/Mocha.html +112 -0
- data/doc/Mocha/API.html +898 -0
- data/doc/Mocha/ClassMethods.html +246 -0
- data/doc/Mocha/Configuration.html +471 -0
- data/doc/Mocha/Expectation.html +2570 -0
- data/doc/Mocha/Mock.html +830 -0
- data/doc/Mocha/ObjectMethods.html +668 -0
- data/doc/Mocha/ParameterMatchers.html +2715 -0
- data/doc/Mocha/ParameterMatchers/AllOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/AnyOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/AnyParameters.html +136 -0
- data/doc/Mocha/ParameterMatchers/Anything.html +136 -0
- data/doc/Mocha/ParameterMatchers/Base.html +419 -0
- data/doc/Mocha/ParameterMatchers/Equals.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasEntries.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasEntry.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasKey.html +137 -0
- data/doc/Mocha/ParameterMatchers/HasValue.html +137 -0
- data/doc/Mocha/ParameterMatchers/Includes.html +137 -0
- data/doc/Mocha/ParameterMatchers/InstanceOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/IsA.html +136 -0
- data/doc/Mocha/ParameterMatchers/KindOf.html +137 -0
- data/doc/Mocha/ParameterMatchers/Not.html +137 -0
- data/doc/Mocha/ParameterMatchers/Optionally.html +136 -0
- data/doc/Mocha/ParameterMatchers/QueryStringMatches.html +136 -0
- data/doc/Mocha/ParameterMatchers/RegexpMatches.html +137 -0
- data/doc/Mocha/ParameterMatchers/RespondsWith.html +137 -0
- data/doc/Mocha/ParameterMatchers/YamlEquivalent.html +137 -0
- data/doc/Mocha/Sequence.html +133 -0
- data/doc/Mocha/StateMachine.html +510 -0
- data/doc/Mocha/StateMachine/State.html +125 -0
- data/doc/Mocha/StateMachine/StatePredicate.html +125 -0
- data/doc/Mocha/StubbingError.html +134 -0
- data/doc/Mocha/UnexpectedInvocation.html +124 -0
- data/doc/_index.html +481 -0
- data/doc/class_list.html +47 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +55 -0
- data/doc/css/style.css +322 -0
- data/doc/file.COPYING.html +72 -0
- data/doc/file.MIT-LICENSE.html +86 -0
- data/doc/file.README.html +153 -0
- data/doc/file.RELEASE.html +933 -0
- data/doc/file.misc.html +108 -0
- data/doc/file.mocha.html +90 -0
- data/doc/file.stubba.html +129 -0
- data/doc/file_list.html +67 -0
- data/doc/frames.html +13 -0
- data/doc/index.html +153 -0
- data/doc/js/app.js +205 -0
- data/doc/js/full_list.js +173 -0
- data/doc/js/jquery.js +16 -0
- data/doc/method_list.html +510 -0
- data/doc/top-level-namespace.html +105 -0
- data/lib/mocha/any_instance_method.rb +16 -11
- data/lib/mocha/api.rb +120 -106
- data/lib/mocha/argument_iterator.rb +6 -6
- data/lib/mocha/backtrace_filter.rb +1 -1
- data/lib/mocha/cardinality.rb +21 -21
- data/lib/mocha/central.rb +8 -8
- data/lib/mocha/change_state_side_effect.rb +5 -5
- data/lib/mocha/class_method.rb +14 -19
- data/lib/mocha/configuration.rb +47 -37
- data/lib/mocha/deprecation.rb +8 -8
- data/lib/mocha/exception_raiser.rb +7 -7
- data/lib/mocha/expectation.rb +173 -77
- data/lib/mocha/expectation_error.rb +2 -2
- data/lib/mocha/expectation_list.rb +11 -11
- data/lib/mocha/in_state_ordering_constraint.rb +5 -5
- data/lib/mocha/inspect.rb +8 -8
- data/lib/mocha/instance_method.rb +1 -17
- data/lib/mocha/integration.rb +9 -9
- data/lib/mocha/integration/mini_test/assertion_counter.rb +9 -9
- data/lib/mocha/integration/mini_test/exception_translation.rb +6 -6
- data/lib/mocha/integration/mini_test/version_13.rb +6 -6
- data/lib/mocha/integration/mini_test/version_140.rb +6 -6
- data/lib/mocha/integration/mini_test/version_141.rb +6 -6
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +7 -7
- data/lib/mocha/integration/mini_test/version_200.rb +7 -7
- data/lib/mocha/integration/test_unit/assertion_counter.rb +10 -10
- data/lib/mocha/integration/test_unit/gem_version_200.rb +6 -6
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +6 -6
- data/lib/mocha/integration/test_unit/ruby_version_185_and_below.rb +6 -6
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +6 -6
- data/lib/mocha/is_a.rb +1 -1
- data/lib/mocha/logger.rb +6 -6
- data/lib/mocha/method_matcher.rb +7 -7
- data/lib/mocha/mock.rb +85 -48
- data/lib/mocha/mockery.rb +47 -33
- data/lib/mocha/module_method.rb +2 -2
- data/lib/mocha/multiple_yields.rb +9 -9
- data/lib/mocha/names.rb +18 -18
- data/lib/mocha/no_yields.rb +7 -7
- data/lib/mocha/object.rb +104 -75
- data/lib/mocha/parameter_matchers.rb +3 -3
- data/lib/mocha/parameter_matchers/all_of.rb +22 -12
- data/lib/mocha/parameter_matchers/any_of.rb +23 -12
- data/lib/mocha/parameter_matchers/any_parameters.rb +15 -8
- data/lib/mocha/parameter_matchers/anything.rb +20 -11
- data/lib/mocha/parameter_matchers/base.rb +35 -27
- data/lib/mocha/parameter_matchers/equals.rb +23 -12
- data/lib/mocha/parameter_matchers/has_entries.rb +22 -12
- data/lib/mocha/parameter_matchers/has_entry.rb +31 -13
- data/lib/mocha/parameter_matchers/has_key.rb +21 -11
- data/lib/mocha/parameter_matchers/has_value.rb +21 -11
- data/lib/mocha/parameter_matchers/includes.rb +15 -5
- data/lib/mocha/parameter_matchers/instance_of.rb +23 -12
- data/lib/mocha/parameter_matchers/is_a.rb +23 -12
- data/lib/mocha/parameter_matchers/kind_of.rb +23 -12
- data/lib/mocha/parameter_matchers/not.rb +22 -12
- data/lib/mocha/parameter_matchers/object.rb +5 -3
- data/lib/mocha/parameter_matchers/optionally.rb +24 -12
- data/lib/mocha/parameter_matchers/query_string.rb +14 -3
- data/lib/mocha/parameter_matchers/regexp_matches.rb +21 -11
- data/lib/mocha/parameter_matchers/responds_with.rb +17 -6
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +16 -6
- data/lib/mocha/parameters_matcher.rb +8 -8
- data/lib/mocha/pretty_parameters.rb +7 -7
- data/lib/mocha/return_values.rb +11 -11
- data/lib/mocha/sequence.rb +23 -14
- data/lib/mocha/single_return_value.rb +7 -7
- data/lib/mocha/single_yield.rb +9 -9
- data/lib/mocha/standalone.rb +1 -1
- data/lib/mocha/state_machine.rb +61 -46
- data/lib/mocha/stubbing_error.rb +8 -5
- data/lib/mocha/thrower.rb +2 -2
- data/lib/mocha/unexpected_invocation.rb +9 -6
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha/yield_parameters.rb +10 -10
- data/lib/mocha_standalone.rb +1 -1
- data/mocha.gemspec +4 -7
- data/test/acceptance/acceptance_test_helper.rb +10 -10
- data/test/acceptance/api_test.rb +20 -20
- data/test/acceptance/bug_18914_test.rb +12 -12
- data/test/acceptance/bug_21465_test.rb +6 -6
- data/test/acceptance/bug_21563_test.rb +5 -5
- data/test/acceptance/exception_rescue_test.rb +1 -1
- data/test/acceptance/expected_invocation_count_test.rb +17 -17
- data/test/acceptance/failure_messages_test.rb +13 -13
- data/test/acceptance/minitest_test.rb +39 -39
- data/test/acceptance/mocha_example_test.rb +26 -26
- data/test/acceptance/mocha_test_result_test.rb +13 -13
- data/test/acceptance/mock_test.rb +5 -5
- data/test/acceptance/mock_with_initializer_block_test.rb +5 -5
- data/test/acceptance/mocked_methods_dispatch_test.rb +4 -4
- data/test/acceptance/multiple_expectations_failure_message_test.rb +2 -2
- data/test/acceptance/optional_parameters_test.rb +5 -5
- data/test/acceptance/parameter_matcher_test.rb +18 -18
- data/test/acceptance/partial_mocks_test.rb +9 -9
- data/test/acceptance/raise_exception_test.rb +1 -1
- data/test/acceptance/return_value_test.rb +5 -5
- data/test/acceptance/sequence_test.rb +29 -29
- data/test/acceptance/states_test.rb +17 -17
- data/test/acceptance/stub_any_instance_method_test.rb +13 -13
- data/test/acceptance/stub_class_method_defined_on_active_record_association_proxy_test.rb +7 -7
- data/test/acceptance/stub_everything_test.rb +5 -5
- data/test/acceptance/stub_instance_method_defined_on_singleton_class_test.rb +1 -1
- data/test/acceptance/stub_module_method_test.rb +17 -17
- data/test/acceptance/stub_test.rb +5 -5
- data/test/acceptance/stubba_example_test.rb +24 -24
- data/test/acceptance/stubba_test_result_test.rb +11 -11
- data/test/acceptance/stubbing_error_backtrace_test.rb +6 -6
- data/test/acceptance/stubbing_frozen_object_test.rb +88 -0
- data/test/acceptance/stubbing_method_unnecessarily_test.rb +9 -9
- data/test/acceptance/stubbing_nil_test.rb +59 -0
- data/test/acceptance/stubbing_non_existent_any_instance_method_test.rb +13 -13
- data/test/acceptance/stubbing_non_existent_class_method_test.rb +15 -15
- data/test/acceptance/stubbing_non_existent_instance_method_test.rb +14 -14
- data/test/acceptance/stubbing_non_public_any_instance_method_test.rb +13 -13
- data/test/acceptance/stubbing_non_public_class_method_test.rb +12 -12
- data/test/acceptance/stubbing_non_public_instance_method_test.rb +12 -12
- data/test/acceptance/stubbing_on_non_mock_object_test.rb +9 -9
- data/test/acceptance/stubbing_same_class_method_on_parent_and_child_classes_test.rb +35 -0
- data/test/acceptance/throw_test.rb +1 -1
- data/test/deprecation_disabler.rb +1 -1
- data/test/execution_point.rb +7 -7
- data/test/method_definer.rb +1 -1
- data/test/mini_test_result.rb +21 -21
- data/test/simple_counter.rb +5 -5
- data/test/test_helper.rb +1 -1
- data/test/test_runner.rb +22 -24
- data/test/test_unit_result.rb +20 -0
- data/test/unit/any_instance_method_test.rb +28 -32
- data/test/unit/array_inspect_test.rb +3 -3
- data/test/unit/backtrace_filter_test.rb +3 -3
- data/test/unit/cardinality_test.rb +12 -12
- data/test/unit/central_test.rb +18 -18
- data/test/unit/change_state_side_effect_test.rb +12 -12
- data/test/unit/class_method_test.rb +33 -91
- data/test/unit/date_time_inspect_test.rb +4 -4
- data/test/unit/exception_raiser_test.rb +5 -5
- data/test/unit/expectation_list_test.rb +7 -7
- data/test/unit/expectation_test.rb +66 -66
- data/test/unit/hash_inspect_test.rb +4 -4
- data/test/unit/in_state_ordering_constraint_test.rb +13 -13
- data/test/unit/method_matcher_test.rb +3 -3
- data/test/unit/mock_test.rb +40 -40
- data/test/unit/mockery_test.rb +25 -25
- data/test/unit/multiple_yields_test.rb +2 -2
- data/test/unit/no_yields_test.rb +2 -2
- data/test/unit/object_inspect_test.rb +4 -4
- data/test/unit/object_test.rb +15 -15
- data/test/unit/parameter_matchers/all_of_test.rb +6 -6
- data/test/unit/parameter_matchers/any_of_test.rb +6 -6
- data/test/unit/parameter_matchers/anything_test.rb +5 -5
- data/test/unit/parameter_matchers/has_entries_test.rb +10 -10
- data/test/unit/parameter_matchers/has_entry_test.rb +13 -13
- data/test/unit/parameter_matchers/has_key_test.rb +11 -11
- data/test/unit/parameter_matchers/has_value_test.rb +12 -12
- data/test/unit/parameter_matchers/includes_test.rb +4 -4
- data/test/unit/parameter_matchers/not_test.rb +6 -6
- data/test/unit/parameter_matchers/regexp_matches_test.rb +9 -9
- data/test/unit/parameter_matchers/responds_with_test.rb +6 -6
- data/test/unit/parameter_matchers/stub_matcher.rb +4 -4
- data/test/unit/parameter_matchers/yaml_equivalent_test.rb +6 -6
- data/test/unit/parameters_matcher_test.rb +16 -16
- data/test/unit/return_values_test.rb +5 -5
- data/test/unit/sequence_test.rb +10 -10
- data/test/unit/single_return_value_test.rb +3 -3
- data/test/unit/single_yield_test.rb +2 -2
- data/test/unit/state_machine_test.rb +19 -19
- data/test/unit/string_inspect_test.rb +2 -2
- data/test/unit/thrower_test.rb +1 -1
- data/test/unit/yield_parameters_test.rb +11 -11
- metadata +113 -101
@@ -1,8 +1,8 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
3
|
-
# Used as parameters for Expectation#with to restrict the parameter values which will match the expectation. Can be nested.
|
2
|
+
|
3
|
+
# Used as parameters for {Expectation#with} to restrict the parameter values which will match the expectation. Can be nested.
|
4
4
|
module ParameterMatchers; end
|
5
|
-
|
5
|
+
|
6
6
|
end
|
7
7
|
|
8
8
|
require 'mocha/parameter_matchers/object'
|
@@ -1,17 +1,23 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
4
|
+
|
5
5
|
module ParameterMatchers
|
6
6
|
|
7
|
-
#
|
7
|
+
# Matches if all +matchers+ match.
|
8
|
+
#
|
9
|
+
# @param [*Array<Base>] parameter_matchers parameter matchers.
|
10
|
+
# @return [AllOf] parameter matcher.
|
11
|
+
#
|
12
|
+
# @see Expectation#with
|
8
13
|
#
|
9
|
-
#
|
14
|
+
# @example All parameter matchers match.
|
10
15
|
# object = mock()
|
11
16
|
# object.expects(:method_1).with(all_of(includes(1), includes(3)))
|
12
17
|
# object.method_1([1, 3])
|
13
18
|
# # no error raised
|
14
19
|
#
|
20
|
+
# @example One of the parameter matchers does not match.
|
15
21
|
# object = mock()
|
16
22
|
# object.expects(:method_1).with(all_of(includes(1), includes(3)))
|
17
23
|
# object.method_1([1, 2])
|
@@ -19,24 +25,28 @@ module Mocha
|
|
19
25
|
def all_of(*matchers)
|
20
26
|
AllOf.new(*matchers)
|
21
27
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
28
|
+
|
29
|
+
# Parameter matcher which combines a number of other matchers using a logical AND.
|
30
|
+
class AllOf < Base
|
31
|
+
|
32
|
+
# @private
|
25
33
|
def initialize(*matchers)
|
26
34
|
@matchers = matchers
|
27
35
|
end
|
28
|
-
|
36
|
+
|
37
|
+
# @private
|
29
38
|
def matches?(available_parameters)
|
30
39
|
parameter = available_parameters.shift
|
31
40
|
@matchers.all? { |matcher| matcher.to_matcher.matches?([parameter]) }
|
32
41
|
end
|
33
|
-
|
42
|
+
|
43
|
+
# @private
|
34
44
|
def mocha_inspect
|
35
45
|
"all_of(#{@matchers.map { |matcher| matcher.mocha_inspect }.join(", ") })"
|
36
46
|
end
|
37
|
-
|
47
|
+
|
38
48
|
end
|
39
|
-
|
49
|
+
|
40
50
|
end
|
41
|
-
|
42
|
-
end
|
51
|
+
|
52
|
+
end
|
@@ -1,22 +1,29 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
4
|
+
|
5
5
|
module ParameterMatchers
|
6
6
|
|
7
|
-
#
|
7
|
+
# Matches if any +matchers+ match.
|
8
|
+
#
|
9
|
+
# @param [*Array<Base>] parameter_matchers parameter matchers.
|
10
|
+
# @return [AnyOf] parameter matcher.
|
11
|
+
#
|
12
|
+
# @see Expectation#with
|
8
13
|
#
|
9
|
-
#
|
14
|
+
# @example One parameter matcher matches.
|
10
15
|
# object = mock()
|
11
16
|
# object.expects(:method_1).with(any_of(1, 3))
|
12
17
|
# object.method_1(1)
|
13
18
|
# # no error raised
|
14
19
|
#
|
20
|
+
# @example The other parameter matcher matches.
|
15
21
|
# object = mock()
|
16
22
|
# object.expects(:method_1).with(any_of(1, 3))
|
17
23
|
# object.method_1(3)
|
18
24
|
# # no error raised
|
19
25
|
#
|
26
|
+
# @example Neither parameter matcher matches.
|
20
27
|
# object = mock()
|
21
28
|
# object.expects(:method_1).with(any_of(1, 3))
|
22
29
|
# object.method_1(2)
|
@@ -24,24 +31,28 @@ module Mocha
|
|
24
31
|
def any_of(*matchers)
|
25
32
|
AnyOf.new(*matchers)
|
26
33
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
34
|
+
|
35
|
+
# Parameter matcher which combines a number of other matchers using a logical OR.
|
36
|
+
class AnyOf < Base
|
37
|
+
|
38
|
+
# @private
|
30
39
|
def initialize(*matchers)
|
31
40
|
@matchers = matchers
|
32
41
|
end
|
33
|
-
|
42
|
+
|
43
|
+
# @private
|
34
44
|
def matches?(available_parameters)
|
35
45
|
parameter = available_parameters.shift
|
36
46
|
@matchers.any? { |matcher| matcher.to_matcher.matches?([parameter]) }
|
37
47
|
end
|
38
|
-
|
48
|
+
|
49
|
+
# @private
|
39
50
|
def mocha_inspect
|
40
51
|
"any_of(#{@matchers.map { |matcher| matcher.mocha_inspect }.join(", ") })"
|
41
52
|
end
|
42
|
-
|
53
|
+
|
43
54
|
end
|
44
|
-
|
55
|
+
|
45
56
|
end
|
46
|
-
|
47
|
-
end
|
57
|
+
|
58
|
+
end
|
@@ -1,12 +1,16 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
4
|
+
|
5
5
|
module ParameterMatchers
|
6
6
|
|
7
|
-
#
|
7
|
+
# Matches any parameters. This is used as the default for a newly built expectation.
|
8
|
+
#
|
9
|
+
# @return [AnyParameters] parameter matcher.
|
10
|
+
#
|
11
|
+
# @see Expectation#with
|
8
12
|
#
|
9
|
-
#
|
13
|
+
# @example Any parameters will match.
|
10
14
|
# object = mock()
|
11
15
|
# object.expects(:method_1).with(any_parameters)
|
12
16
|
# object.method_1(1, 2, 3, 4)
|
@@ -20,8 +24,10 @@ module Mocha
|
|
20
24
|
AnyParameters.new
|
21
25
|
end
|
22
26
|
|
23
|
-
|
24
|
-
|
27
|
+
# Parameter matcher which always matches whatever the parameters.
|
28
|
+
class AnyParameters < Base
|
29
|
+
|
30
|
+
# @private
|
25
31
|
def matches?(available_parameters)
|
26
32
|
while available_parameters.length > 0 do
|
27
33
|
available_parameters.shift
|
@@ -29,12 +35,13 @@ module Mocha
|
|
29
35
|
return true
|
30
36
|
end
|
31
37
|
|
38
|
+
# @private
|
32
39
|
def mocha_inspect
|
33
40
|
"any_parameters"
|
34
41
|
end
|
35
42
|
|
36
43
|
end
|
37
|
-
|
44
|
+
|
38
45
|
end
|
39
|
-
|
40
|
-
end
|
46
|
+
|
47
|
+
end
|
@@ -1,33 +1,42 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
4
|
+
|
5
5
|
module ParameterMatchers
|
6
6
|
|
7
|
-
# :call-seq: anything() -> parameter_matcher
|
8
|
-
#
|
9
7
|
# Matches any object.
|
8
|
+
#
|
9
|
+
# @return [Anything] parameter matcher.
|
10
|
+
#
|
11
|
+
# @see Expectation#with
|
12
|
+
#
|
13
|
+
# @example Any object will match.
|
10
14
|
# object = mock()
|
11
15
|
# object.expects(:method_1).with(anything)
|
12
16
|
# object.method_1('foo')
|
17
|
+
# object.method_1(789)
|
18
|
+
# object.method_1(:bar)
|
13
19
|
# # no error raised
|
14
20
|
def anything
|
15
21
|
Anything.new
|
16
22
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
|
24
|
+
# Parameter matcher which always matches a single parameter.
|
25
|
+
class Anything < Base
|
26
|
+
|
27
|
+
# @private
|
20
28
|
def matches?(available_parameters)
|
21
29
|
available_parameters.shift
|
22
30
|
return true
|
23
31
|
end
|
24
|
-
|
32
|
+
|
33
|
+
# @private
|
25
34
|
def mocha_inspect
|
26
35
|
"anything"
|
27
36
|
end
|
28
|
-
|
37
|
+
|
29
38
|
end
|
30
|
-
|
39
|
+
|
31
40
|
end
|
32
|
-
|
33
|
-
end
|
41
|
+
|
42
|
+
end
|
@@ -1,53 +1,61 @@
|
|
1
1
|
module Mocha
|
2
|
-
|
2
|
+
|
3
3
|
module ParameterMatchers
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
|
5
|
+
# @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}.
|
6
|
+
class Base
|
7
|
+
|
8
|
+
# @private
|
9
|
+
def to_matcher
|
8
10
|
self
|
9
11
|
end
|
10
|
-
|
11
|
-
#
|
12
|
+
|
13
|
+
# A shorthand way of combining two matchers when both must match.
|
12
14
|
#
|
13
|
-
#
|
14
|
-
# all match.
|
15
|
+
# Returns a new {AllOf} parameter matcher combining two matchers using a logical AND.
|
15
16
|
#
|
16
|
-
#
|
17
|
-
# given matcher and the receiver.
|
17
|
+
# This shorthand will not work with an implicit equals match. Instead, an explicit {Equals} matcher should be used.
|
18
18
|
#
|
19
|
-
#
|
19
|
+
# @param [Base] matcher parameter matcher.
|
20
|
+
# @return [AllOf] parameter matcher.
|
21
|
+
#
|
22
|
+
# @see Expectation#with
|
23
|
+
#
|
24
|
+
# @example Alternative ways to combine matchers with a logical AND.
|
20
25
|
# object = mock()
|
21
26
|
# object.expects(:run).with(all_of(has_key(:foo), has_key(:bar)))
|
22
27
|
# object.run(:foo => 'foovalue', :bar => 'barvalue')
|
23
28
|
#
|
24
|
-
# #
|
29
|
+
# # is exactly equivalent to
|
30
|
+
#
|
25
31
|
# object.expects(:run).with(has_key(:foo) & has_key(:bar))
|
26
32
|
# object.run(:foo => 'foovalue', :bar => 'barvalue)
|
27
33
|
def &(matcher)
|
28
34
|
AllOf.new(self, matcher)
|
29
35
|
end
|
30
|
-
|
31
|
-
#
|
36
|
+
|
37
|
+
# A shorthand way of combining two matchers when at least one must match.
|
38
|
+
#
|
39
|
+
# Returns a new +AnyOf+ parameter matcher combining two matchers using a logical OR.
|
32
40
|
#
|
33
|
-
#
|
34
|
-
# one of which should pass.
|
41
|
+
# This shorthand will not work with an implicit equals match. Instead, an explicit {Equals} matcher should be used.
|
35
42
|
#
|
36
|
-
#
|
37
|
-
#
|
43
|
+
# @param [Base] matcher parameter matcher.
|
44
|
+
# @return [AnyOf] parameter matcher.
|
38
45
|
#
|
39
|
-
#
|
46
|
+
# @see Expectation#with
|
47
|
+
#
|
48
|
+
# @example Alternative ways to combine matchers with a logical OR.
|
40
49
|
# object = mock()
|
41
50
|
# object.expects(:run).with(any_of(has_key(:foo), has_key(:bar)))
|
42
51
|
# object.run(:foo => 'foovalue')
|
43
52
|
#
|
44
|
-
# #
|
53
|
+
# # is exactly equivalent to
|
54
|
+
#
|
45
55
|
# object.expects(:run).with(has_key(:foo) | has_key(:bar))
|
46
56
|
# object.run(:foo => 'foovalue')
|
47
57
|
#
|
48
|
-
#
|
49
|
-
# an explicit equals matcher should be used:
|
50
|
-
#
|
58
|
+
# @example Using an explicit {Equals} matcher in combination with {#|}.
|
51
59
|
# object.expects(:run).with(equals(1) | equals(2))
|
52
60
|
# object.run(1) # passes
|
53
61
|
# object.run(2) # passes
|
@@ -55,9 +63,9 @@ module Mocha
|
|
55
63
|
def |(matcher)
|
56
64
|
AnyOf.new(self, matcher)
|
57
65
|
end
|
58
|
-
|
66
|
+
|
59
67
|
end
|
60
|
-
|
68
|
+
|
61
69
|
end
|
62
|
-
|
70
|
+
|
63
71
|
end
|
@@ -1,42 +1,53 @@
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
2
2
|
|
3
3
|
module Mocha
|
4
|
-
|
4
|
+
|
5
5
|
module ParameterMatchers
|
6
6
|
|
7
|
-
#
|
7
|
+
# Matches any +Object+ equalling +value+.
|
8
|
+
#
|
9
|
+
# @param [Object] value expected value.
|
10
|
+
# @return [Equals] parameter matcher.
|
11
|
+
#
|
12
|
+
# @see Expectation#with
|
13
|
+
# @see Object#==
|
8
14
|
#
|
9
|
-
#
|
15
|
+
# @example Actual parameter equals expected parameter.
|
10
16
|
# object = mock()
|
11
17
|
# object.expects(:method_1).with(equals(2))
|
12
18
|
# object.method_1(2)
|
13
19
|
# # no error raised
|
14
20
|
#
|
21
|
+
# @example Actual parameter does not equal expected parameter.
|
15
22
|
# object = mock()
|
16
23
|
# object.expects(:method_1).with(equals(2))
|
17
24
|
# object.method_1(3)
|
18
|
-
# # error raised, because method_1 was not called with Object
|
25
|
+
# # error raised, because method_1 was not called with an +Object+ that equals 3
|
19
26
|
def equals(value)
|
20
27
|
Equals.new(value)
|
21
28
|
end
|
22
29
|
|
23
|
-
|
24
|
-
|
30
|
+
# Parameter matcher which matches when actual parameter equals expected value.
|
31
|
+
class Equals < Base
|
32
|
+
|
33
|
+
# @private
|
25
34
|
def initialize(value)
|
26
35
|
@value = value
|
27
36
|
end
|
28
|
-
|
37
|
+
|
38
|
+
# @private
|
29
39
|
def matches?(available_parameters)
|
30
40
|
parameter = available_parameters.shift
|
31
41
|
parameter == @value
|
32
42
|
end
|
33
|
-
|
43
|
+
|
44
|
+
# @private
|
34
45
|
def mocha_inspect
|
35
46
|
@value.mocha_inspect
|
36
47
|
end
|
37
|
-
|
48
|
+
|
38
49
|
end
|
39
|
-
|
50
|
+
|
40
51
|
end
|
41
|
-
|
42
|
-
end
|
52
|
+
|
53
|
+
end
|
@@ -3,17 +3,23 @@ require 'mocha/parameter_matchers/all_of'
|
|
3
3
|
require 'mocha/parameter_matchers/has_entry'
|
4
4
|
|
5
5
|
module Mocha
|
6
|
-
|
6
|
+
|
7
7
|
module ParameterMatchers
|
8
8
|
|
9
|
-
# :call-seq: has_entries(entries) -> parameter_matcher
|
10
|
-
#
|
11
9
|
# Matches +Hash+ containing all +entries+.
|
10
|
+
#
|
11
|
+
# @param [Hash] entries expected +Hash+ entries.
|
12
|
+
# @return [HasEntries] parameter matcher.
|
13
|
+
#
|
14
|
+
# @see Expectation#with
|
15
|
+
#
|
16
|
+
# @example Actual parameter contains all expected entries.
|
12
17
|
# object = mock()
|
13
18
|
# object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
|
14
19
|
# object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)
|
15
20
|
# # no error raised
|
16
21
|
#
|
22
|
+
# @example Actual parameter does not contain all expected entries.
|
17
23
|
# object = mock()
|
18
24
|
# object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
|
19
25
|
# object.method_1('key_1' => 1, 'key_2' => 99)
|
@@ -21,25 +27,29 @@ module Mocha
|
|
21
27
|
def has_entries(entries)
|
22
28
|
HasEntries.new(entries)
|
23
29
|
end
|
24
|
-
|
25
|
-
|
26
|
-
|
30
|
+
|
31
|
+
# Parameter matcher which matches when actual parameter contains all expected +Hash+ entries.
|
32
|
+
class HasEntries < Base
|
33
|
+
|
34
|
+
# @private
|
27
35
|
def initialize(entries)
|
28
36
|
@entries = entries
|
29
37
|
end
|
30
|
-
|
38
|
+
|
39
|
+
# @private
|
31
40
|
def matches?(available_parameters)
|
32
41
|
parameter = available_parameters.shift
|
33
42
|
has_entry_matchers = @entries.map { |key, value| HasEntry.new(key, value) }
|
34
43
|
AllOf.new(*has_entry_matchers).matches?([parameter])
|
35
44
|
end
|
36
|
-
|
45
|
+
|
46
|
+
# @private
|
37
47
|
def mocha_inspect
|
38
48
|
"has_entries(#{@entries.mocha_inspect})"
|
39
49
|
end
|
40
|
-
|
50
|
+
|
41
51
|
end
|
42
|
-
|
52
|
+
|
43
53
|
end
|
44
|
-
|
45
|
-
end
|
54
|
+
|
55
|
+
end
|