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

157 lines
4 KiB
Ruby
Raw Normal View History

require_relative '../../spec_helper'
require_relative 'shared/compose'
ruby_version_is "2.6" do
describe "Proc#<<" do
it "returns a Proc that is the composition of self and the passed Proc" do
upcase = proc { |s| s.upcase }
succ = proc { |s| s.succ }
(succ << upcase).call('Ruby').should == "RUBZ"
end
it "calls passed Proc with arguments and then calls self with result" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f << g).call(2).should == 16
(g << f).call(2).should == 8
end
it "accepts any callable object" do
inc = proc { |n| n + 1 }
double = Object.new
def double.call(n); n * 2; end
(inc << double).call(3).should == 7
end
it_behaves_like :proc_compose, :<<, -> { proc { |s| s.upcase } }
describe "composition" do
it "is a Proc" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f << g).is_a?(Proc).should == true
2020-05-03 06:28:29 -04:00
(f << g).should_not.lambda?
end
2020-09-15 15:54:31 -04:00
ruby_version_is(''...'3.0') do
2019-12-30 18:13:55 -05:00
it "is a Proc when other is lambda" do
f = proc { |x| x * x }
g = -> x { x + x }
(f << g).is_a?(Proc).should == true
2020-05-03 06:28:29 -04:00
(f << g).should_not.lambda?
2019-12-30 18:13:55 -05:00
end
it "is a lambda when self is lambda" do
f = -> x { x * x }
g = proc { |x| x + x }
(f << g).is_a?(Proc).should == true
2020-05-03 06:28:29 -04:00
(f << g).should.lambda?
2019-12-30 18:13:55 -05:00
end
2019-12-27 10:46:08 -05:00
end
2020-09-15 15:54:31 -04:00
ruby_version_is('3.0') do
2019-12-30 18:13:55 -05:00
it "is a lambda when parameter is lambda" do
f = -> x { x * x }
g = proc { |x| x + x }
lambda_proc = -> x { x }
2019-12-27 10:46:08 -05:00
2019-12-30 18:13:55 -05:00
(f << g).is_a?(Proc).should == true
2020-05-03 06:28:29 -04:00
(f << g).should_not.lambda?
(f << lambda_proc).should.lambda?
2019-12-30 18:13:55 -05:00
end
2019-12-27 10:46:08 -05:00
end
it "may accept multiple arguments" do
inc = proc { |n| n + 1 }
mul = proc { |n, m| n * m }
(inc << mul).call(2, 3).should == 7
end
2019-07-27 06:40:09 -04:00
it "passes blocks to the second proc" do
ScratchPad.record []
one = proc { |&arg| arg.call :one if arg }
two = proc { |&arg| arg.call :two if arg }
(one << two).call { |x| ScratchPad << x }
ScratchPad.recorded.should == [:two]
end
end
end
describe "Proc#>>" do
it "returns a Proc that is the composition of self and the passed Proc" do
upcase = proc { |s| s.upcase }
succ = proc { |s| s.succ }
(succ >> upcase).call('Ruby').should == "RUBZ"
end
it "calls passed Proc with arguments and then calls self with result" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f >> g).call(2).should == 8
(g >> f).call(2).should == 16
end
it "accepts any callable object" do
inc = proc { |n| n + 1 }
double = Object.new
def double.call(n); n * 2; end
(inc >> double).call(3).should == 8
end
it_behaves_like :proc_compose, :>>, -> { proc { |s| s.upcase } }
describe "composition" do
it "is a Proc" do
f = proc { |x| x * x }
g = proc { |x| x + x }
(f >> g).is_a?(Proc).should == true
2020-05-03 06:28:29 -04:00
(f >> g).should_not.lambda?
end
2019-12-27 10:46:08 -05:00
it "is a Proc when other is lambda" do
f = proc { |x| x * x }
g = -> x { x + x }
(f >> g).is_a?(Proc).should == true
2020-05-03 06:28:29 -04:00
(f >> g).should_not.lambda?
2019-12-27 10:46:08 -05:00
end
it "is a lambda when self is lambda" do
f = -> x { x * x }
g = proc { |x| x + x }
(f >> g).is_a?(Proc).should == true
2020-05-03 06:28:29 -04:00
(f >> g).should.lambda?
2019-12-27 10:46:08 -05:00
end
it "may accept multiple arguments" do
inc = proc { |n| n + 1 }
mul = proc { |n, m| n * m }
(mul >> inc).call(2, 3).should == 7
end
2019-07-27 06:40:09 -04:00
it "passes blocks to the first proc" do
ScratchPad.record []
one = proc { |&arg| arg.call :one if arg }
two = proc { |&arg| arg.call :two if arg }
(one >> two).call { |x| ScratchPad << x }
ScratchPad.recorded.should == [:one]
end
end
end
end