mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
add tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25803 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3dc8a69df7
commit
31ee9fd966
2 changed files with 154 additions and 86 deletions
|
@ -128,81 +128,78 @@ module Test
|
||||||
|
|
||||||
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
|
LANG_ENVS = %w"LANG LC_ALL LC_CTYPE"
|
||||||
|
|
||||||
def invoke_ruby_assertion(args, test_stdin="", test_stdout=nil, test_stderr=nil, test_status=true, message = nil, opt={})
|
def invoke_ruby(args, stdin_data="", capture_stdout=false, capture_stderr=false, opt={})
|
||||||
in_c, in_p = IO.pipe
|
begin
|
||||||
out_p, out_c = IO.pipe if test_stdout
|
in_c, in_p = IO.pipe
|
||||||
err_p, err_c = IO.pipe if test_stderr
|
out_p, out_c = IO.pipe if capture_stdout
|
||||||
c = "C"
|
err_p, err_c = IO.pipe if capture_stderr
|
||||||
env = {}
|
c = "C"
|
||||||
LANG_ENVS.each {|lc| env[lc], ENV[lc] = ENV[lc], c}
|
env = {}
|
||||||
opt = opt.dup
|
LANG_ENVS.each {|lc| env[lc], ENV[lc] = ENV[lc], c}
|
||||||
opt[:in] = in_c
|
opt = opt.dup
|
||||||
opt[:out] = out_c if test_stdout
|
opt[:in] = in_c
|
||||||
opt[:err] = err_c if test_stderr
|
opt[:out] = out_c if capture_stdout
|
||||||
pid = spawn(EnvUtil.rubybin, *args, opt)
|
opt[:err] = err_c if capture_stderr
|
||||||
in_c.close
|
pid = spawn(EnvUtil.rubybin, *args, opt)
|
||||||
out_c.close if test_stdout
|
in_c.close
|
||||||
err_c.close if test_stderr
|
out_c.close if capture_stdout
|
||||||
in_p.write test_stdin
|
err_c.close if capture_stderr
|
||||||
in_p.close
|
in_p.write stdin_data
|
||||||
th_stdout = Thread.new { out_p.read } if test_stdout
|
in_p.close
|
||||||
th_stderr = Thread.new { err_p.read } if test_stderr
|
th_stdout = Thread.new { out_p.read } if capture_stdout
|
||||||
if (!test_stdout || th_stdout.join(10)) && (!test_stderr || th_stderr.join(10))
|
th_stderr = Thread.new { err_p.read } if capture_stderr
|
||||||
stdout = th_stdout.value if test_stdout
|
if (!capture_stdout || th_stdout.join(10)) && (!capture_stderr || th_stderr.join(10))
|
||||||
stderr = th_stderr.value if test_stderr
|
stdout = th_stdout.value if capture_stdout
|
||||||
else
|
stderr = th_stderr.value if capture_stderr
|
||||||
flunk("timeout")
|
|
||||||
end
|
|
||||||
out_p.close if test_stdout
|
|
||||||
err_p.close if test_stderr
|
|
||||||
Process.wait pid
|
|
||||||
status = $?
|
|
||||||
if block_given?
|
|
||||||
yield(test_stdout ? stdout.lines.map {|l| l.chomp } : nil, test_stderr ? stderr.lines.map {|l| l.chomp } : nil)
|
|
||||||
else
|
|
||||||
if test_stdout
|
|
||||||
if test_stdout.is_a?(Regexp)
|
|
||||||
assert_match(test_stdout, stdout, message)
|
|
||||||
else
|
|
||||||
assert_equal(test_stdout, stdout.lines.map {|l| l.chomp }, message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if test_stderr
|
|
||||||
if test_stderr.is_a?(Regexp)
|
|
||||||
assert_match(test_stderr, stderr, message)
|
|
||||||
else
|
|
||||||
assert_equal(test_stderr, stderr.lines.map {|l| l.chomp }, message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if test_status
|
|
||||||
assert(status.success?, "ruby exit stauts is not success: #{status.inspect}")
|
|
||||||
end
|
|
||||||
status
|
|
||||||
ensure
|
|
||||||
env.each_pair {|lc, v|
|
|
||||||
if v
|
|
||||||
ENV[lc] = v
|
|
||||||
else
|
else
|
||||||
ENV.delete(lc)
|
flunk("timeout")
|
||||||
end
|
end
|
||||||
} if env
|
out_p.close if capture_stdout
|
||||||
in_c.close if in_c && !in_c.closed?
|
err_p.close if capture_stderr
|
||||||
in_p.close if in_p && !in_p.closed?
|
Process.wait pid
|
||||||
out_c.close if out_c && !out_c.closed?
|
status = $?
|
||||||
out_p.close if out_p && !out_p.closed?
|
ensure
|
||||||
err_c.close if err_c && !err_c.closed?
|
env.each_pair {|lc, v|
|
||||||
err_p.close if err_p && !err_p.closed?
|
if v
|
||||||
(th_stdout.kill; th_stdout.join) if th_stdout
|
ENV[lc] = v
|
||||||
(th_stderr.kill; th_stderr.join) if th_stderr
|
else
|
||||||
|
ENV.delete(lc)
|
||||||
|
end
|
||||||
|
} if env
|
||||||
|
in_c.close if in_c && !in_c.closed?
|
||||||
|
in_p.close if in_p && !in_p.closed?
|
||||||
|
out_c.close if out_c && !out_c.closed?
|
||||||
|
out_p.close if out_p && !out_p.closed?
|
||||||
|
err_c.close if err_c && !err_c.closed?
|
||||||
|
err_p.close if err_p && !err_p.closed?
|
||||||
|
(th_stdout.kill; th_stdout.join) if th_stdout
|
||||||
|
(th_stderr.kill; th_stderr.join) if th_stderr
|
||||||
|
end
|
||||||
|
return stdout, stderr, status
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_in_out_err(args, test_stdin = "", test_stdout = [], test_stderr = [], message = nil, opt={}, &b)
|
def assert_in_out_err(args, test_stdin = "", test_stdout = [], test_stderr = [], message = nil, opt={})
|
||||||
invoke_ruby_assertion(args, test_stdin, test_stdout, test_stderr, false, message, opt, &b)
|
stdout, stderr, status = invoke_ruby(args, test_stdin, true, true, opt)
|
||||||
|
if block_given?
|
||||||
|
yield(stdout.lines.map {|l| l.chomp }, stderr.lines.map {|l| l.chomp })
|
||||||
|
else
|
||||||
|
if test_stdout.is_a?(Regexp)
|
||||||
|
assert_match(test_stdout, stdout, message)
|
||||||
|
else
|
||||||
|
assert_equal(test_stdout, stdout.lines.map {|l| l.chomp }, message)
|
||||||
|
end
|
||||||
|
if test_stderr.is_a?(Regexp)
|
||||||
|
assert_match(test_stderr, stderr, message)
|
||||||
|
else
|
||||||
|
assert_equal(test_stderr, stderr.lines.map {|l| l.chomp }, message)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_ruby_status(args, test_stdin = "", message = nil, opt={}, &b)
|
def assert_ruby_status(args, test_stdin="", message=nil, opt={}, &b)
|
||||||
invoke_ruby_assertion(args, test_stdin, nil, nil, true, message, opt, &b)
|
stdout, stderr, status = invoke_ruby(args, test_stdin, false, false, opt)
|
||||||
|
m = message ? "#{message} (#{status.inspect})" : "ruby exit stauts is not success: #{status.inspect}"
|
||||||
|
assert(status.success?, m)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,6 +11,93 @@ class TestDir_M17N < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
## UTF-8 default_external, no default_internal
|
||||||
|
|
||||||
|
def test_filename_extutf8
|
||||||
|
with_tmpdir {|d|
|
||||||
|
assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename = "\u3042"
|
||||||
|
File.open(filename, "w") {}
|
||||||
|
ents = Dir.entries(".")
|
||||||
|
exit ents.include?(filename)
|
||||||
|
EOS
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_filename_extutf8_invalid
|
||||||
|
with_tmpdir {|d|
|
||||||
|
assert_ruby_status(%w[-EASCII-8BIT], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename = "\xff".force_encoding("ASCII-8BIT") # invalid byte sequence as UTF-8
|
||||||
|
File.open(filename, "w") {}
|
||||||
|
ents = Dir.entries(".")
|
||||||
|
exit ents.include?(filename)
|
||||||
|
EOS
|
||||||
|
assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename = "\xff".force_encoding("UTF-8") # invalid byte sequence as UTF-8
|
||||||
|
File.open(filename, "w") {}
|
||||||
|
ents = Dir.entries(".")
|
||||||
|
exit ents.include?(filename)
|
||||||
|
EOS
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
## UTF-8 default_external, EUC-JP default_internal
|
||||||
|
|
||||||
|
def test_filename_extutf8_inteucjp_representable
|
||||||
|
with_tmpdir {|d|
|
||||||
|
assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename = "\u3042"
|
||||||
|
File.open(filename, "w") {}
|
||||||
|
ents = Dir.entries(".")
|
||||||
|
exit ents.include?(filename)
|
||||||
|
EOS
|
||||||
|
assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename = "\xA4\xA2".force_encoding("euc-jp")
|
||||||
|
ents = Dir.entries(".")
|
||||||
|
exit ents.include?(filename)
|
||||||
|
EOS
|
||||||
|
assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename = "\xA4\xA2".force_encoding("euc-jp")
|
||||||
|
begin
|
||||||
|
open(filename) {}
|
||||||
|
exit true
|
||||||
|
rescue Errno::ENOENT
|
||||||
|
exit false
|
||||||
|
end
|
||||||
|
EOS
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_filename_extutf8_inteucjp_unrepresentable
|
||||||
|
with_tmpdir {|d|
|
||||||
|
assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename1 = "\u2661" # WHITE HEART SUIT which is not representable in EUC-JP
|
||||||
|
filename2 = "\u3042" # HIRAGANA LETTER A which is representable in EUC-JP
|
||||||
|
File.open(filename1, "w") {}
|
||||||
|
File.open(filename2, "w") {}
|
||||||
|
ents = Dir.entries(".")
|
||||||
|
exit ents.include?(filename1) && ents.include?(filename2)
|
||||||
|
EOS
|
||||||
|
assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename1 = "\u2661" # WHITE HEART SUIT which is not representable in EUC-JP
|
||||||
|
filename2 = "\xA4\xA2".force_encoding("euc-jp") # HIRAGANA LETTER A in EUC-JP
|
||||||
|
ents = Dir.entries(".")
|
||||||
|
exit ents.include?(filename1) && ents.include?(filename2)
|
||||||
|
EOS
|
||||||
|
assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
|
||||||
|
filename1 = "\u2661" # WHITE HEART SUIT which is not representable in EUC-JP
|
||||||
|
filename2 = "\u3042" # HIRAGANA LETTER A which is representable in EUC-JP
|
||||||
|
filename3 = "\xA4\xA2".force_encoding("euc-jp") # HIRAGANA LETTER A in EUC-JP
|
||||||
|
s1 = File.stat(filename1) rescue nil
|
||||||
|
s2 = File.stat(filename2) rescue nil
|
||||||
|
s3 = File.stat(filename3) rescue nil
|
||||||
|
exit (s1 && s2 && s3) ? true : false
|
||||||
|
EOS
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
## others
|
||||||
|
|
||||||
def test_filename_bytes_euc_jp
|
def test_filename_bytes_euc_jp
|
||||||
with_tmpdir {|d|
|
with_tmpdir {|d|
|
||||||
assert_ruby_status(%w[-EEUC-JP], <<-'EOS', nil, :chdir=>d)
|
assert_ruby_status(%w[-EEUC-JP], <<-'EOS', nil, :chdir=>d)
|
||||||
|
@ -39,7 +126,7 @@ class TestDir_M17N < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_filename_utf_8
|
def test_filename_utf8_raw_name
|
||||||
with_tmpdir {|d|
|
with_tmpdir {|d|
|
||||||
assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
|
assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
|
||||||
filename = "\u3042".force_encoding("utf-8")
|
filename = "\u3042".force_encoding("utf-8")
|
||||||
|
@ -71,21 +158,5 @@ class TestDir_M17N < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_filename_ext_utf_8_and_int_euc_jp
|
|
||||||
with_tmpdir {|d|
|
|
||||||
assert_ruby_status(%w[-EUTF-8], <<-'EOS', nil, :chdir=>d)
|
|
||||||
filename = "\u3042"
|
|
||||||
File.open(filename, "w") {}
|
|
||||||
ents = Dir.entries(".")
|
|
||||||
exit ents.include?(filename)
|
|
||||||
EOS
|
|
||||||
assert_ruby_status(%w[-EUTF-8:EUC-JP], <<-'EOS', nil, :chdir=>d)
|
|
||||||
filename = "\xA4\xA2".force_encoding("euc-jp")
|
|
||||||
ents = Dir.entries(".")
|
|
||||||
exit ents.include?(filename)
|
|
||||||
EOS
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue