-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc_compose_spec.rb
39 lines (35 loc) · 966 Bytes
/
proc_compose_spec.rb
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
require 'spec_helper'
describe 'Proc#compose' do
let(:double) do
proc {|a| a * 2 }
end
let(:triple) do
proc {|a| a * 3 }
end
let(:negate) do
proc {|a| -a }
end
it 'lives on Proc' do
expect(double.respond_to?(:compose)).to eq(true)
expect(double.respond_to?(:*)).to eq(true)
end
it 'creates new Proc from 2 Procs' do
expect(double * triple).to be_a Proc
end
it 'compose Proc can be evaluated' do
expect((double * triple).(2)).to eq(triple.(double.(2)))
end
it 'compose on & on' do
expect((double * triple * negate).(2)).to eq(-12)
end
context 'lambdas' do
it 'removes lambda information' do
a, b = [lambda {|x| x + 2 }, lambda {|y| y + 3 }]
# works by coercing to Proc
expect((a * b).(2)).to eq(7)
# if lambdas were preserved, this would be:
#expect{ (a * b).() }.to raise_error ArgumentError
expect{ (a * b).() }.to raise_error NoMethodError
end
end
end