minitest-substitute 0.2.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ada3a9ae885b2050d30579935751f6f6d6d3a668b299def79602098549897ff6
4
- data.tar.gz: 2892e09363f655605a9c7f19da56cc40f2229d44cb332e4b583d50f193c18cef
3
+ metadata.gz: 9f8802ed1f575511d0d40b4c9c6aa83ac9e0438a4e516a3e9d371d64cbeec44c
4
+ data.tar.gz: 94dab332d69b4340512030e332d20b68975a80b0860f7e8cdb72334c25cfd076
5
5
  SHA512:
6
- metadata.gz: 3222b3c80b2cf6aee7451d14f602b6aa154367a4ef95b873bc1917d2c76ab240df9183a2322c917507737059eb7a83b3d17f5952c8363f23874c04e4eaa81eda
7
- data.tar.gz: d85e9ea4fb25448a7065c0d0c32a0aa28b00df47cc03495d72b2f8df8134c43fb7aadb920dda4c1b71792012a0bf048f1ac7f0e863bf55b4b30d4a6b780449bb
6
+ metadata.gz: 9ebd589721d0d15745712e3b280cf395bce7d185ccdbe9fc728213c87a7991d7b8ba442e853ce7932e673bc6693e98b6712b3bf75878ce240b6eeb7298ae0a97
7
+ data.tar.gz: fc1cb86a0d3f3123afcde191b906c154044975f4fe95eb8b0f9af0639aa27c822da7377f36e0213fcc380d2b776ed00c5db5bace1a46c6945e994ca61928f7a2
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -2,9 +2,21 @@
2
2
 
3
3
  Nothing so far
4
4
 
5
+ ## 1.0.0
6
+
7
+ #### Breaking Changes
8
+ * Switch from `with` to `substitute` because Rails 7 pollutes `Object` for
9
+ everybody by introducing `Object#with`. 💣
10
+
11
+ ## 0.3.0
12
+
13
+ #### Additions
14
+ * Substition of constants
15
+
5
16
  ## 0.2.1
6
17
 
7
- Nothing so far
18
+ #### Changes
19
+ * Defer evaluation of the substitution value block for specs
8
20
 
9
21
  ## 0.2.0
10
22
 
