diff options
Diffstat (limited to 'test/ruby/test_argf.rb')
-rw-r--r-- | test/ruby/test_argf.rb | 110 |
1 files changed, 60 insertions, 50 deletions
diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index 12f7d6485a..55a06296aa 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -9,39 +9,23 @@ class TestArgf < Test::Unit::TestCase def setup @tmpdir = Dir.mktmpdir @tmp_count = 0 - @t1 = make_tempfile0("argf-foo") - @t1.binmode - @t1.puts "1" - @t1.puts "2" - @t1.close - @t2 = make_tempfile0("argf-bar") - @t2.binmode - @t2.puts "3" - @t2.puts "4" - @t2.close - @t3 = make_tempfile0("argf-baz") - @t3.binmode - @t3.puts "5" - @t3.puts "6" - @t3.close + @t1 = make_tempfile("argf-foo", %w"1 2", binmode: true) + @t2 = make_tempfile("argf-bar", %w"3 4", binmode: true) + @t3 = make_tempfile("argf-baz", %w"5 6", binmode: true) end def teardown FileUtils.rmtree(@tmpdir) end - def make_tempfile0(basename) + def make_tempfile(basename = "argf-qux", data = %w[foo bar baz], binmode: false) @tmp_count += 1 - open("#{@tmpdir}/#{basename}-#{@tmp_count}", "w") - end - - def make_tempfile(basename = "argf-qux") - t = make_tempfile0(basename) - t.puts "foo" - t.puts "bar" - t.puts "baz" - t.close - t + path = "#{@tmpdir}/#{basename}-#{@tmp_count}" + File.open(path, "w") do |f| + f.binmode if binmode + f.puts(*data) + f + end end def ruby(*args, external_encoding: Encoding::UTF_8) @@ -571,15 +555,11 @@ class TestArgf < Test::Unit::TestCase end end - t1 = open("#{@tmpdir}/argf-hoge", "w") - t1.binmode - t1.puts "foo" - t1.close - t2 = open("#{@tmpdir}/argf-moge", "w") - t2.binmode - t2.puts "bar" - t2.close - ruby('-e', 'STDERR.reopen(STDOUT); ARGF.gets; ARGF.skip; p ARGF.eof?', t1.path, t2.path) do |f| + t1 = "#{@tmpdir}/argf-hoge" + t2 = "#{@tmpdir}/argf-moge" + File.binwrite(t1, "foo\n") + File.binwrite(t2, "bar\n") + ruby('-e', 'STDERR.reopen(STDOUT); ARGF.gets; ARGF.skip; p ARGF.eof?', t1, t2) do |f| assert_equal(%w(false), f.read.split(/\n/)) end end @@ -593,7 +573,7 @@ class TestArgf < Test::Unit::TestCase def test_read2 ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f| {# - s = "" + s = +"" ARGF.read(8, s) p s }; @@ -604,7 +584,7 @@ class TestArgf < Test::Unit::TestCase def test_read2_with_not_empty_buffer ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f| {# - s = "0123456789" + s = +"0123456789" ARGF.read(8, s) p s }; @@ -617,7 +597,7 @@ class TestArgf < Test::Unit::TestCase {# nil while ARGF.gets p ARGF.read - p ARGF.read(0, "") + p ARGF.read(0, +"") }; assert_equal("nil\n\"\"\n", f.read) end @@ -626,13 +606,13 @@ class TestArgf < Test::Unit::TestCase def test_readpartial ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f| {# - s = "" + s = +"" begin loop do s << ARGF.readpartial(1) - t = ""; ARGF.readpartial(1, t); s << t + t = +""; ARGF.readpartial(1, t); s << t # not empty buffer - u = "abcdef"; ARGF.readpartial(1, u); s << u + u = +"abcdef"; ARGF.readpartial(1, u); s << u end rescue EOFError puts s @@ -645,11 +625,11 @@ class TestArgf < Test::Unit::TestCase def test_readpartial2 ruby('-e', "#{<<~"{#"}\n#{<<~'};'}") do |f| {# - s = "" + s = +"" begin loop do s << ARGF.readpartial(1) - t = ""; ARGF.readpartial(1, t); s << t + t = +""; ARGF.readpartial(1, t); s << t end rescue EOFError $stdout.binmode @@ -680,7 +660,7 @@ class TestArgf < Test::Unit::TestCase def test_getc ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f| {# - s = "" + s = +"" while c = ARGF.getc s << c end @@ -706,7 +686,7 @@ class TestArgf < Test::Unit::TestCase def test_readchar ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f| {# - s = "" + s = +"" begin while c = ARGF.readchar s << c @@ -784,7 +764,7 @@ class TestArgf < Test::Unit::TestCase def test_each_char ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f| {# - s = "" + s = +"" ARGF.each_char {|c| s << c } puts s }; @@ -854,7 +834,7 @@ class TestArgf < Test::Unit::TestCase def test_binmode bug5268 = '[ruby-core:39234]' - open(@t3.path, "wb") {|f| f.write "5\r\n6\r\n"} + File.binwrite(@t3.path, "5\r\n6\r\n") ruby('-e', "ARGF.binmode; STDOUT.binmode; puts ARGF.read", @t1.path, @t2.path, @t3.path) do |f| f.binmode assert_equal("1\n2\n3\n4\n5\r\n6\r\n", f.read, bug5268) @@ -863,7 +843,7 @@ class TestArgf < Test::Unit::TestCase def test_textmode bug5268 = '[ruby-core:39234]' - open(@t3.path, "wb") {|f| f.write "5\r\n6\r\n"} + File.binwrite(@t3.path, "5\r\n6\r\n") ruby('-e', "STDOUT.binmode; puts ARGF.read", @t1.path, @t2.path, @t3.path) do |f| f.binmode assert_equal("1\n2\n3\n4\n5\n6\n", f.read, bug5268) @@ -1073,7 +1053,7 @@ class TestArgf < Test::Unit::TestCase ruby('-e', "#{<<~"{#"}\n#{<<~'};'}") do |f| {# $stdout.sync = true - :wait_readable == ARGF.read_nonblock(1, "", exception: false) or + :wait_readable == ARGF.read_nonblock(1, +"", exception: false) or abort "did not return :wait_readable" begin @@ -1086,7 +1066,7 @@ class TestArgf < Test::Unit::TestCase IO.select([ARGF]) == [[ARGF], [], []] or abort 'did not awaken for readability (before byte)' - buf = '' + buf = +'' buf.object_id == ARGF.read_nonblock(1, buf).object_id or abort "read destination buffer failed" print buf @@ -1140,4 +1120,34 @@ class TestArgf < Test::Unit::TestCase argf.close end end + + def test_putc + t = make_tempfile("argf-#{__method__}", 'bar') + ruby('-pi-', '-e', "print ARGF.putc('x')", t.path) do |f| + end + assert_equal("xxbar\n", File.read(t.path)) + end + + def test_puts + t = make_tempfile("argf-#{__method__}", 'bar') + err = "#{@tmpdir}/errout" + ruby('-pi-', '-W2', '-e', "print ARGF.puts('foo')", t.path, {err: err}) do |f| + end + assert_equal("foo\nbar\n", File.read(t.path)) + assert_empty File.read(err) + end + + def test_print + t = make_tempfile("argf-#{__method__}", 'bar') + ruby('-pi-', '-e', "print ARGF.print('foo')", t.path) do |f| + end + assert_equal("foobar\n", File.read(t.path)) + end + + def test_printf + t = make_tempfile("argf-#{__method__}", 'bar') + ruby('-pi-', '-e', "print ARGF.printf('%s', 'foo')", t.path) do |f| + end + assert_equal("foobar\n", File.read(t.path)) + end end |