mocha 3.0.0.pre.rc.1 → 3.0.0.pre.rc.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 +7 -37
- data/.yardopts +2 -0
- data/Gemfile +2 -5
- data/RELEASE.md +42 -0
- data/Rakefile +39 -19
- data/lib/mocha/any_instance_method.rb +4 -4
- data/lib/mocha/api.rb +1 -1
- data/lib/mocha/class_methods.rb +9 -5
- data/lib/mocha/configuration.rb +1 -1
- data/lib/mocha/deprecation.rb +1 -1
- data/lib/mocha/expectation.rb +2 -2
- data/lib/mocha/expectation_list.rb +1 -1
- data/lib/mocha/hooks.rb +4 -4
- data/lib/mocha/ignoring_warning.rb +20 -0
- data/lib/mocha/instance_method.rb +4 -4
- data/lib/mocha/integration/minitest/adapter.rb +4 -3
- data/lib/mocha/integration/minitest.rb +1 -1
- data/lib/mocha/integration/test_unit/adapter.rb +7 -4
- data/lib/mocha/integration/test_unit.rb +1 -1
- data/lib/mocha/integration.rb +5 -0
- data/lib/mocha/mock.rb +8 -4
- data/lib/mocha/mockery.rb +20 -12
- data/lib/mocha/object_methods.rb +13 -1
- data/lib/mocha/parameter_matchers/all_of.rb +24 -22
- data/lib/mocha/parameter_matchers/any_of.rb +30 -28
- data/lib/mocha/parameter_matchers/any_parameters.rb +22 -20
- data/lib/mocha/parameter_matchers/anything.rb +19 -17
- data/lib/mocha/parameter_matchers/{base.rb → base_methods.rb} +6 -4
- data/lib/mocha/parameter_matchers/equals.rb +25 -23
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +28 -24
- data/lib/mocha/parameter_matchers/has_entries.rb +25 -23
- data/lib/mocha/parameter_matchers/has_entry.rb +66 -66
- data/lib/mocha/parameter_matchers/has_key.rb +25 -23
- data/lib/mocha/parameter_matchers/has_keys.rb +25 -23
- data/lib/mocha/parameter_matchers/has_value.rb +25 -23
- data/lib/mocha/parameter_matchers/includes.rb +63 -61
- data/lib/mocha/parameter_matchers/instance_methods.rb +2 -2
- data/lib/mocha/parameter_matchers/instance_of.rb +25 -23
- data/lib/mocha/parameter_matchers/is_a.rb +26 -24
- data/lib/mocha/parameter_matchers/kind_of.rb +25 -23
- data/lib/mocha/parameter_matchers/not.rb +25 -23
- data/lib/mocha/parameter_matchers/optionally.rb +35 -33
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +2 -2
- data/lib/mocha/parameter_matchers/regexp_matches.rb +25 -23
- data/lib/mocha/parameter_matchers/responds_with.rb +49 -47
- data/lib/mocha/parameter_matchers/yaml_equivalent.rb +24 -22
- data/lib/mocha/parameter_matchers.rb +5 -2
- data/lib/mocha/state_machine.rb +1 -1
- data/lib/mocha/stubbed_method.rb +7 -7
- data/lib/mocha/version.rb +1 -1
- data/lib/mocha.rb +15 -0
- metadata +9 -4
|
@@ -1,64 +1,66 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mocha/parameter_matchers/
|
|
3
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
4
4
|
|
|
5
5
|
module Mocha
|
|
6
6
|
module ParameterMatchers
|
|
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
|
-
|
|
7
|
+
module Methods
|
|
8
|
+
# Matches +Hash+ containing entry with +key+ and +value+.
|
|
9
|
+
#
|
|
10
|
+
# @overload def has_entry(key, value)
|
|
11
|
+
# @param [Object] key key for entry.
|
|
12
|
+
# @param [Object] value value for entry.
|
|
13
|
+
# @overload def has_entry(single_entry_hash)
|
|
14
|
+
# @param [Hash] single_entry_hash +Hash+ with single entry.
|
|
15
|
+
# @raise [ArgumentError] if +single_entry_hash+ does not contain exactly one entry.
|
|
16
|
+
#
|
|
17
|
+
# @return [HasEntry] parameter matcher.
|
|
18
|
+
#
|
|
19
|
+
# @see Expectation#with
|
|
20
|
+
#
|
|
21
|
+
# @example Actual parameter contains expected entry supplied as key and value.
|
|
22
|
+
# object = mock()
|
|
23
|
+
# object.expects(:method_1).with(has_entry('key_1', 1))
|
|
24
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
25
|
+
# # no error raised
|
|
26
|
+
#
|
|
27
|
+
# @example Actual parameter contains expected entry supplied as +Hash+ entry.
|
|
28
|
+
# object = mock()
|
|
29
|
+
# object.expects(:method_1).with(has_entry('key_1' => 1))
|
|
30
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
31
|
+
# # no error raised
|
|
32
|
+
#
|
|
33
|
+
# @example Actual parameter does not contain expected entry supplied as key and value.
|
|
34
|
+
# object = mock()
|
|
35
|
+
# object.expects(:method_1).with(has_entry('key_1', 1))
|
|
36
|
+
# object.method_1('key_1' => 2, 'key_2' => 1)
|
|
37
|
+
# # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
|
|
38
|
+
#
|
|
39
|
+
# @example Actual parameter does not contain expected entry supplied as +Hash+ entry.
|
|
40
|
+
#
|
|
41
|
+
# object = mock()
|
|
42
|
+
# object.expects(:method_1).with(has_entry('key_1' => 1))
|
|
43
|
+
# object.method_1('key_1' => 2, 'key_2' => 1)
|
|
44
|
+
# # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
|
|
45
|
+
#
|
|
46
|
+
def has_entry(*options) # rubocop:disable Naming/PredicatePrefix
|
|
47
|
+
case options.length
|
|
48
|
+
when 0
|
|
49
|
+
raise ArgumentError, 'No arguments. Expecting at least one.'
|
|
50
|
+
when 1
|
|
51
|
+
key, value = HasEntry.parse_option(options[0])
|
|
52
|
+
when 2
|
|
53
|
+
key, value = options
|
|
54
|
+
else
|
|
55
|
+
raise ArgumentError, 'Too many arguments; use either a single argument (must be a Hash) or two arguments (a key and a value).'
|
|
56
|
+
end
|
|
57
|
+
HasEntry.new(key, value)
|
|
55
58
|
end
|
|
56
|
-
HasEntry.new(key, value)
|
|
57
59
|
end
|
|
58
60
|
|
|
59
61
|
# Parameter matcher which matches when actual parameter contains expected +Hash+ entry.
|
|
60
62
|
class HasEntry
|
|
61
|
-
include
|
|
63
|
+
include BaseMethods
|
|
62
64
|
|
|
63
65
|
# @private
|
|
64
66
|
def initialize(key, value)
|
|
@@ -79,24 +81,22 @@ module Mocha
|
|
|
79
81
|
def mocha_inspect
|
|
80
82
|
"has_entry(#{@key.mocha_inspect} => #{@value.mocha_inspect})"
|
|
81
83
|
end
|
|
82
|
-
end
|
|
83
84
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
85
|
+
# @private
|
|
86
|
+
def self.parse_option(option)
|
|
87
|
+
case option
|
|
88
|
+
when Hash
|
|
89
|
+
case option.length
|
|
90
|
+
when 0
|
|
91
|
+
raise ArgumentError, 'Argument has no entries.'
|
|
92
|
+
when 1
|
|
93
|
+
option.first
|
|
94
|
+
else
|
|
95
|
+
raise ArgumentError, 'Argument has multiple entries. Use Mocha::ParameterMatchers#has_entries instead.'
|
|
96
|
+
end
|
|
95
97
|
else
|
|
96
|
-
raise ArgumentError, 'Argument
|
|
98
|
+
raise ArgumentError, 'Argument is not a Hash.'
|
|
97
99
|
end
|
|
98
|
-
else
|
|
99
|
-
raise ArgumentError, 'Argument is not a Hash.'
|
|
100
100
|
end
|
|
101
101
|
end
|
|
102
102
|
end
|
|
@@ -1,35 +1,37 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mocha/parameter_matchers/
|
|
3
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
4
4
|
|
|
5
5
|
module Mocha
|
|
6
6
|
module ParameterMatchers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
module Methods
|
|
8
|
+
# Matches +Hash+ containing +key+.
|
|
9
|
+
#
|
|
10
|
+
# @param [Object] key expected key.
|
|
11
|
+
# @return [HasKey] parameter matcher.
|
|
12
|
+
#
|
|
13
|
+
# @see Expectation#with
|
|
14
|
+
#
|
|
15
|
+
# @example Actual parameter contains entry with expected key.
|
|
16
|
+
# object = mock()
|
|
17
|
+
# object.expects(:method_1).with(has_key('key_1'))
|
|
18
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
19
|
+
# # no error raised
|
|
20
|
+
#
|
|
21
|
+
# @example Actual parameter does not contain entry with expected key.
|
|
22
|
+
# object = mock()
|
|
23
|
+
# object.expects(:method_1).with(has_key('key_1'))
|
|
24
|
+
# object.method_1('key_2' => 2)
|
|
25
|
+
# # error raised, because method_1 was not called with Hash containing key: 'key_1'
|
|
26
|
+
#
|
|
27
|
+
def has_key(key) # rubocop:disable Naming/PredicatePrefix
|
|
28
|
+
HasKey.new(key)
|
|
29
|
+
end
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
# Parameter matcher which matches when actual parameter contains +Hash+ entry with expected key.
|
|
31
33
|
class HasKey
|
|
32
|
-
include
|
|
34
|
+
include BaseMethods
|
|
33
35
|
|
|
34
36
|
# @private
|
|
35
37
|
def initialize(key)
|
|
@@ -1,35 +1,37 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mocha/parameter_matchers/
|
|
3
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
4
4
|
|
|
5
5
|
module Mocha
|
|
6
6
|
module ParameterMatchers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
module Methods
|
|
8
|
+
# Matches +Hash+ containing +keys+.
|
|
9
|
+
#
|
|
10
|
+
# @param [*Array<Object>] keys expected keys.
|
|
11
|
+
# @return [HasKeys] parameter matcher.
|
|
12
|
+
#
|
|
13
|
+
# @see Expectation#with
|
|
14
|
+
#
|
|
15
|
+
# @example Actual parameter contains entry with expected keys.
|
|
16
|
+
# object = mock()
|
|
17
|
+
# object.expects(:method_1).with(has_keys(:key_1, :key_2))
|
|
18
|
+
# object.method_1(:key_1 => 1, :key_2 => 2, :key_3 => 3)
|
|
19
|
+
# # no error raised
|
|
20
|
+
#
|
|
21
|
+
# @example Actual parameter does not contain all expected keys.
|
|
22
|
+
# object = mock()
|
|
23
|
+
# object.expects(:method_1).with(has_keys(:key_1, :key_2))
|
|
24
|
+
# object.method_1(:key_2 => 2)
|
|
25
|
+
# # error raised, because method_1 was not called with Hash containing key: :key_1
|
|
26
|
+
#
|
|
27
|
+
def has_keys(*keys) # rubocop:disable Naming/PredicatePrefix
|
|
28
|
+
HasKeys.new(*keys)
|
|
29
|
+
end
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
# Parameter matcher which matches when actual parameter contains +Hash+ with all expected keys.
|
|
31
33
|
class HasKeys
|
|
32
|
-
include
|
|
34
|
+
include BaseMethods
|
|
33
35
|
|
|
34
36
|
# @private
|
|
35
37
|
def initialize(*keys)
|
|
@@ -1,35 +1,37 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mocha/parameter_matchers/
|
|
3
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
4
4
|
|
|
5
5
|
module Mocha
|
|
6
6
|
module ParameterMatchers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
module Methods
|
|
8
|
+
# Matches +Hash+ containing +value+.
|
|
9
|
+
#
|
|
10
|
+
# @param [Object] value expected value.
|
|
11
|
+
# @return [HasValue] parameter matcher.
|
|
12
|
+
#
|
|
13
|
+
# @see Expectation#with
|
|
14
|
+
#
|
|
15
|
+
# @example Actual parameter contains entry with expected value.
|
|
16
|
+
# object = mock()
|
|
17
|
+
# object.expects(:method_1).with(has_value(1))
|
|
18
|
+
# object.method_1('key_1' => 1, 'key_2' => 2)
|
|
19
|
+
# # no error raised
|
|
20
|
+
#
|
|
21
|
+
# @example Actual parameter does not contain entry with expected value.
|
|
22
|
+
# object = mock()
|
|
23
|
+
# object.expects(:method_1).with(has_value(1))
|
|
24
|
+
# object.method_1('key_2' => 2)
|
|
25
|
+
# # error raised, because method_1 was not called with Hash containing value: 1
|
|
26
|
+
#
|
|
27
|
+
def has_value(value) # rubocop:disable Naming/PredicatePrefix
|
|
28
|
+
HasValue.new(value)
|
|
29
|
+
end
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
# Parameter matcher which matches when actual parameter contains +Hash+ entry with expected value.
|
|
31
33
|
class HasValue
|
|
32
|
-
include
|
|
34
|
+
include BaseMethods
|
|
33
35
|
|
|
34
36
|
# @private
|
|
35
37
|
def initialize(value)
|
|
@@ -1,74 +1,76 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'mocha/parameter_matchers/all_of'
|
|
4
|
-
require 'mocha/parameter_matchers/
|
|
4
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
5
5
|
|
|
6
6
|
module Mocha
|
|
7
7
|
module ParameterMatchers
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
8
|
+
module Methods
|
|
9
|
+
# Matches any object that responds with +true+ to +include?(item)+
|
|
10
|
+
# for all items.
|
|
11
|
+
#
|
|
12
|
+
# @param [*Array] items expected items.
|
|
13
|
+
# @return [Includes] parameter matcher.
|
|
14
|
+
#
|
|
15
|
+
# @see Expectation#with
|
|
16
|
+
#
|
|
17
|
+
# @example Actual parameter includes all items.
|
|
18
|
+
# object = mock()
|
|
19
|
+
# object.expects(:method_1).with(includes('foo', 'bar'))
|
|
20
|
+
# object.method_1(['foo', 'bar', 'baz'])
|
|
21
|
+
# # no error raised
|
|
22
|
+
#
|
|
23
|
+
# @example Actual parameter does not include all items.
|
|
24
|
+
# object.method_1(['foo', 'baz'])
|
|
25
|
+
# # error raised, because ['foo', 'baz'] does not include 'bar'.
|
|
26
|
+
#
|
|
27
|
+
# @example Actual parameter includes item which matches nested matcher.
|
|
28
|
+
# object = mock()
|
|
29
|
+
# object.expects(:method_1).with(includes(has_key(:key)))
|
|
30
|
+
# object.method_1(['foo', 'bar', {key: 'baz'}])
|
|
31
|
+
# # no error raised
|
|
32
|
+
#
|
|
33
|
+
# @example Actual parameter does not include item matching nested matcher.
|
|
34
|
+
# object.method_1(['foo', 'bar', {:other_key => 'baz'}])
|
|
35
|
+
# # error raised, because no element matches `has_key(:key)` matcher
|
|
36
|
+
#
|
|
37
|
+
# @example Actual parameter is a String including substring.
|
|
38
|
+
# object = mock()
|
|
39
|
+
# object.expects(:method_1).with(includes('bar'))
|
|
40
|
+
# object.method_1('foobarbaz')
|
|
41
|
+
# # no error raised
|
|
42
|
+
#
|
|
43
|
+
# @example Actual parameter is a String not including substring.
|
|
44
|
+
# object.method_1('foobaz')
|
|
45
|
+
# # error raised, because 'foobaz' does not include 'bar'
|
|
46
|
+
#
|
|
47
|
+
# @example Actual parameter is a Hash including the given key.
|
|
48
|
+
# object = mock()
|
|
49
|
+
# object.expects(:method_1).with(includes(:bar))
|
|
50
|
+
# object.method_1({foo: 1, bar: 2})
|
|
51
|
+
# # no error raised
|
|
52
|
+
#
|
|
53
|
+
# @example Actual parameter is a Hash without the given key.
|
|
54
|
+
# object.method_1({foo: 1, baz: 2})
|
|
55
|
+
# # error raised, because hash does not include key 'bar'
|
|
56
|
+
#
|
|
57
|
+
# @example Actual parameter is a Hash with a key matching the given matcher.
|
|
58
|
+
# object = mock()
|
|
59
|
+
# object.expects(:method_1).with(includes(regexp_matches(/ar/)))
|
|
60
|
+
# object.method_1({'foo' => 1, 'bar' => 2})
|
|
61
|
+
# # no error raised
|
|
62
|
+
#
|
|
63
|
+
# @example Actual parameter is a Hash no key matching the given matcher.
|
|
64
|
+
# object.method_1({'foo' => 1, 'baz' => 3})
|
|
65
|
+
# # error raised, because hash does not include a key matching /ar/
|
|
66
|
+
def includes(*items)
|
|
67
|
+
Includes.new(*items)
|
|
68
|
+
end
|
|
67
69
|
end
|
|
68
70
|
|
|
69
71
|
# Parameter matcher which matches when actual parameter includes expected values.
|
|
70
72
|
class Includes
|
|
71
|
-
include
|
|
73
|
+
include BaseMethods
|
|
72
74
|
|
|
73
75
|
# @private
|
|
74
76
|
def initialize(*items)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mocha/parameter_matchers/
|
|
3
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
4
4
|
require 'mocha/parameter_matchers/equals'
|
|
5
5
|
require 'mocha/parameter_matchers/positional_or_keyword_hash'
|
|
6
6
|
|
|
@@ -10,7 +10,7 @@ module Mocha
|
|
|
10
10
|
module InstanceMethods
|
|
11
11
|
# @private
|
|
12
12
|
def to_matcher(expectation: nil, top_level: false, last: false)
|
|
13
|
-
if
|
|
13
|
+
if is_a?(BaseMethods)
|
|
14
14
|
self
|
|
15
15
|
elsif Hash === self && top_level
|
|
16
16
|
Mocha::ParameterMatchers::PositionalOrKeywordHash.new(self, expectation, last)
|
|
@@ -1,35 +1,37 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mocha/parameter_matchers/
|
|
3
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
4
4
|
|
|
5
5
|
module Mocha
|
|
6
6
|
module ParameterMatchers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
7
|
+
module Methods
|
|
8
|
+
# Matches any object that is an instance of +klass+
|
|
9
|
+
#
|
|
10
|
+
# @param [Class] klass expected class.
|
|
11
|
+
# @return [InstanceOf] parameter matcher.
|
|
12
|
+
#
|
|
13
|
+
# @see Expectation#with
|
|
14
|
+
# @see Kernel#instance_of?
|
|
15
|
+
#
|
|
16
|
+
# @example Actual parameter is an instance of +String+.
|
|
17
|
+
# object = mock()
|
|
18
|
+
# object.expects(:method_1).with(instance_of(String))
|
|
19
|
+
# object.method_1('string')
|
|
20
|
+
# # no error raised
|
|
21
|
+
#
|
|
22
|
+
# @example Actual parameter is not an instance of +String+.
|
|
23
|
+
# object = mock()
|
|
24
|
+
# object.expects(:method_1).with(instance_of(String))
|
|
25
|
+
# object.method_1(99)
|
|
26
|
+
# # error raised, because method_1 was not called with an instance of String
|
|
27
|
+
def instance_of(klass)
|
|
28
|
+
InstanceOf.new(klass)
|
|
29
|
+
end
|
|
28
30
|
end
|
|
29
31
|
|
|
30
32
|
# Parameter matcher which matches when actual parameter is an instance of the specified class.
|
|
31
33
|
class InstanceOf
|
|
32
|
-
include
|
|
34
|
+
include BaseMethods
|
|
33
35
|
|
|
34
36
|
# @private
|
|
35
37
|
def initialize(klass)
|
|
@@ -1,36 +1,38 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'mocha/parameter_matchers/
|
|
3
|
+
require 'mocha/parameter_matchers/base_methods'
|
|
4
4
|
|
|
5
5
|
module Mocha
|
|
6
6
|
module ParameterMatchers
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
7
|
+
module Methods
|
|
8
|
+
# Matches any object that is a +klass+.
|
|
9
|
+
#
|
|
10
|
+
# @param [Class] klass expected class.
|
|
11
|
+
# @return [IsA] parameter matcher.
|
|
12
|
+
#
|
|
13
|
+
# @see Expectation#with
|
|
14
|
+
# @see Kernel#is_a?
|
|
15
|
+
#
|
|
16
|
+
# @example Actual parameter is a +Integer+.
|
|
17
|
+
# object = mock()
|
|
18
|
+
# object.expects(:method_1).with(is_a(Integer))
|
|
19
|
+
# object.method_1(99)
|
|
20
|
+
# # no error raised
|
|
21
|
+
#
|
|
22
|
+
# @example Actual parameter is not a +Integer+.
|
|
23
|
+
# object = mock()
|
|
24
|
+
# object.expects(:method_1).with(is_a(Integer))
|
|
25
|
+
# object.method_1('string')
|
|
26
|
+
# # error raised, because method_1 was not called with an Integer
|
|
27
|
+
#
|
|
28
|
+
def is_a(klass) # rubocop:disable Naming/PredicatePrefix
|
|
29
|
+
IsA.new(klass)
|
|
30
|
+
end
|
|
29
31
|
end
|
|
30
32
|
|
|
31
33
|
# Parameter matcher which matches when actual parameter is a specific class.
|
|
32
34
|
class IsA
|
|
33
|
-
include
|
|
35
|
+
include BaseMethods
|
|
34
36
|
|
|
35
37
|
# @private
|
|
36
38
|
def initialize(klass)
|