ripper_ruby_parser 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +19 -0
  3. data/Rakefile +2 -2
  4. data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +55 -4
  5. data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +20 -13
  6. data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +27 -12
  7. data/lib/ripper_ruby_parser/sexp_handlers/hashes.rb +25 -12
  8. data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +4 -2
  9. data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +19 -15
  10. data/lib/ripper_ruby_parser/sexp_handlers/loops.rb +25 -11
  11. data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +12 -4
  12. data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +8 -4
  13. data/lib/ripper_ruby_parser/sexp_handlers/operators.rb +1 -5
  14. data/lib/ripper_ruby_parser/version.rb +1 -1
  15. data/lib/ripper_ruby_parser.rb +2 -2
  16. data/test/end_to_end/comments_test.rb +4 -4
  17. data/test/end_to_end/comparison_test.rb +15 -15
  18. data/test/end_to_end/error_conditions_test.rb +16 -16
  19. data/test/end_to_end/lib_comparison_test.rb +3 -3
  20. data/test/end_to_end/line_numbering_test.rb +4 -4
  21. data/test/end_to_end/samples_comparison_test.rb +4 -4
  22. data/test/end_to_end/test_comparison_test.rb +3 -3
  23. data/test/pt_testcase/pt_test.rb +4 -4
  24. data/test/test_helper.rb +1 -1
  25. data/test/unit/commenting_ripper_parser_test.rb +33 -33
  26. data/test/unit/parser_assignment_test.rb +30 -30
  27. data/test/unit/parser_blocks_test.rb +83 -65
  28. data/test/unit/parser_conditionals_test.rb +96 -64
  29. data/test/unit/parser_literals_test.rb +308 -212
  30. data/test/unit/parser_loops_test.rb +85 -15
  31. data/test/unit/parser_method_calls_test.rb +100 -41
  32. data/test/unit/parser_operators_test.rb +60 -28
  33. data/test/unit/parser_test.rb +435 -410
  34. data/test/unit/sexp_processor_test.rb +82 -82
  35. data/test/unit/version_test.rb +1 -1
  36. metadata +2 -2
@@ -1,342 +1,352 @@
1
1
  require File.expand_path('../test_helper.rb', File.dirname(__FILE__))
2
2
 
3
3
  describe RipperRubyParser::Parser do
4
- describe "#parse" do
5
- describe "for regexp literals" do
6
- it "works for a simple regex literal" do
7
- "/foo/".
4
+ describe '#parse' do
5
+ describe 'for regexp literals' do
6
+ it 'works for a simple regex literal' do
7
+ '/foo/'.
8
8
  must_be_parsed_as s(:lit, /foo/)
9
9
  end
10
10
 
11
- it "works for regex literals with escaped right bracket" do
11
+ it 'works for regex literals with escaped right bracket' do
12
12
  '/\\)/'.
13
13
  must_be_parsed_as s(:lit, /\)/)
14
14
  end
15
15
 
16
- it "works for regex literals with escape sequences" do
16
+ it 'works for regex literals with escape sequences' do
17
17
  '/\\)\\n\\\\/'.
18
18
  must_be_parsed_as s(:lit, /\)\n\\/)
19
19
  end
20
20
 
21
- it "works for a regex literal with the multiline flag" do
22
- "/foo/m".
21
+ it 'works for a regex literal with the multiline flag' do
22
+ '/foo/m'.
23
23
  must_be_parsed_as s(:lit, /foo/m)
24
24
  end
25
25
 
26
- it "works for a regex literal with the extended flag" do
27
- "/foo/x".
26
+ it 'works for a regex literal with the extended flag' do
27
+ '/foo/x'.
28
28
  must_be_parsed_as s(:lit, /foo/x)
29
29
  end
30
30
 
31
- it "works for a regex literal with the ignorecase flag" do
32
- "/foo/i".
31
+ it 'works for a regex literal with the ignorecase flag' do
32
+ '/foo/i'.
33
33
  must_be_parsed_as s(:lit, /foo/i)
34
34
  end
35
35
 
36
- it "works for a regex literal with a combination of flags" do
37
- "/foo/ixm".
36
+ it 'works for a regex literal with a combination of flags' do
37
+ '/foo/ixm'.
38
38
  must_be_parsed_as s(:lit, /foo/ixm)
39
39
  end
40
40
 
41
- it "works with the no-encoding flag" do
41
+ it 'works with the no-encoding flag' do
42
42
  parser = RipperRubyParser::Parser.new
43
- result = parser.parse "/foo/n"
43
+ result = parser.parse '/foo/n'
44
44
  # Use inspect since regular == finds no difference between /foo/n
45
45
  # and /foo/
46
46
  result.inspect.must_equal s(:lit, /foo/n).inspect
47
47
  end
48
48
 
