unparser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/.circle.yml +4 -0
  3. data/.gitignore +2 -0
  4. data/.rspec +3 -0
  5. data/.travis.yml +16 -0
  6. data/Changelog.md +3 -0
  7. data/Gemfile +6 -0
  8. data/Gemfile.devtools +60 -0
  9. data/Guardfile +18 -0
  10. data/README.md +41 -0
  11. data/Rakefile +24 -0
  12. data/TODO +1 -0
  13. data/config/devtools.yml +2 -0
  14. data/config/flay.yml +3 -0
  15. data/config/flog.yml +2 -0
  16. data/config/mutant.yml +3 -0
  17. data/config/reek.yml +107 -0
  18. data/config/yardstick.yml +2 -0
  19. data/lib/unparser/buffer.rb +93 -0
  20. data/lib/unparser/constants.rb +87 -0
  21. data/lib/unparser/emitter/alias.rb +27 -0
  22. data/lib/unparser/emitter/argument.rb +154 -0
  23. data/lib/unparser/emitter/assignment.rb +140 -0
  24. data/lib/unparser/emitter/begin.rb +179 -0
  25. data/lib/unparser/emitter/binary.rb +52 -0
  26. data/lib/unparser/emitter/block.rb +53 -0
  27. data/lib/unparser/emitter/break.rb +28 -0
  28. data/lib/unparser/emitter/case.rb +97 -0
  29. data/lib/unparser/emitter/cbase.rb +22 -0
  30. data/lib/unparser/emitter/class.rb +72 -0
  31. data/lib/unparser/emitter/def.rb +107 -0
  32. data/lib/unparser/emitter/defined.rb +27 -0
  33. data/lib/unparser/emitter/for.rb +50 -0
  34. data/lib/unparser/emitter/hookexe.rb +42 -0
  35. data/lib/unparser/emitter/if.rb +85 -0
  36. data/lib/unparser/emitter/literal/composed.rb +64 -0
  37. data/lib/unparser/emitter/literal/dynamic.rb +54 -0
  38. data/lib/unparser/emitter/literal/dynamic_body.rb +109 -0
  39. data/lib/unparser/emitter/literal/execute_string.rb +38 -0
  40. data/lib/unparser/emitter/literal/primitive.rb +49 -0
  41. data/lib/unparser/emitter/literal/range.rb +33 -0
  42. data/lib/unparser/emitter/literal/regexp.rb +96 -0
  43. data/lib/unparser/emitter/literal/singleton.rb +24 -0
  44. data/lib/unparser/emitter/literal.rb +7 -0
  45. data/lib/unparser/emitter/module.rb +37 -0
  46. data/lib/unparser/emitter/next.rb +22 -0
  47. data/lib/unparser/emitter/not.rb +25 -0
  48. data/lib/unparser/emitter/op_assign.rb +63 -0
  49. data/lib/unparser/emitter/redo.rb +22 -0
  50. data/lib/unparser/emitter/repetition.rb +33 -0
  51. data/lib/unparser/emitter/retry.rb +22 -0
  52. data/lib/unparser/emitter/return.rb +38 -0
  53. data/lib/unparser/emitter/root.rb +7 -0
  54. data/lib/unparser/emitter/send/binary.rb +100 -0
  55. data/lib/unparser/emitter/send/index.rb +82 -0
  56. data/lib/unparser/emitter/send/unary.rb +29 -0
  57. data/lib/unparser/emitter/send.rb +273 -0
  58. data/lib/unparser/emitter/splat.rb +24 -0
  59. data/lib/unparser/emitter/super.rb +46 -0
  60. data/lib/unparser/emitter/undef.rb +25 -0
  61. data/lib/unparser/emitter/variable.rb +83 -0
  62. data/lib/unparser/emitter/yield.rb +28 -0
  63. data/lib/unparser/emitter.rb +308 -0
  64. data/lib/unparser.rb +87 -0
  65. data/spec/integration/unparser/spike_spec.rb +914 -0
  66. data/spec/spec_helper.rb +13 -0
  67. data/spec/unit/unparser/buffer/append_spec.rb +23 -0
  68. data/spec/unit/unparser/buffer/content_spec.rb +38 -0
  69. data/spec/unit/unparser/buffer/indent_spec.rb +18 -0
  70. data/spec/unit/unparser/buffer/nl_spec.rb +16 -0
  71. data/spec/unit/unparser/buffer/unindent_spec.rb +18 -0
  72. data/spec/unit/unparser/class_methods/unparse_spec.rb +16 -0
  73. data/spec/unit/unparser/emitter/class_methods/handle_spec.rb +17 -0
  74. data/spec/unit/unparser/emitter/class_methods/visit_spec.rb +37 -0
  75. data/spec/unit/unparser/emitter/source_map/class_methods/emit_spec.rb +20 -0
  76. data/unparser.gemspec +24 -0
  77. metadata +201 -0
