2019-01-20 15:38:57 -05:00
|
|
|
require_relative '../../spec_helper'
|
|
|
|
require_relative 'fixtures/classes'
|
|
|
|
require_relative '../proc/shared/compose'
|
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
describe "Method#<<" do
|
|
|
|
it "returns a Proc that is the composition of self and the passed Proc" do
|
|
|
|
succ = MethodSpecs::Composition.new.method(:succ)
|
|
|
|
upcase = proc { |s| s.upcase }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(succ << upcase).call('Ruby').should == "RUBZ"
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "calls passed Proc with arguments and then calls self with result" do
|
|
|
|
pow_2_proc = proc { |x| x * x }
|
|
|
|
double_proc = proc { |x| x + x }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
pow_2_method = MethodSpecs::Composition.new.method(:pow_2)
|
|
|
|
double_method = MethodSpecs::Composition.new.method(:double)
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(pow_2_method << double_proc).call(2).should == 16
|
|
|
|
(double_method << pow_2_proc).call(2).should == 8
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "accepts any callable object" do
|
|
|
|
inc = MethodSpecs::Composition.new.method(:inc)
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
double = Object.new
|
|
|
|
def double.call(n); n * 2; end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(inc << double).call(3).should == 7
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it_behaves_like :proc_compose, :<<, -> { MethodSpecs::Composition.new.method(:upcase) }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
describe "composition" do
|
|
|
|
it "is a lambda" do
|
|
|
|
pow_2 = MethodSpecs::Composition.new.method(:pow_2)
|
|
|
|
double = proc { |x| x + x }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(pow_2 << double).is_a?(Proc).should == true
|
|
|
|
ruby_version_is(''...'3.0') { (pow_2 << double).should.lambda? }
|
|
|
|
ruby_version_is('3.0') { (pow_2 << double).should_not.lambda? }
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "may accept multiple arguments" do
|
|
|
|
inc = MethodSpecs::Composition.new.method(:inc)
|
|
|
|
mul = proc { |n, m| n * m }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(inc << mul).call(2, 3).should == 7
|
2019-01-20 15:38:57 -05:00
|
|
|
end
|
|
|
|
end
|
2021-07-29 16:11:21 -04:00
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
describe "Method#>>" do
|
|
|
|
it "returns a Proc that is the composition of self and the passed Proc" do
|
|
|
|
upcase = proc { |s| s.upcase }
|
|
|
|
succ = MethodSpecs::Composition.new.method(:succ)
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(succ >> upcase).call('Ruby').should == "RUBZ"
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "calls passed Proc with arguments and then calls self with result" do
|
|
|
|
pow_2_proc = proc { |x| x * x }
|
|
|
|
double_proc = proc { |x| x + x }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
pow_2_method = MethodSpecs::Composition.new.method(:pow_2)
|
|
|
|
double_method = MethodSpecs::Composition.new.method(:double)
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(pow_2_method >> double_proc).call(2).should == 8
|
|
|
|
(double_method >> pow_2_proc).call(2).should == 16
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "accepts any callable object" do
|
|
|
|
inc = MethodSpecs::Composition.new.method(:inc)
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
double = Object.new
|
|
|
|
def double.call(n); n * 2; end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(inc >> double).call(3).should == 8
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it_behaves_like :proc_compose, :>>, -> { MethodSpecs::Composition.new.method(:upcase) }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
describe "composition" do
|
|
|
|
it "is a lambda" do
|
|
|
|
pow_2 = MethodSpecs::Composition.new.method(:pow_2)
|
|
|
|
double = proc { |x| x + x }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(pow_2 >> double).is_a?(Proc).should == true
|
|
|
|
(pow_2 >> double).should.lambda?
|
|
|
|
end
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
it "may accept multiple arguments" do
|
|
|
|
mul = MethodSpecs::Composition.new.method(:mul)
|
|
|
|
inc = proc { |n| n + 1 }
|
2019-01-20 15:38:57 -05:00
|
|
|
|
2021-07-29 16:11:21 -04:00
|
|
|
(mul >> inc).call(2, 3).should == 7
|
2019-01-20 15:38:57 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|