49
- describe "with interpolations" do
50
- it "works for a simple interpolation" do
49
+ describe 'with interpolations' do
50
+ it 'works for a simple interpolation' do
51
51
  '/foo#{bar}baz/'.
52
52
  must_be_parsed_as s(:dregx,
53
- "foo",
53
+ 'foo',
54
54
  s(:evstr, s(:call, nil, :bar)),
55
- s(:str, "baz"))
55
+ s(:str, 'baz'))
56
56
  end
57
57
 
58
- it "works for a regex literal with flags and interpolation" do
58
+ it 'works for a regex literal with flags and interpolation' do
59
59
  '/foo#{bar}/ixm'.
60
60
  must_be_parsed_as s(:dregx,
61
- "foo",
61
+ 'foo',
62
62
  s(:evstr, s(:call, nil, :bar)),
63
63
  7)
64
64
  end
65
65
 
66
- it "works with the no-encoding flag" do
66
+ it 'works with the no-encoding flag' do
67
67
  '/foo#{bar}/n'.
68
68
  must_be_parsed_as s(:dregx,
69
- "foo",
69
+ 'foo',
70
70
  s(:evstr,
71
71
  s(:call, nil, :bar)), 32)
72
72
  end
73
73
 
74
- it "works with the unicode-encoding flag" do
74
+ it 'works with the unicode-encoding flag' do
75
75
  '/foo#{bar}/u'.
76
76
  must_be_parsed_as s(:dregx,
77
- "foo",
77
+ 'foo',
78
78
  s(:evstr,
79
79
  s(:call, nil, :bar)), 16)
80
80
  end
81
81
 
82
- it "works with the euc-encoding flag" do
82
+ it 'works with the euc-encoding flag' do
83
83
  '/foo#{bar}/e'.
84
84
  must_be_parsed_as s(:dregx,
85
- "foo",
85
+ 'foo',
86
86
  s(:evstr,
87
87
  s(:call, nil, :bar)), 16)
88
88
  end
89
89
 
90
- it "works with the sjis-encoding flag" do
90
+ it 'works with the sjis-encoding flag' do
91
91
  '/foo#{bar}/s'.
92
92
  must_be_parsed_as s(:dregx,
93
- "foo",
93
+ 'foo',
94
94
  s(:evstr,
95
95
  s(:call, nil, :bar)), 16)
96
96
  end
97
97
 
98
- it "works for a regex literal with interpolate-once flag" do
98
+ it 'works for a regex literal with interpolate-once flag' do
99
99
  '/foo#{bar}/o'.
100
100
  must_be_parsed_as s(:dregx_once,
101
- "foo",
101
+ 'foo',
102
102
  s(:evstr, s(:call, nil, :bar)))
103
103
  end
104
104
 
105
- it "works with an empty interpolation" do
105
+ it 'works with an empty interpolation' do
106
106
  '/foo#{}bar/'.
107
107
  must_be_parsed_as s(:dregx,
108
- "foo",
108
+ 'foo',
109
109
  s(:evstr),
110
- s(:str, "bar"))
110
+ s(:str, 'bar'))
111
111
  end
112
112
 
113
- describe "containing just a literal string" do
114
- it "performs the interpolation when it is at the end" do
113
+ describe 'containing just a literal string' do
114
+ it 'performs the interpolation when it is at the end' do
115
115
  '/foo#{"bar"}/'.must_be_parsed_as s(:lit, /foobar/)
116
116
  end
117
117
 
118
- it "performs the interpolation when it is in the middle" do
118
+ it 'performs the interpolation when it is in the middle' do
119
119
  '/foo#{"bar"}baz/'.must_be_parsed_as s(:lit, /foobarbaz/)
120
120
  end
121
121
 
122
- it "performs the interpolation when it is at the start" do
122
+ it 'performs the interpolation when it is at the start' do
123
123
  '/#{"foo"}bar/'.must_be_parsed_as s(:lit, /foobar/)
124
124
  end
125
125
  end
126
126
  end
127
127
  end
128
128
 
129
- describe "for string literals" do
130
- it "works for empty strings" do
129
+ describe 'for string literals' do
130
+ it 'works for empty strings' do
131
131
  "''".
132
- must_be_parsed_as s(:str, "")
132
+ must_be_parsed_as s(:str, '')
133
133
  end
134
134
 
135
- it "sets the encoding for literal strings to utf8 even if ascii would do" do
135
+ it 'sets the encoding for literal strings to utf8 even if ascii would do' do
136
136
  parser = RipperRubyParser::Parser.new
137
- result = parser.parse "\"foo\""
138
- result.must_equal s(:str, "foo")
139
- result[1].encoding.to_s.must_equal "UTF-8"
137
+ result = parser.parse '"foo"'
138
+ result.must_equal s(:str, 'foo')
139
+ result[1].encoding.to_s.must_equal 'UTF-8'
140
140
  end
