mocha 2.8.0 → 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.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +37 -3
  3. data/.rubocop_todo.yml +25 -13
  4. data/.yardopts +2 -0
  5. data/Gemfile +9 -28
  6. data/README.md +3 -4
  7. data/RELEASE.md +64 -0
  8. data/Rakefile +43 -22
  9. data/gemfiles/Gemfile.minitest.latest +3 -3
  10. data/gemfiles/Gemfile.rubocop +9 -0
  11. data/gemfiles/Gemfile.test-unit.latest +3 -3
  12. data/lib/mocha/any_instance_method.rb +6 -4
  13. data/lib/mocha/any_instance_receiver.rb +20 -0
  14. data/lib/mocha/api.rb +4 -2
  15. data/lib/mocha/argument_iterator.rb +5 -5
  16. data/lib/mocha/backtrace_filter.rb +2 -0
  17. data/lib/mocha/{block_matcher.rb → block_matchers.rb} +2 -0
  18. data/lib/mocha/cardinality.rb +2 -2
  19. data/lib/mocha/central.rb +4 -0
  20. data/lib/mocha/change_state_side_effect.rb +2 -0
  21. data/lib/mocha/class_methods.rb +14 -9
  22. data/lib/mocha/configuration.rb +18 -41
  23. data/lib/mocha/default_name.rb +15 -0
  24. data/lib/mocha/default_receiver.rb +13 -0
  25. data/lib/mocha/deprecation.rb +15 -9
  26. data/lib/mocha/detection/minitest.rb +2 -0
  27. data/lib/mocha/detection/test_unit.rb +4 -1
  28. data/lib/mocha/error_with_filtered_backtrace.rb +2 -0
  29. data/lib/mocha/exception_raiser.rb +3 -0
  30. data/lib/mocha/expectation.rb +57 -23
  31. data/lib/mocha/expectation_error.rb +2 -0
  32. data/lib/mocha/expectation_error_factory.rb +2 -0
  33. data/lib/mocha/expectation_list.rb +3 -1
  34. data/lib/mocha/hooks.rb +6 -4
  35. data/lib/mocha/ignoring_warning.rb +20 -0
  36. data/lib/mocha/impersonating_any_instance_name.rb +13 -0
  37. data/lib/mocha/impersonating_name.rb +13 -0
  38. data/lib/mocha/in_state_ordering_constraint.rb +2 -0
  39. data/lib/mocha/inspect.rb +13 -10
  40. data/lib/mocha/instance_method.rb +6 -4
  41. data/lib/mocha/integration/assertion_counter.rb +2 -0
  42. data/lib/mocha/integration/minitest/adapter.rb +7 -3
  43. data/lib/mocha/integration/minitest.rb +5 -4
  44. data/lib/mocha/integration/monkey_patcher.rb +4 -2
  45. data/lib/mocha/integration/test_unit/adapter.rb +10 -4
  46. data/lib/mocha/integration/test_unit.rb +5 -4
  47. data/lib/mocha/integration.rb +5 -0
  48. data/lib/mocha/invocation.rb +9 -6
  49. data/lib/mocha/logger.rb +2 -0
  50. data/lib/mocha/macos_version.rb +2 -0
  51. data/lib/mocha/method_matcher.rb +2 -0
  52. data/lib/mocha/minitest.rb +2 -1
  53. data/lib/mocha/mock.rb +16 -12
  54. data/lib/mocha/mockery.rb +35 -17
  55. data/lib/mocha/name.rb +13 -0
  56. data/lib/mocha/not_initialized_error.rb +2 -0
  57. data/lib/mocha/object_methods.rb +20 -6
  58. data/lib/mocha/object_receiver.rb +20 -0
  59. data/lib/mocha/parameter_matchers/all_of.rb +3 -6
  60. data/lib/mocha/parameter_matchers/any_of.rb +3 -6
  61. data/lib/mocha/parameter_matchers/any_parameters.rb +3 -6
  62. data/lib/mocha/parameter_matchers/anything.rb +3 -6
  63. data/lib/mocha/parameter_matchers/{base.rb → base_methods.rb} +2 -16
  64. data/lib/mocha/parameter_matchers/equals.rb +3 -6
  65. data/lib/mocha/parameter_matchers/equivalent_uri.rb +9 -10
  66. data/lib/mocha/parameter_matchers/has_entries.rb +4 -7
  67. data/lib/mocha/parameter_matchers/has_entry.rb +5 -7
  68. data/lib/mocha/parameter_matchers/has_key.rb +5 -7
  69. data/lib/mocha/parameter_matchers/has_keys.rb +5 -8
  70. data/lib/mocha/parameter_matchers/has_value.rb +5 -7
  71. data/lib/mocha/parameter_matchers/includes.rb +6 -12
  72. data/lib/mocha/parameter_matchers/instance_methods.rb +5 -3
  73. data/lib/mocha/parameter_matchers/instance_of.rb +3 -6
  74. data/lib/mocha/parameter_matchers/is_a.rb +4 -7
  75. data/lib/mocha/parameter_matchers/kind_of.rb +3 -6
  76. data/lib/mocha/parameter_matchers/not.rb +3 -6
  77. data/lib/mocha/parameter_matchers/optionally.rb +2 -5
  78. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +39 -19
  79. data/lib/mocha/parameter_matchers/regexp_matches.rb +4 -6
  80. data/lib/mocha/parameter_matchers/responds_with.rb +3 -6
  81. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +3 -6
  82. data/lib/mocha/parameter_matchers.rb +2 -1
  83. data/lib/mocha/parameters_matcher.rb +9 -1
  84. data/lib/mocha/raised_exception.rb +2 -0
  85. data/lib/mocha/return_values.rb +2 -0
  86. data/lib/mocha/ruby_version.rb +3 -0
  87. data/lib/mocha/sequence.rb +2 -0
  88. data/lib/mocha/single_return_value.rb +1 -1
  89. data/lib/mocha/state_machine.rb +3 -1
  90. data/lib/mocha/stubbed_method.rb +15 -8
  91. data/lib/mocha/stubbing_error.rb +2 -0
  92. data/lib/mocha/test_unit.rb +2 -1
  93. data/lib/mocha/thrower.rb +2 -0
  94. data/lib/mocha/thrown_object.rb +2 -0
  95. data/lib/mocha/version.rb +3 -1
  96. data/lib/mocha/yield_parameters.rb +2 -0
  97. data/lib/mocha.rb +20 -0
  98. data/mocha.gemspec +10 -4
  99. metadata +18 -12
  100. data/lib/mocha/debug.rb +0 -9
  101. data/lib/mocha/integration/minitest/exception_translation.rb +0 -14
  102. data/lib/mocha/is_a.rb +0 -7
  103. data/lib/mocha/names.rb +0 -43
  104. data/lib/mocha/parameter_matchers/deprecations.rb +0 -46
  105. data/lib/mocha/receivers.rb +0 -45
