-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
LoopsSTCTest.groovy
389 lines (361 loc) · 12.1 KB
/
LoopsSTCTest.groovy
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package groovy.transform.stc
/**
* Unit tests for static type checking : loops.
*/
class LoopsSTCTest extends StaticTypeCheckingTestCase {
void testMethodCallInLoop() {
for (type in ['int', 'def', 'var']) {
assertScript """
int foo(int x) { x+1 }
$type x = 0
for (int i=0;i<10;i++) {
x = foo(x)
}
"""
}
}
// GROOVY-5258
void testMethodCallInLoopAndDefAndTwoFooMethods() {
shouldFailWithMessages '''
Date foo(Integer x) { new Date() }
Integer foo(Date x) { 1 }
def x = 0
2.times {
// there are two possible target methods; this is not a problem for STC, but it is for static compilation
x = foo(x)
}
''',
'Cannot find matching method'
}
// GROOVY-5258
void testMethodCallInLoopAndDefAndTwoFooMethods2() {
shouldFailWithMessages '''
Date foo(Integer x) { new Date() }
Integer foo(Date x) { 1 }
def x = 0
for (int i=0;i<10;i++) {
// there are two possible target methods; this is not a problem for STC, but it is for static compilation
x = foo(x)
}
''',
'Cannot find matching method'
}
// GROOVY-5258
void testMethodCallInLoopAndDefAndTwoFooMethodsAndOneWithBadType() {
shouldFailWithMessages '''
Double foo(Integer x) { x+1 }
Date foo(Double x) { new Date(x.longValue()) }
def x = 0
for (int i=0;i<10;i++) {
// there are two possible target methods and one returns a type which is assigned to 'x'
// then called in turn as a parameter of foo() -- there's no foo(Date)
x = foo(x)
}
''',
'Cannot find matching method'
}
// GROOVY-5258
void testMethodCallInLoopAndDefAndTwoFooMethodsAndOneWithBadTypeAndIndirection() {
shouldFailWithMessages '''
Double foo(Integer x) { x+1 }
Date foo(Double x) { new Date(x.longValue()) }
def x = 0
for (int i=0;i<10;i++) {
def y = foo(x)
// there are two possible target methods and one returns a type which is assigned to 'x'
// then called in turn as a parameter of foo() -- there's no foo(Date)
x = y
}
''',
'Cannot find matching method'
}
// GROOVY-5258
void testMethodCallInLoopAndDefAndTwoFooMethodsAndOneWithBadTypeAndIndirection2() {
shouldFailWithMessages '''
Double foo(Integer x) { x+1 }
Date foo(Double x) { new Date(x.longValue()) }
def x = 0
2.times {
def y = foo(x)
// there are two possible target methods and one returns a type which is assigned to 'x'
// then called in turn as a parameter of foo() -- there's no foo(Date)
x = y
}
''',
'Cannot find matching method'
}
// GROOVY-5587
void testForInLoopOnMap1() {
assertScript '''
@ASTTest(phase=INSTRUCTION_SELECTION, value={
lookup('forLoop').each {
assert it instanceof org.codehaus.groovy.ast.stmt.ForStatement
def collection = it.collectionExpression // MethodCallExpression
def inft = collection.getNodeMetaData(INFERRED_TYPE)
assert inft == make(Set)
def entryInft = inft.genericsTypes[0].type
assert entryInft == make(Map.Entry)
assert entryInft.genericsTypes[0].type == STRING_TYPE
assert entryInft.genericsTypes[1].type == Integer_TYPE
}
})
void test() {
def result = ""
def sum = 0
forLoop:
for ( Map.Entry<String, Integer> it in [a:1, b:3].entrySet() ) {
result += it.getKey()
sum += it.getValue()
}
assert result == "ab"
assert sum == 4
}
test()
'''
}
// GROOVY-6240
void testForInLoopOnMap2() {
assertScript '''
Map<String, Integer> map = [foo: 123, bar: 456]
for (entry in map) {
assert entry.key.reverse() in ['oof','rab']
assert entry.value * 2 in [246, 912]
}
'''
}
void testForInLoopOnMap3() {
assertScript '''
class MyMap extends LinkedHashMap<String,Integer> {
}
def map = new MyMap([foo: 123, bar: 456])
for (entry in map) {
assert entry.key.reverse() in ['oof','rab']
assert entry.value * 2 in [246, 912]
}
'''
}
// GROOVY-10179
void testForInLoopOnMap4() {
assertScript '''
void test(args) {
if (args instanceof Map) {
for (e in args) {
print "$e.key $e.value"
}
}
}
test(a:1,b:2,c:3.14)
'''
}
// GROOVY-8643
void testForInLoopOnList() {
assertScript '''
List<String> list = null
for (item in list) {
item.toUpperCase()
}
'''
}
// GROOVY-8643
void testForInLoopOnArray() {
assertScript '''
String[] strings = null
for (string in strings) {
string.toUpperCase()
}
'''
}
void testForInLoopOnArray2() {
assertScript '''
String[] strings = ['a','b','c']
for (string in strings) {
string.toUpperCase()
}
'''
}
// GROOVY-10579
void testForInLoopOnArray3() {
assertScript '''
int[] numbers = [1,2,3,4,5]
int sum = 0
for (i in numbers) {
sum += i
}
assert sum == 15
'''
}
// GROOVY-10476
void testForInLoopOnStream() {
assertScript '''
def list = []
for (item in ['a','b','c'].stream()) {
list.add(item.toUpperCase())
}
assert list == ['A','B','C']
'''
}
// GROOVY-8882
void testForInLoopOnString() {
assertScript '''
for (s in 'abc') assert s instanceof String
for (String s in 'abc') assert s instanceof String
'''
assertScript '''
for (char c in 'abc') assert c instanceof Character
for (Character c in 'abc') assert c instanceof Character
'''
}
// GROOVY-8487, GROOVY-10712
void testForInLoopOnIterator() {
assertScript '''
def list = []
for (item in ['a','b','c'].iterator()) {
list.add(item.toUpperCase())
}
assert list == ['A','B','C']
'''
}
// GROOVY-11335
void testForInLoopOnCollection() {
assertScript '''
def whatever(Collection<String> coll) {
if (coll instanceof Serializable) {
for (item in coll) {
return item.toLowerCase()
}
}
}
assert whatever(['Works']) == 'works'
'''
}
// GROOVY-6123
void testForInLoopOnEnumeration() {
assertScript '''
Vector<String> v = new Vector<>()
v.add('ooo')
Enumeration<String> en = v.elements()
for (e in en) {
assert e.toUpperCase() == 'OOO'
}
v.add('groovy')
en = v.elements()
for (e in en) {
assert e.toUpperCase() == 'OOO'
break
}
en = v.elements()
for (e in en) {
assert e.toUpperCase() in ['OOO','GROOVY']
if (e=='ooo') continue
}
en = null
for (e in en) {
e.toUpperCase()
}
'''
}
// GROOVY-10651
void testForInLoopOnRawTypeIterable() {
assertScript '''
void test(groovy.transform.stc.TreeNode node) {
for (child in node) {
test(child) // Cannot find matching method #test(java.lang.Object)
}
}
'''
}
// GROOVY-10651
void testForInLoopOnUnboundedIterable() {
assertScript '''
void test(groovy.transform.stc.TreeNode<?> node) {
for (child in node) {
test(child) // Cannot find matching method #test(java.lang.Object)
}
}
'''
}
void testShouldNotInferSoftReferenceAsElementType() {
assertScript '''
@ASTTest(phase=INSTRUCTION_SELECTION, value={
def loop = lookup('loop')[0]
assert loop instanceof org.codehaus.groovy.ast.stmt.ForStatement
def collectionType = loop.collectionExpression.getNodeMetaData(INFERRED_TYPE)
assert collectionType == make(java.lang.reflect.Field).makeArray()
})
void test() {
int i = 0
loop:
for (def field : String.class.declaredFields) {
i++
}
assert i > 0
}
'''
}
// GROOVY-5640
void testShouldInferNodeElementTypeForIterableOfNodes() {
assertScript '''
class Node {
}
interface Traverser {
Iterable<Node> nodes()
}
class MyTraverser implements Traverser {
Iterable<Node> nodes() {
[]
}
}
@ASTTest(phase=INSTRUCTION_SELECTION, value={
def loop = lookup('loop')[0]
assert loop instanceof org.codehaus.groovy.ast.stmt.ForStatement
def collectionType = loop.collectionExpression.getNodeMetaData(INFERRED_TYPE)
assert collectionType == make(Iterable)
assert collectionType.isUsingGenerics()
assert collectionType.genericsTypes.length == 1
assert collectionType.genericsTypes[0].type.name == 'Node'
})
void test() {
loop:
for (def node : new MyTraverser().nodes()) {
println node.class.name
}
}
'''
}
// GROOVY-5641
void testShouldInferLoopElementTypeWithUndeclaredType() {
assertScript '''
@ASTTest(phase=INSTRUCTION_SELECTION, value={
def loop = lookup('loop')[0]
assert loop instanceof org.codehaus.groovy.ast.stmt.ForStatement
def collectionType = loop.collectionExpression.getNodeMetaData(INFERRED_TYPE)
assert collectionType == make(IntRange)
})
void test() {
int[] ints = new int[10]
loop:
for (i in 0..<10) {
assert ints[i-0] == 0
}
}
'''
}
}