141
141
 
142
- describe "with double-quoted strings with escape sequences" do
143
- it "works for strings with escape sequences" do
144
- "\"\\n\"".
142
+ describe 'with double-quoted strings with escape sequences' do
143
+ it 'works for strings with escape sequences' do
144
+ '"\\n"'.
145
145
  must_be_parsed_as s(:str, "\n")
146
146
  end
147
147
 
148
- it "works for strings with useless escape sequences" do
149
- "\"F\\OO\"".
150
- must_be_parsed_as s(:str, "FOO")
148
+ it 'works for strings with useless escape sequences' do
149
+ '"F\\OO"'.
150
+ must_be_parsed_as s(:str, 'FOO')
151
151
  end
152
152
 
153
- it "works for strings with escaped backslashes" do
154
- "\"\\\\n\"".
155
- must_be_parsed_as s(:str, "\\n")
153
+ it 'works for strings with escaped backslashes' do
154
+ '"\\\\n"'.
155
+ must_be_parsed_as s(:str, '\\n')
156
156
  end
157
157
 
158
- it "works for a representation of a regex literal with escaped right bracket" do
159
- "\"/\\\\)/\"".
160
- must_be_parsed_as s(:str, "/\\)/")
158
+ it 'works for a representation of a regex literal with escaped right bracket' do
159
+ '"/\\\\)/"'.
160
+ must_be_parsed_as s(:str, '/\\)/')
161
161
  end
162
162
 
163
- it "works for a uselessly escaped right bracket" do
164
- "\"/\\)/\"".
165
- must_be_parsed_as s(:str, "/)/")
163
+ it 'works for a uselessly escaped right bracket' do
164
+ '"/\\)/"'.
165
+ must_be_parsed_as s(:str, '/)/')
166
166
  end
167
167
 
168
- it "works for a string containing escaped quotes" do
169
- "\"\\\"\"".
170
- must_be_parsed_as s(:str, "\"")
168
+ it 'works for a string containing escaped quotes' do
169
+ '"\\""'.
170
+ must_be_parsed_as s(:str, '"')
171
171
  end
172
172
 
173
- it "works with hex escapes" do
174
- "\"\\x36\"".must_be_parsed_as s(:str, "6")
175
- "\"\\x4a\"".must_be_parsed_as s(:str, "J")
176
- "\"\\x4A\"".must_be_parsed_as s(:str, "J")
177
- "\"\\x3Z\"".must_be_parsed_as s(:str, "\x03Z")
173
+ it 'works with hex escapes' do
174
+ '"\\x36"'.must_be_parsed_as s(:str, '6')
175
+ '"\\x4a"'.must_be_parsed_as s(:str, 'J')
176
+ '"\\x4A"'.must_be_parsed_as s(:str, 'J')
177
+ '"\\x3Z"'.must_be_parsed_as s(:str, "\x03Z")
178
178
  end
179
179
 
180
- it "works with single-letter escapes" do
181
- "\"foo\\abar\"".must_be_parsed_as s(:str, "foo\abar")
182
- "\"foo\\bbar\"".must_be_parsed_as s(:str, "foo\bbar")
183
- "\"foo\\ebar\"".must_be_parsed_as s(:str, "foo\ebar")
184
- "\"foo\\fbar\"".must_be_parsed_as s(:str, "foo\fbar")
185
- "\"foo\\nbar\"".must_be_parsed_as s(:str, "foo\nbar")
186
- "\"foo\\rbar\"".must_be_parsed_as s(:str, "foo\rbar")
187
- "\"foo\\sbar\"".must_be_parsed_as s(:str, "foo\sbar")
188
- "\"foo\\tbar\"".must_be_parsed_as s(:str, "foo\tbar")
189
- "\"foo\\vbar\"".must_be_parsed_as s(:str, "foo\vbar")
180
+ it 'works with single-letter escapes' do
181
+ '"foo\\abar"'.must_be_parsed_as s(:str, "foo\abar")
182
+ '"foo\\bbar"'.must_be_parsed_as s(:str, "foo\bbar")
183
+ '"foo\\ebar"'.must_be_parsed_as s(:str, "foo\ebar")
184
+ '"foo\\fbar"'.must_be_parsed_as s(:str, "foo\fbar")
185
+ '"foo\\nbar"'.must_be_parsed_as s(:str, "foo\nbar")
186
+ '"foo\\rbar"'.must_be_parsed_as s(:str, "foo\rbar")
187
+ '"foo\\sbar"'.must_be_parsed_as s(:str, "foo\sbar")
188
+ '"foo\\tbar"'.must_be_parsed_as s(:str, "foo\tbar")
189
+ '"foo\\vbar"'.must_be_parsed_as s(:str, "foo\vbar")
190
190
  end
