mocha 2.7.1 → 3.0.0.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +67 -3
  3. data/.rubocop_todo.yml +25 -13
  4. data/Gemfile +11 -27
  5. data/README.md +3 -4
  6. data/RELEASE.md +37 -0
  7. data/Rakefile +6 -5
  8. data/gemfiles/Gemfile.minitest.latest +3 -3
  9. data/gemfiles/Gemfile.rubocop +9 -0
  10. data/gemfiles/Gemfile.test-unit.latest +3 -3
  11. data/lib/mocha/any_instance_method.rb +2 -0
  12. data/lib/mocha/any_instance_receiver.rb +20 -0
  13. data/lib/mocha/api.rb +4 -2
  14. data/lib/mocha/argument_iterator.rb +5 -5
  15. data/lib/mocha/backtrace_filter.rb +2 -0
  16. data/lib/mocha/{block_matcher.rb → block_matchers.rb} +2 -0
  17. data/lib/mocha/cardinality.rb +2 -2
  18. data/lib/mocha/central.rb +4 -0
  19. data/lib/mocha/change_state_side_effect.rb +2 -0
  20. data/lib/mocha/class_methods.rb +5 -4
  21. data/lib/mocha/configuration.rb +18 -41
  22. data/lib/mocha/default_name.rb +15 -0
  23. data/lib/mocha/default_receiver.rb +13 -0
  24. data/lib/mocha/deprecation.rb +15 -9
  25. data/lib/mocha/detection/minitest.rb +2 -0
  26. data/lib/mocha/detection/test_unit.rb +4 -1
  27. data/lib/mocha/error_with_filtered_backtrace.rb +2 -0
  28. data/lib/mocha/exception_raiser.rb +3 -0
  29. data/lib/mocha/expectation.rb +57 -23
  30. data/lib/mocha/expectation_error.rb +2 -0
  31. data/lib/mocha/expectation_error_factory.rb +2 -0
  32. data/lib/mocha/expectation_list.rb +2 -0
  33. data/lib/mocha/hooks.rb +2 -0
  34. data/lib/mocha/impersonating_any_instance_name.rb +13 -0
  35. data/lib/mocha/impersonating_name.rb +13 -0
  36. data/lib/mocha/in_state_ordering_constraint.rb +2 -0
  37. data/lib/mocha/inspect.rb +13 -10
  38. data/lib/mocha/instance_method.rb +2 -0
  39. data/lib/mocha/integration/assertion_counter.rb +2 -0
  40. data/lib/mocha/integration/minitest/adapter.rb +3 -0
  41. data/lib/mocha/integration/minitest.rb +4 -3
  42. data/lib/mocha/integration/monkey_patcher.rb +4 -2
  43. data/lib/mocha/integration/test_unit/adapter.rb +3 -0
  44. data/lib/mocha/integration/test_unit.rb +4 -3
  45. data/lib/mocha/invocation.rb +9 -6
  46. data/lib/mocha/logger.rb +2 -0
  47. data/lib/mocha/macos_version.rb +2 -0
  48. data/lib/mocha/method_matcher.rb +2 -0
  49. data/lib/mocha/minitest.rb +2 -1
  50. data/lib/mocha/mock.rb +8 -8
  51. data/lib/mocha/mockery.rb +16 -6
  52. data/lib/mocha/name.rb +13 -0
  53. data/lib/mocha/not_initialized_error.rb +2 -0
  54. data/lib/mocha/object_methods.rb +7 -5
  55. data/lib/mocha/object_receiver.rb +20 -0
  56. data/lib/mocha/parameter_matchers/all_of.rb +5 -1
  57. data/lib/mocha/parameter_matchers/any_of.rb +5 -1
  58. data/lib/mocha/parameter_matchers/any_parameters.rb +5 -1
  59. data/lib/mocha/parameter_matchers/anything.rb +5 -1
  60. data/lib/mocha/parameter_matchers/base.rb +4 -2
  61. data/lib/mocha/parameter_matchers/equals.rb +5 -1
  62. data/lib/mocha/parameter_matchers/equivalent_uri.rb +5 -1
  63. data/lib/mocha/parameter_matchers/has_entries.rb +5 -1
  64. data/lib/mocha/parameter_matchers/has_entry.rb +6 -1
  65. data/lib/mocha/parameter_matchers/has_key.rb +6 -1
  66. data/lib/mocha/parameter_matchers/has_keys.rb +6 -2
  67. data/lib/mocha/parameter_matchers/has_value.rb +6 -1
  68. data/lib/mocha/parameter_matchers/includes.rb +8 -7
  69. data/lib/mocha/parameter_matchers/instance_methods.rb +4 -2
  70. data/lib/mocha/parameter_matchers/instance_of.rb +5 -1
  71. data/lib/mocha/parameter_matchers/is_a.rb +5 -1
  72. data/lib/mocha/parameter_matchers/kind_of.rb +5 -1
  73. data/lib/mocha/parameter_matchers/not.rb +5 -1
  74. data/lib/mocha/parameter_matchers/optionally.rb +6 -1
  75. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +41 -16
  76. data/lib/mocha/parameter_matchers/regexp_matches.rb +6 -1
  77. data/lib/mocha/parameter_matchers/responds_with.rb +5 -1
  78. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +5 -1
  79. data/lib/mocha/parameter_matchers.rb +2 -0
  80. data/lib/mocha/parameters_matcher.rb +9 -1
  81. data/lib/mocha/raised_exception.rb +2 -0
  82. data/lib/mocha/return_values.rb +2 -0
  83. data/lib/mocha/ruby_version.rb +3 -0
  84. data/lib/mocha/sequence.rb +2 -0
  85. data/lib/mocha/single_return_value.rb +1 -1
  86. data/lib/mocha/state_machine.rb +2 -0
  87. data/lib/mocha/stubbed_method.rb +9 -2
  88. data/lib/mocha/stubbing_error.rb +2 -0
  89. data/lib/mocha/test_unit.rb +2 -1
  90. data/lib/mocha/thrower.rb +2 -0
  91. data/lib/mocha/thrown_object.rb +2 -0
  92. data/lib/mocha/version.rb +3 -1
  93. data/lib/mocha/yield_parameters.rb +2 -0
  94. data/lib/mocha.rb +5 -0
  95. data/mocha.gemspec +10 -4
  96. metadata +17 -15
  97. data/lib/mocha/debug.rb +0 -9
  98. data/lib/mocha/integration/minitest/exception_translation.rb +0 -14
  99. data/lib/mocha/is_a.rb +0 -7
  100. data/lib/mocha/names.rb +0 -43
  101. data/lib/mocha/receivers.rb +0 -45
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class DefaultReceiver
5
+ def initialize(mock)
6
+ @mock = mock
7
+ end
8
+
9
+ def mocks
10
+ [@mock]
11
+ end
12
+ end
13
+ end
@@ -1,21 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/backtrace_filter'
2
4
 
