minitest-substitute 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +6 -0
- data/README.md +24 -11
- data/lib/minitest/substitute/spec.rb +9 -8
- data/lib/minitest/substitute/substitutor.rb +40 -0
- data/lib/minitest/substitute/version.rb +1 -1
- data/lib/minitest/substitute/with.rb +3 -16
- data/lib/minitest/substitute.rb +1 -0
- data.tar.gz.sig +0 -0
- metadata +8 -3
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 288e4fc7e4e20c4b649237471762f8723ce333f2886d4244048033ea281c4ab2
|
4
|
+
data.tar.gz: ee23d391142761ea6be22ecb306944313e612fc697c4849ab4f70c7a6ce8e844
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d2abd17135f9a750c2e7efb89e1fbc4d2b1ce30a7767e60ad368715059de575435e36856128e5e911c495cfc407169ef68baf9176721ee11eb28f55197fb13f
|
7
|
+
data.tar.gz: ba8d78a68880474bd1051887ad5435e0efdda86aa78c643ddad004bdf606e725ee688385da114abdc4dfb110ad5eb42e5c1a7de146de380e49372296ae6ec9de
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -41,13 +41,6 @@ require 'minitest/substitute'
|
|
41
41
|
|
42
42
|
## Usage
|
43
43
|
|
44
|
-
This lightweight gem implements features on a "as needed" basis, as of now:
|
45
|
-
|
46
|
-
* substitution of instance variables
|
47
|
-
* substitution of global variables
|
48
|
-
|
49
|
-
Please [create an issue describing your use case](https://github.com/svoop/minitest-substitute/issues) in case you need more features such as the substitution of class variables or substitution via accessor methods.
|
50
|
-
|
51
44
|
### Block
|
52
45
|
|
53
46
|
To substitute the value of an instance variable for the duration of a block:
|
@@ -59,11 +52,29 @@ class Config
|
|
59
52
|
end
|
60
53
|
end
|
61
54
|
|
62
|
-
Config.
|
63
|
-
|
64
|
-
|
55
|
+
config = Config.new
|
56
|
+
|
57
|
+
config.instance_variable_get('@version') # => 1
|
58
|
+
with '@version', 2, on: config do
|
59
|
+
config.instance_variable_get('@version') # => 2
|
60
|
+
end
|
61
|
+
config.instance_variable_get('@version') # => 1
|
62
|
+
```
|
63
|
+
|
64
|
+
:warning: The target `on` is set explicitly in this case. If you omit this argument, `self` will be used as target by default.
|
65
|
+
|
66
|
+
Class variables can be substituted as well:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class Config
|
70
|
+
@@counter = 0
|
71
|
+
end
|
72
|
+
|
73
|
+
Config.class_variable_get('@@counter') # => 0
|
74
|
+
with '@@counter', 42, on: Config do
|
75
|
+
Config.class_variable_get('@@counter') # => 42
|
65
76
|
end
|
66
|
-
Config.
|
77
|
+
Config.class_variable_get('@@counter') # => 0
|
67
78
|
```
|
68
79
|
|
69
80
|
Same goes for global variables:
|
@@ -128,6 +139,8 @@ with '@version', on: Config do
|
|
128
139
|
end
|
129
140
|
```
|
130
141
|
|
142
|
+
:warning: The target `on` is set explicitly in this case. If you omit this argument, `:subject` will be used as target by default which refers to the subject defined by the `subject {}` helper.
|
143
|
+
|
131
144
|
It's safe to use multiple `with` statements within one `describe` block.
|
132
145
|
|
133
146
|
(The spec integration is borrowed from [minitest-around](https://rubygems.org/gems/minitest-around) for elegance and compatibility.)
|
@@ -3,23 +3,23 @@
|
|
3
3
|
require 'minitest/spec'
|
4
4
|
|
5
5
|
Minitest::Spec::DSL.class_eval do
|
6
|
-
class Substitutor
|
7
|
-
include Minitest::Substitute::With
|
8
|
-
end
|
9
6
|
|
10
7
|
# Substitute the variable value for the duration of the current description
|
11
8
|
#
|
12
9
|
# @param variable [String] instance or global variable name
|
13
10
|
# @param substitute [Object] temporary substitution value
|
14
|
-
# @param on [Object, nil] substitute in the context of this object
|
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
|
15
13
|
# @yield temporary substitution value (takes precedence over +substitute+ param)
|
16
|
-
def with(variable, substitute=nil, on:
|
14
|
+
def with(variable, substitute=nil, on: :subject)
|
15
|
+
substitute = yield if block_given?
|
16
|
+
substitutor = Minitest::Substitute::Substitutor.new(variable, substitute, on: on)
|
17
17
|
before do
|
18
|
-
|
19
|
-
|
18
|
+
substitutor.on = subject if on == :subject && respond_to?(:subject)
|
19
|
+
substitutor.commit
|
20
20
|
end
|
21
21
|
after do
|
22
|
-
|
22
|
+
substitutor.rollback
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -44,4 +44,5 @@ Minitest::Spec::DSL.class_eval do
|
|
44
44
|
end
|
45
45
|
end) #.then &:include
|
46
46
|
end
|
47
|
+
|
47
48
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Minitest
|
4
|
+
module Substitute
|
5
|
+
class Substitutor
|
6
|
+
|
7
|
+
EVAL_METHODS = [:instance_eval, :instance_eval, :class_eval].freeze
|
8
|
+
|
9
|
+
attr_writer :on
|
10
|
+
|
11
|
+
def initialize(variable, substitute, on:)
|
12
|
+
@variable, @substitute, @on = variable, substitute, on
|
13
|
+
end
|
14
|
+
|
15
|
+
def commit
|
16
|
+
@original = get
|
17
|
+
set @substitute
|
18
|
+
end
|
19
|
+
|
20
|
+
def rollback
|
21
|
+
set @original
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def eval_method
|
27
|
+
EVAL_METHODS[@variable.count('@')]
|
28
|
+
end
|
29
|
+
|
30
|
+
def get
|
31
|
+
@on.send(eval_method, @variable.to_s)
|
32
|
+
end
|
33
|
+
|
34
|
+
def set(value)
|
35
|
+
@on.send(eval_method, "#{@variable} = value")
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -3,10 +3,6 @@
|
|
3
3
|
module Minitest
|
4
4
|
module Substitute
|
5
5
|
module With
|
6
|
-
@original = {}
|
7
|
-
class << self
|
8
|
-
attr_accessor :original
|
9
|
-
end
|
10
6
|
|
11
7
|
# Substitute the variable value for the duration of the given block
|
12
8
|
#
|
@@ -16,22 +12,13 @@ module Minitest
|
|
16
12
|
# @yield block during which the substitution is made
|
17
13
|
# @return [Object] return value of the yielded block
|
18
14
|
def with(variable, substitute, on: self)
|
19
|
-
|
15
|
+
substitutor = Minitest::Substitute::Substitutor.new(variable, substitute, on: on)
|
16
|
+
substitutor.commit
|
20
17
|
yield.tap do
|
21
|
-
|
18
|
+
substitutor.rollback
|
22
19
|
end
|
23
20
|
end
|
24
21
|
|
25
|
-
private
|
26
|
-
|
27
|
-
def commit_substitution(variable, substitute, on:)
|
28
|
-
Minitest::Substitute::With.original[variable.hash] = on.instance_eval variable.to_s
|
29
|
-
on.instance_eval "#{variable} = substitute"
|
30
|
-
end
|
31
|
-
|
32
|
-
def rollback_substitution(variable, on:)
|
33
|
-
on.instance_eval "#{variable} = Minitest::Substitute::With.original.delete(variable.hash)"
|
34
|
-
end
|
35
22
|
end
|
36
23
|
end
|
37
24
|
end
|
data/lib/minitest/substitute.rb
CHANGED
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.
|
4
|
+
version: 0.2.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-08-
|
32
|
+
date: 2023-08-25 00:00:00.000000000 Z
|
33
33
|
dependencies:
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: minitest
|
@@ -157,7 +157,11 @@ dependencies:
|
|
157
157
|
- - ">="
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
|
-
description:
|
160
|
+
description: |
|
161
|
+
Simple Minitest helper to replace values such as an instance variable of an
|
162
|
+
object or an environment variable for the duration of a block or a group of
|
163
|
+
tests. This comes in very handy when you have to derive from default
|
164
|
+
configuration in order to test some aspects of your code.
|
161
165
|
email:
|
162
166
|
- ruby@bitcetera.com
|
163
167
|
executables: []
|
@@ -172,6 +176,7 @@ files:
|
|
172
176
|
- README.md
|
173
177
|
- lib/minitest/substitute.rb
|
174
178
|
- lib/minitest/substitute/spec.rb
|
179
|
+
- lib/minitest/substitute/substitutor.rb
|
175
180
|
- lib/minitest/substitute/version.rb
|
176
181
|
- lib/minitest/substitute/with.rb
|
177
182
|
homepage: https://github.com/svoop/minitest-substitute
|
metadata.gz.sig
CHANGED
Binary file
|