unparser 0.0.16 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a584013dc79bab624a49c0ce4e8b788d8b62412
4
- data.tar.gz: b43573081cef30e216c7ee0caf856d51b97b20f2
3
+ metadata.gz: e2fb94d62a7e99d2533e6507b8190ee433449b64
4
+ data.tar.gz: 6f3c81cd1f8a171c37ad49c4d1a135d4c8f2c49b
5
5
  SHA512:
6
- metadata.gz: 34c5b966271853e563b7f3b9d6126aff72234e85f32ba3358bdc3dab4530d9a3041762ae94c184e88f191511bcd4e85e16ac09c3006e9f9011ad4cec9d4a9c56
7
- data.tar.gz: ea0188da6b1b3311dbe36363b092905b336b4555c9536301f83f7b6406553b06f6cf7aba089230117b0819f356d6f97f65381a41650c29ebbce94e85e1c71eca
6
+ metadata.gz: 2a2a1ea342bee0e438770a92969087574aa086dd0ddce040b0e51d29fafefd7898b38b59661d7f71af297cb806b70c743ed4ff640cca0e66384e469caa120f51
7
+ data.tar.gz: fb25c1000ec5f4719a149ad85b260b2274d8bc87a5f1b55f0103f8e8be12a4a176ab89a74f4be514dd9116d3e2c6dd084b3b4f3ed4e3782d5804a5a329fe9749
data/Gemfile.devtools CHANGED
@@ -15,7 +15,7 @@ group :guard do
15
15
  gem 'guard-bundler', '~> 1.0.0'
16
16
  gem 'guard-rspec', '~> 3.0.2'
17
17
  gem 'guard-rubocop', '~> 0.2.0'
18
- gem 'guard-mutant', '~> 0.0.1'
18
+ # gem 'guard-mutant', '~> 0.0.1'
19
19
 
20
20
  # file system change event handling
21
21
  gem 'listen', '~> 1.3.0'
@@ -39,7 +39,7 @@ group :metrics do
39
39
  gem 'yardstick', '~> 0.9.7', git: 'https://github.com/dkubb/yardstick.git'
40
40
 
41
41
  platforms :ruby_19, :ruby_20 do
42
- gem 'mutant', git: 'https://github.com/mbj/mutant.git'
42
+ # gem 'mutant', git: 'https://github.com/mbj/mutant.git'
43
43
  gem 'yard-spellcheck', '~> 0.1.5'
44
44
  end
45
45
  end
@@ -74,7 +74,10 @@ module Unparser
74
74
  # @api private
75
75
  #
76
76
  def emit_left
77
- visit(base) if base
77
+ if base
78
+ visit(base)
79
+ write(T_DCL)
80
+ end
78
81
  write(name.to_s)
79
82
  end
80
83
 