@@ -1,23 +1,24 @@
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
 
5
6
  module Mocha
6
7
  module Integration
7
8
  module Minitest
8
- def self.activate
9
+ def self.activate # rubocop:disable Naming/PredicateMethod
9
10
  target = Detection::Minitest.testcase
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,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/api'
2
4
  require 'mocha/integration/assertion_counter'
3
5
  require 'mocha/expectation_error'
4
6
 
5
7
  module Mocha
6
8
  module Integration
9
+ # Contains {Adapter} that integrates Mocha into recent versions of Test::Unit.
7
10
  module TestUnit
8
11
  # Integrates Mocha into recent versions of Test::Unit.
9
12
  #
@@ -23,13 +26,15 @@ module Mocha
23
26
 
24
27
  # @private
25
28
  def self.included(mod)
26
- mod.setup :mocha_setup, before: :prepend
29
+ mod.setup before: :prepend do
30
+ assertion_counter = Integration::AssertionCounter.new(self)
31
+ mocha_setup(assertion_counter)
32
+ end
27
33
 
28
34
  mod.exception_handler(:handle_mocha_expectation_error)
29
35
 
30
36
  mod.cleanup after: :append do
31
- assertion_counter = Integration::AssertionCounter.new(self)
32
- mocha_verify(assertion_counter)
37
+ mocha_verify
33
38
  end
