minitest-substitute 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 82dcc1be480cea4b024afc50aa111065cb8f6762847d40db97e2eae37823d43e
4
+ data.tar.gz: f9b44bae2d05d0552cbfdc944531ceb71834ee1e017eea62df34b8d5cd952e9e
5
+ SHA512:
6
+ metadata.gz: ea455efb4e176abb0441e9fe57306628f0b5c232fda56ce4b152152127a902ddf0e75cd1a6a40513e610e14dee5d00cb8b187534fbe2ff5cef609be16e1c2803
7
+ data.tar.gz: 52ffbfd39b06e813c4f7a44fd4dade18ab7c2e1728a0b0993971bb5d51c4607ac1ca29321a7e308de2244732efc33598e7be61e5b4afc38c06a6ae7fdd9a9ba4
checksums.yaml.gz.sig ADDED
@@ -0,0 +1,4 @@
1
+ ��Z79{��XP�/�/�@/+ԥ����d�胱��>�C�/1Wqv�J_%wL̔�?}wR��F[�N7�+��\v���Z5�����Ͻ�&_Ls
2
+ ߜa���Fn�&�<�7��?����rޛ���l�H
3
+ vU�u�XE�{�HU3#���_R��#���:���Q�z#H��Z�Y�\�&���f�{!�8�!��a�:i��ᣯZŭ�}�@�
4
+ *��v5�΁[R��^�Q�e�|zXi��% �Mu��‡qzg�
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## Main
2
+
3
+ Nothing so far
4
+
5
+ ## 0.1.0
6
+
7
+ #### Initial Implementation
8
+ * Require Ruby 3.0
9
+ * Substitution of instance variables
10
+ * Substitution of global variables
11
+ * Helper for Minitest spec notation
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sven Schwyn
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ [![Version](https://img.shields.io/gem/v/minitest-substitute.svg?style=flat)](https://rubygems.org/gems/minitest-substitute)
2
+ [![Tests](https://img.shields.io/github/actions/workflow/status/svoop/minitest-substitute/test.yml?style=flat&label=tests)](https://github.com/svoop/minitest-substitute/actions?workflow=Test)
3
+ [![Code Climate](https://img.shields.io/codeclimate/maintainability/svoop/minitest-substitute.svg?style=flat)](https://codeclimate.com/github/svoop/minitest-substitute/)
4
+ [![Donorbox](https://img.shields.io/badge/donate-on_donorbox-yellow.svg)](https://donorbox.org/bitcetera)
5
+
6
+ # Minitest::Substitute
7
+
8
+ Simple Minitest helper to replace values such as an instance variable of an object or an environment variable for the duration of a block or a group of tests.
9
+
10
+ This comes in very handy when you have to derive from default configuration in order to test some aspects of your code.
11
+
12
+ * [Homepage](https://github.com/svoop/minitest-substitute)
13
+ * [API](https://www.rubydoc.info/gems/minitest-substitute)
14
+ * Author: [Sven Schwyn - Bitcetera](https://bitcetera.com)
15
+
16
+ ## Install
17
+
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
+
20
+ ```
21
+ gem cert --add <(curl -Ls https://raw.github.com/svoop/minitest-substitute/main/certs/svoop.pem)
22
+ ```
23
+
24
+ Add the following to the <tt>Gemfile</tt> or <tt>gems.rb</tt> of your [Bundler](https://bundler.io) powered Ruby project:
25
+
26
+ ```ruby
27
+ gem 'minitest-substitute'
28
+ ```
29
+
30
+ And then install the bundle:
31
+
32
+ ```
33
+ bundle install --trust-policy MediumSecurity
34
+ ```
35
+
36
+ Finally, require this gem in your `test_helper.rb` or `spec_helper.rb`:
37
+
38
+ ```ruby
39
+ require 'minitest/substitute'
40
+ ```
41
+
42
+ ## Usage
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
+ ### Block
52
+
53
+ To substitute the value of an instance variable for the duration of a block:
54
+
55
+ ```ruby
56
+ class Config
57
+ def initialize
58
+ @version = 1
59
+ end
60
+ end
61
+
62
+ Config.instance_variable_get('@version') # => 1
63
+ with '@version', 2, on: Config do
64
+ Config.instance_variable_get('@version') # => 2
65
+ end
66
+ Config.instance_variable_get('@version') # => 1
67
+ ```
68
+
69
+ Same goes for global variables:
70
+
71
+ ```ruby
72
+ $verbose = false # => false
73
+ with '$verbose', true do
74
+ $verbose # => true
75
+ end
76
+ $verbose # => false
77
+ ```
78
+
79
+ And it works for hashes as well which comes in handy when you have to temporarily override the value of an environment variable:
80
+
81
+ ```ruby
82
+ ENV['EDITOR'] # => 'vi'
83
+ with "ENV['EDITOR']", 'nano' do
84
+ ENV['EDITOR'] # => 'nano'
85
+ end
86
+ ENV['EDITOR'] # => 'vi'
87
+ ```
88
+
89
+ It's safe to nest multiple `with` statements.
90
+
91
+ ### Group of Tests
92
+
93
+ When using spec notation, you can change a value for all tests within a `describe` group:
94
+
95
+ ```ruby
96
+ class Config
97
+ def initialize
98
+ @version = 1
99
+ end
100
+ end
101
+
102
+ describe Config do
103
+ subject do
104
+ Config.new
105
+ end
106
+
107
+ describe 'original version' do
108
+ it "returns the original version" do
109
+ _(subject.instance_variable_get('@version')).must_equal 1
110
+ end
111
+ end
112
+
113
+ describe 'sustituted version' do
114
+ with '@version', 2, on: Config
115
+
116
+ it "returns the substituted version" do
117
+ _(subject.instance_variable_get('@version')).must_equal 2
118
+ end
119
+ end
120
+ end
121
+ ```
122
+
123
+ For consistency with other Minitest helpers, the following alternative does exactly the same:
124
+
125
+ ```ruby
126
+ with '@version', on: Config do
127
+ 2
128
+ end
129
+ ```
130
+
131
+ It's safe to use multiple `with` statements within one `describe` block.
132
+
133
+ (The spec integration is borrowed from [minitest-around](https://rubygems.org/gems/minitest-around) for elegance and compatibility.)
134
+
135
+ ## Development
136
+
137
+ To install the development dependencies and then run the test suite:
138
+
139
+ ```
140
+ bundle install
141
+ bundle exec rake # run tests once
142
+ bundle exec guard # run tests whenever files are modified
143
+ ```
144
+
145
+ You're welcome to [submit issues](https://github.com/svoop/minitest-substitute/issues) and contribute code by [forking the project and submitting pull requests](https://docs.github.com/en/get-started/quickstart/fork-a-repo).
146
+
147
+ ## License
148
+
149
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/spec'
4
+
5
+ Minitest::Spec::DSL.class_eval do
6
+ class Substitutor
7
+ include Minitest::Substitute::With
8
+ end
9
+
10
+ # Substitute the variable value for the duration of the current description
11
+ #
12
+ # @param variable [String] instance or global variable name
13
+ # @param substitute [Object] temporary substitution value
14
+ # @param on [Object, nil] substitute in the context of this object
15
+ # @yield temporary substitution value (takes precedence over +substitute+ param)
16
+ def with(variable, substitute=nil, on: self)
17
+ before do
18
+ substitute = yield if block_given?
19
+ Substitutor.send(:commit_substitution, variable, substitute, on: on)
20
+ end
21
+ after do
22
+ Substitutor.send(:rollback_substitution, variable, on: on)
23
+ end
24
+ end
25
+
26
+ # Minitest does not support multiple before/after blocks
27
+ remove_method :before
28
+ def before(_type=nil, &block)
29
+ include(Module.new do
30
+ define_method(:setup) do
31
+ super()
32
+ instance_eval &block
33
+ end
34
+ end) # .then &:include
35
+ end
36
+
37
+ remove_method :after
38
+ def after(_type=nil, &block)
39
+ include(Module.new do
40
+ define_method(:teardown) do
41
+ instance_eval &block
42
+ ensure
43
+ super()
44
+ end
45
+ end) #.then &:include
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minitest
4
+ module Substitute
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minitest
4
+ module Substitute
5
+ module With
6
+ @original = {}
7
+ class << self
8
+ attr_accessor :original
9
+ end
10
+
11
+ # Substitute the variable value for the duration of the given block
12
+ #
13
+ # @param variable [String] instance or global variable name
14
+ # @param substitute [Object] temporary substitution value
15
+ # @param on [Object, nil] substitute in the context of this object
16
+ # @yield block during which the substitution is made
17
+ # @return [Object] return value of the yielded block
18
+ def with(variable, substitute, on: self)
19
+ commit_substitution(variable, substitute, on: on)
20
+ yield.tap do
21
+ rollback_substitution(variable, on: on)
22
+ end
23
+ end
24
+
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
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'substitute/version'
4
+
5
+ require_relative 'substitute/with'
6
+ require_relative 'substitute/spec'
7
+
8
+ include Minitest::Substitute::With
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,212 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-substitute
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sven Schwyn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDODCCAiCgAwIBAgIBATANBgkqhkiG9w0BAQsFADAjMSEwHwYDVQQDDBhydWJ5
14
+ L0RDPWJpdGNldGVyYS9EQz1jb20wHhcNMjIxMTA2MTIzNjUwWhcNMjMxMTA2MTIz
15
+ NjUwWjAjMSEwHwYDVQQDDBhydWJ5L0RDPWJpdGNldGVyYS9EQz1jb20wggEiMA0G
16
+ CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDcLg+IHjXYaUlTSU7R235lQKD8ZhEe
17
+ KMhoGlSUonZ/zo1OT3KXcqTCP1iMX743xYs6upEGALCWWwq+nxvlDdnWRjF3AAv7
18
+ ikC+Z2BEowjyeCCT/0gvn4ohKcR0JOzzRaIlFUVInlGSAHx2QHZ2N8ntf54lu7nd
19
+ L8CiDK8rClsY4JBNGOgH9UC81f+m61UUQuTLxyM2CXfAYkj/sGNTvFRJcNX+nfdC
20
+ hM9r2kH1+7wsa8yG7wJ2IkrzNACD8v84oE6qVusN8OLEMUI/NaEPVPbw2LUM149H
21
+ PVa0i729A4IhroNnFNmw4wOC93ARNbM1+LW36PLMmKjKudf5Exg8VmDVAgMBAAGj
22
+ dzB1MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQWBBSfK8MtR62mQ6oN
23
+ yoX/VKJzFjLSVDAdBgNVHREEFjAUgRJydWJ5QGJpdGNldGVyYS5jb20wHQYDVR0S
24
+ BBYwFIEScnVieUBiaXRjZXRlcmEuY29tMA0GCSqGSIb3DQEBCwUAA4IBAQAYG2na
25
+ ye8OE2DANQIFM/xDos/E4DaPWCJjX5xvFKNKHMCeQYPeZvLICCwyw2paE7Otwk6p
26
+ uvbg2Ks5ykXsbk5i6vxDoeeOLvmxCqI6m+tHb8v7VZtmwRJm8so0eSX0WvTaKnIf
27
+ CAn1bVUggczVdNoBXw9WAILKyw9bvh3Ft740XZrR74sd+m2pGwjCaM8hzLvrVbGP
28
+ DyYhlBeRWyQKQ0WDIsiTSRhzK8HwSTUWjvPwx7SEdIU/HZgyrk0ETObKPakVu6bH
29
+ kAyiRqgxF4dJviwtqI7mZIomWL63+kXLgjOjMe1SHxfIPo/0ji6+r1p4KYa7o41v
30
+ fwIwU1MKlFBdsjkd
31
+ -----END CERTIFICATE-----
32
+ date: 2023-08-23 00:00:00.000000000 Z
33
+ dependencies:
34
+ - !ruby/object:Gem::Dependency
35
+ name: minitest
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5'
48
+ - !ruby/object:Gem::Dependency
49
+ name: debug
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ - !ruby/object:Gem::Dependency
77
+ name: minitest
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ - !ruby/object:Gem::Dependency
91
+ name: minitest-flash
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ - !ruby/object:Gem::Dependency
105
+ name: minitest-focus
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ - !ruby/object:Gem::Dependency
119
+ name: guard
120
+ requirement: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ - !ruby/object:Gem::Dependency
133
+ name: guard-minitest
134
+ requirement: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ type: :development
140
+ prerelease: false
141
+ version_requirements: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ - !ruby/object:Gem::Dependency
147
+ name: yard
148
+ requirement: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ type: :development
154
+ prerelease: false
155
+ version_requirements: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ description: ''
161
+ email:
162
+ - ruby@bitcetera.com
163
+ executables: []
164
+ extensions: []
165
+ extra_rdoc_files:
166
+ - README.md
167
+ - CHANGELOG.md
168
+ - LICENSE.txt
169
+ files:
170
+ - CHANGELOG.md
171
+ - LICENSE.txt
172
+ - README.md
173
+ - lib/minitest/substitute.rb
174
+ - lib/minitest/substitute/spec.rb
175
+ - lib/minitest/substitute/version.rb
176
+ - lib/minitest/substitute/with.rb
177
+ homepage: https://github.com/svoop/minitest-substitute
178
+ licenses:
179
+ - MIT
180
+ metadata:
181
+ homepage_uri: https://github.com/svoop/minitest-substitute
182
+ changelog_uri: https://github.com/svoop/minitest-substitute/blob/main/CHANGELOG.md
183
+ source_code_uri: https://github.com/svoop/minitest-substitute
184
+ documentation_uri: https://www.rubydoc.info/gems/minitest-substitute
185
+ bug_tracker_uri: https://github.com/svoop/minitest-substitute/issues
186
+ post_install_message:
187
+ rdoc_options:
188
+ - "--title"
189
+ - Minitest::Substitute
190
+ - "--main"
191
+ - README.md
192
+ - "--line-numbers"
193
+ - "--inline-source"
194
+ - "--quiet"
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: 3.0.0
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubygems_version: 3.4.19
209
+ signing_key:
210
+ specification_version: 4
211
+ summary: Substitute values for the duration of a block or a group of tests
212
+ test_files: []
metadata.gz.sig ADDED
Binary file