3
5
  module Mocha
4
6
  class Deprecation
5
- class << self
6
- attr_accessor :mode, :messages
7
-
8
- def warning(*messages)
9
- message = messages.join
10
- @messages << message
11
- return if mode == :disabled
7
+ class Logger
8
+ def warning(message)
12
9
  filter = BacktraceFilter.new
13
10
  location = filter.filtered(caller)[0]
14
11
  warn "Mocha deprecation warning at #{location}: #{message}"
15
12
  end
16
13
  end
17
14
 
18
- self.mode = :enabled
19
- self.messages = []
15
+ class << self
16
+ attr_writer :logger
17
+
18
+ def warning(message)
19
+ logger.call(message)
20
+ end
21
+
22
+ def logger
23
+ @logger || Logger.new
24
+ end
25
+ end
20
26
  end
21
27
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  module Detection
3
5
  module Minitest
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  module Detection
3
5
  module TestUnit
@@ -14,7 +16,8 @@ module Mocha
14
16
  if testcase
15
17
  begin
16
18
  require 'test/unit/version'
17
- rescue LoadError # rubocop:disable Lint/HandleExceptions
19
+ rescue LoadError
20
+ warn "Unable to load 'test/unit/version', but continuing anyway" if $DEBUG
18
21
  end
19
22
  if defined?(::Test::Unit::VERSION)
20
23
  version = ::Test::Unit::VERSION
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/backtrace_filter'
2
4
 
