You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/04/08 12:59:09 UTC

[groovy] 17/20: Add more tests for method reference and constructor reference

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

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

commit eb35c34fc4bad57069eb0a6f147347b811d03e7c
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Mar 27 00:21:23 2019 +0800

    Add more tests for method reference and constructor reference
---
 .../transform/stc/MethodReferenceTest.groovy       | 89 ++++++++++++++++++++++
 1 file changed, 89 insertions(+)

diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index 70b1f7f..4adc4b4 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -292,6 +292,78 @@ class MethodReferenceTest extends GroovyTestCase {
         '''
     }
 
+    // class::staticMethod
+    void testFunctionCS_RHS_NOTYPE() {
+        assertScript '''
+            import java.util.function.Function
+            import java.util.stream.Stream
+            import java.util.stream.Collectors
+
+            @groovy.transform.CompileStatic
+            void p() {
+                def f = Math::abs // No explicit type defined, so it is actually a method closure. We can make it smarter in a later version.
+                def result = [1, -2, 3].stream().map(f).collect(Collectors.toList())
+
+                assert [1, 2, 3] == result
+            }
+            
+            p()
+        '''
+    }
+
+    // instance::instanceMethod
+    void testBinaryOperatorII_RHS() {
+        assertScript '''
+            import java.util.function.BinaryOperator
+            import java.util.stream.Stream
+            import java.util.stream.Collectors
+
+            @groovy.transform.CompileStatic
+            void p() {
+                Adder adder = new Adder()
+                BinaryOperator<BigDecimal> b = adder::add
+                def result = [new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)].stream().reduce(new BigDecimal(0), b)
+
+                assert new BigDecimal(6) == result
+            }
+            
+            p()
+            
+            @groovy.transform.CompileStatic
+            class Adder {
+                public BigDecimal add(BigDecimal a, BigDecimal b) {
+                    return a.add(b)
+                }
+            }
+        '''
+    }
+
+    // expression::instanceMethod
+    void testBinaryOperatorII_RHS2() {
+        assertScript '''
+            import java.util.function.BinaryOperator
+            import java.util.stream.Stream
+            import java.util.stream.Collectors
+
+            @groovy.transform.CompileStatic
+            void p() {
+                BinaryOperator<BigDecimal> b = new Adder()::add
+                def result = [new BigDecimal(1), new BigDecimal(2), new BigDecimal(3)].stream().reduce(new BigDecimal(0), b)
+
+                assert new BigDecimal(6) == result
+            }
+            
+            p()
+            
+            @groovy.transform.CompileStatic
+            class Adder {
+                public BigDecimal add(BigDecimal a, BigDecimal b) {
+                    return a.add(b)
+                }
+            }
+        '''
+    }
+
     // class::new
     void testFunctionCN_RHS() {
         assertScript '''
@@ -309,4 +381,21 @@ class MethodReferenceTest extends GroovyTestCase {
 
         '''
     }
+
+    // arrayClass::new
+    void testIntFunctionCN_RHS() {
+        assertScript '''
+            import java.util.function.IntFunction
+            import java.util.stream.Stream
+
+            @groovy.transform.CompileStatic
+            void p() {
+                IntFunction<Integer[]> f = Integer[]::new
+                assert new Integer[] { 1, 2, 3 } == [1, 2, 3].stream().toArray(f)
+            }
+            
+            p()
+
+        '''
+    }
 }