34
39
 
35
40
  mod.teardown :mocha_teardown, after: :append
@@ -43,8 +48,9 @@ module Mocha
43
48
  end
44
49
 
45
50
  # @private
46
- def handle_mocha_expectation_error(exception)
51
+ def handle_mocha_expectation_error(exception) # rubocop:disable Naming/PredicateMethod
47
52
  return false unless exception.is_a?(Mocha::ExpectationError)
53
+
48
54
  problem_occurred
49
55
  add_failure(exception.message, exception.backtrace)
50
56
  true
@@ -1,23 +1,24 @@
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
 
5
6
  module Mocha
6
7
  module Integration
7
8
  module TestUnit
8
- def self.activate
9
+ def self.activate # rubocop:disable Naming/PredicateMethod
9
10
  target = Detection::TestUnit.testcase
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
 
@@ -0,0 +1,5 @@
1
+ module Mocha
2
+ # Contains adapters that provide built-in support for +Minitest+ and +Test::Unit+.
3
+ module Integration
4
+ end
5
+ end
@@ -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)
@@ -276,8 +276,9 @@ module Mocha
276
276
  end
277
277
 
278
278
  # @private
279
- def initialize(mockery, name = nil, receiver = nil)
279
+ def initialize(mockery, assertion_counter, name = nil, receiver = nil)
280
280
  @mockery = mockery
281
+ @assertion_counter = assertion_counter
281
282
  @name = name || DefaultName.new(self)
282
283
  @receiver = receiver || DefaultReceiver.new(self)
283
284
  @expectations = ExpectationList.new
@@ -315,13 +316,13 @@ module Mocha
315
316
  end
316
317
 
317
318
  # @private
318
- def method_missing(symbol, *arguments, &block) # rubocop:disable Style/MethodMissingSuper
319
+ def method_missing(symbol, *arguments, &block)
319
320
  handle_method_call(symbol, arguments, block)
320
321
  end
321
322
  ruby2_keywords(:method_missing)
322
323
 
323
324
  # @private
324
- def handle_method_call(symbol, arguments, block) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
325
+ def handle_method_call(symbol, arguments, block)
325
326
  check_expiry
326
327
  check_responder_responds_to(symbol)
327
328
  invocation = Invocation.new(self, symbol, arguments, block)
@@ -355,8 +356,8 @@ module Mocha
355
356
  end
356
357
 
357
358
  # @private
358
- def __verified__?(assertion_counter = nil)
359
- @expectations.verified?(assertion_counter)
359
+ def __verified__?
360
+ @expectations.verified?(@assertion_counter)
360
361
  end
361
362
 
362
363
  # @private
@@ -389,7 +390,10 @@ module Mocha
389
390
  def raise_unexpected_invocation_error(invocation, matching_expectation)
390
391
  if @unexpected_invocation.nil?
391
392
  @unexpected_invocation = invocation
392
- matching_expectation.invoke(invocation) if matching_expectation
393
+ if matching_expectation
394
+ matching_expectation.invoke(invocation)
395
+ @assertion_counter.increment
396
+ end
393
397
  call_description = @unexpected_invocation.call_description
394
398
  if matching_expectation && !matching_expectation.in_correct_order?
395
399
  call_description += ' invoked out of order'
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'
@@ -12,6 +17,10 @@ require 'mocha/expectation_error_factory'
12
17
  module Mocha
13
18
  class Mockery
14
19
  class Null < self
20
+ def self.build
21
+ new(nil)
22
+ end
23
+
15
24
  def add_mock(*)
16
25
  raise_not_initialized_error
17
26
  end
@@ -34,49 +43,57 @@ module Mocha
34
43
 
35
44
  class << self
36
45
  def instance
37
- @instances.last || Null.new
46
+ @instances.last || Null.build
38
47
  end
39
48
 
40
- def setup
49
+ def setup(assertion_counter)
41
50
  @instances ||= []
42
- mockery = new
51
+ mockery = new(assertion_counter)
43
52
  mockery.logger = instance.logger unless @instances.empty?
