-
Notifications
You must be signed in to change notification settings - Fork 316
/
Compiler2Tests.java
2214 lines (2088 loc) · 57.7 KB
/
Compiler2Tests.java
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
/**
* Copyright (c) 2012, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xbase.tests.compiler;
import org.eclipse.xtext.util.JavaVersion;
import org.eclipse.xtext.xbase.compiler.GeneratorConfig;
import org.eclipse.xtext.xbase.compiler.output.FakeTreeAppendable;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.junit.Test;
public class Compiler2Tests extends AbstractOutputComparingCompilerTests {
@Override
protected FakeTreeAppendable createAppendable() {
return new FakeTreeAppendable();
}
/**
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=424279
*/
@Test
public void testBug_424279() throws Exception {
compilesTo(
"{\n" +
" val treeIt = <CharSequence>newArrayList.iterator;\n" +
" while (treeIt.hasNext) {\n" +
" val o = treeIt.next;\n" +
" if (o instanceof java.util.List<?>) {\n" +
" return o;\n" +
" } else {\n" +
" return o;\n" +
" }\n" +
" }\n" +
" return null;\n" +
"}\n",
"\n" +
"final java.util.Iterator<CharSequence> treeIt = org.eclipse.xtext.xbase.lib.CollectionLiterals.<CharSequence>newArrayList().iterator();\n" +
"while (treeIt.hasNext()) {\n" +
" {\n" +
" final CharSequence o = treeIt.next();\n" +
" if ((o instanceof java.util.List<?>)) {\n" +
" return o;\n" +
" } else {\n" +
" return o;\n" +
" }\n" +
" }\n" +
"}\n" +
"return null;\n");
}
@Test
public void testAbstractIterator() throws Exception {
compilesTo(
"{\n" +
" var com.google.common.collect.AbstractIterator<String> iter = [| return self.endOfData ]\n" +
" return iter\n" +
"}\n",
"\n" +
"final com.google.common.collect.AbstractIterator<String> _function = new com.google.common.collect.AbstractIterator<String>() {\n" +
" @Override\n" +
" protected String computeNext() {\n" +
" return this.endOfData();\n" +
" }\n" +
"};\n" +
"com.google.common.collect.AbstractIterator<String> iter = _function;\n" +
"return iter;\n");
}
@Test
public void testVariableDeclaration() throws Exception {
compilesTo(
"{\n" +
" val x = 42\n" +
"}\n", "\n" +
"final int x = 42;\n");
}
@Test
public void testVariableDeclaration1() throws Exception {
compilesTo(
"{\n" +
" val a = 'foo'\n" +
" val x = a\n" +
"}\n", "\n" +
"final String a = \"foo\";\n" +
"final String x = a;\n");
}
@Test
public void testVariableDeclaration2() throws Exception {
compilesTo(
"{\n" +
" val x = 'foo'.toUpperCase\n" +
"}\n", "\n" +
"final String x = \"foo\".toUpperCase();\n");
}
@Test
public void testBug367144() throws Exception {
compilesTo(
"foo::bar::SubOfClassWithStatics::MY_STATIC_FIELD\n", "\n" +
"return foo.bar.SubOfClassWithStatics.MY_STATIC_FIELD;\n");
}
@Test
public void testBug367144_1() throws Exception {
compilesTo(
"foo::bar::SubOfClassWithStatics::MY_STATIC_METHOD\n",
"\n" +
"String _MY_STATIC_METHOD = foo.bar.SubOfClassWithStatics.MY_STATIC_METHOD();\n" +
"return _MY_STATIC_METHOD;\n");
}
@Test
public void testBug371321_1() throws Exception {
compilesTo(
"try {\n" +
" 'foo'.length\n" +
"} catch (RuntimeException assert) {\n" +
" assert.printStackTrace\n" +
"}\n",
"\n" +
"int _xtrycatchfinallyexpression = (int) 0;\n" +
"try {\n" +
" _xtrycatchfinallyexpression = \"foo\".length();\n" +
"} catch (final Throwable _t) {\n" +
" if (_t instanceof RuntimeException) {\n" +
" final RuntimeException assert_ = (RuntimeException)_t;\n" +
" assert_.printStackTrace();\n" +
" } else {\n" +
" throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" +
" }\n" +
"}\n" +
"return Integer.valueOf(_xtrycatchfinallyexpression);\n");
}
@Test
public void testBug420984_EmptyCatch() throws Exception {
compilesTo(
"try {\n" +
" println(\"\")\n" +
"} catch (RuntimeException e) {\n" +
"}\n",
"\n" +
"String _xtrycatchfinallyexpression = null;\n" +
"try {\n" +
" _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.<String>println(\"\");\n" +
"} catch (final Throwable _t) {\n" +
" if (_t instanceof RuntimeException) {\n" +
" _xtrycatchfinallyexpression = null;\n" +
" } else {\n" +
" throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" +
" }\n" +
"}\n" +
"return _xtrycatchfinallyexpression;\n");
}
@Test
public void testBug420984_EmptyCatchWithoutReturnType() throws Exception {
compilesToStatement(
"try {\n" +
" println(\"\")\n" +
"} catch (RuntimeException e) {\n" +
"}\n",
"try {\n" +
" org.eclipse.xtext.xbase.lib.InputOutput.<String>println(\"\");\n" +
"} catch (final Throwable _t) {\n" +
" if (_t instanceof RuntimeException) {\n" +
" } else {\n" +
" throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" +
" }\n" +
"}");
}
@Test
public void testBug420984_CatchWithoutReference() throws Exception {
compilesTo(
"try {\n" +
" println(\"\")\n" +
"} catch (RuntimeException e) {\n" +
" println(\"\")\n" +
"}\n",
"\n" +
"String _xtrycatchfinallyexpression = null;\n" +
"try {\n" +
" _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.<String>println(\"\");\n" +
"} catch (final Throwable _t) {\n" +
" if (_t instanceof RuntimeException) {\n" +
" _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.<String>println(\"\");\n" +
" } else {\n" +
" throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" +
" }\n" +
"}\n" +
"return _xtrycatchfinallyexpression;\n");
}
@Test
public void testBug420984_CatchWithReference() throws Exception {
compilesTo(
"try {\n" +
" println(\"\")\n" +
"} catch (RuntimeException e) {\n" +
" println(e)\n" +
"}\n",
"\n" +
"java.io.Serializable _xtrycatchfinallyexpression = null;\n" +
"try {\n" +
" _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.<String>println(\"\");\n" +
"} catch (final Throwable _t) {\n" +
" if (_t instanceof RuntimeException) {\n" +
" final RuntimeException e = (RuntimeException)_t;\n" +
" _xtrycatchfinallyexpression = org.eclipse.xtext.xbase.lib.InputOutput.<RuntimeException>println(e);\n" +
" } else {\n" +
" throw org.eclipse.xtext.xbase.lib.Exceptions.sneakyThrow(_t);\n" +
" }\n" +
"}\n" +
"return _xtrycatchfinallyexpression;\n");
}
@Test
public void testBug371321_2() throws Exception {
compilesTo(
"for (assert : 'foo'.toCharArray) {\n" +
" println(assert)\n" +
"}\n",
"\n" +
"char[] _charArray = \"foo\".toCharArray();\n" +
"for (final char assert_ : _charArray) {\n" +
" org.eclipse.xtext.xbase.lib.InputOutput.<Character>println(Character.valueOf(assert_));\n" +
"}\n");
}
@Test
public void testNewThread() throws Exception {
compilesTo(
"new Thread [| ]\n",
"\n" +
"final Runnable _function = new Runnable() {\n" +
" public void run() {\n" +
" }\n" +
"};\n" +
"Thread _thread = new Thread(_function);\n" +
"return _thread;\n");
}
@Test
public void testForLoop_01() throws Exception {
compilesTo(
"for(int i: 1..2) { i.toString }\n",
"\n" +
"org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" +
"for (final int i : _upTo) {\n" +
" Integer.valueOf(i).toString();\n" +
"}\n");
}
@Test
public void testForLoop_02() throws Exception {
compilesTo(
"for(Integer i: 1..2) { i.toString }\n",
"\n" +
"org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" +
"for (final Integer i : _upTo) {\n" +
" i.toString();\n" +
"}\n");
}
@Test
public void testForLoop_03() throws Exception {
compilesTo(
"for(i: 1..2) { i.toString }\n",
"\n" +
"org.eclipse.xtext.xbase.lib.IntegerRange _upTo = new org.eclipse.xtext.xbase.lib.IntegerRange(1, 2);\n" +
"for (final Integer i : _upTo) {\n" +
" i.toString();\n" +
"}\n");
}
@Test
public void testForLoop_04() throws Exception {
compilesTo(
"for(i: null as int[]) { i.toString }\n",
"\n" +
"for (final int i : ((int[]) null)) {\n" +
" Integer.valueOf(i).toString();\n" +
"}\n");
}
@Test
public void testForLoop_05() throws Exception {
compilesTo(
"for(i: null as Integer[]) { i.toString }\n", "\n" +
"for (final Integer i : ((Integer[]) null)) {\n" +
" i.toString();\n" +
"}\n");
}
@Test
public void testForLoop_06() throws Exception {
compilesTo(
"for(Integer i: null as int[]) { i.toString }\n", "\n" +
"for (final Integer i : ((int[]) null)) {\n" +
" i.toString();\n" +
"}\n");
}
@Test
public void testForLoop_07() throws Exception {
compilesTo(
"for(int i: null as Integer[]) { i.toString }\n",
"\n" +
"for (final int i : ((Integer[]) null)) {\n" +
" Integer.valueOf(i).toString();\n" +
"}\n");
}
@Test
public void testForLoop_08() throws Exception {
compilesTo(
"for(Integer i: null as int[]) { i.toString }\n", "\n" +
"for (final Integer i : ((int[]) null)) {\n" +
" i.toString();\n" +
"}\n");
}
@Test
public void testForLoop_09() throws Exception {
compilesTo(
"for(int i: null as Integer[]) { i.toString }\n",
"\n" +
"for (final int i : ((Integer[]) null)) {\n" +
" Integer.valueOf(i).toString();\n" +
"}\n");
}
@Test
public void testForLoop_10() throws Exception {
compilesTo(
"for(int i: newArrayList) { i.toString }\n",
"\n" +
"java.util.ArrayList<Integer> _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.<Integer>newArrayList();\n" +
"for (final int i : _newArrayList) {\n" +
" Integer.valueOf(i).toString();\n" +
"}\n");
}
@Test
public void testForLoop_11() throws Exception {
compilesTo(
"for(Integer i: newArrayList) { i.toString }\n",
"\n" +
"java.util.ArrayList<Integer> _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.<Integer>newArrayList();\n" +
"for (final Integer i : _newArrayList) {\n" +
" i.toString();\n" +
"}\n");
}
@Test
public void testForLoop_12() throws Exception {
compilesTo(
"for(double d: <Integer>newArrayList) { d.toString }\n",
"\n" +
"java.util.ArrayList<Integer> _newArrayList = org.eclipse.xtext.xbase.lib.CollectionLiterals.<Integer>newArrayList();\n" +
"for (final double d : _newArrayList) {\n" +
" Double.valueOf(d).toString();\n" +
"}\n");
}
@Test
public void testForLoop_13() throws Exception {
compilesTo(
"for(double d: null as int[]) { d.toString }\n",
"\n" +
"for (final double d : ((int[]) null)) {\n" +
" Double.valueOf(d).toString();\n" +
"}\n");
}
@Test
public void testForLoop_14() throws Exception {
compilesTo(
"for(double d: null as Integer[]) { d.toString }\n",
"\n" +
"for (final double d : ((Integer[]) null)) {\n" +
" Double.valueOf(d).toString();\n" +
"}\n");
}
@Test
public void testNullSafe_01() throws Exception {
compilesTo(
"('foo'.toUpperCase as CharSequence)?.subSequence(0,2)\n",
"\n" +
"String _upperCase = \"foo\".toUpperCase();\n" +
"CharSequence _subSequence = null;\n" +
"if (((CharSequence) _upperCase)!=null) {\n" +
" _subSequence=((CharSequence) _upperCase).subSequence(0, 2);\n" +
"}\n" +
"return _subSequence;\n");
}
@Test
public void testSwitch() throws Exception {
compilesTo(
"{\n" +
" val it = String\n" +
" switch name {\n" +
" CharSequence : name\n" +
" default : 'noname'\n" +
" }\n" +
"}\n",
"\n" +
"String _xblockexpression = null;\n" +
"{\n" +
" final Class<String> it = String.class;\n" +
" String _switchResult = null;\n" +
" String _name = it.getName();\n" +
" boolean _matched = false;\n" +
" if (_name instanceof CharSequence) {\n" +
" _matched=true;\n" +
" _switchResult = it.getName();\n" +
" }\n" +
" if (!_matched) {\n" +
" _switchResult = \"noname\";\n" +
" }\n" +
" _xblockexpression = _switchResult;\n" +
"}\n" +
"return _xblockexpression;\n");
}
@Test
public void testSwitchScopes_Bug470586_01() throws Exception {
compilesTo(
"\n" +
"{\n" +
" val list = #[\"1\", \"2\"]\n" +
" list.forEach[\n" +
" val res = switch it {\n" +
" CharSequence : it\n" +
" }\n" +
" ]\n" +
" list.forEach[\n" +
" println(it)\n" +
" ]\n" +
"}\n",
"\n" +
"final java.util.List<String> list = java.util.Collections.<String>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<String>newArrayList(\"1\", \"2\"));\n" +
"final java.util.function.Consumer<String> _function = new java.util.function.Consumer<String>() {\n" +
" public void accept(final String it) {\n" +
" String _switchResult = null;\n" +
" boolean _matched = false;\n" +
" if (it instanceof CharSequence) {\n" +
" _matched=true;\n" +
" _switchResult = it;\n" +
" }\n" +
" final String res = _switchResult;\n" +
" }\n" +
"};\n" +
"list.forEach(_function);\n" +
"final java.util.function.Consumer<String> _function_1 = new java.util.function.Consumer<String>() {\n" +
" public void accept(final String it) {\n" +
" org.eclipse.xtext.xbase.lib.InputOutput.<String>println(it);\n" +
" }\n" +
"};\n" +
"list.forEach(_function_1);\n");
}
@Test
public void testSwitchScopes_Bug470586_02() throws Exception {
compilesTo(
"{\n" +
" val list = #[\"1\", \"2\"]\n" +
" list.forEach[\n" +
" val res = switch it {\n" +
" CharSequence : it\n" +
" default : ''\n" +
" }\n" +
" ]\n" +
" list.forEach[\n" +
" println(it)\n" +
" ]\n" +
"}\n",
"\n" +
"final java.util.List<String> list = java.util.Collections.<String>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<String>newArrayList(\"1\", \"2\"));\n" +
"final java.util.function.Consumer<String> _function = new java.util.function.Consumer<String>() {\n" +
" public void accept(final String it) {\n" +
" String _switchResult = null;\n" +
" boolean _matched = false;\n" +
" if (it instanceof CharSequence) {\n" +
" _matched=true;\n" +
" _switchResult = it;\n" +
" }\n" +
" if (!_matched) {\n" +
" _switchResult = \"\";\n" +
" }\n" +
" final String res = _switchResult;\n" +
" }\n" +
"};\n" +
"list.forEach(_function);\n" +
"final java.util.function.Consumer<String> _function_1 = new java.util.function.Consumer<String>() {\n" +
" public void accept(final String it) {\n" +
" org.eclipse.xtext.xbase.lib.InputOutput.<String>println(it);\n" +
" }\n" +
"};\n" +
"list.forEach(_function_1);\n");
}
@Test
public void testSwitchScopes_Bug470586_03() throws Exception {
compilesTo(
"{\n" +
" val list = #[\"1\", \"2\"]\n" +
" list.forEach[\n" +
" val res = switch it {\n" +
" String, CharSequence : it\n" +
" default : ''\n" +
" }\n" +
" ]\n" +
" list.forEach[\n" +
" println(it)\n" +
" ]\n" +
"}\n",
"\n" +
"final java.util.List<String> list = java.util.Collections.<String>unmodifiableList(org.eclipse.xtext.xbase.lib.CollectionLiterals.<String>newArrayList(\"1\", \"2\"));\n" +
"final java.util.function.Consumer<String> _function = new java.util.function.Consumer<String>() {\n" +
" public void accept(final String it) {\n" +
" String _switchResult = null;\n" +
" boolean _matched = false;\n" +
" if (it instanceof String) {\n" +
" _matched=true;\n" +
" }\n" +
" if (!_matched) {\n" +
" if (it instanceof CharSequence) {\n" +
" _matched=true;\n" +
" }\n" +
" }\n" +
" if (_matched) {\n" +
" _switchResult = it;\n" +
" }\n" +
" if (!_matched) {\n" +
" _switchResult = \"\";\n" +
" }\n" +
" final String res = _switchResult;\n" +
" }\n" +
"};\n" +
"list.forEach(_function);\n" +
"final java.util.function.Consumer<String> _function_1 = new java.util.function.Consumer<String>() {\n" +
" public void accept(final String it) {\n" +
" org.eclipse.xtext.xbase.lib.InputOutput.<String>println(it);\n" +
" }\n" +
"};\n" +
"list.forEach(_function_1);\n");
}
@Test
public void testFallThroughSwitch() throws Exception {
compilesTo(
"{\n" +
" switch x : 1 {\n" +
" case 1,\n" +
" case 2:\n" +
" 'lalala'\n" +
" }\n" +
"}\n",
"\n" +
"String _switchResult = null;\n" +
"final int x = 1;\n" +
"switch (x) {\n" +
" case 1:\n" +
" case 2:\n" +
" _switchResult = \"lalala\";\n" +
" break;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testFallThroughSwitch_2() throws Exception {
compilesTo(
"{\n" +
" switch x : 'lalala' {\n" +
" case 'a',\n" +
" case 'b',\n" +
" case 'c':\n" +
" 'lalala'\n" +
" }\n" +
"}\n",
"\n" +
"String _switchResult = null;\n" +
"final String x = \"lalala\";\n" +
"boolean _matched = false;\n" +
"if (java.util.Objects.equals(x, \"a\")) {\n" +
" _matched=true;\n" +
"}\n" +
"if (!_matched) {\n" +
" if (java.util.Objects.equals(x, \"b\")) {\n" +
" _matched=true;\n" +
" }\n" +
"}\n" +
"if (!_matched) {\n" +
" if (java.util.Objects.equals(x, \"c\")) {\n" +
" _matched=true;\n" +
" }\n" +
"}\n" +
"if (_matched) {\n" +
" _switchResult = \"lalala\";\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testFallThroughSwitch_3() throws Exception {
compilesTo(
"{\n" +
" switch x : 1 {\n" +
" case 1,\n" +
" case 2,\n" +
" default:\n" +
" 'lalala'\n" +
" }\n" +
"}\n",
"\n" +
"String _switchResult = null;\n" +
"final int x = 1;\n" +
"switch (x) {\n" +
" case 1:\n" +
" case 2:\n" +
" default:\n" +
" _switchResult = \"lalala\";\n" +
" break;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testFallThroughSwitch_4() throws Exception {
compilesTo(
"{\n" +
" switch x : 'lalala' {\n" +
" case 'a',\n" +
" case 'b',\n" +
" default:\n" +
" 'lalala'\n" +
" }\n" +
"}\n",
"\n" +
"String _switchResult = null;\n" +
"final String x = \"lalala\";\n" +
"boolean _matched = false;\n" +
"if (java.util.Objects.equals(x, \"a\")) {\n" +
" _matched=true;\n" +
"}\n" +
"if (!_matched) {\n" +
" if (java.util.Objects.equals(x, \"b\")) {\n" +
" _matched=true;\n" +
" }\n" +
"}\n" +
"if (_matched) {\n" +
" _switchResult = \"lalala\";\n" +
"}\n" +
"if (!_matched) {\n" +
" _switchResult = \"lalala\";\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testFallThroughSwitch_5() throws Exception {
compilesTo(
"{\n" +
" switch x : 1 {\n" +
" case 1:\n" +
" 'blabla'\n" +
" case 2,\n" +
" default:\n" +
" 'lalala'\n" +
" }\n" +
"}\n",
"\n" +
"String _switchResult = null;\n" +
"final int x = 1;\n" +
"switch (x) {\n" +
" case 1:\n" +
" _switchResult = \"blabla\";\n" +
" break;\n" +
" case 2:\n" +
" default:\n" +
" _switchResult = \"lalala\";\n" +
" break;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testFallThroughSwitch_6() throws Exception {
compilesTo(
"{\n" +
" switch x : new Object {\n" +
" String case 'a':\n" +
" 'blabla'\n" +
" Integer case 1,\n" +
" case 2,\n" +
" default:\n" +
" 'lalala'\n" +
" }\n" +
"}\n",
"\n" +
"String _switchResult = null;\n" +
"Object _object = new Object();\n" +
"final Object x = _object;\n" +
"boolean _matched = false;\n" +
"if (x instanceof String) {\n" +
" if (java.util.Objects.equals(x, \"a\")) {\n" +
" _matched=true;\n" +
" _switchResult = \"blabla\";\n" +
" }\n" +
"}\n" +
"if (!_matched) {\n" +
" if (x instanceof Integer) {\n" +
" if (java.util.Objects.equals(x, 1)) {\n" +
" _matched=true;\n" +
" }\n" +
" }\n" +
" if (!_matched) {\n" +
" if (java.util.Objects.equals(x, 2)) {\n" +
" _matched=true;\n" +
" }\n" +
" }\n" +
" if (_matched) {\n" +
" _switchResult = \"lalala\";\n" +
" }\n" +
"}\n" +
"if (!_matched) {\n" +
" _switchResult = \"lalala\";\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testFallThroughSwitch_7() throws Exception {
compilesTo(
"{\n" +
" switch x : 'lalala' as Object { \n" +
" String, Integer case 1: 0\n" +
" Integer, default: 1\n" +
" }\n" +
"}\n",
"\n" +
"int _switchResult = (int) 0;\n" +
"final Object x = ((Object) \"lalala\");\n" +
"boolean _matched = false;\n" +
"if (x instanceof String) {\n" +
" _matched=true;\n" +
"}\n" +
"if (!_matched) {\n" +
" if (x instanceof Integer) {\n" +
" if (java.util.Objects.equals(x, 1)) {\n" +
" _matched=true;\n" +
" }\n" +
" }\n" +
"}\n" +
"if (_matched) {\n" +
" _switchResult = 0;\n" +
"}\n" +
"if (!_matched) {\n" +
" if (x instanceof Integer) {\n" +
" _matched=true;\n" +
" _switchResult = 1;\n" +
" }\n" +
"}\n" +
"if (!_matched) {\n" +
" _switchResult = 1;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testFallThroughSwitch_8() throws Exception {
compilesTo(
"{\n" +
" switch x : 1 as Object {\n" +
" Integer,\n" +
" Double: x\n" +
" }\n" +
"}\n",
"\n" +
"Number _switchResult = null;\n" +
"final Object x = ((Object) Integer.valueOf(1));\n" +
"boolean _matched = false;\n" +
"if (x instanceof Integer) {\n" +
" _matched=true;\n" +
"}\n" +
"if (!_matched) {\n" +
" if (x instanceof Double) {\n" +
" _matched=true;\n" +
" }\n" +
"}\n" +
"if (_matched) {\n" +
" _switchResult = ((Number)x);\n" +
"}\n" +
"return ((Number)_switchResult);\n");
}
@Test
public void testEmptySwitch() throws Exception {
compilesTo(
"{\n" +
" switch 1 {\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final int _switchValue = 1;\n" +
"switch (_switchValue) {\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_2() throws Exception {
compilesTo(
"{\n" +
" switch 1 {\n" +
" default: {\n" +
" }\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final int _switchValue = 1;\n" +
"switch (_switchValue) {\n" +
" default:\n" +
" _switchResult = null;\n" +
" break;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_3() throws Exception {
compilesTo(
"{\n" +
" switch Object {\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Class<Object> Object = Object.class;\n" +
"boolean _matched = false;\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_4() throws Exception {
compilesTo(
"{\n" +
" switch Object {\n" +
" default: {\n" +
" }\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Class<Object> Object = Object.class;\n" +
"boolean _matched = false;\n" +
"if (!_matched) {\n" +
" _switchResult = null;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_5() throws Exception {
compilesTo(
"{\n" +
" switch Thread.State.NEW {\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Thread.State _switchValue = Thread.State.NEW;\n" +
"if (_switchValue != null) {\n" +
" switch (_switchValue) {\n" +
" default:\n" +
" break;\n" +
" }\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_6() throws Exception {
compilesTo(
"{\n" +
" switch Thread.State.NEW {\n" +
" default: {\n" +
" }\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Thread.State _switchValue = Thread.State.NEW;\n" +
"if (_switchValue != null) {\n" +
" switch (_switchValue) {\n" +
" default:\n" +
" _switchResult = null;\n" +
" break;\n" +
" }\n" +
"} else {\n" +
" _switchResult = null;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_7() throws Exception {
compilesTo(
"{\n" +
" switch Thread.State x : Thread.State.NEW {\n" +
" default: {\n" +
" }\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Thread.State x = Thread.State.NEW;\n" +
"if (x != null) {\n" +
" switch (x) {\n" +
" default:\n" +
" _switchResult = null;\n" +
" break;\n" +
" }\n" +
"} else {\n" +
" _switchResult = null;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_8() throws Exception {
compilesTo(
"{\n" +
" switch (Thread.State x : Thread.State.NEW) {\n" +
" default: {\n" +
" }\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Thread.State x = Thread.State.NEW;\n" +
"if (x != null) {\n" +
" switch (x) {\n" +
" default:\n" +
" _switchResult = null;\n" +
" break;\n" +
" }\n" +
"} else {\n" +
" _switchResult = null;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_9() throws Exception {
compilesTo(
"{\n" +
" switch Object x : Thread.State.NEW {\n" +
" default: {\n" +
" }\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Object x = Thread.State.NEW;\n" +
"boolean _matched = false;\n" +
"if (!_matched) {\n" +
" _switchResult = null;\n" +
"}\n" +
"return _switchResult;\n");
}
@Test
public void testEmptySwitch_10() throws Exception {
compilesTo(
"{\n" +
" switch (Object x : Thread.State.NEW) {\n" +
" default: {\n" +
" }\n" +
" }\n" +
"}\n",
"\n" +
"Object _switchResult = null;\n" +
"final Object x = Thread.State.NEW;\n" +