alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (ClosureMethodCallTest.groovy)

This example Groovy source code file (ClosureMethodCallTest.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

closuremethodcalltest, groovytestcase, hello, hello, map, map, object, object, string, string

The Groovy ClosureMethodCallTest.groovy source code

package groovy

/** 
 * @author <a href="mailto:james@coredevelopers.net">James Strachan
 * @version $Revision: 16421 $
 */
class ClosureMethodCallTest extends GroovyTestCase {

    void testCallingClosureWithMultipleArguments() {
        def foo
        def closure = { a, b -> foo = "hello ${a} and ${b}".toString() }
        
        closure("james", "bob")

        assert foo == "hello james and bob"

        closure.call("sam", "james")

        assert foo == "hello sam and james"
    }
    
    void testClosureCallMethodWithObjectArray() {
        // GROOVY-2266
        def args = [1] as Object[]
        def closure = {x -> x[0]}
        assert closure.call(args) == 1
    }
    
    void testClosureWithStringArrayCastet() {
        def doSomething={ list ->  list }

        String[] x=["hello", "world"]
        String[] y=["hello", "world"]

        assert doSomething(x as String[]) == x
        assert doSomething( y ) == y
    }
    
    void testClosureAsLocalVar() {
        def local = { Map params -> params.x * params.y  }
        assert local(x : 2, y : 3) == 6
    }
    
    void testClosureDirectly() {
        assert { Map params -> params.x * params.y }(x : 2, y : 3) == 6
    }
    
    def attribute
    
    void testClosureAsAttribute() {
        attribute = { Map params ->  params.x * params.y  } 
        assert attribute(x : 2, y : 3) == 6
    }
    
    void testSystemOutPrintlnAsAClosure() {
        def closure = System.out.&println
        closure("Hello world")
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ClosureMethodCallTest.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.