3
5
  module Mocha
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class ExceptionRaiser
3
5
  def initialize(exception, message)
@@ -9,6 +11,7 @@ module Mocha
9
11
  invocation.raised(@exception)
10
12
  raise @exception, @exception.to_s if @exception.is_a?(Module) && (@exception < Interrupt)
11
13
  raise @exception, @message if @message
14
+
12
15
  raise @exception
13
16
  end
14
17
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ruby2_keywords'
2
4
  require 'mocha/method_matcher'
3
5
  require 'mocha/parameters_matcher'
@@ -6,23 +8,22 @@ require 'mocha/return_values'
6
8
  require 'mocha/exception_raiser'
7
9
  require 'mocha/thrower'
8
10
  require 'mocha/yield_parameters'
9
- require 'mocha/is_a'
10
11
  require 'mocha/in_state_ordering_constraint'
11
12
  require 'mocha/change_state_side_effect'
12
13
  require 'mocha/cardinality'
13
14
  require 'mocha/configuration'
14
- require 'mocha/block_matcher'
15
+ require 'mocha/block_matchers'
15
16
  require 'mocha/backtrace_filter'
16
17
 
17
18
  module Mocha
18
19
  # Methods on expectations returned from {Mock#expects}, {Mock#stubs}, {ObjectMethods#expects} and {ObjectMethods#stubs}.
19
20
  class Expectation
20
- # Modifies expectation so that the number of calls to the expected method must be within a specific +range+.
21
+ # Modifies expectation so that the number of invocations of the expected method must be within a specified range or exactly equal to a specified number.
21
22
  #
22
- # @param [Range,Integer] range specifies the allowable range in the number of expected invocations.
23
+ # @param [Range,Integer] range_or_number specifies the allowable range for the number of expected invocations or the specified number of expected invocations.
23
24
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
24
25
  #
25
- # @example Specifying a specific number of expected invocations.
26
+ # @example Specifying an exact number of expected invocations.
26
27
  # object = mock()
27
28
  # object.expects(:expected_method).times(3)
28
29
  # 3.times { object.expected_method }
@@ -33,7 +34,7 @@ module Mocha
33
34
  # 2.times { object.expected_method }
34
35
  # # => verify fails
35
36
  #
36
- # @example Specifying a range in the number of expected invocations.
37
+ # @example Specifying a range for the number of expected invocations.
37
38
  # object = mock()
38
39
  # object.expects(:expected_method).times(2..4)
39
40
  # 3.times { object.expected_method }
@@ -43,12 +44,41 @@ module Mocha
43
44
  # object.expects(:expected_method).times(2..4)
44
45
  # object.expected_method
45
46
  # # => verify fails
46
- def times(range)
47
- @cardinality.times(range)
47
+ def times(range_or_number)
48
+ @cardinality.times(range_or_number)
49
+ self
50
+ end
51
+
52
+ # Modifies expectation so that the expected method must be called exactly three times. This is equivalent to calling {#times} with an argument of +3+.
53
+ #
54
+ # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
55
+ #
56
+ # @example Expected method must be invoked exactly three times.
57
+ # object = mock()
58
+ # object.expects(:expected_method).thrice
59
+ # object.expected_method
60
+ # object.expected_method
61
+ # object.expected_method
62
+ # # => verify succeeds
63
+ #
64
+ # object = mock()
65
+ # object.expects(:expected_method).thrice
66
+ # object.expected_method
67
+ # object.expected_method
68
+ # object.expected_method
69
+ # object.expected_method # => unexpected invocation
70
+ #
71
+ # object = mock()
72
+ # object.expects(:expected_method).thrice
73
+ # object.expected_method
74
+ # object.expected_method
75
+ # # => verify fails
76
+ def thrice
77
+ @cardinality.exactly(3)
48
78
  self
49
79
  end
50
80
 
51
- # Modifies expectation so that the expected method must be called exactly twice.
81
+ # Modifies expectation so that the expected method must be called exactly twice. This is equivalent to calling {#times} with an argument of +2+.
52
82
  #
53
83
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
54
84
  #
@@ -74,7 +104,7 @@ module Mocha
74
104
  self
75
105
  end
76
106
 
