mocha 1.16.1 → 2.7.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 +10 -13
- data/.yardopts +1 -2
- data/COPYING.md +2 -2
- data/Gemfile +7 -4
- data/MIT-LICENSE.md +1 -1
- data/README.md +23 -24
- data/RELEASE.md +294 -0
- data/Rakefile +23 -24
- data/lib/mocha/api.rb +30 -71
- data/lib/mocha/backtrace_filter.rb +2 -2
- data/lib/mocha/cardinality.rb +4 -0
- data/lib/mocha/configuration.rb +44 -126
- data/lib/mocha/debug.rb +2 -5
- 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 +99 -12
- 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 +15 -4
- data/lib/mocha/integration/{mini_test → minitest}/adapter.rb +21 -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 +9 -4
- data/lib/mocha/integration/test_unit.rb +10 -31
- data/lib/mocha/invocation.rb +2 -15
- data/lib/mocha/minitest.rb +3 -6
- data/lib/mocha/mock.rb +45 -18
- data/lib/mocha/mockery.rb +13 -9
- data/lib/mocha/names.rb +1 -1
- data/lib/mocha/object_methods.rb +2 -2
- data/lib/mocha/parameter_matchers/base.rb +4 -9
- data/lib/mocha/parameter_matchers/equivalent_uri.rb +1 -2
- data/lib/mocha/parameter_matchers/has_entries.rb +7 -2
- data/lib/mocha/parameter_matchers/includes.rb +3 -3
- data/lib/mocha/parameter_matchers/instance_methods.rb +10 -2
- data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +66 -0
- data/lib/mocha/parameter_matchers/responds_with.rb +32 -5
- data/lib/mocha/parameters_matcher.rb +10 -6
- data/lib/mocha/ruby_version.rb +2 -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 +11 -1
- metadata +31 -31
- 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/mini_test.rb +0 -56
- 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
- data/yard-templates/default/layout/html/google_analytics.erb +0 -8
- data/yard-templates/default/layout/html/setup.rb +0 -5
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'
|
@@ -11,6 +12,7 @@ require 'mocha/change_state_side_effect'
|
|
11
12
|
require 'mocha/cardinality'
|
12
13
|
require 'mocha/configuration'
|
13
14
|
require 'mocha/block_matcher'
|
15
|
+
require 'mocha/backtrace_filter'
|
14
16
|
|
15
17
|
module Mocha
|
16
18
|
# Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}.
|
@@ -187,17 +189,31 @@ module Mocha
|
|
187
189
|
at_most(1)
|
188
190
|
end
|
189
191
|
|
190
|
-
# Modifies expectation so that the expected method must be called with +
|
192
|
+
# Modifies expectation so that the expected method must be called with +expected_parameters_or_matchers+.
|
191
193
|
#
|
192
|
-
# May be used with parameter matchers
|
194
|
+
# 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
195
|
#
|
194
|
-
#
|
196
|
+
# Alternatively a block argument can be passed to {#with} to implement custom parameter matching. The block receives the +*actual_parameters+ as its arguments and should return +true+ if they are acceptable or +false+ otherwise. See the example below where a method is expected to be called with a value divisible by 4.
|
197
|
+
# The block argument takes precedence over +expected_parameters_or_matchers+. The block may be called multiple times per invocation of the expected method and so it should be idempotent.
|
198
|
+
#
|
199
|
+
# Note that if {#with} is called multiple times on the same expectation, the last call takes precedence; other calls are ignored.
|
200
|
+
#
|
201
|
+
# 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.
|
202
|
+
#
|
203
|
+
# 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.
|
204
|
+
#
|
205
|
+
# 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.
|
206
|
+
#
|
207
|
+
# @see ParameterMatchers
|
208
|
+
# @see Configuration#strict_keyword_argument_matching=
|
209
|
+
#
|
210
|
+
# @param [Array<Object,ParameterMatchers::Base>] expected_parameters_or_matchers expected parameter values or parameter matchers.
|
195
211
|
# @yield optional block specifying custom matching.
|
196
|
-
# @yieldparam [
|
197
|
-
# @yieldreturn [Boolean] +true+ if +actual_parameters+ are acceptable.
|
212
|
+
# @yieldparam [Array<Object>] actual_parameters parameters with which expected method was invoked.
|
213
|
+
# @yieldreturn [Boolean] +true+ if +actual_parameters+ are acceptable; +false+ otherwise.
|
198
214
|
# @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
|
199
215
|
#
|
200
|
-
# @example Expected method must be called with
|
216
|
+
# @example Expected method must be called with exact parameter values.
|
201
217
|
# object = mock()
|
202
218
|
# object.expects(:expected_method).with(:param1, :param2)
|
203
219
|
# object.expected_method(:param1, :param2)
|
@@ -208,7 +224,44 @@ module Mocha
|
|
208
224
|
# object.expected_method(:param3)
|
209
225
|
# # => verify fails
|
210
226
|
#
|
211
|
-
# @example Expected method must be called with
|
227
|
+
# @example Expected method must be called with parameters matching parameter matchers.
|
228
|
+
# object = mock()
|
229
|
+
# object.expects(:expected_method).with(includes('string2'), anything)
|
230
|
+
# object.expected_method(['string1', 'string2'], 'any-old-value')
|
231
|
+
# # => verify succeeds
|
232
|
+
#
|
233
|
+
# object = mock()
|
234
|
+
# object.expects(:expected_method).with(includes('string2'), anything)
|
235
|
+
# object.expected_method(['string1'], 'any-old-value')
|
236
|
+
# # => verify fails
|
237
|
+
#
|
238
|
+
# @example Loose keyword argument matching (default)
|
239
|
+
#
|
240
|
+
# class Example
|
241
|
+
# def foo(a, bar:); end
|
242
|
+
# end
|
243
|
+
#
|
244
|
+
# example = Example.new
|
245
|
+
# example.expects(:foo).with('a', bar: 'b')
|
246
|
+
# example.foo('a', { bar: 'b' })
|
247
|
+
# # This passes the test, but would result in an ArgumentError in practice
|
248
|
+
#
|
249
|
+
# @example Strict keyword argument matching
|
250
|
+
#
|
251
|
+
# Mocha.configure do |c|
|
252
|
+
# c.strict_keyword_argument_matching = true
|
253
|
+
# end
|
254
|
+
#
|
255
|
+
# class Example
|
256
|
+
# def foo(a, bar:); end
|
257
|
+
# end
|
258
|
+
#
|
259
|
+
# example = Example.new
|
260
|
+
# example.expects(:foo).with('a', bar: 'b')
|
261
|
+
# example.foo('a', { bar: 'b' })
|
262
|
+
# # This now fails as expected
|
263
|
+
#
|
264
|
+
# @example Using a block argument to expect the method to be called with a value divisible by 4.
|
212
265
|
# object = mock()
|
213
266
|
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
|
214
267
|
# object.expected_method(16)
|
@@ -218,10 +271,27 @@ module Mocha
|
|
218
271
|
# object.expects(:expected_method).with() { |value| value % 4 == 0 }
|
219
272
|
# object.expected_method(17)
|
220
273
|
# # => verify fails
|
221
|
-
|
222
|
-
|
274
|
+
#
|
275
|
+
# @example Extracting a custom matcher into an instance method on the test class.
|
276
|
+
# class MyTest < Minitest::Test
|
277
|
+
# def test_expected_method_is_called_with_a_value_divisible_by_4
|
278
|
+
# object = mock()
|
279
|
+
# object.expects(:expected_method).with(&method(:divisible_by_4))
|
280
|
+
# object.expected_method(16)
|
281
|
+
# # => verify succeeds
|
282
|
+
# end
|
283
|
+
#
|
284
|
+
# private
|
285
|
+
#
|
286
|
+
# def divisible_by_4(value)
|
287
|
+
# value % 4 == 0
|
288
|
+
# end
|
289
|
+
# end
|
290
|
+
def with(*expected_parameters_or_matchers, &matching_block)
|
291
|
+
@parameters_matcher = ParametersMatcher.new(expected_parameters_or_matchers, self, &matching_block)
|
223
292
|
self
|
224
293
|
end
|
294
|
+
ruby2_keywords(:with)
|
225
295
|
|
226
296
|
# Modifies expectation so that the expected method must be called with a block.
|
227
297
|
#
|
@@ -583,14 +653,20 @@ module Mocha
|
|
583
653
|
@ordering_constraints.all?(&:allows_invocation_now?)
|
584
654
|
end
|
585
655
|
|
656
|
+
# @private
|
657
|
+
def ordering_constraints_not_allowing_invocation_now
|
658
|
+
@ordering_constraints.reject(&:allows_invocation_now?)
|
659
|
+
end
|
660
|
+
|
586
661
|
# @private
|
587
662
|
def matches_method?(method_name)
|
588
663
|
@method_matcher.match?(method_name)
|
589
664
|
end
|
590
665
|
|
591
666
|
# @private
|
592
|
-
def match?(invocation)
|
593
|
-
@method_matcher.match?(invocation.method_name) && @parameters_matcher.match?(invocation.arguments) && @block_matcher.match?(invocation.block)
|
667
|
+
def match?(invocation, ignoring_order: false)
|
668
|
+
order_independent_match = @method_matcher.match?(invocation.method_name) && @parameters_matcher.match?(invocation.arguments) && @block_matcher.match?(invocation.block)
|
669
|
+
ignoring_order ? order_independent_match : order_independent_match && in_correct_order?
|
594
670
|
end
|
595
671
|
|
596
672
|
# @private
|
@@ -598,6 +674,11 @@ module Mocha
|
|
598
674
|
@cardinality.invocations_allowed?
|
599
675
|
end
|
600
676
|
|
677
|
+
# @private
|
678
|
+
def invocations_never_allowed?
|
679
|
+
@cardinality.invocations_never_allowed?
|
680
|
+
end
|
681
|
+
|
601
682
|
# @private
|
602
683
|
def satisfied?
|
603
684
|
@cardinality.satisfied?
|
@@ -625,7 +706,7 @@ module Mocha
|
|
625
706
|
def inspect
|
626
707
|
address = __id__ * 2
|
627
708
|
address += 0x100000000 if address < 0
|
628
|
-
"#<Expectation:0x#{format('
|
709
|
+
"#<Expectation:0x#{format('%<address>x', address: address)} #{mocha_inspect} >"
|
629
710
|
end
|
630
711
|
|
631
712
|
# @private
|
@@ -644,5 +725,11 @@ module Mocha
|
|
644
725
|
signature << " #{@block_matcher.mocha_inspect}" if @block_matcher.mocha_inspect
|
645
726
|
signature
|
646
727
|
end
|
728
|
+
|
729
|
+
# @private
|
730
|
+
def definition_location
|
731
|
+
filter = BacktraceFilter.new
|
732
|
+
filter.filtered(backtrace)[0]
|
733
|
+
end
|
647
734
|
end
|
648
735
|
end
|
@@ -6,9 +6,9 @@ module Mocha
|
|
6
6
|
#
|
7
7
|
# This class should only be used by authors of test libraries and not by typical "users" of Mocha.
|
8
8
|
#
|
9
|
-
# For example, it is used by +Mocha::Integration::
|
9
|
+
# For example, it is used by +Mocha::Integration::Minitest::Adapter+ in order to have Mocha raise a +Minitest::Assertion+ which can then be sensibly handled by +Minitest::Unit::TestCase+.
|
10
10
|
#
|
11
|
-
# @see Mocha::Integration::
|
11
|
+
# @see Mocha::Integration::Minitest::Adapter
|
12
12
|
class ExpectationErrorFactory
|
13
13
|
class << self
|
14
14
|
# @!attribute exception_class
|
@@ -17,14 +17,18 @@ module Mocha
|
|
17
17
|
@expectations.any? { |expectation| expectation.matches_method?(method_name) }
|
18
18
|
end
|
19
19
|
|
20
|
-
def match(invocation)
|
21
|
-
matching_expectations(invocation).first
|
20
|
+
def match(invocation, ignoring_order: false)
|
21
|
+
matching_expectations(invocation, ignoring_order: ignoring_order).first
|
22
22
|
end
|
23
23
|
|
24
24
|
def match_allowing_invocation(invocation)
|
25
25
|
matching_expectations(invocation).detect(&:invocations_allowed?)
|
26
26
|
end
|
27
27
|
|
28
|
+
def match_never_allowing_invocation(invocation)
|
29
|
+
matching_expectations(invocation).detect(&:invocations_never_allowed?)
|
30
|
+
end
|
31
|
+
|
28
32
|
def verified?(assertion_counter = nil)
|
29
33
|
@expectations.all? { |expectation| expectation.verified?(assertion_counter) }
|
30
34
|
end
|
@@ -49,10 +53,8 @@ module Mocha
|
|
49
53
|
self.class.new(to_a + other.to_a)
|
50
54
|
end
|
51
55
|
|
52
|
-
|
53
|
-
|
54
|
-
def matching_expectations(invocation)
|
55
|
-
@expectations.select { |e| e.match?(invocation) }
|
56
|
+
def matching_expectations(invocation, ignoring_order: false)
|
57
|
+
@expectations.select { |e| e.match?(invocation, ignoring_order: ignoring_order) }
|
56
58
|
end
|
57
59
|
end
|
58
60
|
end
|
data/lib/mocha/hooks.rb
CHANGED
@@ -7,12 +7,12 @@ module Mocha
|
|
7
7
|
#
|
8
8
|
# This module is provided as part of the +Mocha::API+ module and is therefore part of the public API, but should only be used by authors of test libraries and not by typical "users" of Mocha.
|
9
9
|
#
|
10
|
-
# Integration with Test::Unit and
|
10
|
+
# Integration with Test::Unit and Minitest are provided as part of Mocha, because they are (or were once) part of the Ruby standard library. Integration with other test libraries is not provided as *part* of Mocha, but is supported by means of the methods in this module.
|
11
11
|
#
|
12
12
|
# See the code in the +Adapter+ modules for examples of how to use the methods in this module. +Mocha::ExpectationErrorFactory+ may be used if you want +Mocha+ to raise a different type of exception.
|
13
13
|
#
|
14
14
|
# @see Mocha::Integration::TestUnit::Adapter
|
15
|
-
# @see Mocha::Integration::
|
15
|
+
# @see Mocha::Integration::Minitest::Adapter
|
16
16
|
# @see Mocha::ExpectationErrorFactory
|
17
17
|
# @see Mocha::API
|
18
18
|
module Hooks
|
@@ -35,8 +35,14 @@ module Mocha
|
|
35
35
|
# Resets Mocha after a test (only for use by authors of test libraries).
|
36
36
|
#
|
37
37
|
# This method should be called after each individual test has finished (including after any "teardown" code).
|
38
|
-
def mocha_teardown
|
39
|
-
Mockery.teardown
|
38
|
+
def mocha_teardown(origin = mocha_test_name)
|
39
|
+
Mockery.teardown(origin)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a string representing the unit test name, to be included in some Mocha
|
43
|
+
# to help track down potential bugs.
|
44
|
+
def mocha_test_name
|
45
|
+
nil
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|
data/lib/mocha/inspect.rb
CHANGED
@@ -6,7 +6,7 @@ module Mocha
|
|
6
6
|
def mocha_inspect
|
7
7
|
address = __id__ * 2
|
8
8
|
address += 0x100000000 if address < 0
|
9
|
-
inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('
|
9
|
+
inspect =~ /#</ ? "#<#{self.class}:0x#{Kernel.format('%<address>x', address: address)}>" : inspect
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -18,9 +18,20 @@ module Mocha
|
|
18
18
|
end
|
19
19
|
|
20
20
|
module HashMethods
|
21
|
-
def mocha_inspect
|
22
|
-
|
23
|
-
|
21
|
+
def mocha_inspect
|
22
|
+
if Hash.ruby2_keywords_hash?(self)
|
23
|
+
collect do |key, value|
|
24
|
+
case key
|
25
|
+
when Symbol
|
26
|
+
"#{key}: #{value.mocha_inspect}"
|
27
|
+
else
|
28
|
+
"#{key.mocha_inspect} => #{value.mocha_inspect}"
|
29
|
+
end
|
30
|
+
end.join(', ')
|
31
|
+
else
|
32
|
+
unwrapped = collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')
|
33
|
+
"{#{unwrapped}}"
|
34
|
+
end
|
24
35
|
end
|
25
36
|
end
|
26
37
|
|
@@ -4,26 +4,26 @@ require 'mocha/expectation_error_factory'
|
|
4
4
|
|
5
5
|
module Mocha
|
6
6
|
module Integration
|
7
|
-
module
|
8
|
-
# Integrates Mocha into recent versions of
|
7
|
+
module Minitest
|
8
|
+
# Integrates Mocha into recent versions of Minitest.
|
9
9
|
#
|
10
10
|
# See the source code for an example of how to integrate Mocha into a test library.
|
11
11
|
module Adapter
|
12
12
|
include Mocha::API
|
13
13
|
|
14
14
|
# @private
|
15
|
-
def self.applicable_to?(
|
16
|
-
Gem::Requirement.new('>= 3.3.0').satisfied_by?(
|
15
|
+
def self.applicable_to?(minitest_version)
|
16
|
+
Gem::Requirement.new('>= 3.3.0').satisfied_by?(minitest_version)
|
17
17
|
end
|
18
18
|
|
19
19
|
# @private
|
20
20
|
def self.description
|
21
|
-
'adapter for
|
21
|
+
'adapter for Minitest gem >= v3.3.0'
|
22
22
|
end
|
23
23
|
|
24
24
|
# @private
|
25
25
|
def self.included(_mod)
|
26
|
-
Mocha::ExpectationErrorFactory.exception_class = ::
|
26
|
+
Mocha::ExpectationErrorFactory.exception_class = ::Minitest::Assertion
|
27
27
|
end
|
28
28
|
|
29
29
|
# @private
|
@@ -46,6 +46,21 @@ module Mocha
|
|
46
46
|
super
|
47
47
|
mocha_teardown
|
48
48
|
end
|
49
|
+
|
50
|
+
# @private
|
51
|
+
def mocha_test_name
|
52
|
+
if respond_to?(:name)
|
53
|
+
test_name = name
|
54
|
+
elsif respond_to?(:__name__) # Older minitest
|
55
|
+
test_name = __name__
|
56
|
+
end
|
57
|
+
|
58
|
+
if test_name
|
59
|
+
"#{self.class.name}##{test_name}"
|
60
|
+
else
|
61
|
+
self.class.name
|
62
|
+
end
|
63
|
+
end
|
49
64
|
end
|
50
65
|
end
|
51
66
|
end
|
@@ -2,10 +2,10 @@ require 'mocha/expectation_error'
|
|
2
2
|
|
3
3
|
module Mocha
|
4
4
|
module Integration
|
5
|
-
module
|
5
|
+
module Minitest
|
6
6
|
def self.translate(exception)
|
7
7
|
return exception unless exception.is_a?(::Mocha::ExpectationError)
|
8
|
-
translated_exception = ::
|
8
|
+
translated_exception = ::Minitest::Assertion.new(exception.message)
|
9
9
|
translated_exception.set_backtrace(exception.backtrace)
|
10
10
|
translated_exception
|
11
11
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'mocha/debug'
|
2
|
+
require 'mocha/detection/minitest'
|
3
|
+
require 'mocha/integration/minitest/adapter'
|
4
|
+
|
5
|
+
module Mocha
|
6
|
+
module Integration
|
7
|
+
module Minitest
|
8
|
+
def self.activate
|
9
|
+
target = Detection::Minitest.testcase
|
10
|
+
return false unless target
|
11
|
+
|
12
|
+
minitest_version = Gem::Version.new(Detection::Minitest.version)
|
13
|
+
Debug.puts "Detected Minitest version: #{minitest_version}"
|
14
|
+
|
15
|
+
unless Minitest::Adapter.applicable_to?(minitest_version)
|
16
|
+
raise 'Versions of minitest earlier than v3.3.0 are not supported.'
|
17
|
+
end
|
18
|
+
|
19
|
+
unless target < Minitest::Adapter
|
20
|
+
Debug.puts "Applying #{Minitest::Adapter.description}"
|
21
|
+
target.send(:include, Minitest::Adapter)
|
22
|
+
end
|
23
|
+
|
24
|
+
true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -12,7 +12,7 @@ module Mocha
|
|
12
12
|
include Mocha::API
|
13
13
|
|
14
14
|
# @private
|
15
|
-
def self.applicable_to?(test_unit_version
|
15
|
+
def self.applicable_to?(test_unit_version)
|
16
16
|
Gem::Requirement.new('>= 2.5.1').satisfied_by?(test_unit_version)
|
17
17
|
end
|
18
18
|
|
@@ -23,20 +23,25 @@ module Mocha
|
|
23
23
|
|
24
24
|
# @private
|
25
25
|
def self.included(mod)
|
26
|
-
mod.setup :mocha_setup, :
|
26
|
+
mod.setup :mocha_setup, before: :prepend
|
27
27
|
|
28
28
|
mod.exception_handler(:handle_mocha_expectation_error)
|
29
29
|
|
30
|
-
mod.cleanup :
|
30
|
+
mod.cleanup after: :append do
|
31
31
|
assertion_counter = Integration::AssertionCounter.new(self)
|
32
32
|
mocha_verify(assertion_counter)
|
33
33
|
end
|
34
34
|
|
35
|
-
mod.teardown :mocha_teardown, :
|
35
|
+
mod.teardown :mocha_teardown, after: :append
|
36
36
|
end
|
37
37
|
|
38
38
|
private
|
39
39
|
|
40
|
+
# @private
|
41
|
+
def mocha_test_name
|
42
|
+
name
|
43
|
+
end
|
44
|
+
|
40
45
|
# @private
|
41
46
|
def handle_mocha_expectation_error(exception)
|
42
47
|
return false unless exception.is_a?(Mocha::ExpectationError)
|
@@ -1,47 +1,26 @@
|
|
1
1
|
require 'mocha/debug'
|
2
|
-
|
3
2
|
require 'mocha/detection/test_unit'
|
4
|
-
|
5
|
-
require 'mocha/integration/test_unit/nothing'
|
6
|
-
require 'mocha/integration/test_unit/ruby_version_186_and_above'
|
7
|
-
require 'mocha/integration/test_unit/gem_version_200'
|
8
|
-
require 'mocha/integration/test_unit/gem_version_201_to_202'
|
9
|
-
require 'mocha/integration/test_unit/gem_version_203_to_220'
|
10
|
-
require 'mocha/integration/test_unit/gem_version_230_to_250'
|
11
3
|
require 'mocha/integration/test_unit/adapter'
|
12
4
|
|
13
|
-
require 'mocha/deprecation'
|
14
|
-
|
15
5
|
module Mocha
|
16
6
|
module Integration
|
17
7
|
module TestUnit
|
18
8
|
def self.activate
|
19
|
-
|
20
|
-
|
21
|
-
ruby_version = Gem::Version.new(RUBY_VERSION.dup)
|
9
|
+
target = Detection::TestUnit.testcase
|
10
|
+
return false unless target
|
22
11
|
|
23
|
-
|
12
|
+
test_unit_version = Gem::Version.new(Detection::TestUnit.version)
|
24
13
|
Debug.puts "Detected Test::Unit version: #{test_unit_version}"
|
25
14
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
TestUnit::GemVersion203To220,
|
30
|
-
TestUnit::GemVersion201To202,
|
31
|
-
TestUnit::GemVersion200,
|
32
|
-
TestUnit::RubyVersion186AndAbove,
|
33
|
-
TestUnit::Nothing
|
34
|
-
].detect { |m| m.applicable_to?(test_unit_version, ruby_version) }
|
15
|
+
unless TestUnit::Adapter.applicable_to?(test_unit_version)
|
16
|
+
raise 'Versions of test-unit earlier than v2.5.1 are not supported.'
|
17
|
+
end
|
35
18
|
|
36
|
-
unless
|
37
|
-
|
38
|
-
|
39
|
-
'Versions of test-unit earlier than v2.5.1 will not be supported in future versions of Mocha.'
|
40
|
-
)
|
41
|
-
end
|
42
|
-
Debug.puts "Applying #{integration_module.description}"
|
43
|
-
::Test::Unit::TestCase.send(:include, integration_module)
|
19
|
+
unless target < TestUnit::Adapter
|
20
|
+
Debug.puts "Applying #{TestUnit::Adapter.description}"
|
21
|
+
target.send(:include, TestUnit::Adapter)
|
44
22
|
end
|
23
|
+
|
45
24
|
true
|
46
25
|
end
|
47
26
|
end
|
data/lib/mocha/invocation.rb
CHANGED
@@ -3,8 +3,6 @@ require 'mocha/raised_exception'
|
|
3
3
|
require 'mocha/return_values'
|
4
4
|
require 'mocha/thrown_object'
|
5
5
|
require 'mocha/yield_parameters'
|
6
|
-
require 'mocha/configuration'
|
7
|
-
require 'mocha/deprecation'
|
8
6
|
|
9
7
|
module Mocha
|
10
8
|
class Invocation
|
@@ -22,18 +20,8 @@ module Mocha
|
|
22
20
|
def call(yield_parameters = YieldParameters.new, return_values = ReturnValues.new)
|
23
21
|
yield_parameters.next_invocation.each do |yield_args|
|
24
22
|
@yields << ParametersMatcher.new(yield_args)
|
25
|
-
|
26
|
-
|
27
|
-
else
|
28
|
-
raise LocalJumpError unless Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
|
29
|
-
yield_args_description = ParametersMatcher.new(yield_args).mocha_inspect
|
30
|
-
Deprecation.warning(
|
31
|
-
"Stubbed method was instructed to yield #{yield_args_description}, but no block was given by invocation: #{call_description}.",
|
32
|
-
' This will raise a LocalJumpError in the future.',
|
33
|
-
' Use Expectation#with_block_given to constrain this expectation to match invocations supplying a block.',
|
34
|
-
' And, if necessary, add another expectation to match invocations not supplying a block.'
|
35
|
-
)
|
36
|
-
end
|
23
|
+
raise LocalJumpError unless @block
|
24
|
+
@block.call(*yield_args)
|
37
25
|
end
|
38
26
|
return_values.next(self)
|
39
27
|
end
|
@@ -79,7 +67,6 @@ module Mocha
|
|
79
67
|
def argument_description
|
80
68
|
signature = arguments.mocha_inspect
|
81
69
|
signature = signature.gsub(/^\[|\]$/, '')
|
82
|
-
signature = signature.gsub(/^\{|\}$/, '') if arguments.length == 1
|
83
70
|
"(#{signature})"
|
84
71
|
end
|
85
72
|
end
|
data/lib/mocha/minitest.rb
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
require 'mocha/ruby_version'
|
2
|
-
require 'mocha/integration/
|
3
|
-
require 'mocha/deprecation'
|
2
|
+
require 'mocha/integration/minitest'
|
4
3
|
|
5
|
-
unless Mocha::Integration::
|
6
|
-
|
7
|
-
"MiniTest must be loaded *before* `require 'mocha/minitest'`."
|
8
|
-
)
|
4
|
+
unless Mocha::Integration::Minitest.activate
|
5
|
+
raise "Minitest must be loaded *before* `require 'mocha/minitest'`."
|
9
6
|
end
|