1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
|
// looks like tests are compiled by the old version of compiler
// therefore certain scala-reflect tests give me AMEs after the SI-8063 overhaul
// TODO: fix this in build.xml
/**
package scala.reflect.internal
import org.junit.Test
import org.junit.Assert._
import scala.tools.reflect._
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror=>cm}
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
@RunWith(classOf[JUnit4])
class PrintersTest extends BasePrintTests
with ClassPrintTests
with TraitPrintTests
with ValAndDefPrintTests
with QuasiTreesPrintTests
with PackagePrintTests
object PrinterHelper {
val toolbox = cm.mkToolBox()
import scala.reflect.internal.Chars._
private def normalizeEOL(resultCode: String) =
resultCode.lines mkString s"$LF"
def assertResultCode(code: String)(parsedCode: String = "", typedCode: String = "", wrap: Boolean = false, printRoot: Boolean = false) = {
def toolboxTree(tree: => Tree) = try{
tree
} catch {
case e:scala.tools.reflect.ToolBoxError => throw new Exception(e.getMessage + ": " + code)
}
def wrapCode(source: String) = {
val context = sm"""
|trait PrintersContext {
| class baz extends scala.annotation.StaticAnnotation;
| class foo1[A, B] extends scala.annotation.StaticAnnotation;
| class foo2[A, B](a: scala.Int)(b: scala.Int) extends scala.annotation.StaticAnnotation;
| class foo3[Af, Bf](a: scala.Int)(b: scala.Float, c: PrintersContext.this.foo1[Af, Bf]) extends scala.annotation.StaticAnnotation;
| trait A1;
| trait B1;
|${source.trim.lines map {" " + _} mkString s"$LF"}
|}"""
if (wrap) context.trim() else source.trim
}
val parsedTree = toolboxTree(toolbox.parse(wrapCode(code)))
if (!parsedCode.isEmpty())
assertEquals("using toolbox parser" + LF, wrapCode(parsedCode), normalizeEOL(showCode(parsedTree)))
if (!typedCode.isEmpty()) {
val typedTree = toolboxTree(toolbox.typecheck(parsedTree))
assertEquals("using toolbox typechecker" + LF, wrapCode(typedCode), normalizeEOL(showCode(typedTree, printRootPkg = printRoot)))
}
}
def assertTreeCode(tree: Tree)(code: String) = {
assertEquals("using quasiquote or given tree"+LF, code.trim, normalizeEOL(showCode(tree)))
}
def assertPrintedCode(source: String, checkTypedTree: Boolean = true, wrapCode: Boolean = false) = {
if (checkTypedTree)
assertResultCode(source)(source, source, wrapCode)
else assertResultCode(source)(parsedCode = source, wrap = wrapCode)
}
implicit class StrContextStripMarginOps(val stringContext: StringContext) extends util.StripMarginInterpolator
}
import PrinterHelper._
trait BasePrintTests {
@Test def testIdent = assertTreeCode(Ident("*"))("*")
@Test def testConstant1 = assertTreeCode(Literal(Constant("*")))("\"*\"")
@Test def testConstant2 = assertTreeCode(Literal(Constant(42)))("42")
@Test def testConstantFloat = assertTreeCode(Literal(Constant(42f)))("42.0F")
@Test def testConstantDouble = assertTreeCode(Literal(Constant(42d)))("42.0")
@Test def testConstantLong = assertTreeCode(Literal(Constant(42l)))("42L")
@Test def testOpExpr = assertPrintedCode("(5).+(4)", checkTypedTree = false)
@Test def testName1 = assertPrintedCode("class test")
@Test def testName2 = assertPrintedCode("class *")
@Test def testName4 = assertPrintedCode("class `a*`")
@Test def testName5 = assertPrintedCode("val :::: = 1")
@Test def testName6 = assertPrintedCode("val `::::t` = 1")
@Test def testName7 = assertPrintedCode("""class \/""")
@Test def testName8 = assertPrintedCode("""class \\\\""")
@Test def testName9 = assertPrintedCode("""class test_\/""")
@Test def testName10 = assertPrintedCode("""class `*_*`""")
@Test def testName11 = assertPrintedCode("""class `a_*`""")
@Test def testName12 = assertPrintedCode("""class `*_a`""")
@Test def testName13 = assertPrintedCode("""class a_a""")
@Test def testName14 = assertPrintedCode("val x$11 = 5")
@Test def testName15 = assertPrintedCode("class `[]`")
@Test def testName16 = assertPrintedCode("class `()`")
@Test def testName17 = assertPrintedCode("class `{}`")
@Test def testName18 = assertPrintedCode("class <>")
@Test def testName19 = assertPrintedCode("""class `class`""")
@Test def testName20 = assertPrintedCode("""class `test name`""")
@Test def testIfExpr1 = assertResultCode(code = sm"""
|val a = 1
|if (a > 1)
| a: Int
|else
| (a.toString): String
""")(
parsedCode = sm"""
|val a = 1;
|if (a.>(1))
| ((a): Int)
|else
| ((a.toString): String)""",
typedCode=sm"""
|val a = 1;
|if (PrintersContext.this.a.>(1))
| ((PrintersContext.this.a): scala.Int)
|else
| ((PrintersContext.this.a.toString()): scala.Predef.String)
""", wrap = true)
@Test def testIfExpr2 = assertPrintedCode(sm"""
|class A {
| (if (true)
| {
| false;
| ()
| }
|else
| {
| true;
| ()
| }).toString()
|}""")
@Test def testIfExpr3 = assertPrintedCode(sm"""
|class A {
| (if (true)
| {
| false;
| ()
| }
|else
| {
| true;
| ()
| }).toString().hashCode()
|}""")
//val x = true && true && false.!
@Test def testBooleanExpr1 = assertPrintedCode("val x = true.&&(true).&&(false.`unary_!`)", checkTypedTree = false)
//val x = true && !(true && false)
@Test def testBooleanExpr2 = assertPrintedCode("val x = true.&&(true.&&(false).`unary_!`)", checkTypedTree = false)
@Test def testNewExpr1 = assertResultCode(
code = sm"""
|class foo
|new foo()
|""")(
parsedCode = sm"""
|class foo;
|new foo()""",
typedCode = sm"""
|class foo;
|new PrintersContext.this.foo()
|""",
wrap = true)
@Test def testNewExpr2 = assertResultCode(
code = sm"""
|class foo
|new foo { "test" }
|""")(
parsedCode = sm"""
|class foo;
|{
| final class $$anon extends foo {
| "test"
| };
| new $$anon()
|}""",
typedCode = sm"""
|class foo;
|{
| final class $$anon extends PrintersContext.this.foo {
| "test"
| };
| new $$anon()
|}""",
wrap = true)
@Test def testNewExpr3 = assertPrintedCode(sm"""
|{
| class foo[t];
| new foo[scala.Int]()
|}""")
@Test def testNewExpr4 = assertPrintedCode(sm"""
|{
| class foo(x: scala.Int);
| val x = 5;
| new foo(x)
|}""")
@Test def testNewExpr5 = assertPrintedCode(sm"""
|{
| class foo[t](x: scala.Int);
| val x = 5;
| new foo[scala.Predef.String](x)
|}""")
//new foo[t](x) { () }
@Test def testNewExpr6 = assertResultCode(
code = sm"""
|class foo[t](x: Int)
|new foo[String](3) { () }
|""")(
parsedCode = sm"""
|{
| class foo[t](x: Int);
| {
| final class $$anon extends foo[String](3) {
| ()
| };
| new $$anon()
| }
|}""",
typedCode = sm"""
|{
| class foo[t](x: scala.Int);
| {
| final class $$anon extends foo[scala.Predef.String](3) {
| ()
| };
| new $$anon()
| }
|}""")
//new foo with bar
@Test def testNewExpr7 = assertPrintedCode(sm"""
|{
| trait foo;
| trait bar;
| {
| final class $$anon extends foo with bar;
| new $$anon()
| }
|}""")
//new { anonymous }
@Test def testNewExpr8 = assertPrintedCode(sm"""
|{
| final class $$anon {
| 5
| };
| new $$anon()
|}""")
//new { val early = 1 } with Parent[Int] { body }
@Test def testNewExpr9 = assertPrintedCode(sm"""
|{
| class Parent[t];
| {
| final class $$anon extends {
| val early = 1
| } with Parent[scala.Int] {
| "testNewExpr"
| };
| new $$anon()
| }
|}""")
//new Foo { self => }
@Test def testNewExpr10 = assertPrintedCode(sm"""
|{
| class Foo;
| {
| final class $$anon extends Foo { self =>
|
| };
| new $$anon()
| }
|}""")
@Test def testReturn = assertPrintedCode("def test: scala.Int = return 42")
@Test def testFunc1 = assertResultCode(
code = "List(1, 2, 3).map((i: Int) => i - 1)")(
parsedCode = "List(1, 2, 3).map(((i: Int) => i.-(1)))",
typedCode = sm"scala.collection.immutable.List.apply(1, 2, 3).map(((i: scala.Int) => i.-(1)))(scala.collection.immutable.List.canBuildFrom)")
@Test def testFunc2 = assertResultCode(
code = "val sum: Seq[Int] => Int = _ reduceLeft (_+_)")(
parsedCode = "val sum: _root_.scala.Function1[Seq[Int], Int] = ((x$1) => x$1.reduceLeft(((x$2, x$3) => x$2.+(x$3))))",
typedCode = "val sum: _root_.scala.Function1[scala.`package`.Seq[scala.Int], scala.Int] = ((x$1) => x$1.reduceLeft(((x$2, x$3) => x$2.+(x$3))))")
@Test def testFunc3 = assertResultCode(
code = "List(1, 2, 3) map (_ - 1)")(
parsedCode = "List(1, 2, 3).map(((x$1) => x$1.-(1))) ",
typedCode = "scala.collection.immutable.List.apply(1, 2, 3).map(((x$1) => x$1.-(1)))(scala.collection.immutable.List.canBuildFrom)")
@Test def testFunc4 = assertResultCode(
code = "val x: String => Int = ((str: String) => 1)")(
parsedCode = "val x: _root_.scala.Function1[String, Int] = ((str: String) => 1)",
typedCode = " val x: _root_.scala.Function1[_root_.scala.Predef.String, _root_.scala.Int] = ((str: _root_.scala.Predef.String) => 1)", printRoot = true)
@Test def testImport1 = assertPrintedCode("import scala.collection.mutable")
@Test def testImport2 = assertPrintedCode("import java.lang.{String=>Str}")
@Test def testImport3 = assertPrintedCode("import java.lang.{String=>Str, Object=>_, _}")
@Test def testImport4 = assertPrintedCode("import scala.collection._")
}
trait ClassPrintTests {
@Test def testClass = assertPrintedCode("class *")
@Test def testClassWithBody = assertPrintedCode(sm"""
|class X {
| def y = "test"
|}""")
@Test def testClassWithPublicParams = assertPrintedCode("class X(val x: scala.Int, val s: scala.Predef.String)")
@Test def testClassWithParams1 = assertPrintedCode("class X(x: scala.Int, s: scala.Predef.String)")
@Test def testClassWithParams2 = assertPrintedCode("class X(@test x: Int, s: String)", checkTypedTree = false)
@Test def testClassWithParams3 = assertPrintedCode("class X(implicit x: Int, s: String)", checkTypedTree = false)
@Test def testClassWithParams4 = assertPrintedCode("class X(implicit @unchecked x: Int, s: String)", checkTypedTree = false)
@Test def testClassWithParams5 = assertPrintedCode(sm"""
|{
| class Y {
| val x = 5
| };
| class X(override private[this] val x: scala.Int, s: scala.Predef.String) extends Y;
| ()
|}""")
@Test def testClassWithParams6 = assertPrintedCode("class X(@test1 override private[this] val x: Int, @test2(param1 = 7) s: String) extends Y", checkTypedTree = false)
@Test def testClassWithParams7 = assertPrintedCode("class X protected (val x: scala.Int, val s: scala.Predef.String)")
@Test def testClassWithParams8 = assertPrintedCode("class X(var x: scala.Int)")
@Test def testClassWithParams9 = assertPrintedCode("def test(x: scala.Int*) = 5")
@Test def testClassWithByNameParam = assertPrintedCode("class X(x: => scala.Int)")
@Test def testClassWithDefault = assertPrintedCode(sm"""
|{
| class X(var x: scala.Int = 5);
| ()
|}""")
@Test def testClassWithParams10 = assertPrintedCode("class X(protected[zzz] var x: Int)", checkTypedTree = false)
@Test def testClassWithParams11 = assertPrintedCode(sm"""
|{
| class F(x: scala.Int);
| trait E {
| var x: scala.Int
| };
| class X(override var x: scala.Int = 5) extends F(x) with E;
| ()
|}""")
@Test def testClassWithParams12 = assertPrintedCode("class X(val y: scala.Int)()(var z: scala.Double)")
@Test def testClassWithImplicitParams = assertPrintedCode("class X(var i: scala.Int)(implicit val d: scala.Double, var f: scala.Float)")
@Test def testClassWithEarly = assertPrintedCode(sm"""
|class X(var i: scala.Int) extends {
| val a = i;
| type B
|} with scala.Serializable""")
@Test def testClassWithThrow1 = assertPrintedCode(sm"""
|class Throw1 {
| throw new scala.`package`.Exception("exception!")
|}""")
@Test def testClassWithThrow2 = assertPrintedCode(sm"""
|class Throw2 {
| var msg = " ";
| val e = new scala.`package`.Exception(Throw2.this.msg);
| throw Throw2.this.e
|}""")
/*
class Test {
val (a, b) = (1, 2)
}
*/
@Test def testClassWithAssignmentWithTuple1 = assertPrintedCode(sm"""
|class Test {
| private[this] val x$$1 = (scala.Tuple2.apply(1, 2): @scala.unchecked) match {
| case scala.Tuple2((a @ _), (b @ _)) => scala.Tuple2.apply(a, b)
| };
| val a = Test.this.x$$1._1;
| val b = Test.this.x$$1._2
|}""")
@Test def testClassWithAssignmentWithTuple2 = assertResultCode(
code = sm"""
|class Test {
| val (a, b) = (1).->(2)
|}""")(
parsedCode = sm"""
|class Test {
| private[this] val x$$1 = ((1).->(2): @scala.unchecked) match {
| case scala.Tuple2((a @ _), (b @ _)) => scala.Tuple2(a, b)
| };
| val a = x$$1._1;
| val b = x$$1._2
|}""",
typedCode = sm"""
|class Test {
| private[this] val x$$1 = (scala.Predef.ArrowAssoc(1).->(2): @scala.unchecked) match {
| case scala.Tuple2((a @ _), (b @ _)) => scala.Tuple2.apply(a, b)
| };
| val a = Test.this.x$$1._1;
| val b = Test.this.x$$1._2
|}""")
/*
class Test {
val List(one, three, five) = List(1,3,5)
}
*/
@Test def testClassWithPatternMatchInAssignment = assertPrintedCode(sm"""
|class Test {
| private[this] val x$$1 = (scala.collection.immutable.List.apply(1, 3, 5): @scala.unchecked) match {
| case scala.collection.immutable.List((one @ _), (three @ _), (five @ _)) => scala.Tuple3.apply(one, three, five)
| };
| val one = Test.this.x$$1._1;
| val three = Test.this.x$$1._2;
| val five = Test.this.x$$1._3
|}""")
//class A(l: List[_])
@Test def testClassWithExistentialParameter1 = assertPrintedCode(sm"""
|class Test(l: (scala.`package`.List[_$$1] forSome {
| type _$$1
|}))""")
@Test def testClassWithExistentialParameter2 = assertPrintedCode(sm"""
|class B(l: (scala.`package`.List[T] forSome {
| type T
|}))""")
@Test def testClassWithCompoundTypeTree = assertPrintedCode(sm"""
|{
| trait A;
| trait B;
| abstract class C(val a: A with B) {
| def method(x: A with B with C {
| val x: scala.Float
| }): A with B
| };
| ()
|}""")
@Test def testClassWithSelectFromTypeTree = assertPrintedCode(sm"""
|{
| trait A {
| type T
| };
| class B(t: (A)#T);
| ()
|}""")
@Test def testImplicitClass = assertPrintedCode(sm"""
|{
| implicit class X(protected[this] var x: scala.Int);
| ()
|}""",
checkTypedTree = true)
@Test def testAbstractClass = assertPrintedCode("abstract class X(protected[this] var x: scala.Int)")
@Test def testCaseClassWithParams1 = assertPrintedCode(sm"""
|{
| case class X(x: scala.Int, s: scala.Predef.String);
| ()
|}""")
@Test def testCaseClassWithParams2 = assertPrintedCode(sm"""
|{
| case class X(protected val x: scala.Int, s: scala.Predef.String);
| ()
|}""")
@Test def testCaseClassWithParams3 = assertPrintedCode(sm"""
|{
| case class X(implicit x: scala.Int, s: scala.Predef.String);
| ()
|}""")
@Test def testCaseClassWithParams4 = assertPrintedCode(sm"""
|{
| trait V {
| val x: scala.Int
| };
| case class X(override val x: scala.Int, s: scala.Predef.String) extends scala.Cloneable;
| ()
|}""")
@Test def testCaseClassWithBody = assertPrintedCode(sm"""
|{
| case class X() {
| def y = "test"
| };
| ()
|}""")
@Test def testLocalClass = assertPrintedCode(sm"""
|def test = {
| class X(var a: scala.Int) {
| def y = "test"
| };
| new X(5)
|}""")
@Test def testLocalCaseClass = assertPrintedCode(sm"""
|def test = {
| case class X(var a: scala.Int) {
| def y = "test"
| };
| new X(5)
|}""")
@Test def testSuperInClass = assertPrintedCode(sm"""
|{
| trait Root {
| def r = "Root"
| };
| class X extends Root {
| def superX = super.r
| };
| class Y extends X with Root {
| class Inner {
| val myY = Y.super.r
| };
| def fromX = super[X].r;
| def fromRoot = super[Root].r
| };
| ()
|}""")
@Test def testThisInClass = assertPrintedCode(sm"""
|class Outer {
| class Inner {
| val outer = Outer.this
| };
| val self = this
|}""")
@Test def testCaseClassWithParamsAndBody = assertPrintedCode(sm"""
|{
| case class X(var x: scala.Int, var s: scala.Predef.String) {
| def y = "test"
| };
| ()
|}""")
@Test def testObject = assertPrintedCode("object *")
@Test def testObjectWithBody = assertPrintedCode(sm"""
|object X {
| def y = "test"
|}""")
@Test def testObjectWithEarly1 = assertPrintedCode(sm"""
|object X extends {
| val early: scala.Int = 42
|} with scala.Serializable""")
@Test def testObjectWithEarly2 = assertPrintedCode(sm"""
|object X extends {
| val early: scala.Int = 42;
| type EarlyT = scala.Predef.String
|} with scala.Serializable""")
@Test def testObjectWithSelf = assertPrintedCode(sm"""
|object Foo extends scala.Serializable { self =>
| 42
|}""")
@Test def testObjectInh = assertPrintedCode(sm"""
|trait Y {
| private[Y] object X extends scala.Serializable with scala.Cloneable
|}""")
@Test def testObjectWithPatternMatch1 = assertPrintedCode(sm"""
|object PM1 {
| scala.collection.immutable.List.apply(1, 2) match {
| case (i @ _) => i
| }
|}""")
@Test def testObjectWithPatternMatch2 = assertResultCode(
code = sm"""
|object PM2 {
| List(1, 2).map {
| case i if i > 5 => i
| }
|}""")(
parsedCode = sm"""
|object PM2 {
| List(1, 2).map({
| case (i @ _) if i.>(5) => i
| })
|}""")
/*
typedCode = sm"""
|object PM2 {
| scala.collection.immutable.List.apply(1, 2).map(((x0$$1) => x0$$1 match {
| case (i @ _) if i.>(5) => i
| }))(scala.collection.immutable.List.canBuildFrom)
|}""")
*
*/
@Test def testObjectWithPatternMatch3 = assertResultCode(
code = sm"""
|object PM3 {
| List(1, 2).map {
| case i: Int => i
| }
|}""")(
parsedCode = sm"""
|object PM3 {
| List(1, 2).map({
| case (i @ ((_): Int)) => i
| })
|}""")
/*
typedCode = sm"""
|object PM3 {
| scala.collection.immutable.List.apply(1, 2).map(((x0$$2) => x0$$2 match {
| case (i @ ((_): scala.Int)) => i
| }))(scala.collection.immutable.List.canBuildFrom)
|}""")
*
*/
@Test def testObjectWithPatternMatch4 = assertResultCode(
code = sm"""
|object PM4 {
| List(1, 2).map {
| case _ => 42
| }
|}""")(
parsedCode = sm"""
|object PM4 {
| List(1, 2).map({
| case _ => 42
| })
|}""")
/*
typedCode = sm"""
|object PM4 {
| scala.collection.immutable.List.apply(1, 2).map(((x0$$3) => x0$$3 match {
| case _ => 42
| }))(scala.collection.immutable.List.canBuildFrom)
|}""")
*
*/
@Test def testObjectWithPatternMatch5 = assertResultCode(
code = sm"""
|object PM5 {
| List(1, 2) match {
| case x :: xs => x
| }
|}""")(
parsedCode = sm"""
|object PM5 {
| List(1, 2) match {
| case ::((x @ _), (xs @ _)) => x
| }
|}""",
typedCode = sm"""
|object PM5 {
| scala.collection.immutable.List.apply(1, 2) match {
| case scala.`package`.::((x @ _), (xs @ _)) => x
| }
|}""")
@Test def testObjectWithPatternMatch6 = assertResultCode(
code = sm"""
|object PM6 {
| List(1, 2).map {
| case (0 | 1) => true
| case _ => false
| }
|}""")(
parsedCode = sm"""
|object PM6 {
| List(1, 2).map({
| case (0| 1) => true
| case _ => false
| })
|}""")
/*
typedCode = sm"""
|object PM6 {
| scala.collection.immutable.List.apply(1, 2).map(((x0$$4) => x0$$4 match {
| case (0| 1) => true
| case _ => false
| }))(scala.collection.immutable.List.canBuildFrom)
|}"""
*
*/
@Test def testObjectWithPatternMatch7 = assertPrintedCode(sm"""
|object PM7 {
| scala.Predef.augmentString("abcde").toList match {
| case scala.collection.Seq((car @ _), _*) => car
| }
|}""")
@Test def testObjectWithPatternMatch8 = assertPrintedCode(sm"""
|{
| object Extractor {
| def unapply(i: scala.Int) = scala.Some.apply(i)
| };
| object PM9 {
| 42 match {
| case (a @ Extractor((i @ _))) => i
| }
| };
| ()
|}""")
@Test def testObjectWithPartialFunc = assertPrintedCode(sm"""
|object Test {
| def partFuncTest[A, B](e: scala.`package`.Either[A, B]): scala.Unit = e match {
| case scala.`package`.Right(_) => ()
| }
|}""")
@Test def testObjectWithTry = assertResultCode(
code = sm"""
|object Test {
| import java.io._;
| var file: PrintStream = null;
| try {
| val out = new FileOutputStream("myfile.txt");
| file = new PrintStream(out)
| } catch {
| case ioe: IOException => println("ioe")
| case e: Exception => println("e")
| } finally println("finally")
|}""")(
parsedCode = sm"""
|object Test {
| import java.io._;
| var file: PrintStream = null;
| try {
| val out = new FileOutputStream("myfile.txt");
| file = new PrintStream(out)
| } catch {
| case (ioe @ ((_): IOException)) => println("ioe")
| case (e @ ((_): Exception)) => println("e")
| } finally println("finally")
|}""",
typedCode = sm"""
|object Test {
| import java.io._;
| var file: java.io.PrintStream = null;
| try {
| val out = new java.io.FileOutputStream("myfile.txt");
| Test.this.`file_=`(new java.io.PrintStream(out))
| } catch {
| case (ioe @ ((_): java.io.IOException)) => scala.Predef.println("ioe")
| case (e @ ((_): scala.`package`.Exception)) => scala.Predef.println("e")
| } finally scala.Predef.println("finally")
|}""")
}
trait TraitPrintTests {
@Test def testTrait = assertPrintedCode("trait *")
@Test def testTraitWithBody = assertPrintedCode(sm"""
|trait X {
| def y = "test"
|}""")
@Test def testTraitWithSelfTypeAndBody = assertPrintedCode(sm"""
|trait X { self: scala.Cloneable =>
| def y = "test"
|}""")
@Test def testTraitWithSelf1 = assertPrintedCode(sm"""
|trait X { self =>
| def y = "test"
|}""")
@Test def testTraitWithSelf2 = assertPrintedCode(sm"""
|trait X { self: scala.Cloneable with scala.Serializable =>
| val x: scala.Int = 1
|}""")
@Test def testTraitTypeParams = assertPrintedCode("trait X[A, B]")
@Test def testTraitWithBody2 = assertPrintedCode(sm"""
|trait X {
| def foo: scala.Unit;
| val bar: scala.Predef.String
|}""")
@Test def testTraitWithInh = assertPrintedCode("trait X extends scala.Cloneable with scala.Serializable")
@Test def testTraitWithEarly1 = assertPrintedCode(sm"""
|trait X extends {
| val x: Int = 1
|} with AnyRef""", checkTypedTree = false)
@Test def testTraitWithEarly2 = assertPrintedCode(sm"""
|trait X extends {
| val x: scala.Int = 0;
| type Foo = scala.Unit
|} with scala.Cloneable""")
@Test def testTraitWithEarly3 = assertPrintedCode(sm"""
|trait X extends {
| val x: scala.Int = 5;
| val y: scala.Double = 4.0;
| type Foo;
| type XString = scala.Predef.String
|} with scala.Serializable""")
@Test def testTraitWithEarly4 = assertPrintedCode(sm"""
|trait X extends {
| val x: scala.Int = 5;
| val y: scala.Double = 4.0;
| type Foo;
| type XString = scala.Predef.String
|} with scala.Serializable {
| val z = 7
|}""")
@Test def testTraitWithSingletonTypeTree = assertPrintedCode(sm"""
|trait Test {
| def testReturnSingleton(): Test.this.type
|}""")
@Test def testTraitWithThis = assertTreeCode(q"trait Test { this: X with Y => }")(sm"""
|trait Test { _ : X with Y =>
|
|}""")
@Test def testTraitWithWhile1 = assertPrintedCode(sm"""
|trait Test {
| while (false)
| scala.Predef.println("testing...")
|
|}""")
@Test def testTraitWithWhile2 = assertPrintedCode(sm"""
|trait Test {
| while (true)
| {
| scala.Predef.println("testing...");
| scala.Predef.println("testing...")
| }
|
|}""")
@Test def testTraitWithDoWhile1 = assertPrintedCode(sm"""
|trait Test {
| do
| scala.Predef.println("testing...")
| while (true)
|}""")
@Test def testTraitWithTypes = assertResultCode(
code = sm"""
|trait Test {
| type A = Int;
| type B >: Nothing <: AnyRef;
| protected type C >: Nothing;
| type D <: AnyRef
|}""")(
parsedCode = sm"""
|trait Test {
| type A = Int;
| type B >: Nothing <: AnyRef;
| protected type C >: Nothing;
| type D <: AnyRef
|}""",
typedCode = sm"""
|trait Test {
| type A = scala.Int;
| type B <: scala.AnyRef;
| protected type C;
| type D <: scala.AnyRef
|}""")
}
trait ValAndDefPrintTests {
@Test def testVal1 = assertPrintedCode("val a: scala.Unit = ()")
@Test def testVal2 = assertPrintedCode("val * : scala.Unit = ()")
@Test def testVal3 = assertPrintedCode("val a_ : scala.Unit = ()")
@Test def testDef1 = assertPrintedCode("def a = ()")
@Test def testDef2 = assertPrintedCode("def * : scala.Unit = ()")
@Test def testDef3 = assertPrintedCode("def a_(x: scala.Int): scala.Unit = ()")
@Test def testDef4 = assertPrintedCode("def a_ : scala.Unit = ()")
@Test def testDef5 = assertPrintedCode("def a_(* : scala.Int): scala.Unit = ()")
@Test def testDef6 = assertPrintedCode("def a_(b_ : scala.Int) = ()")
@Test def testDef7 = assertTreeCode{
Block(
DefDef(NoMods, newTermName("test1"), Nil, Nil, EmptyTree, Literal(Constant(()))),
DefDef(NoMods, newTermName("test2"), Nil, Nil :: Nil, EmptyTree, Literal(Constant(())))
)
}(sm"""
|{
| def test1 = ();
| def test2() = ()
|}""")
@Test def testDef8 = {
val arg = ValDef(Modifiers(Flag.IMPLICIT) , newTermName("a"),
AppliedTypeTree(Ident(newTypeName("R")), List(Ident(newTypeName("X")))), EmptyTree)
//def m[X](implicit a: R[X]) = ()
val tree = DefDef(NoMods, newTermName("test"), TypeDef(NoMods, newTypeName("X"), Nil, EmptyTree) :: Nil,
List(List(arg)), EmptyTree, Literal(Constant(())))
assertTreeCode(tree)("def test[X](implicit a: R[X]) = ()")
}
@Test def testDef9 = assertPrintedCode("def a(x: scala.Int)(implicit z: scala.Double, y: scala.Float): scala.Unit = ()")
@Test def testDefWithLazyVal1 = assertResultCode(
code = "def a = { lazy val test: Int = 42 }")(
parsedCode = sm"""
|def a = {
| lazy val test: Int = 42;
| ()
|}
""",
typedCode = sm"""
|def a = {
| lazy val test: scala.Int = 42;
| ()
|}""")
@Test def testDefWithLazyVal2 = assertPrintedCode(sm"""
|def a = {
| lazy val test = {
| scala.Predef.println();
| scala.Predef.println()
| };
| ()
|}""")
@Test def testDefWithParams1 = assertPrintedCode("def foo(x: scala.Int*) = ()")
@Test def testDefWithParams2 = assertPrintedCode(sm"""
|{
| def foo(x: scala.Int)(y: scala.Int = 1) = ();
| ()
|}""")
@Test def testDefWithTypeParams1 = assertPrintedCode(sm"""
|{
| def foo[A, B, C](x: A)(y: scala.Int = 1): C = ().asInstanceOf[C];
| ()
|}""")
@Test def testDefWithTypeParams2 = assertPrintedCode("def foo[A, B <: scala.AnyVal] = ()")
@Test def testDefWithAnn1 = assertPrintedCode("@annot def foo = null", checkTypedTree = false)
@Test def testDefWithAnn2 = assertPrintedCode("@a(x) def foo = null", checkTypedTree = false)
@Test def testDefWithAnn3 = assertPrintedCode("@Foo[A, B] def foo = null", checkTypedTree = false)
@Test def testDefWithAnn4 = assertPrintedCode("@Foo(a)(b)(x, y) def foo = null", checkTypedTree = false)
@Test def testDefWithAnn5 = assertPrintedCode("@Foo[A, B](a)(b) @Bar def foo(x: Int) = null", checkTypedTree = false)
@Test def testDefWithAnn6 = assertPrintedCode("@test1(new test2()) def foo = 42", checkTypedTree = false)
@Test def testDefWithAnn7 = assertPrintedCode("@`t*` def foo = 42", checkTypedTree = false)
@Test def testDefWithAnn8 = assertPrintedCode("@throws(classOf[Exception]) def foo = throw new Exception()", checkTypedTree = false)
@Test def testAnnotated1 = assertResultCode(
code = "def foo = 42: @baz")(
parsedCode = "def foo = 42: @baz",
typedCode = "def foo = (42: @baz)",
wrap = true)
@Test def testAnnotated2 = assertResultCode(
code = "def foo = 42: @foo2[A1, B1](4)(2)")(
parsedCode = "def foo = 42: @foo2[A1, B1](4)(2)",
typedCode = "def foo = (42: @foo2[A1, B1](4)(2))",
wrap = true)
@Test def testAnnotated3 = assertResultCode(
code = "def foo = (42: @foo1[A1, B1]): @foo2[A1, B1](4)(2)")(
parsedCode = "def foo = (42: @foo1[A1, B1]): @foo2[A1, B1](4)(2)",
typedCode = "def foo = ((42: @foo1[A1, B1]): @foo2[A1, B1](4)(2))",
wrap = true)
@Test def testAnnotated4 = assertResultCode(
code = "def foo = 42: @foo3[A1, B1](4)(2.0F, new foo1[A1, B1]())")(
parsedCode = "def foo = 42: @foo3[A1, B1](4)(2.0F, new foo1[A1, B1]())",
typedCode = "def foo = (42: @foo3[A1, B1](4)(2.0F, new foo1[A1, B1]()))",
wrap = true)
@Test def testAnnotated5 = assertPrintedCode(sm"""
|{
| val x = 5;
| (x: @unchecked) match {
| case ((_): scala.Int) => true
| case _ => false
| }
|}""")
@Test def testAnnotated8 = assertPrintedCode(sm"""
|{
| val x = 5;
| ((x: @unchecked): @foo3(4)(2.0F, new foo1[A1, B1]())) match {
| case _ => true
| }
|}""", wrapCode = true)
}
trait PackagePrintTests {
@Test def testPackage1 = assertPrintedCode(sm"""
|package foo.bar {
|
|}""", checkTypedTree = false)
@Test def testPackage2 = assertPrintedCode(sm"""
|package foo {
| class C
|
| object D
|}""", checkTypedTree = false)
//package object foo extends a with b
@Test def testPackage3 = assertPrintedCode(sm"""
|package foo {
| object `package` extends a with b
|}""", checkTypedTree = false)
//package object foo { def foo; val x = 1 }
@Test def testPackage4 = assertPrintedCode(sm"""
|package foo {
| object `package` {
| def foo: scala.Unit = ();
| val x = 1
| }
|}""", checkTypedTree = false)
//package object foo extends { val x = 1; type I = Int } with Any
@Test def testPackage5 = assertPrintedCode(sm"""
|package foo {
| object `package` extends {
| val x = 1;
| type I = Int
| } with AnyRef
|}""", checkTypedTree = false)
}
trait QuasiTreesPrintTests {
@Test def testQuasiIdent = assertTreeCode(q"*")("*")
@Test def testQuasiVal = assertTreeCode(q"val * : Unit = null")("val * : Unit = null")
@Test def testQuasiDef = assertTreeCode(q"def * : Unit = null")("def * : Unit = null")
@Test def testQuasiTrait = assertTreeCode(q"trait *")("trait *")
@Test def testQuasiClass = assertTreeCode(q"class *")("class *")
@Test def testQuasiClassWithPublicParams = assertTreeCode(q"class X(val x: Int, val s:String)")("class X(val x: Int, val s: String)")
@Test def testQuasiClassWithParams = assertTreeCode(q"class X(x: Int, s:String)")("class X(x: Int, s: String)")
@Test def testQuasiObject = assertTreeCode(q"object *")("object *")
@Test def testQuasiObjectWithBody = assertTreeCode(q"""object X{ def y = "test" }""")(sm"""
|object X {
| def y = "test"
|}""")
@Test def testQuasiClassWithBody = assertTreeCode(q"""class X{ def y = "test" }""")(sm"""
|class X {
| def y = "test"
|}""")
@Test def testQuasiTraitWithBody = assertTreeCode(q"""trait X{ def y = "test" }""")(sm"""
|trait X {
| def y = "test"
|}""")
@Test def testQuasiTraitWithSelfTypeAndBody = assertTreeCode(q"""trait X{ self: Order => def y = "test" }""")(sm"""
|trait X { self: Order =>
| def y = "test"
|}""")
@Test def testQuasiTraitWithSelf = assertTreeCode(q"""trait X{ self => def y = "test" }""")(sm"""
|trait X { self =>
| def y = "test"
|}""")
@Test def testQuasiCaseClassWithBody = assertTreeCode(q"""case class X() { def y = "test" }""")(sm"""
|case class X() {
| def y = "test"
|}""")
@Test def testQuasiCaseClassWithParamsAndBody = assertTreeCode(q"""case class X(x: Int, s: String){ def y = "test" }""")(sm"""
|case class X(x: Int, s: String) {
| def y = "test"
|}""")
}
*/
|