Class: RSpec::Mocks::ConstantMutator::DefinedConstantReplacer

Inherits:
BaseMutator show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb

Overview

Replaces a defined constant for the duration of an example.

Instance Attribute Summary

Attributes inherited from BaseMutator

#full_constant_name, #original_value

Instance Method Summary collapse

Methods inherited from BaseMutator

#idempotently_reset

Methods included from Support::RecursiveConstMethods

#const_defined_on?, #constants_defined_on, #get_const_defined_on, #normalize_const_name, #recursive_const_defined?, #recursive_const_get

Constructor Details

#initialize(*args) ⇒ DefinedConstantReplacer

Returns a new instance of DefinedConstantReplacer.



198
199
200
201
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb', line 198

def initialize(*args)
  super
  @constants_to_transfer = []
end

Instance Method Details

#mutateObject



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb', line 203

def mutate
  @context = recursive_const_get(@context_parts.join('::'))
  @original_value = get_const_defined_on(@context, @const_name)

  @constants_to_transfer = verify_constants_to_transfer!

  @context.__send__(:remove_const, @const_name)
  @context.const_set(@const_name, @mutated_value)

  transfer_nested_constants
end

#resetObject



223
224
225
226
227
228
229
230
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb', line 223

def reset
  @constants_to_transfer.each do |const|
    @mutated_value.__send__(:remove_const, const)
  end

  @context.__send__(:remove_const, @const_name)
  @context.const_set(@const_name, @original_value)
end

#should_transfer_nested_constants?Boolean

Returns:

  • (Boolean)


268
269
270
271
272
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb', line 268

def should_transfer_nested_constants?
  return true  if @transfer_nested_constants
  return false unless RSpec::Mocks.configuration.transfer_nested_constants?
  @original_value.respond_to?(:constants) && @mutated_value.respond_to?(:constants)
end

#to_constantObject



215
216
217
218
219
220
221
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb', line 215

def to_constant
  const = super
  const.stubbed = true
  const.previously_defined = true

  const
end

#transfer_nested_constantsObject



232
233
234
235
236
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb', line 232

def transfer_nested_constants
  @constants_to_transfer.each do |const|
    @mutated_value.const_set(const, get_const_defined_on(original_value, const))
  end
end

#verify_constants_to_transfer!Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/mutate_const.rb', line 238

def verify_constants_to_transfer!
  return [] unless should_transfer_nested_constants?

  { @original_value => "the original value", @mutated_value => "the stubbed value" }.each do |value, description|
    next if value.respond_to?(:constants)

    raise ArgumentError,
          "Cannot transfer nested constants for #{@full_constant_name} " \
          "since #{description} is not a class or module and only classes " \
          "and modules support nested constants."
  end

  if Array === @transfer_nested_constants
    @transfer_nested_constants = @transfer_nested_constants.map(&:to_s) if RUBY_VERSION == '1.8.7'
    undefined_constants = @transfer_nested_constants - constants_defined_on(@original_value)

    if undefined_constants.any?
      available_constants = constants_defined_on(@original_value) - @transfer_nested_constants
      raise ArgumentError,
            "Cannot transfer nested constant(s) #{undefined_constants.join(' and ')} " \
            "for #{@full_constant_name} since they are not defined. Did you mean " \
            "#{available_constants.join(' or ')}?"
    end

    @transfer_nested_constants
  else
    constants_defined_on(@original_value)
  end
end