-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathdelegation_spec.rb
More file actions
162 lines (138 loc) · 3.98 KB
/
delegation_spec.rb
File metadata and controls
162 lines (138 loc) · 3.98 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require_relative '../spec_helper'
require_relative 'fixtures/delegation'
# Forwarding anonymous parameters
describe "delegation with def(...)" do
it "delegates rest and kwargs" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(...)
target(...)
end
RUBY
a.new.delegate(1, b: 2).should == [[1], {b: 2}, nil]
end
it "delegates a block literal" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate_block(...)
target_block(...)
end
RUBY
a.new.delegate_block(1, b: 2) { |x| x }.should == [{b: 2}, [1]]
end
it "delegates a block argument" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(...)
target(...)
end
RUBY
block = proc {}
a.new.delegate(1, b: 2, &block).should == [[1], {b: 2}, block]
end
it "delegates with additional arguments" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(...)
target(:first, :second, ...)
end
RUBY
a.new.delegate(1, b: 2).should == [[:first, :second, 1], {b: 2}, nil]
end
it "parses as open endless Range when brackets are omitted" do
a = Class.new(DelegationSpecs::Target)
suppress_warning do
a.class_eval(<<-RUBY)
def delegate(...)
target ...
end
RUBY
end
a.new.delegate(1, b: 2).should == Range.new([[], {}, nil], nil, true)
end
end
describe "delegation with def(x, ...)" do
it "delegates rest and kwargs" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(x, ...)
target(...)
end
RUBY
a.new.delegate(0, 1, b: 2).should == [[1], {b: 2}, nil]
end
it "delegates a block literal" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate_block(x, ...)
target_block(...)
end
RUBY
a.new.delegate_block(0, 1, b: 2) { |x| x }.should == [{b: 2}, [1]]
end
it "delegates a block argument" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(...)
target(...)
end
RUBY
block = proc {}
a.new.delegate(1, b: 2, &block).should == [[1], {b: 2}, block]
end
end
describe "delegation with def(*)" do
it "delegates rest" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(*)
target(*)
end
RUBY
a.new.delegate(0, 1).should == [[0, 1], {}, nil]
end
context "within a block that accepts anonymous rest within a method that accepts anonymous rest" do
it "does not allow delegating rest" do
-> {
eval "def m(*); proc { |*| n(*) } end"
}.should raise_error(SyntaxError, /anonymous rest parameter is also used within block/)
end
end
end
describe "delegation with def(**)" do
it "delegates kwargs" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(**)
target(**)
end
RUBY
a.new.delegate(a: 1) { |x| x }.should == [[], {a: 1}, nil]
end
context "within a block that accepts anonymous kwargs within a method that accepts anonymous kwargs" do
it "does not allow delegating kwargs" do
-> {
eval "def m(**); proc { |**| n(**) } end"
}.should raise_error(SyntaxError, /anonymous keyword rest parameter is also used within block/)
end
end
end
describe "delegation with def(&)" do
it "delegates an anonymous block parameter" do
a = Class.new(DelegationSpecs::Target)
a.class_eval(<<-RUBY)
def delegate(&)
target(&)
end
RUBY
block = proc {}
a.new.delegate(&block).should == [[], {}, block]
end
context "within a block that accepts anonymous block within a method that accepts anonymous block" do
it "does not allow delegating a block" do
-> {
eval "def m(&); proc { |&| n(&) } end"
}.should raise_error(SyntaxError, /anonymous block parameter is also used within block/)
end
end
end