Refine test_argf.rb

- Make `make_tempfile` to take data to write, `binmode:` flag, and a
  block.
- Use `make_tempfile` instead of `make_tempfile0`.
- Use `File.binwrite`.
This commit is contained in:
Nobuyoshi Nakada 2024-04-04 18:06:06 +09:00
parent f5e387a300
commit 64b0f4303e

View File

@ -9,39 +9,23 @@ class TestArgf < Test::Unit::TestCase
def setup def setup
@tmpdir = Dir.mktmpdir @tmpdir = Dir.mktmpdir
@tmp_count = 0 @tmp_count = 0
@t1 = make_tempfile0("argf-foo") @t1 = make_tempfile("argf-foo", %w"1 2", binmode: true)
@t1.binmode @t2 = make_tempfile("argf-bar", %w"3 4", binmode: true)
@t1.puts "1" @t3 = make_tempfile("argf-baz", %w"5 6", binmode: true)
@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
end end
def teardown def teardown
FileUtils.rmtree(@tmpdir) FileUtils.rmtree(@tmpdir)
end end
def make_tempfile0(basename) def make_tempfile(basename = "argf-qux", data = %w[foo bar baz], binmode: false)
@tmp_count += 1 @tmp_count += 1
open("#{@tmpdir}/#{basename}-#{@tmp_count}", "w") path = "#{@tmpdir}/#{basename}-#{@tmp_count}"
end File.open(path, "w") do |f|
f.binmode if binmode
def make_tempfile(basename = "argf-qux") f.puts(*data)
t = make_tempfile0(basename) f
t.puts "foo" end
t.puts "bar"
t.puts "baz"
t.close
t
end end
def ruby(*args, external_encoding: Encoding::UTF_8) def ruby(*args, external_encoding: Encoding::UTF_8)
@ -571,15 +555,11 @@ class TestArgf < Test::Unit::TestCase
end end
end end
t1 = open("#{@tmpdir}/argf-hoge", "w") t1 = "#{@tmpdir}/argf-hoge"
t1.binmode t2 = "#{@tmpdir}/argf-moge"
t1.puts "foo" File.binwrite(t1, "foo\n")
t1.close File.binwrite(t2, "bar\n")
t2 = open("#{@tmpdir}/argf-moge", "w") ruby('-e', 'STDERR.reopen(STDOUT); ARGF.gets; ARGF.skip; p ARGF.eof?', t1, t2) do |f|
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|
assert_equal(%w(false), f.read.split(/\n/)) assert_equal(%w(false), f.read.split(/\n/))
end end
end end
@ -854,7 +834,7 @@ class TestArgf < Test::Unit::TestCase
def test_binmode def test_binmode
bug5268 = '[ruby-core:39234]' 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| ruby('-e', "ARGF.binmode; STDOUT.binmode; puts ARGF.read", @t1.path, @t2.path, @t3.path) do |f|
f.binmode f.binmode
assert_equal("1\n2\n3\n4\n5\r\n6\r\n", f.read, bug5268) 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 def test_textmode
bug5268 = '[ruby-core:39234]' 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| ruby('-e', "STDOUT.binmode; puts ARGF.read", @t1.path, @t2.path, @t3.path) do |f|
f.binmode f.binmode
assert_equal("1\n2\n3\n4\n5\n6\n", f.read, bug5268) assert_equal("1\n2\n3\n4\n5\n6\n", f.read, bug5268)
@ -1142,36 +1122,28 @@ class TestArgf < Test::Unit::TestCase
end end
def test_putc def test_putc
t = make_tempfile0("argf-#{__method__}") t = make_tempfile("argf-#{__method__}", 'bar')
t.puts 'bar'
t.close
ruby('-pi-', '-e', "print ARGF.putc('x')", t.path) do |f| ruby('-pi-', '-e', "print ARGF.putc('x')", t.path) do |f|
end end
assert_equal("xxbar\n", File.read(t.path)) assert_equal("xxbar\n", File.read(t.path))
end end
def test_puts def test_puts
t = make_tempfile0("argf-#{__method__}") t = make_tempfile("argf-#{__method__}", 'bar')
t.puts 'bar'
t.close
ruby('-pi-', '-W0', '-e', "print ARGF.puts('foo')", t.path) do |f| ruby('-pi-', '-W0', '-e', "print ARGF.puts('foo')", t.path) do |f|
end end
assert_equal("foo\nbar\n", File.read(t.path)) assert_equal("foo\nbar\n", File.read(t.path))
end end
def test_print def test_print
t = make_tempfile0("argf-#{__method__}") t = make_tempfile("argf-#{__method__}", 'bar')
t.puts 'bar'
t.close
ruby('-pi-', '-e', "print ARGF.print('foo')", t.path) do |f| ruby('-pi-', '-e', "print ARGF.print('foo')", t.path) do |f|
end end
assert_equal("foobar\n", File.read(t.path)) assert_equal("foobar\n", File.read(t.path))
end end
def test_printf def test_printf
t = make_tempfile0("argf-#{__method__}") t = make_tempfile("argf-#{__method__}", 'bar')
t.puts 'bar'
t.close
ruby('-pi-', '-e', "print ARGF.printf('%s', 'foo')", t.path) do |f| ruby('-pi-', '-e', "print ARGF.printf('%s', 'foo')", t.path) do |f|
end end
assert_equal("foobar\n", File.read(t.path)) assert_equal("foobar\n", File.read(t.path))