77
- # Modifies expectation so that the expected method must be called exactly once.
107
+ # Modifies expectation so that the expected method must be called exactly once. This is equivalent to calling {#times} with an argument of +1+.
78
108
  #
79
109
  # Note that this is the default behaviour for an expectation, but you may wish to use it for clarity/emphasis.
80
110
  #
@@ -136,7 +166,7 @@ module Mocha
136
166
  self
137
167
  end
138
168
 
139
- # Modifies expectation so that the expected method must be called at least once.
169
+ # Modifies expectation so that the expected method must be called at least once. This is equivalent to calling {#at_least} with an argument of +1+.
140
170
  #
141
171
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
142
172
  #
@@ -172,7 +202,7 @@ module Mocha
172
202
  self
173
203
  end
174
204
 
175
- # Modifies expectation so that the expected method must be called at most once.
205
+ # Modifies expectation so that the expected method must be called at most once. This is equivalent to calling {#at_most} with an argument of +1+.
176
206
  #
177
207
  # @return [Expectation] the same expectation, thereby allowing invocations of other {Expectation} methods to be chained.
178
208
  #
@@ -200,9 +230,9 @@ module Mocha
200
230
  #
201
231
  # 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
232
  #
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.
233
+ # When {Configuration#strict_keyword_argument_matching=} is set to +false+ (which is the default in Ruby v2.7), 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.
204
234
  #
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.
235
+ # When {Configuration#strict_keyword_argument_matching=} is set to +true+ (which is the default in Ruby >= v3.0), 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
236
  #
207
237
  # @see ParameterMatchers
208
238
  # @see Configuration#strict_keyword_argument_matching=
@@ -235,7 +265,11 @@ module Mocha
235
265
  # object.expected_method(['string1'], 'any-old-value')
236
266
  # # => verify fails
237
267
  #
238
- # @example Loose keyword argument matching (default)
268
+ # @example Loose keyword argument matching (default in Ruby v2.7).
269
+ #
270
+ # Mocha.configure do |c|
271
+ # c.strict_keyword_argument_matching = false
272
+ # end
239
273
  #
240
274
  # class Example
241
275
  # def foo(a, bar:); end
@@ -246,7 +280,7 @@ module Mocha
246
280
  # example.foo('a', { bar: 'b' })
247
281
  # # This passes the test, but would result in an ArgumentError in practice
248
282
  #
249
- # @example Strict keyword argument matching
283
+ # @example Strict keyword argument matching (default in Ruby >= 3.0).
250
284
  #
251
285
  # Mocha.configure do |c|
252
286
  # c.strict_keyword_argument_matching = true
@@ -711,19 +745,19 @@ module Mocha
711
745
 
712
746
  # @private
713
747
  def mocha_inspect
714
- message = "#{@cardinality.anticipated_times}, #{@cardinality.invoked_times}: #{method_signature}"
715
- message << "; #{@ordering_constraints.map(&:mocha_inspect).join('; ')}" unless @ordering_constraints.empty?
748
+ strings = ["#{@cardinality.anticipated_times}, #{@cardinality.invoked_times}: #{method_signature}"]
749
+ strings << "; #{@ordering_constraints.map(&:mocha_inspect).join('; ')}" unless @ordering_constraints.empty?
716
750
  if Mocha.configuration.display_matching_invocations_on_failure?
717
- message << @cardinality.actual_invocations
751
+ strings << @cardinality.actual_invocations
718
752
  end
719
- message
753
+ strings.join
720
754
  end
721
755
 
722
756
  # @private
723
757
  def method_signature
724
- signature = "#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"
725
- signature << " #{@block_matcher.mocha_inspect}" if @block_matcher.mocha_inspect
726
- signature
758
+ strings = ["#{@mock.mocha_inspect}.#{@method_matcher.mocha_inspect}#{@parameters_matcher.mocha_inspect}"]
759
+ strings << " #{@block_matcher.mocha_inspect}" if @block_matcher.mocha_inspect
760
+ strings.join
727
761
  end
728
762
 
729
763
  # @private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  # Default exception class raised when an unexpected invocation or an unsatisfied expectation occurs.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/backtrace_filter'
2
4
  require 'mocha/expectation_error'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class ExpectationList
3
5
  def initialize(expectations = [])
