diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-11-22 05:51:40 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-11-22 05:51:40 +0000 |
commit | a43e967b8d277fee5cf18329b26bc5c31a3e0363 (patch) | |
tree | a73019bcd581bd65ce6b2e8e7941374c1da143ba /test/ruby/test_proc.rb | |
parent | 655257273af98a1cce848393c73a7c3012724c59 (diff) |
proc.c: Implement Proc#* for Proc composition
* proc.c (proc_compose): Implement Proc#* for Proc composition, enabling
composition of Procs and Methods. [Feature #6284]
* test/ruby/test_proc.rb: Add test cases for Proc composition.
From: Paul Mucur <[email protected]>
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65911 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_proc.rb')
-rw-r--r-- | test/ruby/test_proc.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 1f713f3dbc..7ea4556e8a 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -1416,4 +1416,55 @@ class TestProc < Test::Unit::TestCase def test_proc_without_block_for_symbol assert_equal('1', method_for_test_proc_without_block_for_symbol(&:to_s).call(1), '[Bug #14782]') end + + def test_compose + f = proc {|x| x * 2} + g = proc {|x| x + 1} + h = f * g + + assert_equal(6, h.call(2)) + end + + def test_compose_with_multiple_args + f = proc {|x| x * 2} + g = proc {|x, y| x + y} + h = f * g + + assert_equal(6, h.call(1, 2)) + end + + def test_compose_with_block + f = proc {|x| x * 2} + g = proc {|&blk| blk.call(1) } + h = f * g + + assert_equal(8, h.call { |x| x + 3 }) + end + + def test_compose_with_lambda + f = lambda {|x| x * 2} + g = lambda {|x| x} + h = f * g + + assert_predicate(h, :lambda?) + end + + def test_compose_with_method + f = proc {|x| x * 2} + c = Class.new { + def g(x) x + 1 end + } + g = c.new.method(:g) + h = f * g + + assert_equal(6, h.call(2)) + end + + def test_compose_with_nonproc_or_method + f = proc {|x| x * 2} + + assert_raise(TypeError) { + f * 5 + } + end end |