2004-01-06 05:34:24 +03:00
|
|
|
module InlineTest
|
2004-01-14 11:54:21 +03:00
|
|
|
def eval_part(libname, sep, part)
|
|
|
|
path = libpath(libname)
|
|
|
|
program = File.open(path) { |f| f.read }
|
|
|
|
mainpart, endpart = program.split(sep)
|
|
|
|
if endpart.nil?
|
2006-12-31 18:02:22 +03:00
|
|
|
raise RuntimeError.new("No #{part} part in the library '#{path}'")
|
2004-01-14 11:54:21 +03:00
|
|
|
end
|
2004-01-14 15:13:11 +03:00
|
|
|
eval(endpart, TOPLEVEL_BINDING, path, mainpart.count("\n")+1)
|
2004-01-14 11:54:21 +03:00
|
|
|
end
|
|
|
|
module_function :eval_part
|
|
|
|
|
2004-01-06 05:34:24 +03:00
|
|
|
def loadtest(libname)
|
2004-05-19 13:00:48 +04:00
|
|
|
require(libname)
|
2004-01-06 05:34:24 +03:00
|
|
|
in_critical do
|
|
|
|
in_progname(libpath(libname)) do
|
2004-01-14 11:54:21 +03:00
|
|
|
eval_part(libname, /^(?=if\s+(?:\$0\s*==\s*__FILE__|__FILE__\s*==\s*\$0)(?:[\#\s]|$))/, '$0 == __FILE__')
|
2004-01-06 05:34:24 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :loadtest
|
|
|
|
|
|
|
|
def loadtest__END__part(libname)
|
2004-05-19 13:00:48 +04:00
|
|
|
require(libname)
|
2006-12-31 18:02:22 +03:00
|
|
|
eval_part(libname, /^__END__\r?$/, '__END__')
|
2004-01-06 05:34:24 +03:00
|
|
|
end
|
|
|
|
module_function :loadtest__END__part
|
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
@mutex = Mutex.new
|
|
|
|
|
|
|
|
def self.in_critical(&block)
|
|
|
|
@mutex.synchronize(&block)
|
2004-01-06 05:34:24 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.in_progname(progname)
|
2008-06-27 16:19:11 +04:00
|
|
|
$program_name = progname
|
|
|
|
alias $0 $program_name
|
2004-01-06 05:34:24 +03:00
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
2008-06-27 16:19:11 +04:00
|
|
|
alias $0 $PROGRAM_NAME
|
2004-01-06 05:34:24 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.libpath(libname)
|
|
|
|
libpath = nil
|
|
|
|
$:.find do |path|
|
|
|
|
File.file?(testname = File.join(path, libname)) && libpath = testname
|
|
|
|
end
|
|
|
|
if libpath.nil?
|
|
|
|
raise RuntimeError.new("'#{libname}' not found")
|
|
|
|
end
|
|
|
|
libpath
|
|
|
|
end
|
|
|
|
end
|