191
191
 
192
- it "works with octal number escapes" do
193
- "\"foo\\123bar\"".must_be_parsed_as s(:str, "foo\123bar")
194
- "\"foo\\23bar\"".must_be_parsed_as s(:str, "foo\023bar")
195
- "\"foo\\3bar\"".must_be_parsed_as s(:str, "foo\003bar")
192
+ it 'works with octal number escapes' do
193
+ '"foo\\123bar"'.must_be_parsed_as s(:str, "foo\123bar")
194
+ '"foo\\23bar"'.must_be_parsed_as s(:str, "foo\023bar")
195
+ '"foo\\3bar"'.must_be_parsed_as s(:str, "foo\003bar")
196
196
 
197
- "\"foo\\118bar\"".must_be_parsed_as s(:str, "foo\0118bar")
198
- "\"foo\\18bar\"".must_be_parsed_as s(:str, "foo\0018bar")
197
+ '"foo\\118bar"'.must_be_parsed_as s(:str, "foo\0118bar")
198
+ '"foo\\18bar"'.must_be_parsed_as s(:str, "foo\0018bar")
199
199
  end
200
200
 
201
- it "works with simple short hand control sequence escapes" do
202
- "\"foo\\cabar\"".must_be_parsed_as s(:str, "foo\cabar")
203
- "\"foo\\cZbar\"".must_be_parsed_as s(:str, "foo\cZbar")
201
+ it 'works with simple short hand control sequence escapes' do
202
+ '"foo\\cabar"'.must_be_parsed_as s(:str, "foo\cabar")
203
+ '"foo\\cZbar"'.must_be_parsed_as s(:str, "foo\cZbar")
204
204
  end
205
205
 
206
- it "works with simple regular control sequence escapes" do
207
- "\"foo\\C-abar\"".must_be_parsed_as s(:str, "foo\C-abar")
208
- "\"foo\\C-Zbar\"".must_be_parsed_as s(:str, "foo\C-Zbar")
206
+ it 'works with simple regular control sequence escapes' do
207
+ '"foo\\C-abar"'.must_be_parsed_as s(:str, "foo\C-abar")
208
+ '"foo\\C-Zbar"'.must_be_parsed_as s(:str, "foo\C-Zbar")
209
209
  end
210
210
 
211
211
  # TODO: Implement remaining escape sequence cases.
212
212
 
213
213
  # TODO: Behave differently in extra_compatible mode.
214
- it "works with unicode escapes (unlike RubyParser)" do
215
- "\"foo\\u273bbar\"".must_be_parsed_as s(:str, "foo✻bar")
214
+ it 'works with unicode escapes (unlike RubyParser)' do
215
+ '"foo\\u273bbar"'.must_be_parsed_as s(:str, 'foo✻bar')
216
216
  end
217
217
  end
218
218
 
219
- describe "with interpolations" do
220
- describe "containing just a literal string" do
221
- it "performs the interpolation when it is at the end" do
222
- '"foo#{"bar"}"'.must_be_parsed_as s(:str, "foobar")
219
+ describe 'with interpolations' do
220
+ describe 'containing just a literal string' do
221
+ it 'performs the interpolation when it is at the end' do
222
+ '"foo#{"bar"}"'.must_be_parsed_as s(:str, 'foobar')
223
223
  end
224
224
 
225
- it "performs the interpolation when it is in the middle" do
226
- '"foo#{"bar"}baz"'.must_be_parsed_as s(:str, "foobarbaz")
225
+ it 'performs the interpolation when it is in the middle' do
226
+ '"foo#{"bar"}baz"'.must_be_parsed_as s(:str, 'foobarbaz')
227
227
  end
228
228
 
229
- it "performs the interpolation when it is at the start" do
230
- '"#{"foo"}bar"'.must_be_parsed_as s(:str, "foobar")
229
+ it 'performs the interpolation when it is at the start' do
230
+ '"#{"foo"}bar"'.must_be_parsed_as s(:str, 'foobar')
231
231
  end
232
232
  end
233
233
 
234
- describe "without braces" do
235
- it "works for ivars" do
234
+ describe 'without braces' do
235
+ it 'works for ivars' do
236
236
  "\"foo\#@bar\"".must_be_parsed_as s(:dstr,
237
- "foo",
237
+ 'foo',
238
238
  s(:evstr, s(:ivar, :@bar)))
239
239
  end
240
240
 
241
- it "works for gvars" do
241
+ it 'works for gvars' do
242
242
  "\"foo\#$bar\"".must_be_parsed_as s(:dstr,
243
- "foo",
243
+ 'foo',
244
244
  s(:evstr, s(:gvar, :$bar)))
245
245
  end
