aboutsummaryrefslogtreecommitdiffstats
path: root/test/ruby/test_io_m17n.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-20 15:05:48 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-10-20 15:05:48 +0000
commit24181da2c55a2df3ba99b6eab079d905a2507545 (patch)
tree1736b0918e20318f3d94e322660b94d474a9c558 /test/ruby/test_io_m17n.rb
parentd8dfba24bb82fa66f19197e94eb555c58ddc56fd (diff)
downloadruby-24181da2c55a2df3ba99b6eab079d905a2507545.tar.gz
* test/ruby/test_io.rb (TestIO#pipe): get rid of deadlock on pipe.
a patch from Tomoyuki Chikanaga at [ruby-dev:42435]. #3970 * test/ruby/test_io_m17n.rb (TestIO_M17N#pipe): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_io_m17n.rb')
-rw-r--r--test/ruby/test_io_m17n.rb787
1 files changed, 451 insertions, 336 deletions
diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb
index 24041f07ad..16195699e3 100644
--- a/test/ruby/test_io_m17n.rb
+++ b/test/ruby/test_io_m17n.rb
@@ -19,6 +19,18 @@ class TestIO_M17N < Test::Unit::TestCase
}
end
+ def pipe(*args, wp, rp)
+ r, w = IO.pipe(*args)
+ rt = Thread.new { rp.call(r) }
+ wt = Thread.new { wp.call(w) }
+ flunk("timeout") unless rt.join(10) && wt.join(10)
+ ensure
+ r.close unless !r || r.closed?
+ w.close unless !w || r.closed?
+ (rt.kill; rt.join) if rt
+ (wt.kill; wt.join) if wt
+ end
+
def with_pipe(*args)
r, w = IO.pipe(*args)
begin
@@ -220,54 +232,68 @@ EOT
end
def test_s_pipe_invalid
- with_pipe("utf-8", "euc-jp", :invalid=>:replace) {|r, w|
- w << "\x80"
- w.close
- assert_equal("?", r.read)
- }
+ pipe("utf-8", "euc-jp", { :invalid=>:replace },
+ proc do |w|
+ w << "\x80"
+ w.close
+ end,
+ proc do |r|
+ assert_equal("?", r.read)
+ end)
end
def test_s_pipe_undef
- with_pipe("utf-8:euc-jp", :undef=>:replace) {|r, w|
- w << "\ufffd"
- w.close
- assert_equal("?", r.read)
- }
+ pipe("utf-8:euc-jp", { :undef=>:replace },
+ proc do |w|
+ w << "\ufffd"
+ w.close
+ end,
+ proc do |r|
+ assert_equal("?", r.read)
+ end)
end
def test_s_pipe_undef_replace_string
- with_pipe("utf-8:euc-jp", :undef=>:replace, :replace=>"X") {|r, w|
- w << "\ufffd"
- w.close
- assert_equal("X", r.read)
- }
+ pipe("utf-8:euc-jp", { :undef=>:replace, :replace=>"X" },
+ proc do |w|
+ w << "\ufffd"
+ w.close
+ end,
+ proc do |r|
+ assert_equal("X", r.read)
+ end)
end
def test_dup
- with_pipe("utf-8:euc-jp") {|r, w|
- w << "\u3042"
- w.close
- r2 = r.dup
- begin
- assert_equal("\xA4\xA2".force_encoding("euc-jp"), r2.read)
- ensure
- r2.close
- end
-
- }
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ w << "\u3042"
+ w.close
+ end,
+ proc do |r|
+ r2 = r.dup
+ begin
+ assert_equal("\xA4\xA2".force_encoding("euc-jp"), r2.read)
+ ensure
+ r2.close
+ end
+ end)
end
def test_dup_undef
- with_pipe("utf-8:euc-jp", :undef=>:replace) {|r, w|
- w << "\uFFFD"
- w.close
- r2 = r.dup
- begin
- assert_equal("?", r2.read)
- ensure
- r2.close
- end
- }
+ pipe("utf-8:euc-jp", { :undef=>:replace },
+ proc do |w|
+ w << "\uFFFD"
+ w.close
+ end,
+ proc do |r|
+ r2 = r.dup
+ begin
+ assert_equal("?", r2.read)
+ ensure
+ r2.close
+ end
+ end)
end
def test_stdin
@@ -331,47 +357,53 @@ EOT
end
def test_pipe_terminator_conversion
- with_pipe("euc-jp:utf-8") {|r, w|
- w.write "before \xa2\xa2 after"
- rs = "\xA2\xA2".encode("utf-8", "euc-jp")
- w.close
- timeout(1) {
- assert_equal("before \xa2\xa2".encode("utf-8", "euc-jp"),
- r.gets(rs))
- }
- }
+ rs = "\xA2\xA2".encode("utf-8", "euc-jp")
+ pipe("euc-jp:utf-8",
+ proc do |w|
+ w.write "before \xa2\xa2 after"
+ w.close
+ end,
+ proc do |r|
+ timeout(1) {
+ assert_equal("before \xa2\xa2".encode("utf-8", "euc-jp"),
+ r.gets(rs))
+ }
+ end)
end
def test_pipe_conversion
- with_pipe("euc-jp:utf-8") {|r, w|
- w.write "\xa1\xa1"
- assert_equal("\xa1\xa1".encode("utf-8", "euc-jp"), r.getc)
- }
+ pipe("euc-jp:utf-8",
+ proc do |w|
+ w.write "\xa1\xa1"
+ end,
+ proc do |r|
+ assert_equal("\xa1\xa1".encode("utf-8", "euc-jp"), r.getc)
+ end)
end
def test_pipe_convert_partial_read
- with_pipe("euc-jp:utf-8") {|r, w|
- begin
- t = Thread.new {
- w.write "\xa1"
- sleep 0.1
- w.write "\xa1"
- }
- assert_equal("\xa1\xa1".encode("utf-8", "euc-jp"), r.getc)
- ensure
- t.join if t
- end
- }
+ pipe("euc-jp:utf-8",
+ proc do |w|
+ w.write "\xa1"
+ sleep 0.1
+ w.write "\xa1"
+ end,
+ proc do |r|
+ assert_equal("\xa1\xa1".encode("utf-8", "euc-jp"), r.getc)
+ end)
end
def test_getc_invalid
- with_pipe("euc-jp:utf-8") {|r, w|
- w << "\xa1xyz"
- w.close
- err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc }
- assert_equal("\xA1".force_encoding("ascii-8bit"), err.error_bytes)
- assert_equal("xyz", r.read(10))
- }
+ pipe("euc-jp:utf-8",
+ proc do |w|
+ w << "\xa1xyz"
+ w.close
+ end,
+ proc do |r|
+ err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc }
+ assert_equal("\xA1".force_encoding("ascii-8bit"), err.error_bytes)
+ assert_equal("xyz", r.read(10))
+ end)
end
def test_getc_stateful_conversion
@@ -597,37 +629,47 @@ EOT
utf8 = "\u6666"
eucjp = "\xb3\xa2".force_encoding("EUC-JP")
- with_pipe {|r,w|
- assert_equal(Encoding.default_external, r.external_encoding)
- assert_equal(nil, r.internal_encoding)
+ pipe(proc do |w|
w << utf8
w.close
+ end, proc do |r|
+ assert_equal(Encoding.default_external, r.external_encoding)
+ assert_equal(nil, r.internal_encoding)
s = r.read
assert_equal(Encoding.default_external, s.encoding)
assert_str_equal(utf8.dup.force_encoding(Encoding.default_external), s)
- }
-
- with_pipe("EUC-JP") {|r,w|
- assert_equal(Encoding::EUC_JP, r.external_encoding)
- assert_equal(nil, r.internal_encoding)
- w << eucjp
- w.close
- assert_equal(eucjp, r.read)
- }
-
- with_pipe("UTF-8") {|r,w|
- w << "a" * 1023 + "\u3042" + "a" * 1022
- w.close
- assert_equal(true, r.read.valid_encoding?)
- }
-
- with_pipe("UTF-8:EUC-JP") {|r,w|
- assert_equal(Encoding::UTF_8, r.external_encoding)
- assert_equal(Encoding::EUC_JP, r.internal_encoding)
- w << utf8
- w.close
- assert_equal(eucjp, r.read)
- }
+ end)
+
+ pipe("EUC-JP",
+ proc do |w|
+ w << eucjp
+ w.close
+ end,
+ proc do |r|
+ assert_equal(Encoding::EUC_JP, r.external_encoding)
+ assert_equal(nil, r.internal_encoding)
+ assert_equal(eucjp, r.read)
+ end)
+
+ pipe("UTF-8",
+ proc do |w|
+ w << "a" * 1023 + "\u3042" + "a" * 1022
+ w.close
+ end,
+ proc do |r|
+ assert_equal(true, r.read.valid_encoding?)
+ end)
+
+ pipe("UTF-8:EUC-JP",
+ proc do |w|
+ w << utf8
+ w.close
+ end,
+ proc do |r|
+ assert_equal(Encoding::UTF_8, r.external_encoding)
+ assert_equal(Encoding::EUC_JP, r.internal_encoding)
+ assert_equal(eucjp, r.read)
+ end)
e = assert_raise(ArgumentError) {with_pipe("UTF-8", "UTF-8".encode("UTF-32BE")) {}}
assert_match(/invalid name encoding/, e.message)
@@ -635,147 +677,174 @@ EOT
assert_match(/invalid name encoding/, e.message)
ENCS.each {|enc|
- with_pipe(enc) {|r, w|
- w << "\xc2\xa1"
- w.close
- s = r.getc
- assert_equal(enc, s.encoding)
- }
+ pipe(enc,
+ proc do |w|
+ w << "\xc2\xa1"
+ w.close
+ end,
+ proc do |r|
+ s = r.getc
+ assert_equal(enc, s.encoding)
+ end)
}
ENCS.each {|enc|
next if enc == Encoding::ASCII_8BIT
next if enc == Encoding::UTF_8
- with_pipe("#{enc}:UTF-8") {|r, w|
- w << "\xc2\xa1"
- w.close
- s = r.read
- assert_equal(Encoding::UTF_8, s.encoding)
- assert_equal(s.encode("UTF-8"), s)
- }
+ pipe("#{enc}:UTF-8",
+ proc do |w|
+ w << "\xc2\xa1"
+ w.close
+ end,
+ proc do |r|
+ s = r.read
+ assert_equal(Encoding::UTF_8, s.encoding)
+ assert_equal(s.encode("UTF-8"), s)
+ end)
}
end
def test_marshal
- with_pipe("EUC-JP") {|r, w|
- data = 56225
- Marshal.dump(data, w)
- w.close
- result = nil
- assert_nothing_raised("[ruby-dev:33264]") { result = Marshal.load(r) }
- assert_equal(data, result)
- }
+ data = 56225
+ pipe("EUC-JP",
+ proc do |w|
+ Marshal.dump(data, w)
+ w.close
+ end,
+ proc do |r|
+ result = nil
+ assert_nothing_raised("[ruby-dev:33264]") { result = Marshal.load(r) }
+ assert_equal(data, result)
+ end)
end
def test_gets_nil
- with_pipe("UTF-8:EUC-JP") {|r, w|
- w << "\u{3042}"
- w.close
- result = r.gets(nil)
- assert_equal("\u{3042}".encode("euc-jp"), result)
- }
+ pipe("UTF-8:EUC-JP",
+ proc do |w|
+ w << "\u{3042}"
+ w.close
+ end,
+ proc do |r|
+ result = r.gets(nil)
+ assert_equal("\u{3042}".encode("euc-jp"), result)
+ end)
end
def test_gets_limit
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.gets(1))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.gets(2))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2\xa4\xa4".force_encoding("euc-jp"), r.gets(3))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2\xa4\xa4".force_encoding("euc-jp"), r.gets(4))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6".force_encoding("euc-jp"), r.gets(5))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6".force_encoding("euc-jp"), r.gets(6))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(7))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(8))
- }
- with_pipe("euc-jp") {|r, w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close
- assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(9))
- }
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.gets(1)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.gets(2)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2\xa4\xa4".force_encoding("euc-jp"), r.gets(3)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2\xa4\xa4".force_encoding("euc-jp"), r.gets(4)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6".force_encoding("euc-jp"), r.gets(5)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6".force_encoding("euc-jp"), r.gets(6)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(7)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(8)) })
+ pipe("euc-jp",
+ proc {|w| w << "\xa4\xa2\xa4\xa4\xa4\xa6\n\xa4\xa8\xa4\xaa"; w.close },
+ proc {|r| assert_equal("\xa4\xa2\xa4\xa4\xa4\xa6\n".force_encoding("euc-jp"), r.gets(9)) })
end
def test_gets_invalid
- with_pipe("utf-8:euc-jp") {|r, w|
- before = "\u{3042}\u{3044}"
- invalid = "\x80".force_encoding("utf-8")
- after = "\u{3046}\u{3048}"
- w << before + invalid + after
- w.close
- err = assert_raise(Encoding::InvalidByteSequenceError) { r.gets }
- assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
- assert_equal(after.encode("euc-jp"), r.gets)
- }
+ before = "\u{3042}\u{3044}"
+ invalid = "\x80".force_encoding("utf-8")
+ after = "\u{3046}\u{3048}"
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ w << before + invalid + after
+ w.close
+ end,
+ proc do |r|
+ err = assert_raise(Encoding::InvalidByteSequenceError) { r.gets }
+ assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
+ assert_equal(after.encode("euc-jp"), r.gets)
+ end)
end
def test_getc_invalid2
- with_pipe("utf-8:euc-jp") {|r, w|
- before1 = "\u{3042}"
- before2 = "\u{3044}"
- invalid = "\x80".force_encoding("utf-8")
- after1 = "\u{3046}"
- after2 = "\u{3048}"
- w << before1 + before2 + invalid + after1 + after2
- w.close
- assert_equal(before1.encode("euc-jp"), r.getc)
- assert_equal(before2.encode("euc-jp"), r.getc)
- err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc }
- assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
- assert_equal(after1.encode("euc-jp"), r.getc)
- assert_equal(after2.encode("euc-jp"), r.getc)
- }
+ before1 = "\u{3042}"
+ before2 = "\u{3044}"
+ invalid = "\x80".force_encoding("utf-8")
+ after1 = "\u{3046}"
+ after2 = "\u{3048}"
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ w << before1 + before2 + invalid + after1 + after2
+ w.close
+ end,
+ proc do |r|
+ assert_equal(before1.encode("euc-jp"), r.getc)
+ assert_equal(before2.encode("euc-jp"), r.getc)
+ err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc }
+ assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
+ assert_equal(after1.encode("euc-jp"), r.getc)
+ assert_equal(after2.encode("euc-jp"), r.getc)
+ end)
end
def test_getc_invalid3
- with_pipe("utf-16le:euc-jp", binmode: true) {|r, w|
- before1 = "\x42\x30".force_encoding("utf-16le")
- before2 = "\x44\x30".force_encoding("utf-16le")
- invalid = "\x00\xd8".force_encoding("utf-16le")
- after1 = "\x46\x30".force_encoding("utf-16le")
- after2 = "\x48\x30".force_encoding("utf-16le")
- w << before1 + before2 + invalid + after1 + after2
- w.close
- assert_equal(before1.encode("euc-jp"), r.getc)
- assert_equal(before2.encode("euc-jp"), r.getc)
- err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc }
- assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
- assert_equal(after1.encode("euc-jp"), r.getc)
- assert_equal(after2.encode("euc-jp"), r.getc)
- }
+ before1 = "\x42\x30".force_encoding("utf-16le")
+ before2 = "\x44\x30".force_encoding("utf-16le")
+ invalid = "\x00\xd8".force_encoding("utf-16le")
+ after1 = "\x46\x30".force_encoding("utf-16le")
+ after2 = "\x48\x30".force_encoding("utf-16le")
+ pipe("utf-16le:euc-jp", { :binmode => true },
+ proc do |w|
+ w << before1 + before2 + invalid + after1 + after2
+ w.close
+ end,
+ proc do |r|
+ assert_equal(before1.encode("euc-jp"), r.getc)
+ assert_equal(before2.encode("euc-jp"), r.getc)
+ err = assert_raise(Encoding::InvalidByteSequenceError) { r.getc }
+ assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
+ assert_equal(after1.encode("euc-jp"), r.getc)
+ assert_equal(after2.encode("euc-jp"), r.getc)
+ end)
end
def test_read_all
- with_pipe("utf-8:euc-jp") {|r, w|
- str = "\u3042\u3044"
- w << str
- w.close
- assert_equal(str.encode("euc-jp"), r.read)
- }
+ str = "\u3042\u3044"
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ w << str
+ w.close
+ end,
+ proc do |r|
+ assert_equal(str.encode("euc-jp"), r.read)
+ end)
end
def test_read_all_invalid
- with_pipe("utf-8:euc-jp") {|r, w|
- before = "\u{3042}\u{3044}"
- invalid = "\x80".force_encoding("utf-8")
- after = "\u{3046}\u{3048}"
- w << before + invalid + after
- w.close
- err = assert_raise(Encoding::InvalidByteSequenceError) { r.read }
- assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
- assert_equal(after.encode("euc-jp"), r.read)
- }
+ before = "\u{3042}\u{3044}"
+ invalid = "\x80".force_encoding("utf-8")
+ after = "\u{3046}\u{3048}"
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ w << before + invalid + after
+ w.close
+ end,
+ proc do |r|
+ err = assert_raise(Encoding::InvalidByteSequenceError) { r.read }
+ assert_equal(invalid.force_encoding("ascii-8bit"), err.error_bytes)
+ assert_equal(after.encode("euc-jp"), r.read)
+ end)
end
def test_file_foreach
@@ -788,84 +857,104 @@ EOT
end
def test_set_encoding
- with_pipe("utf-8:euc-jp") {|r, w|
- s = "\u3042".force_encoding("ascii-8bit")
- s << "\x82\xa0".force_encoding("ascii-8bit")
- w << s
- w.close
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
- r.set_encoding("shift_jis:euc-jp")
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
- }
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ s = "\u3042".force_encoding("ascii-8bit")
+ s << "\x82\xa0".force_encoding("ascii-8bit")
+ w << s
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
+ r.set_encoding("shift_jis:euc-jp")
+ assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
+ end)
end
def test_set_encoding2
- with_pipe("utf-8:euc-jp") {|r, w|
- s = "\u3042".force_encoding("ascii-8bit")
- s << "\x82\xa0".force_encoding("ascii-8bit")
- w << s
- w.close
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
- r.set_encoding("shift_jis", "euc-jp")
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
- }
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ s = "\u3042".force_encoding("ascii-8bit")
+ s << "\x82\xa0".force_encoding("ascii-8bit")
+ w << s
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
+ r.set_encoding("shift_jis", "euc-jp")
+ assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
+ end)
end
def test_set_encoding_nil
- with_pipe("utf-8:euc-jp") {|r, w|
- s = "\u3042".force_encoding("ascii-8bit")
- s << "\x82\xa0".force_encoding("ascii-8bit")
- w << s
- w.close
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
- r.set_encoding(nil)
- assert_equal("\x82\xa0".force_encoding(Encoding.default_external), r.read)
- }
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ s = "\u3042".force_encoding("ascii-8bit")
+ s << "\x82\xa0".force_encoding("ascii-8bit")
+ w << s
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
+ r.set_encoding(nil)
+ assert_equal("\x82\xa0".force_encoding(Encoding.default_external), r.read)
+ end)
end
def test_set_encoding_enc
- with_pipe("utf-8:euc-jp") {|r, w|
- s = "\u3042".force_encoding("ascii-8bit")
- s << "\x82\xa0".force_encoding("ascii-8bit")
- w << s
- w.close
- assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
- r.set_encoding(Encoding::Shift_JIS)
- assert_equal("\x82\xa0".force_encoding(Encoding::Shift_JIS), r.getc)
- }
+ pipe("utf-8:euc-jp",
+ proc do |w|
+ s = "\u3042".force_encoding("ascii-8bit")
+ s << "\x82\xa0".force_encoding("ascii-8bit")
+ w << s
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\xa4\xa2".force_encoding("euc-jp"), r.getc)
+ r.set_encoding(Encoding::Shift_JIS)
+ assert_equal("\x82\xa0".force_encoding(Encoding::Shift_JIS), r.getc)
+ end)
end
def test_set_encoding_invalid
- with_pipe {|r, w|
- w << "\x80"
- w.close
- r.set_encoding("utf-8:euc-jp", :invalid=>:replace)
- assert_equal("?", r.read)
- }
+ pipe(proc do |w|
+ w << "\x80"
+ w.close
+ end,
+ proc do |r|
+ r.set_encoding("utf-8:euc-jp", :invalid=>:replace)
+ assert_equal("?", r.read)
+ end)
end
def test_set_encoding_undef
- with_pipe {|r, w|
- w << "\ufffd"
- w.close
- r.set_encoding("utf-8", "euc-jp", :undef=>:replace)
- assert_equal("?", r.read)
- }
+ pipe(proc do |w|
+ w << "\ufffd"
+ w.close
+ end,
+ proc do |r|
+ r.set_encoding("utf-8", "euc-jp", :undef=>:replace)
+ assert_equal("?", r.read)
+ end)
end
def test_set_encoding_undef_replace
- with_pipe {|r, w|
- w << "\ufffd"
- w.close
- r.set_encoding("utf-8", "euc-jp", :undef=>:replace, :replace=>"ZZZ")
- assert_equal("ZZZ", r.read)
- }
- with_pipe {|r, w|
- w << "\ufffd"
- w.close
- r.set_encoding("utf-8:euc-jp", :undef=>:replace, :replace=>"ZZZ")
- assert_equal("ZZZ", r.read)
- }
+ pipe(proc do |w|
+ w << "\ufffd"
+ w.close
+ end,
+ proc do |r|
+ r.set_encoding("utf-8", "euc-jp", :undef=>:replace, :replace=>"ZZZ")
+ assert_equal("ZZZ", r.read)
+ end)
+ pipe(proc do |w|
+ w << "\ufffd"
+ w.close
+ end,
+ proc do |r|
+ r.set_encoding("utf-8:euc-jp", :undef=>:replace, :replace=>"ZZZ")
+ assert_equal("ZZZ", r.read)
+ end)
end
def test_set_encoding_binmode
@@ -899,56 +988,67 @@ EOT
end
def test_write_conversion_fixenc
- with_pipe {|r, w|
- w.set_encoding("iso-2022-jp:utf-8")
- t = Thread.new { r.read.force_encoding("ascii-8bit") }
- w << "\u3042"
- w << "\u3044"
- w.close
- assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"), t.value)
- }
+ pipe(proc do |w|
+ w.set_encoding("iso-2022-jp:utf-8")
+ w << "\u3042"
+ w << "\u3044"
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"),
+ r.read.force_encoding("ascii-8bit"))
+ end)
end
def test_write_conversion_anyenc_stateful
- with_pipe {|r, w|
- w.set_encoding("iso-2022-jp")
- t = Thread.new { r.read.force_encoding("ascii-8bit") }
- w << "\u3042"
- w << "\x82\xa2".force_encoding("sjis")
- w.close
- assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"), t.value)
- }
+ pipe(proc do |w|
+ w.set_encoding("iso-2022-jp")
+ w << "\u3042"
+ w << "\x82\xa2".force_encoding("sjis")
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"),
+ r.read.force_encoding("ascii-8bit"))
+ end)
end
def test_write_conversion_anyenc_stateless
- with_pipe {|r, w|
- w.set_encoding("euc-jp")
- t = Thread.new { r.read.force_encoding("ascii-8bit") }
- w << "\u3042"
- w << "\x82\xa2".force_encoding("sjis")
- w.close
- assert_equal("\xa4\xa2\xa4\xa4".force_encoding("ascii-8bit"), t.value)
- }
+ pipe(proc do |w|
+ w.set_encoding("euc-jp")
+ w << "\u3042"
+ w << "\x82\xa2".force_encoding("sjis")
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\xa4\xa2\xa4\xa4".force_encoding("ascii-8bit"),
+ r.read.force_encoding("ascii-8bit"))
+ end)
end
def test_write_conversion_anyenc_stateful_nosync
- with_pipe {|r, w|
- w.sync = false
- w.set_encoding("iso-2022-jp")
- t = Thread.new { r.read.force_encoding("ascii-8bit") }
- w << "\u3042"
- w << "\x82\xa2".force_encoding("sjis")
- w.close
- assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"), t.value)
- }
+ pipe(proc do |w|
+ w.sync = false
+ w.set_encoding("iso-2022-jp")
+ w << "\u3042"
+ w << "\x82\xa2".force_encoding("sjis")
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\e$B$\"$$\e(B".force_encoding("ascii-8bit"),
+ r.read.force_encoding("ascii-8bit"))
+ end)
end
def test_read_stateful
- with_pipe("euc-jp:iso-2022-jp") {|r, w|
- w << "\xA4\xA2"
- w.close
- assert_equal("\e$B$\"\e(B".force_encoding("iso-2022-jp"), r.read)
- }
+ pipe("euc-jp:iso-2022-jp",
+ proc do |w|
+ w << "\xA4\xA2"
+ w.close
+ end,
+ proc do |r|
+ assert_equal("\e$B$\"\e(B".force_encoding("iso-2022-jp"), r.read)
+ end)
end
def test_stdin_external_encoding_with_reopen
@@ -1814,30 +1914,39 @@ EOT
end
def test_cbuf_select
- with_pipe("US-ASCII:UTF-8", :universal_newline => true) do |r, w|
- w << "\r\n"
- r.ungetc(r.getc)
- assert_equal([[r],[],[]], IO.select([r], nil, nil, 1))
- end
+ pipe("US-ASCII:UTF-8", { :universal_newline => true },
+ proc do |w|
+ w << "\r\n"
+ end,
+ proc do |r|
+ r.ungetc(r.getc)
+ assert_equal([[r],[],[]], IO.select([r], nil, nil, 1))
+ end)
end
def test_textmode_paragraphmode
- with_pipe("US-ASCII:UTF-8", :universal_newline => true) do |r, w|
- w << "a\n\n\nc".gsub(/\n/, "\r\n")
- w.close
- assert_equal("a\n\n", r.gets(""))
- assert_equal("c", r.gets(""), "[ruby-core:23723] (18)")
- end
+ pipe("US-ASCII:UTF-8", { :universal_newline => true },
+ proc do |w|
+ w << "a\n\n\nc".gsub(/\n/, "\r\n")
+ w.close
+ end,
+ proc do |r|
+ assert_equal("a\n\n", r.gets(""))
+ assert_equal("c", r.gets(""), "[ruby-core:23723] (18)")
+ end)
end
def test_textmode_paragraph_binaryread
- with_pipe("US-ASCII:UTF-8", :universal_newline => true) do |r, w|
- w << "a\n\n\ncdefgh".gsub(/\n/, "\r\n")
- w.close
- assert_equal("a\n\n", r.gets(""))
- assert_equal("c", r.getc)
- assert_equal("defgh", r.readpartial(10))
- end
+ pipe("US-ASCII:UTF-8", { :universal_newline => true },
+ proc do |w|
+ w << "a\n\n\ncdefgh".gsub(/\n/, "\r\n")
+ w.close
+ end,
+ proc do |r|
+ assert_equal("a\n\n", r.gets(""))
+ assert_equal("c", r.getc)
+ assert_equal("defgh", r.readpartial(10))
+ end)
end
def test_textmode_paragraph_nonasciicompat
@@ -1847,9 +1956,10 @@ EOT
Encoding::UTF_16BE, Encoding::UTF_16LE,
Encoding::UTF_8].each do |e|
r.set_encoding(Encoding::US_ASCII, e)
- w.print(bug3534[0], "\n\n\n\n", bug3534[1], "\n")
+ wthr = Thread.new{ w.print(bug3534[0], "\n\n\n\n", bug3534[1], "\n") }
assert_equal((bug3534[0]+"\n\n").encode(e), r.gets(""), bug3534[0])
assert_equal((bug3534[1]+"\n").encode(e), r.gets(), bug3534[1])
+ wthr.join
end
end
@@ -1862,28 +1972,33 @@ EOT
Encoding::UTF_16BE, Encoding::UTF_16LE,
Encoding::UTF_8].each do |e|
r.set_encoding(Encoding::US_ASCII, e)
- w.print(bug3534[0], "\n\n\n\n", bug3534[1], "\n")
+ wthr = Thread.new{ w.print(bug3534[0], "\n\n\n\n", bug3534[1], "\n") }
assert_equal((bug3534[0]+"\n\n").encode(e), r.gets(""), bug3534[0])
assert_equal((bug3534[1]+"\n").encode(e), r.gets(), bug3534[1])
+ wthr.join
end
end
def test_puts_widechar
bug = '[ruby-dev:42212]'
- r, w = IO.pipe(Encoding::ASCII_8BIT)
- r.binmode
- w.binmode
- w.puts(0x010a.chr(Encoding::UTF_32BE))
- w.puts(0x010a.chr(Encoding::UTF_16BE))
- w.puts(0x0a010000.chr(Encoding::UTF_32LE))
- w.puts(0x0a01.chr(Encoding::UTF_16LE))
- w.close
- assert_equal("\x00\x00\x01\x0a\n", r.read(5), bug)
- assert_equal("\x01\x0a\n", r.read(3), bug)
- assert_equal("\x00\x00\x01\x0a\n", r.read(5), bug)
- assert_equal("\x01\x0a\n", r.read(3), bug)
- assert_equal("", r.read, bug)
- r.close
+ pipe(Encoding::ASCII_8BIT,
+ proc do |w|
+ w.binmode
+ w.puts(0x010a.chr(Encoding::UTF_32BE))
+ w.puts(0x010a.chr(Encoding::UTF_16BE))
+ w.puts(0x0a010000.chr(Encoding::UTF_32LE))
+ w.puts(0x0a01.chr(Encoding::UTF_16LE))
+ w.close
+ end,
+ proc do |r|
+ r.binmode
+ assert_equal("\x00\x00\x01\x0a\n", r.read(5), bug)
+ assert_equal("\x01\x0a\n", r.read(3), bug)
+ assert_equal("\x00\x00\x01\x0a\n", r.read(5), bug)
+ assert_equal("\x01\x0a\n", r.read(3), bug)
+ assert_equal("", r.read, bug)
+ r.close
+ end)
end
end