1
1
/*
2
- * Copyright 2009-2022 the original author or authors.
2
+ * Copyright 2009-2024 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.
@@ -61,8 +61,7 @@ final class BuiltInDSLInferencingTests extends DSLInferencingTestSuite {
61
61
}
62
62
63
63
assert pluginEntry != null : " Did not find the Plugin DSLD classpath entry. Exsting resolved roots: [\n ${ -> elements.join(', ')} \n ]\n Other DSLD fragments: [\n ${ -> possibleFrags.join('\n')} \n ]"
64
- assert root != null : " Plugin DSLD classpath entry should exist. Exsting resolved roots: [\n ${ -> elements.join(', ')} \n ]\n Other DSLD fragments: [\n ${ -> possibleFrags.join('\n')} \n ]"
65
- assert root. exists() : ' Plugin DSLD classpath entry should exist'
64
+ assert root != null && root. exists() : " Plugin DSLD classpath entry should exist. Exsting resolved roots: [\n ${ -> elements.join(', ')} \n ]\n Other DSLD fragments: [\n ${ -> possibleFrags.join('\n')} \n ]"
66
65
67
66
root. resource(). refreshLocal(IResource . DEPTH_INFINITE , null )
68
67
root. close(); root. open(null )
@@ -90,10 +89,57 @@ final class BuiltInDSLInferencingTests extends DSLInferencingTestSuite {
90
89
@Test
91
90
void testDelegate2 () {
92
91
String contents = ''' \
93
- |class Bar {
92
+ |class Foo {
93
+ | @Delegate List<Integer> list
94
+ |}
95
+ |new Foo().spliterator() // default method of List
96
+ |''' . stripMargin()
97
+
98
+ inferType(contents, ' spliterator' ). with {
99
+ assert result. extraDoc?. replace(' }' , ' ' ) =~ ' Delegate AST transform'
100
+ assert declaringTypeName == ' java.util.List<java.lang.Integer>'
101
+ assert typeName == ' java.util.Spliterator<java.lang.Integer>'
102
+ }
103
+ }
104
+
105
+ @Test
106
+ void testDelegate3 () {
107
+ String contents = ''' \
108
+ |class Foo {
109
+ | @Delegate List<Integer> list
110
+ |}
111
+ |new Foo().stream() // default method of Collection
112
+ |''' . stripMargin()
113
+
114
+ inferType(contents, ' stream' ). with {
115
+ assert result. extraDoc?. replace(' }' , ' ' ) =~ ' Delegate AST transform'
116
+ assert declaringTypeName == ' java.util.List<java.lang.Integer>'
117
+ assert typeName == ' java.util.stream.Stream<java.lang.Integer>'
118
+ }
119
+ }
120
+
121
+ @Test
122
+ void testDelegate4 () {
123
+ String contents = ''' \
124
+ |class Foo {
125
+ | @Delegate List<Integer> list
126
+ |}
127
+ |new Foo()./**/equals(null) // method of List and Object
128
+ |''' . stripMargin()
129
+
130
+ inferType(contents, ' equals' ). with {
131
+ assert declaringTypeName == ' java.lang.Object'
132
+ assert result. extraDoc == null
133
+ }
134
+ }
135
+
136
+ @Test
137
+ void testDelegate5 () {
138
+ String contents = ''' \
139
+ |class Foo {
94
140
| @Delegate URL url
95
141
|}
96
- |new Bar ().file
142
+ |new Foo ().file // getFile() as property
97
143
|''' . stripMargin()
98
144
99
145
inferType(contents, ' file' ). with {
@@ -104,7 +150,7 @@ final class BuiltInDSLInferencingTests extends DSLInferencingTestSuite {
104
150
}
105
151
106
152
@Test // GROOVY-5204
107
- void testDelegate3 () {
153
+ void testDelegate6 () {
108
154
String contents = ''' \
109
155
|class Bar {
110
156
| def baz() {}
@@ -123,7 +169,7 @@ final class BuiltInDSLInferencingTests extends DSLInferencingTestSuite {
123
169
}
124
170
125
171
@Test // GROOVY-5204
126
- void testDelegate4 () {
172
+ void testDelegate7 () {
127
173
String contents = ''' \
128
174
|class Bar {
129
175
| def baz() {}
@@ -143,14 +189,14 @@ final class BuiltInDSLInferencingTests extends DSLInferencingTestSuite {
143
189
}
144
190
145
191
@Test // GROOVY-3917
146
- void testDelegate5 () {
192
+ void testDelegate8 () {
147
193
String contents = ''' \
148
194
|class Bar {
149
195
|}
150
196
|class Foo {
151
197
| @Delegate Bar bar = new Bar()
152
198
|}
153
- |new Foo().getProperty('baz')
199
+ |new Foo().getProperty('baz') // method of GroovyObject
154
200
|''' . stripMargin()
155
201
156
202
inferType(contents, ' getProperty' ). with {
@@ -159,6 +205,109 @@ final class BuiltInDSLInferencingTests extends DSLInferencingTestSuite {
159
205
}
160
206
}
161
207
208
+ @Test // GROOVY-8164
209
+ void testDelegate9 () {
210
+ String contents = ''' \
211
+ |class Bar {
212
+ | def baz
213
+ |}
214
+ |class Foo {
215
+ | @Delegate Comparator<Bar> cmp
216
+ |}
217
+ |new Foo().comparing(Bar.&getBaz) // static method of Comparator
218
+ |''' . stripMargin()
219
+
220
+ inferType(contents, ' comparing' ). with {
221
+ assert result. confidence. name() == ' UNKNOWN'
222
+ }
223
+ }
224
+
225
+ @Test
226
+ void testDelegate10 () {
227
+ String contents = ''' \
228
+ |class Bar {
229
+ | def baz
230
+ |}
231
+ |class Foo {
232
+ | @Delegate(excludes=['compare','equals']) Comparator<Bar> cmp
233
+ |}
234
+ |new Foo().compare(null, null)
235
+ |''' . stripMargin()
236
+
237
+ inferType(contents, ' compare' ). with {
238
+ assert result. confidence. name() == ' UNKNOWN'
239
+ }
240
+ }
241
+
242
+ @Test
243
+ void testDelegate11 () {
244
+ String contents = ''' \
245
+ |class Bar {
246
+ | def baz
247
+ |}
248
+ |class Foo {
249
+ | @Delegate(includes='compare') Comparator<Bar> cmp
250
+ |}
251
+ |new Foo().compare(null, null)
252
+ |''' . stripMargin()
253
+
254
+ inferType(contents, ' compare' ). with {
255
+ assert result. extraDoc?. replace(' }' , ' ' ) =~ ' Delegate AST transform'
256
+ assert declaringTypeName == ' java.util.Comparator<Bar>'
257
+ assert typeName == ' java.lang.Integer'
258
+ }
259
+ }
260
+
261
+ @Test
262
+ void testDelegate12 () {
263
+ addGroovySource ''' \
264
+ |class Bar {
265
+ | @Deprecated
266
+ | int baz(){}
267
+ |}
268
+ |''' . stripMargin(), ' Bar'
269
+
270
+ String contents = ''' \
271
+ |class Foo {
272
+ | @Delegate Bar bar
273
+ |}
274
+ |new Foo().baz()
275
+ |''' . stripMargin()
276
+
277
+ inferType(contents, ' baz' ). with {
278
+ assert result. confidence. name() == ' UNKNOWN'
279
+ }
280
+
281
+ contents = contents. replace(' @Delegate' , ' @Delegate(deprecated=true)' )
282
+
283
+ inferType(contents, ' baz' ). with {
284
+ assert result. extraDoc?. replace(' }' , ' ' ) =~ ' Delegate AST transform'
285
+ assert declaringTypeName == ' Bar'
286
+ assert typeName == ' java.lang.Integer'
287
+ }
288
+ }
289
+
290
+ @Test
291
+ void testDelegate13 () {
292
+ addGroovySource ''' \
293
+ |interface Bar {
294
+ | @Deprecated
295
+ | int baz()
296
+ |}
297
+ |''' . stripMargin(), ' Bar'
298
+
299
+ String contents = ''' \
300
+ |class Foo {
301
+ | @Delegate(interfaces=false) Bar bar
302
+ |}
303
+ |new Foo().baz()
304
+ |''' . stripMargin()
305
+
306
+ inferType(contents, ' baz' ). with {
307
+ assert result. confidence. name() == ' UNKNOWN'
308
+ }
309
+ }
310
+
162
311
@Test
163
312
void testField1 () {
164
313
String contents = ''' \
@@ -382,7 +531,7 @@ final class BuiltInDSLInferencingTests extends DSLInferencingTestSuite {
382
531
|class E {
383
532
| String value
384
533
|}
385
- |new E().compareTo(null)
534
+ |new E()./**/ compareTo(null)
386
535
|''' . stripMargin()
387
536
388
537
inferType(contents, ' compareTo' ). with {
0 commit comments