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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61f3a7bc2e73c16b715a31472883ed85fc55726d28e43d051b5d62da21362b52
4
- data.tar.gz: 8e2b6b411543932dfb02f9d97cbe0eb35ed48e983e9079a426c7963f075416da
3
+ metadata.gz: e64d5427fb1829eee6958310ca322357d6fc0a8fa0d7856ea6b616edbf326c45
4
+ data.tar.gz: 052c4fc645bb95c2f09f9d66e1ef6a9025aea478b90accd1fc932de0151f684b
5
5
  SHA512:
6
- metadata.gz: dd352ea7b0ef64b7e56880a90faecd452b67891765a23441013f73405546da1746314025bb4c556b1322ea621a7d28d940efc77272a52b63716993bc27ebe94f
7
- data.tar.gz: e229f55ddd4d53350941bef0af9d3b1026a76f904b666d187ebd6d808feb284101677c66e47005ea3889767626e1cd556330e222d80752f57be89665d4fafd3a
6
+ metadata.gz: efd275f6eb30e58383bc67037953e8f02ffa0d0d9ca920ec492267184fe6f3ac8a4765b074d39d8d3b1de38d1e11a374262e3e4a4a3b3e7ee7877f8aba0529b0
7
+ data.tar.gz: 22b0a2331dc25358032dd83823de41c884a752eb69e80b21befc1d79f931cee4dcc13cf5d40f04bddc3e9ef894b1d2168b90daf3fc89935ed0cd85b41e1b3611
data/.rubocop.yml CHANGED
@@ -1,7 +1,22 @@
1
+ require:
2
+ - rubocop-rake
3
+
1
4
  inherit_from: .rubocop_todo.yml
2
5
 
6
+ inherit_mode:
7
+ merge:
8
+ - Include
9
+
3
10
  AllCops:
4
- TargetRubyVersion: 2.2 # closest to required_ruby_version of '>= 2.1'
11
+ TargetRubyVersion: 2.2
12
+
13
+ Include:
14
+ - '**/Gemfile.*'
15
+
16
+ Exclude:
17
+ - '**/Gemfile*.lock'
18
+
19
+ NewCops: enable
5
20
 
6
21
  # Even the reference in the documentation suggests that you should prefer
7
22
  # `alias_method` vs `alias`, so I don't understand why that isn't the default.
@@ -50,9 +65,58 @@ Layout/AccessModifierIndentation:
50
65
  Enabled: false
51
66
 
52
67
  # Allow long comment lines, e.g. YARD documentation
53
- Metrics/LineLength:
54
- IgnoredPatterns: ['\A\s*#']
68
+ Layout/LineLength:
69
+ AllowedPatterns: ['\A\s*#']
55
70
 
56
71
  # It's not possible to set TargetRubyVersion to Ruby < v2.2
57
72
  Gemspec/RequiredRubyVersion:
58
73
  Enabled: false
74
+
75
+ # It can be useful to violate this cop in tests in order to be more explicit
76
+ Lint/UselessTimes:
77
+ Exclude:
78
+ - 'test/**/*.rb'
79
+
80
+ # It can be useful to violate this cop in tests
81
+ Lint/EmptyClass:
82
+ Exclude:
83
+ - 'test/**/*.rb'
84
+
85
+ # It can be useful to violate this cop in tests when testing methods that accept a block
86
+ Lint/EmptyBlock:
87
+ Exclude:
88
+ - 'test/**/*.rb'
89
+
90
+ # There are a mix of styles in the tests and so I don't think it's worth enforcing for now
91
+ Naming/VariableNumber:
92
+ Exclude:
93
+ - 'test/**/*.rb'
94
+
95
+ # These methods from Ruby core are legitimately overridden in the tests
96
+ Style/OptionalBooleanParameter:
97
+ AllowedMethods:
98
+ - respond_to?
99
+ - public_methods
100
+ - protected_methods
101
+ - private_methods
102
+ - public_instance_methods
103
+ - protected_instance_methods
104
+ - private_instance_methods
105
+
106
+ # This cop is useful for required environment variables, but not for optional ones
107
+ Style/FetchEnvVar:
108
+ AllowedVars:
109
+ - MOCHA_RUN_INTEGRATION_TESTS
110
+
111
+ Naming/FileName:
112
+ ExpectMatchingDefinition: true
113
+ CheckDefinitionPathHierarchyRoots:
114
+ - test/unit
115
+ - test/acceptance
116
+ Exclude:
117
+ - lib/mocha/version.rb
118
+ - lib/mocha/minitest.rb
119
+ - lib/mocha/test_unit.rb
120
+ - lib/mocha/ruby_version.rb
121
+ - lib/mocha/macos_version.rb
122
+ - test/test_helper.rb
data/.rubocop_todo.yml CHANGED
@@ -1,27 +1,39 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-11-16 18:15:36 +0000 using RuboCop version 0.58.2.
3
+ # on 2025-01-03 14:37:32 UTC using RuboCop version 1.69.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 57
9
+ # Offense count: 34
10
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
10
11
  Metrics/AbcSize:
