You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/10/04 20:27:35 UTC

[groovy] branch GROOVY-8258 updated: GROOVY-8258: Add 2 test cases

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


The following commit(s) were added to refs/heads/GROOVY-8258 by this push:
     new 606c5d9  GROOVY-8258: Add 2 test cases
606c5d9 is described below

commit 606c5d9b8b5dfe7ec324e10fe1742fa0c4bb2a09
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Oct 5 04:25:12 2020 +0800

    GROOVY-8258: Add 2 test cases
---
 .../apache/groovy/linq/LinqGroovyMethods.groovy    | 12 ++++++++++--
 .../groovy/org/apache/groovy/linq/LinqTest.groovy  | 22 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/LinqGroovyMethods.groovy b/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/LinqGroovyMethods.groovy
index ff368fb..71c3fe5 100644
--- a/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/LinqGroovyMethods.groovy
+++ b/subprojects/groovy-linq/src/main/groovy/org/apache/groovy/linq/LinqGroovyMethods.groovy
@@ -83,13 +83,21 @@ class LinqGroovyMethods {
         Map.Entry<VariableExpression, Expression> fromEntry = linqContext.fromMap.entrySet().toList().get(0)
         VariableExpression aliasVariable = fromEntry.key
 
+        Expression selectMethodReceiver = null
+
         MethodCallExpression from = macro {
             org.apache.groovy.linq.provider.QueryableCollection
                     .from($v { fromEntry.value })
         }
 
-        MethodCallExpression where = callXWithClosure(from, "where", aliasVariable.name, linqContext.whereList[0])
-        MethodCallExpression select = callXWithClosure(where, "select", aliasVariable.name, linqContext.selectList[0])
+        selectMethodReceiver = from
+
+        if (linqContext.whereList.size() > 0) {
+            MethodCallExpression where = callXWithClosure(from, "where", aliasVariable.name, linqContext.whereList[0])
+            selectMethodReceiver = where
+        }
+
+        MethodCallExpression select = callXWithClosure(selectMethodReceiver, "select", aliasVariable.name, linqContext.selectList[0])
 
         return select
     }
diff --git a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/LinqTest.groovy b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/LinqTest.groovy
index a19d39e..b2b0887 100644
--- a/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/LinqTest.groovy
+++ b/subprojects/groovy-linq/src/test/groovy/org/apache/groovy/linq/LinqTest.groovy
@@ -28,6 +28,28 @@ import static groovy.test.GroovyAssert.assertScript
 @CompileStatic
 class LinqTest {
     @Test
+    void "testLinqMacroMethod - from select - 1"() {
+        assertScript '''
+            def numbers = [0, 1, 2]
+            assert [0, 1, 2] == LINQ {
+                from n of numbers
+                select n
+            }.toList()
+        '''
+    }
+
+    @Test
+    void "testLinqMacroMethod - from select - 2"() {
+        assertScript '''
+            def numbers = [0, 1, 2]
+            assert [0, 2, 4] == LINQ {
+                from n of numbers
+                select n * 2
+            }.toList()
+        '''
+    }
+
+    @Test
     void "testLinqMacroMethod - from where select"() {
         assertScript '''
             def numbers = [0, 1, 2, 3, 4, 5]