44
53
  @instances.push(mockery)
45
54
  end
46
55
 
47
- def verify(*args)
48
- instance.verify(*args)
56
+ def verify
57
+ instance.verify
49
58
  end
50
59
 
51
60
  def teardown(origin = nil)
61
+ if @instances.nil?
62
+ raise NotInitializedError, 'Mocha::Mockery.teardown called before Mocha::Mockery.setup'
63
+ end
64
+
52
65
  instance.teardown(origin)
53
66
  ensure
54
- @instances.pop
67
+ @instances.pop unless @instances.nil?
55
68
  end
56
69
  end
57
70
 
71
+ def initialize(assertion_counter)
72
+ @assertion_counter = assertion_counter
73
+ end
74
+
58
75
  def named_mock(name)
59
- add_mock(Mock.new(self, Name.new(name)))
76
+ add_mock(Mock.new(self, @assertion_counter, Name.new(name)))
60
77
  end
61
78
 
62
79
  def unnamed_mock
63
- add_mock(Mock.new(self))
80
+ add_mock(Mock.new(self, @assertion_counter))
64
81
  end
65
82
 
66
83
  def mock_impersonating(object)
67
- add_mock(Mock.new(self, ImpersonatingName.new(object), ObjectReceiver.new(object)))
84
+ add_mock(Mock.new(self, @assertion_counter, ImpersonatingName.new(object), ObjectReceiver.new(object)))
68
85
  end
69
86
 
70
87
  def mock_impersonating_any_instance_of(klass)
71
- add_mock(Mock.new(self, ImpersonatingAnyInstanceName.new(klass), AnyInstanceReceiver.new(klass)))
88
+ add_mock(Mock.new(self, @assertion_counter, ImpersonatingAnyInstanceName.new(klass), AnyInstanceReceiver.new(klass)))
72
89
  end
73
90
 
74
91
  def new_state_machine(name)
75
92
  add_state_machine(StateMachine.new(name))
76
93
  end
77
94
 
78
- def verify(assertion_counter = nil)
79
- unless mocks.all? { |mock| mock.__verified__?(assertion_counter) }
95
+ def verify
96
+ unless mocks.all?(&:__verified__?)
80
97
  message = "not all expectations were satisfied\n#{mocha_inspect}"
81
98
  backtrace = if unsatisfied_expectations.empty?
82
99
  caller
@@ -124,12 +141,11 @@ module Mocha
124
141
  def on_stubbing(object, method)
125
142
  signature_proc = lambda { "#{object.mocha_inspect}.#{method}" }
126
143
  check(:stubbing_non_existent_method, 'non-existent method', signature_proc) do
127
- !(object.stubba_class.__method_exists__?(method, true) || object.respond_to?(method))
144
+ !(object.stubba_class.__method_exists__?(method) || object.stubba_respond_to?(method))
128
145
  end
129
146
  check(:stubbing_non_public_method, 'non-public method', signature_proc) do
130
- object.stubba_class.__method_exists__?(method, false)
147
+ object.stubba_class.__method_exists__?(method, include_public_methods: false)
131
148
  end
132
- check(:stubbing_method_on_nil, 'method on nil', signature_proc) { object.nil? }
133
149
  check(:stubbing_method_on_non_mock_object, 'method on non-mock object', signature_proc)
134
150
  end
135
151
 
@@ -144,9 +160,11 @@ module Mocha
144
160
  def check(action, description, signature_proc, backtrace = caller)
145
161
  treatment = Mocha.configuration.send(action)
146
162
  return if (treatment == :allow) || (block_given? && !yield)
163
+
147
164
  method_signature = signature_proc.call
148
165
  message = "stubbing #{description}: #{method_signature}"
149
166
  raise StubbingError.new(message, backtrace) if treatment == :prevent
167
+
150
168
  logger.warn(message) if treatment == :warn
151
169
  end
152
170
 
data/lib/mocha/name.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class Name
5
+ def initialize(name)
6
+ @name = name.to_s
7
+ end
8
+
9
+ def mocha_inspect
10
+ "#<Mock:#{@name}>"
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/error_with_filtered_backtrace'
2
4
 
