summaryrefslogtreecommitdiff
path: root/test/ruby/test_argf.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_argf.rb')
-rw-r--r--test/ruby/test_argf.rb213
1 files changed, 95 insertions, 118 deletions
diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb
index 63b278198b..eb5556b98a 100644
--- a/test/ruby/test_argf.rb
+++ b/test/ruby/test_argf.rb
@@ -131,92 +131,83 @@ class TestArgf < Test::Unit::TestCase
end
def test_inplace
- EnvUtil.rubyexec("-", @t1.path, @t2.path, @t3.path) do |w, r, e|
- w.puts "ARGF.inplace_mode = '.bak'"
- w.puts "while line = ARGF.gets"
- w.puts " puts line.chomp + '.new'"
- w.puts "end"
- w.close
- assert_equal("", e.read)
- assert_equal("", r.read)
- assert_equal("1.new\n2.new\n", File.read(@t1.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("1\n2\n", File.read(@t1.path + ".bak"))
- assert_equal("3\n4\n", File.read(@t2.path + ".bak"))
- assert_equal("5\n6\n", File.read(@t3.path + ".bak"))
- end
+ assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], [])
+ ARGF.inplace_mode = '.bak'
+ while line = ARGF.gets
+ puts line.chomp + '.new'
+ end
+ INPUT
+ assert_equal("1.new\n2.new\n", File.read(@t1.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("1\n2\n", File.read(@t1.path + ".bak"))
+ assert_equal("3\n4\n", File.read(@t2.path + ".bak"))
+ assert_equal("5\n6\n", File.read(@t3.path + ".bak"))
end
def test_inplace2
- EnvUtil.rubyexec("-", @t1.path, @t2.path, @t3.path) do |w, r, e|
- w.puts "ARGF.inplace_mode = '.bak'"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "p ARGF.inplace_mode"
- w.puts "ARGF.inplace_mode = nil"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "p ARGF.inplace_mode"
- w.puts "ARGF.inplace_mode = '.bak'"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "p ARGF.inplace_mode"
- w.puts "ARGF.inplace_mode = nil"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.close
- assert_equal("", e.read)
- assert_equal("", r.read)
- 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("5.new\n\".bak\"\n6.new\n", File.read(@t3.path))
- assert_equal("1\n2\n", File.read(@t1.path + ".bak"))
- assert_equal(false, File.file?(@t2.path + ".bak"))
- assert_equal("5\n6\n", File.read(@t3.path + ".bak"))
- end
+ assert_in_out_err(["-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], [])
+ ARGF.inplace_mode = '.bak'
+ puts ARGF.gets.chomp + '.new'
+ puts ARGF.gets.chomp + '.new'
+ p ARGF.inplace_mode
+ ARGF.inplace_mode = nil
+ puts ARGF.gets.chomp + '.new'
+ puts ARGF.gets.chomp + '.new'
+ p ARGF.inplace_mode
+ ARGF.inplace_mode = '.bak'
+ puts ARGF.gets.chomp + '.new'
+ p ARGF.inplace_mode
+ ARGF.inplace_mode = nil
+ 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("3\n4\n", File.read(@t2.path))
+ assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path))
+ assert_equal("1\n2\n", File.read(@t1.path + ".bak"))
+ assert_equal(false, File.file?(@t2.path + ".bak"))
+ assert_equal("5\n6\n", File.read(@t3.path + ".bak"))
end
def test_inplace3
- EnvUtil.rubyexec("-i.bak", "-", @t1.path, @t2.path, @t3.path) do |w, r, e|
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "p $-i"
- w.puts "$-i = nil"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "p $-i"
- w.puts "$-i = '.bak'"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.puts "p $-i"
- w.puts "$-i = nil"
- w.puts "puts ARGF.gets.chomp + '.new'"
- w.close
- assert_equal("", e.read)
- assert_equal("", r.read)
- 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("5.new\n\".bak\"\n6.new\n", File.read(@t3.path))
- assert_equal("1\n2\n", File.read(@t1.path + ".bak"))
- assert_equal(false, File.file?(@t2.path + ".bak"))
- assert_equal("5\n6\n", File.read(@t3.path + ".bak"))
- end
+ assert_in_out_err(["-i.bak", "-", @t1.path, @t2.path, @t3.path], <<-INPUT, [], [])
+ puts ARGF.gets.chomp + '.new'
+ puts ARGF.gets.chomp + '.new'
+ p $-i
+ $-i = nil
+ puts ARGF.gets.chomp + '.new'
+ puts ARGF.gets.chomp + '.new'
+ p $-i
+ $-i = '.bak'
+ puts ARGF.gets.chomp + '.new'
+ p $-i
+ $-i = nil
+ 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("3\n4\n", File.read(@t2.path))
+ assert_equal("5.new\n\".bak\"\n6.new\n", File.read(@t3.path))
+ assert_equal("1\n2\n", File.read(@t1.path + ".bak"))
+ assert_equal(false, File.file?(@t2.path + ".bak"))
+ assert_equal("5\n6\n", File.read(@t3.path + ".bak"))
end
def test_inplace_rename_impossible
t = make_tempfile
- EnvUtil.rubyexec("-", t.path) do |w, r, e|
- w.puts "ARGF.inplace_mode = '/\\\\'"
- w.puts "while line = ARGF.gets"
- w.puts " puts line.chomp + '.new'"
- w.puts "end"
- w.close
+ assert_in_out_err(["-", t.path], <<-INPUT) do |r, e|
+ ARGF.inplace_mode = '/\\\\'
+ while line = ARGF.gets
+ puts line.chomp + '.new'
+ end
+ INPUT
if no_safe_rename
- assert_equal("", e.read)
- assert_equal("", r.read)
+ assert_equal([], e)
+ assert_equal([], r)
assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path))
else
- assert_match(/Can't rename .* to .*: .*. skipping file/, e.read) #'
- assert_equal("", r.read)
+ assert_match(/Can't rename .* to .*: .*. skipping file/, e.first) #'
+ assert_equal([], r)
assert_equal("foo\nbar\nbaz\n", File.read(t.path))
end
end
@@ -225,17 +216,17 @@ class TestArgf < Test::Unit::TestCase
def test_inplace_no_backup
t = make_tempfile
- EnvUtil.rubyexec("-", t.path) do |w, r, e|
- w.puts "ARGF.inplace_mode = ''"
- w.puts "while line = ARGF.gets"
- w.puts " puts line.chomp + '.new'"
- w.puts "end"
- w.close
+ assert_in_out_err(["-", t.path], <<-INPUT) do |r, e|
+ ARGF.inplace_mode = ''
+ while line = ARGF.gets
+ puts line.chomp + '.new'
+ end
+ INPUT
if no_safe_rename
- assert_match(/Can't do inplace edit without backup/, e.read) #'
+ assert_match(/Can't do inplace edit without backup/, e.join) #'
else
- assert_equal("", e.read)
- assert_equal("", r.read)
+ assert_equal([], e)
+ assert_equal([], r)
assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path))
end
end
@@ -244,46 +235,37 @@ class TestArgf < Test::Unit::TestCase
def test_inplace_dup
t = make_tempfile
- EnvUtil.rubyexec("-", t.path) do |w, r, e|
- w.puts "ARGF.inplace_mode = '.bak'"
- w.puts "f = ARGF.dup"
- w.puts "while line = f.gets"
- w.puts " puts line.chomp + '.new'"
- w.puts "end"
- w.close
- assert_equal("", e.read)
- assert_equal("", r.read)
- assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path))
- end
+ assert_in_out_err(["-", t.path], <<-INPUT, [], [])
+ ARGF.inplace_mode = '.bak'
+ f = ARGF.dup
+ while line = f.gets
+ puts line.chomp + '.new'
+ end
+ INPUT
+ assert_equal("foo.new\nbar.new\nbaz.new\n", File.read(t.path))
end
def test_inplace_stdin
t = make_tempfile
- EnvUtil.rubyexec("-", "-") do |w, r, e|
- w.puts "ARGF.inplace_mode = '.bak'"
- w.puts "f = ARGF.dup"
- w.puts "while line = f.gets"
- w.puts " puts line.chomp + '.new'"
- w.puts "end"
- w.close
- assert_match("Can't do inplace edit for stdio; skipping", e.read)
- assert_equal("", r.read)
- end
+ assert_in_out_err(["-", "-"], <<-INPUT, [], /Can't do inplace edit for stdio; skipping/)
+ ARGF.inplace_mode = '.bak'
+ f = ARGF.dup
+ while line = f.gets
+ puts line.chomp + '.new'
+ end
+ INPUT
end
def test_inplace_stdin2
t = make_tempfile
- EnvUtil.rubyexec("-") do |w, r, e|
- w.puts "ARGF.inplace_mode = '.bak'"
- w.puts "while line = ARGF.gets"
- w.puts " puts line.chomp + '.new'"
- w.puts "end"
- w.close
- assert_match("Can't do inplace edit for stdio", e.read)
- assert_equal("", r.read)
- end
+ assert_in_out_err(["-"], <<-INPUT, [], /Can't do inplace edit for stdio/)
+ ARGF.inplace_mode = '.bak'
+ while line = ARGF.gets
+ puts line.chomp + '.new'
+ end
+ INPUT
end
def test_encoding
@@ -583,13 +565,8 @@ class TestArgf < Test::Unit::TestCase
end
def test_each_line_paragraph
- EnvUtil.rubyexec('-e', 'ARGF.each_line("") {|para| p para}') do |w, r, e|
- w << "a\n\nb\n"
- w.close
- assert_equal("\"a\\n\\n\"\n", r.gets, "[ruby-dev:34958]")
- assert_equal("\"b\\n\"\n", r.gets)
- assert_equal(nil, r.gets)
- end
+ assert_in_out_err(['-e', 'ARGF.each_line("") {|para| p para}'], "a\n\nb\n",
+ ["\"a\\n\\n\"", "\"b\\n\""], [])
end
def test_each_byte