246
246
 
247
- it "works for cvars" do
247
+ it 'works for cvars' do
248
248
  "\"foo\#@@bar\"".must_be_parsed_as s(:dstr,
249
- "foo",
249
+ 'foo',
250
250
  s(:evstr, s(:cvar, :@@bar)))
251
251
  end
252
252
  end
253
253
 
254
- describe "with braces" do
255
- it "works for trivial interpolated strings" do
254
+ describe 'with braces' do
255
+ it 'works for trivial interpolated strings' do
256
256
  '"#{foo}"'.
257
257
  must_be_parsed_as s(:dstr,
258
- "",
258
+ '',
259
259
  s(:evstr,
260
260
  s(:call, nil, :foo)))
261
261
  end
262
262
 
263
- it "works for basic interpolated strings" do
263
+ it 'works for basic interpolated strings' do
264
264
  '"foo#{bar}"'.
265
265
  must_be_parsed_as s(:dstr,
266
- "foo",
266
+ 'foo',
267
267
  s(:evstr,
268
268
  s(:call, nil, :bar)))
269
269
  end
270
270
 
271
- it "works for strings with several interpolations" do
271
+ it 'works for strings with several interpolations' do
272
272
  '"foo#{bar}baz#{qux}"'.
273
273
  must_be_parsed_as s(:dstr,
274
- "foo",
274
+ 'foo',
275
275
  s(:evstr, s(:call, nil, :bar)),
276
- s(:str, "baz"),
276
+ s(:str, 'baz'),
277
277
  s(:evstr, s(:call, nil, :qux)))
278
278
  end
279
279
 
280
- it "correctly handles two interpolations in a row" do
280
+ it 'correctly handles two interpolations in a row' do
281
281
  "\"\#{bar}\#{qux}\"".
282
282
  must_be_parsed_as s(:dstr,
283
- "",
283
+ '',
284
284
  s(:evstr, s(:call, nil, :bar)),
285
285
  s(:evstr, s(:call, nil, :qux)))
286
286
  end
287
287
 
288
- it "works for strings with interpolations followed by escape sequences" do
288
+ it 'works for strings with interpolations followed by escape sequences' do
289
289
  '"#{foo}\\n"'.
290
290
  must_be_parsed_as s(:dstr,
291
- "",
291
+ '',
292
292
  s(:evstr, s(:call, nil, :foo)),
293
293
  s(:str, "\n"))
294
294
  end
295
295
 
296
- it "works with an empty interpolation" do
296
+ it 'works with an empty interpolation' do
297
297
  "\"foo\#{}bar\"".
298
298
  must_be_parsed_as s(:dstr,
299
- "foo",
299
+ 'foo',
300
300
  s(:evstr),
301
- s(:str, "bar"))
301
+ s(:str, 'bar'))
302
302
  end
303
303
  end
304
304
  end
305
305
 
306
- describe "with string concatenation" do
307
- it "performs the concatenation in the case of two simple literal strings" do
308
- "\"foo\" \"bar\"".must_be_parsed_as s(:str, "foobar")
306
+ describe 'with string concatenation' do
307
+ it 'performs the concatenation in the case of two simple literal strings' do
308
+ '"foo" "bar"'.must_be_parsed_as s(:str, 'foobar')
309
309
  end
310
310
 
311
- it "performs the concatenation when the right string has interpolations" do
311
+ it 'performs the concatenation when the right string has interpolations' do
312
312
  "\"foo\" \"bar\#{baz}\"".
313
313
  must_be_parsed_as s(:dstr,
314
- "foobar",
314
+ 'foobar',
315
315
  s(:evstr, s(:call, nil, :baz)))
316
316
  end
317
317
 
318
- it "performs the concatenation when the left string has interpolations" do
319
- "\"foo\#{bar}\" \"baz\"".
320
- must_be_parsed_as s(:dstr,
321
- "foo",
322
- s(:evstr, s(:call, nil, :bar)),
323
- s(:str, "baz"))
318
+ describe 'when the left string has interpolations' do
319
+ it 'performs the concatenation' do
320
+ "\"foo\#{bar}\" \"baz\"".
321
+ must_be_parsed_as s(:dstr,
322
+ 'foo',
323
+ s(:evstr, s(:call, nil, :bar)),
324
+ s(:str, 'baz'))
325
+ end
326
+
327
+ it 'performs the concatenation with an empty string' do
328
+ "\"foo\#{bar}\" \"\"".
329
+ must_be_parsed_as s(:dstr,
330
+ 'foo',
331
+ s(:evstr, s(:call, nil, :bar)),
332
+ s(:str, ''))
333
+ end
324
334
  end
325
335
 
