diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-23 16:35:43 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-12-23 16:35:43 +0000 |
commit | f5512ba7a3432aec693326ac0d49a07009c8cdd1 (patch) | |
tree | 214b4df1d92c10944c733c0e51905e23622bd135 /test/ruby/test_io_m17n.rb | |
parent | 4b45ef7ba32455b4d61dbc1777ef79637c88380b (diff) | |
download | ruby-f5512ba7a3432aec693326ac0d49a07009c8cdd1.tar.gz |
add tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14536 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_io_m17n.rb')
-rw-r--r-- | test/ruby/test_io_m17n.rb | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index e6ba9913a9..49a386ce81 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -62,14 +62,15 @@ EOT Encoding::Shift_JIS, Encoding::UTF_8 ].each {|enc| + content = src.dup.force_encoding(enc) s = open('tmp', "r:#{enc}") {|f| f.gets } assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc), s) + assert_str_equal(content, s) } } end - def test_bytes + def test_read_encoding with_tmpdir { src = "\xc2\xa1\n".force_encoding("ASCII-8BIT") generate_file('tmp', "\xc2\xa1\n") @@ -79,43 +80,44 @@ EOT Encoding::Shift_JIS, Encoding::UTF_8 ].each {|enc| + content = src.dup.force_encoding(enc) open('tmp', "r:#{enc}") {|f| s = f.getc assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc)[0], s) + assert_str_equal(content[0], s) } open('tmp', "r:#{enc}") {|f| s = f.readchar assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc)[0], s) + assert_str_equal(content[0], s) } open('tmp', "r:#{enc}") {|f| s = f.gets assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc), s) + assert_str_equal(content, s) } open('tmp', "r:#{enc}") {|f| s = f.readline assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc), s) + assert_str_equal(content, s) } open('tmp', "r:#{enc}") {|f| lines = f.readlines assert_equal(1, lines.length) s = lines[0] assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc), s) + assert_str_equal(content, s) } open('tmp', "r:#{enc}") {|f| f.each_line {|s| assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc), s) + assert_str_equal(content, s) } } open('tmp', "r:#{enc}") {|f| s = f.read assert_equal(enc, s.encoding) - assert_str_equal(src.dup.force_encoding(enc), s) + assert_str_equal(content, s) } open('tmp', "r:#{enc}") {|f| s = f.read(1) @@ -136,5 +138,41 @@ EOT } end + 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| + f.write src.dup.force_encoding(enc) + } + } + open('tmp', 'rb') {|f| + assert_equal(src*encs.length, f.read) + } + } + end + + def test_write_enc + utf8 = "\u6666" + eucjp = "\xb3\xa2".force_encoding("EUC-JP") + with_tmpdir { + open('tmp', "w:EUC-JP") {|f| + f.print utf8 + } + assert_equal(eucjp, File.read('tmp').force_encoding("EUC-JP")) + open('tmp', 'r:UTF-8:EUC-JP') {|f| + assert_equal(Encoding::EUC_JP, f.external_encoding) + assert_equal(Encoding::UTF_8, f.internal_encoding) + assert_equal(utf8, f.read) + } + } + end + end |