ripper_ruby_parser 1.6.1 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +4 -23
- data/Rakefile +12 -12
- data/lib/ripper_ruby_parser.rb +2 -2
- data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +9 -9
- data/lib/ripper_ruby_parser/parser.rb +3 -3
- data/lib/ripper_ruby_parser/sexp_handlers.rb +9 -9
- data/lib/ripper_ruby_parser/sexp_handlers/assignment.rb +3 -9
- data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +19 -24
- data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +14 -18
- data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +3 -3
- data/lib/ripper_ruby_parser/sexp_processor.rb +4 -4
- data/lib/ripper_ruby_parser/unescape.rb +11 -11
- data/lib/ripper_ruby_parser/version.rb +1 -1
- data/test/end_to_end/comments_test.rb +10 -10
- data/test/end_to_end/comparison_test.rb +28 -28
- data/test/end_to_end/lib_comparison_test.rb +6 -6
- data/test/end_to_end/line_numbering_test.rb +10 -10
- data/test/end_to_end/samples_comparison_test.rb +5 -5
- data/test/end_to_end/test_comparison_test.rb +6 -6
- data/test/pt_testcase/pt_test.rb +7 -7
- data/test/ripper_ruby_parser/commenting_ripper_parser_test.rb +163 -169
- data/test/ripper_ruby_parser/parser_test.rb +338 -338
- data/test/ripper_ruby_parser/sexp_handlers/assignment_test.rb +475 -511
- data/test/ripper_ruby_parser/sexp_handlers/blocks_test.rb +582 -564
- data/test/ripper_ruby_parser/sexp_handlers/conditionals_test.rb +469 -469
- data/test/ripper_ruby_parser/sexp_handlers/literals_test.rb +713 -724
- data/test/ripper_ruby_parser/sexp_handlers/loops_test.rb +155 -155
- data/test/ripper_ruby_parser/sexp_handlers/method_calls_test.rb +181 -181
- data/test/ripper_ruby_parser/sexp_handlers/methods_test.rb +337 -352
- data/test/ripper_ruby_parser/sexp_handlers/operators_test.rb +298 -298
- data/test/ripper_ruby_parser/sexp_processor_test.rb +119 -119
- data/test/ripper_ruby_parser/version_test.rb +2 -2
- data/test/samples/lambdas.rb +5 -0
- data/test/samples/misc.rb +3 -0
- data/test/samples/strings.rb +7 -0
- data/test/test_helper.rb +8 -6
- metadata +12 -10
@@ -1,191 +1,192 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require File.expand_path(
|
3
|
+
require File.expand_path("../../test_helper.rb", File.dirname(__FILE__))
|
4
4
|
|
5
5
|
describe RipperRubyParser::Parser do
|
6
6
|
let(:parser) { RipperRubyParser::Parser.new }
|
7
7
|
|
8
|
-
describe
|
9
|
-
describe
|
10
|
-
it
|
11
|
-
|
12
|
-
must_be_parsed_as s(:cdecl,
|
13
|
-
|
14
|
-
|
8
|
+
describe "#parse" do
|
9
|
+
describe "for single assignment" do
|
10
|
+
it "works when assigning to a namespaced constant" do
|
11
|
+
_("Foo::Bar = baz")
|
12
|
+
.must_be_parsed_as s(:cdecl,
|
13
|
+
s(:colon2, s(:const, :Foo), :Bar),
|
14
|
+
s(:call, nil, :baz))
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
18
|
-
|
19
|
-
must_be_parsed_as s(:cdecl,
|
20
|
-
|
21
|
-
|
17
|
+
it "works when assigning to constant in the root namespace" do
|
18
|
+
_("::Foo = bar")
|
19
|
+
.must_be_parsed_as s(:cdecl,
|
20
|
+
s(:colon3, :Foo),
|
21
|
+
s(:call, nil, :bar))
|
22
22
|
end
|
23
23
|
|
24
|
-
it
|
25
|
-
|
26
|
-
must_be_parsed_as s(:lasgn, :foo, s(:call, nil, :bar))
|
24
|
+
it "works with blocks" do
|
25
|
+
_("foo = begin; bar; end")
|
26
|
+
.must_be_parsed_as s(:lasgn, :foo, s(:call, nil, :bar))
|
27
27
|
end
|
28
28
|
|
29
|
-
describe
|
30
|
-
it
|
31
|
-
|
32
|
-
must_be_parsed_as s(:lasgn, :foo,
|
33
|
-
|
34
|
-
|
35
|
-
|
29
|
+
describe "with a right-hand splat" do
|
30
|
+
it "works in the simple case" do
|
31
|
+
_("foo = *bar")
|
32
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
33
|
+
s(:svalue,
|
34
|
+
s(:splat,
|
35
|
+
s(:call, nil, :bar))))
|
36
36
|
end
|
37
37
|
|
38
|
-
it
|
39
|
-
|
40
|
-
must_be_parsed_as s(:lasgn, :foo,
|
41
|
-
|
38
|
+
it "works with blocks" do
|
39
|
+
_("foo = *begin; bar; end")
|
40
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
41
|
+
s(:svalue, s(:splat, s(:call, nil, :bar))))
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
describe
|
46
|
-
it
|
47
|
-
|
48
|
-
must_be_parsed_as s(:lasgn, :foo,
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
45
|
+
describe "with several items on the right hand side" do
|
46
|
+
it "works in the simple case" do
|
47
|
+
_("foo = bar, baz")
|
48
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
49
|
+
s(:svalue,
|
50
|
+
s(:array,
|
51
|
+
s(:call, nil, :bar),
|
52
|
+
s(:call, nil, :baz))))
|
53
53
|
end
|
54
54
|
|
55
|
-
it
|
56
|
-
|
57
|
-
must_be_parsed_as s(:lasgn, :foo,
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
it "works with a splat" do
|
56
|
+
_("foo = bar, *baz")
|
57
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
58
|
+
s(:svalue,
|
59
|
+
s(:array,
|
60
|
+
s(:call, nil, :bar),
|
61
|
+
s(:splat,
|
62
|
+
s(:call, nil, :baz)))))
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
-
describe
|
66
|
+
describe "with an array literal on the right hand side" do
|
67
67
|
specify do
|
68
|
-
|
69
|
-
must_be_parsed_as s(:lasgn, :foo,
|
70
|
-
|
71
|
-
|
72
|
-
|
68
|
+
_("foo = [bar, baz]")
|
69
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
70
|
+
s(:array,
|
71
|
+
s(:call, nil, :bar),
|
72
|
+
s(:call, nil, :baz)))
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
it
|
77
|
-
|
78
|
-
must_be_parsed_as s(:iasgn,
|
79
|
-
|
80
|
-
|
76
|
+
it "works when assigning to an instance variable" do
|
77
|
+
_("@foo = bar")
|
78
|
+
.must_be_parsed_as s(:iasgn,
|
79
|
+
:@foo,
|
80
|
+
s(:call, nil, :bar))
|
81
81
|
end
|
82
82
|
|
83
|
-
it
|
84
|
-
|
85
|
-
must_be_parsed_as s(:cdecl,
|
86
|
-
|
87
|
-
|
83
|
+
it "works when assigning to a constant" do
|
84
|
+
_("FOO = bar")
|
85
|
+
.must_be_parsed_as s(:cdecl,
|
86
|
+
:FOO,
|
87
|
+
s(:call, nil, :bar))
|
88
88
|
end
|
89
89
|
|
90
|
-
it
|
91
|
-
|
92
|
-
must_be_parsed_as s(:attrasgn,
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
90
|
+
it "works when assigning to a collection element" do
|
91
|
+
_("foo[bar] = baz")
|
92
|
+
.must_be_parsed_as s(:attrasgn,
|
93
|
+
s(:call, nil, :foo),
|
94
|
+
:[]=,
|
95
|
+
s(:call, nil, :bar),
|
96
|
+
s(:call, nil, :baz))
|
97
97
|
end
|
98
98
|
|
99
|
-
it
|
100
|
-
|
101
|
-
must_be_parsed_as s(:attrasgn,
|
102
|
-
|
103
|
-
|
104
|
-
|
99
|
+
it "works when assigning to an attribute" do
|
100
|
+
_("foo.bar = baz")
|
101
|
+
.must_be_parsed_as s(:attrasgn,
|
102
|
+
s(:call, nil, :foo),
|
103
|
+
:bar=,
|
104
|
+
s(:call, nil, :baz))
|
105
105
|
end
|
106
106
|
|
107
|
-
describe
|
108
|
-
it
|
109
|
-
|
110
|
-
must_be_parsed_as s(:cvdecl,
|
111
|
-
|
112
|
-
|
107
|
+
describe "when assigning to a class variable" do
|
108
|
+
it "works outside a method" do
|
109
|
+
_("@@foo = bar")
|
110
|
+
.must_be_parsed_as s(:cvdecl,
|
111
|
+
:@@foo,
|
112
|
+
s(:call, nil, :bar))
|
113
113
|
end
|
114
114
|
|
115
|
-
it
|
116
|
-
|
117
|
-
must_be_parsed_as s(:defn,
|
118
|
-
|
119
|
-
|
115
|
+
it "works inside a method" do
|
116
|
+
_("def foo; @@bar = baz; end")
|
117
|
+
.must_be_parsed_as s(:defn,
|
118
|
+
:foo, s(:args),
|
119
|
+
s(:cvasgn, :@@bar, s(:call, nil, :baz)))
|
120
120
|
end
|
121
121
|
|
122
|
-
it
|
123
|
-
|
124
|
-
must_be_parsed_as s(:defs,
|
125
|
-
|
126
|
-
|
127
|
-
|
122
|
+
it "works inside a method with a receiver" do
|
123
|
+
_("def self.foo; @@bar = baz; end")
|
124
|
+
.must_be_parsed_as s(:defs,
|
125
|
+
s(:self),
|
126
|
+
:foo, s(:args),
|
127
|
+
s(:cvasgn, :@@bar, s(:call, nil, :baz)))
|
128
128
|
end
|
129
129
|
|
130
|
-
it
|
131
|
-
|
132
|
-
must_be_parsed_as s(:defn,
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
130
|
+
it "works inside method arguments" do
|
131
|
+
_("def foo(bar = (@@baz = qux)); end")
|
132
|
+
.must_be_parsed_as s(:defn,
|
133
|
+
:foo,
|
134
|
+
s(:args,
|
135
|
+
s(:lasgn, :bar,
|
136
|
+
s(:cvasgn, :@@baz, s(:call, nil, :qux)))),
|
137
|
+
s(:nil))
|
138
138
|
end
|
139
139
|
|
140
|
-
it
|
141
|
-
|
142
|
-
must_be_parsed_as s(:defs,
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
140
|
+
it "works inside method arguments of a singleton method" do
|
141
|
+
_("def self.foo(bar = (@@baz = qux)); end")
|
142
|
+
.must_be_parsed_as s(:defs,
|
143
|
+
s(:self),
|
144
|
+
:foo,
|
145
|
+
s(:args,
|
146
|
+
s(:lasgn, :bar,
|
147
|
+
s(:cvasgn, :@@baz, s(:call, nil, :qux)))),
|
148
|
+
s(:nil))
|
149
149
|
end
|
150
150
|
|
151
|
-
it
|
152
|
-
|
153
|
-
must_be_parsed_as s(:defs,
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
151
|
+
it "works inside the receiver in a method definition" do
|
152
|
+
_("def (bar = (@@baz = qux)).foo; end")
|
153
|
+
.must_be_parsed_as s(:defs,
|
154
|
+
s(:lasgn, :bar,
|
155
|
+
s(:cvdecl, :@@baz,
|
156
|
+
s(:call, nil, :qux))), :foo,
|
157
|
+
s(:args), s(:nil))
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|
161
|
-
it
|
162
|
-
|
163
|
-
must_be_parsed_as s(:gasgn,
|
164
|
-
|
165
|
-
|
161
|
+
it "works when assigning to a global variable" do
|
162
|
+
_("$foo = bar")
|
163
|
+
.must_be_parsed_as s(:gasgn,
|
164
|
+
:$foo,
|
165
|
+
s(:call, nil, :bar))
|
166
166
|
end
|
167
167
|
|
168
|
-
describe
|
169
|
-
it
|
170
|
-
|
171
|
-
must_be_parsed_as s(:lasgn, :foo,
|
172
|
-
|
173
|
-
|
174
|
-
|
168
|
+
describe "with a rescue modifier" do
|
169
|
+
it "works with assigning a bare method call" do
|
170
|
+
_("foo = bar rescue baz")
|
171
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
172
|
+
s(:rescue,
|
173
|
+
s(:call, nil, :bar),
|
174
|
+
s(:resbody, s(:array), s(:call, nil, :baz))))
|
175
175
|
end
|
176
176
|
|
177
|
-
it
|
178
|
-
|
179
|
-
must_be_parsed_as s(:lasgn, :foo,
|
180
|
-
|
181
|
-
|
182
|
-
|
177
|
+
it "works with a method call with argument" do
|
178
|
+
_("foo = bar(baz) rescue qux")
|
179
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
180
|
+
s(:rescue,
|
181
|
+
s(:call, nil, :bar, s(:call, nil, :baz)),
|
182
|
+
s(:resbody, s(:array), s(:call, nil, :qux))))
|
183
183
|
end
|
184
184
|
|
185
|
-
it
|
186
|
-
expected = if RUBY_VERSION <
|
185
|
+
it "works with a method call with argument without brackets" do
|
186
|
+
expected = if RUBY_VERSION < "2.4.0"
|
187
187
|
s(:rescue,
|
188
|
-
s(:lasgn, :foo,
|
188
|
+
s(:lasgn, :foo,
|
189
|
+
s(:call, nil, :bar, s(:call, nil, :baz))),
|
189
190
|
s(:resbody, s(:array), s(:call, nil, :qux)))
|
190
191
|
else
|
191
192
|
s(:lasgn, :foo,
|
@@ -193,13 +194,14 @@ describe RipperRubyParser::Parser do
|
|
193
194
|
s(:call, nil, :bar, s(:call, nil, :baz)),
|
194
195
|
s(:resbody, s(:array), s(:call, nil, :qux))))
|
195
196
|
end
|
196
|
-
|
197
|
+
_("foo = bar baz rescue qux").must_be_parsed_as expected
|
197
198
|
end
|
198
199
|
|
199
|
-
it
|
200
|
-
expected = if RUBY_VERSION <
|
200
|
+
it "works with a class method call with argument without brackets" do
|
201
|
+
expected = if RUBY_VERSION < "2.4.0"
|
201
202
|
s(:rescue,
|
202
|
-
s(:lasgn, :foo,
|
203
|
+
s(:lasgn, :foo,
|
204
|
+
s(:call, s(:const, :Bar), :baz, s(:call, nil, :qux))),
|
203
205
|
s(:resbody, s(:array), s(:call, nil, :quuz)))
|
204
206
|
else
|
205
207
|
s(:lasgn, :foo,
|
@@ -207,14 +209,15 @@ describe RipperRubyParser::Parser do
|
|
207
209
|
s(:call, s(:const, :Bar), :baz, s(:call, nil, :qux)),
|
208
210
|
s(:resbody, s(:array), s(:call, nil, :quuz))))
|
209
211
|
end
|
210
|
-
|
211
|
-
must_be_parsed_as expected
|
212
|
+
_("foo = Bar.baz qux rescue quuz")
|
213
|
+
.must_be_parsed_as expected
|
212
214
|
end
|
213
215
|
|
214
|
-
it
|
215
|
-
expected = if RUBY_VERSION <
|
216
|
+
it "works with a method call with argument without brackets" do
|
217
|
+
expected = if RUBY_VERSION < "2.4.0"
|
216
218
|
s(:rescue,
|
217
|
-
s(:lasgn, :foo,
|
219
|
+
s(:lasgn, :foo,
|
220
|
+
s(:call, nil, :bar, s(:call, nil, :baz))),
|
218
221
|
s(:resbody, s(:array), s(:call, nil, :qux)))
|
219
222
|
else
|
220
223
|
s(:lasgn, :foo,
|
@@ -222,13 +225,14 @@ describe RipperRubyParser::Parser do
|
|
222
225
|
s(:call, nil, :bar, s(:call, nil, :baz)),
|
223
226
|
s(:resbody, s(:array), s(:call, nil, :qux))))
|
224
227
|
end
|
225
|
-
|
228
|
+
_("foo = bar baz rescue qux").must_be_parsed_as expected
|
226
229
|
end
|
227
230
|
|
228
|
-
it
|
229
|
-
expected = if RUBY_VERSION <
|
231
|
+
it "works with a class method call with argument without brackets" do
|
232
|
+
expected = if RUBY_VERSION < "2.4.0"
|
230
233
|
s(:rescue,
|
231
|
-
s(:lasgn, :foo,
|
234
|
+
s(:lasgn, :foo,
|
235
|
+
s(:call, s(:const, :Bar), :baz, s(:call, nil, :qux))),
|
232
236
|
s(:resbody, s(:array), s(:call, nil, :quuz)))
|
233
237
|
else
|
234
238
|
s(:lasgn, :foo,
|
@@ -236,413 +240,373 @@ describe RipperRubyParser::Parser do
|
|
236
240
|
s(:call, s(:const, :Bar), :baz, s(:call, nil, :qux)),
|
237
241
|
s(:resbody, s(:array), s(:call, nil, :quuz))))
|
238
242
|
end
|
239
|
-
|
240
|
-
must_be_parsed_as expected
|
243
|
+
_("foo = Bar.baz qux rescue quuz")
|
244
|
+
.must_be_parsed_as expected
|
241
245
|
end
|
242
246
|
end
|
243
247
|
|
244
|
-
it
|
245
|
-
result = parser.parse
|
246
|
-
result.line.must_equal 1
|
248
|
+
it "sets the correct line numbers" do
|
249
|
+
result = parser.parse "foo = {}"
|
250
|
+
_(result.line).must_equal 1
|
247
251
|
end
|
248
252
|
end
|
249
253
|
|
250
|
-
describe
|
254
|
+
describe "for multiple assignment" do
|
251
255
|
specify do
|
252
|
-
|
253
|
-
must_be_parsed_as s(:masgn,
|
254
|
-
|
255
|
-
|
256
|
+
_("foo, * = bar")
|
257
|
+
.must_be_parsed_as s(:masgn,
|
258
|
+
s(:array, s(:lasgn, :foo), s(:splat)),
|
259
|
+
s(:to_ary, s(:call, nil, :bar)))
|
256
260
|
end
|
257
261
|
|
258
262
|
specify do
|
259
|
-
|
260
|
-
must_be_parsed_as s(:masgn,
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
263
|
+
_("(foo, *bar) = baz")
|
264
|
+
.must_be_parsed_as s(:masgn,
|
265
|
+
s(:array,
|
266
|
+
s(:lasgn, :foo),
|
267
|
+
s(:splat, s(:lasgn, :bar))),
|
268
|
+
s(:to_ary, s(:call, nil, :baz)))
|
265
269
|
end
|
266
270
|
|
267
271
|
specify do
|
268
|
-
|
269
|
-
must_be_parsed_as s(:masgn,
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
end
|
275
|
-
|
276
|
-
it
|
277
|
-
|
278
|
-
must_be_parsed_as s(:masgn,
|
279
|
-
|
280
|
-
|
281
|
-
end
|
282
|
-
|
283
|
-
it
|
284
|
-
|
285
|
-
must_be_parsed_as s(:rescue,
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
end
|
291
|
-
|
292
|
-
it
|
293
|
-
|
294
|
-
must_be_parsed_as s(:masgn,
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
end
|
300
|
-
|
301
|
-
it
|
302
|
-
|
303
|
-
must_be_parsed_as s(:masgn,
|
304
|
-
|
305
|
-
|
306
|
-
end
|
307
|
-
|
308
|
-
it
|
309
|
-
|
310
|
-
must_be_parsed_as s(:masgn,
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
end
|
316
|
-
|
317
|
-
it
|
318
|
-
|
319
|
-
must_be_parsed_as s(:masgn,
|
320
|
-
|
321
|
-
|
322
|
-
end
|
323
|
-
|
324
|
-
it
|
325
|
-
|
326
|
-
must_be_parsed_as s(:masgn,
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
end
|
335
|
-
|
336
|
-
it
|
337
|
-
|
338
|
-
must_be_parsed_as s(:masgn,
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
end
|
352
|
-
|
353
|
-
it
|
354
|
-
|
355
|
-
must_be_parsed_as s(:masgn,
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
end
|
363
|
-
|
364
|
-
it
|
365
|
-
|
366
|
-
must_be_parsed_as s(:masgn,
|
367
|
-
|
368
|
-
|
369
|
-
end
|
370
|
-
|
371
|
-
it
|
372
|
-
|
373
|
-
must_be_parsed_as s(:masgn,
|
374
|
-
|
375
|
-
|
376
|
-
end
|
377
|
-
|
378
|
-
it
|
379
|
-
|
380
|
-
must_be_parsed_as s(:masgn,
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
end
|
386
|
-
|
387
|
-
it
|
388
|
-
|
389
|
-
must_be_parsed_as s(:masgn,
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
end
|
397
|
-
|
398
|
-
it
|
399
|
-
|
400
|
-
must_be_parsed_as s(:masgn,
|
401
|
-
|
402
|
-
|
403
|
-
end
|
404
|
-
|
405
|
-
it
|
406
|
-
|
407
|
-
must_be_parsed_as s(:masgn,
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
end
|
413
|
-
|
414
|
-
it
|
415
|
-
|
416
|
-
must_be_parsed_as s(:masgn,
|
417
|
-
|
418
|
-
|
419
|
-
end
|
420
|
-
|
421
|
-
it
|
422
|
-
|
423
|
-
must_be_parsed_as s(:masgn,
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
end
|
429
|
-
|
430
|
-
it
|
431
|
-
|
432
|
-
must_be_parsed_as s(:masgn,
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
end
|
437
|
-
|
438
|
-
it
|
439
|
-
result = parser.parse
|
440
|
-
result.line.must_equal 1
|
272
|
+
_("*foo, bar = baz")
|
273
|
+
.must_be_parsed_as s(:masgn,
|
274
|
+
s(:array,
|
275
|
+
s(:splat, s(:lasgn, :foo)),
|
276
|
+
s(:lasgn, :bar)),
|
277
|
+
s(:to_ary, s(:call, nil, :baz)))
|
278
|
+
end
|
279
|
+
|
280
|
+
it "works with blocks" do
|
281
|
+
_("foo, bar = begin; baz; end")
|
282
|
+
.must_be_parsed_as s(:masgn,
|
283
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
284
|
+
s(:to_ary, s(:call, nil, :baz)))
|
285
|
+
end
|
286
|
+
|
287
|
+
it "works with a rescue modifier" do
|
288
|
+
_("foo, bar = baz rescue qux")
|
289
|
+
.must_be_parsed_as s(:rescue,
|
290
|
+
s(:masgn,
|
291
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
292
|
+
s(:to_ary, s(:call, nil, :baz))),
|
293
|
+
s(:resbody, s(:array), s(:call, nil, :qux)))
|
294
|
+
end
|
295
|
+
|
296
|
+
it "works the same number of items on each side" do
|
297
|
+
_("foo, bar = baz, qux")
|
298
|
+
.must_be_parsed_as s(:masgn,
|
299
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
300
|
+
s(:array,
|
301
|
+
s(:call, nil, :baz),
|
302
|
+
s(:call, nil, :qux)))
|
303
|
+
end
|
304
|
+
|
305
|
+
it "works with a single item on the right-hand side" do
|
306
|
+
_("foo, bar = baz")
|
307
|
+
.must_be_parsed_as s(:masgn,
|
308
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
309
|
+
s(:to_ary, s(:call, nil, :baz)))
|
310
|
+
end
|
311
|
+
|
312
|
+
it "works with left-hand splat" do
|
313
|
+
_("foo, *bar = baz, qux")
|
314
|
+
.must_be_parsed_as s(:masgn,
|
315
|
+
s(:array, s(:lasgn, :foo), s(:splat, s(:lasgn, :bar))),
|
316
|
+
s(:array,
|
317
|
+
s(:call, nil, :baz),
|
318
|
+
s(:call, nil, :qux)))
|
319
|
+
end
|
320
|
+
|
321
|
+
it "works with parentheses around the left-hand side" do
|
322
|
+
_("(foo, bar) = baz")
|
323
|
+
.must_be_parsed_as s(:masgn,
|
324
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
325
|
+
s(:to_ary, s(:call, nil, :baz)))
|
326
|
+
end
|
327
|
+
|
328
|
+
it "works with complex destructuring" do
|
329
|
+
_("foo, (bar, baz) = qux")
|
330
|
+
.must_be_parsed_as s(:masgn,
|
331
|
+
s(:array,
|
332
|
+
s(:lasgn, :foo),
|
333
|
+
s(:masgn,
|
334
|
+
s(:array,
|
335
|
+
s(:lasgn, :bar),
|
336
|
+
s(:lasgn, :baz)))),
|
337
|
+
s(:to_ary, s(:call, nil, :qux)))
|
338
|
+
end
|
339
|
+
|
340
|
+
it "works with complex destructuring of the value" do
|
341
|
+
_("foo, (bar, baz) = [qux, [quz, quuz]]")
|
342
|
+
.must_be_parsed_as s(:masgn,
|
343
|
+
s(:array,
|
344
|
+
s(:lasgn, :foo),
|
345
|
+
s(:masgn,
|
346
|
+
s(:array,
|
347
|
+
s(:lasgn, :bar),
|
348
|
+
s(:lasgn, :baz)))),
|
349
|
+
s(:to_ary,
|
350
|
+
s(:array,
|
351
|
+
s(:call, nil, :qux),
|
352
|
+
s(:array,
|
353
|
+
s(:call, nil, :quz),
|
354
|
+
s(:call, nil, :quuz)))))
|
355
|
+
end
|
356
|
+
|
357
|
+
it "works with destructuring with multiple levels" do
|
358
|
+
_("((foo, bar)) = baz")
|
359
|
+
.must_be_parsed_as s(:masgn,
|
360
|
+
s(:array,
|
361
|
+
s(:masgn,
|
362
|
+
s(:array,
|
363
|
+
s(:lasgn, :foo),
|
364
|
+
s(:lasgn, :bar)))),
|
365
|
+
s(:to_ary, s(:call, nil, :baz)))
|
366
|
+
end
|
367
|
+
|
368
|
+
it "works with instance variables" do
|
369
|
+
_("@foo, @bar = baz")
|
370
|
+
.must_be_parsed_as s(:masgn,
|
371
|
+
s(:array, s(:iasgn, :@foo), s(:iasgn, :@bar)),
|
372
|
+
s(:to_ary, s(:call, nil, :baz)))
|
373
|
+
end
|
374
|
+
|
375
|
+
it "works with class variables" do
|
376
|
+
_("@@foo, @@bar = baz")
|
377
|
+
.must_be_parsed_as s(:masgn,
|
378
|
+
s(:array, s(:cvdecl, :@@foo), s(:cvdecl, :@@bar)),
|
379
|
+
s(:to_ary, s(:call, nil, :baz)))
|
380
|
+
end
|
381
|
+
|
382
|
+
it "works with attributes" do
|
383
|
+
_("foo.bar, foo.baz = qux")
|
384
|
+
.must_be_parsed_as s(:masgn,
|
385
|
+
s(:array,
|
386
|
+
s(:attrasgn, s(:call, nil, :foo), :bar=),
|
387
|
+
s(:attrasgn, s(:call, nil, :foo), :baz=)),
|
388
|
+
s(:to_ary, s(:call, nil, :qux)))
|
389
|
+
end
|
390
|
+
|
391
|
+
it "works with collection elements" do
|
392
|
+
_("foo[1], bar[2] = baz")
|
393
|
+
.must_be_parsed_as s(:masgn,
|
394
|
+
s(:array,
|
395
|
+
s(:attrasgn,
|
396
|
+
s(:call, nil, :foo), :[]=, s(:lit, 1)),
|
397
|
+
s(:attrasgn,
|
398
|
+
s(:call, nil, :bar), :[]=, s(:lit, 2))),
|
399
|
+
s(:to_ary, s(:call, nil, :baz)))
|
400
|
+
end
|
401
|
+
|
402
|
+
it "works with constants" do
|
403
|
+
_("Foo, Bar = baz")
|
404
|
+
.must_be_parsed_as s(:masgn,
|
405
|
+
s(:array, s(:cdecl, :Foo), s(:cdecl, :Bar)),
|
406
|
+
s(:to_ary, s(:call, nil, :baz)))
|
407
|
+
end
|
408
|
+
|
409
|
+
it "works with instance variables and splat" do
|
410
|
+
_("@foo, *@bar = baz")
|
411
|
+
.must_be_parsed_as s(:masgn,
|
412
|
+
s(:array,
|
413
|
+
s(:iasgn, :@foo),
|
414
|
+
s(:splat, s(:iasgn, :@bar))),
|
415
|
+
s(:to_ary, s(:call, nil, :baz)))
|
416
|
+
end
|
417
|
+
|
418
|
+
it "works with a right-hand single splat" do
|
419
|
+
_("foo, bar = *baz")
|
420
|
+
.must_be_parsed_as s(:masgn,
|
421
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
422
|
+
s(:splat, s(:call, nil, :baz)))
|
423
|
+
end
|
424
|
+
|
425
|
+
it "works with a splat in a list of values on the right hand" do
|
426
|
+
_("foo, bar = baz, *qux")
|
427
|
+
.must_be_parsed_as s(:masgn,
|
428
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
429
|
+
s(:array,
|
430
|
+
s(:call, nil, :baz),
|
431
|
+
s(:splat, s(:call, nil, :qux))))
|
432
|
+
end
|
433
|
+
|
434
|
+
it "works with a right-hand single splat with begin..end block" do
|
435
|
+
_("foo, bar = *begin; baz; end")
|
436
|
+
.must_be_parsed_as s(:masgn,
|
437
|
+
s(:array, s(:lasgn, :foo), s(:lasgn, :bar)),
|
438
|
+
s(:splat,
|
439
|
+
s(:call, nil, :baz)))
|
440
|
+
end
|
441
|
+
|
442
|
+
it "sets the correct line numbers" do
|
443
|
+
result = parser.parse "foo, bar = {}, {}"
|
444
|
+
_(result.line).must_equal 1
|
441
445
|
end
|
442
446
|
end
|
443
447
|
|
444
|
-
describe
|
445
|
-
it
|
446
|
-
|
447
|
-
must_be_parsed_as s(:attrasgn,
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
448
|
+
describe "for assignment to a collection element" do
|
449
|
+
it "handles multiple indices" do
|
450
|
+
_("foo[bar, baz] = qux")
|
451
|
+
.must_be_parsed_as s(:attrasgn,
|
452
|
+
s(:call, nil, :foo),
|
453
|
+
:[]=,
|
454
|
+
s(:call, nil, :bar),
|
455
|
+
s(:call, nil, :baz),
|
456
|
+
s(:call, nil, :qux))
|
453
457
|
end
|
454
458
|
end
|
455
459
|
|
456
|
-
describe
|
457
|
-
it
|
458
|
-
|
459
|
-
must_be_parsed_as s(:lasgn, :foo,
|
460
|
-
|
461
|
-
|
462
|
-
|
460
|
+
describe "for operator assignment" do
|
461
|
+
it "works with +=" do
|
462
|
+
_("foo += bar")
|
463
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
464
|
+
s(:call, s(:lvar, :foo),
|
465
|
+
:+,
|
466
|
+
s(:call, nil, :bar)))
|
463
467
|
end
|
464
468
|
|
465
|
-
it
|
466
|
-
|
467
|
-
must_be_parsed_as s(:lasgn, :foo,
|
468
|
-
|
469
|
-
|
470
|
-
|
469
|
+
it "works with -=" do
|
470
|
+
_("foo -= bar")
|
471
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
472
|
+
s(:call, s(:lvar, :foo),
|
473
|
+
:-,
|
474
|
+
s(:call, nil, :bar)))
|
471
475
|
end
|
472
476
|
|
473
|
-
it
|
474
|
-
|
475
|
-
must_be_parsed_as s(:lasgn, :foo,
|
476
|
-
|
477
|
-
|
478
|
-
|
477
|
+
it "works with *=" do
|
478
|
+
_("foo *= bar")
|
479
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
480
|
+
s(:call, s(:lvar, :foo),
|
481
|
+
:*,
|
482
|
+
s(:call, nil, :bar)))
|
479
483
|
end
|
480
484
|
|
481
|
-
it
|
482
|
-
|
483
|
-
must_be_parsed_as s(:lasgn, :foo,
|
484
|
-
|
485
|
-
|
486
|
-
|
485
|
+
it "works with /=" do
|
486
|
+
_("foo /= bar")
|
487
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
488
|
+
s(:call,
|
489
|
+
s(:lvar, :foo), :/,
|
490
|
+
s(:call, nil, :bar)))
|
487
491
|
end
|
488
492
|
|
489
|
-
it
|
490
|
-
|
491
|
-
must_be_parsed_as s(:op_asgn_or,
|
492
|
-
|
493
|
-
|
494
|
-
|
493
|
+
it "works with ||=" do
|
494
|
+
_("foo ||= bar")
|
495
|
+
.must_be_parsed_as s(:op_asgn_or,
|
496
|
+
s(:lvar, :foo),
|
497
|
+
s(:lasgn, :foo,
|
498
|
+
s(:call, nil, :bar)))
|
495
499
|
end
|
496
500
|
|
497
|
-
it
|
498
|
-
|
499
|
-
must_be_parsed_as s(:iasgn, :@foo,
|
500
|
-
|
501
|
-
|
502
|
-
|
501
|
+
it "works when assigning to an instance variable" do
|
502
|
+
_("@foo += bar")
|
503
|
+
.must_be_parsed_as s(:iasgn, :@foo,
|
504
|
+
s(:call,
|
505
|
+
s(:ivar, :@foo), :+,
|
506
|
+
s(:call, nil, :bar)))
|
503
507
|
end
|
504
508
|
|
505
|
-
it
|
506
|
-
|
507
|
-
must_be_parsed_as s(:op_asgn_and,
|
508
|
-
|
509
|
+
it "works with boolean operators" do
|
510
|
+
_("foo &&= bar")
|
511
|
+
.must_be_parsed_as s(:op_asgn_and,
|
512
|
+
s(:lvar, :foo), s(:lasgn, :foo, s(:call, nil, :bar)))
|
509
513
|
end
|
510
514
|
|
511
|
-
it
|
512
|
-
|
513
|
-
must_be_parsed_as s(:op_asgn_and,
|
514
|
-
|
515
|
+
it "works with boolean operators and blocks" do
|
516
|
+
_("foo &&= begin; bar; end")
|
517
|
+
.must_be_parsed_as s(:op_asgn_and,
|
518
|
+
s(:lvar, :foo), s(:lasgn, :foo, s(:call, nil, :bar)))
|
515
519
|
end
|
516
520
|
|
517
|
-
it
|
518
|
-
|
519
|
-
must_be_parsed_as s(:lasgn, :foo,
|
520
|
-
|
521
|
+
it "works with arithmetic operators and blocks" do
|
522
|
+
_("foo += begin; bar; end")
|
523
|
+
.must_be_parsed_as s(:lasgn, :foo,
|
524
|
+
s(:call, s(:lvar, :foo), :+, s(:call, nil, :bar)))
|
521
525
|
end
|
522
526
|
end
|
523
527
|
|
524
|
-
describe
|
525
|
-
it
|
526
|
-
|
527
|
-
must_be_parsed_as s(:op_asgn2,
|
528
|
-
|
529
|
-
|
530
|
-
|
528
|
+
describe "for operator assignment to an attribute" do
|
529
|
+
it "works with +=" do
|
530
|
+
_("foo.bar += baz")
|
531
|
+
.must_be_parsed_as s(:op_asgn2,
|
532
|
+
s(:call, nil, :foo),
|
533
|
+
:bar=, :+,
|
534
|
+
s(:call, nil, :baz))
|
531
535
|
end
|
532
536
|
|
533
|
-
it
|
534
|
-
|
535
|
-
must_be_parsed_as s(:op_asgn2,
|
536
|
-
|
537
|
-
|
538
|
-
|
537
|
+
it "works with ||=" do
|
538
|
+
_("foo.bar ||= baz")
|
539
|
+
.must_be_parsed_as s(:op_asgn2,
|
540
|
+
s(:call, nil, :foo),
|
541
|
+
:bar=, :'||',
|
542
|
+
s(:call, nil, :baz))
|
539
543
|
end
|
540
544
|
end
|
541
545
|
|
542
|
-
describe
|
543
|
-
it
|
544
|
-
|
545
|
-
must_be_parsed_as s(:op_asgn1,
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
end
|
551
|
-
|
552
|
-
it
|
553
|
-
|
554
|
-
must_be_parsed_as s(:op_asgn1,
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
end
|
560
|
-
|
561
|
-
it
|
562
|
-
|
563
|
-
must_be_parsed_as s(:op_asgn1,
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
end
|
571
|
-
|
572
|
-
it
|
573
|
-
|
574
|
-
must_be_parsed_as s(:op_asgn1,
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
end
|
580
|
-
|
581
|
-
it
|
582
|
-
|
583
|
-
must_be_parsed_as s(:op_asgn1,
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
end
|
589
|
-
|
590
|
-
it
|
591
|
-
|
592
|
-
must_be_parsed_as s(:op_asgn1,
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
end
|
598
|
-
|
599
|
-
it
|
600
|
-
|
601
|
-
must_be_parsed_as s(:op_asgn1,
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
end
|
607
|
-
|
608
|
-
it 'works with a function call without parentheses in extra compatible mode' do
|
609
|
-
'foo[bar] += baz qux'.
|
610
|
-
must_be_parsed_as s(:op_asgn1,
|
611
|
-
s(:call, nil, :foo),
|
612
|
-
s(:array, s(:call, nil, :bar)),
|
613
|
-
:+,
|
614
|
-
s(:call, nil, :baz, s(:call, nil, :qux))),
|
615
|
-
extra_compatible: true
|
616
|
-
end
|
617
|
-
|
618
|
-
it 'works with a function call with parentheses in extra compatible mode' do
|
619
|
-
'foo[bar] += baz(qux)'.
|
620
|
-
must_be_parsed_as s(:op_asgn1,
|
621
|
-
s(:call, nil, :foo),
|
622
|
-
s(:arglist, s(:call, nil, :bar)),
|
623
|
-
:+,
|
624
|
-
s(:call, nil, :baz, s(:call, nil, :qux))),
|
625
|
-
extra_compatible: true
|
626
|
-
end
|
627
|
-
|
628
|
-
it 'works with a method call without parentheses in extra compatible mode' do
|
629
|
-
'foo[bar] += baz.qux quuz'.
|
630
|
-
must_be_parsed_as s(:op_asgn1,
|
631
|
-
s(:call, nil, :foo),
|
632
|
-
s(:array, s(:call, nil, :bar)),
|
633
|
-
:+,
|
634
|
-
s(:call, s(:call, nil, :baz), :qux, s(:call, nil, :quuz))),
|
635
|
-
extra_compatible: true
|
636
|
-
end
|
637
|
-
|
638
|
-
it 'works with a method call with parentheses in extra compatible mode' do
|
639
|
-
'foo[bar] += baz.qux(quuz)'.
|
640
|
-
must_be_parsed_as s(:op_asgn1,
|
641
|
-
s(:call, nil, :foo),
|
642
|
-
s(:arglist, s(:call, nil, :bar)),
|
643
|
-
:+,
|
644
|
-
s(:call, s(:call, nil, :baz), :qux, s(:call, nil, :quuz))),
|
645
|
-
extra_compatible: true
|
546
|
+
describe "for operator assignment to a collection element" do
|
547
|
+
it "works with +=" do
|
548
|
+
_("foo[bar] += baz")
|
549
|
+
.must_be_parsed_as s(:op_asgn1,
|
550
|
+
s(:call, nil, :foo),
|
551
|
+
s(:arglist, s(:call, nil, :bar)),
|
552
|
+
:+,
|
553
|
+
s(:call, nil, :baz))
|
554
|
+
end
|
555
|
+
|
556
|
+
it "works with ||=" do
|
557
|
+
_("foo[bar] ||= baz")
|
558
|
+
.must_be_parsed_as s(:op_asgn1,
|
559
|
+
s(:call, nil, :foo),
|
560
|
+
s(:arglist, s(:call, nil, :bar)),
|
561
|
+
:"||",
|
562
|
+
s(:call, nil, :baz))
|
563
|
+
end
|
564
|
+
|
565
|
+
it "handles multiple indices" do
|
566
|
+
_("foo[bar, baz] += qux")
|
567
|
+
.must_be_parsed_as s(:op_asgn1,
|
568
|
+
s(:call, nil, :foo),
|
569
|
+
s(:arglist,
|
570
|
+
s(:call, nil, :bar),
|
571
|
+
s(:call, nil, :baz)),
|
572
|
+
:+,
|
573
|
+
s(:call, nil, :qux))
|
574
|
+
end
|
575
|
+
|
576
|
+
it "works with a function call without parentheses" do
|
577
|
+
_("foo[bar] += baz qux")
|
578
|
+
.must_be_parsed_as s(:op_asgn1,
|
579
|
+
s(:call, nil, :foo),
|
580
|
+
s(:arglist, s(:call, nil, :bar)),
|
581
|
+
:+,
|
582
|
+
s(:call, nil, :baz, s(:call, nil, :qux)))
|
583
|
+
end
|
584
|
+
|
585
|
+
it "works with a function call with parentheses" do
|
586
|
+
_("foo[bar] += baz(qux)")
|
587
|
+
.must_be_parsed_as s(:op_asgn1,
|
588
|
+
s(:call, nil, :foo),
|
589
|
+
s(:arglist, s(:call, nil, :bar)),
|
590
|
+
:+,
|
591
|
+
s(:call, nil, :baz, s(:call, nil, :qux)))
|
592
|
+
end
|
593
|
+
|
594
|
+
it "works with a method call without parentheses" do
|
595
|
+
_("foo[bar] += baz.qux quuz")
|
596
|
+
.must_be_parsed_as s(:op_asgn1,
|
597
|
+
s(:call, nil, :foo),
|
598
|
+
s(:arglist, s(:call, nil, :bar)),
|
599
|
+
:+,
|
600
|
+
s(:call, s(:call, nil, :baz), :qux, s(:call, nil, :quuz)))
|
601
|
+
end
|
602
|
+
|
603
|
+
it "works with a method call with parentheses" do
|
604
|
+
_("foo[bar] += baz.qux(quuz)")
|
605
|
+
.must_be_parsed_as s(:op_asgn1,
|
606
|
+
s(:call, nil, :foo),
|
607
|
+
s(:arglist, s(:call, nil, :bar)),
|
608
|
+
:+,
|
609
|
+
s(:call, s(:call, nil, :baz), :qux, s(:call, nil, :quuz)))
|
646
610
|
end
|
647
611
|
end
|
648
612
|
end
|