3
5
  module Mocha
@@ -1,18 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/mockery'
2
4
  require 'mocha/instance_method'
3
5
  require 'mocha/argument_iterator'
4
6
  require 'mocha/expectation_error_factory'
7
+ require 'mocha/ignoring_warning'
5
8
 
6
9
  module Mocha
7
10
  # Methods added to all objects to allow mocking and stubbing on real (i.e. non-mock) objects.
8
11
  #
9
12
  # Both {#expects} and {#stubs} return an {Expectation} which can be further modified by methods on {Expectation}.
10
13
  module ObjectMethods
14
+ extend IgnoringWarning
15
+
11
16
  # @private
12
- alias_method :_method, :method
17
+ JRUBY_ALIAS_SPECIAL_METHODS_WARNING = /accesses caller method's state and should not be aliased/.freeze
18
+
19
+ ignoring_warning(JRUBY_ALIAS_SPECIAL_METHODS_WARNING, if_: RUBY_ENGINE == 'jruby') do
20
+ alias_method :_method, :method
21
+ end
13
22
 
14
23
  # @private
15
- def mocha(instantiate = true)
24
+ def mocha(instantiate: true)
16
25
  if instantiate
17
26
  @mocha ||= Mocha::Mockery.instance.mock_impersonating(self)
18
27
  else
@@ -40,6 +49,11 @@ module Mocha
40
49
  singleton_class
41
50
  end
42
51
 
52
+ # @private
53
+ def stubba_respond_to?(symbol)
54
+ respond_to?(symbol)
55
+ end
56
+
43
57
  # Adds an expectation that the specified method must be called exactly once with any parameters.
44
58
  #
45
59
  # The original implementation of the method is replaced during the test and then restored at the end of the test. The temporary replacement method has the same visibility as the original method.
@@ -75,11 +89,11 @@ module Mocha
75
89
  if frozen?
76
90
  raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
77
91
  end
92
+
78
93
  expectation = nil
79
94
  mockery = Mocha::Mockery.instance
80
95
  iterator = ArgumentIterator.new(expected_methods_vs_return_values)
81
- iterator.each do |*args|
82
- method_name = args.shift
96
+ iterator.each do |method_name, *args|
83
97
  mockery.on_stubbing(self, method_name)
84
98
  method = stubba_method.new(stubba_object, method_name)
85
99
  mockery.stubba.stub(method)
@@ -121,11 +135,11 @@ module Mocha
121
135
  if frozen?
122
136
  raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}", caller)
123
137
  end
138
+
124
139
  expectation = nil
125
140
  mockery = Mocha::Mockery.instance
126
141
  iterator = ArgumentIterator.new(stubbed_methods_vs_return_values)
127
- iterator.each do |*args|
128
- method_name = args.shift
142
+ iterator.each do |method_name, *args|
129
143
  mockery.on_stubbing(self, method_name)
130
144
  method = stubba_method.new(stubba_object, method_name)
131
145
  mockery.stubba.stub(method)
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class ObjectReceiver
5
+ def initialize(object)
6
+ @object = object
7
+ end
8
+
9
+ def mocks
10
+ object = @object
11
+ mocks = []
12
+ while object
13
+ mocha = object.mocha(instantiate: false)
14
+ mocks << mocha if mocha
15
+ object = object.is_a?(Class) ? object.superclass : nil
16
+ end
17
+ mocks
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,6 @@
1
- require 'mocha/parameter_matchers/base'
2
- require 'mocha/parameter_matchers/deprecations'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
3
4
 
4
5
  module Mocha
5
6
  module ParameterMatchers
@@ -27,8 +28,6 @@ module Mocha
27
28
  end
28
29
  end
29
30
 
30
- define_deprecated_matcher_method(:all_of)
31
-
32
31
  # Parameter matcher which combines a number of other matchers using a logical AND.
33
32
  class AllOf
34
33
  include BaseMethods
@@ -49,7 +48,5 @@ module Mocha
49
48
  "all_of(#{@matchers.map(&:mocha_inspect).join(', ')})"