@@ -0,0 +1,914 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser, 'spike' do
4
+
5
+
6
+ PARSERS = IceNine.deep_freeze(
7
+ '1.8' => Parser::Ruby18,
8
+ '1.9' => Parser::Ruby19,
9
+ '2.0' => Parser::Ruby20
10
+ )
11
+
12
+ RUBIES = PARSERS.keys.freeze
13
+
14
+ def self.parser_for_ruby_version(version)
15
+ PARSERS.fetch(version) do
16
+ raise "Unrecognized Ruby version #{version}"
17
+ end
18
+ end
19
+
20
+ def self.with_versions(versions)
21
+ versions.each do |version|
22
+ parser = parser_for_ruby_version(version)
23
+ yield version, parser
24
+ end
25
+ end
26
+
27
+ def self.strip(ruby)
28
+ lines = ruby.lines
29
+ line = lines.first
30
+ match = /\A[ ]*/.match(line)
31
+ length = match[0].length
32
+ source = lines.map do |line|
33
+ line[(length..-1)]
34
+ end.join
35
+ source.chomp
36
+ end
37
+
38
+ def assert_round_trip(input, parser)
39
+ ast = parser.parse(input)
40
+ generated = Unparser.unparse(ast)
41
+ generated.should eql(input)
42
+ end
43
+
44
+ def self.assert_generates(ast, expected, versions = RUBIES)
45
+ with_versions(versions) do |version, parser|
46
+ it "should generate #{ast.inspect} as #{expected} under #{version}" do
47
+ unless ast.kind_of?(Parser::AST::Node)
48
+ ast = parser.parse(ast)
49
+ end
50
+ generated = Unparser.unparse(ast)
51
+ generated.should eql(expected)
52
+ ast = parser.parse(generated)
53
+ Unparser.unparse(ast).should eql(expected)
54
+ end
55
+ end
56
+ end
57
+
58
+ def self.assert_round_trip(input, versions = RUBIES)
59
+ with_versions(versions) do |version, parser|
60
+ it "should round trip #{input.inspect} under #{version}" do
61
+ assert_round_trip(input, parser)
62
+ end
63
+ end
64
+ end
65
+
66
+ def self.assert_source(input, versions = RUBIES)
67
+ assert_round_trip(strip(input), versions)
68
+ end
69
+
70
+ context 'literal' do
71
+ context 'fixnum' do
72
+ assert_generates s(:int, 1), '1'
73
+ assert_generates s(:int, -1), '-1'
74
+ assert_source '1'
75
+ assert_source '0x1'
76
+ assert_source '1_000'
77
+ assert_source '1e10'
78
+ assert_source '?c'
79
+ end
80
+
81
+ context 'string' do
82
+ assert_generates %q("foo" "bar"), %q("foobar")
83
+ assert_generates %q(%Q(foo"#{@bar})), %q("foo\"#{@bar}")
84
+ assert_source %q("\"")
85
+ assert_source %q("foo#{1}bar")
86
+ assert_source %q("\"#{@a}")
87
+ end
88
+
89
+ context 'execute string' do
90
+ assert_source '`foo`'
91
+ assert_source '`foo#{@bar}`'
92
+ assert_generates '%x(\))', '`)`'
93
+ #assert_generates '%x(`)', '`\``'
94
+ assert_source '`"`'
95
+ end
96
+
97
+ context 'symbol' do
98
+ assert_generates s(:sym, :foo), ':foo'
99
+ assert_generates s(:sym, :"A B"), ':"A B"'
100
+ assert_source ':foo'
101
+ assert_source ':"A B"'
102
+ assert_source ':"A\"B"'
103
+ end
104
+
105
+ context 'regexp' do
106
+ assert_source '/foo/'
107
+ assert_source %q(/[^-+',.\/:@[:alnum:]\[\]\x80-\xff]+/)
108
+ assert_source '/foo#{@bar}/'
109
+ assert_source '/foo#{@bar}/im'
110
+ assert_generates '%r(/)', '/\//'
111
+ assert_generates '%r(\))', '/)/'
112
+ assert_generates '%r(#{@bar}baz)', '/#{@bar}baz/'
113
+ end
114
+
115
+ context 'dynamic string' do
116
+ assert_source %q("foo#{@bar}")
117
+ assert_source %q("fo\no#{bar}b\naz")
118
+ end
119
+
120
+ context 'dynamic symbol' do
121
+ assert_source ':"foo#{bar}baz"'
122
+ assert_source ':"fo\no#{bar}b\naz"'
123
+ assert_source ':"#{bar}foo"'
124
+ assert_source ':"foo#{bar}"'
125
+ end
126
+
127
+ context 'irange' do
128
+ assert_generates '1..2', %q(1..2)
129
+ end
130
+
131
+ context 'erange' do
132
+ assert_generates '1...2', %q(1...2)
133
+ end
134
+
135
+ context 'float' do
136
+ assert_source '-0.1'
137
+ assert_source '0.1'
138
+ assert_generates s(:float, -0.1), '-0.1'
139
+ assert_generates s(:float, 0.1), '0.1'
140
+ end
141
+
142
+ context 'array' do
143
+ assert_source '[1, 2]'
144
+ assert_source '[1]'
145
+ assert_source '[]'
146
+ assert_source '[1, *@foo]'
147
+ assert_source '[*@foo, 1]', RUBIES - %w(1.8)
148
+ assert_source '[*@foo, *@baz]', RUBIES - %w(1.8)
149
+ end
150
+
151
+ context 'hash' do
152
+ assert_source '{}'
153
+ assert_source '{1 => 2}'
154
+ assert_source '{1 => 2, 3 => 4}'
155
+ end
156
+ end
157
+
158
+ context 'access' do
159
+ assert_source '@a'
160
+ assert_source '@@a'
161
+ assert_source '$a'
162
+ assert_source '$1'
163
+ assert_source '$`'
164
+ assert_source 'CONST'
165
+ assert_source 'SCOPED::CONST'
166
+ assert_source '::TOPLEVEL'
167
+ assert_source '::TOPLEVEL::CONST'
168
+ end
169
+
170
+ context 'break' do
171
+ assert_source 'break'
172
+ assert_source 'break(a)'
173
+ end
174
+
175
+ context 'next' do
176
+ assert_source 'next'
177
+ end
178
+
179
+ context 'retry' do
180
+ assert_source 'retry'
181
+ end
182
+
183
+ context 'redo' do
184
+ assert_source 'redo'
185
+ end
186
+
187
+ context 'singletons' do
188
+ assert_source 'self'
189
+ assert_source 'true'
190
+ assert_source 'false'
191
+ assert_source 'nil'
192
+ end
193
+
194
+ context 'magic keywords' do
195
+ assert_generates '__ENCODING__', 'Encoding::UTF_8', RUBIES - %w(1.8)
196
+ assert_source '__FILE__'
197
+ assert_source '__LINE__'
198
+ end
199
+
200
+ context 'assignment' do
201
+ context 'single' do
202
+ assert_source 'a = 1'
203
+ assert_source '@a = 1'
204
+ assert_source '@@a = 1'
205
+ assert_source '$a = 1'
206
+ assert_source 'CONST = 1'
207
+ end
208
+
209
+ context 'multiple' do
210
+ assert_source 'a, b = 1, 2'
211
+ assert_source 'a, *foo = 1, 2'
212
+ assert_source 'a, * = 1, 2'
213
+ assert_source '*foo = 1, 2'
214
+ assert_source '@a, @b = 1, 2'
215
+ assert_source 'a.foo, a.bar = 1, 2'
216
+ assert_source 'a[0, 2]'
217
+ assert_source 'a[0], a[1] = 1, 2'
218
+ assert_source 'a[*foo], a[1] = 1, 2'
219
+ assert_source '@@a, @@b = 1, 2'
220
+ assert_source '$a, $b = 1, 2'
221
+ assert_source 'a, b = foo'
222
+ end
223
+ end
224
+
225
+ context 'return' do
226
+ assert_source <<-RUBY
227
+ return
228
+ RUBY
229
+
230
+ assert_source <<-RUBY
231
+ return(1)
232
+ RUBY
233
+ end
234
+
235
+ context 'send' do
236
+ assert_source 'foo'
237
+ assert_source 'self.foo'
238
+ assert_source 'a.foo'
239
+ assert_source 'A.foo'
240
+ assert_source 'foo[1]'
241
+ assert_source 'foo(1)'
242
+ assert_source 'foo(bar)'
243
+ assert_source 'foo(&block)'
244
+ assert_source 'foo(*arguments)'
245
+ assert_source "foo do\n nil\nend"
246
+ assert_source "foo(1) do\n nil\nend"
247
+ assert_source "foo do |a, b|\n nil\nend"
248
+ assert_source "foo do |a, *b|\n nil\nend"
249
+ assert_source "foo do |a, *|\n nil\nend"
250
+ assert_source "foo do\n bar\nend"
251
+ assert_source 'foo.bar(*args)'
252
+
253
+ assert_source <<-RUBY
254
+ foo.bar do |(a, b), c|
255
+ d
256
+ end
257
+ RUBY
258
+
259
+ assert_source <<-RUBY
260
+ foo.bar do |(a, b)|
261
+ d
262
+ end
263
+ RUBY
264
+
265
+ # Special cases
266
+ assert_source '(1..2).max'
267
+
268
+ assert_source 'foo.bar(*args)'
269
+ assert_source 'foo.bar(*arga, foo, *argb)', RUBIES - %w(1.8)
270
+ assert_source 'foo.bar(*args, foo)', RUBIES - %w(1.8)
271
+ assert_source 'foo.bar(foo, *args)'
272
+ assert_source 'foo.bar(foo, *args, &block)'
273
+ assert_source <<-RUBY
274
+ foo(bar, *args)
275
+ RUBY
276
+
277
+ assert_source <<-RUBY
278
+ foo(*args, &block)
279
+ RUBY
280
+
281
+ assert_source 'foo.bar(&baz)'
282
+ assert_source 'foo.bar(:baz, &baz)'
283
+ assert_source 'foo.bar=(:baz)'
284
+ assert_source 'self.foo=(:bar)'
285
+ end
286
+
287
+ context 'begin; end' do
288
+ assert_generates s(:begin), 'nil'
289
+
290
+ assert_source <<-RUBY
291
+ foo
292
+ bar
293
+ RUBY
294
+
295
+ assert_source <<-RUBY
296
+ begin
297
+ foo
298
+ bar
299
+ end.blah
300
+ RUBY
301
+ end
302
+
303
+ context 'begin / rescue / ensure' do
304
+ assert_source <<-RUBY
305
+ begin
306
+ foo
307
+ ensure
308
+ bar
309
+ baz
310
+ end
311
+ RUBY
312
+
313
+ assert_source <<-RUBY
314
+ begin
315
+ foo
316
+ rescue
317
+ baz
318
+ end
319
+ RUBY
320
+
321
+ assert_source <<-RUBY
322
+ begin
323
+ foo
324
+ bar
325
+ rescue
326
+ baz
327
+ bar
328
+ end
329
+ RUBY
330
+
331
+ assert_source <<-RUBY
332
+ begin
333
+ foo
334
+ rescue Exception
335
+ bar
336
+ end
337
+ RUBY
338
+
339
+ assert_source <<-RUBY
340
+ begin
341
+ foo
342
+ rescue => bar
343
+ bar
344
+ end
345
+ RUBY
346
+
347
+ assert_source <<-RUBY
348
+ begin
349
+ foo
350
+ rescue Exception, Other => bar
351
+ bar
352
+ end
353
+ RUBY
354
+
355
+ assert_source <<-RUBY
356
+ begin
357
+ foo
358
+ rescue Exception => bar
359
+ bar
360
+ end
361
+ RUBY
362
+
363
+ assert_source <<-RUBY
364
+ begin
365
+ bar
366
+ rescue SomeError, *bar
367
+ baz
368
+ end
369
+ RUBY
370
+
371
+ assert_source <<-RUBY
372
+ begin
373
+ bar
374
+ rescue SomeError, *bar => exception
375
+ baz
376
+ end
377
+ RUBY
378
+
379
+ assert_source <<-RUBY
380
+ begin
381
+ bar
382
+ rescue *bar
383
+ baz
384
+ end
385
+ RUBY
386
+
387
+ assert_source <<-RUBY
388
+ begin
389
+ bar
390
+ rescue *bar => exception
391
+ baz
392
+ end
393
+ RUBY
394
+ end
395
+
396
+ context 'super' do
397
+ assert_source 'super'
398
+
399
+ assert_source <<-RUBY
400
+ super do
401
+ foo
402
+ end
403
+ RUBY
404
+
405
+ assert_source 'super()'
406
+
407
+ assert_source <<-RUBY
408
+ super() do
409
+ foo
410
+ end
411
+ RUBY
412
+
413
+ assert_source 'super(a)'
414
+
415
+ assert_source <<-RUBY
416
+ super(a) do
417
+ foo
418
+ end
419
+ RUBY
420
+
421
+ assert_source 'super(a, b)'
422
+
423
+ assert_source <<-RUBY
424
+ super(a, b) do
425
+ foo
426
+ end
427
+ RUBY
428
+
429
+ assert_source 'super(&block)'
430
+ assert_source 'super(a, &block)'
431
+ end
432
+
433
+ context 'undef' do
434
+ assert_source 'undef :foo'
435
+ end
436
+
437
+ context 'BEGIN' do
438
+ assert_source <<-RUBY
439
+ BEGIN {
440
+ foo
441
+ }
442
+ RUBY
443
+ end
444
+
445
+ context 'END' do
446
+ assert_source <<-RUBY
447
+ END {
448
+ foo
449
+ }
450
+ RUBY
451
+ end
452
+
453
+ context 'alias' do
454
+ assert_source <<-RUBY
455
+ alias $foo $bar
456
+ RUBY
457
+
458
+ assert_source <<-RUBY
459
+ alias foo bar
460
+ RUBY
461
+ end
462
+
463
+ context 'yield' do
464
+ context 'without arguments' do
465
+ assert_source 'yield'
466
+ end
467
+
468
+ context 'with argument' do
469
+ assert_source 'yield(a)'
470
+ end
471
+
472
+ context 'with arguments' do
473
+ assert_source 'yield(a, b)'
474
+ end
475
+ end
476
+
477
+ context 'if statement' do
478
+ assert_source <<-RUBY
479
+ if /foo/
480
+ bar
481
+ end
482
+ RUBY
483
+
484
+ assert_source <<-RUBY
485
+ if 3
486
+ 9
487
+ end
488
+ RUBY
489
+
490
+ assert_source <<-RUBY
491
+ if 4
492
+ 5
493
+ else
494
+ 6
495
+ end
496
+ RUBY
497
+
498
+ assert_source <<-RUBY
499
+ unless 3
500
+ nil
501
+ end
502
+ RUBY
503
+
504
+ assert_source <<-RUBY
505
+ unless 3
506
+ 9
507
+ end
508
+ RUBY
509
+ end
510
+
511
+ context 'def' do
512
+ context 'on instance' do
513
+
514
+ assert_source <<-RUBY
515
+ def foo
516
+ bar
517
+ end
518
+ RUBY
519
+
520
+ assert_source <<-RUBY
521
+ def foo(bar)
522
+ bar
523
+ end
524
+ RUBY
525
+
526
+ assert_source <<-RUBY
527
+ def foo(bar, baz)
528
+ bar
529
+ end
530
+ RUBY
531
+
532
+ assert_source <<-RUBY
533
+ def foo(bar = true)
534
+ bar
535
+ end
536
+ RUBY
537
+
538
+ assert_source <<-RUBY
539
+ def foo(bar, baz = true)
540
+ bar
541
+ end
542
+ RUBY
543
+
544
+ assert_source <<-RUBY, %w(1.9 2.0)
545
+ def foo(bar, baz = true, foo)
546
+ bar
547
+ end
548
+ RUBY
549
+
550
+ assert_source <<-RUBY
551
+ def foo(*)
552
+ bar
553
+ end
554
+ RUBY
555
+
556
+ assert_source <<-RUBY
557
+ def foo(*bar)
558
+ bar
559
+ end
560
+ RUBY
561
+
562
+ assert_source <<-RUBY
563
+ def foo(bar, *baz)
564
+ bar
565
+ end
566
+ RUBY
567
+
568
+ assert_source <<-RUBY
569
+ def foo(baz = true, *bor)
570
+ bar
571
+ end
572
+ RUBY
573
+
574
+ assert_source <<-RUBY
575
+ def foo(baz = true, *bor, &block)
576
+ bar
577
+ end
578
+ RUBY
579
+
580
+ assert_source <<-RUBY
581
+ def foo(bar, baz = true, *bor)
582
+ bar
583
+ end
584
+ RUBY
585
+
586
+ assert_source <<-RUBY
587
+ def foo(&block)
588
+ bar
589
+ end
590
+ RUBY
591
+
592
+ assert_source <<-RUBY
593
+ def foo(bar, &block)
594
+ bar
595
+ end
596
+ RUBY
597
+
598
+ assert_source <<-RUBY
599
+ def foo
600
+ bar
601
+ baz
602
+ end
603
+ RUBY
604
+
605
+ assert_source <<-RUBY
606
+ def self.foo
607
+ bar
608
+ baz
609
+ end
610
+ RUBY
611
+ end
612
+
613
+ context 'on singleton' do
614
+
615
+ assert_source <<-RUBY
616
+ def self.foo
617
+ bar
618
+ end
619
+ RUBY
620
+
621
+ assert_source <<-RUBY
622
+ def Foo.bar
623
+ bar
624
+ end
625
+ RUBY
626
+
627
+ end
628
+
629
+ context 'class' do
630
+ assert_source <<-RUBY
631
+ class TestClass
632
+
633
+ end
634
+ RUBY
635
+
636
+ assert_source <<-RUBY
637
+ class << some_object
638
+
639
+ end
640
+ RUBY
641
+
642
+ assert_source <<-RUBY
643
+ class << some_object
644
+ the_body
645
+ end
646
+ RUBY
647
+
648
+ assert_source <<-RUBY
649
+ class SomeNameSpace::TestClass
650
+
651
+ end
652
+ RUBY
653
+
654
+ assert_source <<-RUBY
655
+ class Some::Name::Space::TestClass
656
+
657
+ end
658
+ RUBY
659
+
660
+ assert_source <<-RUBY
661
+ class TestClass < Object
662
+
663
+ end
664
+ RUBY
665
+
666
+ assert_source <<-RUBY
667
+ class TestClass < SomeNameSpace::Object
668
+
669
+ end
670
+ RUBY
671
+
672
+ assert_source <<-RUBY
673
+ class TestClass
674
+ def foo
675
+ :bar
676
+ end
677
+ end
678
+ RUBY
679
+
680
+ assert_source <<-RUBY
681
+ class ::TestClass
682
+
683
+ end
684
+ RUBY
685
+ end
686
+
687
+ context 'module' do
688
+
689
+ assert_source <<-RUBY
690
+ module TestModule
691
+
692
+ end
693
+ RUBY
694
+
695
+ assert_source <<-RUBY
696
+ module SomeNameSpace::TestModule
697
+
698
+ end
699
+ RUBY
700
+
701
+ assert_source <<-RUBY
702
+ module Some::Name::Space::TestModule
703
+
704
+ end
705
+ RUBY
706
+
707
+ assert_source <<-RUBY
708
+ module TestModule
709
+ def foo
710
+ :bar
711
+ end
712
+ end
713
+ RUBY
714
+
715
+ end
716
+
717
+ context 'op assign' do
718
+ %w(|= ||= &= &&= += -= *= /= **= %=).each do |op|
719
+ assert_source "self.foo #{op} bar"
720
+ assert_source "foo[key] #{op} bar"
721
+ end
722
+ end
723
+
724
+ context 'element assignment' do
725
+ assert_source 'array[index] = value'
726
+ assert_source 'array[*index] = value'
727
+
728
+ %w(+ - * / % & | || &&).each do |operator|
729
+ context "with #{operator}" do
730
+ assert_source "array[index] #{operator}= 2"
731
+ assert_source "array[] #{operator}= 2"
732
+ end
733
+ end
734
+ end
735
+
736
+ context 'defined?' do
737
+ assert_source <<-RUBY
738
+ defined?(@foo)
739
+ RUBY
740
+
741
+ assert_source <<-RUBY
742
+ defined?(Foo)
743
+ RUBY
744
+ end
745
+ end
746
+
747
+ context 'lambda' do
748
+ assert_source <<-RUBY
749
+ lambda do |a, b|
750
+ a
751
+ end
752
+ RUBY
753
+ end
754
+
755
+ context 'match operators' do
756
+ assert_source <<-RUBY
757
+ /bar/ =~ foo
758
+ RUBY
759
+
760
+ assert_source <<-RUBY
761
+ foo =~ /bar/
762
+ RUBY
763
+ end
764
+
765
+ context 'binary operators methods' do
766
+ %w(+ - * / & | << >> == === != <= < <=> > >= =~ !~ ^ **).each do |operator|
767
+ rubies = RUBIES - (%w(!= !~).include?(operator) ? %w(1.8) : [])
768
+ assert_source "1 #{operator} 2", rubies
769
+ assert_source "left.#{operator}(*foo)", rubies
770
+ assert_source "left.#{operator}(a, b)", rubies
771
+ assert_source "self #{operator} b", rubies
772
+ assert_source "a #{operator} b", rubies
773
+ assert_source "(a #{operator} b).foo", rubies
774
+ end
775
+ end
776
+
777
+ context 'binary operator' do
778
+ assert_source '((a) || (break(foo)))'
779
+ assert_source '((break(foo)) || (a))'
780
+ assert_source '((a) || (b)).foo'
781
+ assert_source '((a) || (((b) || (c))))'
782
+ end
783
+
784
+ { :or => :'||', :and => :'&&' }.each do |word, symbol|
785
+ assert_generates "a #{word} break foo", "((a) #{symbol} (break(foo)))"
786
+ end
787
+
788
+ context 'expansion of shortcuts' do
789
+ assert_source 'a += 2'
790
+ assert_source 'a -= 2'
791
+ assert_source 'a **= 2'
792
+ assert_source 'a *= 2'
793
+ assert_source 'a /= 2'
794
+ end
795
+
796
+ context 'shortcuts' do
797
+ assert_source 'a &&= b'
798
+ assert_source 'a ||= 2'
799
+ assert_source '(a ||= 2).bar'
800
+ end
801
+
802
+ #context 'flip flops' do
803
+ # context 'flip2' do
804
+ # assert_source <<-RUBY
805
+ # if (((i) == (4)))..(((i) == (4)))
806
+ # foo
807
+ # end
808
+ # RUBY
809
+ # end
810
+
811
+ # context 'flip3' do
812
+ # assert_source <<-RUBY
813
+ # if (((i) == (4)))...(((i) == (4)))
814
+ # foo
815
+ # end
816
+ # RUBY
817
+ # end
818
+ #end
819
+
820
+ context 'case statement' do
821
+ assert_source <<-RUBY
822
+ case
823
+ when bar
824
+ baz
825
+ when baz
826
+ bar
827
+ end
828
+ RUBY
829
+
830
+ assert_source <<-RUBY
831
+ case foo
832
+ when bar
833
+ baz
834
+ when baz
835
+ bar
836
+ end
837
+ RUBY
838
+
839
+ assert_source <<-RUBY
840
+ case foo
841
+ when bar, baz
842
+ :other
843
+ end
844
+ RUBY
845
+
846
+ assert_source <<-RUBY
847
+ case foo
848
+ when *bar
849
+ :value
850
+ end
851
+ RUBY
852
+
853
+ assert_source <<-RUBY
854
+ case foo
855
+ when bar
856
+ baz
857
+ else
858
+ :foo
859
+ end
860
+ RUBY
861
+ end
862
+
863
+ context 'for' do
864
+ assert_source <<-RUBY
865
+ for a in bar do
866
+ baz
867
+ end
868
+ RUBY
869
+
870
+ assert_source <<-RUBY
871
+ for a, *b in bar do
872
+ baz
873
+ end
874
+ RUBY
875
+
876
+ assert_source <<-RUBY
877
+ for a, b in bar do
878
+ baz
879
+ end
880
+ RUBY
881
+ end
882
+
883
+ context 'unary operators' do
884
+ assert_source '!1'
885
+ assert_source '!!1'
886
+ assert_source '~a'
887
+ assert_source '-a'
888
+ assert_source '+a'
889
+ end
890
+
891
+ context 'loop' do
892
+ assert_source <<-RUBY
893
+ loop do
894
+ foo
895
+ end
896
+ RUBY
897
+ end
898
+
899
+ context 'while' do
900
+ assert_source <<-RUBY
901
+ while false
902
+ 3
903
+ end
904
+ RUBY
905
+ end
906
+
907
+ context 'until' do
908
+ assert_source <<-RUBY
909
+ until false
910
+ 3
911
+ end
912
+ RUBY
913
+ end
914
+ end