parser 0.9.alpha1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/AST_FORMAT.md +1338 -0
- data/README.md +58 -3
- data/Rakefile +32 -12
- data/bin/benchmark +47 -0
- data/bin/explain-parse +14 -0
- data/bin/parse +6 -0
- data/lib/parser.rb +84 -0
- data/lib/parser/all.rb +2 -0
- data/lib/parser/ast/node.rb +11 -0
- data/lib/parser/ast/processor.rb +8 -0
- data/lib/parser/base.rb +116 -0
- data/lib/parser/builders/default.rb +654 -0
- data/lib/parser/compatibility/ruby1_8.rb +13 -0
- data/lib/parser/diagnostic.rb +44 -0
- data/lib/parser/diagnostic/engine.rb +44 -0
- data/lib/parser/lexer.rl +335 -245
- data/lib/parser/lexer/explanation.rb +37 -0
- data/lib/parser/{lexer_literal.rb → lexer/literal.rb} +22 -12
- data/lib/parser/lexer/stack_state.rb +38 -0
- data/lib/parser/ruby18.y +1957 -0
- data/lib/parser/ruby19.y +2154 -0
- data/lib/parser/source/buffer.rb +78 -0
- data/lib/parser/source/map.rb +20 -0
- data/lib/parser/source/map/operator.rb +15 -0
- data/lib/parser/source/map/variable_assignment.rb +15 -0
- data/lib/parser/source/range.rb +66 -0
- data/lib/parser/static_environment.rb +12 -6
- data/parser.gemspec +23 -13
- data/test/helper.rb +45 -0
- data/test/parse_helper.rb +204 -0
- data/test/racc_coverage_helper.rb +130 -0
- data/test/test_diagnostic.rb +47 -0
- data/test/test_diagnostic_engine.rb +58 -0
- data/test/test_lexer.rb +601 -357
- data/test/test_lexer_stack_state.rb +69 -0
- data/test/test_parse_helper.rb +74 -0
- data/test/test_parser.rb +3654 -0
- data/test/test_source_buffer.rb +80 -0
- data/test/test_source_range.rb +51 -0
- data/test/test_static_environment.rb +1 -4
- metadata +137 -12
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestLexerStackState < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@state = Parser::Lexer::StackState.new('state')
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_push
|
9
|
+
refute @state.active?
|
10
|
+
|
11
|
+
assert_equal true, @state.push(true)
|
12
|
+
assert @state.active?
|
13
|
+
|
14
|
+
assert_equal false, @state.push(false)
|
15
|
+
refute @state.active?
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_pop
|
19
|
+
@state.push(true)
|
20
|
+
|
21
|
+
assert_equal true, @state.pop
|
22
|
+
refute @state.active?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_pop_empty
|
26
|
+
assert_equal false, @state.pop
|
27
|
+
refute @state.active?
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_lexpop_10
|
31
|
+
@state.push(true)
|
32
|
+
@state.push(false)
|
33
|
+
|
34
|
+
assert_equal true, @state.lexpop
|
35
|
+
assert_equal true, @state.pop
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_lexpop_01
|
39
|
+
@state.push(false)
|
40
|
+
@state.push(true)
|
41
|
+
|
42
|
+
assert_equal true, @state.lexpop
|
43
|
+
assert_equal true, @state.pop
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_lexpop_00
|
47
|
+
@state.push(false)
|
48
|
+
@state.push(false)
|
49
|
+
|
50
|
+
assert_equal false, @state.lexpop
|
51
|
+
assert_equal false, @state.pop
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_dup
|
55
|
+
@state.push(true)
|
56
|
+
new_state = @state.dup
|
57
|
+
|
58
|
+
assert_equal true, @state.pop
|
59
|
+
assert_equal true, new_state.pop
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_to_s
|
63
|
+
@state.push(true)
|
64
|
+
@state.push(false)
|
65
|
+
@state.push(false)
|
66
|
+
|
67
|
+
assert_equal '[100 <= state]', @state.to_s
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'parse_helper'
|
3
|
+
|
4
|
+
class TestParseHelper < MiniTest::Unit::TestCase
|
5
|
+
include ParseHelper
|
6
|
+
|
7
|
+
def test_parser_for_ruby_version
|
8
|
+
assert_instance_of Parser::Ruby18,
|
9
|
+
parser_for_ruby_version('1.8')
|
10
|
+
|
11
|
+
assert_instance_of Parser::Ruby19,
|
12
|
+
parser_for_ruby_version('1.9')
|
13
|
+
|
14
|
+
# assert_instance_of Parser::Ruby20,
|
15
|
+
# parser_for_ruby_version('2.0')
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_maps(what)
|
19
|
+
parse_source_map_descriptions(what).to_a
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_parse_mapsation_description
|
23
|
+
assert_equal [[0, 3, 'expr', [], '~~~~ expr']],
|
24
|
+
parse_maps('~~~~ expr')
|
25
|
+
|
26
|
+
assert_equal [[0, 3, 'expr', [], '^~~~ expr']],
|
27
|
+
parse_maps('^~~~ expr')
|
28
|
+
|
29
|
+
assert_equal [[0, 3, 'expr', [], '^^^^ expr']],
|
30
|
+
parse_maps('^^^^ expr')
|
31
|
+
|
32
|
+
assert_equal [[2, 2, 'op', [], ' ^ op']],
|
33
|
+
parse_maps(' ^ op')
|
34
|
+
|
35
|
+
assert_equal [[2, 2, 'op', ['foo'], ' ~ op (foo)']],
|
36
|
+
parse_maps(' ~ op (foo)')
|
37
|
+
|
38
|
+
assert_equal [[2, 3, 'op', ['foo', 'bar'], ' ~~ op (foo.bar)']],
|
39
|
+
parse_maps(' ~~ op (foo.bar)')
|
40
|
+
|
41
|
+
assert_equal [[2, 3, 'op', ['foo/2', 'bar'], ' ~~ op (foo/2.bar)']],
|
42
|
+
parse_maps(' ~~ op (foo/2.bar)')
|
43
|
+
|
44
|
+
assert_equal [[0, 3, 'expr', [], '~~~~ expr'],
|
45
|
+
[5, 6, 'op', ['str', 'e_h'], ' ~~ op (str.e_h)']],
|
46
|
+
parse_maps(%{
|
47
|
+
|~~~~ expr
|
48
|
+
| ~~ op (str.e_h)
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_traverse_ast
|
53
|
+
ast = s(:send,
|
54
|
+
s(:int, 1), :+,
|
55
|
+
s(:dstr,
|
56
|
+
s(:str, "foo"),
|
57
|
+
s(:int, 2),
|
58
|
+
s(:int, 3)))
|
59
|
+
|
60
|
+
assert_equal ast, traverse_ast(ast, %w())
|
61
|
+
|
62
|
+
assert_equal s(:int, 1), traverse_ast(ast, %w(int))
|
63
|
+
assert_equal nil, traverse_ast(ast, %w(str))
|
64
|
+
|
65
|
+
assert_equal s(:str, "foo"), traverse_ast(ast, %w(dstr str))
|
66
|
+
assert_equal s(:int, 2), traverse_ast(ast, %w(dstr int/1))
|
67
|
+
assert_equal s(:int, 3), traverse_ast(ast, %w(dstr int/2))
|
68
|
+
assert_equal nil, traverse_ast(ast, %w(dstr int/3))
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_assert_parses
|
72
|
+
# Someone more clever and/or motivated than me is going to test this.
|
73
|
+
end
|
74
|
+
end
|
data/test/test_parser.rb
ADDED
@@ -0,0 +1,3654 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'parse_helper'
|
3
|
+
|
4
|
+
class TestParser < MiniTest::Unit::TestCase
|
5
|
+
include ParseHelper
|
6
|
+
|
7
|
+
def parser_for_ruby_version(version)
|
8
|
+
parser = super
|
9
|
+
parser.diagnostics.all_errors_are_fatal = true
|
10
|
+
|
11
|
+
%w(foo bar baz).each do |metasyntactic_var|
|
12
|
+
parser.static_env.declare(metasyntactic_var)
|
13
|
+
end
|
14
|
+
|
15
|
+
parser
|
16
|
+
end
|
17
|
+
|
18
|
+
# Guidelines for test naming:
|
19
|
+
# * Test structure follows structure of AST_FORMAT.md.
|
20
|
+
# * Test names follow node names.
|
21
|
+
# * Structurally similar sources may be grouped into one test.
|
22
|
+
# * If, following the guidelines above, names clash, append
|
23
|
+
# an abbreviated disambiguator. E.g. `test_class` and
|
24
|
+
# `test_class_super`.
|
25
|
+
# * When writing a test for a bug, append unabbreviated (but
|
26
|
+
# concise) bug description. E.g. `test_class_bug_missing_newline`.
|
27
|
+
# * Do not append Ruby language version to the name.
|
28
|
+
# * When in doubt, look at existing test names.
|
29
|
+
#
|
30
|
+
# Guidelines for writing assertions:
|
31
|
+
# * Don't check for structurally same source mapping information
|
32
|
+
# more than once or twice in the entire file. It clutters the
|
33
|
+
# source for no reason.
|
34
|
+
# * Don't forget to check for optional delimiters. `()`, `then`, etc.
|
35
|
+
# * When in doubt, look at existing assertions.
|
36
|
+
|
37
|
+
#
|
38
|
+
# Literals
|
39
|
+
#
|
40
|
+
|
41
|
+
def test_empty_stmt
|
42
|
+
assert_parses(
|
43
|
+
s(:nil),
|
44
|
+
%q{})
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_nil
|
48
|
+
assert_parses(
|
49
|
+
s(:nil),
|
50
|
+
%q{nil},
|
51
|
+
%q{~~~ expression})
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_true
|
55
|
+
assert_parses(
|
56
|
+
s(:true),
|
57
|
+
%q{true},
|
58
|
+
%q{~~~~ expression})
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_false
|
62
|
+
assert_parses(
|
63
|
+
s(:false),
|
64
|
+
%q{false},
|
65
|
+
%q{~~~~~ expression})
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_int
|
69
|
+
assert_parses(
|
70
|
+
s(:int, 42),
|
71
|
+
%q{42},
|
72
|
+
%q{~~ expression})
|
73
|
+
|
74
|
+
assert_parses(
|
75
|
+
s(:int, -42),
|
76
|
+
%q{-42},
|
77
|
+
%q{~~~ expression})
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_int___LINE__
|
81
|
+
assert_parses(
|
82
|
+
s(:int, 1),
|
83
|
+
%q{__LINE__},
|
84
|
+
%q{~~~~~~~~ expression})
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_float
|
88
|
+
assert_parses(
|
89
|
+
s(:float, 1.33),
|
90
|
+
%q{1.33},
|
91
|
+
%q{~~~~ expression})
|
92
|
+
|
93
|
+
assert_parses(
|
94
|
+
s(:float, -1.33),
|
95
|
+
%q{-1.33},
|
96
|
+
%q{~~~~~ expression})
|
97
|
+
end
|
98
|
+
|
99
|
+
# Strings
|
100
|
+
|
101
|
+
def test_string_plain
|
102
|
+
assert_parses(
|
103
|
+
s(:str, 'foobar'),
|
104
|
+
%q{'foobar'},
|
105
|
+
%q{^ begin
|
106
|
+
| ^ end
|
107
|
+
|~~~~~~~~ expression})
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_string_interp
|
111
|
+
assert_parses(
|
112
|
+
s(:dstr,
|
113
|
+
s(:str, 'foo'),
|
114
|
+
s(:lvar, :bar),
|
115
|
+
s(:str, 'baz')),
|
116
|
+
%q{"foo#{bar}baz"},
|
117
|
+
%q{^ begin
|
118
|
+
| ^ end
|
119
|
+
|~~~~~~~~~~~~~~ expression})
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_string_dvar
|
123
|
+
assert_parses(
|
124
|
+
s(:dstr,
|
125
|
+
s(:ivar, :@a),
|
126
|
+
s(:str, ' '),
|
127
|
+
s(:cvar, :@@a),
|
128
|
+
s(:str, ' '),
|
129
|
+
s(:gvar, :$a)),
|
130
|
+
%q{"#@a #@@a #$a"})
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_string_concat
|
134
|
+
assert_parses(
|
135
|
+
s(:dstr,
|
136
|
+
s(:dstr,
|
137
|
+
s(:str, 'foo'),
|
138
|
+
s(:ivar, :@a)),
|
139
|
+
s(:str, 'bar')),
|
140
|
+
%q{"foo#@a" "bar"},
|
141
|
+
%q{^ begin
|
142
|
+
|^ begin (dstr)
|
143
|
+
| ^ end (dstr)
|
144
|
+
| ^ begin (str)
|
145
|
+
| ^ end (str)
|
146
|
+
| ^ end
|
147
|
+
|~~~~~~~~~~~~~~ expression})
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_string___FILE__
|
151
|
+
assert_parses(
|
152
|
+
s(:str, '(assert_parses)'),
|
153
|
+
%q{__FILE__},
|
154
|
+
%q{~~~~~~~~ expression})
|
155
|
+
end
|
156
|
+
|
157
|
+
# Symbols
|
158
|
+
|
159
|
+
def test_symbol_plain
|
160
|
+
assert_parses(
|
161
|
+
s(:sym, :foo),
|
162
|
+
%q{:foo},
|
163
|
+
%q{~~~~ expression})
|
164
|
+
|
165
|
+
assert_parses(
|
166
|
+
s(:sym, :foo),
|
167
|
+
%q{:'foo'},
|
168
|
+
%q{ ^ begin
|
169
|
+
| ^ end
|
170
|
+
|~~~~~~ expression})
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_symbol_interp
|
174
|
+
assert_parses(
|
175
|
+
s(:dsym,
|
176
|
+
s(:str, 'foo'),
|
177
|
+
s(:lvar, :bar),
|
178
|
+
s(:str, 'baz')),
|
179
|
+
%q{:"foo#{bar}baz"},
|
180
|
+
%q{ ^ begin
|
181
|
+
| ^ end
|
182
|
+
| ~~~~~~~~~~~~~~ expression})
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_symbol_empty
|
186
|
+
assert_diagnoses(
|
187
|
+
[:error, :empty_symbol],
|
188
|
+
%q{:''},
|
189
|
+
%q{^^^ location},
|
190
|
+
%w(1.8))
|
191
|
+
|
192
|
+
assert_diagnoses(
|
193
|
+
[:error, :empty_symbol],
|
194
|
+
%q{:""},
|
195
|
+
%q{^^^ location},
|
196
|
+
%w(1.8))
|
197
|
+
end
|
198
|
+
|
199
|
+
# Execute-strings
|
200
|
+
|
201
|
+
def test_xstring_plain
|
202
|
+
assert_parses(
|
203
|
+
s(:xstr, s(:str, 'foobar')),
|
204
|
+
%q{`foobar`},
|
205
|
+
%q{^ begin
|
206
|
+
| ^ end
|
207
|
+
|~~~~~~~~ expression})
|
208
|
+
end
|
209
|
+
|
210
|
+
def test_xstring_interp
|
211
|
+
assert_parses(
|
212
|
+
s(:xstr,
|
213
|
+
s(:str, 'foo'),
|
214
|
+
s(:lvar, :bar),
|
215
|
+
s(:str, 'baz')),
|
216
|
+
%q{`foo#{bar}baz`},
|
217
|
+
%q{^ begin
|
218
|
+
| ^ end
|
219
|
+
|~~~~~~~~~~~~~~ expression})
|
220
|
+
end
|
221
|
+
|
222
|
+
# Regexp
|
223
|
+
|
224
|
+
def test_regex_plain
|
225
|
+
assert_parses(
|
226
|
+
s(:regexp, s(:str, 'source'), s(:regopt, :i, :m)),
|
227
|
+
%q{/source/im},
|
228
|
+
%q{^ begin
|
229
|
+
| ^ end
|
230
|
+
| ~~ expression (regopt)
|
231
|
+
|~~~~~~~~~~ expression})
|
232
|
+
end
|
233
|
+
|
234
|
+
def test_regex_interp
|
235
|
+
assert_parses(
|
236
|
+
s(:regexp,
|
237
|
+
s(:str, 'foo'),
|
238
|
+
s(:lvar, :bar),
|
239
|
+
s(:str, 'baz'),
|
240
|
+
s(:regopt)),
|
241
|
+
%q{/foo#{bar}baz/},
|
242
|
+
%q{^ begin
|
243
|
+
| ^ end
|
244
|
+
|~~~~~~~~~~~~~~ expression})
|
245
|
+
end
|
246
|
+
|
247
|
+
# Arrays
|
248
|
+
|
249
|
+
def test_array_plain
|
250
|
+
assert_parses(
|
251
|
+
s(:array, s(:int, 1), s(:int, 2)),
|
252
|
+
%q{[1, 2]},
|
253
|
+
%q{^ begin
|
254
|
+
| ^ end
|
255
|
+
|~~~~~~ expression})
|
256
|
+
end
|
257
|
+
|
258
|
+
def test_array_splat
|
259
|
+
assert_parses(
|
260
|
+
s(:array,
|
261
|
+
s(:int, 1),
|
262
|
+
s(:splat, s(:lvar, :foo)),
|
263
|
+
s(:int, 2)),
|
264
|
+
%q{[1, *foo, 2]},
|
265
|
+
%q{^ begin
|
266
|
+
| ^ end
|
267
|
+
| ^ operator (splat)
|
268
|
+
| ~~~~ expression (splat)
|
269
|
+
|~~~~~~~~~~~~ expression},
|
270
|
+
ALL_VERSIONS - %w(1.8))
|
271
|
+
|
272
|
+
assert_parses(
|
273
|
+
s(:array,
|
274
|
+
s(:int, 1),
|
275
|
+
s(:splat, s(:lvar, :foo))),
|
276
|
+
%q{[1, *foo]},
|
277
|
+
%q{^ begin
|
278
|
+
| ^ end
|
279
|
+
| ^ operator (splat)
|
280
|
+
| ~~~~ expression (splat)
|
281
|
+
|~~~~~~~~~ expression})
|
282
|
+
|
283
|
+
assert_parses(
|
284
|
+
s(:array,
|
285
|
+
s(:splat, s(:lvar, :foo))),
|
286
|
+
%q{[*foo]})
|
287
|
+
end
|
288
|
+
|
289
|
+
def test_array_assocs
|
290
|
+
assert_parses(
|
291
|
+
s(:array,
|
292
|
+
s(:hash, s(:pair, s(:int, 1), s(:int, 2)))),
|
293
|
+
%q{[ 1 => 2 ]},
|
294
|
+
%q{ ~~ operator (hash.pair)
|
295
|
+
| ~~~~~~ expression (hash.pair)
|
296
|
+
| ~~~~~~ expression (hash)})
|
297
|
+
|
298
|
+
assert_parses(
|
299
|
+
s(:array,
|
300
|
+
s(:int, 1),
|
301
|
+
s(:hash, s(:pair, s(:int, 2), s(:int, 3)))),
|
302
|
+
%q{[ 1, 2 => 3 ]},
|
303
|
+
%q{},
|
304
|
+
ALL_VERSIONS - %w(1.8))
|
305
|
+
end
|
306
|
+
|
307
|
+
def test_array_words
|
308
|
+
assert_parses(
|
309
|
+
s(:array, s(:str, "foo"), s(:str, "bar")),
|
310
|
+
%q{%w[foo bar]},
|
311
|
+
%q{^^^ begin
|
312
|
+
| ^ end
|
313
|
+
| ~~~ expression (str)
|
314
|
+
|~~~~~~~~~~~ expression})
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_array_words_interp
|
318
|
+
assert_parses(
|
319
|
+
s(:array, s(:str, "foo"), s(:lvar, :bar)),
|
320
|
+
%q{%W[foo #{bar}]},
|
321
|
+
%q{^^^ begin
|
322
|
+
| ^ end
|
323
|
+
| ~~~ expression (str)
|
324
|
+
| ~~~ expression (lvar)
|
325
|
+
|~~~~~~~~~~~~~~ expression})
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_array_words_empty
|
329
|
+
assert_parses(
|
330
|
+
s(:array),
|
331
|
+
%q{%w[]},
|
332
|
+
%q{^^^ begin
|
333
|
+
| ^ end
|
334
|
+
|~~~~ expression})
|
335
|
+
|
336
|
+
assert_parses(
|
337
|
+
s(:array),
|
338
|
+
%q{%W()})
|
339
|
+
end
|
340
|
+
|
341
|
+
# Hashes
|
342
|
+
|
343
|
+
def test_hash_empty
|
344
|
+
assert_parses(
|
345
|
+
s(:hash),
|
346
|
+
%q[{ }],
|
347
|
+
%q{^ begin
|
348
|
+
| ^ end
|
349
|
+
|~~~ expression})
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_hash_hashrocket
|
353
|
+
assert_parses(
|
354
|
+
s(:hash, s(:pair, s(:int, 1), s(:int, 2))),
|
355
|
+
%q[{ 1 => 2 }],
|
356
|
+
%q{^ begin
|
357
|
+
| ^ end
|
358
|
+
| ^^ operator (pair)
|
359
|
+
| ~~~~~~ expression (pair)
|
360
|
+
|~~~~~~~~~~ expression})
|
361
|
+
|
362
|
+
assert_parses(
|
363
|
+
s(:hash,
|
364
|
+
s(:pair, s(:int, 1), s(:int, 2)),
|
365
|
+
s(:pair, s(:sym, :foo), s(:str, 'bar'))),
|
366
|
+
%q[{ 1 => 2, :foo => "bar" }])
|
367
|
+
end
|
368
|
+
|
369
|
+
def test_hash_label
|
370
|
+
assert_parses(
|
371
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 2))),
|
372
|
+
%q[{ foo: 2 }],
|
373
|
+
%q{^ begin
|
374
|
+
| ^ end
|
375
|
+
| ^ operator (pair)
|
376
|
+
| ~~~ expression (pair.sym)
|
377
|
+
| ~~~~~~ expression (pair)
|
378
|
+
|~~~~~~~~~~ expression},
|
379
|
+
ALL_VERSIONS - %w(1.8))
|
380
|
+
end
|
381
|
+
|
382
|
+
# def test_hash_kwsplat
|
383
|
+
# assert_parses(
|
384
|
+
# s(:hash,
|
385
|
+
# s(:pair, s(:symbol, :foo), s(:int, 2))
|
386
|
+
# s(:kwsplat, s(:lvar, :bar))),
|
387
|
+
# %q[{ foo: 2, **bar }],
|
388
|
+
# %q{ ^^ operator (kwsplat)
|
389
|
+
# | ~~~~~ expression (kwsplat)},
|
390
|
+
# %w(2.0))
|
391
|
+
# end
|
392
|
+
|
393
|
+
def test_hash_no_hashrocket
|
394
|
+
assert_parses(
|
395
|
+
s(:hash, s(:pair, s(:int, 1), s(:int, 2))),
|
396
|
+
%q[{ 1, 2 }],
|
397
|
+
%q{^ begin
|
398
|
+
| ^ end
|
399
|
+
| ^ operator (pair)
|
400
|
+
| ~~~~ expression (pair)
|
401
|
+
|~~~~~~~~ expression},
|
402
|
+
%w(1.8))
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_hash_no_hashrocket_odd
|
406
|
+
assert_diagnoses(
|
407
|
+
[:error, :odd_hash],
|
408
|
+
%q[{ 1, 2, 3 }],
|
409
|
+
%q( ~ location),
|
410
|
+
%w(1.8))
|
411
|
+
end
|
412
|
+
|
413
|
+
# Range
|
414
|
+
|
415
|
+
def test_range_inclusive
|
416
|
+
assert_parses(
|
417
|
+
s(:irange, s(:int, 1), s(:int, 2)),
|
418
|
+
%q{1..2},
|
419
|
+
%q{ ~~ operator
|
420
|
+
|~~~~ expression})
|
421
|
+
end
|
422
|
+
|
423
|
+
def test_range_exclusive
|
424
|
+
assert_parses(
|
425
|
+
s(:erange, s(:int, 1), s(:int, 2)),
|
426
|
+
%q{1...2},
|
427
|
+
%q{ ~~~ operator
|
428
|
+
|~~~~~ expression})
|
429
|
+
end
|
430
|
+
|
431
|
+
#
|
432
|
+
# Access
|
433
|
+
#
|
434
|
+
|
435
|
+
# Variables and pseudovariables
|
436
|
+
|
437
|
+
def test_self
|
438
|
+
assert_parses(
|
439
|
+
s(:self),
|
440
|
+
%q{self},
|
441
|
+
%q{~~~~ expression})
|
442
|
+
end
|
443
|
+
|
444
|
+
def test_lvar
|
445
|
+
assert_parses(
|
446
|
+
s(:lvar, :foo),
|
447
|
+
%q{foo},
|
448
|
+
%q{~~~ expression})
|
449
|
+
end
|
450
|
+
|
451
|
+
def test_ivar
|
452
|
+
assert_parses(
|
453
|
+
s(:ivar, :@foo),
|
454
|
+
%q{@foo},
|
455
|
+
%q{~~~~ expression})
|
456
|
+
end
|
457
|
+
|
458
|
+
def test_cvar
|
459
|
+
assert_parses(
|
460
|
+
s(:cvar, :@@foo),
|
461
|
+
%q{@@foo},
|
462
|
+
%q{~~~~~ expression})
|
463
|
+
end
|
464
|
+
|
465
|
+
def test_gvar
|
466
|
+
assert_parses(
|
467
|
+
s(:gvar, :$foo),
|
468
|
+
%q{$foo},
|
469
|
+
%q{~~~~ expression})
|
470
|
+
end
|
471
|
+
|
472
|
+
def test_back_ref
|
473
|
+
assert_parses(
|
474
|
+
s(:back_ref, :$+),
|
475
|
+
%q{$+},
|
476
|
+
%q{~~ expression})
|
477
|
+
end
|
478
|
+
|
479
|
+
def test_nth_ref
|
480
|
+
assert_parses(
|
481
|
+
s(:nth_ref, 10),
|
482
|
+
%q{$10},
|
483
|
+
%q{~~~ expression})
|
484
|
+
end
|
485
|
+
|
486
|
+
# Constants
|
487
|
+
|
488
|
+
def test_const_toplevel
|
489
|
+
assert_parses(
|
490
|
+
s(:const, s(:cbase), :Foo),
|
491
|
+
%q{::Foo},
|
492
|
+
%q{ ~~~ name
|
493
|
+
|~~~~~ expression})
|
494
|
+
end
|
495
|
+
|
496
|
+
def test_const_scoped
|
497
|
+
assert_parses(
|
498
|
+
s(:const, s(:const, nil, :Bar), :Foo),
|
499
|
+
%q{Bar::Foo},
|
500
|
+
%q{ ~~~ name
|
501
|
+
|~~~~~~~~ expression})
|
502
|
+
end
|
503
|
+
|
504
|
+
def test_const_unscoped
|
505
|
+
assert_parses(
|
506
|
+
s(:const, nil, :Foo),
|
507
|
+
%q{Foo},
|
508
|
+
%q{~~~ name
|
509
|
+
|~~~ expression})
|
510
|
+
end
|
511
|
+
|
512
|
+
def test___ENCODING__
|
513
|
+
assert_parses(
|
514
|
+
s(:const, s(:const, nil, :Encoding), :UTF_8),
|
515
|
+
%q{__ENCODING__},
|
516
|
+
%q{~~~~~~~~~~~~ expression},
|
517
|
+
ALL_VERSIONS - %w(1.8))
|
518
|
+
end
|
519
|
+
|
520
|
+
# defined?
|
521
|
+
|
522
|
+
def test_defined
|
523
|
+
assert_parses(
|
524
|
+
s(:defined?, s(:lvar, :foo)),
|
525
|
+
%q{defined? foo},
|
526
|
+
%q{~~~~~~~~ keyword
|
527
|
+
|~~~~~~~~~~~~ expression})
|
528
|
+
|
529
|
+
assert_parses(
|
530
|
+
s(:defined?, s(:lvar, :foo)),
|
531
|
+
%q{defined?(foo)},
|
532
|
+
%q{~~~~~~~~ keyword
|
533
|
+
| ^ begin
|
534
|
+
| ^ end
|
535
|
+
|~~~~~~~~~~~~~ expression})
|
536
|
+
|
537
|
+
assert_parses(
|
538
|
+
s(:defined?, s(:ivar, :@foo)),
|
539
|
+
%q{defined? @foo})
|
540
|
+
end
|
541
|
+
|
542
|
+
#
|
543
|
+
# Assignment
|
544
|
+
#
|
545
|
+
|
546
|
+
# Variables
|
547
|
+
|
548
|
+
def test_lvasgn
|
549
|
+
assert_parses(
|
550
|
+
s(:begin,
|
551
|
+
s(:lvasgn, :var, s(:int, 10)),
|
552
|
+
s(:lvar, :var)),
|
553
|
+
%q{var = 10; var},
|
554
|
+
%q{~~~ name (lvasgn)
|
555
|
+
| ^ operator (lvasgn)
|
556
|
+
|~~~~~~~~ expression (lvasgn)
|
557
|
+
})
|
558
|
+
end
|
559
|
+
|
560
|
+
def test_ivasgn
|
561
|
+
assert_parses(
|
562
|
+
s(:ivasgn, :@var, s(:int, 10)),
|
563
|
+
%q{@var = 10},
|
564
|
+
%q{~~~~ name
|
565
|
+
| ^ operator
|
566
|
+
|~~~~~~~~~ expression
|
567
|
+
})
|
568
|
+
end
|
569
|
+
|
570
|
+
def test_cvdecl
|
571
|
+
assert_parses(
|
572
|
+
s(:cvdecl, :@@var, s(:int, 10)),
|
573
|
+
%q{@@var = 10},
|
574
|
+
%q{~~~~~ name
|
575
|
+
| ^ operator
|
576
|
+
|~~~~~~~~~~ expression
|
577
|
+
})
|
578
|
+
end
|
579
|
+
|
580
|
+
def test_cvasgn
|
581
|
+
assert_parses(
|
582
|
+
s(:def, :a, s(:args),
|
583
|
+
s(:cvasgn, :@@var, s(:int, 10))),
|
584
|
+
%q{def a; @@var = 10; end},
|
585
|
+
%q{ ~~~~~ name (cvasgn)
|
586
|
+
| ^ operator (cvasgn)
|
587
|
+
| ~~~~~~~~~~ expression (cvasgn)
|
588
|
+
})
|
589
|
+
end
|
590
|
+
|
591
|
+
def test_gvasgn
|
592
|
+
assert_parses(
|
593
|
+
s(:gvasgn, :$var, s(:int, 10)),
|
594
|
+
%q{$var = 10},
|
595
|
+
%q{~~~~ name
|
596
|
+
| ^ operator
|
597
|
+
|~~~~~~~~~ expression
|
598
|
+
})
|
599
|
+
end
|
600
|
+
|
601
|
+
def test_asgn_cmd
|
602
|
+
assert_parses(
|
603
|
+
s(:lvasgn, :foo, s(:send, nil, :m, s(:lvar, :foo))),
|
604
|
+
%q{foo = m foo})
|
605
|
+
|
606
|
+
assert_parses(
|
607
|
+
s(:lvasgn, :foo,
|
608
|
+
s(:lvasgn, :bar,
|
609
|
+
s(:send, nil, :m, s(:lvar, :foo)))),
|
610
|
+
%q{foo = bar = m foo},
|
611
|
+
%q{},
|
612
|
+
ALL_VERSIONS - %w(1.8))
|
613
|
+
end
|
614
|
+
|
615
|
+
def test_asgn_keyword_invalid
|
616
|
+
assert_diagnoses(
|
617
|
+
[:error, :invalid_assignment],
|
618
|
+
%q{nil = foo},
|
619
|
+
%q{~~~ location})
|
620
|
+
|
621
|
+
assert_diagnoses(
|
622
|
+
[:error, :invalid_assignment],
|
623
|
+
%q{self = foo},
|
624
|
+
%q{~~~~ location})
|
625
|
+
|
626
|
+
assert_diagnoses(
|
627
|
+
[:error, :invalid_assignment],
|
628
|
+
%q{true = foo},
|
629
|
+
%q{~~~~ location})
|
630
|
+
|
631
|
+
assert_diagnoses(
|
632
|
+
[:error, :invalid_assignment],
|
633
|
+
%q{false = foo},
|
634
|
+
%q{~~~~~ location})
|
635
|
+
|
636
|
+
assert_diagnoses(
|
637
|
+
[:error, :invalid_assignment],
|
638
|
+
%q{__FILE__ = foo},
|
639
|
+
%q{~~~~~~~~ location})
|
640
|
+
|
641
|
+
assert_diagnoses(
|
642
|
+
[:error, :invalid_assignment],
|
643
|
+
%q{__LINE__ = foo},
|
644
|
+
%q{~~~~~~~~ location})
|
645
|
+
end
|
646
|
+
|
647
|
+
def test_asgn_backref_invalid
|
648
|
+
assert_diagnoses(
|
649
|
+
[:error, :backref_assignment],
|
650
|
+
%q{$1 = foo},
|
651
|
+
%q{~~ location})
|
652
|
+
end
|
653
|
+
|
654
|
+
# Constants
|
655
|
+
|
656
|
+
def test_cdecl_toplevel
|
657
|
+
assert_parses(
|
658
|
+
s(:cdecl, s(:cbase), :Foo, s(:int, 10)),
|
659
|
+
%q{::Foo = 10},
|
660
|
+
%q{ ~~~ name
|
661
|
+
| ^ operator
|
662
|
+
|~~~~~~~~~~ expression
|
663
|
+
})
|
664
|
+
end
|
665
|
+
|
666
|
+
def test_cdecl_scoped
|
667
|
+
assert_parses(
|
668
|
+
s(:cdecl, s(:const, nil, :Bar), :Foo, s(:int, 10)),
|
669
|
+
%q{Bar::Foo = 10},
|
670
|
+
%q{ ~~~ name
|
671
|
+
| ^ operator
|
672
|
+
|~~~~~~~~~~~~~ expression
|
673
|
+
})
|
674
|
+
end
|
675
|
+
|
676
|
+
def test_cdecl_unscoped
|
677
|
+
assert_parses(
|
678
|
+
s(:cdecl, nil, :Foo, s(:int, 10)),
|
679
|
+
%q{Foo = 10},
|
680
|
+
%q{~~~ name
|
681
|
+
| ^ operator
|
682
|
+
|~~~~~~~~ expression
|
683
|
+
})
|
684
|
+
end
|
685
|
+
|
686
|
+
def test_cdecl_invalid
|
687
|
+
assert_diagnoses(
|
688
|
+
[:error, :dynamic_const],
|
689
|
+
%q{def f; Foo = 1; end},
|
690
|
+
%q{ ~~~ location})
|
691
|
+
|
692
|
+
assert_diagnoses(
|
693
|
+
[:error, :dynamic_const],
|
694
|
+
%q{def f; Foo::Bar = 1; end},
|
695
|
+
%q{ ~~~ location})
|
696
|
+
|
697
|
+
assert_diagnoses(
|
698
|
+
[:error, :dynamic_const],
|
699
|
+
%q{def f; ::Bar = 1; end},
|
700
|
+
%q{ ~~~ location})
|
701
|
+
end
|
702
|
+
|
703
|
+
# Multiple assignment
|
704
|
+
|
705
|
+
def test_masgn
|
706
|
+
assert_parses(
|
707
|
+
s(:masgn,
|
708
|
+
s(:mlhs, s(:lvasgn, :foo), s(:lvasgn, :bar)),
|
709
|
+
s(:array, s(:int, 1), s(:int, 2))),
|
710
|
+
%q{foo, bar = 1, 2},
|
711
|
+
%q{ ^ operator
|
712
|
+
|~~~~~~~~ expression (mlhs)
|
713
|
+
| ~~~~ expression (array)
|
714
|
+
|~~~~~~~~~~~~~~~ expression
|
715
|
+
})
|
716
|
+
|
717
|
+
assert_parses(
|
718
|
+
s(:masgn,
|
719
|
+
s(:mlhs, s(:lvasgn, :foo), s(:lvasgn, :bar)),
|
720
|
+
s(:array, s(:int, 1), s(:int, 2))),
|
721
|
+
%q{(foo, bar) = 1, 2},
|
722
|
+
%q{^ begin
|
723
|
+
| ^ end})
|
724
|
+
|
725
|
+
assert_parses(
|
726
|
+
s(:masgn,
|
727
|
+
s(:mlhs,
|
728
|
+
s(:lvasgn, :foo),
|
729
|
+
s(:lvasgn, :bar),
|
730
|
+
s(:lvasgn, :baz)),
|
731
|
+
s(:array, s(:int, 1), s(:int, 2))),
|
732
|
+
%q{foo, bar, baz = 1, 2})
|
733
|
+
end
|
734
|
+
|
735
|
+
def test_masgn_splat
|
736
|
+
assert_parses(
|
737
|
+
s(:masgn,
|
738
|
+
s(:mlhs, s(:ivasgn, :@foo), s(:cvdecl, :@@bar)),
|
739
|
+
s(:array, s(:splat, s(:lvar, :foo)))),
|
740
|
+
%q{@foo, @@bar = *foo},
|
741
|
+
%q{ ^ operator (array.splat)
|
742
|
+
| ~~~~ expression (array.splat)
|
743
|
+
})
|
744
|
+
|
745
|
+
assert_parses(
|
746
|
+
s(:masgn,
|
747
|
+
s(:mlhs, s(:lvasgn, :a), s(:lvasgn, :b)),
|
748
|
+
s(:array, s(:splat, s(:lvar, :foo)), s(:lvar, :bar))),
|
749
|
+
%q{a, b = *foo, bar},
|
750
|
+
%q{},
|
751
|
+
ALL_VERSIONS - %w(1.8))
|
752
|
+
|
753
|
+
assert_parses(
|
754
|
+
s(:masgn,
|
755
|
+
s(:mlhs, s(:lvasgn, :a), s(:splat, s(:lvasgn, :b))),
|
756
|
+
s(:lvar, :bar)),
|
757
|
+
%q{a, *b = bar})
|
758
|
+
|
759
|
+
assert_parses(
|
760
|
+
s(:masgn,
|
761
|
+
s(:mlhs,
|
762
|
+
s(:lvasgn, :a),
|
763
|
+
s(:splat, s(:lvasgn, :b)),
|
764
|
+
s(:lvasgn, :c)),
|
765
|
+
s(:lvar, :bar)),
|
766
|
+
%q{a, *b, c = bar},
|
767
|
+
%q{},
|
768
|
+
ALL_VERSIONS - %w(1.8))
|
769
|
+
|
770
|
+
assert_parses(
|
771
|
+
s(:masgn,
|
772
|
+
s(:mlhs, s(:lvasgn, :a), s(:splat)),
|
773
|
+
s(:lvar, :bar)),
|
774
|
+
%q{a, * = bar})
|
775
|
+
|
776
|
+
assert_parses(
|
777
|
+
s(:masgn,
|
778
|
+
s(:mlhs,
|
779
|
+
s(:lvasgn, :a),
|
780
|
+
s(:splat),
|
781
|
+
s(:lvasgn, :c)),
|
782
|
+
s(:lvar, :bar)),
|
783
|
+
%q{a, *, c = bar},
|
784
|
+
%q{},
|
785
|
+
ALL_VERSIONS - %w(1.8))
|
786
|
+
|
787
|
+
assert_parses(
|
788
|
+
s(:masgn,
|
789
|
+
s(:mlhs, s(:splat, s(:lvasgn, :b))),
|
790
|
+
s(:lvar, :bar)),
|
791
|
+
%q{*b = bar})
|
792
|
+
|
793
|
+
assert_parses(
|
794
|
+
s(:masgn,
|
795
|
+
s(:mlhs,
|
796
|
+
s(:splat, s(:lvasgn, :b)),
|
797
|
+
s(:lvasgn, :c)),
|
798
|
+
s(:lvar, :bar)),
|
799
|
+
%q{*b, c = bar},
|
800
|
+
%q{},
|
801
|
+
ALL_VERSIONS - %w(1.8))
|
802
|
+
|
803
|
+
assert_parses(
|
804
|
+
s(:masgn,
|
805
|
+
s(:mlhs, s(:splat)),
|
806
|
+
s(:lvar, :bar)),
|
807
|
+
%q{* = bar})
|
808
|
+
|
809
|
+
assert_parses(
|
810
|
+
s(:masgn,
|
811
|
+
s(:mlhs,
|
812
|
+
s(:splat),
|
813
|
+
s(:lvasgn, :c),
|
814
|
+
s(:lvasgn, :d)),
|
815
|
+
s(:lvar, :bar)),
|
816
|
+
%q{*, c, d = bar},
|
817
|
+
%q{},
|
818
|
+
ALL_VERSIONS - %w(1.8))
|
819
|
+
end
|
820
|
+
|
821
|
+
def test_masgn_nested
|
822
|
+
assert_parses(
|
823
|
+
s(:masgn,
|
824
|
+
s(:mlhs,
|
825
|
+
s(:lvasgn, :a),
|
826
|
+
s(:mlhs,
|
827
|
+
s(:lvasgn, :b),
|
828
|
+
s(:lvasgn, :c))),
|
829
|
+
s(:lvar, :foo)),
|
830
|
+
%q{a, (b, c) = foo},
|
831
|
+
%q{ ^ begin (mlhs.mlhs)
|
832
|
+
| ^ end (mlhs.mlhs)
|
833
|
+
| ~~~~~~ expression (mlhs.mlhs)
|
834
|
+
})
|
835
|
+
|
836
|
+
assert_parses(
|
837
|
+
s(:masgn,
|
838
|
+
s(:mlhs,
|
839
|
+
s(:lvasgn, :b)),
|
840
|
+
s(:lvar, :foo)),
|
841
|
+
%q{((b, )) = foo},
|
842
|
+
%q{^ begin (mlhs)
|
843
|
+
| ^ end (mlhs)})
|
844
|
+
end
|
845
|
+
|
846
|
+
def test_masgn_attr
|
847
|
+
assert_parses(
|
848
|
+
s(:masgn,
|
849
|
+
s(:mlhs,
|
850
|
+
s(:send, s(:self), :a=),
|
851
|
+
s(:send, s(:self), :[]=, s(:int, 1), s(:int, 2))),
|
852
|
+
s(:lvar, :foo)),
|
853
|
+
%q{self.a, self[1, 2] = foo},
|
854
|
+
%q{~~~~~~ expression (mlhs.send/1)
|
855
|
+
| ~ selector (mlhs.send/1)
|
856
|
+
| ~~~~~~ selector (mlhs.send/2)
|
857
|
+
| ~~~~~~~~~~ expression (mlhs.send/2)})
|
858
|
+
|
859
|
+
assert_parses(
|
860
|
+
s(:masgn,
|
861
|
+
s(:mlhs,
|
862
|
+
s(:send, s(:self), :a=),
|
863
|
+
s(:lvasgn, :foo)),
|
864
|
+
s(:lvar, :foo)),
|
865
|
+
%q{self::a, foo = foo})
|
866
|
+
|
867
|
+
assert_parses(
|
868
|
+
s(:masgn,
|
869
|
+
s(:mlhs,
|
870
|
+
s(:send, s(:self), :A=),
|
871
|
+
s(:lvasgn, :foo)),
|
872
|
+
s(:lvar, :foo)),
|
873
|
+
%q{self.A, foo = foo})
|
874
|
+
end
|
875
|
+
|
876
|
+
def test_masgn_const
|
877
|
+
assert_parses(
|
878
|
+
s(:masgn,
|
879
|
+
s(:mlhs,
|
880
|
+
s(:cdecl, s(:self), :A),
|
881
|
+
s(:lvasgn, :foo)),
|
882
|
+
s(:lvar, :foo)),
|
883
|
+
%q{self::A, foo = foo})
|
884
|
+
|
885
|
+
assert_parses(
|
886
|
+
s(:masgn,
|
887
|
+
s(:mlhs,
|
888
|
+
s(:cdecl, s(:cbase), :A),
|
889
|
+
s(:lvasgn, :foo)),
|
890
|
+
s(:lvar, :foo)),
|
891
|
+
%q{::A, foo = foo})
|
892
|
+
end
|
893
|
+
|
894
|
+
def test_masgn_cmd
|
895
|
+
assert_parses(
|
896
|
+
s(:masgn,
|
897
|
+
s(:mlhs,
|
898
|
+
s(:lvasgn, :foo),
|
899
|
+
s(:lvasgn, :bar)),
|
900
|
+
s(:send, nil, :m, s(:lvar, :foo))),
|
901
|
+
%q{foo, bar = m foo})
|
902
|
+
end
|
903
|
+
|
904
|
+
def test_asgn_mrhs
|
905
|
+
assert_parses(
|
906
|
+
s(:lvasgn, :foo,
|
907
|
+
s(:array, s(:lvar, :bar), s(:int, 1))),
|
908
|
+
%q{foo = bar, 1},
|
909
|
+
%q{ ~~~~~~ array
|
910
|
+
|~~~~~~~~~~~~ expression})
|
911
|
+
|
912
|
+
assert_parses(
|
913
|
+
s(:lvasgn, :foo,
|
914
|
+
s(:array, s(:splat, s(:lvar, :bar)))),
|
915
|
+
%q{foo = *bar})
|
916
|
+
|
917
|
+
assert_parses(
|
918
|
+
s(:lvasgn, :foo,
|
919
|
+
s(:array,
|
920
|
+
s(:lvar, :baz),
|
921
|
+
s(:splat, s(:lvar, :bar)))),
|
922
|
+
%q{foo = baz, *bar})
|
923
|
+
end
|
924
|
+
|
925
|
+
def test_masgn_keyword_invalid
|
926
|
+
assert_diagnoses(
|
927
|
+
[:error, :invalid_assignment],
|
928
|
+
%q{nil, foo = bar},
|
929
|
+
%q{~~~ location})
|
930
|
+
end
|
931
|
+
|
932
|
+
def test_masgn_backref_invalid
|
933
|
+
assert_diagnoses(
|
934
|
+
[:error, :backref_assignment],
|
935
|
+
%q{$1, = foo},
|
936
|
+
%q{~~ location})
|
937
|
+
end
|
938
|
+
|
939
|
+
def test_masgn_const_invalid
|
940
|
+
assert_diagnoses(
|
941
|
+
[:error, :dynamic_const],
|
942
|
+
%q{def f; self::A, foo = foo; end},
|
943
|
+
%q{ ~ location})
|
944
|
+
|
945
|
+
assert_diagnoses(
|
946
|
+
[:error, :dynamic_const],
|
947
|
+
%q{def f; ::A, foo = foo; end},
|
948
|
+
%q{ ~ location})
|
949
|
+
end
|
950
|
+
|
951
|
+
# Variable binary operator-assignment
|
952
|
+
|
953
|
+
def test_var_op_asgn
|
954
|
+
assert_parses(
|
955
|
+
s(:op_asgn, s(:lvasgn, :a), :+, s(:int, 1)),
|
956
|
+
%q{a += 1},
|
957
|
+
%q{ ^^ operator
|
958
|
+
|~~~~~~ expression})
|
959
|
+
|
960
|
+
assert_parses(
|
961
|
+
s(:op_asgn, s(:ivasgn, :@a), :|, s(:int, 1)),
|
962
|
+
%q{@a |= 1},
|
963
|
+
%q{ ^^ operator
|
964
|
+
|~~~~~~~ expression})
|
965
|
+
|
966
|
+
assert_parses(
|
967
|
+
s(:op_asgn, s(:cvdecl, :@@var), :|, s(:int, 10)),
|
968
|
+
%q{@@var |= 10})
|
969
|
+
|
970
|
+
assert_parses(
|
971
|
+
s(:def, :a, s(:args),
|
972
|
+
s(:op_asgn, s(:cvasgn, :@@var), :|, s(:int, 10))),
|
973
|
+
%q{def a; @@var |= 10; end})
|
974
|
+
end
|
975
|
+
|
976
|
+
def test_var_op_asgn_cmd
|
977
|
+
assert_parses(
|
978
|
+
s(:op_asgn,
|
979
|
+
s(:lvasgn, :foo), :+,
|
980
|
+
s(:send, nil, :m, s(:lvar, :foo))),
|
981
|
+
%q{foo += m foo})
|
982
|
+
end
|
983
|
+
|
984
|
+
def test_var_op_asgn_keyword_invalid
|
985
|
+
assert_diagnoses(
|
986
|
+
[:error, :invalid_assignment],
|
987
|
+
%q{nil += foo},
|
988
|
+
%q{~~~ location})
|
989
|
+
end
|
990
|
+
|
991
|
+
# Method binary operator-assignment
|
992
|
+
|
993
|
+
def test_op_asgn
|
994
|
+
assert_parses(
|
995
|
+
s(:op_asgn,
|
996
|
+
s(:send, s(:lvar, :foo), :a), :+,
|
997
|
+
s(:int, 1)),
|
998
|
+
%q{foo.a += 1},
|
999
|
+
%q{ ^^ operator
|
1000
|
+
| ~ selector (send)
|
1001
|
+
|~~~~~ expression (send)
|
1002
|
+
|~~~~~~~~~~ expression})
|
1003
|
+
|
1004
|
+
assert_parses(
|
1005
|
+
s(:op_asgn,
|
1006
|
+
s(:send, s(:lvar, :foo), :a), :+,
|
1007
|
+
s(:int, 1)),
|
1008
|
+
%q{foo::a += 1})
|
1009
|
+
|
1010
|
+
assert_parses(
|
1011
|
+
s(:op_asgn,
|
1012
|
+
s(:send, s(:lvar, :foo), :A), :+,
|
1013
|
+
s(:int, 1)),
|
1014
|
+
%q{foo.A += 1})
|
1015
|
+
end
|
1016
|
+
|
1017
|
+
def test_op_asgn_cmd
|
1018
|
+
assert_parses(
|
1019
|
+
s(:op_asgn,
|
1020
|
+
s(:send, s(:lvar, :foo), :a), :+,
|
1021
|
+
s(:send, nil, :m, s(:lvar, :foo))),
|
1022
|
+
%q{foo.a += m foo})
|
1023
|
+
|
1024
|
+
assert_parses(
|
1025
|
+
s(:op_asgn,
|
1026
|
+
s(:send, s(:lvar, :foo), :a), :+,
|
1027
|
+
s(:send, nil, :m, s(:lvar, :foo))),
|
1028
|
+
%q{foo::a += m foo})
|
1029
|
+
|
1030
|
+
assert_parses(
|
1031
|
+
s(:op_asgn,
|
1032
|
+
s(:send, s(:lvar, :foo), :A), :+,
|
1033
|
+
s(:send, nil, :m, s(:lvar, :foo))),
|
1034
|
+
%q{foo.A += m foo})
|
1035
|
+
|
1036
|
+
assert_parses(
|
1037
|
+
s(:op_asgn,
|
1038
|
+
s(:send, s(:lvar, :foo), :A), :+,
|
1039
|
+
s(:send, nil, :m, s(:lvar, :foo))),
|
1040
|
+
%q{foo::A += m foo},
|
1041
|
+
%q{},
|
1042
|
+
ALL_VERSIONS - %w(1.8))
|
1043
|
+
end
|
1044
|
+
|
1045
|
+
def test_op_asgn_index
|
1046
|
+
assert_parses(
|
1047
|
+
s(:op_asgn,
|
1048
|
+
s(:send, s(:lvar, :foo), :[],
|
1049
|
+
s(:int, 0), s(:int, 1)), :+,
|
1050
|
+
s(:int, 2)),
|
1051
|
+
%q{foo[0, 1] += 2},
|
1052
|
+
%q{ ^^ operator
|
1053
|
+
| ~~~~~~ selector (send)
|
1054
|
+
|~~~~~~~~~ expression (send)
|
1055
|
+
|~~~~~~~~~~~~~~ expression})
|
1056
|
+
end
|
1057
|
+
|
1058
|
+
def test_op_asgn_index_cmd
|
1059
|
+
assert_parses(
|
1060
|
+
s(:op_asgn,
|
1061
|
+
s(:send, s(:lvar, :foo), :[],
|
1062
|
+
s(:int, 0), s(:int, 1)), :+,
|
1063
|
+
s(:send, nil, :m, s(:lvar, :foo))),
|
1064
|
+
%q{foo[0, 1] += m foo})
|
1065
|
+
end
|
1066
|
+
|
1067
|
+
def test_op_asgn_invalid
|
1068
|
+
assert_diagnoses(
|
1069
|
+
[:error, :backref_assignment],
|
1070
|
+
%q{$1 |= 1},
|
1071
|
+
%q{~~ location})
|
1072
|
+
|
1073
|
+
assert_diagnoses(
|
1074
|
+
[:error, :backref_assignment],
|
1075
|
+
%q{$+ |= 1},
|
1076
|
+
%q{~~ location})
|
1077
|
+
|
1078
|
+
assert_diagnoses(
|
1079
|
+
[:error, :backref_assignment],
|
1080
|
+
%q{$+ |= m foo},
|
1081
|
+
%q{~~ location})
|
1082
|
+
|
1083
|
+
assert_diagnoses(
|
1084
|
+
[:error, :dynamic_const],
|
1085
|
+
%q{Foo::Bar += 1; end},
|
1086
|
+
%q{ ~~~ location})
|
1087
|
+
|
1088
|
+
assert_diagnoses(
|
1089
|
+
[:error, :dynamic_const],
|
1090
|
+
%q{::Bar += 1; end},
|
1091
|
+
%q{ ~~~ location})
|
1092
|
+
end
|
1093
|
+
|
1094
|
+
# Variable logical operator-assignment
|
1095
|
+
|
1096
|
+
def test_var_or_asgn
|
1097
|
+
assert_parses(
|
1098
|
+
s(:or_asgn, s(:lvasgn, :a), s(:int, 1)),
|
1099
|
+
%q{a ||= 1},
|
1100
|
+
%q{ ^^^ operator
|
1101
|
+
|~~~~~~~ expression})
|
1102
|
+
end
|
1103
|
+
|
1104
|
+
def test_var_and_asgn
|
1105
|
+
assert_parses(
|
1106
|
+
s(:and_asgn, s(:lvasgn, :a), s(:int, 1)),
|
1107
|
+
%q{a &&= 1},
|
1108
|
+
%q{ ^^^ operator
|
1109
|
+
|~~~~~~~ expression})
|
1110
|
+
end
|
1111
|
+
|
1112
|
+
# Method logical operator-assignment
|
1113
|
+
|
1114
|
+
def test_or_asgn
|
1115
|
+
assert_parses(
|
1116
|
+
s(:or_asgn,
|
1117
|
+
s(:send, s(:lvar, :foo), :a),
|
1118
|
+
s(:int, 1)),
|
1119
|
+
%q{foo.a ||= 1},
|
1120
|
+
%q{ ^^^ operator
|
1121
|
+
| ~ selector (send)
|
1122
|
+
|~~~~~ expression (send)
|
1123
|
+
|~~~~~~~~~~~ expression})
|
1124
|
+
|
1125
|
+
assert_parses(
|
1126
|
+
s(:or_asgn,
|
1127
|
+
s(:send, s(:lvar, :foo), :[],
|
1128
|
+
s(:int, 0), s(:int, 1)),
|
1129
|
+
s(:int, 2)),
|
1130
|
+
%q{foo[0, 1] ||= 2},
|
1131
|
+
%q{ ^^^ operator
|
1132
|
+
| ~~~~~~ selector (send)
|
1133
|
+
|~~~~~~~~~ expression (send)
|
1134
|
+
|~~~~~~~~~~~~~~~ expression})
|
1135
|
+
end
|
1136
|
+
|
1137
|
+
def test_and_asgn
|
1138
|
+
assert_parses(
|
1139
|
+
s(:and_asgn,
|
1140
|
+
s(:send, s(:lvar, :foo), :a),
|
1141
|
+
s(:int, 1)),
|
1142
|
+
%q{foo.a &&= 1},
|
1143
|
+
%q{ ^^^ operator
|
1144
|
+
| ~ selector (send)
|
1145
|
+
|~~~~~ expression (send)
|
1146
|
+
|~~~~~~~~~~~ expression})
|
1147
|
+
|
1148
|
+
assert_parses(
|
1149
|
+
s(:and_asgn,
|
1150
|
+
s(:send, s(:lvar, :foo), :[],
|
1151
|
+
s(:int, 0), s(:int, 1)),
|
1152
|
+
s(:int, 2)),
|
1153
|
+
%q{foo[0, 1] &&= 2},
|
1154
|
+
%q{ ^^^ operator
|
1155
|
+
| ~~~~~~ selector (send)
|
1156
|
+
|~~~~~~~~~ expression (send)
|
1157
|
+
|~~~~~~~~~~~~~~~ expression})
|
1158
|
+
end
|
1159
|
+
|
1160
|
+
def test_log_asgn_invalid
|
1161
|
+
assert_diagnoses(
|
1162
|
+
[:error, :backref_assignment],
|
1163
|
+
%q{$1 &&= 1},
|
1164
|
+
%q{~~ location})
|
1165
|
+
|
1166
|
+
assert_diagnoses(
|
1167
|
+
[:error, :backref_assignment],
|
1168
|
+
%q{$+ ||= 1},
|
1169
|
+
%q{~~ location})
|
1170
|
+
end
|
1171
|
+
|
1172
|
+
|
1173
|
+
#
|
1174
|
+
# Class and module definitions
|
1175
|
+
#
|
1176
|
+
|
1177
|
+
def test_module
|
1178
|
+
assert_parses(
|
1179
|
+
s(:module,
|
1180
|
+
s(:const, nil, :Foo),
|
1181
|
+
s(:nil)),
|
1182
|
+
%q{module Foo; nil; end},
|
1183
|
+
%q{~~~~~~ keyword
|
1184
|
+
| ~~~ end})
|
1185
|
+
end
|
1186
|
+
|
1187
|
+
def test_module_invalid
|
1188
|
+
assert_diagnoses(
|
1189
|
+
[:error, :module_in_def],
|
1190
|
+
%q{def a; module Foo; end; end})
|
1191
|
+
end
|
1192
|
+
|
1193
|
+
def test_cpath
|
1194
|
+
assert_parses(
|
1195
|
+
s(:module,
|
1196
|
+
s(:const, s(:cbase), :Foo),
|
1197
|
+
s(:nil)),
|
1198
|
+
%q{module ::Foo; nil; end})
|
1199
|
+
|
1200
|
+
assert_parses(
|
1201
|
+
s(:module,
|
1202
|
+
s(:const, s(:const, nil, :Bar), :Foo),
|
1203
|
+
s(:nil)),
|
1204
|
+
%q{module Bar::Foo; nil; end})
|
1205
|
+
end
|
1206
|
+
|
1207
|
+
def test_cpath_invalid
|
1208
|
+
assert_diagnoses(
|
1209
|
+
[:error, :module_name_const],
|
1210
|
+
%q{module foo; nil; end})
|
1211
|
+
end
|
1212
|
+
|
1213
|
+
def test_class
|
1214
|
+
assert_parses(
|
1215
|
+
s(:class,
|
1216
|
+
s(:const, nil, :Foo),
|
1217
|
+
nil,
|
1218
|
+
s(:nil)),
|
1219
|
+
%q{class Foo; nil; end},
|
1220
|
+
%q{~~~~~ keyword
|
1221
|
+
| ~~~ end})
|
1222
|
+
end
|
1223
|
+
|
1224
|
+
def test_class_super
|
1225
|
+
assert_parses(
|
1226
|
+
s(:class,
|
1227
|
+
s(:const, nil, :Foo),
|
1228
|
+
s(:const, nil, :Bar),
|
1229
|
+
s(:nil)),
|
1230
|
+
%q{class Foo < Bar; nil; end},
|
1231
|
+
%q{~~~~~ keyword
|
1232
|
+
| ^ operator
|
1233
|
+
| ~~~ end})
|
1234
|
+
end
|
1235
|
+
|
1236
|
+
def test_class_invalid
|
1237
|
+
assert_diagnoses(
|
1238
|
+
[:error, :class_in_def],
|
1239
|
+
%q{def a; class Foo; end; end})
|
1240
|
+
end
|
1241
|
+
|
1242
|
+
def test_sclass
|
1243
|
+
assert_parses(
|
1244
|
+
s(:sclass,
|
1245
|
+
s(:lvar, :foo),
|
1246
|
+
s(:nil)),
|
1247
|
+
%q{class << foo; nil; end},
|
1248
|
+
%q{~~~~~ keyword
|
1249
|
+
| ^^ operator
|
1250
|
+
| ~~~ end})
|
1251
|
+
end
|
1252
|
+
|
1253
|
+
#
|
1254
|
+
# Method (un)definition
|
1255
|
+
#
|
1256
|
+
|
1257
|
+
def test_def
|
1258
|
+
assert_parses(
|
1259
|
+
s(:def, :foo, s(:args), s(:nil)),
|
1260
|
+
%q{def foo; nil; end},
|
1261
|
+
%q{~~~ keyword
|
1262
|
+
| ~~~ name
|
1263
|
+
| ~~~ end})
|
1264
|
+
end
|
1265
|
+
|
1266
|
+
def test_defs
|
1267
|
+
assert_parses(
|
1268
|
+
s(:defs, s(:self), :foo, s(:args), s(:nil)),
|
1269
|
+
%q{def self.foo; nil; end},
|
1270
|
+
%q{~~~ keyword
|
1271
|
+
| ^ operator
|
1272
|
+
| ~~~ name
|
1273
|
+
| ~~~ end})
|
1274
|
+
|
1275
|
+
assert_parses(
|
1276
|
+
s(:defs, s(:self), :foo, s(:args), s(:nil)),
|
1277
|
+
%q{def self::foo; nil; end},
|
1278
|
+
%q{~~~ keyword
|
1279
|
+
| ^^ operator
|
1280
|
+
| ~~~ name
|
1281
|
+
| ~~~ end})
|
1282
|
+
|
1283
|
+
assert_parses(
|
1284
|
+
s(:defs, s(:lvar, :foo), :foo, s(:args), s(:nil)),
|
1285
|
+
%q{def (foo).foo; end})
|
1286
|
+
end
|
1287
|
+
|
1288
|
+
def test_defs_invalid
|
1289
|
+
assert_diagnoses(
|
1290
|
+
[:error, :singleton_literal],
|
1291
|
+
%q{def (1).foo; end},
|
1292
|
+
%q{ ~ location})
|
1293
|
+
|
1294
|
+
assert_diagnoses(
|
1295
|
+
[:error, :singleton_literal],
|
1296
|
+
%q{def ("foo").foo; end},
|
1297
|
+
%q{ ~~~~~ location})
|
1298
|
+
|
1299
|
+
assert_diagnoses(
|
1300
|
+
[:error, :singleton_literal],
|
1301
|
+
%q{def ("foo#{bar}").foo; end},
|
1302
|
+
%q{ ~~~~~~~~~~~ location})
|
1303
|
+
|
1304
|
+
assert_diagnoses(
|
1305
|
+
[:error, :singleton_literal],
|
1306
|
+
%q{def (:foo).foo; end},
|
1307
|
+
%q{ ~ location})
|
1308
|
+
|
1309
|
+
assert_diagnoses(
|
1310
|
+
[:error, :singleton_literal],
|
1311
|
+
%q{def (:"foo#{bar}").foo; end},
|
1312
|
+
%q{ ~~~~~~~~~~~~ location})
|
1313
|
+
|
1314
|
+
assert_diagnoses(
|
1315
|
+
[:error, :singleton_literal],
|
1316
|
+
%q{def ([]).foo; end},
|
1317
|
+
%q{ ~~ location})
|
1318
|
+
|
1319
|
+
assert_diagnoses(
|
1320
|
+
[:error, :singleton_literal],
|
1321
|
+
%q{def ({}).foo; end},
|
1322
|
+
%q{ ~~ location})
|
1323
|
+
|
1324
|
+
assert_diagnoses(
|
1325
|
+
[:error, :singleton_literal],
|
1326
|
+
%q{def (/foo/).foo; end},
|
1327
|
+
%q{ ~~~~~ location})
|
1328
|
+
end
|
1329
|
+
|
1330
|
+
def test_undef
|
1331
|
+
assert_parses(
|
1332
|
+
s(:undef,
|
1333
|
+
s(:sym, :foo),
|
1334
|
+
s(:sym, :bar),
|
1335
|
+
s(:dsym, s(:str, "foo"), s(:int, 1))),
|
1336
|
+
%q{undef foo, :bar, :"foo#{1}"},
|
1337
|
+
%q{~~~~~ keyword
|
1338
|
+
| ~~~ expression (sym/1)
|
1339
|
+
| ~~~~ expression (sym/2)
|
1340
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
1341
|
+
end
|
1342
|
+
|
1343
|
+
#
|
1344
|
+
# Aliasing
|
1345
|
+
#
|
1346
|
+
|
1347
|
+
def test_alias
|
1348
|
+
assert_parses(
|
1349
|
+
s(:alias, s(:sym, :foo), s(:sym, :bar)),
|
1350
|
+
%q{alias :foo bar},
|
1351
|
+
%q{~~~~~ keyword
|
1352
|
+
| ~~~~ expression (sym/1)
|
1353
|
+
| ~~~ expression (sym/2)
|
1354
|
+
|~~~~~~~~~~~~~~ expression})
|
1355
|
+
end
|
1356
|
+
|
1357
|
+
def test_alias_gvar
|
1358
|
+
assert_parses(
|
1359
|
+
s(:alias, s(:gvar, :$a), s(:gvar, :$b)),
|
1360
|
+
%q{alias $a $b},
|
1361
|
+
%q{ ~~ expression (gvar/1)})
|
1362
|
+
|
1363
|
+
assert_parses(
|
1364
|
+
s(:alias, s(:gvar, :$a), s(:back_ref, :$+)),
|
1365
|
+
%q{alias $a $+},
|
1366
|
+
%q{ ~~ expression (back_ref)})
|
1367
|
+
end
|
1368
|
+
|
1369
|
+
def test_alias_nth_ref
|
1370
|
+
assert_diagnoses(
|
1371
|
+
[:error, :nth_ref_alias],
|
1372
|
+
%q{alias $a $1},
|
1373
|
+
%q{ ~~ location})
|
1374
|
+
end
|
1375
|
+
|
1376
|
+
#
|
1377
|
+
# Formal arguments
|
1378
|
+
#
|
1379
|
+
|
1380
|
+
def test_arg
|
1381
|
+
assert_parses(
|
1382
|
+
s(:def, :f,
|
1383
|
+
s(:args, s(:arg, :foo)),
|
1384
|
+
s(:nil)),
|
1385
|
+
%q{def f(foo); nil; end},
|
1386
|
+
%q{ ~~~ name (args.arg)
|
1387
|
+
| ~~~ expression (args.arg)
|
1388
|
+
| ^ begin (args)
|
1389
|
+
| ^ end (args)
|
1390
|
+
| ~~~~~ expression (args)})
|
1391
|
+
|
1392
|
+
assert_parses(
|
1393
|
+
s(:def, :f,
|
1394
|
+
s(:args, s(:arg, :foo), s(:arg, :bar)),
|
1395
|
+
s(:nil)),
|
1396
|
+
%q{def f(foo, bar); nil; end})
|
1397
|
+
end
|
1398
|
+
|
1399
|
+
def test_optarg
|
1400
|
+
assert_parses(
|
1401
|
+
s(:def, :f,
|
1402
|
+
s(:args, s(:optarg, :foo, s(:int, 1))),
|
1403
|
+
s(:nil)),
|
1404
|
+
%q{def f foo = 1; nil; end},
|
1405
|
+
%q{ ~~~ name (args.optarg)
|
1406
|
+
| ^ operator (args.optarg)
|
1407
|
+
| ~~~~~~~ expression (args.optarg)
|
1408
|
+
| ~~~~~~~ expression (args)})
|
1409
|
+
|
1410
|
+
assert_parses(
|
1411
|
+
s(:def, :f,
|
1412
|
+
s(:args,
|
1413
|
+
s(:optarg, :foo, s(:int, 1)),
|
1414
|
+
s(:optarg, :bar, s(:int, 2))),
|
1415
|
+
s(:nil)),
|
1416
|
+
%q{def f(foo=1, bar=2); nil; end})
|
1417
|
+
end
|
1418
|
+
|
1419
|
+
def test_splatarg_named
|
1420
|
+
assert_parses(
|
1421
|
+
s(:def, :f,
|
1422
|
+
s(:args, s(:splatarg, :foo)),
|
1423
|
+
s(:nil)),
|
1424
|
+
%q{def f(*foo); nil; end},
|
1425
|
+
%q{ ~~~ name (args.splatarg)
|
1426
|
+
| ~~~~ expression (args.splatarg)})
|
1427
|
+
end
|
1428
|
+
|
1429
|
+
def test_splatarg_unnamed
|
1430
|
+
assert_parses(
|
1431
|
+
s(:def, :f,
|
1432
|
+
s(:args, s(:splatarg)),
|
1433
|
+
s(:nil)),
|
1434
|
+
%q{def f(*); nil; end},
|
1435
|
+
%q{ ~ expression (args.splatarg)})
|
1436
|
+
end
|
1437
|
+
|
1438
|
+
def test_blockarg
|
1439
|
+
assert_parses(
|
1440
|
+
s(:def, :f,
|
1441
|
+
s(:args, s(:blockarg, :block)),
|
1442
|
+
s(:nil)),
|
1443
|
+
%q{def f(&block); nil; end},
|
1444
|
+
%q{ ~~~~~ name (args.blockarg)
|
1445
|
+
| ~~~~~~ expression (args.blockarg)})
|
1446
|
+
end
|
1447
|
+
|
1448
|
+
def assert_parses_args(ast, code, versions=ALL_VERSIONS)
|
1449
|
+
assert_parses(
|
1450
|
+
s(:def, :f, ast, s(:nil)),
|
1451
|
+
%Q{def f #{code}; nil; end},
|
1452
|
+
%q{},
|
1453
|
+
versions)
|
1454
|
+
end
|
1455
|
+
|
1456
|
+
def test_arg_combinations
|
1457
|
+
# f_arg tCOMMA f_optarg tCOMMA f_rest_arg opt_f_block_arg
|
1458
|
+
assert_parses_args(
|
1459
|
+
s(:args,
|
1460
|
+
s(:arg, :a),
|
1461
|
+
s(:optarg, :o, s(:int, 1)),
|
1462
|
+
s(:splatarg, :r),
|
1463
|
+
s(:blockarg, :b)),
|
1464
|
+
%q{a, o=1, *r, &b})
|
1465
|
+
|
1466
|
+
# f_arg tCOMMA f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1467
|
+
assert_parses_args(
|
1468
|
+
s(:args,
|
1469
|
+
s(:arg, :a),
|
1470
|
+
s(:optarg, :o, s(:int, 1)),
|
1471
|
+
s(:splatarg, :r),
|
1472
|
+
s(:arg, :p),
|
1473
|
+
s(:blockarg, :b)),
|
1474
|
+
%q{a, o=1, *r, p, &b},
|
1475
|
+
ALL_VERSIONS - %w(1.8))
|
1476
|
+
|
1477
|
+
# f_arg tCOMMA f_optarg opt_f_block_arg
|
1478
|
+
assert_parses_args(
|
1479
|
+
s(:args,
|
1480
|
+
s(:arg, :a),
|
1481
|
+
s(:optarg, :o, s(:int, 1)),
|
1482
|
+
s(:blockarg, :b)),
|
1483
|
+
%q{a, o=1, &b})
|
1484
|
+
|
1485
|
+
# f_arg tCOMMA f_optarg tCOMMA f_arg opt_f_block_arg
|
1486
|
+
assert_parses_args(
|
1487
|
+
s(:args,
|
1488
|
+
s(:arg, :a),
|
1489
|
+
s(:optarg, :o, s(:int, 1)),
|
1490
|
+
s(:arg, :p),
|
1491
|
+
s(:blockarg, :b)),
|
1492
|
+
%q{a, o=1, p, &b},
|
1493
|
+
ALL_VERSIONS - %w(1.8))
|
1494
|
+
|
1495
|
+
# f_arg tCOMMA f_rest_arg opt_f_block_arg
|
1496
|
+
assert_parses_args(
|
1497
|
+
s(:args,
|
1498
|
+
s(:arg, :a),
|
1499
|
+
s(:splatarg, :r),
|
1500
|
+
s(:blockarg, :b)),
|
1501
|
+
%q{a, *r, &b})
|
1502
|
+
|
1503
|
+
# f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1504
|
+
assert_parses_args(
|
1505
|
+
s(:args,
|
1506
|
+
s(:arg, :a),
|
1507
|
+
s(:splatarg, :r),
|
1508
|
+
s(:arg, :p),
|
1509
|
+
s(:blockarg, :b)),
|
1510
|
+
%q{a, *r, p, &b},
|
1511
|
+
ALL_VERSIONS - %w(1.8))
|
1512
|
+
|
1513
|
+
# f_arg opt_f_block_arg
|
1514
|
+
assert_parses_args(
|
1515
|
+
s(:args,
|
1516
|
+
s(:arg, :a),
|
1517
|
+
s(:blockarg, :b)),
|
1518
|
+
%q{a, &b})
|
1519
|
+
|
1520
|
+
# f_optarg tCOMMA f_rest_arg opt_f_block_arg
|
1521
|
+
assert_parses_args(
|
1522
|
+
s(:args,
|
1523
|
+
s(:optarg, :o, s(:int, 1)),
|
1524
|
+
s(:splatarg, :r),
|
1525
|
+
s(:blockarg, :b)),
|
1526
|
+
%q{o=1, *r, &b})
|
1527
|
+
|
1528
|
+
# f_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1529
|
+
assert_parses_args(
|
1530
|
+
s(:args,
|
1531
|
+
s(:optarg, :o, s(:int, 1)),
|
1532
|
+
s(:splatarg, :r),
|
1533
|
+
s(:arg, :p),
|
1534
|
+
s(:blockarg, :b)),
|
1535
|
+
%q{o=1, *r, p, &b},
|
1536
|
+
ALL_VERSIONS - %w(1.8))
|
1537
|
+
|
1538
|
+
# f_optarg opt_f_block_arg
|
1539
|
+
assert_parses_args(
|
1540
|
+
s(:args,
|
1541
|
+
s(:optarg, :o, s(:int, 1)),
|
1542
|
+
s(:blockarg, :b)),
|
1543
|
+
%q{o=1, &b})
|
1544
|
+
|
1545
|
+
# f_optarg tCOMMA f_arg opt_f_block_arg
|
1546
|
+
assert_parses_args(
|
1547
|
+
s(:args,
|
1548
|
+
s(:optarg, :o, s(:int, 1)),
|
1549
|
+
s(:arg, :p),
|
1550
|
+
s(:blockarg, :b)),
|
1551
|
+
%q{o=1, p, &b},
|
1552
|
+
ALL_VERSIONS - %w(1.8))
|
1553
|
+
|
1554
|
+
# f_rest_arg opt_f_block_arg
|
1555
|
+
assert_parses_args(
|
1556
|
+
s(:args,
|
1557
|
+
s(:splatarg, :r),
|
1558
|
+
s(:blockarg, :b)),
|
1559
|
+
%q{*r, &b})
|
1560
|
+
|
1561
|
+
# f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1562
|
+
assert_parses_args(
|
1563
|
+
s(:args,
|
1564
|
+
s(:splatarg, :r),
|
1565
|
+
s(:arg, :p),
|
1566
|
+
s(:blockarg, :b)),
|
1567
|
+
%q{*r, p, &b},
|
1568
|
+
ALL_VERSIONS - %w(1.8))
|
1569
|
+
|
1570
|
+
# f_block_arg
|
1571
|
+
assert_parses_args(
|
1572
|
+
s(:args,
|
1573
|
+
s(:blockarg, :b)),
|
1574
|
+
%q{&b})
|
1575
|
+
|
1576
|
+
# (nothing)
|
1577
|
+
assert_parses_args(
|
1578
|
+
s(:args),
|
1579
|
+
%q{})
|
1580
|
+
end
|
1581
|
+
|
1582
|
+
def assert_parses_margs(ast, code, versions=ALL_VERSIONS - %w(1.8))
|
1583
|
+
assert_parses_args(
|
1584
|
+
s(:args, ast),
|
1585
|
+
%Q{(#{code})},
|
1586
|
+
versions)
|
1587
|
+
end
|
1588
|
+
|
1589
|
+
def test_marg_combinations
|
1590
|
+
# tLPAREN f_margs rparen
|
1591
|
+
assert_parses_margs(
|
1592
|
+
s(:mlhs,
|
1593
|
+
s(:mlhs, s(:arg, :a))),
|
1594
|
+
%q{((a))})
|
1595
|
+
|
1596
|
+
# f_marg_list
|
1597
|
+
assert_parses_margs(
|
1598
|
+
s(:mlhs, s(:arg, :a), s(:arg, :a1)),
|
1599
|
+
%q{(a, a1)})
|
1600
|
+
|
1601
|
+
# f_marg_list tCOMMA tSTAR f_norm_arg
|
1602
|
+
assert_parses_margs(
|
1603
|
+
s(:mlhs, s(:arg, :a), s(:splatarg, :s)),
|
1604
|
+
%q{(a, *s)})
|
1605
|
+
|
1606
|
+
# f_marg_list tCOMMA tSTAR f_norm_arg tCOMMA f_marg_list
|
1607
|
+
assert_parses_margs(
|
1608
|
+
s(:mlhs, s(:arg, :a), s(:splatarg, :s), s(:arg, :p)),
|
1609
|
+
%q{(a, *s, p)})
|
1610
|
+
|
1611
|
+
# f_marg_list tCOMMA tSTAR
|
1612
|
+
assert_parses_margs(
|
1613
|
+
s(:mlhs, s(:arg, :a), s(:splatarg)),
|
1614
|
+
%q{(a, *)})
|
1615
|
+
|
1616
|
+
# f_marg_list tCOMMA tSTAR tCOMMA f_marg_list
|
1617
|
+
assert_parses_margs(
|
1618
|
+
s(:mlhs, s(:arg, :a), s(:splatarg), s(:arg, :p)),
|
1619
|
+
%q{(a, *, p)})
|
1620
|
+
|
1621
|
+
# tSTAR f_norm_arg
|
1622
|
+
assert_parses_margs(
|
1623
|
+
s(:mlhs, s(:splatarg, :s)),
|
1624
|
+
%q{(*s)})
|
1625
|
+
|
1626
|
+
# tSTAR f_norm_arg tCOMMA f_marg_list
|
1627
|
+
assert_parses_margs(
|
1628
|
+
s(:mlhs, s(:splatarg, :s), s(:arg, :p)),
|
1629
|
+
%q{(*s, p)})
|
1630
|
+
|
1631
|
+
# tSTAR
|
1632
|
+
assert_parses_margs(
|
1633
|
+
s(:mlhs, s(:splatarg)),
|
1634
|
+
%q{(*)})
|
1635
|
+
|
1636
|
+
# tSTAR tCOMMA f_marg_list
|
1637
|
+
assert_parses_margs(
|
1638
|
+
s(:mlhs, s(:splatarg), s(:arg, :p)),
|
1639
|
+
%q{(*, p)})
|
1640
|
+
end
|
1641
|
+
|
1642
|
+
def assert_parses_blockargs(ast, code, versions=ALL_VERSIONS)
|
1643
|
+
assert_parses(
|
1644
|
+
s(:block,
|
1645
|
+
s(:send, nil, :f),
|
1646
|
+
ast, s(:nil)),
|
1647
|
+
%Q{f{ #{code} }},
|
1648
|
+
%q{},
|
1649
|
+
versions)
|
1650
|
+
end
|
1651
|
+
|
1652
|
+
def test_block_arg_combinations
|
1653
|
+
# none
|
1654
|
+
assert_parses_blockargs(
|
1655
|
+
s(:args),
|
1656
|
+
%q{})
|
1657
|
+
|
1658
|
+
# tPIPE tPIPE
|
1659
|
+
# tPIPE opt_bv_decl tPIPE
|
1660
|
+
assert_parses_blockargs(
|
1661
|
+
s(:args),
|
1662
|
+
%q{| |})
|
1663
|
+
|
1664
|
+
assert_parses_blockargs(
|
1665
|
+
s(:args, s(:shadowarg, :a)),
|
1666
|
+
%q{|;a|},
|
1667
|
+
ALL_VERSIONS - %w(1.8))
|
1668
|
+
|
1669
|
+
# tOROP
|
1670
|
+
assert_parses_blockargs(
|
1671
|
+
s(:args),
|
1672
|
+
%q{||})
|
1673
|
+
|
1674
|
+
# block_par
|
1675
|
+
# block_par tCOMMA
|
1676
|
+
# block_par tCOMMA tAMPER lhs
|
1677
|
+
# f_arg opt_f_block_arg
|
1678
|
+
# f_arg tCOMMA
|
1679
|
+
assert_parses_blockargs(
|
1680
|
+
s(:args, s(:arg, :a)),
|
1681
|
+
%q{|a|})
|
1682
|
+
|
1683
|
+
assert_parses_blockargs(
|
1684
|
+
s(:args, s(:arg, :a), s(:arg, :c)),
|
1685
|
+
%q{|a, c|})
|
1686
|
+
|
1687
|
+
assert_parses_blockargs(
|
1688
|
+
s(:args, s(:arg_expr, s(:ivasgn, :@a))),
|
1689
|
+
%q{|@a|},
|
1690
|
+
%w(1.8))
|
1691
|
+
|
1692
|
+
assert_parses_blockargs(
|
1693
|
+
s(:args, s(:arg, :a)),
|
1694
|
+
%q{|a,|})
|
1695
|
+
|
1696
|
+
assert_parses_blockargs(
|
1697
|
+
s(:args, s(:arg, :a), s(:blockarg, :b)),
|
1698
|
+
%q{|a, &b|})
|
1699
|
+
|
1700
|
+
assert_parses_blockargs(
|
1701
|
+
s(:args, s(:arg, :a), s(:blockarg_expr, s(:ivasgn, :@b))),
|
1702
|
+
%q{|a, &@b|},
|
1703
|
+
%w(1.8))
|
1704
|
+
|
1705
|
+
# block_par tCOMMA tSTAR lhs tCOMMA tAMPER lhs
|
1706
|
+
# block_par tCOMMA tSTAR tCOMMA tAMPER lhs
|
1707
|
+
# block_par tCOMMA tSTAR lhs
|
1708
|
+
# block_par tCOMMA tSTAR
|
1709
|
+
# f_arg tCOMMA f_rest_arg opt_f_block_arg
|
1710
|
+
assert_parses_blockargs(
|
1711
|
+
s(:args, s(:arg, :a), s(:splatarg, :s), s(:blockarg, :b)),
|
1712
|
+
%q{|a, *s, &b|})
|
1713
|
+
|
1714
|
+
assert_parses_blockargs(
|
1715
|
+
s(:args, s(:arg, :a),
|
1716
|
+
s(:splatarg_expr, s(:ivasgn, :@s)),
|
1717
|
+
s(:blockarg_expr, s(:ivasgn, :@b))),
|
1718
|
+
%q{|a, *@s, &@b|},
|
1719
|
+
%w(1.8))
|
1720
|
+
|
1721
|
+
assert_parses_blockargs(
|
1722
|
+
s(:args, s(:arg, :a), s(:splatarg), s(:blockarg, :b)),
|
1723
|
+
%q{|a, *, &b|})
|
1724
|
+
|
1725
|
+
assert_parses_blockargs(
|
1726
|
+
s(:args, s(:arg, :a),
|
1727
|
+
s(:splatarg),
|
1728
|
+
s(:blockarg_expr, s(:ivasgn, :@b))),
|
1729
|
+
%q{|a, *, &@b|},
|
1730
|
+
%w(1.8))
|
1731
|
+
|
1732
|
+
assert_parses_blockargs(
|
1733
|
+
s(:args, s(:arg, :a), s(:splatarg, :s)),
|
1734
|
+
%q{|a, *s|})
|
1735
|
+
|
1736
|
+
assert_parses_blockargs(
|
1737
|
+
s(:args, s(:arg, :a),
|
1738
|
+
s(:splatarg_expr, s(:ivasgn, :@s))),
|
1739
|
+
%q{|a, *@s|},
|
1740
|
+
%w(1.8))
|
1741
|
+
|
1742
|
+
assert_parses_blockargs(
|
1743
|
+
s(:args, s(:arg, :a), s(:splatarg)),
|
1744
|
+
%q{|a, *|})
|
1745
|
+
|
1746
|
+
# tSTAR lhs tCOMMA tAMPER lhs
|
1747
|
+
# tSTAR lhs
|
1748
|
+
# tSTAR
|
1749
|
+
# tSTAR tCOMMA tAMPER lhs
|
1750
|
+
# f_rest_arg opt_f_block_arg
|
1751
|
+
assert_parses_blockargs(
|
1752
|
+
s(:args, s(:splatarg, :s), s(:blockarg, :b)),
|
1753
|
+
%q{|*s, &b|})
|
1754
|
+
|
1755
|
+
assert_parses_blockargs(
|
1756
|
+
s(:args,
|
1757
|
+
s(:splatarg_expr, s(:ivasgn, :@s)),
|
1758
|
+
s(:blockarg_expr, s(:ivasgn, :@b))),
|
1759
|
+
%q{|*@s, &@b|},
|
1760
|
+
%w(1.8))
|
1761
|
+
|
1762
|
+
assert_parses_blockargs(
|
1763
|
+
s(:args, s(:splatarg), s(:blockarg, :b)),
|
1764
|
+
%q{|*, &b|})
|
1765
|
+
|
1766
|
+
assert_parses_blockargs(
|
1767
|
+
s(:args,
|
1768
|
+
s(:splatarg),
|
1769
|
+
s(:blockarg_expr, s(:ivasgn, :@b))),
|
1770
|
+
%q{|*, &@b|},
|
1771
|
+
%w(1.8))
|
1772
|
+
|
1773
|
+
assert_parses_blockargs(
|
1774
|
+
s(:args, s(:splatarg, :s)),
|
1775
|
+
%q{|*s|})
|
1776
|
+
|
1777
|
+
assert_parses_blockargs(
|
1778
|
+
s(:args,
|
1779
|
+
s(:splatarg_expr, s(:ivasgn, :@s))),
|
1780
|
+
%q{|*@s|},
|
1781
|
+
%w(1.8))
|
1782
|
+
|
1783
|
+
assert_parses_blockargs(
|
1784
|
+
s(:args, s(:splatarg)),
|
1785
|
+
%q{|*|})
|
1786
|
+
|
1787
|
+
# tAMPER lhs
|
1788
|
+
# f_block_arg
|
1789
|
+
assert_parses_blockargs(
|
1790
|
+
s(:args, s(:blockarg, :b)),
|
1791
|
+
%q{|&b|})
|
1792
|
+
|
1793
|
+
assert_parses_blockargs(
|
1794
|
+
s(:args,
|
1795
|
+
s(:blockarg_expr, s(:ivasgn, :@b))),
|
1796
|
+
%q{|&@b|},
|
1797
|
+
%w(1.8))
|
1798
|
+
|
1799
|
+
# f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg opt_f_block_arg
|
1800
|
+
assert_parses_blockargs(
|
1801
|
+
s(:args,
|
1802
|
+
s(:arg, :a),
|
1803
|
+
s(:optarg, :o, s(:int, 1)),
|
1804
|
+
s(:optarg, :o1, s(:int, 2)),
|
1805
|
+
s(:splatarg, :r),
|
1806
|
+
s(:blockarg, :b)),
|
1807
|
+
%q{|a, o=1, o1=2, *r, &b|},
|
1808
|
+
ALL_VERSIONS - %w(1.8))
|
1809
|
+
|
1810
|
+
# f_arg tCOMMA f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1811
|
+
assert_parses_blockargs(
|
1812
|
+
s(:args,
|
1813
|
+
s(:arg, :a),
|
1814
|
+
s(:optarg, :o, s(:int, 1)),
|
1815
|
+
s(:splatarg, :r),
|
1816
|
+
s(:arg, :p),
|
1817
|
+
s(:blockarg, :b)),
|
1818
|
+
%q{|a, o=1, *r, p, &b|},
|
1819
|
+
ALL_VERSIONS - %w(1.8))
|
1820
|
+
|
1821
|
+
# f_arg tCOMMA f_block_optarg opt_f_block_arg
|
1822
|
+
assert_parses_blockargs(
|
1823
|
+
s(:args,
|
1824
|
+
s(:arg, :a),
|
1825
|
+
s(:optarg, :o, s(:int, 1)),
|
1826
|
+
s(:blockarg, :b)),
|
1827
|
+
%q{|a, o=1, &b|},
|
1828
|
+
ALL_VERSIONS - %w(1.8))
|
1829
|
+
|
1830
|
+
# f_arg tCOMMA f_block_optarg tCOMMA f_arg opt_f_block_arg
|
1831
|
+
assert_parses_blockargs(
|
1832
|
+
s(:args,
|
1833
|
+
s(:arg, :a),
|
1834
|
+
s(:optarg, :o, s(:int, 1)),
|
1835
|
+
s(:arg, :p),
|
1836
|
+
s(:blockarg, :b)),
|
1837
|
+
%q{|a, o=1, p, &b|},
|
1838
|
+
ALL_VERSIONS - %w(1.8))
|
1839
|
+
|
1840
|
+
# f_arg tCOMMA f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1841
|
+
assert_parses_blockargs(
|
1842
|
+
s(:args,
|
1843
|
+
s(:arg, :a),
|
1844
|
+
s(:splatarg, :r),
|
1845
|
+
s(:arg, :p),
|
1846
|
+
s(:blockarg, :b)),
|
1847
|
+
%q{|a, *r, p, &b|},
|
1848
|
+
ALL_VERSIONS - %w(1.8))
|
1849
|
+
|
1850
|
+
# f_block_optarg tCOMMA f_rest_arg opt_f_block_arg
|
1851
|
+
assert_parses_blockargs(
|
1852
|
+
s(:args,
|
1853
|
+
s(:optarg, :o, s(:int, 1)),
|
1854
|
+
s(:splatarg, :r),
|
1855
|
+
s(:blockarg, :b)),
|
1856
|
+
%q{|o=1, *r, &b|},
|
1857
|
+
ALL_VERSIONS - %w(1.8))
|
1858
|
+
|
1859
|
+
# f_block_optarg tCOMMA f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1860
|
+
assert_parses_blockargs(
|
1861
|
+
s(:args,
|
1862
|
+
s(:optarg, :o, s(:int, 1)),
|
1863
|
+
s(:splatarg, :r),
|
1864
|
+
s(:arg, :p),
|
1865
|
+
s(:blockarg, :b)),
|
1866
|
+
%q{|o=1, *r, p, &b|},
|
1867
|
+
ALL_VERSIONS - %w(1.8))
|
1868
|
+
|
1869
|
+
# f_block_optarg opt_f_block_arg
|
1870
|
+
assert_parses_blockargs(
|
1871
|
+
s(:args,
|
1872
|
+
s(:optarg, :o, s(:int, 1)),
|
1873
|
+
s(:blockarg, :b)),
|
1874
|
+
%q{|o=1, &b|},
|
1875
|
+
ALL_VERSIONS - %w(1.8))
|
1876
|
+
|
1877
|
+
# f_block_optarg tCOMMA f_arg opt_f_block_arg
|
1878
|
+
assert_parses_blockargs(
|
1879
|
+
s(:args,
|
1880
|
+
s(:optarg, :o, s(:int, 1)),
|
1881
|
+
s(:arg, :p),
|
1882
|
+
s(:blockarg, :b)),
|
1883
|
+
%q{|o=1, p, &b|},
|
1884
|
+
ALL_VERSIONS - %w(1.8))
|
1885
|
+
|
1886
|
+
# f_rest_arg tCOMMA f_arg opt_f_block_arg
|
1887
|
+
assert_parses_blockargs(
|
1888
|
+
s(:args,
|
1889
|
+
s(:splatarg, :r),
|
1890
|
+
s(:arg, :p),
|
1891
|
+
s(:blockarg, :b)),
|
1892
|
+
%q{|*r, p, &b|},
|
1893
|
+
ALL_VERSIONS - %w(1.8))
|
1894
|
+
end
|
1895
|
+
|
1896
|
+
def test_arg_invalid
|
1897
|
+
assert_diagnoses(
|
1898
|
+
[:error, :argument_const],
|
1899
|
+
%q{def foo(Abc); end},
|
1900
|
+
%q{ ~~~ location})
|
1901
|
+
|
1902
|
+
assert_diagnoses(
|
1903
|
+
[:error, :argument_ivar],
|
1904
|
+
%q{def foo(@abc); end},
|
1905
|
+
%q{ ~~~~ location})
|
1906
|
+
|
1907
|
+
assert_diagnoses(
|
1908
|
+
[:error, :argument_gvar],
|
1909
|
+
%q{def foo($abc); end},
|
1910
|
+
%q{ ~~~~ location})
|
1911
|
+
|
1912
|
+
assert_diagnoses(
|
1913
|
+
[:error, :argument_cvar],
|
1914
|
+
%q{def foo(@@abc); end},
|
1915
|
+
%q{ ~~~~~ location})
|
1916
|
+
end
|
1917
|
+
|
1918
|
+
# def test_kwoptarg
|
1919
|
+
# flunk
|
1920
|
+
# end
|
1921
|
+
|
1922
|
+
# def test_kwsplat_named
|
1923
|
+
# flunk
|
1924
|
+
# end
|
1925
|
+
|
1926
|
+
# def test_kwsplat_unnamed
|
1927
|
+
# flunk
|
1928
|
+
# end
|
1929
|
+
|
1930
|
+
#
|
1931
|
+
# Sends
|
1932
|
+
#
|
1933
|
+
|
1934
|
+
# To self
|
1935
|
+
|
1936
|
+
def test_send_self
|
1937
|
+
assert_parses(
|
1938
|
+
s(:send, nil, :fun),
|
1939
|
+
%q{fun},
|
1940
|
+
%q{~~~ selector
|
1941
|
+
|~~~ expression})
|
1942
|
+
|
1943
|
+
assert_parses(
|
1944
|
+
s(:send, nil, :fun!),
|
1945
|
+
%q{fun!},
|
1946
|
+
%q{~~~~ selector
|
1947
|
+
|~~~~ expression})
|
1948
|
+
|
1949
|
+
assert_parses(
|
1950
|
+
s(:send, nil, :fun, s(:int, 1)),
|
1951
|
+
%q{fun(1)},
|
1952
|
+
%q{~~~ selector
|
1953
|
+
| ^ begin
|
1954
|
+
| ^ end
|
1955
|
+
|~~~~~~ expression})
|
1956
|
+
end
|
1957
|
+
|
1958
|
+
def test_send_self_block
|
1959
|
+
assert_parses(
|
1960
|
+
s(:block, s(:send, nil, :fun), s(:args), s(:nil)),
|
1961
|
+
%q{fun { }})
|
1962
|
+
|
1963
|
+
assert_parses(
|
1964
|
+
s(:block, s(:send, nil, :fun), s(:args), s(:nil)),
|
1965
|
+
%q{fun() { }})
|
1966
|
+
|
1967
|
+
assert_parses(
|
1968
|
+
s(:block, s(:send, nil, :fun, s(:int, 1)), s(:args), s(:nil)),
|
1969
|
+
%q{fun(1) { }})
|
1970
|
+
|
1971
|
+
assert_parses(
|
1972
|
+
s(:block, s(:send, nil, :fun), s(:args), s(:nil)),
|
1973
|
+
%q{fun do end})
|
1974
|
+
end
|
1975
|
+
|
1976
|
+
def test_send_block_blockarg
|
1977
|
+
assert_diagnoses(
|
1978
|
+
[:error, :block_and_blockarg],
|
1979
|
+
%q{fun(&bar) do end},
|
1980
|
+
%q{ ~~~~ location})
|
1981
|
+
end
|
1982
|
+
|
1983
|
+
# To receiver
|
1984
|
+
|
1985
|
+
def test_send_plain
|
1986
|
+
assert_parses(
|
1987
|
+
s(:send, s(:lvar, :foo), :fun),
|
1988
|
+
%q{foo.fun},
|
1989
|
+
%q{ ~~~ selector
|
1990
|
+
|~~~~~~~ expression})
|
1991
|
+
|
1992
|
+
assert_parses(
|
1993
|
+
s(:send, s(:lvar, :foo), :fun),
|
1994
|
+
%q{foo::fun},
|
1995
|
+
%q{ ~~~ selector
|
1996
|
+
|~~~~~~~~ expression})
|
1997
|
+
|
1998
|
+
assert_parses(
|
1999
|
+
s(:send, s(:lvar, :foo), :Fun),
|
2000
|
+
%q{foo::Fun()},
|
2001
|
+
%q{ ~~~ selector
|
2002
|
+
|~~~~~~~~~~ expression})
|
2003
|
+
end
|
2004
|
+
|
2005
|
+
def test_send_plain_cmd
|
2006
|
+
assert_parses(
|
2007
|
+
s(:send, s(:lvar, :foo), :fun, s(:lvar, :bar)),
|
2008
|
+
%q{foo.fun bar},
|
2009
|
+
%q{ ~~~ selector
|
2010
|
+
|~~~~~~~~~~~ expression})
|
2011
|
+
|
2012
|
+
assert_parses(
|
2013
|
+
s(:send, s(:lvar, :foo), :fun, s(:lvar, :bar)),
|
2014
|
+
%q{foo::fun bar},
|
2015
|
+
%q{ ~~~ selector
|
2016
|
+
|~~~~~~~~~~~~ expression})
|
2017
|
+
|
2018
|
+
assert_parses(
|
2019
|
+
s(:send, s(:lvar, :foo), :Fun, s(:lvar, :bar)),
|
2020
|
+
%q{foo::Fun bar},
|
2021
|
+
%q{ ~~~ selector
|
2022
|
+
|~~~~~~~~~~~~ expression})
|
2023
|
+
end
|
2024
|
+
|
2025
|
+
def test_send_block_chain_cmd
|
2026
|
+
assert_parses(
|
2027
|
+
s(:send,
|
2028
|
+
s(:block,
|
2029
|
+
s(:send, nil, :meth, s(:int, 1)),
|
2030
|
+
s(:args), s(:nil)),
|
2031
|
+
:fun, s(:lvar, :bar)),
|
2032
|
+
%q{meth 1 do end.fun bar},
|
2033
|
+
%q{ ~~~ selector
|
2034
|
+
|~~~~~~~~~~~~~~~~~~~~~ expression})
|
2035
|
+
|
2036
|
+
assert_parses(
|
2037
|
+
s(:send,
|
2038
|
+
s(:block,
|
2039
|
+
s(:send, nil, :meth, s(:int, 1)),
|
2040
|
+
s(:args), s(:nil)),
|
2041
|
+
:fun, s(:lvar, :bar)),
|
2042
|
+
%q{meth 1 do end.fun(bar)},
|
2043
|
+
%q{ ~~~ selector
|
2044
|
+
| ^ begin
|
2045
|
+
| ^ end
|
2046
|
+
|~~~~~~~~~~~~~~~~~~~~~~ expression})
|
2047
|
+
|
2048
|
+
assert_parses(
|
2049
|
+
s(:send,
|
2050
|
+
s(:block,
|
2051
|
+
s(:send, nil, :meth, s(:int, 1)),
|
2052
|
+
s(:args), s(:nil)),
|
2053
|
+
:fun, s(:lvar, :bar)),
|
2054
|
+
%q{meth 1 do end::fun bar},
|
2055
|
+
%q{ ~~~ selector
|
2056
|
+
|~~~~~~~~~~~~~~~~~~~~~~ expression})
|
2057
|
+
|
2058
|
+
assert_parses(
|
2059
|
+
s(:send,
|
2060
|
+
s(:block,
|
2061
|
+
s(:send, nil, :meth, s(:int, 1)),
|
2062
|
+
s(:args), s(:nil)),
|
2063
|
+
:fun, s(:lvar, :bar)),
|
2064
|
+
%q{meth 1 do end::fun(bar)},
|
2065
|
+
%q{ ~~~ selector
|
2066
|
+
| ^ begin
|
2067
|
+
| ^ end
|
2068
|
+
|~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
2069
|
+
end
|
2070
|
+
|
2071
|
+
def test_send_paren_block_cmd
|
2072
|
+
assert_parses(
|
2073
|
+
s(:send, nil, :foo,
|
2074
|
+
s(:block,
|
2075
|
+
s(:send, nil, :meth, s(:int, 1)),
|
2076
|
+
s(:args), s(:nil))),
|
2077
|
+
%q{foo(meth 1 do end)},
|
2078
|
+
%q{},
|
2079
|
+
%w(1.8))
|
2080
|
+
|
2081
|
+
assert_parses(
|
2082
|
+
s(:send, nil, :foo,
|
2083
|
+
s(:int, 1),
|
2084
|
+
s(:block,
|
2085
|
+
s(:send, nil, :meth, s(:int, 1)),
|
2086
|
+
s(:args), s(:nil))),
|
2087
|
+
%q{foo(1, meth 1 do end)},
|
2088
|
+
%q{},
|
2089
|
+
%w(1.8))
|
2090
|
+
end
|
2091
|
+
|
2092
|
+
def test_send_binary_op
|
2093
|
+
assert_parses(
|
2094
|
+
s(:send, s(:lvar, :foo), :+, s(:int, 1)),
|
2095
|
+
%q{foo + 1},
|
2096
|
+
%q{ ~ selector
|
2097
|
+
|~~~~~~~ expression})
|
2098
|
+
|
2099
|
+
assert_parses(
|
2100
|
+
s(:send, s(:lvar, :foo), :-, s(:int, 1)),
|
2101
|
+
%q{foo - 1})
|
2102
|
+
|
2103
|
+
assert_parses(
|
2104
|
+
s(:send, s(:lvar, :foo), :*, s(:int, 1)),
|
2105
|
+
%q{foo * 1})
|
2106
|
+
|
2107
|
+
assert_parses(
|
2108
|
+
s(:send, s(:lvar, :foo), :/, s(:int, 1)),
|
2109
|
+
%q{foo / 1})
|
2110
|
+
|
2111
|
+
assert_parses(
|
2112
|
+
s(:send, s(:lvar, :foo), :%, s(:int, 1)),
|
2113
|
+
%q{foo % 1})
|
2114
|
+
|
2115
|
+
assert_parses(
|
2116
|
+
s(:send, s(:lvar, :foo), :**, s(:int, 1)),
|
2117
|
+
%q{foo ** 1})
|
2118
|
+
|
2119
|
+
assert_parses(
|
2120
|
+
s(:send, s(:lvar, :foo), :|, s(:int, 1)),
|
2121
|
+
%q{foo | 1})
|
2122
|
+
|
2123
|
+
assert_parses(
|
2124
|
+
s(:send, s(:lvar, :foo), :^, s(:int, 1)),
|
2125
|
+
%q{foo ^ 1})
|
2126
|
+
|
2127
|
+
assert_parses(
|
2128
|
+
s(:send, s(:lvar, :foo), :&, s(:int, 1)),
|
2129
|
+
%q{foo & 1})
|
2130
|
+
|
2131
|
+
assert_parses(
|
2132
|
+
s(:send, s(:lvar, :foo), :<=>, s(:int, 1)),
|
2133
|
+
%q{foo <=> 1})
|
2134
|
+
|
2135
|
+
assert_parses(
|
2136
|
+
s(:send, s(:lvar, :foo), :<, s(:int, 1)),
|
2137
|
+
%q{foo < 1})
|
2138
|
+
|
2139
|
+
assert_parses(
|
2140
|
+
s(:send, s(:lvar, :foo), :<=, s(:int, 1)),
|
2141
|
+
%q{foo <= 1})
|
2142
|
+
|
2143
|
+
assert_parses(
|
2144
|
+
s(:send, s(:lvar, :foo), :>, s(:int, 1)),
|
2145
|
+
%q{foo > 1})
|
2146
|
+
|
2147
|
+
assert_parses(
|
2148
|
+
s(:send, s(:lvar, :foo), :>=, s(:int, 1)),
|
2149
|
+
%q{foo >= 1})
|
2150
|
+
|
2151
|
+
assert_parses(
|
2152
|
+
s(:send, s(:lvar, :foo), :==, s(:int, 1)),
|
2153
|
+
%q{foo == 1})
|
2154
|
+
|
2155
|
+
assert_parses(
|
2156
|
+
s(:not, s(:send, s(:lvar, :foo), :==, s(:int, 1))),
|
2157
|
+
%q{foo != 1},
|
2158
|
+
%q{},
|
2159
|
+
%w(1.8))
|
2160
|
+
|
2161
|
+
assert_parses(
|
2162
|
+
s(:send, s(:lvar, :foo), :'!=', s(:int, 1)),
|
2163
|
+
%q{foo != 1},
|
2164
|
+
%q{},
|
2165
|
+
ALL_VERSIONS - %w(1.8))
|
2166
|
+
|
2167
|
+
assert_parses(
|
2168
|
+
s(:send, s(:lvar, :foo), :===, s(:int, 1)),
|
2169
|
+
%q{foo === 1})
|
2170
|
+
|
2171
|
+
assert_parses(
|
2172
|
+
s(:send, s(:lvar, :foo), :=~, s(:int, 1)),
|
2173
|
+
%q{foo =~ 1})
|
2174
|
+
|
2175
|
+
assert_parses(
|
2176
|
+
s(:not, s(:send, s(:lvar, :foo), :=~, s(:int, 1))),
|
2177
|
+
%q{foo !~ 1},
|
2178
|
+
%q{},
|
2179
|
+
%w(1.8))
|
2180
|
+
|
2181
|
+
assert_parses(
|
2182
|
+
s(:send, s(:lvar, :foo), :'!~', s(:int, 1)),
|
2183
|
+
%q{foo !~ 1},
|
2184
|
+
%q{},
|
2185
|
+
ALL_VERSIONS - %w(1.8))
|
2186
|
+
|
2187
|
+
assert_parses(
|
2188
|
+
s(:send, s(:lvar, :foo), :<<, s(:int, 1)),
|
2189
|
+
%q{foo << 1})
|
2190
|
+
|
2191
|
+
assert_parses(
|
2192
|
+
s(:send, s(:lvar, :foo), :>>, s(:int, 1)),
|
2193
|
+
%q{foo >> 1})
|
2194
|
+
end
|
2195
|
+
|
2196
|
+
def test_send_unary_op
|
2197
|
+
assert_parses(
|
2198
|
+
s(:send, s(:lvar, :foo), :-@),
|
2199
|
+
%q{-foo},
|
2200
|
+
%q{~ selector
|
2201
|
+
|~~~~ expression})
|
2202
|
+
|
2203
|
+
assert_parses(
|
2204
|
+
s(:send, s(:lvar, :foo), :+@),
|
2205
|
+
%q{+foo})
|
2206
|
+
|
2207
|
+
assert_parses(
|
2208
|
+
s(:send, s(:lvar, :foo), :~),
|
2209
|
+
%q{~foo})
|
2210
|
+
end
|
2211
|
+
|
2212
|
+
def test_bang
|
2213
|
+
assert_parses(
|
2214
|
+
s(:not, s(:lvar, :foo)),
|
2215
|
+
%q{!foo},
|
2216
|
+
%{},
|
2217
|
+
%w(1.8))
|
2218
|
+
|
2219
|
+
assert_parses(
|
2220
|
+
s(:send, s(:lvar, :foo), :'!'),
|
2221
|
+
%q{!foo},
|
2222
|
+
%{},
|
2223
|
+
ALL_VERSIONS - %w(1.8))
|
2224
|
+
end
|
2225
|
+
|
2226
|
+
def test_bang_cmd
|
2227
|
+
assert_parses(
|
2228
|
+
s(:not, s(:send, nil, :m, s(:lvar, :foo))),
|
2229
|
+
%q{!m foo},
|
2230
|
+
%{},
|
2231
|
+
%w(1.8))
|
2232
|
+
|
2233
|
+
assert_parses(
|
2234
|
+
s(:send, s(:send, nil, :m, s(:lvar, :foo)), :'!'),
|
2235
|
+
%q{!m foo},
|
2236
|
+
%{},
|
2237
|
+
ALL_VERSIONS - %w(1.8))
|
2238
|
+
end
|
2239
|
+
|
2240
|
+
def test_not
|
2241
|
+
assert_parses(
|
2242
|
+
s(:not, s(:lvar, :foo)),
|
2243
|
+
%q{not foo},
|
2244
|
+
%{},
|
2245
|
+
%w(1.8))
|
2246
|
+
|
2247
|
+
assert_parses(
|
2248
|
+
s(:send, s(:lvar, :foo), :'!'),
|
2249
|
+
%q{not foo},
|
2250
|
+
%{},
|
2251
|
+
ALL_VERSIONS - %w(1.8))
|
2252
|
+
|
2253
|
+
assert_parses(
|
2254
|
+
s(:send, s(:lvar, :foo), :'!'),
|
2255
|
+
%q{not(foo)},
|
2256
|
+
%{},
|
2257
|
+
ALL_VERSIONS - %w(1.8))
|
2258
|
+
|
2259
|
+
assert_parses(
|
2260
|
+
s(:send, s(:nil), :'!'),
|
2261
|
+
%q{not()},
|
2262
|
+
%{},
|
2263
|
+
ALL_VERSIONS - %w(1.8))
|
2264
|
+
end
|
2265
|
+
|
2266
|
+
def test_not_cmd
|
2267
|
+
assert_parses(
|
2268
|
+
s(:not, s(:send, nil, :m, s(:lvar, :foo))),
|
2269
|
+
%q{not m foo},
|
2270
|
+
%{},
|
2271
|
+
%w(1.8))
|
2272
|
+
|
2273
|
+
assert_parses(
|
2274
|
+
s(:send, s(:send, nil, :m, s(:lvar, :foo)), :'!'),
|
2275
|
+
%q{not m foo},
|
2276
|
+
%{},
|
2277
|
+
ALL_VERSIONS - %w(1.8))
|
2278
|
+
end
|
2279
|
+
|
2280
|
+
def test_pow_precedence
|
2281
|
+
assert_parses(
|
2282
|
+
s(:send, s(:send, s(:int, 2), :**, s(:int, 10)), :-@),
|
2283
|
+
%q{-2 ** 10})
|
2284
|
+
|
2285
|
+
assert_parses(
|
2286
|
+
s(:send, s(:send, s(:float, 2.0), :**, s(:int, 10)), :-@),
|
2287
|
+
%q{-2.0 ** 10})
|
2288
|
+
end
|
2289
|
+
|
2290
|
+
def test_send_attr_asgn
|
2291
|
+
assert_parses(
|
2292
|
+
s(:send, s(:lvar, :foo), :a=, s(:int, 1)),
|
2293
|
+
%q{foo.a = 1},
|
2294
|
+
%q{ ~~~ selector
|
2295
|
+
|~~~~~~~~~ expression})
|
2296
|
+
|
2297
|
+
assert_parses(
|
2298
|
+
s(:send, s(:lvar, :foo), :a=, s(:int, 1)),
|
2299
|
+
"foo::a = 1")
|
2300
|
+
|
2301
|
+
assert_parses(
|
2302
|
+
s(:send, s(:lvar, :foo), :A=, s(:int, 1)),
|
2303
|
+
"foo.A = 1")
|
2304
|
+
|
2305
|
+
assert_parses(
|
2306
|
+
s(:cdecl, s(:lvar, :foo), :A, s(:int, 1)),
|
2307
|
+
"foo::A = 1")
|
2308
|
+
end
|
2309
|
+
|
2310
|
+
def test_send_index
|
2311
|
+
assert_parses(
|
2312
|
+
s(:send, s(:lvar, :foo), :[],
|
2313
|
+
s(:int, 1), s(:int, 2)),
|
2314
|
+
%q{foo[1, 2]},
|
2315
|
+
%q{ ~~~~~~ selector
|
2316
|
+
| ^ begin
|
2317
|
+
| ^ end
|
2318
|
+
|~~~~~~~~~ expression})
|
2319
|
+
end
|
2320
|
+
|
2321
|
+
def test_send_index_cmd
|
2322
|
+
assert_parses(
|
2323
|
+
s(:send, s(:lvar, :foo), :[],
|
2324
|
+
s(:send, nil, :m, s(:lvar, :bar))),
|
2325
|
+
%q{foo[m bar]})
|
2326
|
+
end
|
2327
|
+
|
2328
|
+
def test_send_index_asgn
|
2329
|
+
assert_parses(
|
2330
|
+
s(:send, s(:lvar, :foo), :[]=,
|
2331
|
+
s(:int, 1), s(:int, 2), s(:int, 3)),
|
2332
|
+
%q{foo[1, 2] = 3},
|
2333
|
+
%q{ ~~~~~~~~ selector
|
2334
|
+
| ^ begin
|
2335
|
+
| ^ end
|
2336
|
+
|~~~~~~~~~~~~~ expression})
|
2337
|
+
end
|
2338
|
+
|
2339
|
+
def test_send_lambda
|
2340
|
+
assert_parses(
|
2341
|
+
s(:block, s(:send, nil, :lambda),
|
2342
|
+
s(:args), s(:nil)),
|
2343
|
+
%q{->{ }},
|
2344
|
+
%q{~~ selector (send)
|
2345
|
+
| ^ begin
|
2346
|
+
| ^ end
|
2347
|
+
|~~~~~ expression},
|
2348
|
+
ALL_VERSIONS - %w(1.8))
|
2349
|
+
|
2350
|
+
assert_parses(
|
2351
|
+
s(:block, s(:send, nil, :lambda),
|
2352
|
+
s(:args), s(:nil)),
|
2353
|
+
%q{-> do end},
|
2354
|
+
%q{~~ selector (send)
|
2355
|
+
| ^^ begin
|
2356
|
+
| ^^^ end
|
2357
|
+
|~~~~~~~~~ expression},
|
2358
|
+
ALL_VERSIONS - %w(1.8))
|
2359
|
+
end
|
2360
|
+
|
2361
|
+
def test_send_lambda_args
|
2362
|
+
assert_parses(
|
2363
|
+
s(:block, s(:send, nil, :lambda),
|
2364
|
+
s(:args,
|
2365
|
+
s(:arg, :a)),
|
2366
|
+
s(:nil)),
|
2367
|
+
%q{->(a) { }},
|
2368
|
+
%q{~~ selector (send)
|
2369
|
+
| ^ begin (args)
|
2370
|
+
| ^ end (args)
|
2371
|
+
| ^ begin
|
2372
|
+
| ^ end
|
2373
|
+
|~~~~~ expression},
|
2374
|
+
ALL_VERSIONS - %w(1.8))
|
2375
|
+
end
|
2376
|
+
|
2377
|
+
def test_send_lambda_args_shadow
|
2378
|
+
assert_parses(
|
2379
|
+
s(:block, s(:send, nil, :lambda),
|
2380
|
+
s(:args,
|
2381
|
+
s(:arg, :a),
|
2382
|
+
s(:shadowarg, :foo),
|
2383
|
+
s(:shadowarg, :bar)),
|
2384
|
+
s(:nil)),
|
2385
|
+
%q{->(a; foo, bar) { }},
|
2386
|
+
%q{ ~~~ expression (args.shadowarg)},
|
2387
|
+
ALL_VERSIONS - %w(1.8))
|
2388
|
+
end
|
2389
|
+
|
2390
|
+
def test_send_call
|
2391
|
+
assert_parses(
|
2392
|
+
s(:send, s(:lvar, :foo), :call,
|
2393
|
+
s(:int, 1)),
|
2394
|
+
%q{foo.(1)},
|
2395
|
+
%q{ ^ begin
|
2396
|
+
| ^ end
|
2397
|
+
|~~~~~~~ expression},
|
2398
|
+
ALL_VERSIONS - %w(1.8))
|
2399
|
+
|
2400
|
+
assert_parses(
|
2401
|
+
s(:send, s(:lvar, :foo), :call,
|
2402
|
+
s(:int, 1)),
|
2403
|
+
%q{foo::(1)},
|
2404
|
+
%q{ ^ begin
|
2405
|
+
| ^ end
|
2406
|
+
|~~~~~~~~ expression},
|
2407
|
+
ALL_VERSIONS - %w(1.8))
|
2408
|
+
end
|
2409
|
+
|
2410
|
+
# To superclass
|
2411
|
+
|
2412
|
+
def test_super
|
2413
|
+
assert_parses(
|
2414
|
+
s(:super, s(:lvar, :foo)),
|
2415
|
+
%q{super(foo)},
|
2416
|
+
%q{~~~~~ keyword
|
2417
|
+
| ^ begin
|
2418
|
+
| ^ end
|
2419
|
+
|~~~~~~~~~~ expression})
|
2420
|
+
|
2421
|
+
assert_parses(
|
2422
|
+
s(:super, s(:lvar, :foo)),
|
2423
|
+
%q{super foo},
|
2424
|
+
%q{~~~~~ keyword
|
2425
|
+
|~~~~~~~~~ expression})
|
2426
|
+
|
2427
|
+
assert_parses(
|
2428
|
+
s(:super),
|
2429
|
+
%q{super()},
|
2430
|
+
%q{~~~~~ keyword
|
2431
|
+
| ^ begin
|
2432
|
+
| ^ end
|
2433
|
+
|~~~~~~~ expression})
|
2434
|
+
end
|
2435
|
+
|
2436
|
+
def test_zsuper
|
2437
|
+
assert_parses(
|
2438
|
+
s(:zsuper),
|
2439
|
+
%q{super},
|
2440
|
+
%q{~~~~~ keyword
|
2441
|
+
|~~~~~ expression})
|
2442
|
+
end
|
2443
|
+
|
2444
|
+
# To block argument
|
2445
|
+
|
2446
|
+
def test_yield
|
2447
|
+
assert_parses(
|
2448
|
+
s(:yield, s(:lvar, :foo)),
|
2449
|
+
%q{yield(foo)},
|
2450
|
+
%q{~~~~~ keyword
|
2451
|
+
| ^ begin
|
2452
|
+
| ^ end
|
2453
|
+
|~~~~~~~~~~ expression})
|
2454
|
+
|
2455
|
+
assert_parses(
|
2456
|
+
s(:yield, s(:lvar, :foo)),
|
2457
|
+
%q{yield foo},
|
2458
|
+
%q{~~~~~ keyword
|
2459
|
+
|~~~~~~~~~ expression})
|
2460
|
+
|
2461
|
+
assert_parses(
|
2462
|
+
s(:yield),
|
2463
|
+
%q{yield()},
|
2464
|
+
%q{~~~~~ keyword
|
2465
|
+
| ^ begin
|
2466
|
+
| ^ end
|
2467
|
+
|~~~~~~~ expression})
|
2468
|
+
|
2469
|
+
assert_parses(
|
2470
|
+
s(:yield),
|
2471
|
+
%q{yield},
|
2472
|
+
%q{~~~~~ keyword
|
2473
|
+
|~~~~~ expression})
|
2474
|
+
end
|
2475
|
+
|
2476
|
+
# Call arguments
|
2477
|
+
|
2478
|
+
def test_args_cmd
|
2479
|
+
assert_parses(
|
2480
|
+
s(:send, nil, :fun,
|
2481
|
+
s(:send, nil, :f, s(:lvar, :bar))),
|
2482
|
+
%q{fun(f bar)})
|
2483
|
+
end
|
2484
|
+
|
2485
|
+
def test_args_args_star
|
2486
|
+
assert_parses(
|
2487
|
+
s(:send, nil, :fun,
|
2488
|
+
s(:lvar, :foo), s(:splat, s(:lvar, :bar))),
|
2489
|
+
%q{fun(foo, *bar)})
|
2490
|
+
|
2491
|
+
assert_parses(
|
2492
|
+
s(:send, nil, :fun,
|
2493
|
+
s(:lvar, :foo), s(:splat, s(:lvar, :bar)),
|
2494
|
+
s(:block_pass, s(:lvar, :baz))),
|
2495
|
+
%q{fun(foo, *bar, &baz)})
|
2496
|
+
end
|
2497
|
+
|
2498
|
+
def test_args_star
|
2499
|
+
assert_parses(
|
2500
|
+
s(:send, nil, :fun,
|
2501
|
+
s(:splat, s(:lvar, :bar))),
|
2502
|
+
%q{fun(*bar)})
|
2503
|
+
|
2504
|
+
assert_parses(
|
2505
|
+
s(:send, nil, :fun,
|
2506
|
+
s(:splat, s(:lvar, :bar)),
|
2507
|
+
s(:block_pass, s(:lvar, :baz))),
|
2508
|
+
%q{fun(*bar, &baz)})
|
2509
|
+
end
|
2510
|
+
|
2511
|
+
def test_args_block_pass
|
2512
|
+
assert_parses(
|
2513
|
+
s(:send, nil, :fun,
|
2514
|
+
s(:block_pass, s(:lvar, :bar))),
|
2515
|
+
%q{fun(&bar)})
|
2516
|
+
end
|
2517
|
+
|
2518
|
+
def test_args_args_comma
|
2519
|
+
assert_parses(
|
2520
|
+
s(:send, s(:lvar, :foo), :[],
|
2521
|
+
s(:lvar, :bar)),
|
2522
|
+
%q{foo[bar,]},
|
2523
|
+
%q{},
|
2524
|
+
ALL_VERSIONS - %w(1.8))
|
2525
|
+
end
|
2526
|
+
|
2527
|
+
def test_args_assocs
|
2528
|
+
assert_parses(
|
2529
|
+
s(:send, nil, :fun,
|
2530
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1)))),
|
2531
|
+
%q{fun(:foo => 1)})
|
2532
|
+
|
2533
|
+
assert_parses(
|
2534
|
+
s(:send, nil, :fun,
|
2535
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2536
|
+
s(:block_pass, s(:lvar, :baz))),
|
2537
|
+
%q{fun(:foo => 1, &baz)})
|
2538
|
+
end
|
2539
|
+
|
2540
|
+
def test_args_assocs_star
|
2541
|
+
assert_parses(
|
2542
|
+
s(:send, nil, :fun,
|
2543
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2544
|
+
s(:splat, s(:lvar, :bar))),
|
2545
|
+
%q{fun(:foo => 1, *bar)},
|
2546
|
+
%q{},
|
2547
|
+
%w(1.8))
|
2548
|
+
|
2549
|
+
assert_parses(
|
2550
|
+
s(:send, nil, :fun,
|
2551
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2552
|
+
s(:splat, s(:lvar, :bar)),
|
2553
|
+
s(:block_pass, s(:lvar, :baz))),
|
2554
|
+
%q{fun(:foo => 1, *bar, &baz)},
|
2555
|
+
%q{},
|
2556
|
+
%w(1.8))
|
2557
|
+
end
|
2558
|
+
|
2559
|
+
def test_args_assocs_comma
|
2560
|
+
assert_parses(
|
2561
|
+
s(:send, s(:lvar, :foo), :[],
|
2562
|
+
s(:hash, s(:pair, s(:sym, :baz), s(:int, 1)))),
|
2563
|
+
%q{foo[:baz => 1,]},
|
2564
|
+
%q{},
|
2565
|
+
ALL_VERSIONS - %w(1.8))
|
2566
|
+
end
|
2567
|
+
|
2568
|
+
def test_args_args_assocs
|
2569
|
+
assert_parses(
|
2570
|
+
s(:send, nil, :fun,
|
2571
|
+
s(:lvar, :foo),
|
2572
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1)))),
|
2573
|
+
%q{fun(foo, :foo => 1)})
|
2574
|
+
|
2575
|
+
assert_parses(
|
2576
|
+
s(:send, nil, :fun,
|
2577
|
+
s(:lvar, :foo),
|
2578
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2579
|
+
s(:block_pass, s(:lvar, :baz))),
|
2580
|
+
%q{fun(foo, :foo => 1, &baz)})
|
2581
|
+
end
|
2582
|
+
|
2583
|
+
def test_args_args_assocs_comma
|
2584
|
+
assert_parses(
|
2585
|
+
s(:send, s(:lvar, :foo), :[],
|
2586
|
+
s(:lvar, :bar),
|
2587
|
+
s(:hash, s(:pair, s(:sym, :baz), s(:int, 1)))),
|
2588
|
+
%q{foo[bar, :baz => 1,]},
|
2589
|
+
%q{},
|
2590
|
+
ALL_VERSIONS - %w(1.8))
|
2591
|
+
end
|
2592
|
+
|
2593
|
+
def test_args_args_assocs_star
|
2594
|
+
assert_parses(
|
2595
|
+
s(:send, nil, :fun,
|
2596
|
+
s(:lvar, :foo),
|
2597
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2598
|
+
s(:splat, s(:lvar, :bar))),
|
2599
|
+
%q{fun(foo, :foo => 1, *bar)},
|
2600
|
+
%q{},
|
2601
|
+
%w(1.8))
|
2602
|
+
|
2603
|
+
assert_parses(
|
2604
|
+
s(:send, nil, :fun,
|
2605
|
+
s(:lvar, :foo),
|
2606
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2607
|
+
s(:splat, s(:lvar, :bar)),
|
2608
|
+
s(:block_pass, s(:lvar, :baz))),
|
2609
|
+
%q{fun(foo, :foo => 1, *bar, &baz)},
|
2610
|
+
%q{},
|
2611
|
+
%w(1.8))
|
2612
|
+
end
|
2613
|
+
|
2614
|
+
# Call arguments with whitespace
|
2615
|
+
|
2616
|
+
def test_space_args_cmd
|
2617
|
+
assert_parses(
|
2618
|
+
s(:send, nil, :fun,
|
2619
|
+
s(:send, nil, :f, s(:lvar, :bar))),
|
2620
|
+
%q{fun (f bar)})
|
2621
|
+
end
|
2622
|
+
|
2623
|
+
def test_space_args_arg
|
2624
|
+
assert_parses(
|
2625
|
+
s(:send, nil, :fun, s(:int, 1)),
|
2626
|
+
%q{fun (1)})
|
2627
|
+
end
|
2628
|
+
|
2629
|
+
def test_space_args_arg_block
|
2630
|
+
assert_parses(
|
2631
|
+
s(:block,
|
2632
|
+
s(:send, nil, :fun, s(:int, 1)),
|
2633
|
+
s(:args), s(:nil)),
|
2634
|
+
%q{fun (1) {}})
|
2635
|
+
|
2636
|
+
assert_parses(
|
2637
|
+
s(:block,
|
2638
|
+
s(:send, s(:lvar, :foo), :fun, s(:int, 1)),
|
2639
|
+
s(:args), s(:nil)),
|
2640
|
+
%q{foo.fun (1) {}})
|
2641
|
+
|
2642
|
+
assert_parses(
|
2643
|
+
s(:block,
|
2644
|
+
s(:send, s(:lvar, :foo), :fun, s(:int, 1)),
|
2645
|
+
s(:args), s(:nil)),
|
2646
|
+
%q{foo::fun (1) {}})
|
2647
|
+
end
|
2648
|
+
|
2649
|
+
def test_space_args_arg_call
|
2650
|
+
assert_parses(
|
2651
|
+
s(:send, nil, :fun,
|
2652
|
+
s(:send, s(:int, 1), :to_i)),
|
2653
|
+
%q{fun (1).to_i})
|
2654
|
+
|
2655
|
+
assert_diagnoses(
|
2656
|
+
[:warning, :grouped_expression],
|
2657
|
+
%q{fun (1).to_i},
|
2658
|
+
%q{ ~~~ location})
|
2659
|
+
end
|
2660
|
+
|
2661
|
+
def test_space_args_block_pass
|
2662
|
+
assert_parses(
|
2663
|
+
s(:send, nil, :fun,
|
2664
|
+
s(:block_pass, s(:lvar, :foo))),
|
2665
|
+
%q{fun (&foo)},
|
2666
|
+
%q{},
|
2667
|
+
%w(1.8))
|
2668
|
+
end
|
2669
|
+
|
2670
|
+
def test_space_args_arg_block_pass
|
2671
|
+
assert_parses(
|
2672
|
+
s(:send, nil, :fun,
|
2673
|
+
s(:lvar, :foo),
|
2674
|
+
s(:block_pass, s(:lvar, :bar))),
|
2675
|
+
%q{fun (foo, &bar)},
|
2676
|
+
%q{},
|
2677
|
+
%w(1.8))
|
2678
|
+
end
|
2679
|
+
|
2680
|
+
def test_space_args_args_star
|
2681
|
+
assert_parses(
|
2682
|
+
s(:send, nil, :fun,
|
2683
|
+
s(:lvar, :foo), s(:splat, s(:lvar, :bar))),
|
2684
|
+
%q{fun (foo, *bar)},
|
2685
|
+
%q{},
|
2686
|
+
%w(1.8))
|
2687
|
+
|
2688
|
+
assert_parses(
|
2689
|
+
s(:send, nil, :fun,
|
2690
|
+
s(:lvar, :foo), s(:splat, s(:lvar, :bar)),
|
2691
|
+
s(:block_pass, s(:lvar, :baz))),
|
2692
|
+
%q{fun (foo, *bar, &baz)},
|
2693
|
+
%q{},
|
2694
|
+
%w(1.8))
|
2695
|
+
|
2696
|
+
assert_parses(
|
2697
|
+
s(:send, nil, :fun,
|
2698
|
+
s(:lvar, :foo), s(:int, 1),
|
2699
|
+
s(:splat, s(:lvar, :bar))),
|
2700
|
+
%q{fun (foo, 1, *bar)},
|
2701
|
+
%q{},
|
2702
|
+
%w(1.8))
|
2703
|
+
|
2704
|
+
assert_parses(
|
2705
|
+
s(:send, nil, :fun,
|
2706
|
+
s(:lvar, :foo), s(:int, 1),
|
2707
|
+
s(:splat, s(:lvar, :bar)),
|
2708
|
+
s(:block_pass, s(:lvar, :baz))),
|
2709
|
+
%q{fun (foo, 1, *bar, &baz)},
|
2710
|
+
%q{},
|
2711
|
+
%w(1.8))
|
2712
|
+
end
|
2713
|
+
|
2714
|
+
def test_space_args_star
|
2715
|
+
assert_parses(
|
2716
|
+
s(:send, nil, :fun,
|
2717
|
+
s(:splat, s(:lvar, :bar))),
|
2718
|
+
%q{fun (*bar)},
|
2719
|
+
%q{},
|
2720
|
+
%w(1.8))
|
2721
|
+
|
2722
|
+
assert_parses(
|
2723
|
+
s(:send, nil, :fun,
|
2724
|
+
s(:splat, s(:lvar, :bar)),
|
2725
|
+
s(:block_pass, s(:lvar, :baz))),
|
2726
|
+
%q{fun (*bar, &baz)},
|
2727
|
+
%q{},
|
2728
|
+
%w(1.8))
|
2729
|
+
end
|
2730
|
+
|
2731
|
+
def test_space_args_assocs
|
2732
|
+
assert_parses(
|
2733
|
+
s(:send, nil, :fun,
|
2734
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1)))),
|
2735
|
+
%q{fun (:foo => 1)},
|
2736
|
+
%q{},
|
2737
|
+
%w(1.8))
|
2738
|
+
|
2739
|
+
assert_parses(
|
2740
|
+
s(:send, nil, :fun,
|
2741
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2742
|
+
s(:block_pass, s(:lvar, :baz))),
|
2743
|
+
%q{fun (:foo => 1, &baz)},
|
2744
|
+
%q{},
|
2745
|
+
%w(1.8))
|
2746
|
+
end
|
2747
|
+
|
2748
|
+
def test_space_args_assocs_star
|
2749
|
+
assert_parses(
|
2750
|
+
s(:send, nil, :fun,
|
2751
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2752
|
+
s(:splat, s(:lvar, :bar))),
|
2753
|
+
%q{fun (:foo => 1, *bar)},
|
2754
|
+
%q{},
|
2755
|
+
%w(1.8))
|
2756
|
+
|
2757
|
+
assert_parses(
|
2758
|
+
s(:send, nil, :fun,
|
2759
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2760
|
+
s(:splat, s(:lvar, :bar)),
|
2761
|
+
s(:block_pass, s(:lvar, :baz))),
|
2762
|
+
%q{fun (:foo => 1, *bar, &baz)},
|
2763
|
+
%q{},
|
2764
|
+
%w(1.8))
|
2765
|
+
end
|
2766
|
+
|
2767
|
+
def test_space_args_args_assocs
|
2768
|
+
assert_parses(
|
2769
|
+
s(:send, nil, :fun,
|
2770
|
+
s(:lvar, :foo),
|
2771
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1)))),
|
2772
|
+
%q{fun (foo, :foo => 1)},
|
2773
|
+
%q{},
|
2774
|
+
%w(1.8))
|
2775
|
+
|
2776
|
+
assert_parses(
|
2777
|
+
s(:send, nil, :fun,
|
2778
|
+
s(:lvar, :foo),
|
2779
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2780
|
+
s(:block_pass, s(:lvar, :baz))),
|
2781
|
+
%q{fun (foo, :foo => 1, &baz)},
|
2782
|
+
%q{},
|
2783
|
+
%w(1.8))
|
2784
|
+
|
2785
|
+
assert_parses(
|
2786
|
+
s(:send, nil, :fun,
|
2787
|
+
s(:lvar, :foo), s(:int, 1),
|
2788
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1)))),
|
2789
|
+
%q{fun (foo, 1, :foo => 1)},
|
2790
|
+
%q{},
|
2791
|
+
%w(1.8))
|
2792
|
+
|
2793
|
+
assert_parses(
|
2794
|
+
s(:send, nil, :fun,
|
2795
|
+
s(:lvar, :foo), s(:int, 1),
|
2796
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2797
|
+
s(:block_pass, s(:lvar, :baz))),
|
2798
|
+
%q{fun (foo, 1, :foo => 1, &baz)},
|
2799
|
+
%q{},
|
2800
|
+
%w(1.8))
|
2801
|
+
end
|
2802
|
+
|
2803
|
+
def test_space_args_args_assocs_star
|
2804
|
+
assert_parses(
|
2805
|
+
s(:send, nil, :fun,
|
2806
|
+
s(:lvar, :foo),
|
2807
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2808
|
+
s(:splat, s(:lvar, :bar))),
|
2809
|
+
%q{fun (foo, :foo => 1, *bar)},
|
2810
|
+
%q{},
|
2811
|
+
%w(1.8))
|
2812
|
+
|
2813
|
+
assert_parses(
|
2814
|
+
s(:send, nil, :fun,
|
2815
|
+
s(:lvar, :foo),
|
2816
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2817
|
+
s(:splat, s(:lvar, :bar)),
|
2818
|
+
s(:block_pass, s(:lvar, :baz))),
|
2819
|
+
%q{fun (foo, :foo => 1, *bar, &baz)},
|
2820
|
+
%q{},
|
2821
|
+
%w(1.8))
|
2822
|
+
|
2823
|
+
assert_parses(
|
2824
|
+
s(:send, nil, :fun,
|
2825
|
+
s(:lvar, :foo), s(:int, 1),
|
2826
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2827
|
+
s(:splat, s(:lvar, :bar))),
|
2828
|
+
%q{fun (foo, 1, :foo => 1, *bar)},
|
2829
|
+
%q{},
|
2830
|
+
%w(1.8))
|
2831
|
+
|
2832
|
+
assert_parses(
|
2833
|
+
s(:send, nil, :fun,
|
2834
|
+
s(:lvar, :foo), s(:int, 1),
|
2835
|
+
s(:hash, s(:pair, s(:sym, :foo), s(:int, 1))),
|
2836
|
+
s(:splat, s(:lvar, :bar)),
|
2837
|
+
s(:block_pass, s(:lvar, :baz))),
|
2838
|
+
%q{fun (foo, 1, :foo => 1, *bar, &baz)},
|
2839
|
+
%q{},
|
2840
|
+
%w(1.8))
|
2841
|
+
end
|
2842
|
+
|
2843
|
+
def test_space_args_arg_arg
|
2844
|
+
assert_parses(
|
2845
|
+
s(:send, nil, :fun, s(:int, 1), s(:int, 2)),
|
2846
|
+
%q{fun (1, 2)},
|
2847
|
+
%q{},
|
2848
|
+
%w(1.8))
|
2849
|
+
|
2850
|
+
assert_diagnoses(
|
2851
|
+
[:warning, :space_before_lparen],
|
2852
|
+
%q{fun (1, 2)},
|
2853
|
+
%q{ ^ location},
|
2854
|
+
%w(1.8))
|
2855
|
+
end
|
2856
|
+
|
2857
|
+
def test_space_args_none
|
2858
|
+
assert_parses(
|
2859
|
+
s(:send, nil, :fun),
|
2860
|
+
%q{fun ()},
|
2861
|
+
%q{},
|
2862
|
+
%w(1.8))
|
2863
|
+
|
2864
|
+
assert_diagnoses(
|
2865
|
+
[:warning, :space_before_lparen],
|
2866
|
+
%q{fun ()},
|
2867
|
+
%q{ ^ location},
|
2868
|
+
%w(1.8))
|
2869
|
+
end
|
2870
|
+
|
2871
|
+
def test_space_args_block
|
2872
|
+
assert_parses(
|
2873
|
+
s(:block,
|
2874
|
+
s(:send, nil, :fun),
|
2875
|
+
s(:args), s(:nil)),
|
2876
|
+
%q{fun () {}},
|
2877
|
+
%q{ ^ begin (send)
|
2878
|
+
| ^ end (send)},
|
2879
|
+
%w(1.8))
|
2880
|
+
|
2881
|
+
assert_parses(
|
2882
|
+
s(:block,
|
2883
|
+
s(:send, s(:lvar, :foo), :fun),
|
2884
|
+
s(:args), s(:nil)),
|
2885
|
+
%q{foo.fun () {}},
|
2886
|
+
%q{ ^ begin (send)
|
2887
|
+
| ^ end (send)},
|
2888
|
+
%w(1.8))
|
2889
|
+
|
2890
|
+
assert_parses(
|
2891
|
+
s(:block,
|
2892
|
+
s(:send, s(:lvar, :foo), :fun),
|
2893
|
+
s(:args), s(:nil)),
|
2894
|
+
%q{foo::fun () {}},
|
2895
|
+
%q{ ^ begin (send)
|
2896
|
+
| ^ end (send)},
|
2897
|
+
%w(1.8))
|
2898
|
+
end
|
2899
|
+
|
2900
|
+
#
|
2901
|
+
# Control flow
|
2902
|
+
#
|
2903
|
+
|
2904
|
+
# Operators
|
2905
|
+
|
2906
|
+
def test_and
|
2907
|
+
assert_parses(
|
2908
|
+
s(:and, s(:lvar, :foo), s(:lvar, :bar)),
|
2909
|
+
%q{foo and bar},
|
2910
|
+
%q{ ~~~ operator
|
2911
|
+
|~~~~~~~~~~~ expression})
|
2912
|
+
|
2913
|
+
assert_parses(
|
2914
|
+
s(:and, s(:lvar, :foo), s(:lvar, :bar)),
|
2915
|
+
%q{foo && bar},
|
2916
|
+
%q{ ~~ operator
|
2917
|
+
|~~~~~~~~~~ expression})
|
2918
|
+
end
|
2919
|
+
|
2920
|
+
def test_or
|
2921
|
+
assert_parses(
|
2922
|
+
s(:or, s(:lvar, :foo), s(:lvar, :bar)),
|
2923
|
+
%q{foo or bar},
|
2924
|
+
%q{ ~~ operator
|
2925
|
+
|~~~~~~~~~~ expression})
|
2926
|
+
|
2927
|
+
assert_parses(
|
2928
|
+
s(:or, s(:lvar, :foo), s(:lvar, :bar)),
|
2929
|
+
%q{foo || bar},
|
2930
|
+
%q{ ~~ operator
|
2931
|
+
|~~~~~~~~~~ expression})
|
2932
|
+
end
|
2933
|
+
|
2934
|
+
# Branching
|
2935
|
+
|
2936
|
+
def test_if
|
2937
|
+
assert_parses(
|
2938
|
+
s(:if, s(:lvar, :foo), s(:lvar, :bar), nil),
|
2939
|
+
%q{if foo then bar; end},
|
2940
|
+
%q{~~ keyword
|
2941
|
+
| ~~~~ begin
|
2942
|
+
| ~~~ end
|
2943
|
+
|~~~~~~~~~~~~~~~~~~~~ expression})
|
2944
|
+
|
2945
|
+
assert_parses(
|
2946
|
+
s(:if, s(:lvar, :foo), s(:lvar, :bar), nil),
|
2947
|
+
%q{if foo; bar; end},
|
2948
|
+
%q{~~ keyword
|
2949
|
+
| ~~~ end
|
2950
|
+
|~~~~~~~~~~~~~~~~ expression})
|
2951
|
+
end
|
2952
|
+
|
2953
|
+
def test_if_mod
|
2954
|
+
assert_parses(
|
2955
|
+
s(:if, s(:lvar, :foo), s(:lvar, :bar), nil),
|
2956
|
+
%q{bar if foo},
|
2957
|
+
%q{ ~~ keyword
|
2958
|
+
|~~~~~~~~~~ expression})
|
2959
|
+
end
|
2960
|
+
|
2961
|
+
def test_unless
|
2962
|
+
assert_parses(
|
2963
|
+
s(:if, s(:lvar, :foo), nil, s(:lvar, :bar)),
|
2964
|
+
%q{unless foo then bar; end},
|
2965
|
+
%q{~~~~~~ keyword
|
2966
|
+
| ~~~~ begin
|
2967
|
+
| ~~~ end
|
2968
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
2969
|
+
|
2970
|
+
assert_parses(
|
2971
|
+
s(:if, s(:lvar, :foo), nil, s(:lvar, :bar)),
|
2972
|
+
%q{unless foo; bar; end},
|
2973
|
+
%q{~~~~~~ keyword
|
2974
|
+
| ~~~ end
|
2975
|
+
|~~~~~~~~~~~~~~~~~~~~ expression})
|
2976
|
+
end
|
2977
|
+
|
2978
|
+
def test_unless_mod
|
2979
|
+
assert_parses(
|
2980
|
+
s(:if, s(:lvar, :foo), nil, s(:lvar, :bar)),
|
2981
|
+
%q{bar unless foo},
|
2982
|
+
%q{ ~~~~~~ keyword
|
2983
|
+
|~~~~~~~~~~~~~~ expression})
|
2984
|
+
end
|
2985
|
+
|
2986
|
+
def test_if_else
|
2987
|
+
assert_parses(
|
2988
|
+
s(:if, s(:lvar, :foo), s(:lvar, :bar), s(:lvar, :baz)),
|
2989
|
+
%q{if foo then bar; else baz; end},
|
2990
|
+
%q{~~ keyword
|
2991
|
+
| ~~~~ begin
|
2992
|
+
| ~~~~ else
|
2993
|
+
| ~~~ end
|
2994
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
2995
|
+
|
2996
|
+
assert_parses(
|
2997
|
+
s(:if, s(:lvar, :foo), s(:lvar, :bar), s(:lvar, :baz)),
|
2998
|
+
%q{if foo; bar; else baz; end},
|
2999
|
+
%q{~~ keyword
|
3000
|
+
| ~~~~ else
|
3001
|
+
| ~~~ end
|
3002
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3003
|
+
end
|
3004
|
+
|
3005
|
+
def test_unless_else
|
3006
|
+
assert_parses(
|
3007
|
+
s(:if, s(:lvar, :foo), s(:lvar, :baz), s(:lvar, :bar)),
|
3008
|
+
%q{unless foo then bar; else baz; end},
|
3009
|
+
%q{~~~~~~ keyword
|
3010
|
+
| ~~~~ begin
|
3011
|
+
| ~~~~ else
|
3012
|
+
| ~~~ end
|
3013
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3014
|
+
|
3015
|
+
assert_parses(
|
3016
|
+
s(:if, s(:lvar, :foo), s(:lvar, :baz), s(:lvar, :bar)),
|
3017
|
+
%q{unless foo; bar; else baz; end},
|
3018
|
+
%q{~~~~~~ keyword
|
3019
|
+
| ~~~~ else
|
3020
|
+
| ~~~ end
|
3021
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3022
|
+
end
|
3023
|
+
|
3024
|
+
def test_if_elsif
|
3025
|
+
assert_parses(
|
3026
|
+
s(:if, s(:lvar, :foo), s(:lvar, :bar),
|
3027
|
+
s(:if, s(:lvar, :baz), s(:int, 1), s(:int, 2))),
|
3028
|
+
%q{if foo; bar; elsif baz; 1; else 2; end},
|
3029
|
+
%q{~~ keyword
|
3030
|
+
| ~~~~~ else
|
3031
|
+
| ~~~~~ keyword (if)
|
3032
|
+
| ~~~ end
|
3033
|
+
| ~~~ end (if)
|
3034
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3035
|
+
end
|
3036
|
+
|
3037
|
+
def test_ternary
|
3038
|
+
assert_parses(
|
3039
|
+
s(:if, s(:lvar, :foo), s(:int, 1), s(:int, 2)),
|
3040
|
+
%q{foo ? 1 : 2},
|
3041
|
+
%q{ ^ question
|
3042
|
+
| ^ colon
|
3043
|
+
|~~~~~~~~~~~ expression})
|
3044
|
+
end
|
3045
|
+
|
3046
|
+
def test_if_masgn
|
3047
|
+
assert_diagnoses(
|
3048
|
+
[:error, :masgn_as_condition],
|
3049
|
+
%q{if (a, b = foo); end},
|
3050
|
+
%q{ ~~~~~~~~~~~~ location})
|
3051
|
+
end
|
3052
|
+
|
3053
|
+
def test_if_mod_masgn
|
3054
|
+
assert_diagnoses(
|
3055
|
+
[:error, :masgn_as_condition],
|
3056
|
+
%q{1 if (a, b = foo)},
|
3057
|
+
%q{ ~~~~~~~~~~~~ location})
|
3058
|
+
end
|
3059
|
+
|
3060
|
+
def test_tern_masgn
|
3061
|
+
assert_diagnoses(
|
3062
|
+
[:error, :masgn_as_condition],
|
3063
|
+
%q{(a, b = foo) ? 1 : 2},
|
3064
|
+
%q{~~~~~~~~~~~~ location})
|
3065
|
+
end
|
3066
|
+
|
3067
|
+
# Case matching
|
3068
|
+
|
3069
|
+
def test_case_expr
|
3070
|
+
assert_parses(
|
3071
|
+
s(:case, s(:lvar, :foo),
|
3072
|
+
s(:when, s(:str, 'bar'),
|
3073
|
+
s(:lvar, :bar)),
|
3074
|
+
nil),
|
3075
|
+
%q{case foo; when 'bar'; bar; end},
|
3076
|
+
%q{~~~~ keyword
|
3077
|
+
| ~~~~ keyword (when)
|
3078
|
+
| ~~~ end
|
3079
|
+
| ~~~~~~~~~~~~~~~ expression (when)
|
3080
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3081
|
+
end
|
3082
|
+
|
3083
|
+
def test_case_expr_else
|
3084
|
+
assert_parses(
|
3085
|
+
s(:case, s(:lvar, :foo),
|
3086
|
+
s(:when, s(:str, 'bar'),
|
3087
|
+
s(:lvar, :bar)),
|
3088
|
+
s(:lvar, :baz)),
|
3089
|
+
%q{case foo; when 'bar'; bar; else baz; end},
|
3090
|
+
%q{~~~~ keyword
|
3091
|
+
| ~~~~ keyword (when)
|
3092
|
+
| ~~~~ else
|
3093
|
+
| ~~~ end
|
3094
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3095
|
+
end
|
3096
|
+
|
3097
|
+
def test_case_cond
|
3098
|
+
assert_parses(
|
3099
|
+
s(:case, nil,
|
3100
|
+
s(:when, s(:lvar, :foo),
|
3101
|
+
s(:str, 'foo')),
|
3102
|
+
nil),
|
3103
|
+
%q{case; when foo; 'foo'; end},
|
3104
|
+
%q{~~~~ keyword
|
3105
|
+
| ~~~~ keyword (when)
|
3106
|
+
| ~~~ end
|
3107
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3108
|
+
end
|
3109
|
+
|
3110
|
+
def test_case_cond_else
|
3111
|
+
assert_parses(
|
3112
|
+
s(:case, nil,
|
3113
|
+
s(:when, s(:lvar, :foo),
|
3114
|
+
s(:str, 'foo')),
|
3115
|
+
s(:str, 'bar')),
|
3116
|
+
%q{case; when foo; 'foo'; else 'bar'; end},
|
3117
|
+
%q{~~~~ keyword
|
3118
|
+
| ~~~~ keyword (when)
|
3119
|
+
| ~~~~ else
|
3120
|
+
| ~~~ end
|
3121
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3122
|
+
end
|
3123
|
+
|
3124
|
+
def test_case_cond_just_else
|
3125
|
+
assert_parses(
|
3126
|
+
s(:case, nil,
|
3127
|
+
s(:str, 'bar')),
|
3128
|
+
%q{case; else 'bar'; end},
|
3129
|
+
%q{~~~~ keyword
|
3130
|
+
| ~~~~ else
|
3131
|
+
| ~~~ end
|
3132
|
+
|~~~~~~~~~~~~~~~~~~~~~ expression},
|
3133
|
+
%w(1.8))
|
3134
|
+
end
|
3135
|
+
|
3136
|
+
def test_when_then
|
3137
|
+
assert_parses(
|
3138
|
+
s(:case, s(:lvar, :foo),
|
3139
|
+
s(:when, s(:str, 'bar'),
|
3140
|
+
s(:lvar, :bar)),
|
3141
|
+
nil),
|
3142
|
+
%q{case foo; when 'bar' then bar; end},
|
3143
|
+
%q{ ~~~~ keyword (when)
|
3144
|
+
| ~~~~ begin (when)
|
3145
|
+
| ~~~~~~~~~~~~~~~~~~~ expression (when)})
|
3146
|
+
end
|
3147
|
+
|
3148
|
+
def test_when_multi
|
3149
|
+
assert_parses(
|
3150
|
+
s(:case, s(:lvar, :foo),
|
3151
|
+
s(:when, s(:str, 'bar'), s(:str, 'baz'),
|
3152
|
+
s(:lvar, :bar)),
|
3153
|
+
nil),
|
3154
|
+
%q{case foo; when 'bar', 'baz'; bar; end})
|
3155
|
+
end
|
3156
|
+
|
3157
|
+
def test_when_splat
|
3158
|
+
assert_parses(
|
3159
|
+
s(:case, s(:lvar, :foo),
|
3160
|
+
s(:when,
|
3161
|
+
s(:int, 1),
|
3162
|
+
s(:splat, s(:lvar, :baz)),
|
3163
|
+
s(:lvar, :bar)),
|
3164
|
+
s(:when,
|
3165
|
+
s(:splat, s(:lvar, :foo)),
|
3166
|
+
s(:nil)),
|
3167
|
+
nil),
|
3168
|
+
%q{case foo; when 1, *baz; bar; when *foo; end},
|
3169
|
+
%q{ ^ operator (when/1.splat)
|
3170
|
+
| ~~~~ expression (when/1.splat)
|
3171
|
+
| ^ operator (when/2.splat)
|
3172
|
+
| ~~~~ expression (when/2.splat)})
|
3173
|
+
end
|
3174
|
+
|
3175
|
+
# Looping
|
3176
|
+
|
3177
|
+
def test_while
|
3178
|
+
assert_parses(
|
3179
|
+
s(:while, s(:lvar, :foo), s(:send, nil, :meth)),
|
3180
|
+
%q{while foo do meth end},
|
3181
|
+
%q{~~~~~ keyword
|
3182
|
+
| ~~ begin
|
3183
|
+
| ~~~ end
|
3184
|
+
|~~~~~~~~~~~~~~~~~~~~~ expression})
|
3185
|
+
|
3186
|
+
assert_parses(
|
3187
|
+
s(:while, s(:lvar, :foo), s(:send, nil, :meth)),
|
3188
|
+
%q{while foo; meth end},
|
3189
|
+
%q{~~~~~ keyword
|
3190
|
+
| ~~~ end
|
3191
|
+
|~~~~~~~~~~~~~~~~~~~ expression})
|
3192
|
+
end
|
3193
|
+
|
3194
|
+
def test_while_mod
|
3195
|
+
assert_parses(
|
3196
|
+
s(:while, s(:lvar, :foo), s(:send, nil, :meth)),
|
3197
|
+
%q{meth while foo},
|
3198
|
+
%q{ ~~~~~ keyword})
|
3199
|
+
end
|
3200
|
+
|
3201
|
+
def test_until
|
3202
|
+
assert_parses(
|
3203
|
+
s(:until, s(:lvar, :foo), s(:send, nil, :meth)),
|
3204
|
+
%q{until foo do meth end},
|
3205
|
+
%q{~~~~~ keyword
|
3206
|
+
| ~~ begin
|
3207
|
+
| ~~~ end})
|
3208
|
+
|
3209
|
+
assert_parses(
|
3210
|
+
s(:until, s(:lvar, :foo), s(:send, nil, :meth)),
|
3211
|
+
%q{until foo; meth end},
|
3212
|
+
%q{~~~~~ keyword
|
3213
|
+
| ~~~ end})
|
3214
|
+
end
|
3215
|
+
|
3216
|
+
def test_until_mod
|
3217
|
+
assert_parses(
|
3218
|
+
s(:until, s(:lvar, :foo), s(:send, nil, :meth)),
|
3219
|
+
%q{meth until foo},
|
3220
|
+
%q{ ~~~~~ keyword})
|
3221
|
+
end
|
3222
|
+
|
3223
|
+
# TODO implement this
|
3224
|
+
# def test_while_post
|
3225
|
+
# assert_parses(
|
3226
|
+
# s(:while_post, s(:lvar, :foo), s(:send, nil, :meth)),
|
3227
|
+
# %q{begin meth end while foo},
|
3228
|
+
# %q{ ~~~~~ keyword})
|
3229
|
+
# end
|
3230
|
+
|
3231
|
+
# TODO implement this
|
3232
|
+
# def test_until_post
|
3233
|
+
# assert_parses(
|
3234
|
+
# s(:until_post, s(:lvar, :foo), s(:send, nil, :meth)),
|
3235
|
+
# %q{begin meth end until foo},
|
3236
|
+
# %q{ ~~~~~ keyword})
|
3237
|
+
# end
|
3238
|
+
|
3239
|
+
def test_while_masgn
|
3240
|
+
assert_diagnoses(
|
3241
|
+
[:error, :masgn_as_condition],
|
3242
|
+
%q{while (a, b = foo); end},
|
3243
|
+
%q{ ~~~~~~~~~~~~ location})
|
3244
|
+
end
|
3245
|
+
|
3246
|
+
def test_while_mod_masgn
|
3247
|
+
assert_diagnoses(
|
3248
|
+
[:error, :masgn_as_condition],
|
3249
|
+
%q{foo while (a, b = foo)},
|
3250
|
+
%q{ ~~~~~~~~~~~~ location})
|
3251
|
+
end
|
3252
|
+
|
3253
|
+
def test_for
|
3254
|
+
assert_parses(
|
3255
|
+
s(:for,
|
3256
|
+
s(:lvasgn, :a),
|
3257
|
+
s(:lvar, :foo),
|
3258
|
+
s(:send, nil, :p, s(:lvar, :a))),
|
3259
|
+
%q{for a in foo do p a; end},
|
3260
|
+
%q{~~~ keyword
|
3261
|
+
| ~~ in
|
3262
|
+
| ~~ begin
|
3263
|
+
| ~~~ end
|
3264
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3265
|
+
|
3266
|
+
assert_parses(
|
3267
|
+
s(:for,
|
3268
|
+
s(:lvasgn, :a),
|
3269
|
+
s(:lvar, :foo),
|
3270
|
+
s(:send, nil, :p, s(:lvar, :a))),
|
3271
|
+
%q{for a in foo; p a; end})
|
3272
|
+
end
|
3273
|
+
|
3274
|
+
def test_for_mlhs
|
3275
|
+
assert_parses(
|
3276
|
+
s(:for,
|
3277
|
+
s(:mlhs,
|
3278
|
+
s(:lvasgn, :a),
|
3279
|
+
s(:lvasgn, :b)),
|
3280
|
+
s(:lvar, :foo),
|
3281
|
+
s(:send, nil, :p, s(:lvar, :a), s(:lvar, :b))),
|
3282
|
+
%q{for a, b in foo; p a, b; end},
|
3283
|
+
%q{ ~~~~ expression (mlhs)})
|
3284
|
+
end
|
3285
|
+
|
3286
|
+
# Control flow commands
|
3287
|
+
|
3288
|
+
def test_break
|
3289
|
+
assert_parses(
|
3290
|
+
s(:break, s(:lvar, :foo)),
|
3291
|
+
%q{break(foo)},
|
3292
|
+
%q{~~~~~ keyword
|
3293
|
+
| ^ begin
|
3294
|
+
| ^ end
|
3295
|
+
|~~~~~~~~~~ expression},
|
3296
|
+
ALL_VERSIONS - %w(1.8))
|
3297
|
+
|
3298
|
+
assert_parses(
|
3299
|
+
s(:break, s(:lvar, :foo)),
|
3300
|
+
%q{break(foo)},
|
3301
|
+
%q{~~~~~ keyword
|
3302
|
+
|~~~~~~~~~~ expression},
|
3303
|
+
%w(1.8))
|
3304
|
+
|
3305
|
+
assert_parses(
|
3306
|
+
s(:break, s(:lvar, :foo)),
|
3307
|
+
%q{break foo},
|
3308
|
+
%q{~~~~~ keyword
|
3309
|
+
|~~~~~~~~~ expression})
|
3310
|
+
|
3311
|
+
assert_parses(
|
3312
|
+
s(:break, s(:nil)),
|
3313
|
+
%q{break()},
|
3314
|
+
%q{~~~~~ keyword
|
3315
|
+
|~~~~~~~ expression},
|
3316
|
+
ALL_VERSIONS - %w(1.8))
|
3317
|
+
|
3318
|
+
assert_parses(
|
3319
|
+
s(:break),
|
3320
|
+
%q{break},
|
3321
|
+
%q{~~~~~ keyword
|
3322
|
+
|~~~~~ expression})
|
3323
|
+
end
|
3324
|
+
|
3325
|
+
def test_return
|
3326
|
+
assert_parses(
|
3327
|
+
s(:return, s(:lvar, :foo)),
|
3328
|
+
%q{return(foo)},
|
3329
|
+
%q{~~~~~~ keyword
|
3330
|
+
| ^ begin
|
3331
|
+
| ^ end
|
3332
|
+
|~~~~~~~~~~~ expression},
|
3333
|
+
ALL_VERSIONS - %w(1.8))
|
3334
|
+
|
3335
|
+
assert_parses(
|
3336
|
+
s(:return, s(:lvar, :foo)),
|
3337
|
+
%q{return(foo)},
|
3338
|
+
%q{~~~~~~ keyword
|
3339
|
+
|~~~~~~~~~~~ expression},
|
3340
|
+
%w(1.8))
|
3341
|
+
|
3342
|
+
assert_parses(
|
3343
|
+
s(:return, s(:lvar, :foo)),
|
3344
|
+
%q{return foo},
|
3345
|
+
%q{~~~~~~ keyword
|
3346
|
+
|~~~~~~~~~~ expression})
|
3347
|
+
|
3348
|
+
assert_parses(
|
3349
|
+
s(:return, s(:nil)),
|
3350
|
+
%q{return()},
|
3351
|
+
%q{~~~~~~ keyword
|
3352
|
+
|~~~~~~~~ expression},
|
3353
|
+
ALL_VERSIONS - %w(1.8))
|
3354
|
+
|
3355
|
+
assert_parses(
|
3356
|
+
s(:return),
|
3357
|
+
%q{return},
|
3358
|
+
%q{~~~~~~ keyword
|
3359
|
+
|~~~~~~ expression})
|
3360
|
+
end
|
3361
|
+
|
3362
|
+
def test_next
|
3363
|
+
assert_parses(
|
3364
|
+
s(:next, s(:lvar, :foo)),
|
3365
|
+
%q{next(foo)},
|
3366
|
+
%q{~~~~ keyword
|
3367
|
+
| ^ begin
|
3368
|
+
| ^ end
|
3369
|
+
|~~~~~~~~~ expression},
|
3370
|
+
ALL_VERSIONS - %w(1.8))
|
3371
|
+
|
3372
|
+
assert_parses(
|
3373
|
+
s(:next, s(:lvar, :foo)),
|
3374
|
+
%q{next(foo)},
|
3375
|
+
%q{~~~~ keyword
|
3376
|
+
|~~~~~~~~~ expression},
|
3377
|
+
%w(1.8))
|
3378
|
+
|
3379
|
+
assert_parses(
|
3380
|
+
s(:next, s(:lvar, :foo)),
|
3381
|
+
%q{next foo},
|
3382
|
+
%q{~~~~ keyword
|
3383
|
+
|~~~~~~~~ expression})
|
3384
|
+
|
3385
|
+
assert_parses(
|
3386
|
+
s(:next, s(:nil)),
|
3387
|
+
%q{next()},
|
3388
|
+
%q{~~~~ keyword
|
3389
|
+
|~~~~~~ expression},
|
3390
|
+
ALL_VERSIONS - %w(1.8))
|
3391
|
+
|
3392
|
+
assert_parses(
|
3393
|
+
s(:next),
|
3394
|
+
%q{next},
|
3395
|
+
%q{~~~~ keyword
|
3396
|
+
|~~~~ expression})
|
3397
|
+
end
|
3398
|
+
|
3399
|
+
def test_redo
|
3400
|
+
assert_parses(
|
3401
|
+
s(:redo),
|
3402
|
+
%q{redo},
|
3403
|
+
%q{~~~~ keyword
|
3404
|
+
|~~~~ expression})
|
3405
|
+
end
|
3406
|
+
|
3407
|
+
# Exception handling
|
3408
|
+
|
3409
|
+
def test_rescue
|
3410
|
+
assert_parses(
|
3411
|
+
s(:rescue, s(:send, nil, :meth),
|
3412
|
+
s(:resbody, nil, nil, s(:lvar, :foo)),
|
3413
|
+
nil),
|
3414
|
+
%q{begin; meth; rescue; foo; end},
|
3415
|
+
%q{~~~~~ begin
|
3416
|
+
| ~~~ end
|
3417
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3418
|
+
end
|
3419
|
+
|
3420
|
+
def test_rescue_else
|
3421
|
+
assert_parses(
|
3422
|
+
s(:rescue, s(:send, nil, :meth),
|
3423
|
+
s(:resbody, nil, nil, s(:lvar, :foo)),
|
3424
|
+
s(:lvar, :bar)),
|
3425
|
+
%q{begin; meth; rescue; foo; else; bar; end},
|
3426
|
+
%q{~~~~~ begin
|
3427
|
+
| ~~~~ else
|
3428
|
+
| ~~~ end
|
3429
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3430
|
+
end
|
3431
|
+
|
3432
|
+
def test_rescue_else_useless
|
3433
|
+
assert_diagnoses(
|
3434
|
+
[:warning, :useless_else],
|
3435
|
+
%q{begin; 1; else; 2; end},
|
3436
|
+
%q{ ~~~~ location})
|
3437
|
+
end
|
3438
|
+
|
3439
|
+
def test_ensure
|
3440
|
+
assert_parses(
|
3441
|
+
s(:ensure, s(:send, nil, :meth),
|
3442
|
+
s(:lvar, :bar)),
|
3443
|
+
%q{begin; meth; ensure; bar; end},
|
3444
|
+
%q{~~~~~ begin
|
3445
|
+
| ~~~~~~ keyword
|
3446
|
+
| ~~~ end
|
3447
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression})
|
3448
|
+
end
|
3449
|
+
|
3450
|
+
def test_rescue_ensure
|
3451
|
+
assert_parses(
|
3452
|
+
s(:ensure,
|
3453
|
+
s(:rescue,
|
3454
|
+
s(:send, nil, :meth),
|
3455
|
+
s(:resbody, nil, nil, s(:lvar, :baz)),
|
3456
|
+
nil),
|
3457
|
+
s(:lvar, :bar)),
|
3458
|
+
%q{begin; meth; rescue; baz; ensure; bar; end},
|
3459
|
+
%q{~~~~~ begin
|
3460
|
+
|~~~~~ begin (rescue)
|
3461
|
+
| ~~~~~~ keyword
|
3462
|
+
| ~~~~~~ keyword (rescue)
|
3463
|
+
| ~~~ end
|
3464
|
+
| ~~~ end (rescue)
|
3465
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
|
3466
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (rescue)})
|
3467
|
+
end
|
3468
|
+
|
3469
|
+
def test_rescue_else_ensure
|
3470
|
+
assert_parses(
|
3471
|
+
s(:ensure,
|
3472
|
+
s(:rescue,
|
3473
|
+
s(:send, nil, :meth),
|
3474
|
+
s(:resbody, nil, nil, s(:lvar, :baz)),
|
3475
|
+
s(:lvar, :foo)),
|
3476
|
+
s(:lvar, :bar)),
|
3477
|
+
%q{begin; meth; rescue; baz; else foo; ensure; bar end},
|
3478
|
+
%q{~~~~~ begin
|
3479
|
+
|~~~~~ begin (rescue)
|
3480
|
+
| ~~~~~~ keyword
|
3481
|
+
| ~~~~~~ keyword (rescue)
|
3482
|
+
| ~~~~ else (rescue)
|
3483
|
+
| ~~~ end
|
3484
|
+
| ~~~ end (rescue)
|
3485
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression
|
3486
|
+
|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ expression (rescue)})
|
3487
|
+
end
|
3488
|
+
|
3489
|
+
def test_rescue_mod
|
3490
|
+
assert_parses(
|
3491
|
+
s(:rescue,
|
3492
|
+
s(:send, nil, :meth),
|
3493
|
+
s(:resbody, nil, nil, s(:lvar, :bar)),
|
3494
|
+
nil),
|
3495
|
+
%q{meth rescue bar},
|
3496
|
+
%q{ ~~~~~~ keyword (resbody)
|
3497
|
+
|~~~~~~~~~~~~~~~ expression})
|
3498
|
+
end
|
3499
|
+
|
3500
|
+
def test_rescue_mod_asgn
|
3501
|
+
assert_parses(
|
3502
|
+
s(:lvasgn, :foo,
|
3503
|
+
s(:rescue,
|
3504
|
+
s(:send, nil, :meth),
|
3505
|
+
s(:resbody, nil, nil, s(:lvar, :bar)),
|
3506
|
+
nil)),
|
3507
|
+
%q{foo = meth rescue bar},
|
3508
|
+
%q{ ~~~~~~ keyword (rescue.resbody)
|
3509
|
+
| ~~~~~~~~~~~~~~~ expression (rescue)
|
3510
|
+
|~~~~~~~~~~~~~~~~~~~~~ expression})
|
3511
|
+
end
|
3512
|
+
|
3513
|
+
def test_rescue_mod_op_assign
|
3514
|
+
assert_parses(
|
3515
|
+
s(:op_asgn,
|
3516
|
+
s(:lvasgn, :foo), :+,
|
3517
|
+
s(:rescue,
|
3518
|
+
s(:send, nil, :meth),
|
3519
|
+
s(:resbody, nil, nil, s(:lvar, :bar)),
|
3520
|
+
nil)),
|
3521
|
+
%q{foo += meth rescue bar},
|
3522
|
+
%q{ ~~~~~~ keyword (rescue.resbody)
|
3523
|
+
| ~~~~~~~~~~~~~~~ expression (rescue)
|
3524
|
+
|~~~~~~~~~~~~~~~~~~~~~~ expression},
|
3525
|
+
ALL_VERSIONS - %w(1.8))
|
3526
|
+
end
|
3527
|
+
|
3528
|
+
def test_resbody_list
|
3529
|
+
assert_parses(
|
3530
|
+
s(:rescue,
|
3531
|
+
s(:send, nil, :meth),
|
3532
|
+
s(:resbody,
|
3533
|
+
s(:array, s(:const, nil, :Exception)),
|
3534
|
+
nil,
|
3535
|
+
s(:lvar, :bar)),
|
3536
|
+
nil),
|
3537
|
+
%q{begin; meth; rescue Exception; bar; end})
|
3538
|
+
end
|
3539
|
+
|
3540
|
+
def test_resbody_list_mrhs
|
3541
|
+
assert_parses(
|
3542
|
+
s(:rescue,
|
3543
|
+
s(:send, nil, :meth),
|
3544
|
+
s(:resbody,
|
3545
|
+
s(:array,
|
3546
|
+
s(:const, nil, :Exception),
|
3547
|
+
s(:lvar, :foo)),
|
3548
|
+
nil,
|
3549
|
+
s(:lvar, :bar)),
|
3550
|
+
nil),
|
3551
|
+
%q{begin; meth; rescue Exception, foo; bar; end})
|
3552
|
+
end
|
3553
|
+
|
3554
|
+
def test_resbody_var
|
3555
|
+
assert_parses(
|
3556
|
+
s(:rescue,
|
3557
|
+
s(:send, nil, :meth),
|
3558
|
+
s(:resbody, nil, s(:lvasgn, :ex), s(:lvar, :bar)),
|
3559
|
+
nil),
|
3560
|
+
%q{begin; meth; rescue => ex; bar; end})
|
3561
|
+
|
3562
|
+
assert_parses(
|
3563
|
+
s(:rescue,
|
3564
|
+
s(:send, nil, :meth),
|
3565
|
+
s(:resbody, nil, s(:ivasgn, :@ex), s(:lvar, :bar)),
|
3566
|
+
nil),
|
3567
|
+
%q{begin; meth; rescue => @ex; bar; end})
|
3568
|
+
end
|
3569
|
+
|
3570
|
+
def test_resbody_list_var
|
3571
|
+
assert_parses(
|
3572
|
+
s(:rescue,
|
3573
|
+
s(:send, nil, :meth),
|
3574
|
+
s(:resbody,
|
3575
|
+
s(:array, s(:lvar, :foo)),
|
3576
|
+
s(:lvasgn, :ex),
|
3577
|
+
s(:lvar, :bar)),
|
3578
|
+
nil),
|
3579
|
+
%q{begin; meth; rescue foo => ex; bar; end})
|
3580
|
+
end
|
3581
|
+
|
3582
|
+
def test_retry
|
3583
|
+
assert_parses(
|
3584
|
+
s(:retry),
|
3585
|
+
%q{retry},
|
3586
|
+
%q{~~~~~ keyword
|
3587
|
+
|~~~~~ expression})
|
3588
|
+
end
|
3589
|
+
|
3590
|
+
# BEGIN and END
|
3591
|
+
|
3592
|
+
def test_preexe
|
3593
|
+
assert_parses(
|
3594
|
+
s(:preexe, s(:int, 1)),
|
3595
|
+
%q{BEGIN { 1 }},
|
3596
|
+
%q{~~~~~ keyword
|
3597
|
+
| ^ begin
|
3598
|
+
| ^ end})
|
3599
|
+
end
|
3600
|
+
|
3601
|
+
def test_preexe_invalid
|
3602
|
+
assert_diagnoses(
|
3603
|
+
[:error, :begin_in_method],
|
3604
|
+
%q{def f; BEGIN{}; end},
|
3605
|
+
%q{ ~~~~~ location},
|
3606
|
+
# Yes. *Exclude 1.9*. Sigh.
|
3607
|
+
ALL_VERSIONS - %w(1.9))
|
3608
|
+
end
|
3609
|
+
|
3610
|
+
def test_postexe
|
3611
|
+
assert_parses(
|
3612
|
+
s(:postexe, s(:int, 1)),
|
3613
|
+
%q{END { 1 }},
|
3614
|
+
%q{~~~ keyword
|
3615
|
+
| ^ begin
|
3616
|
+
| ^ end})
|
3617
|
+
|
3618
|
+
assert_diagnoses(
|
3619
|
+
[:warning, :end_in_method],
|
3620
|
+
%q{def f; END{ 1 }; end},
|
3621
|
+
%q{ ~~~ location})
|
3622
|
+
end
|
3623
|
+
|
3624
|
+
#
|
3625
|
+
# Error recovery
|
3626
|
+
#
|
3627
|
+
|
3628
|
+
def test_unknown_percent_str
|
3629
|
+
assert_diagnoses(
|
3630
|
+
[:error, :unexpected_percent_str, { :type => '%k' }],
|
3631
|
+
%q{%k[foo]},
|
3632
|
+
%q{~~ location})
|
3633
|
+
end
|
3634
|
+
|
3635
|
+
def test_on_error
|
3636
|
+
assert_diagnoses(
|
3637
|
+
[:error, :unexpected_token, { :token => 'tIDENTIFIER' }],
|
3638
|
+
%q{def foo(bar baz); end},
|
3639
|
+
%q{ ~~~ location})
|
3640
|
+
end
|
3641
|
+
|
3642
|
+
#
|
3643
|
+
# Bug-specific tests
|
3644
|
+
#
|
3645
|
+
|
3646
|
+
def test_bug_cmd_string_lookahead
|
3647
|
+
assert_parses(
|
3648
|
+
s(:block,
|
3649
|
+
s(:send, nil, :desc,
|
3650
|
+
s(:str, "foo")),
|
3651
|
+
s(:args), s(:nil)),
|
3652
|
+
%q{desc "foo" do end})
|
3653
|
+
end
|
3654
|
+
end
|