summaryrefslogtreecommitdiff
path: root/spec/ruby/core/method/compose_spec.rb
blob: 0e2a0eeea2315b821f738022e5db2553baa06142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
        ruby_version_is(''...'2.8') { (pow_2 << double).should.lambda? }
        ruby_version_is('2.8') { (pow_2 << double).should_not.lambda? }
      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).should.lambda?
      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