50
49
  end
51
50
  end
52
-
53
- provide_deprecated_access_to(:AllOf)
54
51
  end
55
52
  end
@@ -1,5 +1,6 @@
1
- require 'mocha/parameter_matchers/base'
2
- require 'mocha/parameter_matchers/deprecations'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
3
4
 
4
5
  module Mocha
5
6
  module ParameterMatchers
@@ -33,8 +34,6 @@ module Mocha
33
34
  end
34
35
  end
35
36
 
36
- define_deprecated_matcher_method(:any_of)
37
-
38
37
  # Parameter matcher which combines a number of other matchers using a logical OR.
39
38
  class AnyOf
40
39
  include BaseMethods
@@ -55,7 +54,5 @@ module Mocha
55
54
  "any_of(#{@matchers.map(&:mocha_inspect).join(', ')})"
56
55
  end
57
56
  end
58
-
59
- provide_deprecated_access_to(:AnyOf)
60
57
  end
61
58
  end
@@ -1,5 +1,6 @@
1
- require 'mocha/parameter_matchers/base'
2
- require 'mocha/parameter_matchers/deprecations'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
3
4
 
4
5
  module Mocha
5
6
  module ParameterMatchers
@@ -25,8 +26,6 @@ module Mocha
25
26
  end
26
27
  end
27
28
 
28
- define_deprecated_matcher_method(:any_parameters)
29
-
30
29
  # Parameter matcher which always matches whatever the parameters.
31
30
  class AnyParameters
32
31
  include BaseMethods
@@ -44,7 +43,5 @@ module Mocha
44
43
  'any_parameters'
45
44
  end
46
45
  end
47
-
48
- provide_deprecated_access_to(:AnyParameters)
49
46
  end
50
47
  end
@@ -1,5 +1,6 @@
1
- require 'mocha/parameter_matchers/base'
2
- require 'mocha/parameter_matchers/deprecations'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
3
4
 
4
5
  module Mocha
5
6
  module ParameterMatchers
@@ -22,8 +23,6 @@ module Mocha
22
23
  end
23
24
  end
24
25
 
25
- define_deprecated_matcher_method(:anything)
26
-
27
26
  # Parameter matcher which always matches a single parameter.
28
27
  class Anything
29
28
  include BaseMethods
@@ -39,7 +38,5 @@ module Mocha
39
38
  'anything'
40
39
  end
41
40
  end
42
-
43
- provide_deprecated_access_to(:Anything)
44
41
  end
45
42
  end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/deprecation'
2
- require 'mocha/parameter_matchers/deprecations'
3
4
 
4
5
  module Mocha
5
6
  module ParameterMatchers
@@ -59,20 +60,5 @@ module Mocha
59
60
  AnyOf.new(self, other)
60
61
  end
61
62
  end
62
-
63
- # @deprecated Include +BaseMethods+ module instead.
64
- class Base
65
- include BaseMethods
66
-
67
- # @private
68
- def self.inherited(subclass)
69
- super
70
- Deprecation.warning(
71
- "Include #{BaseMethods} module into #{subclass} instead of inheriting from #{self}."
72
- )
73
- end
74
- end
75
-
76
- provide_deprecated_access_to(:Base)
77
63
  end
78
64
  end
@@ -1,5 +1,6 @@
1
- require 'mocha/parameter_matchers/base'
2
- require 'mocha/parameter_matchers/deprecations'
1
+ # frozen_string_literal: true
2
+
3
+ require 'mocha/parameter_matchers/base_methods'
3
4
 
4
5
  module Mocha
5
6
  module ParameterMatchers
@@ -28,8 +29,6 @@ module Mocha
28
29
  end
29
30
  end
30
31
 
31
- define_deprecated_matcher_method(:equals)
32
-
33
32
  # Parameter matcher which matches when actual parameter equals expected value.
34
33
  class Equals
35
34
  include BaseMethods
@@ -50,7 +49,5 @@ module Mocha
50
49
  @value.mocha_inspect
51
50
  end
52
51
  end
53
-
54
- provide_deprecated_access_to(:Equals)
55
52
  end
56
53
  end