mocha 1.16.1 → 2.0.0.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/Gemfile +3 -3
- data/RELEASE.md +25 -10
- data/Rakefile +12 -7
- data/lib/mocha/api.rb +5 -65
- data/lib/mocha/configuration.rb +31 -120
- data/lib/mocha/expectation.rb +55 -7
- data/lib/mocha/inspect.rb +2 -2
- data/lib/mocha/integration/mini_test.rb +10 -38
- data/lib/mocha/integration/test_unit/adapter.rb +1 -1
- data/lib/mocha/integration/test_unit.rb +10 -31
- data/lib/mocha/invocation.rb +2 -15
- data/lib/mocha/minitest.rb +1 -4
- data/lib/mocha/mock.rb +21 -17
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +0 -1
- data/lib/mocha/parameter_matchers/instance_methods.rb +9 -0
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +56 -0
- data/lib/mocha/parameters_matcher.rb +0 -1
- data/lib/mocha/ruby_version.rb +1 -9
- data/lib/mocha/stubbed_method.rb +3 -39
- data/lib/mocha/test_unit.rb +1 -4
- data/lib/mocha/version.rb +1 -1
- data/mocha.gemspec +1 -1
- metadata +6 -24
- data/init.rb +0 -1
- data/lib/mocha/integration/mini_test/nothing.rb +0 -19
- data/lib/mocha/integration/mini_test/version_13.rb +0 -54
- data/lib/mocha/integration/mini_test/version_140.rb +0 -54
- data/lib/mocha/integration/mini_test/version_141.rb +0 -65
- data/lib/mocha/integration/mini_test/version_142_to_172.rb +0 -65
- data/lib/mocha/integration/mini_test/version_200.rb +0 -66
- data/lib/mocha/integration/mini_test/version_201_to_222.rb +0 -66
- data/lib/mocha/integration/mini_test/version_2110_to_2111.rb +0 -70
- data/lib/mocha/integration/mini_test/version_2112_to_320.rb +0 -73
- data/lib/mocha/integration/mini_test/version_230_to_2101.rb +0 -68
- data/lib/mocha/integration/test_unit/gem_version_200.rb +0 -62
- data/lib/mocha/integration/test_unit/gem_version_201_to_202.rb +0 -62
- data/lib/mocha/integration/test_unit/gem_version_203_to_220.rb +0 -62
- data/lib/mocha/integration/test_unit/gem_version_230_to_250.rb +0 -68
- data/lib/mocha/integration/test_unit/nothing.rb +0 -19
- data/lib/mocha/integration/test_unit/ruby_version_186_and_above.rb +0 -63
- data/lib/mocha/integration.rb +0 -11
- data/lib/mocha/setup.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69781697a086ec84aecb54fa1f63dff7842796caa275af3f01e82695e7187b0d
|
4
|
+
data.tar.gz: f0aa6493ffccdeeb7f48b0aa8a329ba57612c4c01470642b0df8f23392857534
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddc7f9eab0a9506cc0e5a69ffc0bbc359d68c4e19f2c2cd207554d2b264854f77a38bed9fa7df6f0ac110b25cf26aaa9315ec092d709a0d2cdc8ec610c3cc2fc
|
7
|
+
data.tar.gz: 7b20ce12545006706f32ff11863802dd31fe4c766925e7ca2bd63828f8dc78e98cb692d9bf5558e6ea6f401929be236c52183bc42ecfff05a752c002d9414c2c
|
data/.rubocop.yml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
inherit_from: .rubocop_todo.yml
|
2
2
|
|
3
3
|
AllCops:
|
4
|
-
TargetRubyVersion: 2.2 # closest to required_ruby_version of '>=
|
4
|
+
TargetRubyVersion: 2.2 # closest to required_ruby_version of '>= 2.0'
|
5
5
|
|
6
6
|
# Even the reference in the documentation suggests that you should prefer
|
7
7
|
# `alias_method` vs `alias`, so I don't understand why that isn't the default.
|
data/Gemfile
CHANGED
@@ -3,9 +3,7 @@ source 'https://rubygems.org'
|
|
3
3
|
gemspec
|
4
4
|
|
5
5
|
# rubocop:disable Bundler/DuplicatedGem
|
6
|
-
if RUBY_VERSION < '2'
|
7
|
-
gem 'rake', '~> 12.2.1'
|
8
|
-
elsif RUBY_VERSION < '2.2'
|
6
|
+
if RUBY_VERSION < '2.2'
|
9
7
|
gem 'rake', '~> 12.3.3'
|
10
8
|
else
|
11
9
|
gem 'rake'
|
@@ -30,3 +28,5 @@ if ENV['MOCHA_GENERATE_DOCS']
|
|
30
28
|
gem 'redcarpet'
|
31
29
|
gem 'yard'
|
32
30
|
end
|
31
|
+
|
32
|
+
gem 'ruby2_keywords', '~> 0.0.5'
|
data/RELEASE.md
CHANGED
@@ -1,21 +1,36 @@
|
|
1
1
|
# Release Notes
|
2
2
|
|
3
|
-
##
|
3
|
+
## 2.0.0.alpha
|
4
4
|
|
5
5
|
### External changes
|
6
6
|
|
7
|
-
*
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
*
|
14
|
-
*
|
7
|
+
* Remove support for Ruby v1.9 - thanks to @wasabigeek (#552)
|
8
|
+
* Support strict keyword argument matching - see docs for `Expectation#with` & `Configuration#strict_keyword_argument_matching=` - thanks to @wasabigeek (#446,#535,#544,#562)
|
9
|
+
* Deprecate `Hash` args that don't strictly match (#563,981c31be)
|
10
|
+
* Drop support for older versions of test-unit - gem versions of test-unit earlier than v2.5.1 and versions of test-unit
|
11
|
+
from the Ruby v1.8 standard library are no longer supported (#540,969f4845)
|
12
|
+
* Drop support for older versions of minitest - versions of minitest earlier than v3.3.0 are no longer supported (#541,ca69dc9e)
|
13
|
+
* Remove deprecated `mocha/setup.rb` mechanism (642a0ff4)
|
14
|
+
* Add missing docs for `API#stub` parameter (257b4cb4)
|
15
|
+
* Remove optional reinstatement of v1.9 behaviour (#436,#438,#569,1473ee25)
|
16
|
+
* Remove deprecated methods in `Configuration` (#421,e7ff7528)
|
17
|
+
* Fail fast when mock receives invocations in another test (#440,#442,cb054d59)
|
18
|
+
* Improve docs re using matchers in `Expectation#with` (da7237cd)
|
19
|
+
* Expand `Expectation#with` docs re keyword arguments (fed6808d)
|
20
|
+
* Improve docs for `strict_keyword_argument_matching` (8d8f881d)
|
21
|
+
* Remove deprecated Rails plugin `init.rb` file (1c617175)
|
15
22
|
|
16
23
|
### Internal changes
|
17
24
|
|
18
|
-
*
|
25
|
+
* Separate linting from tests in terms of Rake tasks & CircleCI jobs - thanks to @wasabigeek (#556)
|
26
|
+
* Remove tests specific to Ruby v1.8 behaviour (46fca7ac, 3b369e99)
|
27
|
+
* Multi-line rubocop disable in `Mock#method_missing` (af2194c4)
|
28
|
+
* Remove unused arg for `HashMethods#mocha_inspect` (4f59e27f)
|
29
|
+
* Improve test runner assertions - failure vs error (eec7200a)
|
30
|
+
* Improve test coverage of `PositionalOrKeywordHash` (c294fe70)
|
31
|
+
* More consistent Test::Unit & Minitest integration (27dd3817)
|
32
|
+
* Remove redundant `require` statements (d82218a8,fa17b114)
|
33
|
+
* Add missing `require` statement (73493761)
|
19
34
|
|
20
35
|
## 1.15.0
|
21
36
|
|
data/Rakefile
CHANGED
@@ -5,9 +5,14 @@ end
|
|
5
5
|
require 'bundler/setup'
|
6
6
|
|
7
7
|
require 'rake/testtask'
|
8
|
+
begin
|
9
|
+
# Only available with default Gemfile and in Ruby >= v2.2
|
10
|
+
require 'rubocop/rake_task'
|
11
|
+
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
12
|
+
end
|
8
13
|
|
9
|
-
desc 'Run all tests'
|
10
|
-
task 'default' => ['test', 'test:performance']
|
14
|
+
desc 'Run all linters and tests'
|
15
|
+
task 'default' => ['lint', 'test', 'test:performance']
|
11
16
|
|
12
17
|
desc 'Run tests'
|
13
18
|
task 'test' do
|
@@ -76,13 +81,13 @@ namespace 'test' do # rubocop:disable Metrics/BlockLength
|
|
76
81
|
end
|
77
82
|
end
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
if RUBY_VERSION >= '2.2.0' && (defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby') && ENV['MOCHA_RUN_INTEGRATION_TESTS'].nil?
|
84
|
+
task 'lint' do
|
85
|
+
if defined?(RuboCop::RakeTask)
|
82
86
|
RuboCop::RakeTask.new
|
83
|
-
|
87
|
+
Rake::Task['rubocop'].invoke
|
88
|
+
else
|
89
|
+
puts 'RuboCop not available - skipping linting'
|
84
90
|
end
|
85
|
-
rescue LoadError # rubocop:disable Lint/HandleExceptions
|
86
91
|
end
|
87
92
|
|
88
93
|
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
data/lib/mocha/api.rb
CHANGED
@@ -52,7 +52,6 @@ module Mocha
|
|
52
52
|
#
|
53
53
|
# @overload def mock(name)
|
54
54
|
# @param [String, Symbol] name identifies mock object in error messages.
|
55
|
-
# @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that expected the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
|
56
55
|
# @overload def mock(expected_methods_vs_return_values = {})
|
57
56
|
# @param [Hash] expected_methods_vs_return_values expected method name symbols as keys and corresponding return values as values - these expectations are setup as if {Mock#expects} were called multiple times.
|
58
57
|
# @overload def mock(name, expected_methods_vs_return_values = {})
|
@@ -67,26 +66,8 @@ module Mocha
|
|
67
66
|
# # an error will be raised unless both Motor#start and Motor#stop have been called
|
68
67
|
# end
|
69
68
|
#
|
70
|
-
def mock(*arguments)
|
71
|
-
if
|
72
|
-
if arguments.first.is_a?(Symbol)
|
73
|
-
method_name = arguments[0]
|
74
|
-
Deprecation.warning(
|
75
|
-
"Explicitly include `#{method_name}` in Hash of expected methods vs return values,",
|
76
|
-
" e.g. `mock(:#{method_name} => nil)`."
|
77
|
-
)
|
78
|
-
if arguments[1]
|
79
|
-
Deprecation.warning(
|
80
|
-
"In this case the 2nd argument for `mock(:##{method_name}, ...)` is ignored,",
|
81
|
-
' but in the future a Hash of expected methods vs return values will be respected.'
|
82
|
-
)
|
83
|
-
end
|
84
|
-
elsif arguments.first.is_a?(String)
|
85
|
-
name = arguments.shift
|
86
|
-
end
|
87
|
-
elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
|
88
|
-
name = arguments.shift
|
89
|
-
end
|
69
|
+
def mock(*arguments)
|
70
|
+
name = arguments.shift.to_s if arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
|
90
71
|
expectations = arguments.shift || {}
|
91
72
|
mock = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
|
92
73
|
mock.expects(expectations)
|
@@ -99,11 +80,11 @@ module Mocha
|
|
99
80
|
#
|
100
81
|
# @overload def stub(name)
|
101
82
|
# @param [String, Symbol] name identifies mock object in error messages.
|
102
|
-
# @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that stubbed the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
|
103
83
|
# @overload def stub(stubbed_methods_vs_return_values = {})
|
104
84
|
# @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
|
105
85
|
# @overload def stub(name, stubbed_methods_vs_return_values = {})
|
106
86
|
# @param [String, Symbol] name identifies mock object in error messages.
|
87
|
+
# @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
|
107
88
|
#
|
108
89
|
# @example Using stubbed_methods_vs_return_values Hash to setup stubbed methods.
|
109
90
|
# def test_motor_starts_and_stops
|
@@ -112,33 +93,13 @@ module Mocha
|
|
112
93
|
# assert motor.stop
|
113
94
|
# # an error will not be raised even if either Motor#start or Motor#stop has not been called
|
114
95
|
# end
|
115
|
-
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
116
96
|
def stub(*arguments)
|
117
|
-
if
|
118
|
-
if arguments.first.is_a?(Symbol)
|
119
|
-
method_name = arguments[0]
|
120
|
-
Deprecation.warning(
|
121
|
-
"Explicitly include `#{method_name}` in Hash of stubbed methods vs return values,",
|
122
|
-
" e.g. `stub(:#{method_name} => nil)`."
|
123
|
-
)
|
124
|
-
if arguments[1]
|
125
|
-
Deprecation.warning(
|
126
|
-
"In this case the 2nd argument for `stub(:##{method_name}, ...)` is ignored,",
|
127
|
-
' but in the future a Hash of stubbed methods vs return values will be respected.'
|
128
|
-
)
|
129
|
-
end
|
130
|
-
elsif arguments.first.is_a?(String)
|
131
|
-
name = arguments.shift
|
132
|
-
end
|
133
|
-
elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
|
134
|
-
name = arguments.shift
|
135
|
-
end
|
97
|
+
name = arguments.shift.to_s if arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
|
136
98
|
expectations = arguments.shift || {}
|
137
99
|
stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
|
138
100
|
stub.stubs(expectations)
|
139
101
|
stub
|
140
102
|
end
|
141
|
-
# rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
142
103
|
|
143
104
|
# Builds a mock object that accepts calls to any method. By default it will return +nil+ for any method call.
|
144
105
|
#
|
@@ -146,7 +107,6 @@ module Mocha
|
|
146
107
|
#
|
147
108
|
# @overload def stub_everything(name)
|
148
109
|
# @param [String, Symbol] name identifies mock object in error messages.
|
149
|
-
# @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that stubbed the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
|
150
110
|
# @overload def stub_everything(stubbed_methods_vs_return_values = {})
|
151
111
|
# @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
|
152
112
|
# @overload def stub_everything(name, stubbed_methods_vs_return_values = {})
|
@@ -160,34 +120,14 @@ module Mocha
|
|
160
120
|
# assert_nil motor.irrelevant_method_2 # => no error raised
|
161
121
|
# assert motor.stop
|
162
122
|
# end
|
163
|
-
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
164
123
|
def stub_everything(*arguments)
|
165
|
-
if
|
166
|
-
if arguments.first.is_a?(Symbol)
|
167
|
-
method_name = arguments[0]
|
168
|
-
Deprecation.warning(
|
169
|
-
"Explicitly include `#{method_name}` in Hash of stubbed methods vs return values,",
|
170
|
-
" e.g. `stub_everything(:#{method_name} => nil)`."
|
171
|
-
)
|
172
|
-
if arguments[1]
|
173
|
-
Deprecation.warning(
|
174
|
-
"In this case the 2nd argument for `stub_everything(:##{method_name}, ...)` is ignored,",
|
175
|
-
' but in the future a Hash of stubbed methods vs return values will be respected.'
|
176
|
-
)
|
177
|
-
end
|
178
|
-
elsif arguments.first.is_a?(String)
|
179
|
-
name = arguments.shift
|
180
|
-
end
|
181
|
-
elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
|
182
|
-
name = arguments.shift
|
183
|
-
end
|
124
|
+
name = arguments.shift if arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
|
184
125
|
expectations = arguments.shift || {}
|
185
126
|
stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
|
186
127
|
stub.stub_everything
|
187
128
|
stub.stubs(expectations)
|
188
129
|
stub
|
189
130
|
end
|
190
|
-
# rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
191
131
|
|
192
132
|
# Builds a new sequence which can be used to constrain the order in which expectations can occur.
|
193
133
|
#
|
data/lib/mocha/configuration.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'mocha/ruby_version'
|
2
|
+
|
1
3
|
module Mocha
|
2
4
|
# Allows setting of configuration options. See {Configuration} for the available options.
|
3
5
|
#
|
@@ -43,7 +45,7 @@ module Mocha
|
|
43
45
|
:stubbing_non_public_method => :allow,
|
44
46
|
:stubbing_method_on_nil => :prevent,
|
45
47
|
:display_matching_invocations_on_failure => false,
|
46
|
-
:
|
48
|
+
:strict_keyword_argument_matching => false
|
47
49
|
}.freeze
|
48
50
|
|
49
51
|
attr_reader :options
|
@@ -248,145 +250,54 @@ module Mocha
|
|
248
250
|
@options[:display_matching_invocations_on_failure]
|
249
251
|
end
|
250
252
|
|
251
|
-
#
|
252
|
-
#
|
253
|
-
# Previously when {API#mock}, {API#stub}, or {API#stub_everything} were called with the first argument being a symbol, they built an *unnamed* mock object *and* expected or stubbed the method identified by the symbol argument; subsequent arguments were ignored.
|
254
|
-
# Now these methods build a *named* mock with the name specified by the symbol argument; *no* methods are expected or stubbed and subsequent arguments *are* taken into account.
|
253
|
+
# Perform strict keyword argument comparison. Only supported in Ruby >= v2.7.
|
255
254
|
#
|
256
|
-
#
|
257
|
-
# Now a +LocalJumpError+ is raised.
|
255
|
+
# When this option is set to +false+ a positional +Hash+ and a set of keyword arguments are treated the same during comparison, which can lead to false negatives in Ruby >= v3.0 (see examples below). However, a deprecation warning will be displayed if a positional +Hash+ matches a set of keyword arguments or vice versa. This is because {#strict_keyword_argument_matching=} will default to +true+ in the future.
|
258
256
|
#
|
259
|
-
#
|
257
|
+
# For more details on keyword arguments in Ruby v3, refer to {https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0 this article}.
|
260
258
|
#
|
261
|
-
#
|
262
|
-
# @deprecated Fix deprecation warnings caused by reliance on v1.9 behaviour and remove calls to this method.
|
259
|
+
# Note that +Hash+-related matchers such as {ParameterMatchers#has_value} or {ParameterMatchers#has_key} will still treat a positional +Hash+ and a set of keyword arguments the same, so false negatives are still possible when they are used.
|
263
260
|
#
|
264
|
-
#
|
265
|
-
# Mocha.configure do |c|
|
266
|
-
# c.reinstate_undocumented_behaviour_from_v1_9 = true
|
267
|
-
# end
|
261
|
+
# This configuration option is +false+ by default to enable gradual adoption, but will be +true+ by default in the future.
|
268
262
|
#
|
269
|
-
#
|
270
|
-
# foo.inspect # => #<Mock>
|
263
|
+
# @param [Boolean] value +true+ to enable strict keyword argument matching; +false+ by default.
|
271
264
|
#
|
272
|
-
#
|
273
|
-
# unsatisfied expectations:
|
274
|
-
# - expected exactly once, invoked never: #<Mock>.foo
|
265
|
+
# @example Loose keyword argument matching (default)
|
275
266
|
#
|
276
|
-
#
|
277
|
-
#
|
278
|
-
# c.reinstate_undocumented_behaviour_from_v1_9 = true
|
267
|
+
# class Example
|
268
|
+
# def foo(a, bar:); end
|
279
269
|
# end
|
280
270
|
#
|
281
|
-
#
|
282
|
-
# foo.
|
283
|
-
# foo
|
271
|
+
# example = Example.new
|
272
|
+
# example.expects(:foo).with('a', bar: 'b')
|
273
|
+
# example.foo('a', { bar: 'b' })
|
274
|
+
# # This passes the test, but would result in an ArgumentError in practice
|
284
275
|
#
|
285
|
-
# @example
|
286
|
-
# foo = mock('foo')
|
287
|
-
# foo.stubs(:my_method).yields(1, 2)
|
288
|
-
# foo.my_method # => raises LocalJumpError when no block is supplied
|
276
|
+
# @example Strict keyword argument matching
|
289
277
|
#
|
290
278
|
# Mocha.configure do |c|
|
291
|
-
# c.
|
279
|
+
# c.strict_keyword_argument_matching = true
|
292
280
|
# end
|
293
281
|
#
|
294
|
-
#
|
295
|
-
#
|
296
|
-
#
|
297
|
-
#
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
sentence1 = 'Configuration#reinstate_undocumented_behaviour_from_v1_9= is unnecessarily being set to false, because this is now the default value.'
|
306
|
-
sentence2 = 'Configuration#reinstate_undocumented_behaviour_from_v1_9= will be removed in the future, so you should avoid calling it.'
|
307
|
-
Deprecation.warning([sentence1, sentence2].join(' '))
|
308
|
-
end
|
309
|
-
@options[:reinstate_undocumented_behaviour_from_v1_9] = value
|
282
|
+
# class Example
|
283
|
+
# def foo(a, bar:); end
|
284
|
+
# end
|
285
|
+
#
|
286
|
+
# example = Example.new
|
287
|
+
# example.expects(:foo).with('a', bar: 'b')
|
288
|
+
# example.foo('a', { bar: 'b' })
|
289
|
+
# # This now fails as expected
|
290
|
+
def strict_keyword_argument_matching=(value)
|
291
|
+
raise 'Strict keyword argument matching requires Ruby 2.7 and above.' unless Mocha::RUBY_V27_PLUS
|
292
|
+
@options[:strict_keyword_argument_matching] = value
|
310
293
|
end
|
311
294
|
|
312
295
|
# @private
|
313
|
-
def
|
314
|
-
@options[:
|
296
|
+
def strict_keyword_argument_matching?
|
297
|
+
@options[:strict_keyword_argument_matching]
|
315
298
|
end
|
316
299
|
|
317
300
|
class << self
|
318
|
-
# Allow the specified +action+.
|
319
|
-
#
|
320
|
-
# @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
|
321
|
-
# @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
|
322
|
-
# @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:allow+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:allow+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
|
323
|
-
# * {#stubbing_method_unnecessarily=}
|
324
|
-
# * {#stubbing_method_on_non_mock_object=}
|
325
|
-
# * {#stubbing_non_existent_method=}
|
326
|
-
# * {#stubbing_non_public_method=}
|
327
|
-
# * {#stubbing_method_on_nil=}
|
328
|
-
def allow(action, &block)
|
329
|
-
if block_given?
|
330
|
-
Deprecation.warning("Use Mocha::Configuration.override(#{action}: :allow) with the same block")
|
331
|
-
else
|
332
|
-
Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :allow }")
|
333
|
-
end
|
334
|
-
change_config action, :allow, &block
|
335
|
-
end
|
336
|
-
|
337
|
-
# @private
|
338
|
-
def allow?(action)
|
339
|
-
configuration.allow?(action)
|
340
|
-
end
|
341
|
-
|
342
|
-
# Warn if the specified +action+ is attempted.
|
343
|
-
#
|
344
|
-
# @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
|
345
|
-
# @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
|
346
|
-
# @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:warn+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:warn+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
|
347
|
-
# * {#stubbing_method_unnecessarily=}
|
348
|
-
# * {#stubbing_method_on_non_mock_object=}
|
349
|
-
# * {#stubbing_non_existent_method=}
|
350
|
-
# * {#stubbing_non_public_method=}
|
351
|
-
# * {#stubbing_method_on_nil=}
|
352
|
-
def warn_when(action, &block)
|
353
|
-
if block_given?
|
354
|
-
Deprecation.warning("Use Mocha::Configuration.override(#{action}: :warn) with the same block")
|
355
|
-
else
|
356
|
-
Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :warn }")
|
357
|
-
end
|
358
|
-
change_config action, :warn, &block
|
359
|
-
end
|
360
|
-
|
361
|
-
# @private
|
362
|
-
def warn_when?(action)
|
363
|
-
configuration.warn_when?(action)
|
364
|
-
end
|
365
|
-
|
366
|
-
# Raise a {StubbingError} if the specified +action+ is attempted.
|
367
|
-
#
|
368
|
-
# @param [Symbol] action one of +:stubbing_method_unnecessarily+, +:stubbing_method_on_non_mock_object+, +:stubbing_non_existent_method+, +:stubbing_non_public_method+, +:stubbing_method_on_nil+.
|
369
|
-
# @yield optional block during which the configuration change will be changed before being returned to its original value at the end of the block.
|
370
|
-
# @deprecated If a block is supplied, call {.override} with a +Hash+ containing an entry with the +action+ as the key and +:prevent+ as the value. If no block is supplied, call the appropriate +action+ writer method with +value+ set to +:prevent+ via {Mocha.configure}. The writer method will be the one of the following corresponding to the +action+:
|
371
|
-
# * {#stubbing_method_unnecessarily=}
|
372
|
-
# * {#stubbing_method_on_non_mock_object=}
|
373
|
-
# * {#stubbing_non_existent_method=}
|
374
|
-
# * {#stubbing_non_public_method=}
|
375
|
-
# * {#stubbing_method_on_nil=}
|
376
|
-
def prevent(action, &block)
|
377
|
-
if block_given?
|
378
|
-
Deprecation.warning("Use Mocha::Configuration.override(#{action}: :prevent) with the same block")
|
379
|
-
else
|
380
|
-
Deprecation.warning("Use Mocha.configure { |c| c.#{action} = :prevent }")
|
381
|
-
end
|
382
|
-
change_config action, :prevent, &block
|
383
|
-
end
|
384
|
-
|
385
|
-
# @private
|
386
|
-
def prevent?(action)
|
387
|
-
configuration.prevent?(action)
|
388
|
-
end
|
389
|
-
|
390
301
|
# @private
|
391
302
|
def reset_configuration
|
392
303
|
@configuration = nil
|
data/lib/mocha/expectation.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'ruby2_keywords'
|
1
2
|
require 'mocha/method_matcher'
|
2
3
|
require 'mocha/parameters_matcher'
|
3
4
|
require 'mocha/expectation_error'
|
@@ -187,17 +188,26 @@ module Mocha
|
|
187
188
|
at_most(1)
|
188
189
|
end
|
189
190
|
|
190
|
-
# Modifies expectation so that the expected method must be called with +
|
191
|
+
# Modifies expectation so that the expected method must be called with +expected_parameters_or_matchers+.
|
191
192
|
#
|
192
|
-
# May be used with parameter matchers
|
193
|
+
# May be used with Ruby literals or variables for exact matching or with parameter matchers for less-specific matching, e.g. {ParameterMatchers#includes}, {ParameterMatchers#has_key}, etc. See {ParameterMatchers} for a list of all available parameter matchers.
|
193
194
|
#
|
194
|
-
#
|
195
|
+
# Positional arguments were separated from keyword arguments in Ruby v3 (see {https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0 this article}). In relation to this a new configuration option ({Configuration#strict_keyword_argument_matching=}) is available in Ruby >= 2.7.
|
196
|
+
#
|
197
|
+
# When {Configuration#strict_keyword_argument_matching=} is set to +false+ (which is currently the default), a positional +Hash+ and a set of keyword arguments passed to {#with} are treated the same for the purposes of parameter matching. However, a deprecation warning will be displayed if a positional +Hash+ matches a set of keyword arguments or vice versa. This is because {Configuration#strict_keyword_argument_matching=} will default to +true+ in the future.
|
198
|
+
#
|
199
|
+
# When {Configuration#strict_keyword_argument_matching=} is set to +true+, an actual positional +Hash+ will not match an expected set of keyword arguments; and vice versa, an actual set of keyword arguments will not match an expected positional +Hash+, i.e. the parameter matching is stricter.
|
200
|
+
#
|
201
|
+
# @see ParameterMatchers
|
202
|
+
# @see Configuration#strict_keyword_argument_matching=
|
203
|
+
#
|
204
|
+
# @param [*Array<Object,ParameterMatchers::Base>] expected_parameters_or_matchers expected parameter values or parameter matchers.
|
195
205
|
# @yield optional block specifying custom matching.
|
196
|
-
# @yieldparam [*Array] actual_parameters parameters with which expected method was invoked.
|
206
|
+
# @yieldparam [*Array<Object>] actual_parameters parameters with which expected method was invoked.
|
197
207
|
# @yieldreturn [Boolean] +true+ if +actual_parameters+ are acceptable.
|
198
208
|
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
199
209
|
#
|
200
|
-
# @example Expected method must be called with
|
210
|
+
# @example Expected method must be called with exact parameter values.
|
201
211
|
# object = mock()
|
202
212
|
# object.expects(:expected_method).with(:param1, :param2)
|
203
213
|
# object.expected_method(:param1, :param2)
|
@@ -208,6 +218,43 @@ module Mocha
|
|
208
218
|
# object.expected_method(:param3)
|
209
219
|
# # => verify fails
|
210
220
|
#
|
221
|
+
# @example Expected method must be called with parameters matching parameter matchers.
|
222
|
+
# object = mock()
|
223
|
+
# object.expects(:expected_method).with(includes('string2'), anything)
|
224
|
+
# object.expected_method(['string1', 'string2'], 'any-old-value')
|
225
|
+
# # => verify succeeds
|
226
|
+
#
|
227
|
+
# object = mock()
|
228
|
+
# object.expects(:expected_method).with(includes('string2'), anything)
|
229
|
+
# object.expected_method(['string1'], 'any-old-value')
|
230
|
+
# # => verify fails
|
231
|
+
#
|
232
|
+
# @example Loose keyword argument matching (default)
|
233
|
+
#
|
234
|
+
# class Example
|
235
|
+
# def foo(a, bar:); end
|
236
|
+
# end
|
237
|
+
#
|
238
|
+
# example = Example.new
|
239
|
+
# example.expects(:foo).with('a', bar: 'b')
|
240
|
+
# example.foo('a', { bar: 'b' })
|
241
|
+
# # This passes the test, but would result in an ArgumentError in practice
|
242
|
+
#
|
243
|
+
# @example Strict keyword argument matching
|
244
|
+
#
|
245
|
+
# Mocha.configure do |c|
|
246
|
+
# c.strict_keyword_argument_matching = true
|
247
|
+
# end
|
248
|
+
#
|
249
|
+
# class Example
|
250
|
+
# def foo(a, bar:); end
|
251
|
+
# end
|
252
|
+
#
|
253
|
+
# example = Example.new
|
254
|
+
# example.expects(:foo).with('a', bar: 'b')
|
255
|
+
# example.foo('a', { bar: 'b' })
|
256
|
+
# # This now fails as expected
|
257
|
+
#
|
211
258
|
# @example Expected method must be called with a value divisible by 4.
|
212
259
|
# object = mock()
|
213
260
|
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
|
@@ -218,10 +265,11 @@ module Mocha
|
|
218
265
|
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
|
219
266
|
# object.expected_method(17)
|
220
267
|
# # => verify fails
|
221
|
-
def with(*
|
222
|
-
@parameters_matcher = ParametersMatcher.new(
|
268
|
+
def with(*expected_parameters_or_matchers, &matching_block)
|
269
|
+
@parameters_matcher = ParametersMatcher.new(expected_parameters_or_matchers, &matching_block)
|
223
270
|
self
|
224
271
|
end
|
272
|
+
ruby2_keywords(:with)
|
225
273
|
|
226
274
|
# Modifies expectation so that the expected method must be called with a block.
|
227
275
|
#
|
data/lib/mocha/inspect.rb
CHANGED
@@ -18,9 +18,9 @@ module Mocha
|
|
18
18
|
end
|
19
19
|
|
20
20
|
module HashMethods
|
21
|
-
def mocha_inspect
|
21
|
+
def mocha_inspect
|
22
22
|
unwrapped = collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')
|
23
|
-
|
23
|
+
Hash.ruby2_keywords_hash?(self) ? unwrapped : "{#{unwrapped}}"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -1,54 +1,26 @@
|
|
1
1
|
require 'mocha/debug'
|
2
|
-
|
3
2
|
require 'mocha/detection/mini_test'
|
4
|
-
|
5
|
-
require 'mocha/integration/mini_test/nothing'
|
6
|
-
require 'mocha/integration/mini_test/version_13'
|
7
|
-
require 'mocha/integration/mini_test/version_140'
|
8
|
-
require 'mocha/integration/mini_test/version_141'
|
9
|
-
require 'mocha/integration/mini_test/version_142_to_172'
|
10
|
-
require 'mocha/integration/mini_test/version_200'
|
11
|
-
require 'mocha/integration/mini_test/version_201_to_222'
|
12
|
-
require 'mocha/integration/mini_test/version_230_to_2101'
|
13
|
-
require 'mocha/integration/mini_test/version_2110_to_2111'
|
14
|
-
require 'mocha/integration/mini_test/version_2112_to_320'
|
15
3
|
require 'mocha/integration/mini_test/adapter'
|
16
4
|
|
17
|
-
require 'mocha/deprecation'
|
18
|
-
|
19
5
|
module Mocha
|
20
6
|
module Integration
|
21
7
|
module MiniTest
|
22
8
|
def self.activate
|
23
|
-
|
24
|
-
|
9
|
+
target = Detection::MiniTest.testcase
|
10
|
+
return false unless target
|
25
11
|
|
12
|
+
mini_test_version = Gem::Version.new(Detection::MiniTest.version)
|
26
13
|
Debug.puts "Detected MiniTest version: #{mini_test_version}"
|
27
14
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
MiniTest::Version2110To2111,
|
32
|
-
MiniTest::Version230To2101,
|
33
|
-
MiniTest::Version201To222,
|
34
|
-
MiniTest::Version200,
|
35
|
-
MiniTest::Version142To172,
|
36
|
-
MiniTest::Version141,
|
37
|
-
MiniTest::Version140,
|
38
|
-
MiniTest::Version13,
|
39
|
-
MiniTest::Nothing
|
40
|
-
].detect { |m| m.applicable_to?(mini_test_version) }
|
15
|
+
unless MiniTest::Adapter.applicable_to?(mini_test_version)
|
16
|
+
raise 'Versions of minitest earlier than v3.3.0 are not supported.'
|
17
|
+
end
|
41
18
|
|
42
|
-
target
|
43
|
-
|
44
|
-
|
45
|
-
Deprecation.warning(
|
46
|
-
'Versions of minitest earlier than v3.3.0 will not be supported in future versions of Mocha.'
|
47
|
-
)
|
48
|
-
end
|
49
|
-
Debug.puts "Applying #{integration_module.description}"
|
50
|
-
target.send(:include, integration_module)
|
19
|
+
unless target < MiniTest::Adapter
|
20
|
+
Debug.puts "Applying #{MiniTest::Adapter.description}"
|
21
|
+
target.send(:include, MiniTest::Adapter)
|
51
22
|
end
|
23
|
+
|
52
24
|
true
|
53
25
|
end
|
54
26
|
end
|