aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/proc/compose_spec.rb
blob: 35e949a779b051f8fa490f4f6dc884bad248065b (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
103
104
105
106
107
108
109
110
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
        (f << g).lambda?.should == false
      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

      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
        (f >> g).lambda?.should == false
      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

      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