diff options
Diffstat (limited to 'test/ruby/test_io.rb')
-rw-r--r-- | test/ruby/test_io.rb | 102 |
1 files changed, 50 insertions, 52 deletions
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index c4b4d3850a..de78b71c6a 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -1866,60 +1866,58 @@ End end def test_s_write - t = Tempfile.new("foo") - path = t.path - t.close(false) - File.write(path, "foo\nbar\nbaz") - assert_equal("foo\nbar\nbaz", File.read(path)) - File.write(path, "FOO", 0) - assert_equal("FOO\nbar\nbaz", File.read(path)) - File.write(path, "BAR") - assert_equal("BAR", File.read(path)) - File.write(path, "\u{3042}", mode: "w", encoding: "EUC-JP") - assert_equal("\u{3042}".encode("EUC-JP"), File.read(path, encoding: "EUC-JP")) - File.delete t - assert_equal(6, File.write(path,'string',2)) - File.delete t - assert_raise(Errno::EINVAL) { File.write('/tmp/nonexisting','string',-2) } - assert_equal(6, File.write(path, 'string')) - assert_equal(3, File.write(path, 'sub', 1)) - assert_equal("ssubng", File.read(path)) - File.delete t - assert_equal(3, File.write(path, "foo", encoding: "UTF-8")) - File.delete t - assert_equal(3, File.write(path, "foo", 0, encoding: "UTF-8")) - assert_equal("foo", File.read(path)) - assert_equal(1, File.write(path, "f", 1, encoding: "UTF-8")) - assert_equal("ffo", File.read(path)) - File.delete t - assert_equal(1, File.write(path, "f", 1, encoding: "UTF-8")) - assert_equal("\00f", File.read(path)) - assert_equal(1, File.write(path, "f", 0, encoding: "UTF-8")) - assert_equal("ff", File.read(path)) - t.unlink + mkcdtmpdir do + path = "test_s_write" + File.write(path, "foo\nbar\nbaz") + assert_equal("foo\nbar\nbaz", File.read(path)) + File.write(path, "FOO", 0) + assert_equal("FOO\nbar\nbaz", File.read(path)) + File.write(path, "BAR") + assert_equal("BAR", File.read(path)) + File.write(path, "\u{3042}", mode: "w", encoding: "EUC-JP") + assert_equal("\u{3042}".encode("EUC-JP"), File.read(path, encoding: "EUC-JP")) + File.delete path + assert_equal(6, File.write(path, 'string', 2)) + File.delete path + assert_raise(Errno::EINVAL) { File.write('nonexisting','string', -2) } + assert_equal(6, File.write(path, 'string')) + assert_equal(3, File.write(path, 'sub', 1)) + assert_equal("ssubng", File.read(path)) + File.delete path + assert_equal(3, File.write(path, "foo", encoding: "UTF-8")) + File.delete path + assert_equal(3, File.write(path, "foo", 0, encoding: "UTF-8")) + assert_equal("foo", File.read(path)) + assert_equal(1, File.write(path, "f", 1, encoding: "UTF-8")) + assert_equal("ffo", File.read(path)) + File.delete path + assert_equal(1, File.write(path, "f", 1, encoding: "UTF-8")) + assert_equal("\00f", File.read(path)) + assert_equal(1, File.write(path, "f", 0, encoding: "UTF-8")) + assert_equal("ff", File.read(path)) + end end def test_s_binwrite - t = Tempfile.new("foo") - path = t.path - t.close(false) - File.binwrite(path, "foo\nbar\nbaz") - assert_equal("foo\nbar\nbaz", File.read(path)) - File.binwrite(path, "FOO", 0) - assert_equal("FOO\nbar\nbaz", File.read(path)) - File.binwrite(path, "BAR") - assert_equal("BAR", File.read(path)) - File.binwrite(path, "\u{3042}") - assert_equal("\u{3042}".force_encoding("ASCII-8BIT"), File.binread(path)) - File.delete t - assert_equal(6, File.binwrite(path,'string',2)) - File.delete t - assert_equal(6, File.binwrite(path, 'string')) - assert_equal(3, File.binwrite(path, 'sub', 1)) - assert_equal("ssubng", File.binread(path)) - assert_equal(6, File.size(path)) - assert_raise(Errno::EINVAL) { File.binwrite('/tmp/nonexisting','string',-2) } - assert_nothing_raised(TypeError) { File.binwrite(path, "string", mode: "w", encoding: "EUC-JP") } - t.unlink + mkcdtmpdir do + path = "test_s_binwrite" + File.binwrite(path, "foo\nbar\nbaz") + assert_equal("foo\nbar\nbaz", File.read(path)) + File.binwrite(path, "FOO", 0) + assert_equal("FOO\nbar\nbaz", File.read(path)) + File.binwrite(path, "BAR") + assert_equal("BAR", File.read(path)) + File.binwrite(path, "\u{3042}") + assert_equal("\u{3042}".force_encoding("ASCII-8BIT"), File.binread(path)) + File.delete path + assert_equal(6, File.binwrite(path, 'string', 2)) + File.delete path + assert_equal(6, File.binwrite(path, 'string')) + assert_equal(3, File.binwrite(path, 'sub', 1)) + assert_equal("ssubng", File.binread(path)) + assert_equal(6, File.size(path)) + assert_raise(Errno::EINVAL) { File.binwrite('nonexisting', 'string', -2) } + assert_nothing_raised(TypeError) { File.binwrite(path, "string", mode: "w", encoding: "EUC-JP") } + end end end |