ripper_ruby_parser 1.7.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +76 -0
  3. data/README.md +6 -4
  4. data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +24 -12
  5. data/lib/ripper_ruby_parser/sexp_handlers.rb +2 -0
  6. data/lib/ripper_ruby_parser/sexp_handlers/assignment.rb +9 -4
  7. data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +40 -52
  8. data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +17 -19
  9. data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +35 -2
  10. data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +15 -242
  11. data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +9 -5
  12. data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +22 -17
  13. data/lib/ripper_ruby_parser/sexp_handlers/operators.rb +3 -3
  14. data/lib/ripper_ruby_parser/sexp_handlers/string_literals.rb +256 -0
  15. data/lib/ripper_ruby_parser/sexp_processor.rb +12 -56
  16. data/lib/ripper_ruby_parser/unescape.rb +89 -43
  17. data/lib/ripper_ruby_parser/version.rb +1 -1
  18. metadata +125 -76
  19. data/Rakefile +0 -33
  20. data/test/end_to_end/comments_test.rb +0 -59
  21. data/test/end_to_end/comparison_test.rb +0 -104
  22. data/test/end_to_end/lib_comparison_test.rb +0 -29
  23. data/test/end_to_end/line_numbering_test.rb +0 -31
  24. data/test/end_to_end/samples_comparison_test.rb +0 -13
  25. data/test/end_to_end/test_comparison_test.rb +0 -32
  26. data/test/pt_testcase/pt_test.rb +0 -44
  27. data/test/ripper_ruby_parser/commenting_ripper_parser_test.rb +0 -200
  28. data/test/ripper_ruby_parser/parser_test.rb +0 -553
  29. data/test/ripper_ruby_parser/sexp_handlers/assignment_test.rb +0 -613
  30. data/test/ripper_ruby_parser/sexp_handlers/blocks_test.rb +0 -679
  31. data/test/ripper_ruby_parser/sexp_handlers/conditionals_test.rb +0 -536
  32. data/test/ripper_ruby_parser/sexp_handlers/literals_test.rb +0 -1106
  33. data/test/ripper_ruby_parser/sexp_handlers/loops_test.rb +0 -209
  34. data/test/ripper_ruby_parser/sexp_handlers/method_calls_test.rb +0 -267
  35. data/test/ripper_ruby_parser/sexp_handlers/methods_test.rb +0 -421
  36. data/test/ripper_ruby_parser/sexp_handlers/operators_test.rb +0 -399
  37. data/test/ripper_ruby_parser/sexp_processor_test.rb +0 -303
  38. data/test/ripper_ruby_parser/version_test.rb +0 -7
  39. data/test/samples/assignment.rb +0 -17
  40. data/test/samples/comments.rb +0 -13
  41. data/test/samples/conditionals.rb +0 -23
  42. data/test/samples/lambdas.rb +0 -5
  43. data/test/samples/loops.rb +0 -36
  44. data/test/samples/misc.rb +0 -281
  45. data/test/samples/number.rb +0 -7
  46. data/test/samples/operators.rb +0 -18
  47. data/test/samples/strings.rb +0 -147
  48. data/test/test_helper.rb +0 -107