@@ -0,0 +1,1092 @@
1
+ require 'spec_helper'
2
+
3
+ describe Unparser do
4
+ describe '.unparse' do
5
+
6
+ PARSERS = IceNine.deep_freeze(
7
+ '1.8' => Parser::Ruby18,
8
+ '1.9' => Parser::Ruby19,
9
+ '2.0' => Parser::Ruby20,
10
+ '2.1' => Parser::Ruby21
11
+ )
12
+
13
+ RUBIES = PARSERS.keys.freeze
14
+
15
+ def self.parser_for_ruby_version(version)
16
+ PARSERS.fetch(version) do
17
+ raise "Unrecognized Ruby version #{version}"
18
+ end
19
+ end
20
+
21
+ def self.with_versions(versions)
22
+ versions.each do |version|
23
+ parser = parser_for_ruby_version(version)
24
+ yield version, parser
25
+ end
26
+ end
27
+
28
+ def self.strip(ruby)
29
+ lines = ruby.lines
30
+ line = lines.first
31
+ match = /\A[ ]*/.match(line)
32
+ length = match[0].length
33
+ source = lines.map do |line|
34
+ line[(length..-1)]
35
+ end.join
36
+ source.chomp
37
+ end
38
+
39
+ def assert_round_trip(input, parser)
40
+ ast = parser.parse(input)
41
+ generated = Unparser.unparse(ast)
42
+ generated.should eql(input)
43
+ end
44
+
45
+ def self.assert_generates_one_way(ast, expected, versions = RUBIES)
46
+ with_versions(versions) do |version, parser|
47
+ it "should generate #{ast.inspect} as #{expected} under #{version}" do
48
+ if ast.kind_of?(String)
49
+ ast = parser.parse(ast)
50
+ end
51
+ generated = Unparser.unparse(ast)
52
+ generated.should eql(expected)
53
+ end
54
+ end
55
+ end
56
+
57
+ def self.assert_generates(ast, expected, versions = RUBIES)
58
+ with_versions(versions) do |version, parser|
59
+ it "should generate #{ast.inspect} as #{expected} under #{version}" do
60
+ if ast.kind_of?(String)
61
+ ast = parser.parse(ast)
62
+ end
63
+ generated = Unparser.unparse(ast)
64
+ generated.should eql(expected)
65
+ ast = parser.parse(generated)
66
+ Unparser.unparse(ast).should eql(expected)
67
+ end
68
+ end
69
+ end
70
+
71
+ def self.assert_round_trip(input, versions = RUBIES)
72
+ with_versions(versions) do |version, parser|
73
+ it "should round trip #{input.inspect} under #{version}" do
74
+ assert_round_trip(input, parser)
75
+ end
76
+ end
77
+ end
78
+
79
+ def self.assert_source(input, versions = RUBIES)
80
+ assert_round_trip(strip(input), versions)
81
+ end
82
+
83
+ context 'literal' do
84
+ context 'fixnum' do
85
+ assert_generates s(:int, 1), '1'
86
+ assert_generates s(:int, -1), '-1'
87
+ assert_source '1'
88
+ assert_generates '0x1', '1'
89
+ assert_generates '1_000', '1000'
90
+ assert_generates '1e10', '10000000000.0'
91
+ assert_generates '?c', '"c"', RUBIES - %w(1.8)
92
+ assert_generates '?c', '99', %w(1.8)
93
+ end
94
+
95
+ context 'string' do
96
+ assert_generates %q("foo" "bar"), %q("foobar")
97
+ assert_generates %q(%Q(foo"#{@bar})), %q("foo\"#{@bar}")
98
+ assert_source %q("\"")
99
+ assert_source %q("foo#{1}bar")
100
+ assert_source %q("\"#{@a}")
101
+ end
102
+
103
+ context 'execute string' do
104
+ assert_source '`foo`'
105
+ assert_source '`foo#{@bar}`'
106
+ assert_generates '%x(\))', '`)`'
107
+ #assert_generates '%x(`)', '`\``'
108
+ assert_source '`"`'
109
+ end
110
+
111
+ context 'symbol' do
112
+ assert_generates s(:sym, :foo), ':foo'
113
+ assert_generates s(:sym, :"A B"), ':"A B"'
114
+ assert_source ':foo'
115
+ assert_source ':"A B"'
116
+ assert_source ':"A\"B"'
117
+ end
118
+
119
+ context 'regexp' do
120
+ assert_source '/foo/'
121
+ assert_source %q(/[^-+',.\/:@[:alnum:]\[\]\x80-\xff]+/)
122
+ assert_source '/foo#{@bar}/'
123
+ assert_source '/foo#{@bar}/imx'
124
+ assert_source "/\n/"
125
+ assert_source '/\n/'
126
+ assert_source "/\n/x"
127
+ assert_generates '%r(/)', '/\//'
128
+ assert_generates '%r(\))', '/)/'
129
+ assert_generates '%r(#{@bar}baz)', '/#{@bar}baz/'
130
+ end
131
+
132
+ context 'dynamic string' do
133
+ assert_source %q("foo#{@bar}")
134
+ assert_source %q("fo\no#{bar}b\naz")
135
+ end
136
+
137
+ context 'dynamic symbol' do
138
+ assert_source ':"foo#{bar}baz"'
139
+ assert_source ':"fo\no#{bar}b\naz"'
140
+ assert_source ':"#{bar}foo"'
141
+ assert_source ':"foo#{bar}"'
142
+ end
143
+
144
+ context 'irange' do
145
+ assert_generates '1..2', %q(1..2)
146
+ assert_source '(0.0 / 0.0)..1'
147
+ assert_source '1..(0.0 / 0.0)'
148
+ end
149
+
150
+ context 'erange' do
151
+ assert_generates '1...2', %q(1...2)
152
+ end
153
+
154
+ context 'float' do
155
+ assert_source '-0.1'
156
+ assert_source '0.1'
157
+ assert_generates s(:float, -0.1), '-0.1'
158
+ assert_generates s(:float, 0.1), '0.1'
159
+ end
160
+
161
+ context 'array' do
162
+ assert_source '[1, 2]'
163
+ assert_source '[1]'
164
+ assert_source '[]'
165
+ assert_source '[1, *@foo]'
166
+ assert_source '[*@foo, 1]', RUBIES - %w(1.8)
167
+ assert_source '[*@foo, *@baz]', RUBIES - %w(1.8)
168
+ assert_generates '%w(foo bar)', %q(["foo", "bar"])
169
+ end
170
+
171
+ context 'hash' do
172
+ assert_source '{}'
173
+ assert_source '{1 => 2}'
174
+ assert_source '{1 => 2, 3 => 4}'
175
+ end
176
+ end
177
+
178
+ context 'access' do
179
+ assert_source '@a'
180
+ assert_source '@@a'
181
+ assert_source '$a'
182
+ assert_source '$1'
183
+ assert_source '$`'
184
+ assert_source 'CONST'
185
+ assert_source 'SCOPED::CONST'
186
+ assert_source '::TOPLEVEL'
187
+ assert_source '::TOPLEVEL::CONST'
188
+ end
189
+
190
+ context 'break' do
191
+ assert_source 'break'
192
+ assert_source 'break(a)'
193
+ end
194
+
195
+ context 'next' do
196
+ assert_source 'next'
197
+ assert_source 'next(bar)'
198
+ end
199
+
200
+ context 'retry' do
201
+ assert_source 'retry'
202
+ end
203
+
204
+ context 'redo' do
205
+ assert_source 'redo'
206
+ end
207
+
208
+ context 'singletons' do
209
+ assert_source 'self'
210
+ assert_source 'true'
211
+ assert_source 'false'
212
+ assert_source 'nil'
213
+ end
214
+
215
+ context 'magic keywords' do
216
+ assert_generates '__ENCODING__', 'Encoding::UTF_8', RUBIES - %w(1.8)
217
+ assert_source '__FILE__'
218
+ assert_source '__LINE__'
219
+ end
220
+
221
+ context 'assignment' do
222
+ context 'single' do
223
+ assert_source 'a = 1'
224
+ assert_source '@a = 1'
225
+ assert_source '@@a = 1'
226
+ assert_source '$a = 1'
227
+ assert_source 'CONST = 1'
228
+ assert_source 'Name::Spaced::CONST = 1'
229
+ end
230
+
231
+ context 'multiple' do
232
+ assert_source 'a, b = 1, 2'
233
+ assert_source 'a, *foo = 1, 2'
234
+ assert_source 'a, * = 1, 2'
235
+ assert_source '*foo = 1, 2'
236
+ assert_source '@a, @b = 1, 2'
237
+ assert_source 'a.foo, a.bar = 1, 2'
238
+ assert_source 'a[0, 2]'
239
+ assert_source 'a[0], a[1] = 1, 2'
240
+ assert_source 'a[*foo], a[1] = 1, 2'
241
+ assert_source '@@a, @@b = 1, 2'
242
+ assert_source '$a, $b = 1, 2'
243
+ assert_source 'a, b = foo'
244
+ assert_source 'a, (b, c) = 1, [2, 3]'
245
+ assert_generates_one_way s(:mlhs, s(:lvasgn, :a), s(:lvasgn, :b)), 'a, b'
246
+ end
247
+ end
248
+
249
+ context 'return' do
250
+ assert_source <<-RUBY
251
+ return
252
+ RUBY
253
+
254
+ assert_source <<-RUBY
255
+ return(1)
256
+ RUBY
257
+ end
258
+
259
+ context 'send' do
260
+ assert_source 'foo'
261
+ assert_source 'self.foo'
262
+ assert_source 'a.foo'
263
+ assert_source 'A.foo'
264
+ assert_source 'foo[1]'
265
+ assert_source 'foo[*baz]'
266
+ assert_source 'foo(1)'
267
+ assert_source 'foo(bar)'
268
+ assert_source 'foo(&block)'
269
+ assert_source 'foo(*arguments)'
270
+ assert_source 'foo(*arguments)'
271
+ assert_source <<-RUBY
272
+ foo do
273
+ end
274
+ RUBY
275
+
276
+ assert_source <<-RUBY
277
+ foo(1) do
278
+ nil
279
+ end
280
+ RUBY
281
+
282
+ assert_source <<-RUBY
283
+ foo do |a, b|
284
+ nil
285
+ end
286
+ RUBY
287
+
288
+ assert_source <<-RUBY
289
+ foo do |a, *b|
290
+ nil
291
+ end
292
+ RUBY
293
+
294
+ assert_source <<-RUBY
295
+ foo do |a, *|
296
+ nil
297
+ end
298
+ RUBY
299
+
300
+ assert_source <<-RUBY
301
+ foo do
302
+ bar
303
+ end
304
+ RUBY
305
+
306
+ assert_source <<-RUBY
307
+ foo.bar(*args)
308
+ RUBY
309
+
310
+ assert_source <<-RUBY
311
+ foo.bar do |(a, b), c|
312
+ d
313
+ end
314
+ RUBY
315
+
316
+ assert_source <<-RUBY
317
+ foo.bar do |(a, b)|
318
+ d
319
+ end
320
+ RUBY
321
+
322
+ # Special cases
323
+ assert_source '(1..2).max'
324
+
325
+ assert_source 'foo.bar(*args)'
326
+ assert_source 'foo.bar(*arga, foo, *argb)', RUBIES - %w(1.8)
327
+ assert_source 'foo.bar(*args, foo)', RUBIES - %w(1.8)
328
+ assert_source 'foo.bar(foo, *args)'
329
+ assert_source 'foo.bar(foo, *args, &block)'
330
+ assert_source <<-RUBY
331
+ foo(bar, *args)
332
+ RUBY
333
+
334
+ assert_source <<-RUBY
335
+ foo(*args, &block)
336
+ RUBY
337
+
338
+ assert_source 'foo.bar(&baz)'
339
+ assert_source 'foo.bar(:baz, &baz)'
340
+ assert_source 'foo.bar=(:baz)'
341
+ assert_source 'self.foo=(:bar)'
342
+ end
343
+
344
+ context 'begin; end' do
345
+ assert_generates s(:begin), ''
346
+
347
+ assert_source <<-RUBY
348
+ foo
349
+ bar
350
+ RUBY
351
+
352
+ assert_source <<-RUBY
353
+ begin
354
+ foo
355
+ bar
356
+ end.blah
357
+ RUBY
358
+ end
359
+
360
+ context 'begin / rescue / ensure' do
361
+ assert_source <<-RUBY
362
+ begin
363
+ foo
364
+ ensure
365
+ bar
366
+ baz
367
+ end
368
+ RUBY
369
+
370
+ assert_source <<-RUBY
371
+ begin
372
+ foo
373
+ rescue
374
+ baz
375
+ end
376
+ RUBY
377
+
378
+ assert_source <<-RUBY
379
+ begin
380
+ begin
381
+ foo
382
+ bar
383
+ rescue
384
+ end
385
+ rescue
386
+ baz
387
+ bar
388
+ end
389
+ RUBY
390
+
391
+ assert_source <<-RUBY
392
+ begin
393
+ foo
394
+ bar
395
+ rescue
396
+ baz
397
+ bar
398
+ end
399
+ RUBY
400
+
401
+ assert_source <<-RUBY
402
+ begin
403
+ foo
404
+ rescue Exception
405
+ bar
406
+ end
407
+ RUBY
408
+
409
+ assert_source <<-RUBY
410
+ begin
411
+ foo
412
+ rescue => bar
413
+ bar
414
+ end
415
+ RUBY
416
+
417
+ assert_source <<-RUBY
418
+ begin
419
+ foo
420
+ rescue Exception, Other => bar
421
+ bar
422
+ end
423
+ RUBY
424
+
425
+ assert_source <<-RUBY
426
+ begin
427
+ rescue Exception => e
428
+ end
429
+ RUBY
430
+
431
+ assert_source <<-RUBY
432
+ begin
433
+ rescue
434
+ ensure
435
+ end
436
+ RUBY
437
+
438
+ assert_source <<-RUBY
439
+ begin
440
+ foo
441
+ rescue Exception => bar
442
+ bar
443
+ end
444
+ RUBY
445
+
446
+ assert_source <<-RUBY
447
+ begin
448
+ bar
449
+ rescue SomeError, *bar
450
+ baz
451
+ end
452
+ RUBY
453
+
454
+ assert_source <<-RUBY
455
+ begin
456
+ bar
457
+ rescue SomeError, *bar => exception
458
+ baz
459
+ end
460
+ RUBY
461
+
462
+ assert_source <<-RUBY
463
+ begin
464
+ bar
465
+ rescue *bar
466
+ baz
467
+ end
468
+ RUBY
469
+
470
+ assert_source <<-RUBY
471
+ begin
472
+ bar
473
+ rescue LoadError
474
+ end
475
+ RUBY
476
+
477
+ assert_source <<-RUBY
478
+ begin
479
+ bar
480
+ rescue
481
+ else
482
+ baz
483
+ end
484
+ RUBY
485
+
486
+ assert_source <<-RUBY
487
+ begin
488
+ bar
489
+ rescue *bar => exception
490
+ baz
491
+ end
492
+ RUBY
493
+ end
494
+
495
+ context 'super' do
496
+ assert_source 'super'
497
+
498
+ assert_source <<-RUBY
499
+ super do
500
+ foo
501
+ end
502
+ RUBY
503
+
504
+ assert_source 'super()'
505
+
506
+ assert_source <<-RUBY
507
+ super() do
508
+ foo
509
+ end
510
+ RUBY
511
+
512
+ assert_source 'super(a)'
513
+
514
+ assert_source <<-RUBY
515
+ super(a) do
516
+ foo
517
+ end
518
+ RUBY
519
+
520
+ assert_source 'super(a, b)'
521
+
522
+ assert_source <<-RUBY
523
+ super(a, b) do
524
+ foo
525
+ end
526
+ RUBY
527
+
528
+ assert_source 'super(&block)'
529
+ assert_source 'super(a, &block)'
530
+ end
531
+
532
+ context 'undef' do
533
+ assert_source 'undef :foo'
534
+ end
535
+
536
+ context 'BEGIN' do
537
+ assert_source <<-RUBY
538
+ BEGIN {
539
+ foo
540
+ }
541
+ RUBY
542
+ end
543
+
544
+ context 'END' do
545
+ assert_source <<-RUBY
546
+ END {
547
+ foo
548
+ }
549
+ RUBY
550
+ end
551
+
552
+ context 'alias' do
553
+ assert_source <<-RUBY
554
+ alias $foo $bar
555
+ RUBY
556
+
557
+ assert_source <<-RUBY
558
+ alias :foo :bar
559
+ RUBY
560
+ end
561
+
562
+ context 'yield' do
563
+ context 'without arguments' do
564
+ assert_source 'yield'
565
+ end
566
+
567
+ context 'with argument' do
568
+ assert_source 'yield(a)'
569
+ end
570
+
571
+ context 'with arguments' do
572
+ assert_source 'yield(a, b)'
573
+ end
574
+ end
575
+
576
+ context 'if statement' do
577
+ assert_source <<-RUBY
578
+ if /foo/
579
+ bar
580
+ end
581
+ RUBY
582
+
583
+ assert_source <<-RUBY
584
+ if 3
585
+ 9
586
+ end
587
+ RUBY
588
+
589
+ assert_source <<-RUBY
590
+ if 4
591
+ 5
592
+ else
593
+ 6
594
+ end
595
+ RUBY
596
+
597
+ assert_source <<-RUBY
598
+ unless 3
599
+ nil
600
+ end
601
+ RUBY
602
+
603
+ assert_source <<-RUBY
604
+ unless 3
605
+ 9
606
+ end
607
+ RUBY
608
+ end
609
+
610
+ context 'def' do
611
+ context 'on instance' do
612
+
613
+ assert_source <<-RUBY
614
+ def foo
615
+ end
616
+ RUBY
617
+
618
+ assert_source <<-RUBY
619
+ def foo
620
+ bar
621
+ end
622
+ RUBY
623
+
624
+ assert_source <<-RUBY
625
+ def foo
626
+ bar
627
+ ensure
628
+ baz
629
+ end
630
+ RUBY
631
+
632
+ assert_source <<-RUBY
633
+ def foo
634
+ bar
635
+ rescue
636
+ baz
637
+ end
638
+ RUBY
639
+
640
+ assert_source <<-RUBY
641
+ def foo(bar)
642
+ bar
643
+ end
644
+ RUBY
645
+
646
+ assert_source <<-RUBY
647
+ def foo(bar, baz)
648
+ bar
649
+ end
650
+ RUBY
651
+
652
+ assert_source <<-RUBY
653
+ def foo(bar = true)
654
+ bar
655
+ end
656
+ RUBY
657
+
658
+ assert_source <<-RUBY
659
+ def foo(bar, baz = true)
660
+ bar
661
+ end
662
+ RUBY
663
+
664
+ assert_source <<-RUBY, %w(1.9 2.0)
665
+ def foo(bar, baz = true, foo)
666
+ bar
667
+ end
668
+ RUBY
669
+
670
+ assert_source <<-RUBY, %w(2.1)
671
+ def foo(bar: 1)
672
+ end
673
+ RUBY
674
+
675
+ assert_source <<-RUBY, %w(2.0)
676
+ def foo(**bar)
677
+ end
678
+ RUBY
679
+
680
+ assert_source <<-RUBY
681
+ def foo(*)
682
+ bar
683
+ end
684
+ RUBY
685
+
686
+ assert_source <<-RUBY
687
+ def foo(*bar)
688
+ bar
689
+ end
690
+ RUBY
691
+
692
+ assert_source <<-RUBY
693
+ def foo(bar, *baz)
694
+ bar
695
+ end
696
+ RUBY
697
+
698
+ assert_source <<-RUBY
699
+ def foo(baz = true, *bor)
700
+ bar
701
+ end
702
+ RUBY
703
+
704
+ assert_source <<-RUBY
705
+ def foo(baz = true, *bor, &block)
706
+ bar
707
+ end
708
+ RUBY
709
+
710
+ assert_source <<-RUBY
711
+ def foo(bar, baz = true, *bor)
712
+ bar
713
+ end
714
+ RUBY
715
+
716
+ assert_source <<-RUBY
717
+ def foo(&block)
718
+ bar
719
+ end
720
+ RUBY
721
+
722
+ assert_source <<-RUBY
723
+ def foo(bar, &block)
724
+ bar
725
+ end
726
+ RUBY
727
+
728
+ assert_source <<-RUBY
729
+ def foo
730
+ bar
731
+ baz
732
+ end
733
+ RUBY
734
+ end
735
+
736
+ context 'on singleton' do
737
+ assert_source <<-RUBY
738
+ def self.foo
739
+ end
740
+ RUBY
741
+
742
+
743
+ assert_source <<-RUBY
744
+ def self.foo
745
+ bar
746
+ end
747
+ RUBY
748
+
749
+ assert_source <<-RUBY
750
+ def self.foo
751
+ bar
752
+ baz
753
+ end
754
+ RUBY
755
+
756
+ assert_source <<-RUBY
757
+ def Foo.bar
758
+ bar
759
+ end
760
+ RUBY
761
+
762
+ end
763
+
764
+ context 'class' do
765
+ assert_source <<-RUBY
766
+ class TestClass
767
+ end
768
+ RUBY
769
+
770
+ assert_source <<-RUBY
771
+ class << some_object
772
+ end
773
+ RUBY
774
+
775
+ assert_source <<-RUBY
776
+ class << some_object
777
+ the_body
778
+ end
779
+ RUBY
780
+
781
+ assert_source <<-RUBY
782
+ class SomeNameSpace::TestClass
783
+ end
784
+ RUBY
785
+
786
+ assert_source <<-RUBY
787
+ class Some::Name::Space::TestClass
788
+ end
789
+ RUBY
790
+
791
+ assert_source <<-RUBY
792
+ class TestClass < Object
793
+ end
794
+ RUBY
795
+
796
+ assert_source <<-RUBY
797
+ class TestClass < SomeNameSpace::Object
798
+ end
799
+ RUBY
800
+
801
+ assert_source <<-RUBY
802
+ class TestClass
803
+ def foo
804
+ :bar
805
+ end
806
+ end
807
+ RUBY
808
+
809
+ assert_source <<-RUBY
810
+ class ::TestClass
811
+ end
812
+ RUBY
813
+ end
814
+
815
+ context 'module' do
816
+
817
+ assert_source <<-RUBY
818
+ module TestModule
819
+ end
820
+ RUBY
821
+
822
+ assert_source <<-RUBY
823
+ module SomeNameSpace::TestModule
824
+ end
825
+ RUBY
826
+
827
+ assert_source <<-RUBY
828
+ module Some::Name::Space::TestModule
829
+ end
830
+ RUBY
831
+
832
+ assert_source <<-RUBY
833
+ module TestModule
834
+ def foo
835
+ :bar
836
+ end
837
+ end
838
+ RUBY
839
+
840
+ end
841
+
842
+ context 'op assign' do
843
+ %w(|= ||= &= &&= += -= *= /= **= %=).each do |op|
844
+ assert_source "self.foo #{op} bar"
845
+ assert_source "foo[key] #{op} bar"
846
+ end
847
+ end
848
+
849
+ context 'element assignment' do
850
+ assert_source 'array[index] = value'
851
+ assert_source 'array[*index] = value'
852
+
853
+ %w(+ - * / % & | || &&).each do |operator|
854
+ context "with #{operator}" do
855
+ assert_source "array[index] #{operator}= 2"
856
+ assert_source "array[] #{operator}= 2"
857
+ end
858
+ end
859
+ end
860
+
861
+ context 'defined?' do
862
+ assert_source <<-RUBY
863
+ defined?(@foo)
864
+ RUBY
865
+
866
+ assert_source <<-RUBY
867
+ defined?(Foo)
868
+ RUBY
869
+ end
870
+ end
871
+
872
+ context 'lambda' do
873
+ assert_source <<-RUBY
874
+ lambda do
875
+ end
876
+ RUBY
877
+
878
+ assert_source <<-RUBY
879
+ lambda do |a, b|
880
+ a
881
+ end
882
+ RUBY
883
+ end
884
+
885
+ context 'match operators' do
886
+ assert_source <<-RUBY
887
+ /bar/ =~ foo
888
+ RUBY
889
+
890
+ assert_source <<-RUBY
891
+ foo =~ /bar/
892
+ RUBY
893
+ end
894
+
895
+ context 'binary operator methods' do
896
+ %w(+ - * / & | << >> == === != <= < <=> > >= =~ !~ ^ **).each do |operator|
897
+ rubies = RUBIES - (%w(!= !~).include?(operator) ? %w(1.8) : [])
898
+ assert_source "1 #{operator} 2", rubies
899
+ assert_source "left.#{operator}(*foo)", rubies
900
+ assert_source "left.#{operator}(a, b)", rubies
901
+ assert_source "self #{operator} b", rubies
902
+ assert_source "a #{operator} b", rubies
903
+ assert_source "(a #{operator} b).foo", rubies
904
+ end
905
+ end
906
+
907
+ context 'nested binary operators' do
908
+ assert_source '(a + b) / (c - d)'
909
+ assert_source '(a + b) / (c.-(e, f))'
910
+ assert_source '(a + b) / (c.-(*f))'
911
+ end
912
+
913
+ context 'binary operator' do
914
+ assert_source '((a) || (break(foo)))'
915
+ assert_source '((break(foo)) || (a))'
916
+ assert_source '((a) || (b)).foo'
917
+ assert_source '((a) || (((b) || (c))))'
918
+ end
919
+
920
+ { :or => :'||', :and => :'&&' }.each do |word, symbol|
921
+ assert_generates "a #{word} break foo", "((a) #{symbol} (break(foo)))"
922
+ end
923
+
924
+ context 'expansion of shortcuts' do
925
+ assert_source 'a += 2'
926
+ assert_source 'a -= 2'
927
+ assert_source 'a **= 2'
928
+ assert_source 'a *= 2'
929
+ assert_source 'a /= 2'
930
+ end
931
+
932
+ context 'shortcuts' do
933
+ assert_source 'a &&= b'
934
+ assert_source 'a ||= 2'
935
+ assert_source '(a ||= 2).bar'
936
+ end
937
+
938
+ context 'flip flops' do
939
+ context 'inclusive' do
940
+ assert_source <<-RUBY
941
+ if (i == 4)..(i == 4)
942
+ foo
943
+ end
944
+ RUBY
945
+ end
946
+
947
+ context 'exclusive' do
948
+ assert_source <<-RUBY
949
+ if (i == 4)...(i == 4)
950
+ foo
951
+ end
952
+ RUBY
953
+ end
954
+ end
955
+
956
+ context 'case statement' do
957
+ assert_source <<-RUBY
958
+ case
959
+ when bar
960
+ baz
961
+ when baz
962
+ bar
963
+ end
964
+ RUBY
965
+
966
+ assert_source <<-RUBY
967
+ case foo
968
+ when bar
969
+ when baz
970
+ bar
971
+ end
972
+ RUBY
973
+
974
+
975
+ assert_source <<-RUBY
976
+ case foo
977
+ when bar
978
+ baz
979
+ when baz
980
+ bar
981
+ end
982
+ RUBY
983
+
984
+ assert_source <<-RUBY
985
+ case foo
986
+ when bar, baz
987
+ :other
988
+ end
989
+ RUBY
990
+
991
+ assert_source <<-RUBY
992
+ case foo
993
+ when *bar
994
+ :value
995
+ end
996
+ RUBY
997
+
998
+ assert_source <<-RUBY
999
+ case foo
1000
+ when bar
1001
+ baz
1002
+ else
1003
+ :foo
1004
+ end
1005
+ RUBY
1006
+ end
1007
+
1008
+ context 'for' do
1009
+ assert_source <<-RUBY
1010
+ for a in bar do
1011
+ baz
1012
+ end
1013
+ RUBY
1014
+
1015
+ assert_source <<-RUBY
1016
+ for a, *b in bar do
1017
+ baz
1018
+ end
1019
+ RUBY
1020
+
1021
+ assert_source <<-RUBY
1022
+ for a, b in bar do
1023
+ baz
1024
+ end
1025
+ RUBY
1026
+ end
1027
+
1028
+ context 'unary operators' do
1029
+ assert_source '!1'
1030
+ assert_source '!!1'
1031
+ assert_source '~a'
1032
+ assert_source '-a'
1033
+ assert_source '+a'
1034
+ end
1035
+
1036
+ context 'loop' do
1037
+ assert_source <<-RUBY
1038
+ loop do
1039
+ foo
1040
+ end
1041
+ RUBY
1042
+ end
1043
+
1044
+ context 'post conditions' do
1045
+ assert_source <<-RUBY
1046
+ begin
1047
+ foo
1048
+ end while baz
1049
+ RUBY
1050
+
1051
+ assert_source <<-RUBY
1052
+ begin
1053
+ foo
1054
+ bar
1055
+ end until baz
1056
+ RUBY
1057
+
1058
+ assert_source <<-RUBY
1059
+ begin
1060
+ foo
1061
+ bar
1062
+ end while baz
1063
+ RUBY
1064
+ end
1065
+
1066
+ context 'while' do
1067
+ assert_source <<-RUBY
1068
+ while false
1069
+ end
1070
+ RUBY
1071
+
1072
+ assert_source <<-RUBY
1073
+ while false
1074
+ 3
1075
+ end
1076
+ RUBY
1077
+ end
1078
+
1079
+ context 'until' do
1080
+ assert_source <<-RUBY
1081
+ until false
1082
+ end
1083
+ RUBY
1084
+
1085
+ assert_source <<-RUBY
1086
+ until false
1087
+ 3
1088
+ end
1089
+ RUBY
1090
+ end
1091
+ end
1092
+ end