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

Groovy example source code file (SpreadMapOperatorTest.groovy)

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

call, call, exception, exception, groovytestcase, map, map, spreadmapoperatortest, spreadmapoperatortest

The Groovy SpreadMapOperatorTest.groovy source code

package groovy.operator

/**
 * Test the spread map operator "*:".
 *
 *   For an example,
 *            m = ['a':11, 'aa':22, 'aaa':33]
 *            z = ['c':100, *:m]
 *
 *            m = ['a':11, 'aa':22, 'aaa':33]
 *            w = ['c':100]
 *            m.each {w[it.key] = it.value }
 *
 *            assert z == w
 *
 * @author Pilho Kim
 * @version $Revision: 8533 $
 */

public class SpreadMapOperatorTest extends GroovyTestCase {
    def f(m) {
        println m.c
    }

    def func(m, i, j, k) {
        // The first argument m is a map.
        println m
        println i
        println j
        println k
    }

    def fn() {
        return [ 1:'ein', 2:'zwei', 3:'drei' ]
    }

    void testSpreadMap() {
        try {
            def m = ["a":100, "b":200]
            def x = ['tt':55, *:m]
            println x.size()
            println x
            x = ['tt':55, 'yy':77]
            println x
            x = [*:m, *:m]
            println x
            assert x == m

            x = [*:x, *:fn(), 100:'hundred']
            println x
            println(x.getClass())
            assert x instanceof Map

            def y = [1:1, 2:2, *:[3:3, 4:4, *:[5:5], 6:6], 7:7]
            println y
            println(y.getClass())
            assert y == [1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7]
        }
        catch (Exception e) {
            e.printStackTrace()
        }
    }

    void testSpreadMapVsWithClosure() {
        def m = ['a':11, 'aa':22, 'aaa':33]
        def z = ['c':100, *:m]
 
        def w = ['c':100]
        m.each { w[it.key] = it.value }

        println z 
        println w 
        assert z == w

        def z2 = [*:m, 'c':100]
        def w2 = m
        w2['c'] = 100
        println z2 
        println w2 
        assert z2 == w2
        assert z == z2
        assert w == w2
    }

    void testSpreadMapFunctionCall() {
             def m = ['a':10, 'b':20, 'c':30]
             f(*:m)                 // Call with only one spread map argument
             f(*:m, 'e':50)      // Call with one spread map argument and one named argument
             f('e':100, *:m)     // Call with one named argument and one spread map argument

             func('e':100, 1, 2, 3, *:m)       // Call with one named argument, three usual arguments,  and one spread map argument

             def l = [4, 5]
             func('e':100, *l, *:m, 6)       // Call with one named argument, one spread list argument, one spread map argument, and  one usual argument
             func(7, 'e':100, *l, *:m)       // Call with one usual argument, one named argument, one spread list argument, and one spread map argument 
    }
}

Other Groovy examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

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