test_proc.rb: improve curry tests
* test/ruby/test_proc.rb (test_curry): split. * test/ruby/test_proc.rb (test_curry_passed_block): simplify the assertion. * test/ruby/test_proc.rb (test_curry_with_trace): run all curry tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56457 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
10035cb4bc
commit
a6ed6e2b42
@ -246,37 +246,47 @@ class TestProc < Test::Unit::TestCase
|
|||||||
assert_equal([false, false], m.call, "#{bug8341} nested without block")
|
assert_equal([false, false], m.call, "#{bug8341} nested without block")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_curry
|
def test_curry_proc
|
||||||
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
|
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
|
||||||
assert_equal(6, b.curry[1][2][3])
|
assert_equal(6, b.curry[1][2][3])
|
||||||
assert_equal(6, b.curry[1, 2][3, 4])
|
assert_equal(6, b.curry[1, 2][3, 4])
|
||||||
assert_equal(6, b.curry(5)[1][2][3][4][5])
|
assert_equal(6, b.curry(5)[1][2][3][4][5])
|
||||||
assert_equal(6, b.curry(5)[1, 2][3, 4][5])
|
assert_equal(6, b.curry(5)[1, 2][3, 4][5])
|
||||||
assert_equal(1, b.curry(1)[1])
|
assert_equal(1, b.curry(1)[1])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_curry_proc_splat
|
||||||
b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
b = proc {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
||||||
assert_equal(6, b.curry[1][2][3])
|
assert_equal(6, b.curry[1][2][3])
|
||||||
assert_equal(10, b.curry[1, 2][3, 4])
|
assert_equal(10, b.curry[1, 2][3, 4])
|
||||||
assert_equal(15, b.curry(5)[1][2][3][4][5])
|
assert_equal(15, b.curry(5)[1][2][3][4][5])
|
||||||
assert_equal(15, b.curry(5)[1, 2][3, 4][5])
|
assert_equal(15, b.curry(5)[1, 2][3, 4][5])
|
||||||
assert_equal(1, b.curry(1)[1])
|
assert_equal(1, b.curry(1)[1])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_curry_lambda
|
||||||
b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
|
b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
|
||||||
assert_equal(6, b.curry[1][2][3])
|
assert_equal(6, b.curry[1][2][3])
|
||||||
assert_raise(ArgumentError) { b.curry[1, 2][3, 4] }
|
assert_raise(ArgumentError) { b.curry[1, 2][3, 4] }
|
||||||
assert_raise(ArgumentError) { b.curry(5) }
|
assert_raise(ArgumentError) { b.curry(5) }
|
||||||
assert_raise(ArgumentError) { b.curry(1) }
|
assert_raise(ArgumentError) { b.curry(1) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_curry_lambda_splat
|
||||||
b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
b = lambda {|x, y, z, *w| (x||0) + (y||0) + (z||0) + w.inject(0, &:+) }
|
||||||
assert_equal(6, b.curry[1][2][3])
|
assert_equal(6, b.curry[1][2][3])
|
||||||
assert_equal(10, b.curry[1, 2][3, 4])
|
assert_equal(10, b.curry[1, 2][3, 4])
|
||||||
assert_equal(15, b.curry(5)[1][2][3][4][5])
|
assert_equal(15, b.curry(5)[1][2][3][4][5])
|
||||||
assert_equal(15, b.curry(5)[1, 2][3, 4][5])
|
assert_equal(15, b.curry(5)[1, 2][3, 4][5])
|
||||||
assert_raise(ArgumentError) { b.curry(1) }
|
assert_raise(ArgumentError) { b.curry(1) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_curry_no_arguments
|
||||||
b = proc { :foo }
|
b = proc { :foo }
|
||||||
assert_equal(:foo, b.curry[])
|
assert_equal(:foo, b.curry[])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_curry_given_blocks
|
||||||
b = lambda {|x, y, &blk| blk.call(x + y) }.curry
|
b = lambda {|x, y, &blk| blk.call(x + y) }.curry
|
||||||
b = b.call(2) { raise }
|
b = b.call(2) { raise }
|
||||||
b = b.call(3) {|x| x + 4 }
|
b = b.call(3) {|x| x + 4 }
|
||||||
@ -330,16 +340,11 @@ class TestProc < Test::Unit::TestCase
|
|||||||
assert_equal(fib, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89])
|
assert_equal(fib, [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89])
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_curry_from_knownbug
|
def test_curry_passed_block
|
||||||
a = lambda {|x, y, &b| b }
|
a = lambda {|x, y, &b| b }
|
||||||
b = a.curry[1]
|
b = a.curry[1]
|
||||||
|
|
||||||
assert_equal(:ok,
|
assert_not_nil(b.call(2){}, '[ruby-core:15551]: passed block to curried block')
|
||||||
if b.call(2){} == nil
|
|
||||||
:ng
|
|
||||||
else
|
|
||||||
:ok
|
|
||||||
end, 'moved from btest/knownbug, [ruby-core:15551]')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_curry_instance_exec
|
def test_curry_instance_exec
|
||||||
@ -1231,7 +1236,10 @@ class TestProc < Test::Unit::TestCase
|
|||||||
def test_curry_with_trace
|
def test_curry_with_trace
|
||||||
# bug3751 = '[ruby-core:31871]'
|
# bug3751 = '[ruby-core:31871]'
|
||||||
set_trace_func(proc {})
|
set_trace_func(proc {})
|
||||||
test_curry
|
methods.grep(/\Atest_curry/) do |test|
|
||||||
|
next if test == __method__
|
||||||
|
__send__(test)
|
||||||
|
end
|
||||||
ensure
|
ensure
|
||||||
set_trace_func(nil)
|
set_trace_func(nil)
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user