1
1
/*
2
- * Copyright 2009-2020 the original author or authors.
2
+ * Copyright 2009-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .eclipse .jdt .groovy .core .tests .xform ;
17
17
18
+ import static org .eclipse .jdt .groovy .core .tests .GroovyBundle .isAtLeastGroovy ;
19
+ import static org .junit .Assume .assumeTrue ;
20
+
18
21
import org .eclipse .jdt .groovy .core .tests .basic .GroovyCompilerTestSuite ;
19
22
import org .junit .Test ;
20
23
23
26
*/
24
27
public final class NamedVariantTests extends GroovyCompilerTestSuite {
25
28
26
- @ Test
29
+ private static final String COLOR_CLASS =
30
+ "@groovy.transform.ToString(includeNames=true)\n " +
31
+ "class Color {\n " +
32
+ " int r, g, b\n " +
33
+ "}\n " ;
34
+
35
+ @ Test // GROOVY-9183
27
36
public void testNamedVariant1 () {
28
37
//@formatter:off
29
38
String [] sources = {
@@ -35,13 +44,10 @@ public void testNamedVariant1() {
35
44
" return c\n " +
36
45
"}\n " +
37
46
"\n " +
38
- "print m(g:12, b:42, r:12)" ,
47
+ "print m(g:12, b:42, r:12)\n " ,
39
48
40
49
"Color.groovy" ,
41
- "@groovy.transform.ToString(includeNames=true)\n " +
42
- "class Color {\n " +
43
- " Integer r, g, b\n " +
44
- "}\n " ,
50
+ COLOR_CLASS ,
45
51
};
46
52
//@formatter:on
47
53
@@ -60,13 +66,10 @@ public void testNamedVariant2() {
60
66
" return [color, alpha].join(' ')\n " +
61
67
"}\n " +
62
68
"\n " +
63
- "print m(r:1, g:2, b:3, a: 0)" ,
69
+ "print m(r:1, g:2, b:3, a: 0)\n " ,
64
70
65
71
"Color.groovy" ,
66
- "@groovy.transform.ToString(includeNames=true)\n " +
67
- "class Color {\n " +
68
- " Integer r, g, b\n " +
69
- "}\n " ,
72
+ COLOR_CLASS ,
70
73
};
71
74
//@formatter:on
72
75
@@ -78,7 +81,146 @@ public void testNamedVariant3() {
78
81
//@formatter:off
79
82
String [] sources = {
80
83
"Script.groovy" ,
81
- "print(new Color(g:12, b:42, r:12))" ,
84
+ "import groovy.transform.*\n " +
85
+ "\n " +
86
+ "@NamedVariant\n " +
87
+ "String m(@NamedDelegate Color color, @NamedParam(value='a', type=Number) alpha) {\n " +
88
+ " return [color, alpha].join(' ')\n " +
89
+ "}\n " +
90
+ "print m(r:1, g:2, b:3, a: 0.0)\n " ,
91
+
92
+ "Color.groovy" ,
93
+ COLOR_CLASS ,
94
+ };
95
+ //@formatter:on
96
+
97
+ runConformTest (sources , "Color(r:1, g:2, b:3) 0.0" );
98
+ }
99
+
100
+ @ Test
101
+ public void testNamedVariant4 () {
102
+ //@formatter:off
103
+ String [] sources = {
104
+ "Script.groovy" ,
105
+ "import groovy.transform.*\n " +
106
+ "\n " +
107
+ "@NamedVariant\n " +
108
+ "String m(@NamedDelegate Color color, Number alpha/*=4.5*/) {\n " + // TODO: GROOVY-10498
109
+ " return [color, alpha].join(' ')\n " +
110
+ "}\n " +
111
+ "print m(r:1, g:2, b:3, 0.0)\n " ,
112
+
113
+ "Color.groovy" ,
114
+ COLOR_CLASS ,
115
+ };
116
+ //@formatter:on
117
+
118
+ runConformTest (sources , "Color(r:1, g:2, b:3) 0.0" );
119
+ }
120
+
121
+ @ Test // GROOVY-9158, GROOVY-10176, GROOVY-10484
122
+ public void testNamedVariant5 () {
123
+ //@formatter:off
124
+ String [] sources = {
125
+ "Script.groovy" ,
126
+ "import groovy.transform.*\n " +
127
+ "\n " +
128
+ "@NamedVariant(autoDelegate=false)\n " +
129
+ "String m(Color color, int alpha = 0) {\n " + // color:required, alpha:optional
130
+ " return [color, alpha].join(' ')\n " +
131
+ "}\n " +
132
+ "\n " +
133
+ "@groovy.transform.TypeChecked void test() {\n " +
134
+ " print m(color: new Color(r:1,g:2,b:3))\n " +
135
+ "}\n " +
136
+ "test()\n " ,
137
+
138
+ "Color.groovy" ,
139
+ COLOR_CLASS ,
140
+ };
141
+ //@formatter:on
142
+
143
+ runConformTest (sources , "Color(r:1, g:2, b:3) 0" );
144
+ }
145
+
146
+ @ Test // GROOVY-10261
147
+ public void testNamedVariant6 () {
148
+ //@formatter:off
149
+ String [] sources = {
150
+ "Script.groovy" ,
151
+ "import groovy.transform.*\n " +
152
+ "\n " +
153
+ "@NamedVariant\n " +
154
+ "Color color(int r=10, int g=20, int b=30) {\n " +
155
+ " new Color(r:r, g:g, b:b)\n " +
156
+ "}\n " +
157
+ "print color()\n " +
158
+ "print color(r:128,b:128)\n " ,
159
+
160
+ "Color.groovy" ,
161
+ COLOR_CLASS ,
162
+ };
163
+ //@formatter:on
164
+
165
+ runConformTest (sources , "Color(r:10, g:20, b:30)Color(r:128, g:20, b:128)" );
166
+ }
167
+
168
+ @ Test
169
+ public void testNamedVariant7 () {
170
+ assumeTrue (isAtLeastGroovy (30 ));
171
+
172
+ //@formatter:off
173
+ String [] sources = {
174
+ "Script.groovy" ,
175
+ "import groovy.transform.*\n " +
176
+ "\n " +
177
+ "@NamedVariant(coerce=true)\n " +
178
+ "Color color(int r=10, int g=20, int b=30) {\n " +
179
+ " new Color(r:r, g:g, b:b)\n " +
180
+ "}\n " +
181
+ "print color()\n " +
182
+ "print color(r:128,b:'128')\n " ,
183
+
184
+ "Color.groovy" ,
185
+ COLOR_CLASS ,
186
+ };
187
+ //@formatter:on
188
+
189
+ runConformTest (sources , "Color(r:10, g:20, b:30)Color(r:128, g:20, b:128)" );
190
+ }
191
+
192
+ @ Test
193
+ public void testNamedVariant8 () {
194
+ //@formatter:off
195
+ String [] sources = {
196
+ "Script.groovy" ,
197
+ "import groovy.transform.*\n " +
198
+ "\n " +
199
+ "@NamedVariant\n " +
200
+ "void test(@NamedDelegate Color c, @NamedParam int b) {\n " +
201
+ "}\n " +
202
+ "test([:])\n " ,
203
+
204
+ "Color.groovy" ,
205
+ COLOR_CLASS ,
206
+ };
207
+ //@formatter:on
208
+
209
+ runNegativeTest (sources ,
210
+ "----------\n " +
211
+ "1. ERROR in Script.groovy (at line 3)\n " +
212
+ "\t @NamedVariant\n " +
213
+ "\t ^\n " +
214
+ "Groovy:Error during @NamedVariant processing. Duplicate property 'b' found.\n " +
215
+ "----------\n " );
216
+ }
217
+
218
+ @ Test
219
+ public void testNamedVariant9 () {
220
+ //@formatter:off
221
+ String [] sources = {
222
+ "Script.groovy" ,
223
+ "print(new Color(g:12, b:42, r:12))\n " ,
82
224
83
225
"Color.groovy" ,
84
226
"import groovy.transform.*\n " +
@@ -102,4 +244,40 @@ public void testNamedVariant3() {
102
244
103
245
runConformTest (sources , "Color(r:12, g:12, b:42)" );
104
246
}
247
+
248
+ @ Test // GROOVY-10497
249
+ public void testNamedVariant10 () {
250
+ //@formatter:off
251
+ String [] sources = {
252
+ "Script.groovy" ,
253
+ "import groovy.transform.*\n " +
254
+ "\n " +
255
+ "@NamedVariant\n " +
256
+ "void test(int i, int j = 42) {\n " +
257
+ " print \" $i $j\" \n " +
258
+ "}\n " +
259
+ "test(i:0,j:null)\n " ,
260
+ };
261
+ //@formatter:on
262
+
263
+ runConformTest (sources , "0 0" );
264
+ }
265
+
266
+ @ Test // GROOVY-10497
267
+ public void testNamedVariant11 () {
268
+ //@formatter:off
269
+ String [] sources = {
270
+ "Script.groovy" ,
271
+ "import groovy.transform.*\n " +
272
+ "\n " +
273
+ "@NamedVariant\n " +
274
+ "void test(int i, Integer j = 42) {\n " +
275
+ " print \" $i $j\" \n " +
276
+ "}\n " +
277
+ "test(i:0,j:null)\n " ,
278
+ };
279
+ //@formatter:on
280
+
281
+ runConformTest (sources , "0 null" );
282
+ }
105
283
}
0 commit comments