326
- describe "when both strings have interpolations" do
327
- it "performs the concatenation" do
336
+ describe 'when both strings have interpolations' do
337
+ it 'performs the concatenation' do
328
338
  "\"foo\#{bar}\" \"baz\#{qux}\"".
329
339
  must_be_parsed_as s(:dstr,
330
- "foo",
340
+ 'foo',
331
341
  s(:evstr, s(:call, nil, :bar)),
332
- s(:str, "baz"),
342
+ s(:str, 'baz'),
333
343
  s(:evstr, s(:call, nil, :qux)))
334
344
  end
335
345
 
336
- it "removes empty substrings from the concatenation" do
346
+ it 'removes empty substrings from the concatenation' do
337
347
  "\"foo\#{bar}\" \"\#{qux}\"".
338
348
  must_be_parsed_as s(:dstr,
339
- "foo",
349
+ 'foo',
340
350
  s(:evstr, s(:call, nil, :bar)),
341
351
  s(:evstr, s(:call, nil, :qux)))
342
352
  end
@@ -344,149 +354,194 @@ describe RipperRubyParser::Parser do
344
354
  end
345
355
  end
346
356
 
347
- describe "for word list literals" do
348
- it "correctly handles interpolation" do
357
+ describe 'for word list literals' do
358
+ it 'works for the simle case with %w' do
359
+ '%W(foo bar)'.
360
+ must_be_parsed_as s(:array, s(:str, 'foo'), s(:str, 'bar'))
361
+ end
362
+
363
+ it 'works for the simle case with %W' do
364
+ '%W(foo bar)'.
365
+ must_be_parsed_as s(:array, s(:str, 'foo'), s(:str, 'bar'))
366
+ end
367
+
368
+ it 'correctly handles interpolation' do
349
369
  "%W(foo \#{bar} baz)".
350
370
  must_be_parsed_as s(:array,
351
- s(:str, "foo"),
352
- s(:dstr, "", s(:evstr, s(:call, nil, :bar))),
353
- s(:str, "baz"))
371
+ s(:str, 'foo'),
372
+ s(:dstr, '', s(:evstr, s(:call, nil, :bar))),
373
+ s(:str, 'baz'))
354
374
  end
355
375
 
356
- it "correctly handles braceless interpolation" do
376
+ it 'correctly handles braceless interpolation' do
357
377
  "%W(foo \#@bar baz)".
358
378
  must_be_parsed_as s(:array,
359
- s(:str, "foo"),
360
- s(:dstr, "", s(:evstr, s(:ivar, :@bar))),
361
- s(:str, "baz"))
379
+ s(:str, 'foo'),
380
+ s(:dstr, '', s(:evstr, s(:ivar, :@bar))),
381
+ s(:str, 'baz'))
382
+ end
383
+
384
+ it 'correctly handles in-word interpolation' do
385
+ "%W(foo \#{bar}baz)".
386
+ must_be_parsed_as s(:array,
387
+ s(:str, 'foo'),
388
+ s(:dstr,
389
+ '',
390
+ s(:evstr, s(:call, nil, :bar)),
391
+ s(:str, 'baz')))
362
392
  end
363
393
  end
364
394
 
365
- describe "for character literals" do
366
- it "works for simple character literals" do
367
- "?a".
368
- must_be_parsed_as s(:str, "a")
395
+ describe 'for symbol list literals' do
396
+ it 'works for an array created with %i' do
397
+ '%i(foo bar)'.
398
+ must_be_parsed_as s(:array, s(:lit, :foo), s(:lit, :bar))
369
399
  end
370
400
 
371
- it "works for escaped character literals" do
372
- "?\\n".
401
+ it 'works for an array created with %I' do
402
+ '%I(foo bar)'.
403
+ must_be_parsed_as s(:array, s(:lit, :foo), s(:lit, :bar))
404
+ end
405
+
406
+ it 'correctly handles interpolation' do
407
+ "%I(foo \#{bar} baz)".
408
+ must_be_parsed_as s(:array,
409
+ s(:lit, :foo),
410
+ s(:dsym, "", s(:evstr, s(:call, nil, :bar))),
411
+ s(:lit, :baz))
412
+ end
413
+
414
+ it 'correctly handles in-word interpolation' do
415
+ "%I(foo \#{bar}baz)".
416
+ must_be_parsed_as s(:array,
417
+ s(:lit, :foo),
418
+ s(:dsym,
419
+ "",
420
+ s(:evstr, s(:call, nil, :bar)),
421
+ s(:str, "baz")))
422
+ end
423
+ end
424
+
425
+ describe 'for character literals' do
426
+ it 'works for simple character literals' do
427
+ '?a'.
428
+ must_be_parsed_as s(:str, 'a')
429
+ end
430
+
431
+ it 'works for escaped character literals' do
432
+ '?\\n'.
373
433
  must_be_parsed_as s(:str, "\n")
