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

* eval.c (eval): allow to eval in a binding that has a singleton method.

[ruby-dev:33763]

* test/ruby/test_proc.rb: add tests to achieve over 70% test coverage
  of time.c.

* test/ruby/test_method.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15462 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2008-02-13 15:03:59 +00:00
parent 70f25096c0
commit a707f6249f
4 changed files with 256 additions and 1 deletions

View file

@ -1,6 +1,15 @@
require 'test/unit'
class TestProc < Test::Unit::TestCase
def setup
@verbose = $VERBOSE
$VERBOSE = nil
end
def teardown
$VERBOSE = @verbose
end
def test_proc
p1 = proc{|i| i}
assert_equal(2, p1.call(2))
@ -200,4 +209,92 @@ class TestProc < Test::Unit::TestCase
assert_equal(fib, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89])
end
def test_dup_clone
b = proc {|x| x + "bar" }
class << b; attr_accessor :foo; end
bd = b.dup
assert_equal("foobar", bd.call("foo"))
assert_raise(NoMethodError) { bd.foo = :foo }
assert_raise(NoMethodError) { bd.foo }
bc = b.clone
assert_equal("foobar", bc.call("foo"))
bc.foo = :foo
assert_equal(:foo, bc.foo)
end
def test_binding
b = proc {|x, y, z| proc {}.binding }.call(1, 2, 3)
class << b; attr_accessor :foo; end
bd = b.dup
assert_equal([1, 2, 3], bd.eval("[x, y, z]"))
assert_raise(NoMethodError) { bd.foo = :foo }
assert_raise(NoMethodError) { bd.foo }
bc = b.clone
assert_equal([1, 2, 3], bc.eval("[x, y, z]"))
bc.foo = :foo
assert_equal(:foo, bc.foo)
b = nil
1.times { x, y, z = 1, 2, 3; b = binding }
assert_equal([1, 2, 3], b.eval("[x, y, z]"))
end
def test_proc_lambda
assert_raise(ArgumentError) { proc }
assert_raise(ArgumentError) { lambda }
o = Object.new
def o.foo
b = nil
1.times { b = lambda }
b
end
assert_equal(:foo, o.foo { :foo }.call)
def o.foo(&b)
b = nil
1.times { b = lambda }
b
end
assert_equal(:foo, o.foo { :foo }.call)
end
def test_arity2
assert_equal(0, method(:proc).to_proc.arity)
assert_equal(-1, proc {}.curry.arity)
end
def test_proc_location
t = Thread.new { sleep }
assert_raise(ThreadError) { t.instance_eval { initialize { } } }
t.kill
end
def test_eq2
b1 = proc { }
b2 = b1.dup
assert(b1 == b2)
end
def test_to_proc
b = proc { :foo }
assert_equal(:foo, b.to_proc.call)
end
def test_localjump_error
o = Object.new
def foo; yield; end
exc = foo rescue $!
assert_nil(exc.exit_value)
assert_equal(:noreason, exc.reason)
end
def test_binding2
assert_raise(ArgumentError) { proc {}.curry.binding }
end
end