mocha 2.8.2 → 3.0.0.pre.rc.1
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 +4 -4
- data/.rubocop.yml +67 -3
- data/.rubocop_todo.yml +25 -13
- data/Gemfile +11 -27
- data/README.md +3 -4
- data/RELEASE.md +30 -25
- data/Rakefile +6 -5
- data/gemfiles/Gemfile.minitest.latest +3 -3
- data/gemfiles/Gemfile.rubocop +9 -0
- data/gemfiles/Gemfile.test-unit.latest +3 -3
- data/lib/mocha/any_instance_method.rb +2 -0
- data/lib/mocha/any_instance_receiver.rb +20 -0
- data/lib/mocha/api.rb +5 -3
- data/lib/mocha/argument_iterator.rb +5 -5
- data/lib/mocha/backtrace_filter.rb +2 -0
- data/lib/mocha/{block_matcher.rb → block_matchers.rb} +2 -0
- data/lib/mocha/cardinality.rb +2 -2
- data/lib/mocha/central.rb +4 -0
- data/lib/mocha/change_state_side_effect.rb +2 -0
- data/lib/mocha/class_methods.rb +5 -4
- data/lib/mocha/configuration.rb +19 -42
- data/lib/mocha/default_name.rb +15 -0
- data/lib/mocha/default_receiver.rb +13 -0
- data/lib/mocha/deprecation.rb +15 -9
- data/lib/mocha/detection/minitest.rb +2 -0
- data/lib/mocha/detection/test_unit.rb +4 -1
- data/lib/mocha/error_with_filtered_backtrace.rb +2 -0
- data/lib/mocha/exception_raiser.rb +3 -0
- data/lib/mocha/expectation.rb +59 -25
- data/lib/mocha/expectation_error.rb +2 -0
- data/lib/mocha/expectation_error_factory.rb +2 -0
- data/lib/mocha/expectation_list.rb +2 -0
- data/lib/mocha/hooks.rb +2 -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 +2 -0
- data/lib/mocha/inspect.rb +13 -10
- data/lib/mocha/instance_method.rb +2 -0
- data/lib/mocha/integration/assertion_counter.rb +2 -0
- data/lib/mocha/integration/minitest/adapter.rb +4 -3
- data/lib/mocha/integration/minitest.rb +4 -3
- data/lib/mocha/integration/monkey_patcher.rb +4 -2
- data/lib/mocha/integration/test_unit/adapter.rb +3 -0
- data/lib/mocha/integration/test_unit.rb +4 -3
- data/lib/mocha/invocation.rb +9 -6
- data/lib/mocha/logger.rb +2 -0
- data/lib/mocha/macos_version.rb +2 -0
- data/lib/mocha/method_matcher.rb +2 -0
- data/lib/mocha/minitest.rb +2 -1
- data/lib/mocha/mock.rb +8 -8
- data/lib/mocha/mockery.rb +16 -6
- data/lib/mocha/name.rb +13 -0
- data/lib/mocha/not_initialized_error.rb +2 -0
- data/lib/mocha/object_methods.rb +7 -5
- data/lib/mocha/object_receiver.rb +20 -0
- data/lib/mocha/parameter_matchers/all_of.rb +23 -28
- data/lib/mocha/parameter_matchers/any_of.rb +29 -34
- data/lib/mocha/parameter_matchers/any_parameters.rb +21 -26
- data/lib/mocha/parameter_matchers/anything.rb +18 -23
- data/lib/mocha/parameter_matchers/base.rb +5 -23
- data/lib/mocha/parameter_matchers/equals.rb +24 -29
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +23 -28
- data/lib/mocha/parameter_matchers/has_entries.rb +24 -29
- data/lib/mocha/parameter_matchers/has_entry.rb +68 -70
- data/lib/mocha/parameter_matchers/has_key.rb +25 -29
- data/lib/mocha/parameter_matchers/has_keys.rb +25 -30
- data/lib/mocha/parameter_matchers/has_value.rb +25 -29
- data/lib/mocha/parameter_matchers/includes.rb +65 -73
- data/lib/mocha/parameter_matchers/instance_methods.rb +5 -3
- data/lib/mocha/parameter_matchers/instance_of.rb +24 -29
- data/lib/mocha/parameter_matchers/is_a.rb +25 -30
- data/lib/mocha/parameter_matchers/kind_of.rb +24 -29
- data/lib/mocha/parameter_matchers/not.rb +24 -29
- data/lib/mocha/parameter_matchers/optionally.rb +35 -40
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +39 -19
- data/lib/mocha/parameter_matchers/regexp_matches.rb +25 -29
- data/lib/mocha/parameter_matchers/responds_with.rb +48 -53
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +23 -28
- data/lib/mocha/parameter_matchers.rb +4 -6
- data/lib/mocha/parameters_matcher.rb +9 -1
- data/lib/mocha/raised_exception.rb +2 -0
- data/lib/mocha/return_values.rb +2 -0
- data/lib/mocha/ruby_version.rb +3 -0
- data/lib/mocha/sequence.rb +2 -0
- data/lib/mocha/single_return_value.rb +1 -1
- data/lib/mocha/state_machine.rb +2 -0
- data/lib/mocha/stubbed_method.rb +9 -2
- data/lib/mocha/stubbing_error.rb +2 -0
- data/lib/mocha/test_unit.rb +2 -1
- data/lib/mocha/thrower.rb +2 -0
- data/lib/mocha/thrown_object.rb +2 -0
- data/lib/mocha/version.rb +3 -1
- data/lib/mocha/yield_parameters.rb +2 -0
- data/lib/mocha.rb +5 -0
- data/mocha.gemspec +10 -4
- metadata +17 -16
- data/lib/mocha/debug.rb +0 -9
- data/lib/mocha/integration/minitest/exception_translation.rb +0 -14
- data/lib/mocha/is_a.rb +0 -7
- data/lib/mocha/names.rb +0 -43
- data/lib/mocha/parameter_matchers/deprecations.rb +0 -44
- data/lib/mocha/receivers.rb +0 -45
|
@@ -1,38 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'mocha/parameter_matchers/base'
|
|
2
|
-
require 'mocha/parameter_matchers/deprecations'
|
|
3
4
|
|
|
4
5
|
module Mocha
|
|
5
6
|
module ParameterMatchers
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Equals.new(value)
|
|
28
|
-
end
|
|
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#==
|
|
14
|
+
#
|
|
15
|
+
# @example Actual parameter equals expected parameter.
|
|
16
|
+
# object = mock()
|
|
17
|
+
# object.expects(:method_1).with(equals(2))
|
|
18
|
+
# object.method_1(2)
|
|
19
|
+
# # no error raised
|
|
20
|
+
#
|
|
21
|
+
# @example Actual parameter does not equal expected parameter.
|
|
22
|
+
# object = mock()
|
|
23
|
+
# object.expects(:method_1).with(equals(2))
|
|
24
|
+
# object.method_1(3)
|
|
25
|
+
# # error raised, because method_1 was not called with an +Object+ that equals 2
|
|
26
|
+
def equals(value)
|
|
27
|
+
Equals.new(value)
|
|
29
28
|
end
|
|
30
29
|
|
|
31
|
-
define_deprecated_matcher_method(:equals)
|
|
32
|
-
|
|
33
30
|
# Parameter matcher which matches when actual parameter equals expected value.
|
|
34
31
|
class Equals
|
|
35
|
-
include
|
|
32
|
+
include Base
|
|
36
33
|
|
|
37
34
|
# @private
|
|
38
35
|
def initialize(value)
|
|
@@ -50,7 +47,5 @@ module Mocha
|
|
|
50
47
|
@value.mocha_inspect
|
|
51
48
|
end
|
|
52
49
|
end
|
|
53
|
-
|
|
54
|
-
provide_deprecated_access_to(:Equals)
|
|
55
50
|
end
|
|
56
51
|
end
|
|
@@ -1,39 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'mocha/parameter_matchers/base'
|
|
2
|
-
require 'mocha/parameter_matchers/deprecations'
|
|
3
4
|
require 'uri'
|
|
4
5
|
require 'cgi'
|
|
5
6
|
|
|
6
7
|
module Mocha
|
|
7
8
|
module ParameterMatchers
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
EquivalentUri.new(uri)
|
|
29
|
-
end
|
|
9
|
+
# Matches a URI without regard to the ordering of parameters in the query string.
|
|
10
|
+
#
|
|
11
|
+
# @param [String] uri URI to match.
|
|
12
|
+
# @return [EquivalentUri] parameter matcher.
|
|
13
|
+
#
|
|
14
|
+
# @see Expectation#with
|
|
15
|
+
#
|
|
16
|
+
# @example Actual URI is equivalent.
|
|
17
|
+
# object = mock()
|
|
18
|
+
# object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
|
|
19
|
+
# object.method_1('http://example.com/foo?b=2&a=1')
|
|
20
|
+
# # no error raised
|
|
21
|
+
#
|
|
22
|
+
# @example Actual URI is not equivalent.
|
|
23
|
+
# object = mock()
|
|
24
|
+
# object.expects(:method_1).with(equivalent_uri('http://example.com/foo?a=1&b=2))
|
|
25
|
+
# object.method_1('http://example.com/foo?a=1&b=3')
|
|
26
|
+
# # error raised, because the query parameters were different
|
|
27
|
+
def equivalent_uri(uri)
|
|
28
|
+
EquivalentUri.new(uri)
|
|
30
29
|
end
|
|
31
30
|
|
|
32
|
-
define_deprecated_matcher_method(:equivalent_uri)
|
|
33
|
-
|
|
34
31
|
# Parameter matcher which matches URIs with equivalent query strings.
|
|
35
32
|
class EquivalentUri
|
|
36
|
-
include
|
|
33
|
+
include Base
|
|
37
34
|
|
|
38
35
|
# @private
|
|
39
36
|
def initialize(uri)
|
|
@@ -60,7 +57,5 @@ module Mocha
|
|
|
60
57
|
URI::Generic::COMPONENT.inject({}) { |h, k| h.merge(k => uri.__send__(k)) }.merge(query: query_hash)
|
|
61
58
|
end
|
|
62
59
|
end
|
|
63
|
-
|
|
64
|
-
provide_deprecated_access_to(:EquivalentUri)
|
|
65
60
|
end
|
|
66
61
|
end
|
|
@@ -1,40 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'mocha/parameter_matchers/base'
|
|
2
4
|
require 'mocha/parameter_matchers/all_of'
|
|
3
5
|
require 'mocha/parameter_matchers/has_entry'
|
|
4
|
-
require 'mocha/parameter_matchers/deprecations'
|
|
5
6
|
|
|
6
7
|
module Mocha
|
|
7
8
|
module ParameterMatchers
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
HasEntries.new(entries)
|
|
30
|
-
end
|
|
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.
|
|
17
|
+
# object = mock()
|
|
18
|
+
# object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
|
|
19
|
+
# object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)
|
|
20
|
+
# # no error raised
|
|
21
|
+
#
|
|
22
|
+
# @example Actual parameter does not contain all expected entries.
|
|
23
|
+
# object = mock()
|
|
24
|
+
# object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
|
|
25
|
+
# object.method_1('key_1' => 1, 'key_2' => 99)
|
|
26
|
+
# # error raised, because method_1 was not called with Hash containing entries: 'key_1' => 1, 'key_2' => 2
|
|
27
|
+
#
|
|
28
|
+
def has_entries(entries) # rubocop:disable Naming/PredicateName
|
|
29
|
+
HasEntries.new(entries)
|
|
31
30
|
end
|
|
32
31
|
|
|
33
|
-
define_deprecated_matcher_method(:has_entries)
|
|
34
|
-
|
|
35
32
|
# Parameter matcher which matches when actual parameter contains all expected +Hash+ entries.
|
|
36
33
|
class HasEntries
|
|
37
|
-
include
|
|
34
|
+
include Base
|
|
38
35
|
|
|
39
36
|
# @private
|
|
40
37
|
def initialize(entries, exact: false)
|
|
@@ -58,7 +55,5 @@ module Mocha
|
|
|
58
55
|
@exact ? @entries.mocha_inspect : "has_entries(#{@entries.mocha_inspect})"
|
|
59
56
|
end
|
|
60
57
|
end
|
|
61
|
-
|
|
62
|
-
provide_deprecated_access_to(:HasEntries)
|
|
63
58
|
end
|
|
64
59
|
end
|
|
@@ -1,67 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'mocha/parameter_matchers/base'
|
|
2
|
-
require 'mocha/parameter_matchers/deprecations'
|
|
3
4
|
|
|
4
5
|
module Mocha
|
|
5
6
|
module ParameterMatchers
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).'
|
|
55
|
-
end
|
|
56
|
-
HasEntry.new(key, value)
|
|
7
|
+
# Matches +Hash+ containing entry with +key+ and +value+.
|
|
8
|
+
#
|
|
9
|
+
# @overload def has_entry(key, value)
|
|
10
|
+
# @param [Object] key key for entry.
|
|
11
|
+
# @param [Object] value value for entry.
|
|
12
|
+
# @overload def has_entry(single_entry_hash)
|
|
13
|
+
# @param [Hash] single_entry_hash +Hash+ with single entry.
|
|
14
|
+
# @raise [ArgumentError] if +single_entry_hash+ does not contain exactly one entry.
|
|
15
|
+
#
|
|
16
|
+
# @return [HasEntry] parameter matcher.
|
|
17
|
+
#
|
|
18
|
+
# @see Expectation#with
|
|
19
|
+
#
|
|
20
|
+
# @example Actual parameter contains expected entry supplied as key and value.
|
|
21
|
+
# object = mock()
|
|
22
|
+
# object.expects(:method_1).with(has_entry('key_1', 1))
|
|
23
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
24
|
+
# # no error raised
|
|
25
|
+
#
|
|
26
|
+
# @example Actual parameter contains expected entry supplied as +Hash+ entry.
|
|
27
|
+
# object = mock()
|
|
28
|
+
# object.expects(:method_1).with(has_entry('key_1' => 1))
|
|
29
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
30
|
+
# # no error raised
|
|
31
|
+
#
|
|
32
|
+
# @example Actual parameter does not contain expected entry supplied as key and value.
|
|
33
|
+
# object = mock()
|
|
34
|
+
# object.expects(:method_1).with(has_entry('key_1', 1))
|
|
35
|
+
# object.method_1('key_1' => 2, 'key_2' => 1)
|
|
36
|
+
# # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
|
|
37
|
+
#
|
|
38
|
+
# @example Actual parameter does not contain expected entry supplied as +Hash+ entry.
|
|
39
|
+
#
|
|
40
|
+
# object = mock()
|
|
41
|
+
# object.expects(:method_1).with(has_entry('key_1' => 1))
|
|
42
|
+
# object.method_1('key_1' => 2, 'key_2' => 1)
|
|
43
|
+
# # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
|
|
44
|
+
#
|
|
45
|
+
def has_entry(*options) # rubocop:disable Naming/PredicateName
|
|
46
|
+
case options.length
|
|
47
|
+
when 0
|
|
48
|
+
raise ArgumentError, 'No arguments. Expecting at least one.'
|
|
49
|
+
when 1
|
|
50
|
+
key, value = parse_option(options[0])
|
|
51
|
+
when 2
|
|
52
|
+
key, value = options
|
|
53
|
+
else
|
|
54
|
+
raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).'
|
|
57
55
|
end
|
|
56
|
+
HasEntry.new(key, value)
|
|
58
57
|
end
|
|
59
58
|
|
|
60
|
-
define_deprecated_matcher_method(:has_entry)
|
|
61
|
-
|
|
62
59
|
# Parameter matcher which matches when actual parameter contains expected +Hash+ entry.
|
|
63
60
|
class HasEntry
|
|
64
|
-
include
|
|
61
|
+
include Base
|
|
65
62
|
|
|
66
63
|
# @private
|
|
67
64
|
def initialize(key, value)
|
|
@@ -73,6 +70,7 @@ module Mocha
|
|
|
73
70
|
def matches?(available_parameters)
|
|
74
71
|
parameter = available_parameters.shift
|
|
75
72
|
return false unless parameter.respond_to?(:keys) && parameter.respond_to?(:[])
|
|
73
|
+
|
|
76
74
|
matching_keys = parameter.keys.select { |key| @key.to_matcher.matches?([key]) }
|
|
77
75
|
matching_keys.any? { |key| @value.to_matcher.matches?([parameter[key]]) }
|
|
78
76
|
end
|
|
@@ -81,25 +79,25 @@ module Mocha
|
|
|
81
79
|
def mocha_inspect
|
|
82
80
|
"has_entry(#{@key.mocha_inspect} => #{@value.mocha_inspect})"
|
|
83
81
|
end
|
|
82
|
+
end
|
|
84
83
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
end
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
# @private
|
|
87
|
+
def parse_option(option)
|
|
88
|
+
case option
|
|
89
|
+
when Hash
|
|
90
|
+
case option.length
|
|
91
|
+
when 0
|
|
92
|
+
raise ArgumentError, 'Argument has no entries.'
|
|
93
|
+
when 1
|
|
94
|
+
option.first
|
|
97
95
|
else
|
|
98
|
-
raise ArgumentError, 'Argument
|
|
96
|
+
raise ArgumentError, 'Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.'
|
|
99
97
|
end
|
|
98
|
+
else
|
|
99
|
+
raise ArgumentError, 'Argument is not a Hash.'
|
|
100
100
|
end
|
|
101
101
|
end
|
|
102
|
-
|
|
103
|
-
provide_deprecated_access_to(:HasEntry)
|
|
104
102
|
end
|
|
105
103
|
end
|
|
@@ -1,38 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'mocha/parameter_matchers/base'
|
|
2
|
-
require 'mocha/parameter_matchers/deprecations'
|
|
3
4
|
|
|
4
5
|
module Mocha
|
|
5
6
|
module ParameterMatchers
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
HasKey.new(key)
|
|
28
|
-
end
|
|
7
|
+
# Matches +Hash+ containing +key+.
|
|
8
|
+
#
|
|
9
|
+
# @param [Object] key expected key.
|
|
10
|
+
# @return [HasKey] parameter matcher.
|
|
11
|
+
#
|
|
12
|
+
# @see Expectation#with
|
|
13
|
+
#
|
|
14
|
+
# @example Actual parameter contains entry with expected key.
|
|
15
|
+
# object = mock()
|
|
16
|
+
# object.expects(:method_1).with(has_key('key_1'))
|
|
17
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
18
|
+
# # no error raised
|
|
19
|
+
#
|
|
20
|
+
# @example Actual parameter does not contain entry with expected key.
|
|
21
|
+
# object = mock()
|
|
22
|
+
# object.expects(:method_1).with(has_key('key_1'))
|
|
23
|
+
# object.method_1('key_2' => 2)
|
|
24
|
+
# # error raised, because method_1 was not called with Hash containing key: 'key_1'
|
|
25
|
+
#
|
|
26
|
+
def has_key(key) # rubocop:disable Naming/PredicateName
|
|
27
|
+
HasKey.new(key)
|
|
29
28
|
end
|
|
30
29
|
|
|
31
|
-
define_deprecated_matcher_method(:has_key)
|
|
32
|
-
|
|
33
30
|
# Parameter matcher which matches when actual parameter contains +Hash+ entry with expected key.
|
|
34
31
|
class HasKey
|
|
35
|
-
include
|
|
32
|
+
include Base
|
|
36
33
|
|
|
37
34
|
# @private
|
|
38
35
|
def initialize(key)
|
|
@@ -43,6 +40,7 @@ module Mocha
|
|
|
43
40
|
def matches?(available_parameters)
|
|
44
41
|
parameter = available_parameters.shift
|
|
45
42
|
return false unless parameter.respond_to?(:keys)
|
|
43
|
+
|
|
46
44
|
parameter.keys.any? { |key| @key.to_matcher.matches?([key]) }
|
|
47
45
|
end
|
|
48
46
|
|
|
@@ -51,7 +49,5 @@ module Mocha
|
|
|
51
49
|
"has_key(#{@key.mocha_inspect})"
|
|
52
50
|
end
|
|
53
51
|
end
|
|
54
|
-
|
|
55
|
-
provide_deprecated_access_to(:HasKey)
|
|
56
52
|
end
|
|
57
53
|
end
|
|
@@ -1,38 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'mocha/parameter_matchers/base'
|
|
2
|
-
require 'mocha/parameter_matchers/deprecations'
|
|
3
4
|
|
|
4
5
|
module Mocha
|
|
5
6
|
module ParameterMatchers
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
HasKeys.new(*keys)
|
|
28
|
-
end
|
|
7
|
+
# Matches +Hash+ containing +keys+.
|
|
8
|
+
#
|
|
9
|
+
# @param [*Array<Object>] keys expected keys.
|
|
10
|
+
# @return [HasKeys] parameter matcher.
|
|
11
|
+
#
|
|
12
|
+
# @see Expectation#with
|
|
13
|
+
#
|
|
14
|
+
# @example Actual parameter contains entry with expected keys.
|
|
15
|
+
# object = mock()
|
|
16
|
+
# object.expects(:method_1).with(has_keys(:key_1, :key_2))
|
|
17
|
+
# object.method_1(:key_1 => 1, :key_2 => 2, :key_3 => 3)
|
|
18
|
+
# # no error raised
|
|
19
|
+
#
|
|
20
|
+
# @example Actual parameter does not contain all expected keys.
|
|
21
|
+
# object = mock()
|
|
22
|
+
# object.expects(:method_1).with(has_keys(:key_1, :key_2))
|
|
23
|
+
# object.method_1(:key_2 => 2)
|
|
24
|
+
# # error raised, because method_1 was not called with Hash containing key: :key_1
|
|
25
|
+
#
|
|
26
|
+
def has_keys(*keys) # rubocop:disable Naming/PredicateName
|
|
27
|
+
HasKeys.new(*keys)
|
|
29
28
|
end
|
|
30
29
|
|
|
31
|
-
define_deprecated_matcher_method(:has_keys)
|
|
32
|
-
|
|
33
30
|
# Parameter matcher which matches when actual parameter contains +Hash+ with all expected keys.
|
|
34
31
|
class HasKeys
|
|
35
|
-
include
|
|
32
|
+
include Base
|
|
36
33
|
|
|
37
34
|
# @private
|
|
38
35
|
def initialize(*keys)
|
|
@@ -53,10 +50,8 @@ module Mocha
|
|
|
53
50
|
|
|
54
51
|
# @private
|
|
55
52
|
def mocha_inspect
|
|
56
|
-
"has_keys(#{@keys.mocha_inspect(false)})"
|
|
53
|
+
"has_keys(#{@keys.mocha_inspect(wrapped: false)})"
|
|
57
54
|
end
|
|
58
55
|
end
|
|
59
|
-
|
|
60
|
-
provide_deprecated_access_to(:HasKeys)
|
|
61
56
|
end
|
|
62
57
|
end
|
|
@@ -1,38 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'mocha/parameter_matchers/base'
|
|
2
|
-
require 'mocha/parameter_matchers/deprecations'
|
|
3
4
|
|
|
4
5
|
module Mocha
|
|
5
6
|
module ParameterMatchers
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
HasValue.new(value)
|
|
28
|
-
end
|
|
7
|
+
# Matches +Hash+ containing +value+.
|
|
8
|
+
#
|
|
9
|
+
# @param [Object] value expected value.
|
|
10
|
+
# @return [HasValue] parameter matcher.
|
|
11
|
+
#
|
|
12
|
+
# @see Expectation#with
|
|
13
|
+
#
|
|
14
|
+
# @example Actual parameter contains entry with expected value.
|
|
15
|
+
# object = mock()
|
|
16
|
+
# object.expects(:method_1).with(has_value(1))
|
|
17
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
18
|
+
# # no error raised
|
|
19
|
+
#
|
|
20
|
+
# @example Actual parameter does not contain entry with expected value.
|
|
21
|
+
# object = mock()
|
|
22
|
+
# object.expects(:method_1).with(has_value(1))
|
|
23
|
+
# object.method_1('key_2' => 2)
|
|
24
|
+
# # error raised, because method_1 was not called with Hash containing value: 1
|
|
25
|
+
#
|
|
26
|
+
def has_value(value) # rubocop:disable Naming/PredicateName
|
|
27
|
+
HasValue.new(value)
|
|
29
28
|
end
|
|
30
29
|
|
|
31
|
-
define_deprecated_matcher_method(:has_value)
|
|
32
|
-
|
|
33
30
|
# Parameter matcher which matches when actual parameter contains +Hash+ entry with expected value.
|
|
34
31
|
class HasValue
|
|
35
|
-
include
|
|
32
|
+
include Base
|
|
36
33
|
|
|
37
34
|
# @private
|
|
38
35
|
def initialize(value)
|
|
@@ -43,6 +40,7 @@ module Mocha
|
|
|
43
40
|
def matches?(available_parameters)
|
|
44
41
|
parameter = available_parameters.shift
|
|
45
42
|
return false unless parameter.respond_to?(:values)
|
|
43
|
+
|
|
46
44
|
parameter.values.any? { |value| @value.to_matcher.matches?([value]) }
|
|
47
45
|
end
|
|
48
46
|
|
|
@@ -51,7 +49,5 @@ module Mocha
|
|
|
51
49
|
"has_value(#{@value.mocha_inspect})"
|
|
52
50
|
end
|
|
53
51
|
end
|
|
54
|
-
|
|
55
|
-
provide_deprecated_access_to(:HasValue)
|
|
56
52
|
end
|
|
57
53
|
end
|