You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/08/10 17:40:38 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-7506, GROOVY-8983: add test cases

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new ddf515b3ff GROOVY-7506, GROOVY-8983: add test cases
ddf515b3ff is described below

commit ddf515b3ff59fc8cf0d3a991cc53170de86d89c6
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Wed Aug 10 11:42:57 2022 -0500

    GROOVY-7506, GROOVY-8983: add test cases
---
 .../stc/ArraysAndCollectionsSTCTest.groovy         | 95 ++++++++++++----------
 .../groovy/transform/stc/MethodCallsSTCTest.groovy | 20 +++++
 2 files changed, 74 insertions(+), 41 deletions(-)

diff --git a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
index 885e58cbb4..8cd4a3bbb2 100644
--- a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
@@ -18,6 +18,8 @@
  */
 package groovy.transform.stc
 
+import groovy.test.NotYetImplemented
+
 /**
  * Unit tests for static type checking : arrays and collections.
  */
@@ -34,7 +36,7 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
     void testArrayElementTypeInference() {
         shouldFailWithMessages '''
             String[] strings = ['a','b','c']
-            int str = strings[0]
+            int i = strings[0]
         ''',
         'Cannot assign value of type java.lang.String to variable of type int'
     }
@@ -85,67 +87,67 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
 
     void testAssignValueInArrayWithCorrectType() {
         assertScript '''
-            int[] arr2 = [1, 2, 3]
-            arr2[1] = 4
+            int[] array = [1, 2, 3]
+            array[1] = 4
         '''
     }
 
     void testAssignValueInArrayWithWrongType() {
         shouldFailWithMessages '''
-            int[] arr2 = [1, 2, 3]
-            arr2[1] = "One"
+            int[] array = [1, 2, 3]
+            array[1] = "One"
         ''',
         'Cannot assign value of type java.lang.String to variable of type int'
     }
 
     void testBidimensionalArray() {
         assertScript '''
-            int[][] arr2 = new int[1][]
-            arr2[0] = [1,2]
+            int[][] array = new int[1][]
+            array[0] = [1,2]
         '''
     }
 
     void testBidimensionalArrayWithInitializer() {
         shouldFailWithMessages '''
-            int[][] arr2 = new Object[1][]
+            int[][] array = new Object[1][]
         ''',
         'Cannot assign value of type java.lang.Object[][] to variable of type int[][]'
     }
 
     void testBidimensionalArrayWithWrongSubArrayType() {
         shouldFailWithMessages '''
-            int[][] arr2 = new int[1][]
-            arr2[0] = ['1']
+            int[][] array = new int[1][]
+            array[0] = ['1']
         ''',
         'Cannot assign value of type java.lang.String into array of type int[]'
     }
 
     void testForLoopWithArrayAndUntypedVariable() {
         assertScript '''
-            String[] arr = ['1','2','3']
-            for (i in arr) { }
+            String[] array = ['1','2','3']
+            for (i in array) { }
         '''
     }
 
     void testForLoopWithArrayAndWrongVariableType() {
         shouldFailWithMessages '''
-            String[] arr = ['1','2','3']
-            for (int i in arr) { }
+            String[] array = ['1','2','3']
+            for (int i in array) { }
         ''',
         'Cannot loop with element of type int with collection of type java.lang.String[]'
     }
 
     void testJava5StyleForLoopWithArray() {
         assertScript '''
-            String[] arr = ['1','2','3']
-            for (String i : arr) { }
+            String[] array = ['1','2','3']
+            for (String i : array) { }
         '''
     }
 
     void testJava5StyleForLoopWithArrayAndIncompatibleType() {
         shouldFailWithMessages '''
-            String[] arr = ['1','2','3']
-            for (int i : arr) { }
+            String[] array = ['1','2','3']
+            for (int i : array) { }
         ''',
         'Cannot loop with element of type int with collection of type java.lang.String[]'
     }
@@ -178,7 +180,7 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
         assertScript '''
             List<Integer> a = [1, 3, 5]
 
-            @ASTTest(phase = INSTRUCTION_SELECTION, value = {
+            @ASTTest(phase=INSTRUCTION_SELECTION, value={
                 def type = node.rightExpression.getNodeMetaData(INFERRED_TYPE)
                 assert type == make(List)
                 assert type.genericsTypes.length == 1
@@ -199,7 +201,7 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
 
             def sc = new SpecialCollection()
 
-            @ASTTest(phase = INSTRUCTION_SELECTION, value = {
+            @ASTTest(phase=INSTRUCTION_SELECTION, value={
                 def type = node.rightExpression.getNodeMetaData(INFERRED_TYPE)
                 assert type == make(List)
                 assert type.genericsTypes.length == 1
@@ -260,26 +262,6 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    void testAmbiguousCallWithVargs() {
-        assertScript '''
-            int sum(int x) { 1 }
-            int sum(int... args) {
-                0
-            }
-            assert sum(1) == 1
-        '''
-    }
-
-    void testAmbiguousCallWithVargs2() {
-        assertScript '''
-            int sum(int x) { 1 }
-            int sum(int y, int... args) {
-                0
-            }
-            assert sum(1) == 1
-        '''
-    }
-
     void testCollectMethodCallOnList() {
         assertScript '''
             [1,2,3].collect { it.toString() }
@@ -462,6 +444,37 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    @NotYetImplemented
+    void testShouldAllowArrayAssignment3() {
+        String pogo = '''
+            class C {
+                public String[] strings
+                void setP(String[] strings) {
+                    this.strings = strings
+                }
+            }
+        '''
+        // GROOVY-8983
+        assertScript pogo + '''
+            def list = ['foo','bar']
+
+            def c = new C()
+            c.p = list // implicit conversion
+            assert c.strings == ['foo','bar']
+        '''
+        // GROOVY-7506
+        assertScript pogo + '''
+            def c = new C()
+            c.p = ['foo','bar']
+            assert c.strings == ['foo','bar']
+        '''
+        assertScript pogo + '''
+            def c = new C()
+            c.p = ['foo', 123 ]
+            assert c.strings == ['foo','123']
+        '''
+    }
+
     void testListPlusEquals() {
         assertScript '''
             List<String> list = ['a','b']
@@ -575,7 +588,7 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
             })
             Integer j = org.codehaus.groovy.runtime.DefaultGroovyMethods.find(list) { int it -> it%2 == 0 }
 
-            @ASTTest(phase=INSTRUCTION_SELECTION, value= {
+            @ASTTest(phase=INSTRUCTION_SELECTION, value={
                 assert node.getNodeMetaData(INFERRED_TYPE) == Integer_TYPE
             })
             Integer i = list.find { int it -> it % 2 == 0 }
diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
index 0425fc5da5..37aedf213a 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -968,6 +968,26 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
+    void testVargsSelection2() {
+        assertScript '''
+            int sum(int x) { 1 }
+            int sum(int... args) {
+                0
+            }
+            assert sum(1) == 1
+        '''
+    }
+
+    void testVargsSelection3() {
+        assertScript '''
+            int sum(int x) { 1 }
+            int sum(int y, int... args) {
+                0
+            }
+            assert sum(1) == 1
+        '''
+    }
+
     // GROOVY-5702
     void testShouldFindInterfaceMethod() {
         assertScript '''