test_argf.rb: indent here docs

* test/ruby/test_argf.rb (assert_src_expected): default line
  number to caller's location.

* test/ruby/test_argf.rb (test_lineno, test_lineno2): get rid of a
  bug of wrong indentation after $. in Emacs 25.3 ruby-mode.el

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60096 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-10-02 06:46:41 +00:00
parent 7ae65b24c2
commit 85847e78a0

View File

@ -57,7 +57,7 @@ class TestArgf < Test::Unit::TestCase
/cygwin|mswin|mingw|bccwin/ =~ RUBY_PLATFORM /cygwin|mswin|mingw|bccwin/ =~ RUBY_PLATFORM
end end
def assert_src_expected(line, src, args = nil) def assert_src_expected(src, args = nil, line: caller_locations(1, 1)[0].lineno+1)
args ||= [@t1.path, @t2.path, @t3.path] args ||= [@t1.path, @t2.path, @t3.path]
expected = src.split(/^/) expected = src.split(/^/)
ruby('-e', src, *args) do |f| ruby('-e', src, *args) do |f|
@ -71,7 +71,8 @@ class TestArgf < Test::Unit::TestCase
end end
def test_argf def test_argf
assert_src_expected(__LINE__+1, <<-'SRC') assert_src_expected("#{<<~"{#"}\n#{<<~'};'}")
{#
a = ARGF a = ARGF
b = a.dup b = a.dup
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, "1", 1] p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["1", 1, "1", 1]
@ -87,54 +88,59 @@ class TestArgf < Test::Unit::TestCase
b.rewind b.rewind
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 5] p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["5", 5, "5", 5]
p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, "6", 6] p [a.gets.chomp, a.lineno, b.gets.chomp, b.lineno] #=> ["6", 6, "6", 6]
SRC };
end end
def test_lineno def test_lineno
assert_src_expected(__LINE__+1, <<-'SRC') assert_src_expected("#{<<~"{#"}\n#{<<~'};'}")
{#
a = ARGF a = ARGF
a.gets; p $. #=> 1 a.gets; p($.) #=> 1
a.gets; p $. #=> 2 a.gets; p($.) #=> 2
a.gets; p $. #=> 3 a.gets; p($.) #=> 3
a.rewind; p $. #=> 3 a.rewind; p($.) #=> 3
a.gets; p $. #=> 3 a.gets; p($.) #=> 3
a.gets; p $. #=> 4 a.gets; p($.) #=> 4
a.rewind; p $. #=> 4 a.rewind; p($.) #=> 4
a.gets; p $. #=> 3 a.gets; p($.) #=> 3
a.lineno = 1000; p $. #=> 1000 a.lineno = 1000; p($.) #=> 1000
a.gets; p $. #=> 1001 a.gets; p($.) #=> 1001
a.gets; p $. #=> 1002 a.gets; p($.) #=> 1002
$. = 2000 $. = 2000
a.gets; p $. #=> 2001 a.gets; p($.) #=> 2001
a.gets; p $. #=> 2001 a.gets; p($.) #=> 2001
SRC };
end end
def test_lineno2 def test_lineno2
assert_src_expected(__LINE__+1, <<-'SRC') assert_src_expected("#{<<~"{#"}\n#{<<~'};'}")
{#
a = ARGF.dup a = ARGF.dup
a.gets; p $. #=> 1 a.gets; p($.) #=> 1
a.gets; p $. #=> 2 a.gets; p($.) #=> 2
a.gets; p $. #=> 1 a.gets; p($.) #=> 1
a.rewind; p $. #=> 1 a.rewind; p($.) #=> 1
a.gets; p $. #=> 1 a.gets; p($.) #=> 1
a.gets; p $. #=> 2 a.gets; p($.) #=> 2
a.gets; p $. #=> 1 a.gets; p($.) #=> 1
a.lineno = 1000; p $. #=> 1 a.lineno = 1000; p($.) #=> 1
a.gets; p $. #=> 2 a.gets; p($.) #=> 2
a.gets; p $. #=> 2 a.gets; p($.) #=> 2
$. = 2000 $. = 2000
a.gets; p $. #=> 2000 a.gets; p($.) #=> 2000
a.gets; p $. #=> 2000 a.gets; p($.) #=> 2000
SRC };
end end
def test_lineno3 def test_lineno3
assert_in_out_err(["-", @t1.path, @t2.path], <<-INPUT, %w"1 1 1 2 2 2 3 3 1 4 4 2", [], "[ruby-core:25205]") expected = %w"1 1 1 2 2 2 3 3 1 4 4 2"
assert_in_out_err(["-", @t1.path, @t2.path],
"#{<<~"{#"}\n#{<<~'};'}", expected, [], "[ruby-core:25205]")
{#
ARGF.each do |line| ARGF.each do |line|
puts [$., ARGF.lineno, ARGF.file.lineno] puts [$., ARGF.lineno, ARGF.file.lineno]
end end
INPUT };
end end
def test_new_lineno_each def test_new_lineno_each
@ -159,12 +165,14 @@ class TestArgf < Test::Unit::TestCase
end end
def test_inplace def test_inplace
assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], []) assert_in_out_err(["-", @t1.path, @t2.path, @t3.path],
"#{<<~"{#"}\n#{<<~'};'}")
{#
ARGF.inplace_mode = '.bak' ARGF.inplace_mode = '.bak'
while line = ARGF.gets while line = ARGF.gets
puts line.chomp + '.new' puts line.chomp + '.new'
end end
INPUT };
assert_equal("1.new\n2.new\n", File.read(@t1.path)) assert_equal("1.new\n2.new\n", File.read(@t1.path))
assert_equal("3.new\n4.new\n", File.read(@t2.path)) assert_equal("3.new\n4.new\n", File.read(@t2.path))
assert_equal("5.new\n6.new\n", File.read(@t3.path)) assert_equal("5.new\n6.new\n", File.read(@t3.path))
@ -174,7 +182,9 @@ class TestArgf < Test::Unit::TestCase
end end
def test_inplace2 def test_inplace2
assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], []) assert_in_out_err(["-", @t1.path, @t2.path, @t3.path],
"#{<<~"{#"}\n#{<<~'};'}")
{#
ARGF.inplace_mode = '.bak' ARGF.inplace_mode = '.bak'
puts ARGF.gets.chomp + '.new' puts ARGF.gets.chomp + '.new'
puts ARGF.gets.chomp + '.new' puts ARGF.gets.chomp + '.new'
@ -188,7 +198,7 @@ class TestArgf < Test::Unit::TestCase
p ARGF.inplace_mode p ARGF.inplace_mode
ARGF.inplace_mode = nil ARGF.inplace_mode = nil
puts ARGF.gets.chomp + '.new' puts ARGF.gets.chomp + '.new'
INPUT };
assert_equal("1.new\n2.new\n\".bak\"\n3.new\n4.new\nnil\n", File.read(@t1.path)) assert_equal("1.new\n2.new\n\".bak\"\n3.new\n4.new\nnil\n", File.read(@t1.path))
assert_equal("3\n4\n", File.read(@t2.path)) assert_equal("3\n4\n", File.read(@t2.path))
assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path)) assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path))
@ -198,7 +208,9 @@ class TestArgf < Test::Unit::TestCase
end end
def test_inplace3 def test_inplace3
assert_in_out_err(["-i.bak", "-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], []) assert_in_out_err(["-i.bak", "-", @t1.path, @t2.path, @t3.path],
"#{<<~"{#"}\n#{<<~'};'}")
{#
puts ARGF.gets.chomp + '.new' puts ARGF.gets.chomp + '.new'
puts ARGF.gets.chomp + '.new' puts ARGF.gets.chomp + '.new'
p $-i p $-i
@ -211,7 +223,7 @@ class TestArgf < Test::Unit::TestCase
p $-i p $-i
$-i = nil $-i = nil
puts ARGF.gets.chomp + '.new' puts ARGF.gets.chomp + '.new'
INPUT };
assert_equal("1.new\n2.new\n\".bak\"\n3.new\n4.new\nnil\n", File.read(@t1.path)) assert_equal("1.new\n2.new\n\".bak\"\n3.new\n4.new\nnil\n", File.read(@t1.path))
assert_equal("3\n4\n", File.read(@t2.path)) assert_equal("3\n4\n", File.read(@t2.path))
assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path)) assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path))
@ -223,12 +235,13 @@ class TestArgf < Test::Unit::TestCase
def test_inplace_rename_impossible def test_inplace_rename_impossible
t = make_tempfile t = make_tempfile
assert_in_out_err(["-", t.path], <<-INPUT) do |r, e| assert_in_out_err(["-", t.path], "#{<<~"{#"}\n#{<<~'};'}") do |r, e|
{#
ARGF.inplace_mode = '/\\\\:' ARGF.inplace_mode = '/\\\\:'
while line = ARGF.gets while line = ARGF.gets
puts line.chomp + '.new' puts line.chomp + '.new'
end end
INPUT };
assert_match(/Can't rename .* to .*: .*. skipping file/, e.first) #' assert_match(/Can't rename .* to .*: .*. skipping file/, e.first) #'
assert_equal([], r) assert_equal([], r)
assert_equal("foo\nbar\nbaz\n", File.read(t.path)) assert_equal("foo\nbar\nbaz\n", File.read(t.path))
@ -245,12 +258,13 @@ class TestArgf < Test::Unit::TestCase
def test_inplace_no_backup def test_inplace_no_backup
t = make_tempfile t = make_tempfile
assert_in_out_err(["-", t.path], <<-INPUT) do |r, e| assert_in_out_err(["-", t.path], "#{<<~"{#"}\n#{<<~'};'}") do |r, e|
{#
ARGF.inplace_mode = '' ARGF.inplace_mode = ''
while line = ARGF.gets while line = ARGF.gets
puts line.chomp + '.new' puts line.chomp + '.new'
end end
INPUT };
if no_safe_rename if no_safe_rename
assert_match(/Can't do inplace edit without backup/, e.join) #' assert_match(/Can't do inplace edit without backup/, e.join) #'
else else
@ -264,49 +278,54 @@ class TestArgf < Test::Unit::TestCase
def test_inplace_dup def test_inplace_dup
t = make_tempfile t = make_tempfile
assert_in_out_err(["-", t.path], <<-INPUT, [], []) assert_in_out_err(["-", t.path], "#{<<~"{#"}\n#{<<~'};'}", [], [])
{#
ARGF.inplace_mode = '.bak' ARGF.inplace_mode = '.bak'
f = ARGF.dup f = ARGF.dup
while line = f.gets while line = f.gets
puts line.chomp + '.new' puts line.chomp + '.new'
end end
INPUT };
assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path)) assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path))
end end
def test_inplace_stdin def test_inplace_stdin
assert_in_out_err(["-", "-"], <<-INPUT, [], /Can't do inplace edit for stdio; skipping/) assert_in_out_err(["-", "-"], "#{<<~"{#"}\n#{<<~'};'}", [], /Can't do inplace edit for stdio; skipping/)
{#
ARGF.inplace_mode = '.bak' ARGF.inplace_mode = '.bak'
f = ARGF.dup f = ARGF.dup
while line = f.gets while line = f.gets
puts line.chomp + '.new' puts line.chomp + '.new'
end end
INPUT };
end end
def test_inplace_stdin2 def test_inplace_stdin2
assert_in_out_err(["-"], <<-INPUT, [], /Can't do inplace edit for stdio/) assert_in_out_err(["-"], "#{<<~"{#"}\n#{<<~'};'}", [], /Can't do inplace edit for stdio/)
{#
ARGF.inplace_mode = '.bak' ARGF.inplace_mode = '.bak'
while line = ARGF.gets while line = ARGF.gets
puts line.chomp + '.new' puts line.chomp + '.new'
end end
INPUT };
end end
def test_encoding def test_encoding
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
p ARGF.external_encoding.is_a?(Encoding) p ARGF.external_encoding.is_a?(Encoding)
p ARGF.internal_encoding.is_a?(Encoding) p ARGF.internal_encoding.is_a?(Encoding)
ARGF.gets ARGF.gets
p ARGF.external_encoding.is_a?(Encoding) p ARGF.external_encoding.is_a?(Encoding)
p ARGF.internal_encoding p ARGF.internal_encoding
SRC };
assert_equal("true\ntrue\ntrue\nnil\n", f.read) assert_equal("true\ntrue\ntrue\nnil\n", f.read)
end end
end end
def test_tell def test_tell
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
begin begin
ARGF.binmode ARGF.binmode
loop do loop do
@ -316,7 +335,7 @@ class TestArgf < Test::Unit::TestCase
rescue ArgumentError rescue ArgumentError
puts "end" puts "end"
end end
SRC };
a = f.read.split("\n") a = f.read.split("\n")
[0, 2, 4, 2, 4, 2, 4].map {|i| i.to_s }. [0, 2, 4, 2, 4, 2, 4].map {|i| i.to_s }.
zip((1..6).map {|i| '"' + i.to_s + '\n"' } + ["nil"]).flatten. zip((1..6).map {|i| '"' + i.to_s + '\n"' } + ["nil"]).flatten.
@ -328,7 +347,8 @@ class TestArgf < Test::Unit::TestCase
end end
def test_seek def test_seek
assert_src_expected(__LINE__+1, <<-'SRC') assert_src_expected("#{<<~"{#"}\n#{<<~'};'}")
{#
ARGF.seek(4) ARGF.seek(4)
p ARGF.gets #=> "3\n" p ARGF.gets #=> "3\n"
ARGF.seek(0, IO::SEEK_END) ARGF.seek(0, IO::SEEK_END)
@ -340,11 +360,12 @@ class TestArgf < Test::Unit::TestCase
rescue rescue
puts "end" #=> end puts "end" #=> end
end end
SRC };
end end
def test_set_pos def test_set_pos
assert_src_expected(__LINE__+1, <<-'SRC') assert_src_expected("#{<<~"{#"}\n#{<<~'};'}")
{#
ARGF.pos = 4 ARGF.pos = 4
p ARGF.gets #=> "3\n" p ARGF.gets #=> "3\n"
ARGF.pos = 4 ARGF.pos = 4
@ -356,11 +377,12 @@ class TestArgf < Test::Unit::TestCase
rescue rescue
puts "end" #=> end puts "end" #=> end
end end
SRC };
end end
def test_rewind def test_rewind
assert_src_expected(__LINE__+1, <<-'SRC') assert_src_expected("#{<<~"{#"}\n#{<<~'};'}")
{#
ARGF.pos = 4 ARGF.pos = 4
ARGF.rewind ARGF.rewind
p ARGF.gets #=> "1\n" p ARGF.gets #=> "1\n"
@ -375,11 +397,12 @@ class TestArgf < Test::Unit::TestCase
rescue rescue
puts "end" #=> end puts "end" #=> end
end end
SRC };
end end
def test_fileno def test_fileno
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
p ARGF.fileno p ARGF.fileno
ARGF.gets ARGF.gets
ARGF.gets ARGF.gets
@ -396,7 +419,7 @@ class TestArgf < Test::Unit::TestCase
rescue rescue
puts "end" puts "end"
end end
SRC };
a = f.read.split("\n") a = f.read.split("\n")
fd1, fd2, fd3, fd4, tag = a fd1, fd2, fd3, fd4, tag = a
assert_match(/^\d+$/, fd1) assert_match(/^\d+$/, fd1)
@ -408,12 +431,13 @@ class TestArgf < Test::Unit::TestCase
end end
def test_to_io def test_to_io
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
8.times do 8.times do
p ARGF.to_io p ARGF.to_io
ARGF.gets ARGF.gets
end end
SRC };
a = f.read.split("\n") a = f.read.split("\n")
f11, f12, f13, f21, f22, f31, f32, f4 = a f11, f12, f13, f21, f22, f31, f32, f4 = a
assert_equal(f11, f12) assert_equal(f11, f12)
@ -427,7 +451,8 @@ class TestArgf < Test::Unit::TestCase
end end
def test_eof def test_eof
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
begin begin
8.times do 8.times do
p ARGF.eof? p ARGF.eof?
@ -436,7 +461,7 @@ class TestArgf < Test::Unit::TestCase
rescue IOError rescue IOError
puts "end" puts "end"
end end
SRC };
a = f.read.split("\n") a = f.read.split("\n")
(%w(false) + (%w(false true) * 3) + %w(end)).each do |x| (%w(false) + (%w(false true) * 3) + %w(end)).each do |x|
assert_equal(x, a.shift) assert_equal(x, a.shift)
@ -463,37 +488,41 @@ class TestArgf < Test::Unit::TestCase
end end
def test_read2 def test_read2
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = "" s = ""
ARGF.read(8, s) ARGF.read(8, s)
p s p s
SRC };
assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read) assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read)
end end
end end
def test_read2_with_not_empty_buffer def test_read2_with_not_empty_buffer
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = "0123456789" s = "0123456789"
ARGF.read(8, s) ARGF.read(8, s)
p s p s
SRC };
assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read) assert_equal("\"1\\n2\\n3\\n4\\n\"\n", f.read)
end end
end end
def test_read3 def test_read3
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
nil while ARGF.gets nil while ARGF.gets
p ARGF.read p ARGF.read
p ARGF.read(0, "") p ARGF.read(0, "")
SRC };
assert_equal("nil\n\"\"\n", f.read) assert_equal("nil\n\"\"\n", f.read)
end end
end end
def test_readpartial def test_readpartial
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = "" s = ""
begin begin
loop do loop do
@ -505,13 +534,14 @@ class TestArgf < Test::Unit::TestCase
rescue EOFError rescue EOFError
puts s puts s
end end
SRC };
assert_equal("1\n2\n3\n4\n5\n6\n", f.read) assert_equal("1\n2\n3\n4\n5\n6\n", f.read)
end end
end end
def test_readpartial2 def test_readpartial2
ruby('-e', <<-SRC) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}") do |f|
{#
s = "" s = ""
begin begin
loop do loop do
@ -522,7 +552,7 @@ class TestArgf < Test::Unit::TestCase
$stdout.binmode $stdout.binmode
puts s puts s
end end
SRC };
f.binmode f.binmode
f.puts("foo") f.puts("foo")
f.puts("bar") f.puts("bar")
@ -533,42 +563,46 @@ class TestArgf < Test::Unit::TestCase
end end
def test_readpartial_eof_twice def test_readpartial_eof_twice
ruby('-W1', '-e', <<-SRC, @t1.path) do |f| ruby('-W1', '-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path) do |f|
{#
$stderr = $stdout $stderr = $stdout
print ARGF.readpartial(256) print ARGF.readpartial(256)
ARGF.readpartial(256) rescue p($!.class) ARGF.readpartial(256) rescue p($!.class)
ARGF.readpartial(256) rescue p($!.class) ARGF.readpartial(256) rescue p($!.class)
SRC };
assert_equal("1\n2\nEOFError\nEOFError\n", f.read) assert_equal("1\n2\nEOFError\nEOFError\n", f.read)
end end
end end
def test_getc def test_getc
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = "" s = ""
while c = ARGF.getc while c = ARGF.getc
s << c s << c
end end
puts s puts s
SRC };
assert_equal("1\n2\n3\n4\n5\n6\n", f.read) assert_equal("1\n2\n3\n4\n5\n6\n", f.read)
end end
end end
def test_getbyte def test_getbyte
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = [] s = []
while c = ARGF.getbyte while c = ARGF.getbyte
s << c s << c
end end
p s p s
SRC };
assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read) assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read)
end end
end end
def test_readchar def test_readchar
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = "" s = ""
begin begin
while c = ARGF.readchar while c = ARGF.readchar
@ -577,13 +611,14 @@ class TestArgf < Test::Unit::TestCase
rescue EOFError rescue EOFError
puts s puts s
end end
SRC };
assert_equal("1\n2\n3\n4\n5\n6\n", f.read) assert_equal("1\n2\n3\n4\n5\n6\n", f.read)
end end
end end
def test_readbyte def test_readbyte
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
begin begin
s = [] s = []
while c = ARGF.readbyte while c = ARGF.readbyte
@ -592,17 +627,18 @@ class TestArgf < Test::Unit::TestCase
rescue EOFError rescue EOFError
p s p s
end end
SRC };
assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read) assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read)
end end
end end
def test_each_line def test_each_line
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = [] s = []
ARGF.each_line {|l| s << l } ARGF.each_line {|l| s << l }
p s p s
SRC };
assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read) assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read)
end end
end end
@ -613,32 +649,35 @@ class TestArgf < Test::Unit::TestCase
end end
def test_each_byte def test_each_byte
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = [] s = []
ARGF.each_byte {|c| s << c } ARGF.each_byte {|c| s << c }
p s p s
SRC };
assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read) assert_equal("[49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10]\n", f.read)
end end
end end
def test_each_char def test_each_char
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
s = "" s = ""
ARGF.each_char {|c| s << c } ARGF.each_char {|c| s << c }
puts s puts s
SRC };
assert_equal("1\n2\n3\n4\n5\n6\n", f.read) assert_equal("1\n2\n3\n4\n5\n6\n", f.read)
end end
end end
def test_filename def test_filename
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
begin begin
puts ARGF.filename.dump puts ARGF.filename.dump
end while ARGF.gets end while ARGF.gets
puts ARGF.filename.dump puts ARGF.filename.dump
SRC };
a = f.read.split("\n") a = f.read.split("\n")
assert_equal(@t1.path.dump, a.shift) assert_equal(@t1.path.dump, a.shift)
assert_equal(@t1.path.dump, a.shift) assert_equal(@t1.path.dump, a.shift)
@ -652,12 +691,13 @@ class TestArgf < Test::Unit::TestCase
end end
def test_filename2 def test_filename2
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
begin begin
puts $FILENAME.dump puts $FILENAME.dump
end while ARGF.gets end while ARGF.gets
puts $FILENAME.dump puts $FILENAME.dump
SRC };
a = f.read.split("\n") a = f.read.split("\n")
assert_equal(@t1.path.dump, a.shift) assert_equal(@t1.path.dump, a.shift)
assert_equal(@t1.path.dump, a.shift) assert_equal(@t1.path.dump, a.shift)
@ -671,12 +711,13 @@ class TestArgf < Test::Unit::TestCase
end end
def test_file def test_file
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
begin begin
puts ARGF.file.path.dump puts ARGF.file.path.dump
end while ARGF.gets end while ARGF.gets
puts ARGF.file.path.dump puts ARGF.file.path.dump
SRC };
a = f.read.split("\n") a = f.read.split("\n")
assert_equal(@t1.path.dump, a.shift) assert_equal(@t1.path.dump, a.shift)
assert_equal(@t1.path.dump, a.shift) assert_equal(@t1.path.dump, a.shift)
@ -708,33 +749,37 @@ class TestArgf < Test::Unit::TestCase
end unless IO::BINARY.zero? end unless IO::BINARY.zero?
def test_skip def test_skip
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
ARGF.skip ARGF.skip
puts ARGF.gets puts ARGF.gets
ARGF.skip ARGF.skip
puts ARGF.read puts ARGF.read
SRC };
assert_equal("1\n3\n4\n5\n6\n", f.read) assert_equal("1\n3\n4\n5\n6\n", f.read)
end end
end end
def test_skip_in_each_line def test_skip_in_each_line
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
ARGF.each_line {|l| print l; ARGF.skip} ARGF.each_line {|l| print l; ARGF.skip}
SRC };
assert_equal("1\n3\n5\n", f.read, '[ruby-list:49185]') assert_equal("1\n3\n5\n", f.read, '[ruby-list:49185]')
end end
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
ARGF.each_line {|l| ARGF.skip; puts [l, ARGF.gets].map {|s| s ? s.chomp : s.inspect}.join("+")} ARGF.each_line {|l| ARGF.skip; puts [l, ARGF.gets].map {|s| s ? s.chomp : s.inspect}.join("+")}
SRC };
assert_equal("1+3\n4+5\n6+nil\n", f.read, '[ruby-list:49185]') assert_equal("1+3\n4+5\n6+nil\n", f.read, '[ruby-list:49185]')
end end
end end
def test_skip_in_each_byte def test_skip_in_each_byte
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
ARGF.each_byte {|l| print l; ARGF.skip} ARGF.each_byte {|l| print l; ARGF.skip}
SRC };
assert_equal("135".unpack("C*").join(""), f.read, '[ruby-list:49185]') assert_equal("135".unpack("C*").join(""), f.read, '[ruby-list:49185]')
end end
end end
@ -743,9 +788,10 @@ class TestArgf < Test::Unit::TestCase
[[@t1, "\u{3042}"], [@t2, "\u{3044}"], [@t3, "\u{3046}"]].each do |f, s| [[@t1, "\u{3042}"], [@t2, "\u{3044}"], [@t3, "\u{3046}"]].each do |f, s|
File.write(f.path, s, mode: "w:utf-8") File.write(f.path, s, mode: "w:utf-8")
end end
ruby('-Eutf-8', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-Eutf-8', '-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
ARGF.each_char {|l| print l; ARGF.skip} ARGF.each_char {|l| print l; ARGF.skip}
SRC };
assert_equal("\u{3042 3044 3046}", f.read, '[ruby-list:49185]') assert_equal("\u{3042 3044 3046}", f.read, '[ruby-list:49185]')
end end
end end
@ -754,34 +800,39 @@ class TestArgf < Test::Unit::TestCase
[[@t1, "\u{3042}"], [@t2, "\u{3044}"], [@t3, "\u{3046}"]].each do |f, s| [[@t1, "\u{3042}"], [@t2, "\u{3044}"], [@t3, "\u{3046}"]].each do |f, s|
File.write(f.path, s, mode: "w:utf-8") File.write(f.path, s, mode: "w:utf-8")
end end
ruby('-Eutf-8', '-Eutf-8', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-Eutf-8', '-Eutf-8', '-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
ARGF.each_codepoint {|l| printf "%x:", l; ARGF.skip} ARGF.each_codepoint {|l| printf "%x:", l; ARGF.skip}
SRC };
assert_equal("3042:3044:3046:", f.read, '[ruby-list:49185]') assert_equal("3042:3044:3046:", f.read, '[ruby-list:49185]')
end end
end end
def test_close def test_close
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
ARGF.close ARGF.close
puts ARGF.read puts ARGF.read
SRC };
assert_equal("3\n4\n5\n6\n", f.read) assert_equal("3\n4\n5\n6\n", f.read)
end end
end end
def test_close_replace def test_close_replace
ruby('-e', <<-SRC) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}") do |f|
paths = ['#{@t1.path}', '#{@t2.path}', '#{@t3.path}']
{#
ARGF.close ARGF.close
ARGV.replace ['#{@t1.path}', '#{@t2.path}', '#{@t3.path}'] ARGV.replace paths
puts ARGF.read puts ARGF.read
SRC };
assert_equal("1\n2\n3\n4\n5\n6\n", f.read) assert_equal("1\n2\n3\n4\n5\n6\n", f.read)
end end
end end
def test_closed def test_closed
ruby('-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
3.times do 3.times do
p ARGF.closed? p ARGF.closed?
ARGF.gets ARGF.gets
@ -790,7 +841,7 @@ class TestArgf < Test::Unit::TestCase
p ARGF.closed? p ARGF.closed?
ARGF.gets ARGF.gets
p ARGF.closed? p ARGF.closed?
SRC };
assert_equal("false\nfalse\nfalse\nfalse\ntrue\n", f.read) assert_equal("false\nfalse\nfalse\nfalse\ntrue\n", f.read)
end end
end end
@ -849,49 +900,54 @@ class TestArgf < Test::Unit::TestCase
end end
def test_lines def test_lines
ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-W1', '-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
$stderr = $stdout $stderr = $stdout
s = [] s = []
ARGF.lines {|l| s << l } ARGF.lines {|l| s << l }
p s p s
SRC };
assert_match(/deprecated/, f.gets) assert_match(/deprecated/, f.gets)
assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read) assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read)
end end
end end
def test_bytes def test_bytes
ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-W1', '-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
$stderr = $stdout $stderr = $stdout
print Marshal.dump(ARGF.bytes.to_a) print Marshal.dump(ARGF.bytes.to_a)
SRC };
assert_match(/deprecated/, f.gets) assert_match(/deprecated/, f.gets)
assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read))
end end
end end
def test_chars def test_chars
ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-W1', '-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
$stderr = $stdout $stderr = $stdout
print [Marshal.dump(ARGF.chars.to_a)].pack('m') print [Marshal.dump(ARGF.chars.to_a)].pack('m')
SRC };
assert_match(/deprecated/, f.gets) assert_match(/deprecated/, f.gets)
assert_equal(["1", "\n", "2", "\n", "3", "\n", "4", "\n", "5", "\n", "6", "\n"], Marshal.load(f.read.unpack('m').first)) assert_equal(["1", "\n", "2", "\n", "3", "\n", "4", "\n", "5", "\n", "6", "\n"], Marshal.load(f.read.unpack('m').first))
end end
end end
def test_codepoints def test_codepoints
ruby('-W1', '-e', <<-SRC, @t1.path, @t2.path, @t3.path) do |f| ruby('-W1', '-e', "#{<<~"{#"}\n#{<<~'};'}", @t1.path, @t2.path, @t3.path) do |f|
{#
$stderr = $stdout $stderr = $stdout
print Marshal.dump(ARGF.codepoints.to_a) print Marshal.dump(ARGF.codepoints.to_a)
SRC };
assert_match(/deprecated/, f.gets) assert_match(/deprecated/, f.gets)
assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read))
end end
end end
def test_read_nonblock def test_read_nonblock
ruby('-e', <<-SRC) do |f| ruby('-e', "#{<<~"{#"}\n#{<<~'};'}") do |f|
{#
$stdout.sync = true $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" abort "did not return :wait_readable"
@ -923,7 +979,7 @@ class TestArgf < Test::Unit::TestCase
rescue EOFError rescue EOFError
puts 'done with eof' puts 'done with eof'
end end
SRC };
f.sync = true f.sync = true
assert_equal "starting select\n", f.gets assert_equal "starting select\n", f.gets
f.write('.') # wake up from IO.select f.write('.') # wake up from IO.select
@ -934,11 +990,11 @@ class TestArgf < Test::Unit::TestCase
end end
def test_wrong_type def test_wrong_type
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") assert_separately([], "#{<<~"{#"}\n#{<<~'};'}")
begin; {#
bug11610 = '[ruby-core:71140] [Bug #11610]' bug11610 = '[ruby-core:71140] [Bug #11610]'
ARGV[0] = nil ARGV[0] = nil
assert_raise(TypeError, bug11610) {gets} assert_raise(TypeError, bug11610) {gets}
end; };
end end
end end