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

Groovy example source code file (ClosureDefaultParameterTest.groovy)

This example Groovy source code file (ClosureDefaultParameterTest.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

closuredefaultparametertest, closuredefaultparametertest, groovytestcase, groovytestcase, integer, integer, string, string

The Groovy ClosureDefaultParameterTest.groovy source code

package groovy

/** 
 * @author <a href="mailto:jstrachan@protique.com">James Strachan
 * @version $Revision: 9213 $
 */
class ClosureDefaultParameterTest extends GroovyTestCase {

    void testClosureWithDefaultParams() {

        def block = {a = 123, b = 456 -> println "value of a = $a and b = $b" }

        block = { Integer a = 123, String b = "abc" ->
                  println "value of a = $a and b = $b"; return "$a $b".toString() }

        assert block.call(456, "def") == "456 def"
        assert block.call() == "123 abc"
        assert block(456) == "456 abc"
        assert block(456, "def") == "456 def"
    }
    
    void testClosureWithDefaultParamFromOuterScope() {
        def y = 555
        def boo = {x = y -> x}
        assert boo() == y
        assert boo(1) == 1
    }

}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy ClosureDefaultParameterTest.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.