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

Groovy example source code file (MethodParameterAccessWithinClosureTest.groovy)

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

a, a, b, b, groovytestcase, methodparameteraccesswithinclosuretest, methodparameteraccesswithinclosuretest, string, string

The Groovy MethodParameterAccessWithinClosureTest.groovy source code

package groovy

/**
 * To test access to method scoped variable within closure
 * 
 * @author <a href="mailto:jeremy.rayner@bigfoot.com">Jeremy Rayner
 * @version $Revision: 8042 $
 */


class MethodParameterAccessWithinClosureTest extends GroovyTestCase { 
    def cheese
    def shop
       
    void setUp() {
        cheese = null
        shop = ["wensleydale"]
    }              
    void testSimpleMethodParameterAccess() { 
        assert "wensleydale" == vendor1("wensleydale") 
    }
    void testMethodParameterWithDifferentNameToPropertyUsingClosure() {
        assert "wensleydale" == vendor2("wensleydale")
    }
    void testMethodParameterWithSameNameAsPropertyUsingClosure() {
        assert "wensleydale" == vendor3("wensleydale")
    }
    
    void testOptionalMethodParameterUsedInClosure() {
        assert "wensleydale" == vendor4("wensleydale")
        assert null == vendor4()
    }
    
    void testDoubleParameterAndsingleParameterUsedInClosure() {
         assert vendor5(5.0d,2) == 7.0d
    }
    
    void testAccessToMethodParameterInOverwrittenMethodCalledBySuper() {
         //  GROOVY-2107
         assertScript """
           class A {
             // the closure is accessing the parameter
             def foo(x){ return {x}}
           }
           class B extends A {
              def foo(y) {
                 super.foo(y+1)
              }
           }
           def b = new B()
           assert b.foo(1).call() == 2
        """        
    }
    
    private String vendor1(cheese) {
        cheese
    }
    
    private String vendor2(aCheese) {
        shop.find() {it == aCheese}
    }
    
    private String vendor3(cheese) {
        shop.find() {it == cheese}
    }
    
    /** note: cheese is a field, that is intended **/
    private vendor4(aCheese=cheese) {
        shop.find() {it == aCheese}
    }
    
    private vendor5(double a, int b) {
        b.times {a++}
        return a
    }
}

Other Groovy examples (source code examples)

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