11
- Max: 26
12
+ Max: 27
12
13
 
13
- # Offense count: 23
14
- # Configuration parameters: CountComments.
14
+ # Offense count: 28
15
+ # Configuration parameters: CountComments, CountAsOne.
15
16
  Metrics/ClassLength:
16
- Max: 366
17
+ Max: 381
17
18
 
18
- # Offense count: 172
19
- # Configuration parameters: CountComments.
19
+ # Offense count: 2
20
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
21
+ Metrics/CyclomaticComplexity:
22
+ Max: 9
23
+
24
+ # Offense count: 200
25
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
20
26
  Metrics/MethodLength:
21
- Max: 31
27
+ Max: 27
28
+
29
+ # Offense count: 3
30
+ # Configuration parameters: AllowedMethods, AllowedPatterns.
31
+ Metrics/PerceivedComplexity:
32
+ Max: 10
22
33
 
23
- # Offense count: 545
24
- # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
34
+ # Offense count: 68
35
+ # This cop supports safe autocorrection (--autocorrect).
36
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
25
37
  # URISchemes: http, https
26
- Metrics/LineLength:
27
- Max: 180
38
+ Layout/LineLength:
39
+ Max: 173
data/Gemfile CHANGED
@@ -2,34 +2,18 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- # rubocop:disable Bundler/DuplicatedGem
6
- if RUBY_VERSION < '2.2'
7
- gem 'rake', '~> 12.3.3'
8
- else
5
+ group :development do
6
+ gem 'introspection', '~> 0.0.1'
7
+ gem 'minitest'
9
8
  gem 'rake'
10
- end
11
- # rubocop:enable Bundler/DuplicatedGem
12
9
 
13
- gem 'introspection', '~> 0.0.1'
14
-
15
- # Avoid breaking change in psych v4 (https://bugs.ruby-lang.org/issues/17866)
16
- if RUBY_VERSION >= '3.1.0'
17
- gem 'psych', '< 4'
18
- end
10
+ if RUBY_ENGINE == 'jruby'
11
+ # Workaround for https://github.com/jruby/jruby/issues/8488
12
+ gem 'jar-dependencies', '~> 0.4.1'
13
+ end
19
14
 
20
- if RUBY_VERSION >= '2.2.0'
21
- # No test libraries in standard library
22
- gem 'minitest'
23
- end
24
- if RUBY_VERSION >= '2.2.0'
25
- gem 'jaro_winkler', '>= 1.5.5'
26
- gem 'rubocop', '> 0.56', '<= 0.58.2'
27
- end
28
- if RUBY_ENGINE == 'jruby'
29
- # Workaround for https://github.com/jruby/jruby/issues/8488
30
- gem 'jar-dependencies', '~> 0.4.1'
31
- end
32
- if ENV['MOCHA_GENERATE_DOCS']
33
- gem 'redcarpet'
34
- gem 'yard'
15
+ if ENV['MOCHA_GENERATE_DOCS']
16
+ gem 'redcarpet'
17
+ gem 'yard'
18
+ end
35
19
  end
data/README.md CHANGED
@@ -292,10 +292,9 @@ If you want, Mocha can generate a warning or raise an exception when:
292
292
 
