diff options
Diffstat (limited to 'test/ruby/test_whileuntil.rb')
| -rw-r--r-- | test/ruby/test_whileuntil.rb | 132 |
1 files changed, 78 insertions, 54 deletions
diff --git a/test/ruby/test_whileuntil.rb b/test/ruby/test_whileuntil.rb index 6faed31160..ff6d29ac4a 100644 --- a/test/ruby/test_whileuntil.rb +++ b/test/ruby/test_whileuntil.rb @@ -1,70 +1,94 @@ +# frozen_string_literal: false require 'test/unit' +require 'tmpdir' class TestWhileuntil < Test::Unit::TestCase def test_while - tmp = open("while_tmp", "w") - tmp.print "tvi925\n"; - tmp.print "tvi920\n"; - tmp.print "vt100\n"; - tmp.print "Amiga\n"; - tmp.print "paper\n"; - tmp.close + Dir.mktmpdir("ruby_while_tmp") {|tmpdir| + tmpfilename = "#{tmpdir}/ruby_while_tmp.#{$$}" - tmp = open("while_tmp", "r") - assert_instance_of(File, tmp) - - while line = tmp.gets() - break if /vt100/ =~ line - end + tmp = open(tmpfilename, "w") + tmp.print "tvi925\n"; + tmp.print "tvi920\n"; + tmp.print "vt100\n"; + tmp.print "Amiga\n"; + tmp.print "paper\n"; + tmp.close + + tmp = open(tmpfilename, "r") + assert_instance_of(File, tmp) - assert(!tmp.eof?) - assert_match(/vt100/, line) - tmp.close + while line = tmp.gets() + break if /vt100/ =~ line + end - tmp = open("while_tmp", "r") - while line = tmp.gets() - next if /vt100/ =~ line + assert_not_predicate(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_predicate(tmp, :eof?) assert_no_match(/vt100/, line) - end - assert(tmp.eof?) - assert_no_match(/vt100/, line) - tmp.close + tmp.close - tmp = open("while_tmp", "r") - while tmp.gets() - line = $_ - gsub(/vt100/, 'VT100') - if $_ != line - $_.gsub!('VT100', 'Vt100') - redo + tmp = open(tmpfilename, "r") + 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_no_match(/vt100/, $_) - assert_no_match(/VT100/, $_) - end - assert(tmp.eof?) - tmp.close + assert_predicate(tmp, :eof?) + tmp.close - sum=0 - for i in 1..10 - sum += i - i -= 1 - if i > 0 - redo + sum=0 + for i in 1..10 + sum += i + i -= 1 + if i > 0 + redo + end end - end - assert_equal(220, sum) + assert_equal(220, sum) - tmp = open("while_tmp", "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 + 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 "while_tmp" or `/bin/rm -f "while_tmp"` - assert(!File.exist?("while_tmp")) + File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"` + assert_file.not_exist?(tmpfilename) + } + end + + def test_begin_while + i = 0 + sum = 0 + begin + i += 1 + sum += i + end while i < 10 + assert_equal([10, 55], [i, sum]) + + i = 0 + sum = 0 + ( + i += 1 + sum += i + ) while false + assert_equal([0, 0], [i, sum]) end def test_until @@ -72,6 +96,6 @@ class TestWhileuntil < Test::Unit::TestCase until i>4 i+=1 end - assert(i>4) + assert_operator(i, :>, 4) end end |
