1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

use local variables

* test/ruby/test_eval.rb: use local variables instead global variables
  if possible.
* test/ruby/test_ifunless.rb: ditto.
* test/ruby/test_iterator.rb: ditto.
* test/ruby/test_stringchar.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36662 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-08-08 13:46:09 +00:00
parent 22e145141b
commit 6b4ecb32fb
4 changed files with 62 additions and 62 deletions

View file

@ -229,9 +229,9 @@ class TestEval < Test::Unit::TestCase
def test_eval_orig def test_eval_orig
assert_nil(eval("")) assert_nil(eval(""))
$bad=false bad=false
eval 'while false; $bad = true; print "foo\n" end' eval 'while false; bad = true; print "foo\n" end'
assert(!$bad) assert(!bad)
assert(eval('TRUE')) assert(eval('TRUE'))
assert(eval('true')) assert(eval('true'))
@ -254,34 +254,34 @@ class TestEval < Test::Unit::TestCase
assert_equal(5, eval("i")) assert_equal(5, eval("i"))
assert(eval("defined? i")) assert(eval("defined? i"))
$x = test_ev x = test_ev
assert_equal("local1", eval("local1", $x)) # normal local var assert_equal("local1", eval("local1", x)) # normal local var
assert_equal("local2", eval("local2", $x)) # nested local var assert_equal("local2", eval("local2", x)) # nested local var
$bad = true bad = true
begin begin
p eval("local1") p eval("local1")
rescue NameError # must raise error rescue NameError # must raise error
$bad = false bad = false
end end
assert(!$bad) assert(!bad)
# !! use class_eval to avoid nested definition # !! use class_eval to avoid nested definition
self.class.class_eval %q( x = self.class.class_eval %q(
module EvTest module EvTest
EVTEST1 = 25 EVTEST1 = 25
evtest2 = 125 evtest2 = 125
$x = binding binding
end end
) )
assert_equal(25, eval("EVTEST1", $x)) # constant in module assert_equal(25, eval("EVTEST1", x)) # constant in module
assert_equal(125, eval("evtest2", $x)) # local var in module assert_equal(125, eval("evtest2", x)) # local var in module
$bad = true bad = true
begin begin
eval("EVTEST1") eval("EVTEST1")
rescue NameError # must raise error rescue NameError # must raise error
$bad = false bad = false
end end
assert(!$bad) assert(!bad)
if false if false
# Ruby 2.0 doesn't see Proc as Binding # Ruby 2.0 doesn't see Proc as Binding
@ -291,10 +291,10 @@ class TestEval < Test::Unit::TestCase
x = proc{proc{}}.call x = proc{proc{}}.call
eval "i4 = 22", x eval "i4 = 22", x
assert_equal(22, eval("i4", x)) assert_equal(22, eval("i4", x))
$x = [] t = []
x = proc{proc{}}.call x = proc{proc{}}.call
eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x eval "(0..9).each{|i5| t[i5] = proc{i5*2}}", x
assert_equal(8, $x[4].call) assert_equal(8, t[4].call)
end end
x = binding x = binding
@ -303,10 +303,10 @@ class TestEval < Test::Unit::TestCase
x = proc{binding}.call x = proc{binding}.call
eval "i = 22", x eval "i = 22", x
assert_equal(22, eval("i", x)) assert_equal(22, eval("i", x))
$x = [] t = []
x = proc{binding}.call x = proc{binding}.call
eval "(0..9).each{|i5| $x[i5] = proc{i5*2}}", x eval "(0..9).each{|i5| t[i5] = proc{i5*2}}", x
assert_equal(8, $x[4].call) assert_equal(8, t[4].call)
x = proc{binding}.call x = proc{binding}.call
eval "for i6 in 1..1; j6=i6; end", x eval "for i6 in 1..1; j6=i6; end", x
assert(eval("defined? i6", x)) assert(eval("defined? i6", x))

View file

@ -2,13 +2,13 @@ require 'test/unit'
class TestIfunless < Test::Unit::TestCase class TestIfunless < Test::Unit::TestCase
def test_if_unless def test_if_unless
$x = 'test'; x = 'test';
assert(if $x == $x then true else false end) assert(if x == x then true else false end)
$bad = false bad = false
unless $x == $x unless x == x
$bad = true bad = true
end end
assert(!$bad) assert(!bad)
assert(unless $x != $x then true else false end) assert(unless x != x then true else false end)
end end
end end

View file