data/lib/mocha/hooks.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/mockery'
2
4
 
3
5
  module Mocha
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class ImpersonatingAnyInstanceName
5
+ def initialize(klass)
6
+ @klass = klass
7
+ end
8
+
9
+ def mocha_inspect
10
+ "#<AnyInstance:#{@klass.mocha_inspect}>"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class ImpersonatingName
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ def mocha_inspect
10
+ @object.mocha_inspect
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class InStateOrderingConstraint
3
5
  def initialize(state_predicate)
data/lib/mocha/inspect.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'date'
2
4
 
3
5
  module Mocha
@@ -11,7 +13,7 @@ module Mocha
11
13
  end
12
14
 
13
15
  module ArrayMethods
14
- def mocha_inspect(wrapped = true)
16
+ def mocha_inspect(wrapped: true)
15
17
  unwrapped = collect(&:mocha_inspect).join(', ')
16
18
  wrapped ? "[#{unwrapped}]" : unwrapped
17
19
  end
@@ -19,17 +21,18 @@ module Mocha
19
21
 
20
22
  module HashMethods
21
23
  def mocha_inspect
24
+ unwrapped = collect do |key, value|
25
+ case key
26
+ when Symbol
27
+ "#{key}: #{value.mocha_inspect}"
28
+ else
29
+ "#{key.mocha_inspect} => #{value.mocha_inspect}"
30
+ end
31
+ end.join(', ')
32
+
22
33
  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(', ')
34
+ empty? ? '**{}' : unwrapped
31
35
  else
32
- unwrapped = collect { |key, value| "#{key.mocha_inspect} => #{value.mocha_inspect}" }.join(', ')
33
36
  "{#{unwrapped}}"
34
37
  end
35
38
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/stubbed_method'
2
4
 
3
5
  module Mocha
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  module Integration
3
5
  class AssertionCounter
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/api'
2
4
  require 'mocha/integration/assertion_counter'
3
5
  require 'mocha/expectation_error_factory'
@@ -35,6 +37,7 @@ module Mocha
35
37
  # @private
36
38
  def before_teardown
37
39
  return unless passed?
40
+
38
41
  assertion_counter = Integration::AssertionCounter.new(self)
39
42
  mocha_verify(assertion_counter)
40
43
  ensure
@@ -1,4 +1,5 @@
1
- require 'mocha/debug'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'mocha/detection/minitest'
3
4
  require 'mocha/integration/minitest/adapter'
4
5
 
@@ -10,14 +11,14 @@ module Mocha
10
11
  return false unless target
11
12
 
12
13
  minitest_version = Gem::Version.new(Detection::Minitest.version)
13
- Debug.puts "Detected Minitest version: #{minitest_version}"
14
+ warn "Detected Minitest version: #{minitest_version}" if $DEBUG
14
15
 
15
16
  unless Minitest::Adapter.applicable_to?(minitest_version)
16
17
  raise 'Versions of minitest earlier than v3.3.0 are not supported.'
17
18
  end
18
19
 
19
20
  unless target < Minitest::Adapter
20
- Debug.puts "Applying #{Minitest::Adapter.description}"
21
+ warn "Applying #{Minitest::Adapter.description}" if $DEBUG
21
22
  target.send(:include, Minitest::Adapter)
22
23
  end
23
24
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/api'
2
4
 
3
5
  module Mocha
@@ -5,12 +7,12 @@ module Mocha
5
7
  module MonkeyPatcher
6
8
  def self.apply(mod, run_method_patch)
7
9
  if mod < Mocha::API
8
- Debug.puts "Mocha::API already included in #{mod}"
10
+ warn "Mocha::API already included in #{mod}" if $DEBUG
9
11
  else
10
12
  mod.send(:include, Mocha::API)
11
13
  end
12
14
  if mod.method_defined?(:run_before_mocha)
13
- Debug.puts "#{mod}#run_before_mocha method already defined"
15
+ warn "#{mod}#run_before_mocha method already defined" if $DEBUG
14
16
  elsif mod.method_defined?(:run)
15
17
  mod.send(:alias_method, :run_before_mocha, :run)
16
18
  mod.send(:remove_method, :run)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/api'
2
4
  require 'mocha/integration/assertion_counter'
