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

View file

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

View file

@ -25,14 +25,14 @@ class TestIterator < Test::Unit::TestCase
end
def test_array
$x = [1, 2, 3, 4]
$y = []
x = [1, 2, 3, 4]
y = []
# iterator over array
for i in $x
$y.push i
for i in x
y.push i
end
assert_equal($x, $y)
assert_equal(x, y)
end
def tt
@ -79,36 +79,36 @@ class TestIterator < Test::Unit::TestCase
assert(done)
done = false
$bad = false
bad = false
loop {
break if done
done = true
next
$bad = true # should not reach here
bad = true # should not reach here
}
assert(!$bad)
assert(!bad)
done = false
$bad = false
bad = false
loop {
break if done
done = true
redo
$bad = true # should not reach here
bad = true # should not reach here
}
assert(!$bad)
assert(!bad)
$x = []
x = []
for i in 1 .. 7
$x.push i
x.push i
end
assert_equal(7, $x.size)
assert_equal([1, 2, 3, 4, 5, 6, 7], $x)
assert_equal(7, x.size)
assert_equal([1, 2, 3, 4, 5, 6, 7], x)
end
def test_append_method_to_built_in_class
$x = [[1,2],[3,4],[5,6]]
assert_equal($x.iter_test1{|x|x}, $x.iter_test2{|x|x})
x = [[1,2],[3,4],[5,6]]
assert_equal(x.iter_test1{|x|x}, x.iter_test2{|x|x})
end
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", $&)
$x = <<END;
x = <<END;
ABCD
ABCD
END
$x.gsub!(/((.|\n)*?)B((.|\n)*?)D/m ,'\1\3')
assert_equal("AC\nAC\n", $x)
x.gsub!(/((.|\n)*?)B((.|\n)*?)D/m ,'\1\3')
assert_equal("AC\nAC\n", x)
assert_match(/foo(?=(bar)|(baz))/, "foobar")
assert_match(/foo(?=(bar)|(baz))/, "foobaz")
@ -56,12 +56,12 @@ END
assert_equal('-', foo * 1)
assert_equal('', foo * 0)
$x = "a.gif"
assert_equal("gif", $x.sub(/.*\.([^\.]+)$/, '\1'))
assert_equal("b.gif", $x.sub(/.*\.([^\.]+)$/, 'b.\1'))
assert_equal("", $x.sub(/.*\.([^\.]+)$/, '\2'))
assert_equal("ab", $x.sub(/.*\.([^\.]+)$/, 'a\2b'))
assert_equal("<a.gif>", $x.sub(/.*\.([^\.]+)$/, '<\&>'))
x = "a.gif"
assert_equal("gif", x.sub(/.*\.([^\.]+)$/, '\1'))
assert_equal("b.gif", x.sub(/.*\.([^\.]+)$/, 'b.\1'))
assert_equal("", x.sub(/.*\.([^\.]+)$/, '\2'))
assert_equal("ab", x.sub(/.*\.([^\.]+)$/, 'a\2b'))
assert_equal("<a.gif>", x.sub(/.*\.([^\.]+)$/, '<\&>'))
end
def test_char
@ -78,16 +78,16 @@ END
assert_equal("abc", "abcc".squeeze!("a-z"))
assert_equal("ad", "abcd".delete!("bc"))
$x = "abcdef"
$y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
$bad = false
$x.each_byte {|i|
if i.chr != $y.shift
$bad = true
x = "abcdef"
y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
bad = false
x.each_byte {|i|
if i.chr != y.shift
bad = true
break
end
}
assert(!$bad)
assert(!bad)
s = "a string"
s[0..s.size]="another string"