home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (ClosureExpressionTest.groovy)

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

closureexpressiontest, closureexpressiontest, expression, expression, groovytestcase, integer, integer, string, string

The Groovy ClosureExpressionTest.groovy source code

package org.codehaus.groovy.ast.expr

import org.codehaus.groovy.ast.builder.AstBuilder

/**
 * 
 * @author Hamlet D'Arcy
 */
class ClosureExpressionTest extends GroovyTestCase {

    void testGetText_Simple() {
        def expression = buildFromString 'return { it * it }'
        assert expression.text == '{ -> ... }'
    }

    void testGetText_Parameter() {
        def expression = buildFromString 'return { x -> x * x }'
        assert expression.text == '{ java.lang.Object x -> ... }'
    }

    void testGetText_MultipleParameters() {
        def expression = buildFromString 'return { x, y -> x * y }'
        assert expression.text == '{ java.lang.Object x, java.lang.Object y -> ... }'
    }

    void testGetText_TypedParameter() {
        def expression = buildFromString 'return { String x -> x * x }'
        assert expression.text == '{ java.lang.String x -> ... }'
    }

    void testGetText_MultipleTypedParameters() {
        def expression = buildFromString 'return { String x, Integer y -> x * y }'
        assert expression.text == '{ java.lang.String x, java.lang.Integer y -> ... }'
    }

    private Expression buildFromString(String source) {
        def ast = new AstBuilder().buildFromString(source)
        ast[0].statements[0].expression
    }
}

Other Groovy examples (source code examples)

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

new blog posts

 

Copyright 1998-2016 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.