3
5
  require 'mocha/expectation_error'
@@ -45,6 +47,7 @@ module Mocha
45
47
  # @private
46
48
  def handle_mocha_expectation_error(exception)
47
49
  return false unless exception.is_a?(Mocha::ExpectationError)
50
+
48
51
  problem_occurred
49
52
  add_failure(exception.message, exception.backtrace)
50
53
  true
@@ -1,4 +1,5 @@
1
- require 'mocha/debug'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'mocha/detection/test_unit'
3
4
  require 'mocha/integration/test_unit/adapter'
4
5
 
@@ -10,14 +11,14 @@ module Mocha
10
11
  return false unless target
11
12
 
12
13
  test_unit_version = Gem::Version.new(Detection::TestUnit.version)
13
- Debug.puts "Detected Test::Unit version: #{test_unit_version}"
14
+ warn "Detected Test::Unit version: #{test_unit_version}" if $DEBUG
14
15
 
15
16
  unless TestUnit::Adapter.applicable_to?(test_unit_version)
16
17
  raise 'Versions of test-unit earlier than v2.5.1 are not supported.'
17
18
  end
18
19
 
19
20
  unless target < TestUnit::Adapter
20
- Debug.puts "Applying #{TestUnit::Adapter.description}"
21
+ warn "Applying #{TestUnit::Adapter.description}" if $DEBUG
21
22
  target.send(:include, TestUnit::Adapter)
22
23
  end
23
24
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/parameters_matcher'
2
4
  require 'mocha/raised_exception'
3
5
  require 'mocha/return_values'
@@ -21,6 +23,7 @@ module Mocha
21
23
  yield_parameters.next_invocation.each do |yield_args|
22
24
  @yields << ParametersMatcher.new(yield_args)
23
25
  raise LocalJumpError unless @block
26
+
24
27
  @block.call(*yield_args)
25
28
  end
26
29
  return_values.next(self)
@@ -43,9 +46,9 @@ module Mocha
43
46
  end
44
47
 
45
48
  def call_description
46
- description = "#{@mock.mocha_inspect}.#{@method_name}#{argument_description}"
47
- description << ' { ... }' unless @block.nil?
48
- description
49
+ strings = ["#{@mock.mocha_inspect}.#{@method_name}#{argument_description}"]
50
+ strings << ' { ... }' unless @block.nil?
51
+ strings.join
49
52
  end
50
53
 
51
54
  def short_call_description
@@ -53,9 +56,9 @@ module Mocha
53
56
  end
54
57
 
55
58
  def result_description
56
- desc = "# => #{@result.mocha_inspect}"
57
- desc << " after yielding #{@yields.map(&:mocha_inspect).join(', then ')}" if @yields.any?
58
- desc
59
+ strings = ["# => #{@result.mocha_inspect}"]
60
+ strings << " after yielding #{@yields.map(&:mocha_inspect).join(', then ')}" if @yields.any?
61
+ strings.join
59
62
  end
60
63
 
61
64
  def full_description
data/lib/mocha/logger.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class Logger
3
5
  def initialize(io)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  MACOS = /darwin/.match(RUBY_PLATFORM)
3
5
  MACOS_VERSION = MACOS && /darwin(\d+)/.match(RUBY_PLATFORM)[1].to_i
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class MethodMatcher
3
5
  attr_reader :expected_method_name
@@ -1,4 +1,5 @@
1
- require 'mocha/ruby_version'
1
+ # frozen_string_literal: true
2
+
2
3
  require 'mocha/integration/minitest'
3
4
 
4
5
  unless Mocha::Integration::Minitest.activate
data/lib/mocha/mock.rb CHANGED
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ruby2_keywords'
2
4
  require 'mocha/expectation'
3
5
  require 'mocha/expectation_list'
4
6
  require 'mocha/invocation'
5
- require 'mocha/names'
6
- require 'mocha/receivers'
7
+ require 'mocha/default_name'
8
+ require 'mocha/default_receiver'
7
9
  require 'mocha/method_matcher'
8
10
  require 'mocha/parameters_matcher'
9
11
  require 'mocha/argument_iterator'
@@ -114,8 +116,7 @@ module Mocha
114
116
  def expects(method_name_or_hash, backtrace = nil)