@@ -1,421 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../../test_helper.rb", File.dirname(__FILE__))
4
-
5
- describe RipperRubyParser::Parser do
6
- describe "#parse" do
7
- describe "for instance method definitions" do
8
- it "treats kwargs as a local variable" do
9
- _("def foo(**bar); bar; end")
10
- .must_be_parsed_as s(:defn,
11
- :foo,
12
- s(:args, :"**bar"),
13
- s(:lvar, :bar))
14
- end
15
-
16
- it "treats kwargs as a local variable when other arguments are present" do
17
- _("def foo(bar, **baz); baz; end")
18
- .must_be_parsed_as s(:defn,
19
- :foo,
20
- s(:args, :bar, :"**baz"),
21
- s(:lvar, :baz))
22
- end
23
-
24
- it "treats kwargs as a local variable when an explicit block is present" do
25
- _("def foo(**bar, &baz); bar; end")
26
- .must_be_parsed_as s(:defn,
27
- :foo,
28
- s(:args, :"**bar", :"&baz"),
29
- s(:lvar, :bar))
30
- end
31
-
32
- it "treats block kwargs as lvars" do
33
- _("def foo(**bar); baz { |**qux| bar; qux }; end")
34
- .must_be_parsed_as s(:defn, :foo,
35
- s(:args, :"**bar"),
36
- s(:iter,
37
- s(:call, nil, :baz),
38
- s(:args, :"**qux"),
39
- s(:block,
40
- s(:lvar, :bar),
41
- s(:lvar, :qux))))
42
- end
43
-
44
- it "works with a method argument with a default value" do
45
- _("def foo bar=nil; end")
46
- .must_be_parsed_as s(:defn,
47
- :foo,
48
- s(:args, s(:lasgn, :bar, s(:nil))),
49
- s(:nil))
50
- end
51
-
52
- it "works with several method arguments with default values" do
53
- _("def foo bar=1, baz=2; end")
54
- .must_be_parsed_as s(:defn,
55
- :foo,
56
- s(:args,
57
- s(:lasgn, :bar, s(:lit, 1)),
58
- s(:lasgn, :baz, s(:lit, 2))),
59
- s(:nil))
60
- end
61
-
62
- it "works with parentheses around the parameter list" do
63
- _("def foo(bar); end")
64
- .must_be_parsed_as s(:defn, :foo, s(:args, :bar), s(:nil))
65
- end
66
-
67
- it "works with a simple splat" do
68
- _("def foo *bar; end")
69
- .must_be_parsed_as s(:defn, :foo, s(:args, :"*bar"), s(:nil))
70
- end
71
-
72
- it "works with a regular argument plus splat" do
73
- _("def foo bar, *baz; end")
74
- .must_be_parsed_as s(:defn, :foo, s(:args, :bar, :"*baz"), s(:nil))
75
- end
76
-
77
- it "works with a nameless splat" do
78
- _("def foo *; end")
79
- .must_be_parsed_as s(:defn,
80
- :foo,
81
- s(:args, :"*"),
82
- s(:nil))
83
- end
84
-
85
- it "works for a simple case with explicit block parameter" do
86
- _("def foo &bar; end")
87
- .must_be_parsed_as s(:defn,
88
- :foo,
89
- s(:args, :"&bar"),
90
- s(:nil))
91
- end
92
-
93
- it "works with a regular argument plus explicit block parameter" do
94
- _("def foo bar, &baz; end")
95
- .must_be_parsed_as s(:defn,
96
- :foo,
97
- s(:args, :bar, :"&baz"),
98
- s(:nil))
99
- end
100
-
101
- it "works with a default value plus explicit block parameter" do
102
- _("def foo bar=1, &baz; end")
103
- .must_be_parsed_as s(:defn,
104
- :foo,
105
- s(:args,
106
- s(:lasgn, :bar, s(:lit, 1)),
107
- :"&baz"),
108
- s(:nil))
109
- end
110
-
111
- it "works with a default value plus mandatory argument" do
112
- _("def foo bar=1, baz; end")
113
- .must_be_parsed_as s(:defn,
114
- :foo,
115
- s(:args,
116
- s(:lasgn, :bar, s(:lit, 1)),
117
- :baz),
118
- s(:nil))
119
- end
120
-
121
- it "works with a splat plus explicit block parameter" do
122
- _("def foo *bar, &baz; end")
123
- .must_be_parsed_as s(:defn,
124
- :foo,
125
- s(:args, :"*bar", :"&baz"),
126
- s(:nil))
127
- end
128
-
129
- it "works with a default value plus splat" do
130
- _("def foo bar=1, *baz; end")
131
- .must_be_parsed_as s(:defn,
132
- :foo,
133
- s(:args,
134
- s(:lasgn, :bar, s(:lit, 1)),
135
- :"*baz"),
136
- s(:nil))
137
- end
138
-
139
- it "works with a default value, splat, plus final mandatory arguments" do
140
- _("def foo bar=1, *baz, qux, quuz; end")
141
- .must_be_parsed_as s(:defn,
142
- :foo,
143
- s(:args,
144
- s(:lasgn, :bar, s(:lit, 1)),
145
- :"*baz", :qux, :quuz),
146
- s(:nil))
147
- end
148
-
149
- it "works with a named argument with a default value" do
150
- _("def foo bar: 1; end")
151
- .must_be_parsed_as s(:defn,
152
- :foo,
153
- s(:args,
154
- s(:kwarg, :bar, s(:lit, 1))),
155
- s(:nil))
156
- end
157
-
158
- it "works with a named argument with no default value" do
159
- _("def foo bar:; end")
160
- .must_be_parsed_as s(:defn,
161
- :foo,
162
- s(:args,
163
- s(:kwarg, :bar)),
164
- s(:nil))
165
- end
166
-
167
- it "works with a double splat" do
168
- _("def foo **bar; end")
169
- .must_be_parsed_as s(:defn,
170
- :foo,
171
- s(:args, :'**bar'),
172
- s(:nil))
173
- end
174
-
175
- it "works with argument destructuring" do
176
- _("def foo((bar, baz)); end")
177
- .must_be_parsed_as s(:defn, :foo,
178
- s(:args, s(:masgn, :bar, :baz)),
179
- s(:nil))
180
- end
181
-
182
- it "works with argument destructuring including splat" do
183
- _("def foo((bar, *baz)); end")
184
- .must_be_parsed_as s(:defn, :foo,
185
- s(:args, s(:masgn, :bar, :'*baz')),
186
- s(:nil))
187
- end
188
-
189
- it "works with nested argument destructuring" do
190
- _("def foo((bar, (baz, qux))); end")
191
- .must_be_parsed_as s(:defn, :foo,
192
- s(:args, s(:masgn, :bar, s(:masgn, :baz, :qux))),
193
- s(:nil))
194
- end
195
-
196
- it "works when the method name is an operator" do
197
- _("def +; end")
198
- .must_be_parsed_as s(:defn, :+, s(:args),
199
- s(:nil))
200
- end
201
-
202
- it "works when the method name is a keyword" do
203
- _("def for; end")
204
- .must_be_parsed_as s(:defn, :for, s(:args),
205
- s(:nil))
206
- end
207
-
208
- it "assigns correct line numbers when the body is empty" do
209
- _("def bar\nend")
210
- .must_be_parsed_as s(:defn,
211
- :bar,
212
- s(:args).line(1),
213
- s(:nil).line(2)).line(1),
214
- with_line_numbers: true
215
- end
216
- end
217
-
218
- describe "for singleton method definitions" do
219
- it "works with empty body" do
220
- _("def foo.bar; end")
221
- .must_be_parsed_as s(:defs,
222
- s(:call, nil, :foo),
223
- :bar,
224
- s(:args),
225
- s(:nil))
226
- end
227
-
228
- it "works with a body with multiple statements" do
229
- _("def foo.bar; baz; qux; end")
230
- .must_be_parsed_as s(:defs,
231
- s(:call, nil, :foo),
232
- :bar,
233
- s(:args),
234
- s(:call, nil, :baz),
235
- s(:call, nil, :qux))
236
- end
237
-
238
- it "works with a simple splat" do
239
- _("def foo.bar *baz; end")
240
- .must_be_parsed_as s(:defs,
241
- s(:call, nil, :foo),
242
- :bar,
243
- s(:args, :"*baz"),
244
- s(:nil))
245
- end
246
-
247
- it "works when the method name is a keyword" do
248
- _("def foo.for; end")
249
- .must_be_parsed_as s(:defs,
250
- s(:call, nil, :foo),
251
- :for, s(:args),
252
- s(:nil))
253
- end
254
- end
255
-
256
- describe "for the alias statement" do
257
- it "works with regular barewords" do
258
- _("alias foo bar")
259
- .must_be_parsed_as s(:alias,
260
- s(:lit, :foo), s(:lit, :bar))
261
- end
262
-
263
- it "works with symbols" do
264
- _("alias :foo :bar")
265
- .must_be_parsed_as s(:alias,
266
- s(:lit, :foo), s(:lit, :bar))
267
- end
268
-
269
- it "works with operator barewords" do
270
- _("alias + -")
271
- .must_be_parsed_as s(:alias,
272
- s(:lit, :+), s(:lit, :-))
273
- end
274
-
275
- it "treats keywords as symbols" do
276
- _("alias next foo")
277
- .must_be_parsed_as s(:alias, s(:lit, :next), s(:lit, :foo))
278
- end
279
-
280
- it "works with global variables" do
281
- _("alias $foo $bar")
282
- .must_be_parsed_as s(:valias, :$foo, :$bar)
283
- end
284
- end
285
-
286
- describe "for the undef statement" do
287
- it "works with a single bareword identifier" do
288
- _("undef foo")
289
- .must_be_parsed_as s(:undef, s(:lit, :foo))
290
- end
291
-
292
- it "works with a single symbol" do
293
- _("undef :foo")
294
- .must_be_parsed_as s(:undef, s(:lit, :foo))
295
- end
296
-
297
- it "works with multiple bareword identifiers" do
298
- _("undef foo, bar")
299
- .must_be_parsed_as s(:block,
300
- s(:undef, s(:lit, :foo)),
301
- s(:undef, s(:lit, :bar)))
302
- end
303
-
304
- it "works with multiple bareword symbols" do
305
- _("undef :foo, :bar")
306
- .must_be_parsed_as s(:block,
307
- s(:undef, s(:lit, :foo)),
308
- s(:undef, s(:lit, :bar)))
309
- end
310
- end
311
-
312
- describe "for the return statement" do
313
- it "works with no arguments" do
314
- _("return")
315
- .must_be_parsed_as s(:return)
316
- end
317
-
318
- it "works with one argument" do
319
- _("return foo")
320
- .must_be_parsed_as s(:return,
321
- s(:call, nil, :foo))
322
- end
323
-
324
- it "works with a splat argument" do
325
- _("return *foo")
326
- .must_be_parsed_as s(:return,
327
- s(:svalue,
328
- s(:splat,
329
- s(:call, nil, :foo))))
330
- end
331
-
332
- it "works with multiple arguments" do
333
- _("return foo, bar")
334
- .must_be_parsed_as s(:return,
335
- s(:array,
336
- s(:call, nil, :foo),
337
- s(:call, nil, :bar)))
338
- end
339
-
340
- it "works with a regular argument and a splat argument" do
341
- _("return foo, *bar")
342
- .must_be_parsed_as s(:return,
343
- s(:array,
344
- s(:call, nil, :foo),
345
- s(:splat,
346
- s(:call, nil, :bar))))
347
- end
348
-
349
- it "works with a function call with parentheses" do
350
- _("return foo(bar)")
351
- .must_be_parsed_as s(:return,
352
- s(:call, nil, :foo,
353
- s(:call, nil, :bar)))
354
- end
355
-
356
- it "works with a function call without parentheses" do
357
- _("return foo bar")
358
- .must_be_parsed_as s(:return,
359
- s(:call, nil, :foo,
360
- s(:call, nil, :bar)))
361
- end
362
- end
363
-
364
- describe "for yield" do
365
- it "works with no arguments and no parentheses" do
366
- _("yield")
367
- .must_be_parsed_as s(:yield)
368
- end
369
-
370
- it "works with parentheses but no arguments" do
371
- _("yield()")
372
- .must_be_parsed_as s(:yield)
373
- end
374
-
375
- it "works with one argument and no parentheses" do
376
- _("yield foo")
377
- .must_be_parsed_as s(:yield, s(:call, nil, :foo))
378
- end
379
-
380
- it "works with one argument and parentheses" do
381
- _("yield(foo)")
382
- .must_be_parsed_as s(:yield, s(:call, nil, :foo))
383
- end
384
-
385
- it "works with multiple arguments and no parentheses" do
386
- _("yield foo, bar")
387
- .must_be_parsed_as s(:yield,
388
- s(:call, nil, :foo),
389
- s(:call, nil, :bar))
390
- end
391
-
392
- it "works with multiple arguments and parentheses" do
393
- _("yield(foo, bar)")
394
- .must_be_parsed_as s(:yield,
395
- s(:call, nil, :foo),
396
- s(:call, nil, :bar))
397
- end
398
-
399
- it "works with splat" do
400
- _("yield foo, *bar")
401
- .must_be_parsed_as s(:yield,
402
- s(:call, nil, :foo),
403
- s(:splat, s(:call, nil, :bar)))
404
- end
405
-
406
- it "works with a function call with parentheses" do
407
- _("yield foo(bar)")
408
- .must_be_parsed_as s(:yield,
409
- s(:call, nil, :foo,
410
- s(:call, nil, :bar)))
411
- end
412
-
413
- it "works with a function call without parentheses" do
414
- _("yield foo bar")
415
- .must_be_parsed_as s(:yield,
416
- s(:call, nil, :foo,
417
- s(:call, nil, :bar)))
418
- end
419
- end
420
- end
421
- end
@@ -1,399 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../../test_helper.rb", File.dirname(__FILE__))
4
-
5
- describe RipperRubyParser::Parser do
6
- describe "#parse" do
7
- describe "for boolean operators" do
8
- it "handles :and" do
9
- _("foo and bar")
10
- .must_be_parsed_as s(:and,
11
- s(:call, nil, :foo),
12
- s(:call, nil, :bar))
13
- end
14
-
15
- it "handles double :and" do
16
- _("foo and bar and baz")
17
- .must_be_parsed_as s(:and,
18
- s(:call, nil, :foo),
19
- s(:and,
20
- s(:call, nil, :bar),
21
- s(:call, nil, :baz)))
22
- end
23
-
24
- it "handles :or" do
25
- _("foo or bar")
26
- .must_be_parsed_as s(:or,
27
- s(:call, nil, :foo),
28
- s(:call, nil, :bar))
29
- end
30
-
31
- it "handles double :or" do
32
- _("foo or bar or baz")
33
- .must_be_parsed_as s(:or,
34
- s(:call, nil, :foo),
35
- s(:or,
36
- s(:call, nil, :bar),
37
- s(:call, nil, :baz)))
38
- end
39
-
40
- it "handles :or after :and" do
41
- _("foo and bar or baz")
42
- .must_be_parsed_as s(:or,
43
- s(:and,
44
- s(:call, nil, :foo),
45
- s(:call, nil, :bar)),
46
- s(:call, nil, :baz))
47
- end
48
-
49
- it "handles :and after :or" do
50
- _("foo or bar and baz")
51
- .must_be_parsed_as s(:and,
52
- s(:or,
53
- s(:call, nil, :foo),
54
- s(:call, nil, :bar)),
55
- s(:call, nil, :baz))
56
- end
57
-
58
- it "converts :&& to :and" do
59
- _("foo && bar")
60
- .must_be_parsed_as s(:and,
61
- s(:call, nil, :foo),
62
- s(:call, nil, :bar))
63
- end
64
-
65
- it "handles :|| after :&&" do
66
- _("foo && bar || baz")
67
- .must_be_parsed_as s(:or,
68
- s(:and,
69
- s(:call, nil, :foo),
70
- s(:call, nil, :bar)),
71
- s(:call, nil, :baz))
72
- end
73
-
74
- it "handles :&& after :||" do
75
- _("foo || bar && baz")
76
- .must_be_parsed_as s(:or,
77
- s(:call, nil, :foo),
78
- s(:and,
79
- s(:call, nil, :bar),
80
- s(:call, nil, :baz)))
81
- end
82
-
83
- it "handles :|| with parentheses" do
84
- _("(foo || bar) || baz")
85
- .must_be_parsed_as s(:or,
86
- s(:or,
87
- s(:call, nil, :foo),
88
- s(:call, nil, :bar)),
89
- s(:call, nil, :baz))
90
- end
91
-
92
- it "handles nested :|| with parentheses" do
93
- _("foo || (bar || baz) || qux")
94
- .must_be_parsed_as s(:or,
95
- s(:call, nil, :foo),
96
- s(:or,
97
- s(:or, s(:call, nil, :bar), s(:call, nil, :baz)),
98
- s(:call, nil, :qux)))
99
- end
100
-
101
- it "converts :|| to :or" do
102
- _("foo || bar")
103
- .must_be_parsed_as s(:or,
104
- s(:call, nil, :foo),
105
- s(:call, nil, :bar))
106
- end
107
-
108
- it "handles triple :and" do
109
- _("foo and bar and baz and qux")
110
- .must_be_parsed_as s(:and,
111
- s(:call, nil, :foo),
112
- s(:and,
113
- s(:call, nil, :bar),
114
- s(:and,
115
- s(:call, nil, :baz),
116
- s(:call, nil, :qux))))
117
- end
118
-
119
- it "handles triple :&&" do
120
- _("foo && bar && baz && qux")
121
- .must_be_parsed_as s(:and,
122
- s(:call, nil, :foo),
123
- s(:and,
124
- s(:call, nil, :bar),
125
- s(:and,
126
- s(:call, nil, :baz),
127
- s(:call, nil, :qux))))
128
- end
129
-
130
- it "handles :!=" do
131
- _("foo != bar")
132
- .must_be_parsed_as s(:call,
133
- s(:call, nil, :foo),
134
- :!=,
135
- s(:call, nil, :bar))
136
- end
137
-
138
- it "keeps :begin for the first argument of a binary operator" do
139
- _("begin; bar; end + foo")
140
- .must_be_parsed_as s(:call,
141
- s(:begin, s(:call, nil, :bar)),
142
- :+,
143
- s(:call, nil, :foo))
144
- end
145
-
146
- it "keeps :begin for the second argument of a binary operator" do
147
- _("foo + begin; bar; end")
148
- .must_be_parsed_as s(:call,
149
- s(:call, nil, :foo),
150
- :+,
151
- s(:begin, s(:call, nil, :bar)))
152
- end
153
-
154
- it "does not keep :begin for the first argument of a boolean operator" do
155
- _("begin; bar; end and foo")
156
- .must_be_parsed_as s(:and,
157
- s(:call, nil, :bar),
158
- s(:call, nil, :foo))
159
- end
160
-
161
- it "keeps :begin for the second argument of a boolean operator" do
162
- _("foo and begin; bar; end")
163
- .must_be_parsed_as s(:and,
164
- s(:call, nil, :foo),
165
- s(:begin, s(:call, nil, :bar)))
166
- end
167
-
168
- it "does not keep :begin for the first argument of a shift operator" do
169
- _("begin; bar; end << foo")
170
- .must_be_parsed_as s(:call,
171
- s(:call, nil, :bar),
172
- :<<,
173
- s(:call, nil, :foo))
174
- end
175
-
176
- it "does not keep :begin for the second argument of a shift operator" do
177
- _("foo >> begin; bar; end")
178
- .must_be_parsed_as s(:call,
179
- s(:call, nil, :foo),
180
- :>>,
181
- s(:call, nil, :bar))
182
- end
183
- end
184
-
185
- describe "for the range operator" do
186
- it "handles positive number literals" do
187
- _("1..2")
188
- .must_be_parsed_as s(:lit, 1..2)
189
- end
190
-
191
- it "handles negative number literals" do
192
- _("-1..-2")
193
- .must_be_parsed_as s(:lit, -1..-2)
194
- end
195
-
196
- it "handles float literals" do
197
- _("1.0..2.0")
198
- .must_be_parsed_as s(:dot2,
199
- s(:lit, 1.0),
200
- s(:lit, 2.0))
201
- end
202
-
203
- it "handles string literals" do
204
- _("'a'..'z'")
205
- .must_be_parsed_as s(:dot2,
206
- s(:str, "a"),
207
- s(:str, "z"))
208
- end
209
-
210
- it "handles non-literal begin" do
211
- _("foo..3")
212
- .must_be_parsed_as s(:dot2,
213
- s(:call, nil, :foo),
214
- s(:lit, 3))
215
- end
216
-
217
- it "handles non-literal end" do
218
- _("3..foo")
219
- .must_be_parsed_as s(:dot2,
220
- s(:lit, 3),
221
- s(:call, nil, :foo))
222
- end
223
-
224
- it "handles non-literals" do
225
- _("foo..bar")
226
- .must_be_parsed_as s(:dot2,
227
- s(:call, nil, :foo),
228
- s(:call, nil, :bar))
229
- end
230
- end
231
-
232
- describe "for the exclusive range operator" do
233
- it "handles positive number literals" do
234
- _("1...2")
235
- .must_be_parsed_as s(:lit, 1...2)
236
- end
237
-
238
- it "handles negative number literals" do
239
- _("-1...-2")
240
- .must_be_parsed_as s(:lit, -1...-2)
241
- end
242
-
243
- it "handles float literals" do
244
- _("1.0...2.0")
245
- .must_be_parsed_as s(:dot3,
246
- s(:lit, 1.0),
247
- s(:lit, 2.0))
248
- end
249
-
250
- it "handles string literals" do
251
- _("'a'...'z'")
252
- .must_be_parsed_as s(:dot3,
253
- s(:str, "a"),
254
- s(:str, "z"))
255
- end
256
-
257
- it "handles non-literal begin" do
258
- _("foo...3")
259
- .must_be_parsed_as s(:dot3,
260
- s(:call, nil, :foo),
261
- s(:lit, 3))
262
- end
263
-
264
- it "handles non-literal end" do
265
- _("3...foo")
266
- .must_be_parsed_as s(:dot3,
267
- s(:lit, 3),
268
- s(:call, nil, :foo))
269
- end
270
-
271
- it "handles two non-literals" do
272
- _("foo...bar")
273
- .must_be_parsed_as s(:dot3,
274
- s(:call, nil, :foo),
275
- s(:call, nil, :bar))
276
- end
277
- end
278
-
279
- describe "for unary operators" do
280
- it "handles unary minus with an integer literal" do
281
- _("- 1").must_be_parsed_as s(:call, s(:lit, 1), :-@)
282
- end
283
-
284
- it "handles unary minus with a float literal" do
285
- _("- 3.14").must_be_parsed_as s(:call, s(:lit, 3.14), :-@)
286
- end
287
-
288
- it "handles unary minus with a non-literal" do
289
- _("-foo")
290
- .must_be_parsed_as s(:call,
291
- s(:call, nil, :foo),
292
- :-@)
293
- end
294
-
295
- it "handles unary minus with a negative number literal" do
296
- _("- -1").must_be_parsed_as s(:call, s(:lit, -1), :-@)
297
- end
298
-
299
- it "handles unary plus with a number literal" do
300
- _("+ 1").must_be_parsed_as s(:call, s(:lit, 1), :+@)
301
- end
302
-
303
- it "handles unary plus with a non-literal" do
304
- _("+foo")
305
- .must_be_parsed_as s(:call,
306
- s(:call, nil, :foo),
307
- :+@)
308
- end
309
-
310
- it "handles unary !" do
311
- _("!foo")
312
- .must_be_parsed_as s(:call, s(:call, nil, :foo), :!)
313
- end
314
-
315
- it "converts :not to :!" do
316
- _("not foo")
317
- .must_be_parsed_as s(:call, s(:call, nil, :foo), :!)
318
- end
319
-
320
- it "handles unary ! with a number literal" do
321
- _("!1")
322
- .must_be_parsed_as s(:call, s(:lit, 1), :!)
323
- end
324
-
325
- it "keeps :begin for the argument" do
326
- _("- begin; foo; end")
327
- .must_be_parsed_as s(:call,
328
- s(:begin, s(:call, nil, :foo)),
329
- :-@)
330
- end
331
- end
332
-
333
- describe "for the ternary operater" do
334
- it "works in the simple case" do
335
- _("foo ? bar : baz")
336
- .must_be_parsed_as s(:if,
337
- s(:call, nil, :foo),
338
- s(:call, nil, :bar),
339
- s(:call, nil, :baz))
340
- end
341
-
342
- it "keeps :begin for the first argument" do
343
- _("begin; foo; end ? bar : baz")
344
- .must_be_parsed_as s(:if,
345
- s(:begin, s(:call, nil, :foo)),
346
- s(:call, nil, :bar),
347
- s(:call, nil, :baz))
348
- end
349
-
350
- it "keeps :begin for the second argument" do
351
- _("foo ? begin; bar; end : baz")
352
- .must_be_parsed_as s(:if,
353
- s(:call, nil, :foo),
354
- s(:begin, s(:call, nil, :bar)),
355
- s(:call, nil, :baz))
356
- end
357
-
358
- it "keeps :begin for the third argument" do
359
- _("foo ? bar : begin; baz; end")
360
- .must_be_parsed_as s(:if,
361
- s(:call, nil, :foo),
362
- s(:call, nil, :bar),
363
- s(:begin, s(:call, nil, :baz)))
364
- end
365
- end
366
-
367
- describe "for match operators" do
368
- it "handles :=~ with two non-literals" do
369
- _("foo =~ bar")
370
- .must_be_parsed_as s(:call,
371
- s(:call, nil, :foo),
372
- :=~,
373
- s(:call, nil, :bar))
374
- end
375
-
376
- it "handles :=~ with literal regexp on the left hand side" do
377
- _("/foo/ =~ bar")
378
- .must_be_parsed_as s(:match2,
379
- s(:lit, /foo/),
380
- s(:call, nil, :bar))
381
- end
382
-
383
- it "handles :=~ with literal regexp on the right hand side" do
384
- _("foo =~ /bar/")
385
- .must_be_parsed_as s(:match3,
386
- s(:lit, /bar/),
387
- s(:call, nil, :foo))
388
- end
389
-
390
- it "handles negated match operators" do
391
- _("foo !~ bar").must_be_parsed_as s(:not,
392
- s(:call,
393
- s(:call, nil, :foo),
394
- :=~,
395
- s(:call, nil, :bar)))
396
- end
397
- end
398
- end
399
- end