mocha 2.8.2 → 3.0.0.pre.rc.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.
Files changed (102) 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 +30 -25
  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 +5 -3
  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 +19 -42
  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 +59 -25
  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 +4 -3
  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 +23 -28
  57. data/lib/mocha/parameter_matchers/any_of.rb +29 -34
  58. data/lib/mocha/parameter_matchers/any_parameters.rb +21 -26
  59. data/lib/mocha/parameter_matchers/anything.rb +18 -23
  60. data/lib/mocha/parameter_matchers/base.rb +5 -23
  61. data/lib/mocha/parameter_matchers/equals.rb +24 -29
  62. data/lib/mocha/parameter_matchers/equivalent_uri.rb +23 -28
  63. data/lib/mocha/parameter_matchers/has_entries.rb +24 -29
  64. data/lib/mocha/parameter_matchers/has_entry.rb +68 -70
  65. data/lib/mocha/parameter_matchers/has_key.rb +25 -29
  66. data/lib/mocha/parameter_matchers/has_keys.rb +25 -30
  67. data/lib/mocha/parameter_matchers/has_value.rb +25 -29
  68. data/lib/mocha/parameter_matchers/includes.rb +65 -73
  69. data/lib/mocha/parameter_matchers/instance_methods.rb +5 -3
  70. data/lib/mocha/parameter_matchers/instance_of.rb +24 -29
  71. data/lib/mocha/parameter_matchers/is_a.rb +25 -30
  72. data/lib/mocha/parameter_matchers/kind_of.rb +24 -29
  73. data/lib/mocha/parameter_matchers/not.rb +24 -29
  74. data/lib/mocha/parameter_matchers/optionally.rb +35 -40
  75. data/lib/mocha/parameter_matchers/positional_or_keyword_hash.rb +39 -19
  76. data/lib/mocha/parameter_matchers/regexp_matches.rb +25 -29
  77. data/lib/mocha/parameter_matchers/responds_with.rb +48 -53
  78. data/lib/mocha/parameter_matchers/yaml_equivalent.rb +23 -28
  79. data/lib/mocha/parameter_matchers.rb +4 -6
  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 -16
  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/parameter_matchers/deprecations.rb +0 -44
  102. data/lib/mocha/receivers.rb +0 -45
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9b6824de22258fd93ce5b15925529aa65d031f13c6498c5816ef922e55177ac
4
- data.tar.gz: 8b9b87a21403c0b478dd1d8827401684d13b43c761acb3d3ade545bb620c851a
3
+ metadata.gz: e64d5427fb1829eee6958310ca322357d6fc0a8fa0d7856ea6b616edbf326c45
4
+ data.tar.gz: 052c4fc645bb95c2f09f9d66e1ef6a9025aea478b90accd1fc932de0151f684b
5
5
  SHA512:
6
- metadata.gz: 5162913f48075d311d6630d69ed3e1aa1d9bdcf1e260b88dd7e1e0ddffdca4c2a26c544cb38b5bad0750d3c44df50dc23cdad00e2915fb7e3c298dd645c1a9be
7
- data.tar.gz: 6c914b616fef69f33643a50d86f2593e495ede30a2887122df2d84dbfc1ae4d9bfcd4bdc073821d57dbe92e174f6907e7c59e68ea9eb948e33b278c561251ca0
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,36 +1,41 @@
1
1
  # Release Notes
2
2
 
3
- ## 2.8.2
3
+ ## 3.0.0-rc.1
4
4
 
5
5
  ### External changes
6
6
 
7
- * Improve source location of deprecation warning for `ParameterMatchers` matcher classes (c5171488)
8
-
9
- ## 2.8.1
10
-
11
- ### External changes
12
-
13
- * Restore unqualified access to `ParameterMatchers` matcher classes, but deprecate such access (5231a4f7)
14
- * Restore unqualified access to `ParameterMatchers::BaseMethods`, but deprecate such access (8a898567)
15
-
16
- ## 2.8.0
17
-
18
- Many thanks to @etiennebarrie for his help in testing v3.0.0 release candidates which led to many of these changes.
19
-
20
- ### External changes
21
-
22
- * Extract `ParameterMatchers::BaseMethods` module and deprecate inheriting from `ParameterMatchers::Base` class (0ddfbe4b)
23
- * Move matcher builder methods into `ParameterMatchers::Methods` module and only include that module into `API` (2de41423)
24
- * Provide deprecated access to matcher builder methods when including `ParameterMatchers` module (299488e1)
25
- * Provide deprecated access to matcher classes, e.g. `ParameterMatchers::Equals`, from within tests/specs (dcced1b4)
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
26
21
 
27
22
  ### Internal changes
28
23
 
29
- * Move `ParameterMatchers#parse_option` -> `HasEntry.parse_option` (9e2a6f66)
30
-
31
- **WARNING: This release accidentally removed access to ParameterMatchers classes**
32
- * Fully-qualified access from within a test/spec class still works, e.g. `Mocha::ParameterMatchers::HasKey`, but unqualified access does not work, e.g. `HasKey`.
33
- * The mistake has been fixed in v2.8.1 where unqualified access is restored but deprecated. Unqualified access will be removed in v3.
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)
34
39
 
35
40
  ## 2.7.1
36
41
 
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'
@@ -32,13 +34,13 @@ module Mocha
32
34
  # sheep.chew
33
35
  # sheep.foo # => raises NoMethodError exception
34
36
  module API
35
- include ParameterMatchers::Methods
37
+ include ParameterMatchers
36
38
  include Hooks
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