diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-20 20:38:57 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-01-20 20:38:57 +0000 |
commit | 6204e0804b24f1675b49d5880da014411bcfb831 (patch) | |
tree | ce6c00bf078fc416936ca3cdc972b9b3c1c78dae /spec/ruby/core/method | |
parent | 58573c33e4720315ed27491e31dcc22892e1ce95 (diff) |
Update to ruby/spec@35a9fba
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/core/method')
-rw-r--r-- | spec/ruby/core/method/compose_spec.rb | 101 | ||||
-rw-r--r-- | spec/ruby/core/method/fixtures/classes.rb | 25 |
2 files changed, 126 insertions, 0 deletions
diff --git a/spec/ruby/core/method/compose_spec.rb b/spec/ruby/core/method/compose_spec.rb new file mode 100644 index 0000000000..aa0aded775 --- /dev/null +++ b/spec/ruby/core/method/compose_spec.rb @@ -0,0 +1,101 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/classes' +require_relative '../proc/shared/compose' + +ruby_version_is "2.6" do + 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 } + + (succ << upcase).call('Ruby').should == "RUBZ" + end + + 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 } + + pow_2_method = MethodSpecs::Composition.new.method(:pow_2) + double_method = MethodSpecs::Composition.new.method(:double) + + (pow_2_method << double_proc).call(2).should == 16 + (double_method << pow_2_proc).call(2).should == 8 + end + + it "accepts any callable object" do + inc = MethodSpecs::Composition.new.method(:inc) + + double = Object.new + def double.call(n); n * 2; end + + (inc << double).call(3).should == 7 + end + + it_behaves_like :proc_compose, :<<, -> { MethodSpecs::Composition.new.method(:upcase) } + + describe "composition" do + it "is a lambda" do + pow_2 = MethodSpecs::Composition.new.method(:pow_2) + double = proc { |x| x + x } + + (pow_2 << double).is_a?(Proc).should == true + (pow_2 << double).lambda?.should == true + end + + it "may accept multiple arguments" do + inc = MethodSpecs::Composition.new.method(:inc) + mul = proc { |n, m| n * m } + + (inc << mul).call(2, 3).should == 7 + end + end + end + + 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) + + (succ >> upcase).call('Ruby').should == "RUBZ" + end + + 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 } + + pow_2_method = MethodSpecs::Composition.new.method(:pow_2) + double_method = MethodSpecs::Composition.new.method(:double) + + (pow_2_method >> double_proc).call(2).should == 8 + (double_method >> pow_2_proc).call(2).should == 16 + end + + it "accepts any callable object" do + inc = MethodSpecs::Composition.new.method(:inc) + + double = Object.new + def double.call(n); n * 2; end + + (inc >> double).call(3).should == 8 + end + + it_behaves_like :proc_compose, :>>, -> { MethodSpecs::Composition.new.method(:upcase) } + + describe "composition" do + it "is a lambda" do + pow_2 = MethodSpecs::Composition.new.method(:pow_2) + double = proc { |x| x + x } + + (pow_2 >> double).is_a?(Proc).should == true + (pow_2 >> double).lambda?.should == true + end + + it "may accept multiple arguments" do + mul = MethodSpecs::Composition.new.method(:mul) + inc = proc { |n| n + 1 } + + (mul >> inc).call(2, 3).should == 7 + end + end + end +end diff --git a/spec/ruby/core/method/fixtures/classes.rb b/spec/ruby/core/method/fixtures/classes.rb index 142cbd1bec..315672c6e0 100644 --- a/spec/ruby/core/method/fixtures/classes.rb +++ b/spec/ruby/core/method/fixtures/classes.rb @@ -181,4 +181,29 @@ module MethodSpecs end end + class Composition + def upcase(s) + s.upcase + end + + def succ(s) + s.succ + end + + def pow_2(n) + n * n + end + + def double(n) + n + n + end + + def inc(n) + n + 1 + end + + def mul(n, m) + n * m + end + end end |