2003-10-04 22:56:42 -04:00
|
|
|
require 'test/unit'
|
2003-10-07 02:27:11 -04:00
|
|
|
require 'tempfile'
|
2003-10-21 23:53:41 -04:00
|
|
|
require 'envutil'
|
2003-10-04 22:56:42 -04:00
|
|
|
|
|
|
|
class TestBeginEndBlock < Test::Unit::TestCase
|
|
|
|
DIR = File.dirname(File.expand_path(__FILE__))
|
|
|
|
|
2003-10-07 02:27:11 -04:00
|
|
|
def q(content)
|
|
|
|
"\"#{content}\""
|
|
|
|
end
|
|
|
|
|
2003-10-04 22:56:42 -04:00
|
|
|
def test_beginendblock
|
|
|
|
ruby = EnvUtil.rubybin
|
2003-10-07 02:27:11 -04:00
|
|
|
target = File.join(DIR, 'beginmainend.rb')
|
2006-12-31 10:02:22 -05:00
|
|
|
IO.popen("#{q(ruby)} #{q(target)}"){|io|
|
|
|
|
assert_equal(%w(b1 b2-1 b2 main b3-1 b3 b4 e1 e4 e3 e2 e4-2 e4-1 e1-1 e4-1-1), io.read.split)
|
|
|
|
}
|
2003-10-05 00:51:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_begininmethod
|
|
|
|
assert_raises(SyntaxError) do
|
|
|
|
eval("def foo; BEGIN {}; end")
|
|
|
|
end
|
|
|
|
|
2003-10-07 02:27:11 -04:00
|
|
|
assert_raises(SyntaxError) do
|
|
|
|
eval('eval("def foo; BEGIN {}; end")')
|
2003-10-05 00:51:05 -04:00
|
|
|
end
|
2003-10-07 02:27:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_endblockwarn
|
|
|
|
ruby = EnvUtil.rubybin
|
|
|
|
# Use Tempfile to create temporary file path.
|
|
|
|
launcher = Tempfile.new(self.class.name)
|
|
|
|
errout = Tempfile.new(self.class.name)
|
|
|
|
|
|
|
|
launcher << <<EOF
|
|
|
|
errout = ARGV.shift
|
|
|
|
STDERR.reopen(File.open(errout, "w"))
|
|
|
|
STDERR.sync = true
|
|
|
|
Dir.chdir(#{q(DIR)})
|
|
|
|
cmd = "\\"#{ruby}\\" \\"endblockwarn.rb\\""
|
2003-10-14 11:14:02 -04:00
|
|
|
system(cmd)
|
2003-10-07 02:27:11 -04:00
|
|
|
EOF
|
|
|
|
launcher.close
|
|
|
|
launcherpath = launcher.path
|
|
|
|
errout.close
|
|
|
|
erroutpath = errout.path
|
|
|
|
system("#{q(ruby)} #{q(launcherpath)} #{q(erroutpath)}")
|
2006-12-31 10:02:22 -05:00
|
|
|
# expected = <<EOW
|
|
|
|
#endblockwarn.rb:2: warning: END in method; use at_exit
|
|
|
|
#(eval):2: warning: END in method; use at_exit
|
|
|
|
#EOW
|
2003-10-07 02:27:11 -04:00
|
|
|
expected = <<EOW
|
2006-12-31 10:02:22 -05:00
|
|
|
warning: END in method; use at_exit
|
|
|
|
warning: END in method; use at_exit
|
2003-10-07 02:27:11 -04:00
|
|
|
EOW
|
|
|
|
assert_equal(expected, File.read(erroutpath))
|
|
|
|
# expecting Tempfile to unlink launcher and errout file.
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|