This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY-8258 in repository https://gitbox.apache.org/repos/asf/groovy.git
commit fd4169e340c4b94ae1eae140d95cf640d4cf058d Author: Daniel Sun <[email protected]> AuthorDate: Tue Oct 6 05:05:13 2020 +0800 GROOVY-8258: add more test cases --- .../org/apache/groovy/linq/dsl/GinqBuilder.groovy | 3 +- .../groovy/org/apache/groovy/linq/GinqTest.groovy | 157 ++++++++++++++++++++- 2 files changed, 158 insertions(+), 2 deletions(-) diff --git a/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/dsl/GinqBuilder.groovy b/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/dsl/GinqBuilder.groovy index fdb2e96..b9dd39a 100644 --- a/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/dsl/GinqBuilder.groovy +++ b/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/dsl/GinqBuilder.groovy @@ -34,6 +34,7 @@ import org.codehaus.groovy.ast.expr.Expression import org.codehaus.groovy.ast.expr.ExpressionTransformer import org.codehaus.groovy.ast.expr.ListExpression import org.codehaus.groovy.ast.expr.MethodCallExpression +import org.codehaus.groovy.ast.expr.TupleExpression import org.codehaus.groovy.ast.expr.VariableExpression import static org.codehaus.groovy.ast.tools.GeneralUtils.callX @@ -166,7 +167,7 @@ class GinqBuilder implements GinqVisitor<Object> { projectionExpr = correctVariablesOfProjectExpression(selectExpression, projectionExpr) } - List<Expression> expressionList = ((ArgumentListExpression) projectionExpr).getExpressions() + List<Expression> expressionList = ((TupleExpression) projectionExpr).getExpressions() Expression lambdaCode if (expressionList.size() > 1) { lambdaCode = new ListExpression(expressionList) diff --git a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy index 96d04cb..c97f09d 100644 --- a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy +++ b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/GinqTest.groovy @@ -50,6 +50,69 @@ class GinqTest { } @Test + void "testGinq - from select - 3"() { + assertScript ''' + class Person { + String name + int age + + Person(String name, int age) { + this.name = name + this.age = age + } + } + + def persons = [new Person('Daniel', 35), new Person('Linda', 21), new Person('Peter', 30)] + assert [35, 21, 30] == GINQ { + from p in persons + select p.age + }.toList() + ''' + } + + @Test + void "testGinq - from select - 4"() { + assertScript ''' + class Person { + String name + int age + + Person(String name, int age) { + this.name = name + this.age = age + } + } + + def persons = [new Person('Daniel', 35), new Person('Linda', 21), new Person('Peter', 30)] + assert [['Daniel', 35], ['Linda', 21], ['Peter', 30]] == GINQ { + from p in persons + select p.name, p.age + }.toList() + ''' + } + + @Test + void "testGinq - from select - 5"() { + assertScript ''' + class Person { + String name + int age + + Person(String name, int age) { + this.name = name + this.age = age + } + } + + def persons = [new Person('Daniel', 35), new Person('Linda', 21), new Person('Peter', 30)] + assert [[name:'Daniel', age:35], [name:'Linda', age:21], [name:'Peter', age:30]] == GINQ { + from p in persons + select (name: p.name, age: p.age) + }.toList() + ''' + } + + @Test void "testGinq - from where select"() { assertScript ''' def numbers = [0, 1, 2, 3, 4, 5] @@ -62,7 +125,7 @@ class GinqTest { } @Test - void "testGinq - from innerJoin select"() { + void "testGinq - from innerJoin select - 1"() { assertScript ''' def nums1 = [1, 2, 3] def nums2 = [1, 2, 3] @@ -74,4 +137,96 @@ class GinqTest { }.toList() ''' } + + @Test + void "testGinq - from innerJoin select - 2"() { + assertScript ''' + def nums1 = [1, 2, 3] + def nums2 = [1, 2, 3] + assert [[2, 1], [3, 2], [4, 3]] == GINQ { + from n1 in nums1 + innerJoin n2 in nums2 + on n1 == n2 + select n1 + 1, n2 + }.toList() + ''' + } + + @Test + void "testGinq - from innerJoin select - 3"() { + assertScript ''' + def nums1 = [1, 2, 3] + def nums2 = [1, 2, 3] + assert [[1, 2], [2, 3], [3, 4]] == GINQ { + from n1 in nums1 + innerJoin n2 in nums2 + on n1 == n2 + select n1, n2 + 1 + }.toList() + ''' + } + + @Test + void "testGinq - from innerJoin select - 4"() { + assertScript ''' + def nums1 = [1, 2, 3] + def nums2 = [1, 2, 3] + assert [[1, 2], [2, 3]] == GINQ { + from n1 in nums1 + innerJoin n2 in nums2 + on n1 + 1 == n2 + select n1, n2 + }.toList() + ''' + } + + @Test + void "testGinq - from innerJoin select - 5"() { + assertScript ''' + class Person { + String name + int age + + Person(String name, int age) { + this.name = name + this.age = age + } + } + + def persons1 = [new Person('Daniel', 35), new Person('Linda', 21), new Person('Peter', 30)] + def persons2 = [new Person('Jack', 35), new Person('Rose', 21), new Person('Smith', 30)] + assert [['Daniel', 'Jack'], ['Linda', 'Rose'], ['Peter', 'Smith']] == GINQ { + from p1 in persons1 + innerJoin p2 in persons2 + on p1.age == p2.age + select p1.name, p2.name + }.toList() + ''' + } + + @Test + void "testGinq - from innerJoin select - 6"() { + assertScript ''' + class Person { + String name + int age + + Person(String name, int age) { + this.name = name + this.age = age + } + } + + def same(str) { str } + + def persons1 = [new Person('Daniel', 35), new Person('Linda', 21), new Person('Peter', 30)] + def persons2 = [new Person('Jack', 35), new Person('Rose', 21), new Person('Smith', 30)] + assert [['DANIEL', 'JACK'], ['LINDA', 'ROSE'], ['PETER', 'SMITH']] == GINQ { + from p1 in persons1 + innerJoin p2 in persons2 + on p1.age == p2.age + select same(p1.name.toUpperCase()), same(p2.name.toUpperCase()) + }.toList() + ''' + } }