@ -25,14 +25,14 @@ class TestIterator < Test::Unit::TestCase
end end
def test_array def test_array
$x = [1, 2, 3, 4] x = [1, 2, 3, 4]
$y = [] y = []
# iterator over array # iterator over array
for i in $x for i in x
$y.push i y.push i
end end
assert_equal($x, $y) assert_equal(x, y)
end end
def tt def tt
@ -79,36 +79,36 @@ class TestIterator < Test::Unit::TestCase
assert(done) assert(done)
done = false done = false
$bad = false bad = false
loop { loop {
break if done break if done
done = true done = true
next next
$bad = true # should not reach here bad = true # should not reach here
} }
assert(!$bad) assert(!bad)
done = false done = false
$bad = false bad = false
loop { loop {
break if done break if done
done = true done = true
redo redo
$bad = true # should not reach here bad = true # should not reach here
} }
assert(!$bad) assert(!bad)
$x = [] x = []
for i in 1 .. 7 for i in 1 .. 7
$x.push i x.push i
end end
assert_equal(7, $x.size) assert_equal(7, x.size)
assert_equal([1, 2, 3, 4, 5, 6, 7], $x) assert_equal([1, 2, 3, 4, 5, 6, 7], x)
end end
def test_append_method_to_built_in_class def test_append_method_to_built_in_class
$x = [[1,2],[3,4],[5,6]] x = [[1,2],[3,4],[5,6]]
assert_equal($x.iter_test1{|x|x}, $x.iter_test2{|x|x}) assert_equal(x.iter_test1{|x|x}, x.iter_test2{|x|x})
end end
class IterTest class IterTest

View file

@ -30,12 +30,12 @@ class TestStringchar < Test::Unit::TestCase
assert(/(\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&) assert(/(\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&)
assert(/(?:\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&) assert(/(?:\s+\d+){2}/ =~ " 1 2"); assert_equal(" 1 2", $&)
$x = <<END; x = <<END;
ABCD ABCD
ABCD ABCD
END END
$x.gsub!(/((.|\n)*?)B((.|\n)*?)D/m ,'\1\3') x.gsub!(/((.|\n)*?)B((.|\n)*?)D/m ,'\1\3')
assert_equal("AC\nAC\n", $x) assert_equal("AC\nAC\n", x)
assert_match(/foo(?=(bar)|(baz))/, "foobar") assert_match(/foo(?=(bar)|(baz))/, "foobar")
assert_match(/foo(?=(bar)|(baz))/, "foobaz") assert_match(/foo(?=(bar)|(baz))/, "foobaz")
@ -56,12 +56,12 @@ END
assert_equal('-', foo * 1) assert_equal('-', foo * 1)
assert_equal('', foo * 0) assert_equal('', foo * 0)
$x = "a.gif" x = "a.gif"
assert_equal("gif", $x.sub(/.*\.([^\.]+)$/, '\1')) assert_equal("gif", x.sub(/.*\.([^\.]+)$/, '\1'))
assert_equal("b.gif", $x.sub(/.*\.([^\.]+)$/, 'b.\1')) assert_equal("b.gif", x.sub(/.*\.([^\.]+)$/, 'b.\1'))
assert_equal("", $x.sub(/.*\.([^\.]+)$/, '\2')) assert_equal("", x.sub(/.*\.([^\.]+)$/, '\2'))
assert_equal("ab", $x.sub(/.*\.([^\.]+)$/, 'a\2b')) assert_equal("ab", x.sub(/.*\.([^\.]+)$/, 'a\2b'))
assert_equal("<a.gif>", $x.sub(/.*\.([^\.]+)$/, '<\&>')) assert_equal("<a.gif>", x.sub(/.*\.([^\.]+)$/, '<\&>'))
end end
def test_char def test_char
@ -78,16 +78,16 @@ END
assert_equal("abc", "abcc".squeeze!("a-z")) assert_equal("abc", "abcc".squeeze!("a-z"))
assert_equal("ad", "abcd".delete!("bc")) assert_equal("ad", "abcd".delete!("bc"))
$x = "abcdef" x = "abcdef"
$y = [ ?a, ?b, ?c, ?d, ?e, ?f ] y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
$bad = false bad = false
$x.each_byte {|i| x.each_byte {|i|
if i.chr != $y.shift if i.chr != y.shift
$bad = true bad = true
break break
end end
} }
assert(!$bad) assert(!bad)
s = "a string" s = "a string"
s[0..s.size]="another string" s[0..s.size]="another string"