minitest-substitute 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - checksums.yaml.gz.sig +0 -0
 - data/CHANGELOG.md +7 -1
 - data/README.md +39 -2
 - data/lib/minitest/substitute/spec.rb +5 -5
 - data/lib/minitest/substitute/substitutor.rb +24 -10
 - data/lib/minitest/substitute/version.rb +1 -1
 - data.tar.gz.sig +0 -0
 - metadata +2 -2
 - 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: 5f27cada1056bf6ab2bc4d31d05eb6434d7434f196d59f43a449377d46e258df
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 85aa4a49208eb50dd5f52826489ddb96b636896b56e3f2242990ab2e650f4075
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 8564e7186a0967d7e8bf94b140c76640e8aea84c0d3e7144a1d312c05d7461c59a04b4450b9e279616be85adca22db87abcffbd88de5e743d5c0b6e560881191
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 780c0a5bfd68c52e27fd5ce6786c18845ddb5d7e26bb4c1133dccf1f2189e306845900e3011744e9026b6e836147abe5b6fe9b64e1d6a10fea8d625b12ec9093
         
     | 
    
        checksums.yaml.gz.sig
    CHANGED
    
    | 
         Binary file 
     | 
    
        data/CHANGELOG.md
    CHANGED
    
    
    
        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:// 
     | 
| 
      
 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  
     | 
| 
      
 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  
     | 
| 
      
 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:  
     | 
| 
      
 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  
     | 
| 
      
 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 
     | 
| 
      
 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 
     | 
| 
      
 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( 
     | 
| 
       12 
     | 
    
         
            -
                    @ 
     | 
| 
      
 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 
     | 
    
         
            -
                     
     | 
| 
      
 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 
     | 
    
         
            -
                     
     | 
| 
      
 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
         
     | 
    
        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.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- 
     | 
| 
      
 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 
     |