зеркало из https://github.com/github/ruby.git
refine previous change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13554 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
Родитель
a9c2a18cc7
Коммит
4dc8ff965b
|
@ -15,48 +15,50 @@ class TestSystem < Test::Unit::TestCase
|
||||||
assert_equal("foobar\n", `echo foobar`)
|
assert_equal("foobar\n", `echo foobar`)
|
||||||
assert_equal('foobar', `#{ruby} -e 'print "foobar"'`)
|
assert_equal('foobar', `#{ruby} -e 'print "foobar"'`)
|
||||||
|
|
||||||
tmpfilename = "#{Dir.tmpdir}/ruby_script_tmp.#{$$}"
|
Dir.mktmpdir("ruby_script_tmp") {|tmpdir|
|
||||||
|
tmpfilename = "#{tmpdir}/ruby_script_tmp.#{$$}"
|
||||||
|
|
||||||
tmp = open(tmpfilename, "w")
|
tmp = open(tmpfilename, "w")
|
||||||
tmp.print "print $zzz\n";
|
tmp.print "print $zzz\n";
|
||||||
tmp.close
|
tmp.close
|
||||||
|
|
||||||
assert_equal('true', `#{ruby} -s #{tmpfilename} -zzz`)
|
assert_equal('true', `#{ruby} -s #{tmpfilename} -zzz`)
|
||||||
assert_equal('555', `#{ruby} -s #{tmpfilename} -zzz=555`)
|
assert_equal('555', `#{ruby} -s #{tmpfilename} -zzz=555`)
|
||||||
|
|
||||||
tmp = open(tmpfilename, "w")
|
tmp = open(tmpfilename, "w")
|
||||||
tmp.print "#! /usr/local/bin/ruby -s\n";
|
tmp.print "#! /usr/local/bin/ruby -s\n";
|
||||||
tmp.print "print $zzz\n";
|
tmp.print "print $zzz\n";
|
||||||
tmp.close
|
tmp.close
|
||||||
|
|
||||||
assert_equal('678', `#{ruby} #{tmpfilename} -zzz=678`)
|
assert_equal('678', `#{ruby} #{tmpfilename} -zzz=678`)
|
||||||
|
|
||||||
tmp = open(tmpfilename, "w")
|
tmp = open(tmpfilename, "w")
|
||||||
tmp.print "this is a leading junk\n";
|
tmp.print "this is a leading junk\n";
|
||||||
tmp.print "#! /usr/local/bin/ruby -s\n";
|
tmp.print "#! /usr/local/bin/ruby -s\n";
|
||||||
tmp.print "print $zzz\n";
|
tmp.print "print $zzz\n";
|
||||||
tmp.print "__END__\n";
|
tmp.print "__END__\n";
|
||||||
tmp.print "this is a trailing junk\n";
|
tmp.print "this is a trailing junk\n";
|
||||||
tmp.close
|
tmp.close
|
||||||
|
|
||||||
assert_equal('', `#{ruby} -x #{tmpfilename}`)
|
assert_equal('', `#{ruby} -x #{tmpfilename}`)
|
||||||
assert_equal('555', `#{ruby} -x #{tmpfilename} -zzz=555`)
|
assert_equal('555', `#{ruby} -x #{tmpfilename} -zzz=555`)
|
||||||
|
|
||||||
tmp = open(tmpfilename, "w")
|
tmp = open(tmpfilename, "w")
|
||||||
for i in 1..5
|
for i in 1..5
|
||||||
tmp.print i, "\n"
|
tmp.print i, "\n"
|
||||||
end
|
end
|
||||||
tmp.close
|
tmp.close
|
||||||
|
|
||||||
`#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' #{tmpfilename}`
|
`#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' #{tmpfilename}`
|
||||||
tmp = open(tmpfilename, "r")
|
tmp = open(tmpfilename, "r")
|
||||||
while tmp.gets
|
while tmp.gets
|
||||||
assert_equal(0, $_.to_i % 5)
|
assert_equal(0, $_.to_i % 5)
|
||||||
end
|
end
|
||||||
tmp.close
|
tmp.close
|
||||||
|
|
||||||
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
|
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
|
||||||
File.unlink "#{tmpfilename}.bak" or `/bin/rm -f "#{tmpfilename}.bak"`
|
File.unlink "#{tmpfilename}.bak" or `/bin/rm -f "#{tmpfilename}.bak"`
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_syntax
|
def test_syntax
|
||||||
|
|
|
@ -3,71 +3,73 @@ require 'tmpdir'
|
||||||
|
|
||||||
class TestWhileuntil < Test::Unit::TestCase
|
class TestWhileuntil < Test::Unit::TestCase
|
||||||
def test_while
|
def test_while
|
||||||
tmpfilename = "#{Dir.tmpdir}/ruby_while_tmp.#{$$}"
|
Dir.mktmpdir("ruby_while_tmp") {|tmpdir|
|
||||||
|
tmpfilename = "#{tmpdir}/ruby_while_tmp.#{$$}"
|
||||||
|
|
||||||
tmp = open(tmpfilename, "w")
|
tmp = open(tmpfilename, "w")
|
||||||
tmp.print "tvi925\n";
|
tmp.print "tvi925\n";
|
||||||
tmp.print "tvi920\n";
|
tmp.print "tvi920\n";
|
||||||
tmp.print "vt100\n";
|
tmp.print "vt100\n";
|
||||||
tmp.print "Amiga\n";
|
tmp.print "Amiga\n";
|
||||||
tmp.print "paper\n";
|
tmp.print "paper\n";
|
||||||
tmp.close
|
tmp.close
|
||||||
|
|
||||||
tmp = open(tmpfilename, "r")
|
tmp = open(tmpfilename, "r")
|
||||||
assert_instance_of(File, tmp)
|
assert_instance_of(File, tmp)
|
||||||
|
|
||||||
while line = tmp.gets()
|
while line = tmp.gets()
|
||||||
break if /vt100/ =~ line
|
break if /vt100/ =~ line
|
||||||
end
|
|
||||||
|
|
||||||
assert(!tmp.eof?)
|
|
||||||
assert_match(/vt100/, line)
|
|
||||||
tmp.close
|
|
||||||
|
|
||||||
tmp = open(tmpfilename, "r")
|
|
||||||
while line = tmp.gets()
|
|
||||||
next if /vt100/ =~ line
|
|
||||||
assert_no_match(/vt100/, line)
|
|
||||||
end
|
|
||||||
assert(tmp.eof?)
|
|
||||||
assert_no_match(/vt100/, line)
|
|
||||||
tmp.close
|
|
||||||
|
|
||||||
tmp = open(tmpfilename, "r")
|
|
||||||
while line = tmp.gets()
|
|
||||||
lastline = line
|
|
||||||
line = line.gsub(/vt100/, 'VT100')
|
|
||||||
if lastline != line
|
|
||||||
line.gsub!('VT100', 'Vt100')
|
|
||||||
redo
|
|
||||||
end
|
end
|
||||||
assert_no_match(/vt100/, line)
|
|
||||||
assert_no_match(/VT100/, line)
|
|
||||||
end
|
|
||||||
assert(tmp.eof?)
|
|
||||||
tmp.close
|
|
||||||
|
|
||||||
sum=0
|
assert(!tmp.eof?)
|
||||||
for i in 1..10
|
assert_match(/vt100/, line)
|
||||||
sum += i
|
tmp.close
|
||||||
i -= 1
|
|
||||||
if i > 0
|
tmp = open(tmpfilename, "r")
|
||||||
redo
|
while line = tmp.gets()
|
||||||
|
next if /vt100/ =~ line
|
||||||
|
assert_no_match(/vt100/, line)
|
||||||
end
|
end
|
||||||
end
|
assert(tmp.eof?)
|
||||||
assert_equal(220, sum)
|
|
||||||
|
|
||||||
tmp = open(tmpfilename, "r")
|
|
||||||
while line = tmp.gets()
|
|
||||||
break if 3
|
|
||||||
assert_no_match(/vt100/, line)
|
assert_no_match(/vt100/, line)
|
||||||
assert_no_match(/Amiga/, line)
|
tmp.close
|
||||||
assert_no_match(/paper/, line)
|
|
||||||
end
|
|
||||||
tmp.close
|
|
||||||
|
|
||||||
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
|
tmp = open(tmpfilename, "r")
|
||||||
assert(!File.exist?(tmpfilename))
|
while line = tmp.gets()
|
||||||
|
lastline = line
|
||||||
|
line = line.gsub(/vt100/, 'VT100')
|
||||||
|
if lastline != line
|
||||||
|
line.gsub!('VT100', 'Vt100')
|
||||||
|
redo
|
||||||
|
end
|
||||||
|
assert_no_match(/vt100/, line)
|
||||||
|
assert_no_match(/VT100/, line)
|
||||||
|
end
|
||||||
|
assert(tmp.eof?)
|
||||||
|
tmp.close
|
||||||
|
|
||||||
|
sum=0
|
||||||
|
for i in 1..10
|
||||||
|
sum += i
|
||||||
|
i -= 1
|
||||||
|
if i > 0
|
||||||
|
redo
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal(220, sum)
|
||||||
|
|
||||||
|
tmp = open(tmpfilename, "r")
|
||||||
|
while line = tmp.gets()
|
||||||
|
break if 3
|
||||||
|
assert_no_match(/vt100/, line)
|
||||||
|
assert_no_match(/Amiga/, line)
|
||||||
|
assert_no_match(/paper/, line)
|
||||||
|
end
|
||||||
|
tmp.close
|
||||||
|
|
||||||
|
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
|
||||||
|
assert(!File.exist?(tmpfilename))
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_until
|
def test_until
|
||||||
|
|
|
@ -297,7 +297,7 @@ class TestIOScanf
|
||||||
include Scanf
|
include Scanf
|
||||||
extend ScanfTests
|
extend ScanfTests
|
||||||
|
|
||||||
tmpfilename = "#{Dir.tmpdir}/iotest.dat"
|
tmpfilename = "#{Dir.tmpdir}/iotest.dat.#{$$}"
|
||||||
|
|
||||||
i = 1
|
i = 1
|
||||||
self.tests.each do |test|
|
self.tests.each do |test|
|
||||||
|
|
|
@ -50,8 +50,9 @@ alias set_up setup
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_io1
|
def test_io1
|
||||||
File.open("#{Dir.tmpdir}/iotest.dat", "w") { |fh| fh.puts(@str) }
|
fn = "#{Dir.tmpdir}/iotest.dat.#{$$}"
|
||||||
fh = File.open("#{Dir.tmpdir}/iotest.dat", "rb")
|
File.open(fn, "w") { |fh| fh.puts(@str) }
|
||||||
|
fh = File.open(fn, "rb")
|
||||||
res = fh.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }
|
res = fh.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }
|
||||||
|
|
||||||
assert_equal(
|
assert_equal(
|
||||||
|
@ -62,18 +63,19 @@ alias set_up setup
|
||||||
"Brahms was born in 1833." ],res)
|
"Brahms was born in 1833." ],res)
|
||||||
fh.close
|
fh.close
|
||||||
ensure
|
ensure
|
||||||
File.delete("#{Dir.tmpdir}/iotest.dat")
|
File.delete(fn)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_io2
|
def test_io2
|
||||||
File.open("#{Dir.tmpdir}/iotest.dat", "w").close
|
fn = "#{Dir.tmpdir}/iotest.dat.#{$$}"
|
||||||
fh = File.open("#{Dir.tmpdir}/iotest.dat","rb")
|
File.open(fn, "w").close
|
||||||
|
fh = File.open(fn,"rb")
|
||||||
assert_equal(fh.scanf("") {}, [])
|
assert_equal(fh.scanf("") {}, [])
|
||||||
fh.seek(0)
|
fh.seek(0)
|
||||||
assert_equal(fh.scanf("%d%f%s") {}, [])
|
assert_equal(fh.scanf("%d%f%s") {}, [])
|
||||||
fh.close
|
fh.close
|
||||||
ensure
|
ensure
|
||||||
File.delete("#{Dir.tmpdir}/iotest.dat")
|
File.delete(fn)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Загрузка…
Ссылка в новой задаче