mocha 2.1.0 → 2.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -1
- data/.yardopts +1 -1
- data/Gemfile +6 -1
- data/README.md +13 -18
- data/RELEASE.md +231 -0
- data/Rakefile +9 -9
- data/lib/mocha/api.rb +26 -7
- data/lib/mocha/cardinality.rb +4 -0
- data/lib/mocha/configuration.rb +8 -1
- data/lib/mocha/detection/{mini_test.rb → minitest.rb} +5 -5
- data/lib/mocha/detection/test_unit.rb +2 -2
- data/lib/mocha/expectation.rb +39 -7
- data/lib/mocha/expectation_error_factory.rb +2 -2
- data/lib/mocha/expectation_list.rb +8 -6
- data/lib/mocha/hooks.rb +10 -4
- data/lib/mocha/inspect.rb +13 -2
- data/lib/mocha/integration/{mini_test → minitest}/adapter.rb +23 -6
- data/lib/mocha/integration/{mini_test → minitest}/exception_translation.rb +2 -2
- data/lib/mocha/integration/minitest.rb +28 -0
- data/lib/mocha/integration/test_unit/adapter.rb +5 -0
- data/lib/mocha/minitest.rb +3 -3
- data/lib/mocha/mock.rb +37 -14
- data/lib/mocha/mockery.rb +13 -9
- data/lib/mocha/object_methods.rb +2 -2
- data/lib/mocha/parameter_matchers/all_of.rb +30 -21
- data/lib/mocha/parameter_matchers/any_of.rb +36 -27
- data/lib/mocha/parameter_matchers/any_parameters.rb +28 -19
- data/lib/mocha/parameter_matchers/anything.rb +25 -16
- data/lib/mocha/parameter_matchers/base.rb +28 -13
- data/lib/mocha/parameter_matchers/deprecations.rb +44 -0
- data/lib/mocha/parameter_matchers/equals.rb +31 -22
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +30 -21
- data/lib/mocha/parameter_matchers/has_entries.rb +38 -24
- data/lib/mocha/parameter_matchers/has_entry.rb +72 -65
- data/lib/mocha/parameter_matchers/has_key.rb +31 -22
- data/lib/mocha/parameter_matchers/has_keys.rb +31 -22
- data/lib/mocha/parameter_matchers/has_value.rb +31 -22
- data/lib/mocha/parameter_matchers/includes.rb +69 -60
- data/lib/mocha/parameter_matchers/instance_methods.rb +9 -10
- data/lib/mocha/parameter_matchers/instance_of.rb +31 -22
- data/lib/mocha/parameter_matchers/is_a.rb +32 -23
- data/lib/mocha/parameter_matchers/kind_of.rb +31 -22
- data/lib/mocha/parameter_matchers/not.rb +31 -22
- data/lib/mocha/parameter_matchers/optionally.rb +43 -33
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +9 -2
- data/lib/mocha/parameter_matchers/regexp_matches.rb +31 -22
- data/lib/mocha/parameter_matchers/responds_with.rb +58 -22
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +30 -21
- data/lib/mocha/parameter_matchers.rb +6 -2
- data/lib/mocha/parameters_matcher.rb +8 -4
- data/lib/mocha/ruby_version.rb +1 -0
- data/lib/mocha/version.rb +1 -1
- data/mocha.gemspec +9 -1
- metadata +16 -9
- data/lib/mocha/integration/mini_test.rb +0 -28
|
@@ -1,37 +1,44 @@
|
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
|
2
|
+
require 'mocha/parameter_matchers/deprecations'
|
|
2
3
|
|
|
3
4
|
module Mocha
|
|
4
5
|
module ParameterMatchers
|
|
5
|
-
|
|
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
|
-
|
|
6
|
+
module Methods
|
|
7
|
+
# Matches if any +matchers+ match.
|
|
8
|
+
#
|
|
9
|
+
# @param [*Array<BaseMethods>] matchers parameter matchers.
|
|
10
|
+
# @return [AnyOf] parameter matcher.
|
|
11
|
+
#
|
|
12
|
+
# @see Expectation#with
|
|
13
|
+
#
|
|
14
|
+
# @example One parameter matcher matches.
|
|
15
|
+
# object = mock()
|
|
16
|
+
# object.expects(:method_1).with(any_of(1, 3))
|
|
17
|
+
# object.method_1(1)
|
|
18
|
+
# # no error raised
|
|
19
|
+
#
|
|
20
|
+
# @example The other parameter matcher matches.
|
|
21
|
+
# object = mock()
|
|
22
|
+
# object.expects(:method_1).with(any_of(1, 3))
|
|
23
|
+
# object.method_1(3)
|
|
24
|
+
# # no error raised
|
|
25
|
+
#
|
|
26
|
+
# @example Neither parameter matcher matches.
|
|
27
|
+
# object = mock()
|
|
28
|
+
# object.expects(:method_1).with(any_of(1, 3))
|
|
29
|
+
# object.method_1(2)
|
|
30
|
+
# # error raised, because method_1 was not called with 1 or 3
|
|
31
|
+
def any_of(*matchers)
|
|
32
|
+
AnyOf.new(*matchers)
|
|
33
|
+
end
|
|
31
34
|
end
|
|
32
35
|
|
|
36
|
+
define_deprecated_matcher_method(:any_of)
|
|
37
|
+
|
|
33
38
|
# Parameter matcher which combines a number of other matchers using a logical OR.
|
|
34
|
-
class AnyOf
|
|
39
|
+
class AnyOf
|
|
40
|
+
include BaseMethods
|
|
41
|
+
|
|
35
42
|
# @private
|
|
36
43
|
def initialize(*matchers)
|
|
37
44
|
@matchers = matchers
|
|
@@ -48,5 +55,7 @@ module Mocha
|
|
|
48
55
|
"any_of(#{@matchers.map(&:mocha_inspect).join(', ')})"
|
|
49
56
|
end
|
|
50
57
|
end
|
|
58
|
+
|
|
59
|
+
provide_deprecated_access_to(:AnyOf)
|
|
51
60
|
end
|
|
52
61
|
end
|
|
@@ -1,29 +1,36 @@
|
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
|
2
|
+
require 'mocha/parameter_matchers/deprecations'
|
|
2
3
|
|
|
3
4
|
module Mocha
|
|
4
5
|
module ParameterMatchers
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
6
|
+
module Methods
|
|
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
|
|
12
|
+
#
|
|
13
|
+
# @example Any parameters will match.
|
|
14
|
+
# object = mock()
|
|
15
|
+
# object.expects(:method_1).with(any_parameters)
|
|
16
|
+
# object.method_1(1, 2, 3, 4)
|
|
17
|
+
# # no error raised
|
|
18
|
+
#
|
|
19
|
+
# object = mock()
|
|
20
|
+
# object.expects(:method_1).with(any_parameters)
|
|
21
|
+
# object.method_1(5, 6, 7, 8, 9, 0)
|
|
22
|
+
# # no error raised
|
|
23
|
+
def any_parameters
|
|
24
|
+
AnyParameters.new
|
|
25
|
+
end
|
|
23
26
|
end
|
|
24
27
|
|
|
28
|
+
define_deprecated_matcher_method(:any_parameters)
|
|
29
|
+
|
|
25
30
|
# Parameter matcher which always matches whatever the parameters.
|
|
26
|
-
class AnyParameters
|
|
31
|
+
class AnyParameters
|
|
32
|
+
include BaseMethods
|
|
33
|
+
|
|
27
34
|
# @private
|
|
28
35
|
def matches?(available_parameters)
|
|
29
36
|
until available_parameters.empty?
|
|
@@ -37,5 +44,7 @@ module Mocha
|
|
|
37
44
|
'any_parameters'
|
|
38
45
|
end
|
|
39
46
|
end
|
|
47
|
+
|
|
48
|
+
provide_deprecated_access_to(:AnyParameters)
|
|
40
49
|
end
|
|
41
50
|
end
|
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
|
2
|
+
require 'mocha/parameter_matchers/deprecations'
|
|
2
3
|
|
|
3
4
|
module Mocha
|
|
4
5
|
module ParameterMatchers
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
module Methods
|
|
7
|
+
# Matches any object.
|
|
8
|
+
#
|
|
9
|
+
# @return [Anything] parameter matcher.
|
|
10
|
+
#
|
|
11
|
+
# @see Expectation#with
|
|
12
|
+
#
|
|
13
|
+
# @example Any object will match.
|
|
14
|
+
# object = mock()
|
|
15
|
+
# object.expects(:method_1).with(anything)
|
|
16
|
+
# object.method_1('foo')
|
|
17
|
+
# object.method_1(789)
|
|
18
|
+
# object.method_1(:bar)
|
|
19
|
+
# # no error raised
|
|
20
|
+
def anything
|
|
21
|
+
Anything.new
|
|
22
|
+
end
|
|
20
23
|
end
|
|
21
24
|
|
|
25
|
+
define_deprecated_matcher_method(:anything)
|
|
26
|
+
|
|
22
27
|
# Parameter matcher which always matches a single parameter.
|
|
23
|
-
class Anything
|
|
28
|
+
class Anything
|
|
29
|
+
include BaseMethods
|
|
30
|
+
|
|
24
31
|
# @private
|
|
25
32
|
def matches?(available_parameters)
|
|
26
33
|
available_parameters.shift
|
|
@@ -32,5 +39,7 @@ module Mocha
|
|
|
32
39
|
'anything'
|
|
33
40
|
end
|
|
34
41
|
end
|
|
42
|
+
|
|
43
|
+
provide_deprecated_access_to(:Anything)
|
|
35
44
|
end
|
|
36
45
|
end
|
|
@@ -1,19 +1,17 @@
|
|
|
1
|
+
require 'mocha/deprecation'
|
|
2
|
+
require 'mocha/parameter_matchers/deprecations'
|
|
3
|
+
|
|
1
4
|
module Mocha
|
|
2
5
|
module ParameterMatchers
|
|
3
|
-
# @abstract
|
|
4
|
-
|
|
5
|
-
# @private
|
|
6
|
-
def to_matcher(_expectation = nil)
|
|
7
|
-
self
|
|
8
|
-
end
|
|
9
|
-
|
|
6
|
+
# @abstract Include and implement +#matches?+ and +#mocha_inspect+ to define a custom matcher. Also add a suitably named instance method to {Methods} to build an instance of the new matcher c.f. {Methods#equals}.
|
|
7
|
+
module BaseMethods
|
|
10
8
|
# A shorthand way of combining two matchers when both must match.
|
|
11
9
|
#
|
|
12
10
|
# Returns a new {AllOf} parameter matcher combining two matchers using a logical AND.
|
|
13
11
|
#
|
|
14
12
|
# This shorthand will not work with an implicit equals match. Instead, an explicit {Equals} matcher should be used.
|
|
15
13
|
#
|
|
16
|
-
# @param [
|
|
14
|
+
# @param [BaseMethods] other parameter matcher.
|
|
17
15
|
# @return [AllOf] parameter matcher.
|
|
18
16
|
#
|
|
19
17
|
# @see Expectation#with
|
|
@@ -21,12 +19,12 @@ module Mocha
|
|
|
21
19
|
# @example Alternative ways to combine matchers with a logical AND.
|
|
22
20
|
# object = mock()
|
|
23
21
|
# object.expects(:run).with(all_of(has_key(:foo), has_key(:bar)))
|
|
24
|
-
# object.run(:
|
|
22
|
+
# object.run(foo: 'foovalue', bar: 'barvalue')
|
|
25
23
|
#
|
|
26
24
|
# # is exactly equivalent to
|
|
27
25
|
#
|
|
28
26
|
# object.expects(:run).with(has_key(:foo) & has_key(:bar))
|
|
29
|
-
# object.run(:
|
|
27
|
+
# object.run(foo: 'foovalue', bar: 'barvalue)
|
|
30
28
|
def &(other)
|
|
31
29
|
AllOf.new(self, other)
|
|
32
30
|
end
|
|
@@ -37,7 +35,7 @@ module Mocha
|
|
|
37
35
|
#
|
|
38
36
|
# This shorthand will not work with an implicit equals match. Instead, an explicit {Equals} matcher should be used.
|
|
39
37
|
#
|
|
40
|
-
# @param [
|
|
38
|
+
# @param [BaseMethods] other parameter matcher.
|
|
41
39
|
# @return [AnyOf] parameter matcher.
|
|
42
40
|
#
|
|
43
41
|
# @see Expectation#with
|
|
@@ -45,12 +43,12 @@ module Mocha
|
|
|
45
43
|
# @example Alternative ways to combine matchers with a logical OR.
|
|
46
44
|
# object = mock()
|
|
47
45
|
# object.expects(:run).with(any_of(has_key(:foo), has_key(:bar)))
|
|
48
|
-
# object.run(:
|
|
46
|
+
# object.run(foo: 'foovalue')
|
|
49
47
|
#
|
|
50
48
|
# # is exactly equivalent to
|
|
51
49
|
#
|
|
52
50
|
# object.expects(:run).with(has_key(:foo) | has_key(:bar))
|
|
53
|
-
# object.run(:
|
|
51
|
+
# object.run(foo: 'foovalue')
|
|
54
52
|
#
|
|
55
53
|
# @example Using an explicit {Equals} matcher in combination with {#|}.
|
|
56
54
|
# object.expects(:run).with(equals(1) | equals(2))
|
|
@@ -61,5 +59,22 @@ module Mocha
|
|
|
61
59
|
AnyOf.new(self, other)
|
|
62
60
|
end
|
|
63
61
|
end
|
|
62
|
+
|
|
63
|
+
provide_deprecated_access_to(:BaseMethods)
|
|
64
|
+
|
|
65
|
+
# @deprecated Include +BaseMethods+ module instead.
|
|
66
|
+
class Base
|
|
67
|
+
include BaseMethods
|
|
68
|
+
|
|
69
|
+
# @private
|
|
70
|
+
def self.inherited(subclass)
|
|
71
|
+
super
|
|
72
|
+
Deprecation.warning(
|
|
73
|
+
"Include #{BaseMethods} module into #{subclass} instead of inheriting from #{self}."
|
|
74
|
+
)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
provide_deprecated_access_to(:Base)
|
|
64
79
|
end
|
|
65
80
|
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'mocha/deprecation'
|
|
2
|
+
require 'set'
|
|
3
|
+
|
|
4
|
+
module Mocha
|
|
5
|
+
module ParameterMatchers
|
|
6
|
+
module Deprecations
|
|
7
|
+
# @private
|
|
8
|
+
def const_missing(name)
|
|
9
|
+
if ParameterMatchers.access_deprecated?(name)
|
|
10
|
+
mod = ParameterMatchers.const_get(name)
|
|
11
|
+
Deprecation.warning(
|
|
12
|
+
"Referencing #{name} outside its namespace is deprecated. Use fully-qualified #{mod} instead."
|
|
13
|
+
)
|
|
14
|
+
mod
|
|
15
|
+
else
|
|
16
|
+
super(name)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @private
|
|
22
|
+
@classes_with_access_deprecated = Set.new
|
|
23
|
+
|
|
24
|
+
# @private
|
|
25
|
+
def self.provide_deprecated_access_to(name)
|
|
26
|
+
@classes_with_access_deprecated.add(name)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @private
|
|
30
|
+
def self.access_deprecated?(name)
|
|
31
|
+
@classes_with_access_deprecated.include?(name)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @private
|
|
35
|
+
def self.define_deprecated_matcher_method(name)
|
|
36
|
+
define_method(name) do |*args|
|
|
37
|
+
Deprecation.warning(
|
|
38
|
+
"Calling #{ParameterMatchers}##{name} is deprecated. Use #{Methods}##{name} instead."
|
|
39
|
+
)
|
|
40
|
+
Methods.instance_method(name).bind(self).call(*args)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -1,32 +1,39 @@
|
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
|
2
|
+
require 'mocha/parameter_matchers/deprecations'
|
|
2
3
|
|
|
3
4
|
module Mocha
|
|
4
5
|
module ParameterMatchers
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
6
|
+
module Methods
|
|
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)
|
|
28
|
+
end
|
|
26
29
|
end
|
|
27
30
|
|
|
31
|
+
define_deprecated_matcher_method(:equals)
|
|
32
|
+
|
|
28
33
|
# Parameter matcher which matches when actual parameter equals expected value.
|
|
29
|
-
class Equals
|
|
34
|
+
class Equals
|
|
35
|
+
include BaseMethods
|
|
36
|
+
|
|
30
37
|
# @private
|
|
31
38
|
def initialize(value)
|
|
32
39
|
@value = value
|
|
@@ -43,5 +50,7 @@ module Mocha
|
|
|
43
50
|
@value.mocha_inspect
|
|
44
51
|
end
|
|
45
52
|
end
|
|
53
|
+
|
|
54
|
+
provide_deprecated_access_to(:Equals)
|
|
46
55
|
end
|
|
47
56
|
end
|
|
@@ -1,33 +1,40 @@
|
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
|
2
|
+
require 'mocha/parameter_matchers/deprecations'
|
|
2
3
|
require 'uri'
|
|
3
4
|
require 'cgi'
|
|
4
5
|
|
|
5
6
|
module Mocha
|
|
6
7
|
module ParameterMatchers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
module Methods
|
|
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)
|
|
29
|
+
end
|
|
27
30
|
end
|
|
28
31
|
|
|
32
|
+
define_deprecated_matcher_method(:equivalent_uri)
|
|
33
|
+
|
|
29
34
|
# Parameter matcher which matches URIs with equivalent query strings.
|
|
30
|
-
class EquivalentUri
|
|
35
|
+
class EquivalentUri
|
|
36
|
+
include BaseMethods
|
|
37
|
+
|
|
31
38
|
# @private
|
|
32
39
|
def initialize(uri)
|
|
33
40
|
@uri = URI.parse(uri)
|
|
@@ -53,5 +60,7 @@ module Mocha
|
|
|
53
60
|
URI::Generic::COMPONENT.inject({}) { |h, k| h.merge(k => uri.__send__(k)) }.merge(query: query_hash)
|
|
54
61
|
end
|
|
55
62
|
end
|
|
63
|
+
|
|
64
|
+
provide_deprecated_access_to(:EquivalentUri)
|
|
56
65
|
end
|
|
57
66
|
end
|
|
@@ -1,50 +1,64 @@
|
|
|
1
1
|
require 'mocha/parameter_matchers/base'
|
|
2
2
|
require 'mocha/parameter_matchers/all_of'
|
|
3
3
|
require 'mocha/parameter_matchers/has_entry'
|
|
4
|
+
require 'mocha/parameter_matchers/deprecations'
|
|
4
5
|
|
|
5
6
|
module Mocha
|
|
6
7
|
module ParameterMatchers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
8
|
+
module Methods
|
|
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)
|
|
30
|
+
end
|
|
28
31
|
end
|
|
29
32
|
|
|
33
|
+
define_deprecated_matcher_method(:has_entries)
|
|
34
|
+
|
|
30
35
|
# Parameter matcher which matches when actual parameter contains all expected +Hash+ entries.
|
|
31
|
-
class HasEntries
|
|
36
|
+
class HasEntries
|
|
37
|
+
include BaseMethods
|
|
38
|
+
|
|
32
39
|
# @private
|
|
33
|
-
def initialize(entries)
|
|
40
|
+
def initialize(entries, exact: false)
|
|
34
41
|
@entries = entries
|
|
42
|
+
@exact = exact
|
|
35
43
|
end
|
|
36
44
|
|
|
37
45
|
# @private
|
|
38
46
|
def matches?(available_parameters)
|
|
39
47
|
parameter = available_parameters.shift
|
|
48
|
+
return false unless parameter
|
|
49
|
+
return false unless parameter.respond_to?(:keys)
|
|
50
|
+
return false if @exact && @entries.length != parameter.keys.length
|
|
51
|
+
|
|
40
52
|
has_entry_matchers = @entries.map { |key, value| HasEntry.new(key, value) }
|
|
41
53
|
AllOf.new(*has_entry_matchers).matches?([parameter])
|
|
42
54
|
end
|
|
43
55
|
|
|
44
56
|
# @private
|
|
45
57
|
def mocha_inspect
|
|
46
|
-
"has_entries(#{@entries.mocha_inspect})"
|
|
58
|
+
@exact ? @entries.mocha_inspect : "has_entries(#{@entries.mocha_inspect})"
|
|
47
59
|
end
|
|
48
60
|
end
|
|
61
|
+
|
|
62
|
+
provide_deprecated_access_to(:HasEntries)
|
|
49
63
|
end
|
|
50
64
|
end
|