115
117
  expectation = nil
116
118
  iterator = ArgumentIterator.new(method_name_or_hash)
117
- iterator.each do |*args|
118
- method_name = args.shift
119
+ iterator.each do |method_name, *args|
119
120
  ensure_method_not_already_defined(method_name)
120
121
  expectation = Expectation.new(self, method_name, backtrace)
121
122
  expectation.in_sequence(@mockery.sequences.last) if @mockery.sequences.any?
@@ -153,8 +154,7 @@ module Mocha
153
154
  def stubs(method_name_or_hash, backtrace = nil)
154
155
  expectation = nil
155
156
  iterator = ArgumentIterator.new(method_name_or_hash)
156
- iterator.each do |*args|
157
- method_name = args.shift
157
+ iterator.each do |method_name, *args|
158
158
  ensure_method_not_already_defined(method_name)
159
159
  expectation = Expectation.new(self, method_name, backtrace)
160
160
  expectation.at_least(0)
@@ -315,13 +315,13 @@ module Mocha
315
315
  end
316
316
 
317
317
  # @private
318
- def method_missing(symbol, *arguments, &block) # rubocop:disable Style/MethodMissingSuper
318
+ def method_missing(symbol, *arguments, &block)
319
319
  handle_method_call(symbol, arguments, block)
320
320
  end
321
321
  ruby2_keywords(:method_missing)
322
322
 
323
323
  # @private
324
- def handle_method_call(symbol, arguments, block) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
324
+ def handle_method_call(symbol, arguments, block)
325
325
  check_expiry
326
326
  check_responder_responds_to(symbol)
327
327
  invocation = Invocation.new(self, symbol, arguments, block)
data/lib/mocha/mockery.rb CHANGED
@@ -1,7 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/central'
2
4
  require 'mocha/mock'
3
- require 'mocha/names'
4
- require 'mocha/receivers'
5
+ require 'mocha/name'
6
+ require 'mocha/impersonating_name'
7
+ require 'mocha/impersonating_any_instance_name'
8
+ require 'mocha/object_receiver'
9
+ require 'mocha/any_instance_receiver'
5
10
  require 'mocha/state_machine'
6
11
  require 'mocha/logger'
7
12
  require 'mocha/configuration'
@@ -49,9 +54,13 @@ module Mocha
49
54
  end
50
55
 
51
56
  def teardown(origin = nil)
57
+ if @instances.nil?
58
+ raise NotInitializedError, 'Mocha::Mockery.teardown called before Mocha::Mockery.setup'
59
+ end
60
+
52
61
  instance.teardown(origin)
53
62
  ensure
54
- @instances.pop
63
+ @instances.pop unless @instances.nil?
55
64
  end
56
65
  end
57
66
 
@@ -124,12 +133,11 @@ module Mocha
124
133
  def on_stubbing(object, method)
125
134
  signature_proc = lambda { "#{object.mocha_inspect}.#{method}" }
126
135
  check(:stubbing_non_existent_method, 'non-existent method', signature_proc) do
127
- !(object.stubba_class.__method_exists__?(method, true) || object.respond_to?(method))
136
+ !(object.stubba_class.__method_exists__?(method) || object.respond_to?(method))
128
137
  end
129
138
  check(:stubbing_non_public_method, 'non-public method', signature_proc) do
130
- object.stubba_class.__method_exists__?(method, false)
139
+ object.stubba_class.__method_exists__?(method, include_public_methods: false)
131
140
  end
132
- check(:stubbing_method_on_nil, 'method on nil', signature_proc) { object.nil? }
133
141
  check(:stubbing_method_on_non_mock_object, 'method on non-mock object', signature_proc)
134
142
  end
135
143
 
@@ -144,9 +152,11 @@ module Mocha
144
152
  def check(action, description, signature_proc, backtrace = caller)
145
153
  treatment = Mocha.configuration.send(action)
146
154
  return if (treatment == :allow) || (block_given? && !yield)
155
+
147
156
  method_signature = signature_proc.call
148
157
  message = "stubbing #{description}: #{method_signature}"
149
158
  raise StubbingError.new(message, backtrace) if treatment == :prevent
159
+
150
160
  logger.warn(message) if treatment == :warn
151
161
  end
152
162