From 751bb53a4b5c3ca524b8203951e2332f0a38c838 Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 23 Dec 2007 17:05:40 +0000 Subject: add test for IO.pipe. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14538 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_io_m17n.rb | 66 ++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 29 deletions(-) (limited to 'test/ruby') diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 49a386ce81..e859f6d39e 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -2,13 +2,31 @@ require 'test/unit' require 'tmpdir' class TestIO_M17N < Test::Unit::TestCase + ENCS = [ + Encoding::ASCII_8BIT, + Encoding::EUC_JP, + Encoding::Shift_JIS, + Encoding::UTF_8 + ] + def with_tmpdir Dir.mktmpdir {|dir| - Dir.chdir dir - yield dir + Dir.chdir(dir) { + yield dir + } } end + def with_pipe(enc=nil) + r, w = IO.pipe(enc) + begin + yield r, w + ensure + r.close if !r.closed? + w.close if !w.closed? + end + end + def generate_file(path, content) open(path, "wb") {|f| f.write content } end @@ -39,12 +57,7 @@ EOT with_tmpdir { src = "abc\n" generate_file('tmp', "abc\n") - [ - Encoding::ASCII_8BIT, - Encoding::EUC_JP, - Encoding::Shift_JIS, - Encoding::UTF_8 - ].each {|enc| + ENCS.each {|enc| s = open('tmp', "r:#{enc}") {|f| f.gets } assert_equal(enc, s.encoding) assert_str_equal(src, s) @@ -56,12 +69,7 @@ EOT with_tmpdir { src = "\xc2\xa1\n" generate_file('tmp', src) - [ - Encoding::ASCII_8BIT, - Encoding::EUC_JP, - Encoding::Shift_JIS, - Encoding::UTF_8 - ].each {|enc| + ENCS.each {|enc| content = src.dup.force_encoding(enc) s = open('tmp', "r:#{enc}") {|f| f.gets } assert_equal(enc, s.encoding) @@ -74,12 +82,7 @@ EOT with_tmpdir { src = "\xc2\xa1\n".force_encoding("ASCII-8BIT") generate_file('tmp', "\xc2\xa1\n") - [ - Encoding::ASCII_8BIT, - Encoding::EUC_JP, - Encoding::Shift_JIS, - Encoding::UTF_8 - ].each {|enc| + ENCS.each {|enc| content = src.dup.force_encoding(enc) open('tmp', "r:#{enc}") {|f| s = f.getc @@ -140,25 +143,19 @@ EOT def test_write_noenc src = "\xc2\xa1\n" - encs = [ - Encoding::ASCII_8BIT, - Encoding::EUC_JP, - Encoding::Shift_JIS, - Encoding::UTF_8 - ] with_tmpdir { open('tmp', "w") {|f| - encs.each {|enc| + ENCS.each {|enc| f.write src.dup.force_encoding(enc) } } open('tmp', 'rb') {|f| - assert_equal(src*encs.length, f.read) + assert_equal(src*ENCS.length, f.read) } } end - def test_write_enc + def test_write_conversion utf8 = "\u6666" eucjp = "\xb3\xa2".force_encoding("EUC-JP") with_tmpdir { @@ -174,5 +171,16 @@ EOT } end + def test_pipe + ENCS.each {|enc| + with_pipe(enc) {|r, w| + w << "\xc2\xa1" + w.close + s = r.getc + assert_equal(enc, s.encoding) + } + } + end + end -- cgit v1.2.3