minitest-substitute 0.2.1 → 0.3.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: 5f27cada1056bf6ab2bc4d31d05eb6434d7434f196d59f43a449377d46e258df
4
+ data.tar.gz: 85aa4a49208eb50dd5f52826489ddb96b636896b56e3f2242990ab2e650f4075
5
5
  SHA512:
6
- metadata.gz: 3222b3c80b2cf6aee7451d14f602b6aa154367a4ef95b873bc1917d2c76ab240df9183a2322c917507737059eb7a83b3d17f5952c8363f23874c04e4eaa81eda
7
- data.tar.gz: d85e9ea4fb25448a7065c0d0c32a0aa28b00df47cc03495d72b2f8df8134c43fb7aadb920dda4c1b71792012a0bf048f1ac7f0e863bf55b4b30d4a6b780449bb
6
+ metadata.gz: 8564e7186a0967d7e8bf94b140c76640e8aea84c0d3e7144a1d312c05d7461c59a04b4450b9e279616be85adca22db87abcffbd88de5e743d5c0b6e560881191
7
+ data.tar.gz: 780c0a5bfd68c52e27fd5ce6786c18845ddb5d7e26bb4c1133dccf1f2189e306845900e3011744e9026b6e836147abe5b6fe9b64e1d6a10fea8d625b12ec9093
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGELOG.md CHANGED
@@ -2,9 +2,15 @@
2
2
 
3
3
  Nothing so far
4
4
 
5
+ ## 0.3.0
6
+
7
+ #### Additions
8
+ * Substition of constants
9
+
5
10
  ## 0.2.1
6
11
 
7
- Nothing so far
12
+ #### Changes
13
+ * Defer evaluation of the substitution value block for specs
8
14
 
9
15
  ## 0.2.0
10
16
 
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:
@@ -87,7 +87,7 @@ end
87
87
  $verbose # => false
88
88
  ```
89
89
 
90
- And it works for hashes as well which comes in handy when you have to temporarily override the value of an environment variable:
90
+ 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
91
 
92
92
  ```ruby
93
93
  ENV['EDITOR'] # => 'vi'
@@ -97,6 +97,43 @@ end
97
97
  ENV['EDITOR'] # => 'vi'
98
98
  ```
99
99
 
100
+ You can even substitute constants, however, you have to use their absolute name starting with `::`:
101
+
102
+ ```ruby
103
+ module Animals
104
+ DOG_MAKES = 'woof'
105
+ CAT_MAKES = 'meow'
106
+ end
107
+
108
+ Animals::DOG_MAKES # => 'woof'
109
+ with '::Animals::DOG_MAKES', Animals::CAT_MAKES do
110
+ Animals::DOG_MAKES # => 'meow'
111
+ end
112
+ Animals::DOG_MAKES # => 'woof'
113
+ ```
114
+
115
+ Remember that class declarations are assigned to constants as well:
116
+
117
+ ```ruby
118
+ class Dog
119
+ self.makes
120
+ 'woof'
121
+ end
122
+ end
123
+
124
+ class Cat
125
+ self.makes
126
+ 'meow'
127
+ end
128
+ end
129
+
130
+ Dog.makes # => 'woof'
131
+ with '::Dog', Cat do
132
+ Dog.makes # => 'meow'
133
+ end
134
+ Dog.makes # => 'woof'
135
+ ```
136
+
100
137
  It's safe to nest multiple `with` statements.
101
138
 
102
139
  ### Group of Tests
@@ -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 with(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
@@ -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 = '0.3.0'
6
6
  end
7
7
  end
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: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Schwyn
@@ -29,7 +29,7 @@ cert_chain:
29
29
  kAyiRqgxF4dJviwtqI7mZIomWL63+kXLgjOjMe1SHxfIPo/0ji6+r1p4KYa7o41v
30
30
  fwIwU1MKlFBdsjkd
31
31
  -----END CERTIFICATE-----
32
- date: 2023-09-06 00:00:00.000000000 Z
32
+ date: 2023-09-08 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: minitest
metadata.gz.sig CHANGED
Binary file