-
Notifications
You must be signed in to change notification settings - Fork 317
/
XtendValidationTest.java
3255 lines (2813 loc) · 126 KB
/
XtendValidationTest.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) 2011, 2017 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.xtend.core.tests.validation;
import static org.eclipse.xtend.core.validation.IssueCodes.*;
import static org.eclipse.xtend.core.xtend.XtendPackage.Literals.*;
import static org.eclipse.xtext.xbase.XbasePackage.Literals.*;
import static org.eclipse.xtext.xbase.validation.IssueCodes.*;
import static org.eclipse.xtext.xtype.XtypePackage.Literals.*;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtend.core.tests.AbstractXtendTestCase;
import org.eclipse.xtend.core.validation.IssueCodes;
import org.eclipse.xtend.core.xtend.XtendAnnotationType;
import org.eclipse.xtend.core.xtend.XtendClass;
import org.eclipse.xtend.core.xtend.XtendConstructor;
import org.eclipse.xtend.core.xtend.XtendEnum;
import org.eclipse.xtend.core.xtend.XtendFile;
import org.eclipse.xtend.core.xtend.XtendFunction;
import org.eclipse.xtend.core.xtend.XtendInterface;
import org.eclipse.xtend.core.xtend.XtendMember;
import org.eclipse.xtend.core.xtend.XtendPackage;
import org.eclipse.xtend.core.xtend.XtendTypeDeclaration;
import org.eclipse.xtext.common.types.JvmConstructor;
import org.eclipse.xtext.common.types.JvmDeclaredType;
import org.eclipse.xtext.common.types.TypesPackage;
import org.eclipse.xtext.testing.validation.ValidationTestHelper;
import org.eclipse.xtext.preferences.IPreferenceValuesProvider.SingletonPreferenceValuesProvider;
import org.eclipse.xtext.preferences.MapBasedPreferenceValues;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.util.StringInputStream;
import org.eclipse.xtext.validation.Issue;
import org.eclipse.xtext.xbase.XBlockExpression;
import org.eclipse.xtext.xbase.XbasePackage;
import org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotationsPackage;
import org.eclipse.xtext.xbase.validation.ReadAndWriteTracking;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.Inject;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public class XtendValidationTest extends AbstractXtendTestCase {
@Inject
protected ValidationTestHelper helper;
@Inject
private ReadAndWriteTracking readAndWriteTracking;
private MapBasedPreferenceValues preferences;
@Inject
public void setPreferences(SingletonPreferenceValuesProvider prefProvider) {
preferences = prefProvider.getPreferenceValues(null);
}
@Before
public void setSeverity() {
preferences.put(IssueCodes.JAVA_DOC_LINKING_DIAGNOSTIC, "error");
}
@After
public void clearPreferences() {
preferences.clear();
}
@Test public void testTemplateCompilesToStringConcatenation_01() throws Exception {
XtendClass clazz = clazz(
"import org.eclipse.xtend2.lib.StringConcatenationClient\n"
+ "class Z {"
+ " def StringConcatenationClient m() {"
+ " var myVar = null"
+ " return '''«myVar»'''"
+ " }"
+ "}");
helper.assertError(clazz, XbasePackage.Literals.XFEATURE_CALL, INVALID_MUTABLE_VARIABLE_ACCESS,
"Cannot refer to the non-final variable myVar inside this template.",
"This template compiles to an anonymous subclass of StringConcatenationClient because of its target type.");
}
@Test public void testTemplateCompilesToStringConcatenation_02() throws Exception {
XtendClass clazz = clazz(
"import org.eclipse.xtend2.lib.StringConcatenationClient\n"
+ "class Z {"
+ " def StringConcatenationClient m() {"
+ " val myVar = null"
+ " return '''«myVar»'''"
+ " }"
+ "}");
helper.assertNoErrors(clazz);
}
@Test public void testTemplateCompilesToStringConcatenation_03() throws Exception {
XtendClass clazz = clazz(
"import org.eclipse.xtend2.lib.StringConcatenationClient\n"
+ "class Z {"
+ " def m() {"
+ " val myVar = null"
+ " return '''«myVar»'''"
+ " }"
+ "}");
helper.assertNoErrors(clazz);
}
@Test public void testExtensionMayNotBePrimitive_01() throws Exception {
XtendClass clazz = clazz("class Z { extension int x = 1 }");
helper.assertError(clazz, XTEND_FIELD, INVALID_EXTENSION_TYPE);
}
@Test public void testExtensionMayNotBePrimitive_02() throws Exception {
XtendClass clazz = clazz("class Z { extension int = 1 }");
helper.assertError(clazz, XTEND_FIELD, INVALID_EXTENSION_TYPE);
}
@Test public void testExtensionMayNotBePrimitive_03() throws Exception {
XtendClass clazz = clazz("class Z { extension x = 1.toString }");
helper.assertNoError(clazz, INVALID_EXTENSION_TYPE);
}
@Test public void testExtensionMayNotBePrimitive_04() throws Exception {
XtendClass clazz = clazz("class Z { def void m(extension int i) {} }");
helper.assertError(clazz, XTEND_PARAMETER, INVALID_EXTENSION_TYPE);
}
@Test public void testExtensionMayNotBePrimitive_05() throws Exception {
XtendClass clazz = clazz("class Z { def void m(extension Object i) {} }");
helper.assertNoError(clazz, INVALID_EXTENSION_TYPE);
}
@Test public void testExtensionMayNotBePrimitive_06() throws Exception {
XtendClass clazz = clazz("class Z { def void m() { try {} catch(extension int i) {} } }");
helper.assertNoError(clazz, INVALID_EXTENSION_TYPE);
helper.assertError(clazz, XTEND_FORMAL_PARAMETER, INCOMPATIBLE_TYPES);
}
@Test public void testExtensionMayNotBePrimitive_07() throws Exception {
XtendClass clazz = clazz("class Z { def void m() { extension var i = 0 } }");
helper.assertError(clazz, XTEND_VARIABLE_DECLARATION, INVALID_EXTENSION_TYPE);
}
@Test public void testNoSideEffects() throws Exception {
XtendClass clazz = clazz("class Z { def void foo() { 'foo' } }");
helper.assertError(clazz, XSTRING_LITERAL, INVALID_INNER_EXPRESSION);
}
@Test public void testNoSideEffects_02() throws Exception {
XtendClass clazz = clazz("class Z { def void foo() { " +
" val blockElement =\n" +
" if ('foo'.length > 42) {\n" +
" 'test'\n" +
" } else {\n" +
" 'bar'\n" +
" }\n" +
" blockElement.toString " +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testNoSideEffects_03() throws Exception {
XtendClass clazz = clazz("class Z { def void foo() { " +
" val blockElement =\n" +
" switch switchOver : if ('x'.length>1) 'foo' {\n" +
" case try { 'test' } finally { 'sideeffect'.length } : 52" +
" default : switch switchOver {" +
" case 'x' : 42" +
" }\n" +
" }\n" +
" ('f' + blockElement).toString " +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testNoSideEffects_04() throws Exception {
XtendClass clazz = clazz("class Z { def void foo() { " +
" ''' no side-effect '''" +
" }" +
"}");
helper.assertError(clazz, RICH_STRING, INVALID_INNER_EXPRESSION);
}
@Test public void testNoSideEffects_05() throws Exception {
XtendClass clazz = clazz("class Z { def void foo() { " +
" ''' no «'foo'.length» side-effect '''" +
" }" +
"}");
helper.assertError(clazz, RICH_STRING, INVALID_INNER_EXPRESSION);
}
@Test public void testFieldInitializerType_01() throws Exception {
XtendClass clazz = clazz("class Z { String s = 1 }");
helper.assertError(clazz, XNUMBER_LITERAL, INCOMPATIBLE_TYPES, "cannot convert from int to String");
}
@Test public void testFieldInitializerType_02() throws Exception {
XtendClass clazz = clazz("class Z { String s = 1.toString }");
helper.assertNoErrors(clazz);
}
@Test public void testFieldInitializerType_03() throws Exception {
XtendClass clazz = clazz("class Z { String s = return 1 }");
helper.assertError(clazz, XNUMBER_LITERAL, INCOMPATIBLE_TYPES);
}
@Test public void testFieldInitializerType_04() throws Exception {
XtendClass clazz = clazz("class Z { String s = return '' }");
helper.assertNoErrors(clazz);
}
@Test public void testForwardReferenceInFieldInitializer_01() throws Exception {
XtendClass clazz = clazz("class Z { String s = s }");
helper.assertError(clazz, XFEATURE_CALL, ILLEGAL_FORWARD_REFERENCE);
}
@Test public void testForwardReferenceInFieldInitializer_02() throws Exception {
XtendClass clazz = clazz("class Z { String s1 = newArrayList(s2 + '').toString String s2 = '' }");
helper.assertError(clazz, XFEATURE_CALL, ILLEGAL_FORWARD_REFERENCE);
}
@Test public void testForwardReferenceInFieldInitializer_03() throws Exception {
XtendClass clazz = clazz("class Z { static String s = s }");
helper.assertError(clazz, XFEATURE_CALL, ILLEGAL_FORWARD_REFERENCE);
}
@Test public void testForwardReferenceInFieldInitializer_04() throws Exception {
XtendClass clazz = clazz("class Z { static String s1 = s2 static String s2 = '' }");
helper.assertError(clazz, XFEATURE_CALL, ILLEGAL_FORWARD_REFERENCE);
}
@Test public void testForwardReferenceInFieldInitializer_05() throws Exception {
XtendClass clazz = clazz("class Z { String s = '' }");
helper.assertNoErrors(clazz);
}
@Test public void testForwardReferenceInFieldInitializer_06() throws Exception {
XtendClass clazz = clazz("class Z { String s1 = '' String s2 = s1 }");
helper.assertNoErrors(clazz);
}
@Test public void testForwardReferenceInFieldInitializer_07() throws Exception {
XtendClass clazz = clazz("class Z { static String s = '' }");
helper.assertNoErrors(clazz);
}
@Test public void testForwardReferenceInFieldInitializer_08() throws Exception {
XtendClass clazz = clazz("class Z { static String s1 = '' static String s2 = s1 }");
helper.assertNoErrors(clazz);
}
@Test public void testForwardReferenceInFieldInitializer_09() throws Exception {
XtendClass clazz = clazz("class Z { static val x = [| y] static val y = [|''] }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorInInterface() throws Exception {
XtendInterface interfaze = interfaze("interface Foo { new() {} }");
helper.assertError(interfaze, XTEND_CONSTRUCTOR, CONSTRUCTOR_NOT_PERMITTED);
}
@Test public void testCircularConstructor_01() throws Exception {
XtendClass clazz = clazz("class Z { new() { this() }}");
helper.assertError(clazz, XFEATURE_CALL, CIRCULAR_CONSTRUCTOR_INVOCATION);
}
@Test public void testCircularConstructor_02() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(1) } new(int a) { this() } }");
helper.assertError(clazz.getMembers().get(0), XFEATURE_CALL, CIRCULAR_CONSTRUCTOR_INVOCATION);
helper.assertError(clazz.getMembers().get(1), XFEATURE_CALL, CIRCULAR_CONSTRUCTOR_INVOCATION);
}
@Test public void testCircularConstructor_03() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(1) } new(int a) { super() } }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorCallIsFirst_01() throws Exception {
XtendClass clazz = clazz("class Z { new() { super() } new(int a) { this() } }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorCallIsFirst_02() throws Exception {
XtendClass clazz = clazz("class Z { new() { { this() } }}");
helper.assertError(clazz, XFEATURE_CALL, INVALID_CONSTRUCTOR_INVOCATION);
}
@Test public void testConstructorCallIsFirst_03() throws Exception {
XtendClass clazz = clazz("class Z { new() { if (true) super() } }");
helper.assertError(clazz, XFEATURE_CALL, INVALID_CONSTRUCTOR_INVOCATION);
}
@Test public void testConstructorArgumentIsValid_01() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(1) } new(int i) {} }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_02() throws Exception {
XtendClass clazz = clazz("class Z { static int j new() { this(j) } new(int i) {} }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_03() throws Exception {
XtendClass clazz = clazz("class Z { int j new() { this(j) } new(int i) {} }");
helper.assertError(clazz.getMembers().get(1), XFEATURE_CALL, INVALID_CONSTRUCTOR_ARGUMENT);
}
@Test public void testConstructorArgumentIsValid_04() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(z()) } new(Object o) {} def z() { null } }");
helper.assertError(clazz.getMembers().get(0), XFEATURE_CALL, INVALID_CONSTRUCTOR_ARGUMENT);
}
@Test public void testConstructorArgumentIsValid_05() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(null as Object) } new(Object o) {} }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_06() throws Exception {
XtendClass clazz = clazz("class Z { new(Object o) { this(o as String) } new(String o) {} }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_07() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(z()) } new(Object o) {} def static z() { null } }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_08() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(z() as String) } new(Object o) {} def static z() { null } }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_09() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(z() as String) } new(Object o) {} def z() { null } }");
helper.assertError(clazz.getMembers().get(0), XFEATURE_CALL, INVALID_CONSTRUCTOR_ARGUMENT);
}
@Test public void testConstructorArgumentIsValid_10() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(z().toString + '') } new(Object o) {} def static z() { null } }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_11() throws Exception {
XtendClass clazz = clazz("class Z { new() { this(z().toString + '') } new(Object o) {} def z() { null } }");
helper.assertError(clazz.getMembers().get(0), XFEATURE_CALL, INVALID_CONSTRUCTOR_ARGUMENT);
}
@Test public void testConstructorArgumentIsValid_12() throws Exception {
XtendClass clazz = clazz("class Foo extends Exception { new(java.util.List<Object> foo) { super(foo.map[toString].join) } }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_13() throws Exception {
XtendClass clazz = clazz("class Foo extends Exception { new(Object it) { super(toString) } }");
helper.assertNoErrors(clazz);
}
@Test public void testConstructorArgumentIsValid_14() throws Exception {
XtendClass clazz = clazz("class Foo extends Exception { new(Object obj) { super(toString) } }");
helper.assertError(clazz.getMembers().get(0), XFEATURE_CALL, INVALID_CONSTRUCTOR_ARGUMENT);
}
@Test public void testConstructorArgumentIsValid_15() throws Exception {
XtendClass clazz = clazz("class Foo extends Exception { new(Object it) { super(this.toString) } }");
helper.assertError(clazz.getMembers().get(0), XMEMBER_FEATURE_CALL, INVALID_CONSTRUCTOR_ARGUMENT);
}
@Test public void testConstructorDuplicate() throws Exception {
XtendClass clazz = clazz("class K { new(Object o) {} new(Object o) {} }");
helper.assertError(clazz, XTEND_CONSTRUCTOR, DUPLICATE_METHOD);
helper.assertError(clazz, XTEND_CONSTRUCTOR, DUPLICATE_METHOD);
}
@Test public void testConstructorDuplicateErasure() throws Exception {
XtendClass clazz = clazz("class K { new(java.util.List<Object> o) {} new(java.util.List<String> o) {} }");
helper.assertError(clazz, XTEND_CONSTRUCTOR, DUPLICATE_METHOD);
helper.assertError(clazz, XTEND_CONSTRUCTOR, DUPLICATE_METHOD);
}
@Test public void testConstructorDoesNotSupportTypeArguments() throws Exception {
XtendClass clazz = clazz("class K { new<T>() {} }");
helper.assertError(clazz, XTEND_CONSTRUCTOR, CONSTRUCTOR_TYPE_PARAMS_NOT_SUPPORTED);
}
@Test public void testMissingConstructor() throws Exception {
XtendClass clazz = clazz("class K extends test.NoDefaultConstructor {}");
helper.assertError(clazz, XTEND_CLASS, MISSING_CONSTRUCTOR);
}
@Test public void testMissingSuperConstructorCall() throws Exception {
XtendClass clazz = clazz("class K extends test.NoDefaultConstructor { new() {} }");
helper.assertError(clazz, XTEND_CONSTRUCTOR, MUST_INVOKE_SUPER_CONSTRUCTOR);
}
@Test public void testSuperConstructorCall() throws Exception {
XtendClass clazz = clazz("class K extends test.NoDefaultConstructor { new() { super('') } }");
helper.assertNoErrors(clazz);
}
@Test public void testReturnStatement() throws Exception {
XtendClass clazz = clazz("class Z { def void foo() { return 'holla' }}");
helper.assertError(clazz, XRETURN_EXPRESSION, INVALID_RETURN);
}
@Test public void testReturnStatement1() throws Exception {
XtendClass clazz = clazz("class Z { def Object foo() { return }}");
helper.assertError(clazz, XRETURN_EXPRESSION, INVALID_RETURN);
}
@Test public void testReturnStatement2() throws Exception {
XtendClass clazz = clazz("class Z { def Object foo() { val Object x = if (false) return }}");
helper.assertError(clazz, XRETURN_EXPRESSION, INVALID_RETURN);
}
@Test public void testReturnStatement3() throws Exception {
XtendClass clazz = clazz("class Z { def Object foo() { val Object x = if (false) return '' }}");
helper.assertNoError(clazz, INVALID_RETURN);
}
@Test public void testReturnStatement4() throws Exception {
XtendClass clazz = clazz("class Z { def void foo() { val Object x = if (false) return; x.toString }}");
helper.assertNoError(clazz, INVALID_RETURN);
}
@Test public void testReturnStatement5() throws Exception {
XtendClass clazz = clazz("class Z { def foo() { val Object x = if (false) return; x }}");
helper.assertError(clazz, XFEATURE_CALL, INVALID_INNER_EXPRESSION);
}
@Test public void testReturnStatement6() throws Exception {
XtendClass clazz = clazz("class Z { def foo() { val Object x = if (false) return ''; x }}");
helper.assertNoError(clazz, INVALID_RETURN);
}
@Test public void testBug_357230() throws Exception {
XtendClass clazz = clazz(
"package x class Z {" +
" def dispatch _foo(Object x, boolean b) {}\n" +
" def dispatch _foo(String x, boolean b) {}\n" +
"}\n");
helper.assertError(clazz, XTEND_FUNCTION, DISPATCH_FUNC_NAME_STARTS_WITH_UNDERSCORE);
}
@Test public void testAnnotationTarget_00() throws Exception {
XtendClass clazz = clazz("@testdata.Annotation2('foo') class X { }");
helper.assertError(clazz, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET, "@Annotation2");
}
@Test public void testAnnotationTarget_01() throws Exception {
XtendClass clazz = clazz("class X { @testdata.Annotation2('foo') String foo }");
helper.assertNoErrors(clazz);
}
@Test public void testAnnotationTarget_02() throws Exception {
XtendClass clazz = clazz("class X { @testdata.Annotation2('foo') def String foo() }");
helper.assertError(clazz, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET);
}
@Test public void testAnnotationTarget_03() throws Exception {
XtendClass clazz = clazz("class X { def String foo(@testdata.Annotation2('foo') String bar) }");
helper.assertError(clazz, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET);
}
@Test public void testAnnotationTarget_04() throws Exception {
XtendEnum enumeration = enumeration("@testdata.Annotation2('foo') enum X { }");
helper.assertError(enumeration, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET, "@Annotation2");
}
@Test public void testAnnotationTarget_05() throws Exception {
XtendInterface interfaze = interfaze("@testdata.Annotation2('foo') interface X { }");
helper.assertError(interfaze, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET, "@Annotation2");
}
@Test public void testAnnotationTarget_06() throws Exception {
XtendAnnotationType annotationType = annotationType("@testdata.Annotation2('foo') annotation X { }");
helper.assertError(annotationType, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET, "@Annotation2");
}
@Test public void testAnnotationTarget_07() throws Exception {
XtendAnnotationType annotationType = annotationType("@testdata.Annotation4 annotation A {}");
helper.assertNoErrors(annotationType);
}
@Test public void testAnnotationTarget_08() throws Exception {
XtendClass clazz = clazz("@testdata.Annotation4 class C {}");
helper.assertNoErrors(clazz);
}
@Test public void testAnnotationTarget_09() throws Exception {
XtendAnnotationType annotationType = annotationType("@testdata.Annotation5 annotation A {}");
helper.assertNoErrors(annotationType);
}
@Test public void testAnnotationTarget_10() throws Exception {
XtendClass clazz = clazz("@testdata.Annotation5 class C {}");
helper.assertError(clazz, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET);
}
@Test public void testAnnotationTarget_11() throws Exception {
XtendAnnotationType annotationType = annotationType("annotation A { @testdata.Annotation6 String value = 'Issue206'}");
helper.assertNoErrors(annotationType);
}
@Test public void testAnnotationTarget_12() throws Exception {
XtendClass clazz = clazz("class C { @testdata.Annotation6 String value = 'Issue206'}");
helper.assertError(clazz, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_WRONG_TARGET);
}
@Test public void testMultipleAnnotations_00() throws Exception {
XtendClass clazz = clazz("@testdata.Annotation4 @testdata.Annotation4 class X { }");
helper.assertError(clazz, XAnnotationsPackage.Literals.XANNOTATION, ANNOTATION_MULTIPLE, "@Annotation4");
}
@Test public void testMultipleAnnotations_01() throws Exception {
XtendClass clazz = clazz("@testdata.Annotation1(value = true,children=#[@testdata.Annotation2('bar'), @testdata.Annotation2('foo')]) class X { }");
helper.assertNoErrors(clazz);
}
@Test public void testShadowingVariableNames_00() throws Exception {
XtendClass clazz = clazz("class X { def foo() { val this = 'foo' } }");
helper.assertError(clazz, XVARIABLE_DECLARATION, VARIABLE_NAME_DISALLOWED);
}
@Test public void testShadowingVariableNames_01() throws Exception {
XtendClass clazz = clazz("class X { def foo() { val ^super = 'foo' } }");
helper.assertError(clazz, XVARIABLE_DECLARATION, VARIABLE_NAME_DISALLOWED);
}
@Test public void testShadowingVariableNames_03() throws Exception {
XtendClass clazz = clazz("class X { def foo(String this) { } }");
helper.assertError(clazz, XTEND_PARAMETER, VARIABLE_NAME_DISALLOWED);
}
@Test public void testShadowingVariableNames_04() throws Exception {
XtendClass clazz = clazz("class X { def foo(String x) ''' «val x = 'foo'» «x» ''' }");
helper.assertError(clazz, XVARIABLE_DECLARATION, VARIABLE_NAME_SHADOWING);
}
@Test public void testShadowingVariableNames_05() throws Exception {
XtendClass clazz = clazz("class X { def foo() ''' «val x = 'foo'» «val x = 'bar'» ''' }");
helper.assertError(clazz, XVARIABLE_DECLARATION, VARIABLE_NAME_SHADOWING);
}
@Test public void testSelf_0() throws Exception {
XtendClass clazz = clazz("class X { def foo() { val self = 1 } }");
helper.assertWarning(clazz, XVARIABLE_DECLARATION, VARIABLE_NAME_DISCOURAGED);
}
@Test public void testSelf_1() throws Exception {
XtendClass clazz = clazz("class X { def foo() { val ()=>void x = [| self.apply ]} }");
helper.assertNoError(clazz, VARIABLE_NAME_DISCOURAGED);
}
@Test public void testSelf_2() throws Exception {
XtendClass clazz = clazz("class X { public val self = '' }");
helper.assertWarning(clazz, XTEND_FIELD, VARIABLE_NAME_DISCOURAGED);
}
@Test public void testSelf_3() throws Exception {
XtendClass clazz = clazz("class X { def foo(String self) {} }");
helper.assertWarning(clazz, XTEND_PARAMETER, VARIABLE_NAME_DISCOURAGED);
}
@Test public void testVoidInDependency() throws Exception {
var source = "class X { @Inject void v }";
XtendClass clazz = clazz(source);
helper.assertError(clazz, TypesPackage.Literals.JVM_TYPE_REFERENCE, INVALID_USE_OF_TYPE,
source.indexOf("void"), "void".length());
}
@Test public void testVoidInReturn() throws Exception {
XtendFunction function = function("def void foo() { }");
helper.assertNoError(function, INVALID_USE_OF_TYPE);
}
@Test public void testParameterTypeMayNotBeVoid() throws Exception {
XtendFunction function = function("def void foo(void myParam) { }");
helper.assertError(function, TypesPackage.Literals.JVM_TYPE_REFERENCE, INVALID_USE_OF_TYPE);
}
@Test public void testVarArgIsNotExtension() throws Exception {
XtendFunction function = function("def void foo(extension String... myParam) { }");
helper.assertError(function, XtendPackage.Literals.XTEND_PARAMETER, INVALID_USE_OF_VAR_ARG, "A vararg may not be an extension.");
}
@Test public void testVarArgMustBeLast_0() throws Exception {
XtendFunction function = function("def void foo(String... myParam, String x) { }");
helper.assertError(function, XtendPackage.Literals.XTEND_PARAMETER, INVALID_USE_OF_VAR_ARG);
}
@Test public void testVarArgMustBeLast_1() throws Exception {
XtendConstructor constr = constructor("new(String... myParam, String x) { }");
helper.assertError(constr, XtendPackage.Literals.XTEND_PARAMETER, INVALID_USE_OF_VAR_ARG);
}
@Test public void testVarArgMustBeLast_2() throws Exception {
XtendConstructor constr = constructor("new(String... myParam) { }");
helper.assertNoErrors(constr);
}
@Test public void testVarArgMustBeLast_3() throws Exception {
XtendConstructor constr = constructor("new(int x, String... myParam) { }");
helper.assertNoErrors(constr);
}
@Test public void testCreateMayNotReturnVoid() throws Exception {
XtendFunction function = function("def void create result: new Object() newObject() { }");
helper.assertError(function, TypesPackage.Literals.JVM_TYPE_REFERENCE, INVALID_USE_OF_TYPE, "void", "create", "newObject");
}
@Test public void testCreateMayNotBeStatic() throws Exception {
XtendFunction function = function("def static create new Object() newObject() { }");
helper.assertError(function, XTEND_FUNCTION, INVALID_USE_OF_STATIC, "not", "static");
}
@Test public void testCreateExpressionMayNotReturnVoid_01() throws Exception {
XtendFunction function = function("def create result: while(true){} illegal() { }");
helper.assertError(function, XTEND_FUNCTION, INVALID_USE_OF_TYPE, "void", "create", "method", "illegal");
}
@Test public void testCreateExpressionMayNotReturnVoid_02() throws Exception {
XtendFunction function = function("def String create result: while(true){} illegal() { }");
helper.assertError(function, XWHILE_EXPRESSION, INCOMPATIBLE_TYPES, "cannot convert from void to String");
}
@Test public void testCreateExpressionMayNotReturnVoid_03() throws Exception {
XtendClass clazz = clazz("class C { String s = '' def create s m(int i) {} }");
helper.assertNoError(clazz, org.eclipse.xtext.xbase.validation.IssueCodes.STATIC_ACCESS_TO_INSTANCE_MEMBER);
}
@Test public void testCreateExpressionMayAccessFields() throws Exception {
XtendFunction function = function("override create result: while(true){} toString() { }");
helper.assertError(function, XWHILE_EXPRESSION, INCOMPATIBLE_TYPES, "cannot convert from void to String");
}
@Test public void testNoReturnInCreateFunctions() throws Exception {
XtendFunction function = function("def create result: if (true) return 'foo' else 'bar' foo() { }");
helper.assertError(function, XRETURN_EXPRESSION, INVALID_EARLY_EXIT);
}
@Test public void testNoReturnInCreateFunctions_00() throws Exception {
XtendFunction function = function("def create result: [|if (true) return 'foo' else 'bar'] foo() { }");
helper.assertNoErrors(function);
}
@Test public void testNoReturnInCreateFunctions_01() throws Exception {
XtendFunction function = function("def create result:'foo' foo() { return 'bar' }");
helper.assertError(function, XRETURN_EXPRESSION, INVALID_RETURN, "Void functions cannot return a value.");
helper.assertNoError(function, INVALID_EARLY_EXIT);
}
@Test public void testNoReturnInCreateFunctions_02() throws Exception {
XtendFunction function = function("def create result:'foo' foo() { return }");
helper.assertNoErrors(function);
}
@Test public void testNoReturnInCreateFunctions_03() throws Exception {
XtendFunction function = function("def create result:'foo' foo() { [|return 'foo'].apply() }");
helper.assertNoErrors(function);
}
@Test public void testNoReturnInCreateFunctions_04() throws Exception {
XtendFunction function = function("def create result:'foo' foo() { if (true) 'foo'+'bar' else return 'baz' }");
helper.assertError(function, XRETURN_EXPRESSION, INVALID_RETURN, "Void functions cannot return a value.");
helper.assertNoError(function, INVALID_EARLY_EXIT);
}
@Test public void testTypeCompatibility_01() throws Exception {
XtendFile file = file(
"class Foo { public val Class<? extends java.util.Map<?, ?>> x = typeof(java.util.Map) }");
helper.assertError(file, XTYPE_LITERAL, INCOMPATIBLE_TYPES, "Type mismatch: cannot convert from Class<Map> to Class<? extends Map<?, ?>>");
}
@Test public void testTypeCompatibility_02() throws Exception {
XtendFile file = file(
"class Foo { public val Class<java.util.Map<?, ?>> x = java.util.Map }");
helper.assertError(file, XMEMBER_FEATURE_CALL, INCOMPATIBLE_TYPES, "Type mismatch: cannot convert from Class<Map> to Class<Map<?, ?>>");
}
@Test public void testReturnTypeCompatibility_00() throws Exception {
XtendFunction function = function("def void foo(int bar) { }");
helper.assertNoErrors(function);
}
@Test public void testReturnTypeCompatibility_01() throws Exception {
XtendFunction function = function("def String foo(int bar) { return 42 }");
helper.assertError(function, XNUMBER_LITERAL, INCOMPATIBLE_TYPES, "String", "int");
}
@Test public void testReturnTypeCompatibility_02() throws Exception {
XtendFunction function = function("def Object foo(int bar) { return 42 }");
helper.assertNoErrors(function);
}
@Test public void testReturnTypeCompatibility_03() throws Exception {
XtendFunction function = function(
"def String foo(int bar) { "
+ " if (true) {"
+ " return if (false) 42 else new Object()"
+ " }"
+ "}");
helper.assertError(function, XCONSTRUCTOR_CALL, INCOMPATIBLE_TYPES, "cannot convert from Object to String");
}
@Test public void testReturnTypeCompatibility_04() throws Exception {
XtendClass clazz = clazz("class Foo implements test.SomeInterface { def foo() { 1 } }");
helper.assertError(clazz.getMembers().get(0), XTEND_FUNCTION, MISSING_OVERRIDE);
}
@Test public void testReturnTypeCompatibility_05() throws Exception {
XtendClass clazz = clazz("class Foo implements test.SomeInterface { override foo() { true } }");
helper.assertNoErrors(clazz.getMembers().get(0));
}
@Test public void testReturnTypeCompatibility_06() throws Exception {
XtendClass clazz = clazz("class Foo { def dispatch void a(String x) {} def dispatch a(Object x) {return null} }");
helper.assertError(clazz.getMembers().get(1), XTEND_FUNCTION, INCOMPATIBLE_RETURN_TYPE);
}
@Test public void testReturnTypeCompatibility_07() throws Exception {
XtendClass clazz = clazz(
"class Foo { " +
" def void a() {" +
" val closure = [Integer i| return i]\n" +
" for (x : 1..100) closure.apply(x)" +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testReturnTypeCompatibility_08() throws Exception {
XtendClass clazz = clazz(
"class Foo { " +
" def void a() {" +
" val closure = [Integer i| i]\n" +
" for (x : 1..100) closure.apply(x)" +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testReturnTypeCompatibility_09() throws Exception {
XtendClass clazz = clazz(
"class Foo { " +
" def void a() {" +
" val (Integer)=>Integer closure = [Integer i| return i]\n" +
" for (x : 1..100) closure.apply(x)" +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testReturnTypeCompatibility_10() throws Exception {
XtendClass clazz = clazz(
"class Foo { " +
" def a() {" +
" val closure = [Integer i| return i]\n" +
" for (x : 1..100) closure.apply(x)" +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testReturnTypeCompatibility_11() throws Exception {
XtendClass clazz = clazz(
"class Foo { " +
" def a() {" +
" val closure = [Integer i| i]\n" +
" for (x : 1..100) closure.apply(x)" +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testReturnTypeCompatibility_12() throws Exception {
XtendClass clazz = clazz(
"class Foo { " +
" def a() {" +
" val (Integer)=>Integer closure = [Integer i| return i]\n" +
" for (x : 1..100) closure.apply(x)" +
" }" +
"}");
helper.assertNoErrors(clazz);
}
@Test public void testReturnTypeCompatibility_13() throws Exception {
XtendClass clazz = clazz(
"class C {\n" +
" def dispatch void minus(Object operand) {\n" +
" }\n" +
" def dispatch minus(Integer e) { return -e }\n" +
" def dispatch minus(Double e) { -e }\n" +
"}");
helper.assertError(clazz, XTEND_FUNCTION, INCOMPATIBLE_RETURN_TYPE, "Incompatible return type of dispatch method. Expected double but was void");
}
@Test public void testReturnTypeCompatibility_14() throws Exception {
XtendClass clazz = clazz(
"class C {\n" +
" def dispatch minus(Object operand) {\n" +
" return\n" +
" }\n" +
" def dispatch minus(Integer e) { return -e }\n" +
" def dispatch minus(Double e) { -e }\n" +
"}");
helper.assertError(clazz, XTEND_FUNCTION, INCOMPATIBLE_RETURN_TYPE, "Incompatible return type of dispatch method. Expected double but was void");
}
@Test public void testReturnTypeCompatibility_15() throws Exception {
XtendClass clazz = clazz(
"class C {\n" +
" def dispatch void minus(Object operand) {\n" +
" }\n" +
" def dispatch minus(Integer e) { -e }\n" +
" def dispatch minus(Double e) { -e }\n" +
"}");
helper.assertError(clazz, XTEND_FUNCTION, INCOMPATIBLE_RETURN_TYPE, "Incompatible return type of dispatch method. Expected double but was void");
}
@Test public void testReturnTypeCompatibility_16() throws Exception {
XtendClass clazz = clazz(
"class C {\n" +
" def dispatch minus(Object operand) {\n" +
" return\n" +
" }\n" +
" def dispatch minus(Integer e) { -e }\n" +
" def dispatch minus(Double e) { -e }\n" +
"}");
helper.assertError(clazz, XTEND_FUNCTION, INCOMPATIBLE_RETURN_TYPE, "Incompatible return type of dispatch method. Expected double but was void");
}
@Test public void testThrowsClauseCompatibility_00() throws Exception {
XtendClass clazz = clazz(
"class Foo {" +
" def foo() {" +
" throw new Exception()" +
" }" +
"}");
helper.assertNoError(clazz, UNHANDLED_EXCEPTION);
// helper.assertError(clazz, XBLOCK_EXPRESSION, UNHANDLED_EXCEPTION, "unhandled", "exception");
}
@Test public void testThrowsClauseCompatibility_01() throws Exception {
XtendClass clazz = clazz(
"class Foo {" +
" def foo() {" +
" throw new RuntimeException()" +
" }" +
"}");
helper.assertNoError(clazz, UNHANDLED_EXCEPTION);
}
@Test public void testThrowsClauseCompatibility_02() throws Exception {
XtendClass clazz = clazz(
"class Foo {" +
" def foo() throws RuntimeException {" +
" throw new Exception()" +
" }" +
"}");
helper.assertNoError(clazz, UNHANDLED_EXCEPTION);
// helper.assertError(clazz, XBLOCK_EXPRESSION, UNHANDLED_EXCEPTION, "unhandled", "exception");
}
@Test public void testThrowsClauseCompatibility_03() throws Exception {
XtendClass clazz = clazz(
"class Foo {" +
" def foo() throws Exception {" +
" throw new NoSuchFieldException()" +
" }" +
"}");
helper.assertNoError(clazz, UNHANDLED_EXCEPTION);
}
@Test public void testThrowsClauseCompatibility_04() throws Exception {
XtendClass clazz = clazz(
"class Foo {" +
" def foo() {" +
" try {" +
" throw new NoSuchFieldException()" +
" } catch(NoSuchFieldException e) {" +
" }" +
" }" +
"}");
helper.assertNoError(clazz, UNHANDLED_EXCEPTION);
}
@Test public void testAssignmentToFunctionParameter() throws Exception {
XtendFunction function = function("def void foo(int bar) { bar = 7 }");
helper.assertError(function, XASSIGNMENT, ASSIGNMENT_TO_FINAL, "Assignment", "final",
"parameter");
}
@Test public void testClassExtendsInterface() throws Exception {
var source = "class Foo extends Cloneable {}";
XtendClass clazz = clazz(source);
helper.assertError(clazz, XTEND_CLASS, CLASS_EXPECTED,
source.indexOf("Cloneable"), "Cloneable".length(),
"Superclass");
}
@Test public void testClassImplementsClass() throws Exception {
var source = "class Foo implements Object {}";
XtendClass clazz = clazz(source);
helper.assertError(clazz, XTEND_CLASS, INTERFACE_EXPECTED,
source.indexOf("Object"), "Object".length(),
"Implemented", "interface");
}
@Test public void testClassExtendsItself() throws Exception {
var source = "class Foo extends Foo {}";
XtendClass clazz = clazz(source);
helper.assertError(clazz, XTEND_CLASS, CYCLIC_INHERITANCE,
source.indexOf("Foo"), "Foo".length(),
"hierarchy", "cycles");
}
@Test public void testClassUniqueNames() throws Exception {
XtendClass clazz = clazz("class Foo {} class Foo {}");
helper.assertError(clazz, XTEND_CLASS, DUPLICATE_TYPE_NAME, "type", "already defined");
}
@Test public void testInterfaceExtendsInterface() throws Exception {
XtendInterface interfaze = interfaze("interface Foo extends Cloneable {}");
helper.assertNoErrors(interfaze);
}
@Test public void testInterfaceExtendsClass() throws Exception {
var source = "interface Foo extends Object {}";
XtendInterface interfaze = interfaze(source);
helper.assertError(interfaze, XTEND_INTERFACE, INTERFACE_EXPECTED,
source.indexOf("Object"), "Object".length(),
"Extended", "interface");
}
@Test public void testInterfaceExtendsItself() throws Exception {
XtendInterface interfaze = interfaze("interface Foo extends Foo {}");
helper.assertError(interfaze, XTEND_INTERFACE, CYCLIC_INHERITANCE, "hierarchy", "cycles");
}
@Test public void testTypesUniqueNames() throws Exception {
XtendFile file = file("class Foo {} interface Foo {} annotation Foo {}");
helper.assertError(file, XTEND_INTERFACE, DUPLICATE_TYPE_NAME, "type", "already defined");
helper.assertError(file, XTEND_ANNOTATION_TYPE, DUPLICATE_TYPE_NAME, "type", "already defined");
}
@Test public void testNestedTypesUniqueNames() throws Exception {
XtendFile file = file("class C { static class Foo {} interface Foo {} annotation Foo {} }");
helper.assertError(file, XTEND_INTERFACE, DUPLICATE_TYPE_NAME, "Duplicate nested type Foo");
helper.assertError(file, XTEND_ANNOTATION_TYPE, DUPLICATE_TYPE_NAME, "Duplicate nested type Foo");
}
@Test public void testNestedTypesOuterNameShadowing() throws Exception {
XtendFile file = file("class C { class C {} }");
helper.assertError(file, XTEND_CLASS, INVALID_MEMBER_NAME, "The nested type C cannot hide an enclosing type");
}
@Test public void testInheritanceCycle() throws Exception {
var source = "package test "
+ "class Foo extends Bar {}"
+ "class Bar extends Baz {}"
+ "class Baz extends Foo {}";
Iterator<XtendTypeDeclaration> types = file(source).getXtendTypes().iterator();
helper.assertError(types.next(), XTEND_CLASS, CYCLIC_INHERITANCE,
source.indexOf("Foo"), "Foo".length(),
"hierarchy", "cycles");
helper.assertError(types.next(), XTEND_CLASS, CYCLIC_INHERITANCE,
source.lastIndexOf("Bar"), "Bar".length(),
"hierarchy", "cycles");
helper.assertError(types.next(), XTEND_CLASS, CYCLIC_INHERITANCE,
source.lastIndexOf("Baz"), "Baz".length(),
"hierarchy", "cycles");
}
@Test public void testInheritanceCycle_1() throws Exception {
Iterator<XtendTypeDeclaration> types = file("package test "
+ "class Foo extends Bar {}"
+ "class Bar extends Foo {}").getXtendTypes().iterator();
helper.assertError(types.next(), XTEND_CLASS, CYCLIC_INHERITANCE, "hierarchy", "cycles");
helper.assertError(types.next(), XTEND_CLASS, CYCLIC_INHERITANCE, "hierarchy", "cycles");
}
@Test public void testInheritanceCycle_2() throws Exception {
Iterator<XtendTypeDeclaration> types = file("package test "
+ "interface Foo extends Bar {}"
+ "interface Bar extends Foo {}").getXtendTypes().iterator();