2009-09-09 02:46:31 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require_relative 'envutil'
|
|
|
|
|
|
|
|
class TestAutoload < Test::Unit::TestCase
|
|
|
|
def test_autoload_so
|
|
|
|
# Continuation is always available, unless excluded intentionally.
|
|
|
|
assert_in_out_err([], <<-INPUT, [], [])
|
|
|
|
autoload :Continuation, "continuation"
|
|
|
|
begin Continuation; rescue LoadError; end
|
|
|
|
INPUT
|
|
|
|
end
|
2011-02-04 20:09:39 -05:00
|
|
|
|
|
|
|
def test_non_realpath_in_loadpath
|
|
|
|
require 'tmpdir'
|
|
|
|
tmpdir = Dir.mktmpdir('autoload')
|
|
|
|
tmpdirs = [tmpdir]
|
|
|
|
tmpdirs.unshift(tmpdir + '/foo')
|
|
|
|
Dir.mkdir(tmpdirs[0])
|
|
|
|
tmpfiles = [tmpdir + '/foo.rb', tmpdir + '/foo/bar.rb']
|
|
|
|
open(tmpfiles[0] , 'w') do |f|
|
|
|
|
f.puts <<-INPUT
|
|
|
|
$:.unshift(File.expand_path('..', __FILE__)+'/./foo')
|
|
|
|
module Foo
|
|
|
|
autoload :Bar, 'bar'
|
|
|
|
end
|
2011-02-05 08:48:45 -05:00
|
|
|
p Foo::Bar
|
2011-02-04 20:09:39 -05:00
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
open(tmpfiles[1], 'w') do |f|
|
|
|
|
f.puts 'class Foo::Bar; end'
|
|
|
|
end
|
2011-02-05 08:48:45 -05:00
|
|
|
assert_in_out_err([tmpfiles[0]], "", ["Foo::Bar"], [])
|
2011-02-04 20:09:39 -05:00
|
|
|
ensure
|
|
|
|
File.unlink(*tmpfiles) rescue nil if tmpfiles
|
|
|
|
tmpdirs.each {|dir| Dir.rmdir(dir)}
|
|
|
|
end
|
2011-04-14 08:23:32 -04:00
|
|
|
|
|
|
|
def test_autoload_p
|
|
|
|
bug4565 = '[ruby-core:35679]'
|
|
|
|
|
|
|
|
require 'tmpdir'
|
|
|
|
tmpdir = Dir.mktmpdir('autoload')
|
|
|
|
tmpfile = tmpdir + '/foo.rb'
|
|
|
|
a = Module.new do
|
|
|
|
autoload :X, tmpfile
|
|
|
|
end
|
|
|
|
b = Module.new do
|
|
|
|
include a
|
|
|
|
end
|
|
|
|
assert_equal(true, a.const_defined?(:X))
|
|
|
|
assert_equal(true, b.const_defined?(:X))
|
|
|
|
assert_equal(tmpfile, a.autoload?(:X), bug4565)
|
|
|
|
assert_equal(tmpfile, b.autoload?(:X), bug4565)
|
|
|
|
end
|
2009-09-09 02:46:31 -04:00
|
|
|
end
|