class Object

Object extensions for Minitest::Mock.

Constants

Int

helps to deal with 2.4 deprecation of Fixnum for Integer

STUB6
SomeError

Public Instance Methods

_count() click to toggle source
# File minitest-5.13.0/test/minitest/test_minitest_spec.rb, line 654
def _count
  $let_count ||= 0
end
activate_bundler(bundler_version) click to toggle source
# File rake-13.0.1/bin/bundle, line 77
def activate_bundler(bundler_version)
  if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0")
    bundler_version = "< 2"
  end
  gem_error = activation_error_handling do
    gem "bundler", bundler_version
  end
  return if gem_error.nil?
  require_error = activation_error_handling do
    require "bundler/version"
  end
  return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION))
  warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`"
  exit 42
end
activation_error_handling() { || ... } click to toggle source
# File rake-13.0.1/bin/bundle, line 93
def activation_error_handling
  yield
  nil
rescue StandardError, LoadError => e
  e
end
assert_triggered(expected = "blah", klass = Minitest::Assertion) { || ... } click to toggle source

do not parallelize this suite… it just can“t handle it.

# File minitest-5.13.0/test/minitest/test_minitest_spec.rb, line 20
def assert_triggered expected = "blah", klass = Minitest::Assertion
  @assertion_count += 1

  e = assert_raises(klass) do
    yield
  end

  msg = e.message.sub(/(---Backtrace---).*/m, '\1')
  msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
  msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
  msg.gsub!(/:0x[Xa-fA-F0-9]{4,}[ @].+?>/, ":0xXXXXXX@PATH>")

  if expected
    @assertion_count += 1
    case expected
    when String then
      assert_equal expected, msg
    when Regexp then
      @assertion_count += 1
      assert_match expected, msg
    else
      flunk "Unknown: #{expected.inspect}"
    end
  end
end
bundler_version() click to toggle source
# File rake-13.0.1/bin/bundle, line 63
def bundler_version
  @bundler_version ||= begin
    env_var_version || cli_arg_version ||
      lockfile_version || "#{Gem::Requirement.default}.a"
  end
end
cli_arg_version() click to toggle source
# File rake-13.0.1/bin/bundle, line 24
def cli_arg_version
  return unless invoked_as_script? # don't want to hijack other binstubs
  return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
  bundler_version = nil
  update_index = nil
  ARGV.each_with_index do |a, i|
    if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
      bundler_version = a
    end
    next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
    bundler_version = $1 || ">= 0.a"
    update_index = i
  end
  bundler_version
end
env_var_version() click to toggle source
# File rake-13.0.1/bin/bundle, line 20
def env_var_version
  ENV["BUNDLER_VERSION"]
end
gemfile() click to toggle source
# File rake-13.0.1/bin/bundle, line 40
def gemfile
  gemfile = ENV["BUNDLE_GEMFILE"]
  return gemfile if gemfile && !gemfile.empty?

  File.expand_path("../../Gemfile", __FILE__)
end
invoked_as_script?() click to toggle source
# File rake-13.0.1/bin/bundle, line 16
def invoked_as_script?
  File.expand_path($0) == File.expand_path(__FILE__)
end
load_bundler!() click to toggle source
# File rake-13.0.1/bin/bundle, line 70
def load_bundler!
  ENV["BUNDLE_GEMFILE"] ||= gemfile

  # must dup string for RG < 1.8 compatibility
  activate_bundler(bundler_version.dup)
end
lockfile() click to toggle source
# File rake-13.0.1/bin/bundle, line 47
def lockfile
  lockfile =
    case File.basename(gemfile)
    when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
    else "#{gemfile}.lock"
    end
  File.expand_path(lockfile)
end
lockfile_version() click to toggle source
# File rake-13.0.1/bin/bundle, line 56
def lockfile_version
  return unless File.file?(lockfile)
  lockfile_contents = File.read(lockfile)
  return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
  Regexp.last_match(1)
end
run_test() click to toggle source

experimental. It is for “ruby -rtest-unit -e run_test test/test_*.rb”. Is this API OK or dirty?

# File test-unit-3.3.4/lib/test-unit.rb, line 30
def run_test
  self.class.send(:undef_method, :run_test)
  require "test/unit"
end
stub(name, val_or_callable, *block_args) { |self| ... } click to toggle source

Add a temporary stubbed method replacing name for the duration of the block. If val_or_callable responds to call, then it returns the result of calling it, otherwise returns the value as-is. If stubbed method yields a block, block_args will be passed along. Cleans up the stub at the end of the block. The method name must exist before stubbing.

def test_stale_eh
  obj_under_test = Something.new
  refute obj_under_test.stale?

  Time.stub :now, Time.at(0) do
    assert obj_under_test.stale?
  end
end
Calls superclass method
# File minitest-5.13.0/lib/minitest/mock.rb, line 212
def stub name, val_or_callable, *block_args
  new_name = "__minitest_stub__#{name}"

  metaclass = class << self; self; end

  if respond_to? name and not methods.map(&:to_s).include? name.to_s then
    metaclass.send :define_method, name do |*args|
      super(*args)
    end
  end

  metaclass.send :alias_method, new_name, name

  metaclass.send :define_method, name do |*args, &blk|
    if val_or_callable.respond_to? :call then
      val_or_callable.call(*args, &blk)
    else
      blk.call(*block_args) if blk
      val_or_callable
    end
  end

  yield self
ensure
  metaclass.send :undef_method, name
  metaclass.send :alias_method, name, new_name
  metaclass.send :undef_method, new_name
end
Also aliased as: stub5, stub6
stub5(name, val_or_callable, *block_args)
Alias for: stub
stub6(name, val_or_callable, *block_args)
Alias for: stub