293
293
  See the [documentation](https://mocha.jamesmead.org/Mocha/Configuration.html) for `Mocha::Configuration` for further details.
294
294
 
295
- ##### MOCHA_OPTIONS
296
- `MOCHA_OPTIONS` is an environment variable whose value can be set to a comma-separated list, so that we can specify multiple options e.g. `MOCHA_OPTIONS=debug,use_test_unit_gem`.
297
- Only the following values are currently recognized and have an effect:
298
- * `debug`: Enables a debug mode which will output backtraces for each deprecation warning. This is useful for finding where in the test suite the deprecated calls are.
295
+ ### Debugging
296
+
297
+ Mocha provides some extra output to help with debugging when the standard Ruby debug option (`-d`) is set.
299
298
 
300
299
  ### Semantic versioning
301
300
 
data/RELEASE.md CHANGED
@@ -1,5 +1,42 @@
1
1
  # Release Notes
2
2
 
3
+ ## 3.0.0-rc.1
4
+
5
+ ### External changes
6
+
7
+ * Improvements to docs for cardinality-related methods (#700)
8
+ * Drop support for Ruby v2.1 (#628 & #695)
9
+ * Improve gemspec description (#692 & #696)
10
+ * Remove docs for `use_test_unit_gem` option (eaf03493)
11
+ * Correct docs for `MOCHA_OPTIONS` "debug" value (69ef41c0)
12
+ * Use built-in Ruby debug option vs custom module (#714 & #715)
13
+ * Enable strict keyword argument matching by default in Ruby >= v3 (#697 & #718)
14
+ * Use Ruby v1.9 Hash syntax in `Hash#mocha_inspect` used in failure messages (#651 & #719)
15
+ * Improve `#mocha_inspect` for empty keyword argument `Hash` (#588 & #720) - thanks to @herwinw for reporting
16
+ * Improve error if `Mockery.teardown` called before `Mockery.setup` (#611, #646 & #721) - thanks to @riniculous for reporting
17
+ * Freeze string literals (#722)
18
+ * Remove support for running tests with Minitest < v5 (#727)
19
+ * Allow keyword arguments to match an expectation expecting *only* positional arguments (#593 & #732) - thanks to @seandilda
20
+ for reporting
21
+
22
+ ### Internal changes
23
+
24
+ * Add `Expectation#thrice` (#701) - thanks to @andrewn617
25
+ * Add `base64` to Gemfile for Ruby >= v3.3.0 to avoid warning (8c49314c & a76330d6)
26
+ * Include custom Gemfiles in Rubocop linting (04063f0d)
27
+ * Use ruby:3.4.0-rc1 vs ruby:3.4.0-preview2 in CI in preparation for the Ruby v3.4 release (b2127407)
28
+ * Add `ostruct` to Gemfile for Ruby >= v3.4.0 to avoid warning (2b05e09e)
29
+ * Add Ruby v3.4 to CI build matrix (#708)
30
+ * Fix Ruby v3.4 warnings in ObjectInspectTest (#709 & #710)
31
+ * Rubocop-related improvements (#702 & #705)
32
+ * Consistently use development group in Gemfiles (#706 & #716)
33
+ * Add missing requires for 'mocha/ruby_version' (7f99db69)
34
+ * Convert `ParameterMatchers::Base` class -> module (#712 & #723)
35
+ * Enable `ExpectMatchingDefinition` option on `Naming/FileName` cop (#726)
36
+ * Remove duplicate `DefaultReceiverTest` (39d99954)
37
+ * Improvements to keyword argument matching tests (#730)
38
+ * Improvements to keyword argument matcher unit test (#731)
39
+
3
40
  ## 2.7.1
4
41
 
5
42
  ### External changes
data/Rakefile CHANGED
@@ -6,9 +6,10 @@ require 'bundler/setup'
6
6
 
7
7
  require 'rake/testtask'
8
8
  begin
9
- # Only available with default Gemfile and in Ruby >= v2.2
9
+ # Only available with `gemfiles/Gemfile.rubocop`
10
10
  require 'rubocop/rake_task'
11
- rescue LoadError # rubocop:disable Lint/HandleExceptions
11
+ rescue LoadError
12
+ warn "Unable to load 'rubocop/rake_task', but continuing anyway" if $DEBUG
12
13
  end
13
14
 
14
15
  desc 'Run all linters and tests'
@@ -81,6 +82,7 @@ namespace 'test' do # rubocop:disable Metrics/BlockLength
81
82
  end
82
83
  end
83
84
 
85
+ desc 'Run linters'
84
86
  task 'lint' do
85
87
  if defined?(RuboCop::RakeTask)
86
88
  RuboCop::RakeTask.new
@@ -90,7 +92,6 @@ task 'lint' do
90
92
  end
91
93
  end
92
94
 
93
- # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
94
95
  def benchmark_test_case(klass, iterations)
95
96
  require 'benchmark'
96
97
  require 'mocha/detection/minitest'
@@ -119,8 +120,6 @@ def benchmark_test_case(klass, iterations)
119
120
  Benchmark.realtime { iterations.times { Test::Unit::UI::Console::TestRunner.run(klass, @silent_option) } }
120
121
  end
121
122
  end
122
- # rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
123
-
124
123
  if ENV['MOCHA_GENERATE_DOCS']
125
124
  require 'yard'
126
125
 
@@ -134,10 +133,12 @@ if ENV['MOCHA_GENERATE_DOCS']
134
133
  task.options = ['--title', "Mocha #{Mocha::VERSION}", '--fail-on-warning']
135
134
  end
136
135
 
136
+ desc 'Ensure custom domain remains in place for docs on GitHub Pages'
137
137
  task 'checkout_docs_cname' do
138
138
  `git checkout docs/CNAME`
139
139
  end
140
140
 
141
+ desc 'Ensure custom JavaScript files remain in place for docs on GitHub Pages'
141
142
  task 'checkout_docs_js' do
142
143
  `git checkout docs/js/app.js`
143
144
  `git checkout docs/js/jquery.js`
@@ -1,8 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec :path=>"../"
3
+ gemspec path: '../'
4
4
 
5
5
  group :development do
6
- gem "rake"
7
- gem "minitest"
6
+ gem 'rake'
7
+ gem 'minitest'
8
8
  end
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ gem 'rubocop'
8
+ gem 'rubocop-rake'
9
+ end
@@ -1,8 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec :path=>"../"
3
+ gemspec path: '../'
4
4
 
5
5
  group :development do
6
- gem "rake"
7
- gem "test-unit"
6
+ gem 'rake'
7
+ gem 'test-unit'
8
8
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/stubbed_method'
2
4
 
3
5
  module Mocha
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class AnyInstanceReceiver
5
+ def initialize(klass)
6
+ @klass = klass
7
+ end
8
+
9
+ def mocks
10
+ klass = @klass
11
+ mocks = []
12
+ while klass
13
+ mocha = klass.any_instance.mocha(instantiate: false)
14
+ mocks << mocha if mocha
15
+ klass = klass.superclass
16
+ end
17
+ mocks
18
+ end
19
+ end
20
+ end
data/lib/mocha/api.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/ruby_version'
2
4
  require 'mocha/parameter_matchers'
3
5
  require 'mocha/hooks'
@@ -37,8 +39,8 @@ module Mocha
37
39
 
38
40
  # @private
39
41
  def self.included(_mod)
40
- Object.send(:include, Mocha::ObjectMethods)
41
- Class.send(:include, Mocha::ClassMethods)
42
+ Object.include Mocha::ObjectMethods
43
+ Class.include Mocha::ClassMethods
42
44
  end
43
45
 
44
46
  # @private
@@ -1,16 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class ArgumentIterator
3
5
  def initialize(argument)
4
6
  @argument = argument
5
7
  end
6
8
 
7
- def each
9
+ def each(&block)
8
10
  if @argument.is_a?(Hash)
9
- @argument.each do |method_name, return_value|
10
- yield method_name, return_value
11
- end
11
+ @argument.each(&block)
12
12
  else
13
- yield @argument
13
+ block.call(@argument)
14
14
  end
15
15
  end
16
16
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class BacktraceFilter
3
5
  LIB_DIRECTORY = File.expand_path(File.join(File.dirname(__FILE__), '..')) + File::SEPARATOR
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  module BlockMatchers
3
5
  class OptionalBlock
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class Cardinality
3
5
  INFINITY = 1 / 0.0
@@ -58,7 +60,6 @@ module Mocha
58
60
  @invocations.any? || maximum.zero?
59
61
  end
60
62
 
61
- # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
62
63
  def anticipated_times
63
64
  if allowed_any_number_of_times?
64
65
  'allowed any number of times'
@@ -74,7 +75,6 @@ module Mocha
74
75
  "expected between #{required} and #{count(maximum)}"
75
76
  end
76
77
  end
77
- # rubocop:enable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
78
78
 
79
79
  def invoked_times
80
80
  "invoked #{count(@invocations.size)}"
data/lib/mocha/central.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class Central
3
5
  class Null < self
@@ -23,12 +25,14 @@ module Mocha
23
25
 
24
26
  def stub(method)
25
27
  return if stubba_methods.detect { |m| m.matches?(method) }
28
+
26
29
  method.stub
27
30
  stubba_methods.push(method)
28
31
  end
29
32
 
30
33
  def unstub(method)
31
34
  return unless (existing = stubba_methods.detect { |m| m.matches?(method) })
35
+
32
36
  existing.unstub
33
37
  stubba_methods.delete(existing)
34
38
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mocha
2
4
  class ChangeStateSideEffect
3
5
  def initialize(state)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/mockery'
2
4
  require 'mocha/any_instance_method'
3
5
 
@@ -10,7 +12,7 @@ module Mocha
10
12
  @stubba_object = klass
11
13
  end
12
14
 
13
- def mocha(instantiate = true)
15
+ def mocha(instantiate: true)
14
16
  if instantiate
15
17
  @mocha ||= Mocha::Mockery.instance.mock_impersonating_any_instance_of(@stubba_object)
16
18
  else
@@ -46,17 +48,16 @@ module Mocha
46
48
  if frozen?
47
49
  raise StubbingError.new("can't stub method on frozen object: #{mocha_inspect}.any_instance", caller)
48
50
  end
51
+
49
52
  @any_instance ||= AnyInstance.new(self)
50
53
  end
51
54
 
52
55
  # @private
53
- # rubocop:disable Metrics/CyclomaticComplexity
54
- def __method_visibility__(method, include_public_methods = true)
56
+ def __method_visibility__(method, include_public_methods: true)
55
57
  (include_public_methods && public_method_defined?(method) && :public) ||
56
58
  (protected_method_defined?(method) && :protected) ||
57
59
  (private_method_defined?(method) && :private)
58
60
  end
59
- # rubocop:enable Metrics/CyclomaticComplexity
60
61
  alias_method :__method_exists__?, :__method_visibility__
61
62
  end
62
63
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'mocha/ruby_version'
2
4
  require 'mocha/deprecation'
3
5
 
@@ -14,7 +16,6 @@ module Mocha
14
16
  # Mocha.configure do |c|
15
17
  # c.stubbing_method_unnecessarily = :prevent
16
18
  # c.stubbing_method_on_non_mock_object = :warn
17
- # c.stubbing_method_on_nil = :allow
18
19
  # end
19
20
  #
20
21
  def self.configure
@@ -34,7 +35,6 @@ module Mocha
34
35
  # Mocha.configure do |c|
35
36
  # c.stubbing_method_unnecessarily = :prevent
36
37
  # c.stubbing_method_on_non_mock_object = :warn
37
- # c.stubbing_method_on_nil = :allow
38
38
  # end
39
39
  #
40
40
  class Configuration
@@ -44,9 +44,8 @@ module Mocha
44
44
  stubbing_method_on_non_mock_object: :allow,
45
45
  stubbing_non_existent_method: :allow,
46
46
  stubbing_non_public_method: :allow,
47
- stubbing_method_on_nil: :prevent,
48
47
  display_matching_invocations_on_failure: false,
49
- strict_keyword_argument_matching: false
48
+ strict_keyword_argument_matching: Mocha::RUBY_V30_PLUS
50
49
  }.freeze
51
50
 
52
51
  attr_reader :options
@@ -196,33 +195,6 @@ module Mocha
196
195
  @options[:stubbing_non_public_method]
197
196
  end
198
197
 
199
- # Configure whether stubbing methods on the +nil+ object is allowed.
200
- #
201
- # This is usually done accidentally, but there might be rare cases where it is intended.
202
- #
203
- # This option only works for Ruby < v2.2.0. In later versions of Ruby +nil+ is frozen and so a {StubbingError} will be raised if you attempt to stub a method on +nil+.
204
- #
205
- # When +value+ is +:allow+, do nothing.
206
- # When +value+ is +:warn+, display a warning.
207
- # When +value+ is +:prevent+, raise a {StubbingError}. This is the default.
208
- #
209
- # @param [Symbol] value one of +:allow+, +:warn+, +:prevent+.
210
- # @deprecated This method is deprecated and will be removed in a future release. +nil+ is frozen in Ruby >= v2.2 and Mocha will be dropping support for Ruby v2.1. At that point it won't be possible to stub methods on +nil+ any more.
211
- #
212
- def stubbing_method_on_nil=(value)
213
- Deprecation.warning([
214
- '`Mocha::Configuration#stubbing_method_on_nil=` is deprecated and will be removed in a future release.',
215
- '`nil` is frozen in Ruby >= v2.2 and Mocha will be dropping support for Ruby v2.1.',
216
- "At that point it won't be possible to stub methods on `nil` any more."
217
- ].join(' '))
218
- @options[:stubbing_method_on_nil] = value
219
- end
220
-
221
- # @private
222
- def stubbing_method_on_nil
223
- @options[:stubbing_method_on_nil]
224
- end
225
-
226
198
  # Display matching invocations alongside expectations on Mocha-related test failure.
227
199
  #
228
200
  # @param [Boolean] value +true+ to enable display of matching invocations; disabled by default.
@@ -259,17 +231,21 @@ module Mocha
259
231
 
260
232
  # Perform strict keyword argument comparison. Only supported in Ruby >= v2.7.
261
233
  #
262
- # 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 misleading passing tests 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.
234
+ # 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 misleading passing tests 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.
263
235
  #
264
236
  # 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}.
265
237
  #
266
238
  # 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 misleading passing tests are still possible when they are used.
267
239
  #
268
- # This configuration option is +false+ by default to enable gradual adoption, but will be +true+ by default in the future.
240
+ # This configuration option is +false+ by default in Ruby v2.7 to enable gradual adoption, but +true+ by default in Ruby >= v3.0.
269
241
  #
270
- # @param [Boolean] value +true+ to enable strict keyword argument matching; +false+ by default.
242
+ # @param [Boolean] value +true+ to enable strict keyword argument matching.
271
243
  #
272
- # @example Loose keyword argument matching (default)
244
+ # @example Loose keyword argument matching (default in Ruby v2.7)
245
+ #
246
+ # Mocha.configure do |c|
247
+ # c.strict_keyword_argument_matching = false
248
+ # end
273
249
  #
274
250
  # class Example
275
251
  # def foo(a, bar:); end
@@ -280,7 +256,7 @@ module Mocha
280
256
  # example.foo('a', { bar: 'b' })
281
257
  # # This passes the test, but would result in an ArgumentError in practice
282
258
  #
283
- # @example Strict keyword argument matching
259
+ # @example Strict keyword argument matching (default in Ruby >= v3.0)
284
260
  #
285
261
  # Mocha.configure do |c|
286
262
  # c.strict_keyword_argument_matching = true
@@ -296,6 +272,7 @@ module Mocha
296
272
  # # This now fails as expected
297
273
  def strict_keyword_argument_matching=(value)
298
274
  raise 'Strict keyword argument matching requires Ruby 2.7 and above.' unless Mocha::RUBY_V27_PLUS
275
+
299
276
  @options[:strict_keyword_argument_matching] = value
300
277
  end
301
278
 
@@ -318,9 +295,9 @@ module Mocha
318
295
  # @param [Hash] temporary_options the configuration options to apply for the duration of the block.
319
296
  # @yield block during which the configuration change will be in force.
320
297
  #
321
- # @example Temporarily allow stubbing of +nil+
322
- # Mocha::Configuration.override(stubbing_method_on_nil: :allow) do
323
- # nil.stubs(:foo)
298
+ # @example Temporarily prevent stubbing of non-mock object
299
+ # Mocha::Configuration.override(stubbing_method_on_non_mock_object: :prevent) do
300
+ # 123.stubs(:to_s).returns('456')
324
301
  # end
325
302
  def override(temporary_options)
326
303
  original_configuration = configuration
@@ -342,7 +319,7 @@ module Mocha
342
319
  if block_given?
343
320
  temporarily_change_config action, new_value, &block
344
321
  else
345
- configuration.send("#{action}=".to_sym, new_value)
322
+ configuration.send(:"#{action}=", new_value)
346
323
  end
347
324
  end
348
325
 
@@ -350,7 +327,7 @@ module Mocha
350
327
  def temporarily_change_config(action, new_value)
351
328
  original_configuration = configuration
352
329
  new_configuration = configuration.dup
353
- new_configuration.send("#{action}=".to_sym, new_value)
330
+ new_configuration.send(:"#{action}=", new_value)
354
331
  @configuration = new_configuration
355
332
  yield
356
333
  ensure
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mocha
4
+ class DefaultName
5
+ def initialize(mock)
6
+ @mock = mock
7
+ end
8
+
9
+ def mocha_inspect
10
+ address = @mock.__id__ * 2
11
+ address += 0x100000000 if address < 0
12
+ "#<Mock:0x#{format('%<address>x', address: address)}>"
13
+ end
14
+ end
15
+ end