374
434
  end
375
435
 
376
- it "works for escaped character literals with ctrl" do
377
- "?\\C-a".
436
+ it 'works for escaped character literals with ctrl' do
437
+ '?\\C-a'.
378
438
  must_be_parsed_as s(:str, "\u0001")
379
439
  end
380
440
 
381
- it "works for escaped character literals with meta" do
382
- "?\\M-a".
383
- must_be_parsed_as s(:str, "\xE1".force_encoding("ascii-8bit"))
441
+ it 'works for escaped character literals with meta' do
442
+ '?\\M-a'.
443
+ must_be_parsed_as s(:str, "\xE1".force_encoding('ascii-8bit'))
384
444
  end
385
445
 
386
- it "works for escaped character literals with meta plus shorthand ctrl" do
387
- "?\\M-\\ca".
388
- must_be_parsed_as s(:str, "\x81".force_encoding("ascii-8bit"))
446
+ it 'works for escaped character literals with meta plus shorthand ctrl' do
447
+ '?\\M-\\ca'.
448
+ must_be_parsed_as s(:str, "\x81".force_encoding('ascii-8bit'))
389
449
  end
390
450
 
391
- it "works for escaped character literals with shorthand ctrl plus meta" do
392
- "?\\c\\M-a".
393
- must_be_parsed_as s(:str, "\x81".force_encoding("ascii-8bit"))
451
+ it 'works for escaped character literals with shorthand ctrl plus meta' do
452
+ '?\\c\\M-a'.
453
+ must_be_parsed_as s(:str, "\x81".force_encoding('ascii-8bit'))
394
454
  end
395
455
 
396
- it "works for escaped character literals with meta plus ctrl" do
397
- "?\\M-\\C-a".
398
- must_be_parsed_as s(:str, "\x81".force_encoding("ascii-8bit"))
456
+ it 'works for escaped character literals with meta plus ctrl' do
457
+ '?\\M-\\C-a'.
458
+ must_be_parsed_as s(:str, "\x81".force_encoding('ascii-8bit'))
399
459
  end
400
460
 
401
- it "works for escaped character literals with ctrl plus meta" do
402
- "?\\C-\\M-a".
403
- must_be_parsed_as s(:str, "\x81".force_encoding("ascii-8bit"))
461
+ it 'works for escaped character literals with ctrl plus meta' do
462
+ '?\\C-\\M-a'.
463
+ must_be_parsed_as s(:str, "\x81".force_encoding('ascii-8bit'))
404
464
  end
405
465
  end
406
466
 
407
- describe "for symbol literals" do
408
- it "works for simple symbols" do
409
- ":foo".
467
+ describe 'for symbol literals' do
468
+ it 'works for simple symbols' do
469
+ ':foo'.
410
470
  must_be_parsed_as s(:lit, :foo)
411
471
  end
412
472
 
413
- it "works for symbols that look like instance variable names" do
414
- ":@foo".
473
+ it 'works for symbols that look like instance variable names' do
474
+ ':@foo'.
415
475
  must_be_parsed_as s(:lit, :@foo)
416
476
  end
417
477
 
418
- it "works for simple dsyms" do
478
+ it 'works for simple dsyms' do
419
479
  ':"foo"'.
420
480
  must_be_parsed_as s(:lit, :foo)
421
481
  end
422
482
 
423
- it "works for dsyms with interpolations" do
483
+ it 'works for dsyms with interpolations' do
424
484
  ':"foo#{bar}"'.
425
485
  must_be_parsed_as s(:dsym,
426
- "foo",
486
+ 'foo',
427
487
  s(:evstr, s(:call, nil, :bar)))
428
488
  end
429
489
  end
430
490
 
431
- describe "for backtick string literals" do
432
- it "works for basic backtick strings" do
491
+ describe 'for backtick string literals' do
492
+ it 'works for basic backtick strings' do
433
493
  '`foo`'.
434
- must_be_parsed_as s(:xstr, "foo")
494
+ must_be_parsed_as s(:xstr, 'foo')
435
495
  end
436
496
 
437
- it "works for interpolated backtick strings" do
497
+ it 'works for interpolated backtick strings' do
438
498
  '`foo#{bar}`'.
439
499
  must_be_parsed_as s(:dxstr,
440
- "foo",
500
+ 'foo',
441
501
  s(:evstr, s(:call, nil, :bar)))
442
502
  end
443
503
 
444
- it "works for backtick strings with escape sequences" do
504
+ it 'works for backtick strings with escape sequences' do
445
505
  '`foo\\n`'.
446
506
  must_be_parsed_as s(:xstr, "foo\n")
447
507
  end
448
508
  end
449
509
 
