minitest-substitute 0.1.0 → 0.2.1

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: 82dcc1be480cea4b024afc50aa111065cb8f6762847d40db97e2eae37823d43e
4
- data.tar.gz: f9b44bae2d05d0552cbfdc944531ceb71834ee1e017eea62df34b8d5cd952e9e
3
+ metadata.gz: ada3a9ae885b2050d30579935751f6f6d6d3a668b299def79602098549897ff6
4
+ data.tar.gz: 2892e09363f655605a9c7f19da56cc40f2229d44cb332e4b583d50f193c18cef
5
5
  SHA512:
6
- metadata.gz: ea455efb4e176abb0441e9fe57306628f0b5c232fda56ce4b152152127a902ddf0e75cd1a6a40513e610e14dee5d00cb8b187534fbe2ff5cef609be16e1c2803
7
- data.tar.gz: 52ffbfd39b06e813c4f7a44fd4dade18ab7c2e1728a0b0993971bb5d51c4607ac1ca29321a7e308de2244732efc33598e7be61e5b4afc38c06a6ae7fdd9a9ba4
6
+ metadata.gz: 3222b3c80b2cf6aee7451d14f602b6aa154367a4ef95b873bc1917d2c76ab240df9183a2322c917507737059eb7a83b3d17f5952c8363f23874c04e4eaa81eda
7
+ data.tar.gz: d85e9ea4fb25448a7065c0d0c32a0aa28b00df47cc03495d72b2f8df8134c43fb7aadb920dda4c1b71792012a0bf048f1ac7f0e863bf55b4b30d4a6b780449bb
checksums.yaml.gz.sig CHANGED
@@ -1,4 +1,6 @@
1
- ��Z79{��XP�/�/�@/+ԥ����d�胱��>�C�/1Wqv�J_%wL̔�?}wR��F[�N7�+��\v���Z5�����Ͻ�&_Ls
2
- ߜa���Fn�&�<�7��?����rޛ���l�H
3
- vU�uXE�{�HU3#���_R��#���:���Q�z#H��Z�Y�\�&���f�{!�8�!��a�:i��ᣯZŭ�}�@�
4
- *��v5�΁[R��^�Qe�|zXi��% �Mu��‡qzg
1
+ �GY���5�ln��c��OM��2hf���_��
2
+ ��犲��@��TM�
3
+ Oi֢�u-����X��OB��E�����
4
+ nF(�%�P6����|޼I�O���R}>z�_�B�����z}+����N�u��Y��hC�=W׿
5
+ 8��V���i�?;Hj*�`�����^Qh3b�ғ^m�߽�UM��{� �='eYqX��U�,7
6
+ ���d%ݬܥ�@���B_�{�? �
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  Nothing so far
4
4
 
5
+ ## 0.2.1
6
+
7
+ Nothing so far
8
+
9
+ ## 0.2.0
10
+
11
+ #### Additions
12
+ * Refactor to support class variables as well
13
+ * Use declared subject as default target for with helper
14
+
5
15
  ## 0.1.0
6
16
 
7
17
  #### Initial Implementation
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.instance_variable_get('@version') # => 1
63
- with '@version', 2, on: Config do
64
- Config.instance_variable_get('@version') # => 2
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
65
60
  end
66
- Config.instance_variable_get('@version') # => 1
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
76
+ end
77
+ Config.class_variable_get('@@counter') # => 0
67
78
  ```
68
79
 
69
80
  Same goes for global variables:
@@ -120,14 +131,46 @@ describe Config do
120
131
  end
121
132
  ```
122
133
 
123
- For consistency with other Minitest helpers, the following alternative does exactly the same:
134
+ :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.
135
+
136
+ Alternatively, you can pass the substitution value as a block. This block will be evaluated once in the context of the test, in other words, you can use assignments done with `let` inside the block:
124
137
 
125
138
  ```ruby
126
- with '@version', on: Config do
127
- 2
139
+ class Config
140
+ def initialize
141
+ @version = 1
142
+ end
143
+ end
144
+
145
+ describe Config do
146
+ subject do
147
+ Config.new
148
+ end
149
+
150
+ let :version do
151
+ 2
152
+ end
153
+
154
+ describe 'original version' do
155
+ it "returns the original version" do
156
+ _(subject.instance_variable_get('@version')).must_equal 1
157
+ end
158
+ end
159
+
160
+ describe 'sustituted version' do
161
+ with '@version', on: Config do
162
+ version # set using "let" above
163
+ end
164
+
165
+ it "returns the substituted version" do
166
+ _(subject.instance_variable_get('@version')).must_equal 2
167
+ end
168
+ end
128
169
  end
129
170
  ```
130
171
 
172
+ If both a substitution value and a substitution block are present, the latter takes precedence.
173
+
131
174
  It's safe to use multiple `with` statements within one `describe` block.
132
175
 
133
176
  (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: self)
14
+ def with(variable, substitute=nil, on: :subject, &block)
15
+ substitutor = Minitest::Substitute::Substitutor.new(variable, on: on)
17
16
  before do
18
- substitute = yield if block_given?
19
- Substitutor.send(:commit_substitution, variable, substitute, on: on)
17
+ substitutor.substitute(block ? instance_eval(&block) : substitute)
18
+ substitutor.on = subject if on == :subject && respond_to?(:subject)
19
+ substitutor.commit
20
20
  end
21
21
  after do
22
- Substitutor.send(:rollback_substitution, variable, on: on)
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,45 @@
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, on:)
12
+ @variable, @on = variable, on
13
+ end
14
+
15
+ def substitute(value)
16
+ @substitute = value unless instance_variable_defined? :@substitute
17
+ self
18
+ end
19
+
20
+ def commit
21
+ @original = get
22
+ set @substitute
23
+ end
24
+
25
+ def rollback
26
+ set @original
27
+ end
28
+
29
+ private
30
+
31
+ def eval_method
32
+ EVAL_METHODS[@variable.count('@')]
33
+ end
34
+
35
+ def get
36
+ @on.send(eval_method, @variable.to_s)
37
+ end
38
+
39
+ def set(value)
40
+ @on.send(eval_method, "#{@variable} = value")
41
+ end
42
+
43
+ end
44
+ end
45
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Minitest
4
4
  module Substitute
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.1'
6
6
  end
7
7
  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
- commit_substitution(variable, substitute, on: on)
15
+ substitutor = Minitest::Substitute::Substitutor.new(variable, on: on).substitute(substitute)
16
+ substitutor.commit
20
17
  yield.tap do
21
- rollback_substitution(variable, on: on)
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
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'substitute/version'
4
4
 
5
+ require_relative 'substitute/substitutor'
5
6
  require_relative 'substitute/with'
6
7
  require_relative 'substitute/spec'
7
8
 
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.1.0
4
+ version: 0.2.1
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-23 00:00:00.000000000 Z
32
+ date: 2023-09-06 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