123456789_123456789_123456789_123456789_123456789_

Class: Module

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/minitest/spec.rb

Instance Method Summary

Instance Method Details

#infect_an_assertion(meth, new_name, dont_flip = false)

[ GitHub ]

  
# File 'lib/minitest/spec.rb', line 4

def infect_an_assertion meth, new_name, dont_flip = false # :nodoc:
  block = dont_flip == :block
  dont_flip = false if block
  target_obj = block ? '_{obj.method}' : '_(obj)'

  # https://eregon.me/blog/2021/02/13/correct-delegation-in-ruby-2-27-3.html
  # Drop this when we can drop ruby 2.6 (aka after rails 6.1 EOL, ~2024-06)
  kw_extra = "ruby2_keywords %p" % [new_name] if respond_to?(:ruby2_keywords, true)

  # warn "%-22p -> %p %p" % [meth, new_name, dont_flip]
  self.class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{new_name} *args
      where = Minitest.filter_backtrace(caller).first
      where = where.split(/:in /, 2).first # clean up noise
      Kernel.warn "DEPRECATED: global use of #{new_name} from #\{where}. Use #{target_obj}.#{new_name} instead. This will fail in Minitest 6."
      Minitest::Expectation.new(self, Minitest::Spec.current).#{new_name}(*args)
    end
    #{kw_extra}
  EOM

  Minitest::Expectation.class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{new_name} *args
      raise "Calling ##{new_name} outside of test." unless ctx
      case
      when #{!!dont_flip} then
        ctx.#{meth}(target, *args)
      when #{block} && Proc === target then
        ctx.#{meth}(*args, &target)
      else
        ctx.#{meth}(args.first, target, *args[1..-1])
      end
    end
    #{kw_extra}
  EOM
end