data/README.md CHANGED
@@ -18,7 +18,7 @@ This comes in very handy when you have to derive from default configuration in o
18
18
  This gem is [cryptographically signed](https://guides.rubygems.org/security/#using-gems) in order to assure it hasn't been tampered with. Unless already done, please add the author's public key as a trusted certificate now:
19
19
 
20
20
  ```
21
- gem cert --add <(curl -Ls https://raw.github.com/svoop/minitest-substitute/main/certs/svoop.pem)
21
+ gem cert --add <(curl -Ls https://bitcetera.com/downloads/gem-public_cert.pem)
22
22
  ```
23
23
 
24
24
  Add the following to the <tt>Gemfile</tt> or <tt>gems.rb</tt> of your [Bundler](https://bundler.io) powered Ruby project:
@@ -39,6 +39,24 @@ Finally, require this gem in your `test_helper.rb` or `spec_helper.rb`:
39
39
  require 'minitest/substitute'
40
40
  ```
41
41
 
42
+ ## Update from 0.x.x to 1.x.x
43
+
44
+ Rails 7 has polluted `Object` for everybody by introducing `Object#with`. To prevent collisions, Minitest::Substitute has switched from `with` to `substitute` as of version 1.0.0.
45
+
46
+ After having updated this gem, you'll have to adapt all your tests accordingly:
47
+
48
+ ```ruby
49
+ # Version 0.x.x
50
+ with '@version', 2, on: config do
51
+ config.instance_variable_get('@version') # => 2
52
+ end
53
+
54
+ # Version 1.x.x
55
+ substitute '@version', 2, on: config do
56
+ config.instance_variable_get('@version') # => 2
57
+ end
58
+ ```
59
+
42
60
  ## Usage
43
61
 
44
62
  ### Block
@@ -55,7 +73,7 @@ end
55
73
  config = Config.new
56
74
 
57
75
  config.instance_variable_get('@version') # => 1
58
- with '@version', 2, on: config do
76
+ substitute '@version', 2, on: config do
59
77
  config.instance_variable_get('@version') # => 2
60
78
  end
61
79
  config.instance_variable_get('@version') # => 1
@@ -71,7 +89,7 @@ class Config
71
89
  end
72
90
 
73
91
  Config.class_variable_get('@@counter') # => 0
74
- with '@@counter', 42, on: Config do
92
+ substitute '@@counter', 42, on: Config do
75
93
  Config.class_variable_get('@@counter') # => 42
76
94
  end
77
95
  Config.class_variable_get('@@counter') # => 0
@@ -81,23 +99,60 @@ Same goes for global variables:
81
99
 
82
100
  ```ruby
83
101
  $verbose = false # => false
84
- with '$verbose', true do
102
+ substitute '$verbose', true do
85
103
  $verbose # => true
86
104
  end
87
105
  $verbose # => false
88
106
  ```
89
107
 
90
- And it works for hashes as well which comes in handy when you have to temporarily override the value of an environment variable:
108
+ And it works for globals like `ENV` as well which comes in handy when you have to temporarily override the value of an environment variable:
91
109
 
92
110
  ```ruby
93
111
  ENV['EDITOR'] # => 'vi'
94
- with "ENV['EDITOR']", 'nano' do
112
+ substitute "ENV['EDITOR']", 'nano' do
95
113
  ENV['EDITOR'] # => 'nano'
96
114
  end
97
115
  ENV['EDITOR'] # => 'vi'
98
116
  ```
99
117
 
100
- It's safe to nest multiple `with` statements.
118
+ You can even substitute constants, however, you have to use their absolute name starting with `::`:
119
+
120
+ ```ruby
121
+ module Animals
122
+ DOG_MAKES = 'woof'
123
+ CAT_MAKES = 'meow'
124
+ end
125
+
126
+ Animals::DOG_MAKES # => 'woof'
127
+ substitute '::Animals::DOG_MAKES', Animals::CAT_MAKES do
128
+ Animals::DOG_MAKES # => 'meow'
129
+ end
130
+ Animals::DOG_MAKES # => 'woof'
131
+ ```
132
+
133
+ Remember that class declarations are assigned to constants as well:
134
+
135
+ ```ruby
136
+ class Dog
137
+ self.makes
138
+ 'woof'
139
+ end
140
+ end
141
+
142
+ class Cat
143
+ self.makes
144
+ 'meow'
145
+ end
146
+ end
147
+
148
+ Dog.makes # => 'woof'
149
+ substitute '::Dog', Cat do
150
+ Dog.makes # => 'meow'
151
+ end
152
+ Dog.makes # => 'woof'
153
+ ```
154
+
155
+ It's safe to nest multiple `substitute` statements.
101
156
 
102
157
  ### Group of Tests
103
158
 
@@ -122,7 +177,7 @@ describe Config do
122
177
  end
123
178
 
124
179
  describe 'sustituted version' do
125
- with '@version', 2, on: Config
180
+ substitute '@version', 2, on: Config
126
181
 
127
182
  it "returns the substituted version" do
128
183
  _(subject.instance_variable_get('@version')).must_equal 2
@@ -158,7 +213,7 @@ describe Config do
158
213
  end
159
214
 
160
215
  describe 'sustituted version' do
161
- with '@version', on: Config do
216
+ substitute '@version', on: Config do
162
217
  version # set using "let" above
163
218
  end
164
219
 
@@ -171,7 +226,7 @@ end
171
226
 
172
227
  If both a substitution value and a substitution block are present, the latter takes precedence.
173
228
 
174
- It's safe to use multiple `with` statements within one `describe` block.
229
+ It's safe to use multiple `substitute` statements within one `describe` block.
175
230
 
176
231
  (The spec integration is borrowed from [minitest-around](https://rubygems.org/gems/minitest-around) for elegance and compatibility.)
177
232
 
@@ -9,13 +9,13 @@ Minitest::Spec::DSL.class_eval do
9
9
  # @param variable [String] instance or global variable name
10
10
  # @param substitute [Object] temporary substitution value
11
11
  # @param on [Object, Symbol, nil] substitute in the context of this object
12
- # or in the context of the declared subject if +:subject+ is set
12
+ # or in the context of the declared subject if +nil+
13
13
  # @yield temporary substitution value (takes precedence over +substitute+ param)
14
- def with(variable, substitute=nil, on: :subject, &block)
14
+ def substitute(variable, substitute=nil, on: nil, &block)
15
15
  substitutor = Minitest::Substitute::Substitutor.new(variable, on: on)
16
16
  before do
17
17
  substitutor.substitute(block ? instance_eval(&block) : substitute)
18
- substitutor.on = subject if on == :subject && respond_to?(:subject)
18
+ substitutor.on = subject if on.nil? && respond_to?(:subject)
19
19
  substitutor.commit
20
20
  end
21
21
  after do
@@ -29,7 +29,7 @@ Minitest::Spec::DSL.class_eval do
29
29
  include(Module.new do
30
30
  define_method(:setup) do
31
31
  super()
32
- instance_eval &block
32
+ instance_eval(&block)
33
33
  end
34
34
  end) # .then &:include
35
35
  end
@@ -38,7 +38,7 @@ Minitest::Spec::DSL.class_eval do
38
38
  def after(_type=nil, &block)
39
39
  include(Module.new do
40
40
  define_method(:teardown) do
41
- instance_eval &block
41
+ instance_eval(&block)
42
42
  ensure
43
43
  super()
44
44
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Minitest
4
4
  module Substitute
5
- module With
5
+ module Substitute
6
6
 
7
7
  # Substitute the variable value for the duration of the given block
8
8
  #
@@ -11,7 +11,7 @@ module Minitest
11
11
  # @param on [Object, nil] substitute in the context of this object
12
12
  # @yield block during which the substitution is made
13
13
  # @return [Object] return value of the yielded block
14
- def with(variable, substitute, on: self)
14
+ def substitute(variable, substitute, on: self)
15
15
  substitutor = Minitest::Substitute::Substitutor.new(variable, on: on).substitute(substitute)
16
16
  substitutor.commit
17
17
  yield.tap do
@@ -4,12 +4,10 @@ module Minitest
4
4
  module Substitute
5
5
  class Substitutor
6
6
 
7
- EVAL_METHODS = [:instance_eval, :instance_eval, :class_eval].freeze
8
-
9
7
  attr_writer :on
10
8
 
11
- def initialize(variable, on:)
12
- @variable, @on = variable, on
9
+ def initialize(target, on:)
10
+ @target, @on = target, on
13
11
  end
14
12
 
15
13
  def substitute(value)
@@ -28,16 +26,32 @@ module Minitest
28
26
 
29
27
  private
30
28
 
31
- def eval_method
32
- EVAL_METHODS[@variable.count('@')]
33
- end
34
-
35
29
  def get
36
- @on.send(eval_method, @variable.to_s)
30
+ case @target
31
+ when /^@@/ # class variable
32
+ @on.class_eval @target.to_s
33
+ else # constant, instance or global variable
34
+ @on.instance_eval @target.to_s
35
+ end
37
36
  end
38
37
 
39
38
  def set(value)
40
- @on.send(eval_method, "#{@variable} = value")
39
+ case @target
40
+ when /^::/
41
+ remove_const @target
42
+ eval "#{@target} = value"
43
+ when /^@@/ # class variable
44
+ @on.class_eval "#{@target} = value"
45
+ else # instance or global variable
46
+ @on.instance_eval "#{@target} = value"
47
+ end
48
+ end
49
+
50
+ # Remove constant without warning
51
+ def remove_const(const)
52
+ namespace, _, name = const.rpartition('::')
53
+ receiver = namespace == '' ? Object : Object.const_get(namespace)
54
+ receiver.send(:remove_const, name)
41
55
  end
42
56
 
43
57
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Minitest
4
4
  module Substitute
5
- VERSION = '0.2.1'
5
+ VERSION = '1.0.0'
6
6
  end
7
7
  end
@@ -3,7 +3,7 @@
3
3
  require_relative 'substitute/version'
4
4
 
5
5
  require_relative 'substitute/substitutor'
6
- require_relative 'substitute/with'
6
+ require_relative 'substitute/substitute'
7
7
  require_relative 'substitute/spec'
8
8
 
9
- include Minitest::Substitute::With
9
+ include Minitest::Substitute::Substitute
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-substitute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Schwyn
@@ -10,26 +10,24 @@ bindir: bin
10
10
  cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
- MIIDODCCAiCgAwIBAgIBATANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhydWJ5
14
- L0RDPWJpdGNldGVyYS9EQz1jb20wHhcNMjIxMTA2MTIzNjUwWhcNMjMxMTA2MTIz
15
- NjUwWjAjMSEwHwYDVQQDDBhydWJ5L0RDPWJpdGNldGVyYS9EQz1jb20wggEiMA0G
13
+ MIIC+jCCAeKgAwIBAgIBAjANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhydWJ5
14
+ L0RDPWJpdGNldGVyYS9EQz1jb20wHhcNMjMxMTEwMTgyMzM2WhcNMjQxMTA5MTgy
15
+ MzM2WjAjMSEwHwYDVQQDDBhydWJ5L0RDPWJpdGNldGVyYS9EQz1jb20wggEiMA0G
16
16
  CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDcLg+IHjXYaUlTSU7R235lQKD8ZhEe
17
17
  KMhoGlSUonZ/zo1OT3KXcqTCP1iMX743xYs6upEGALCWWwq+nxvlDdnWRjF3AAv7
18
18
  ikC+Z2BEowjyeCCT/0gvn4ohKcR0JOzzRaIlFUVInlGSAHx2QHZ2N8ntf54lu7nd
19
19
  L8CiDK8rClsY4JBNGOgH9UC81f+m61UUQuTLxyM2CXfAYkj/sGNTvFRJcNX+nfdC
20
20
  hM9r2kH1+7wsa8yG7wJ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+10Zb1iu3aDk8sd0uMgukCY8
28
+ TUmlP5A6NeGdeDJIoLgromAKs+nvI7TWzhQq9ODs51XhxgUFRCvBqUTpjTQigw==
31
29
  -----END CERTIFICATE-----
32
- date: 2023-09-06 00:00:00.000000000 Z
30
+ date: 2024-03-07 00:00:00.000000000 Z
33
31
  dependencies:
34
32
  - !ruby/object:Gem::Dependency
35
33
  name: minitest
@@ -176,9 +174,9 @@ files:
176
174
  - README.md
177
175
  - lib/minitest/substitute.rb
178
176
  - lib/minitest/substitute/spec.rb
177
+ - lib/minitest/substitute/substitute.rb
179
178
  - lib/minitest/substitute/substitutor.rb
180
179
  - lib/minitest/substitute/version.rb
181
- - lib/minitest/substitute/with.rb
182
180
  homepage: https://github.com/svoop/minitest-substitute
183
181
  licenses:
184
182
  - MIT
@@ -188,7 +186,9 @@ metadata:
188
186
  source_code_uri: https://github.com/svoop/minitest-substitute
189
187
  documentation_uri: https://www.rubydoc.info/gems/minitest-substitute
190
188
  bug_tracker_uri: https://github.com/svoop/minitest-substitute/issues
191
- post_install_message:
189
+ post_install_message: '⚠️ Breaking change: minitest-substitute >= 1.0.0 switched from
190
+ "with" to "substitute" to prevent conflicts with Rails 7. Please read the update
191
+ section in the README.'
192
192
  rdoc_options:
193
193
  - "--title"
194
194
  - Minitest::Substitute
@@ -210,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
210
  - !ruby/object:Gem::Version
211
211
  version: '0'
212
212
  requirements: []
213
- rubygems_version: 3.4.19
213
+ rubygems_version: 3.5.6
214
214
  signing_key:
215
215
  specification_version: 4
216
216
  summary: Substitute values for the duration of a block or a group of tests
metadata.gz.sig CHANGED
Binary file