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
@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
@ -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)
@ -1142,36 +1122,28 @@ class TestArgf < Test::Unit::TestCase
end
def test_putc
t = make_tempfile0("argf-#{__method__}")
t.puts 'bar'
t.close
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_tempfile0("argf-#{__method__}")
t.puts 'bar'
t.close
t = make_tempfile("argf-#{__method__}", 'bar')
ruby('-pi-', '-W0', '-e', "print ARGF.puts('foo')", t.path) do |f|
end
assert_equal("foo\nbar\n", File.read(t.path))
end
def test_print
t = make_tempfile0("argf-#{__method__}")
t.puts 'bar'
t.close
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_tempfile0("argf-#{__method__}")
t.puts 'bar'
t.close
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))