450
- describe "for array literals" do
451
- it "works for an empty array" do
452
- "[]".
510
+ describe 'for array literals' do
511
+ it 'works for an empty array' do
512
+ '[]'.
453
513
  must_be_parsed_as s(:array)
454
514
  end
455
515
 
456
- it "works for a simple case with splat" do
457
- "[*foo]".
516
+ it 'works for a simple case with splat' do
517
+ '[*foo]'.
458
518
  must_be_parsed_as s(:array,
459
519
  s(:splat, s(:call, nil, :foo)))
460
520
  end
461
521
 
462
- it "works for a multi-element case with splat" do
463
- "[foo, *bar]".
522
+ it 'works for a multi-element case with splat' do
523
+ '[foo, *bar]'.
464
524
  must_be_parsed_as s(:array,
465
525
  s(:call, nil, :foo),
466
526
  s(:splat, s(:call, nil, :bar)))
467
527
  end
468
-
469
- it "works for an array created with %W" do
470
- "%W(foo bar)".
471
- must_be_parsed_as s(:array, s(:str, "foo"), s(:str, "bar"))
472
- end
473
528
  end
474
529
 
475
- describe "for hash literals" do
476
- it "works for an empty hash" do
477
- "{}".
530
+ describe 'for hash literals' do
531
+ it 'works for an empty hash' do
532
+ '{}'.
478
533
  must_be_parsed_as s(:hash)
479
534
  end
480
535
 
481
- it "works for a hash with one pair" do
482
- "{foo => bar}".
536
+ it 'works for a hash with one pair' do
537
+ '{foo => bar}'.
483
538
  must_be_parsed_as s(:hash,
484
539
  s(:call, nil, :foo),
485
540
  s(:call, nil, :bar))
486
541
  end
487
542
 
488
- it "works for a hash with multiple pairs" do
489
- "{foo => bar, baz => qux}".
543
+ it 'works for a hash with multiple pairs' do
544
+ '{foo => bar, baz => qux}'.
490
545
  must_be_parsed_as s(:hash,
491
546
  s(:call, nil, :foo),
492
547
  s(:call, nil, :bar),
@@ -494,26 +549,67 @@ describe RipperRubyParser::Parser do
494
549
  s(:call, nil, :qux))
495
550
  end
496
551
 
497
- it "works for a hash with label keys (Ruby 1.9 only)" do
498
- "{foo: bar, baz: qux}".
552
+ it 'works for a hash with label keys' do
553
+ '{foo: bar, baz: qux}'.
499
554
  must_be_parsed_as s(:hash,
500
555
  s(:lit, :foo),
501
556
  s(:call, nil, :bar),
502
557
  s(:lit, :baz),
503
558
  s(:call, nil, :qux))
504
559
  end
560
+
561
+ it 'works for a hash with dynamic label keys' do
562
+ skip 'This is not valid syntax below Ruby 2.2' if RUBY_VERSION < '2.2.0'
563
+ "{'foo': bar}".
564
+ must_be_parsed_as s(:hash,
565
+ s(:lit, :foo),
566
+ s(:call, nil, :bar))
567
+ end
568
+
569
+ it 'works for a hash with splat' do
570
+ '{foo: bar, baz: qux, **quux}'.
571
+ must_be_parsed_as s(:hash,
572
+ s(:lit, :foo), s(:call, nil, :bar),
573
+ s(:lit, :baz), s(:call, nil, :qux),
574
+ s(:kwsplat, s(:call, nil, :quux)))
575
+ end
505
576
  end
506
577
 
507
- describe "for number literals" do
508
- it "works for floats" do
509
- "3.14".
578
+ describe 'for number literals' do
579
+ it 'works for floats' do
580
+ '3.14'.
510
581
  must_be_parsed_as s(:lit, 3.14)
511
582
  end
512
583
 
513
- it "works for octal integer literals" do
514
- "0700".
584
+ it 'works for octal integer literals' do
585
+ '0700'.
515
586
  must_be_parsed_as s(:lit, 448)
516
587
  end
588
+
589
+ it 'handles negative sign for integers' do
590
+ '-1'.
591
+ must_be_parsed_as s(:lit, -1)
592
+ end
593
+
594
+ it 'handles space after negative sign for integers' do
595
+ '-1 '.
596
+ must_be_parsed_as s(:lit, -1)
597
+ end
598
+
599
+ it 'handles negative sign for floats' do
600
+ '-3.14'.
601
+ must_be_parsed_as s(:lit, -3.14)
602
+ end
603
+
604
+ it 'handles space after negative sign for floats' do
605
+ '-3.14 '.
606
+ must_be_parsed_as s(:lit, -3.14)
607
+ end
608
+
609
+ it 'handles positive sign' do
610
+ '+1'.
611
+ must_be_parsed_as s(:lit, 1)
612
+ end
517
613
  end
518
614
  end
519
615
  end