"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/groovy/operator/SpreadMapOperatorTest.groovy" (3 Sep 2021, 4317 Bytes) of package /linux/misc/apache-groovy-src-3.0.9.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Java source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "SpreadMapOperatorTest.groovy": 3.0.8_vs_3.0.9.

    1 /*
    2  *  Licensed to the Apache Software Foundation (ASF) under one
    3  *  or more contributor license agreements.  See the NOTICE file
    4  *  distributed with this work for additional information
    5  *  regarding copyright ownership.  The ASF licenses this file
    6  *  to you under the Apache License, Version 2.0 (the
    7  *  "License"); you may not use this file except in compliance
    8  *  with the License.  You may obtain a copy of the License at
    9  *
   10  *    http://www.apache.org/licenses/LICENSE-2.0
   11  *
   12  *  Unless required by applicable law or agreed to in writing,
   13  *  software distributed under the License is distributed on an
   14  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  *  KIND, either express or implied.  See the License for the
   16  *  specific language governing permissions and limitations
   17  *  under the License.
   18  */
   19 package groovy.operator
   20 
   21 import groovy.test.GroovyTestCase
   22 
   23 /**
   24  * Test the spread map operator "*:".
   25  *
   26  *   For an example,
   27  *            m = ['a':11, 'aa':22, 'aaa':33]
   28  *            z = ['c':100, *:m]
   29  *
   30  *            m = ['a':11, 'aa':22, 'aaa':33]
   31  *            w = ['c':100]
   32  *            m.each {w[it.key] = it.value }*
   33  *            assert z == w
   34  *
   35  */
   36 class SpreadMapOperatorTest extends GroovyTestCase {
   37     def f(m) {
   38         println m.c
   39     }
   40 
   41     def func(m, i, j, k) {
   42         // The first argument m is a map.
   43         println m
   44         println i
   45         println j
   46         println k
   47     }
   48 
   49     def fn() {
   50         return [1: 'ein', 2: 'zwei', 3: 'drei']
   51     }
   52 
   53     void testSpreadMap() {
   54         try {
   55             def m = ["a": 100, "b": 200]
   56             def x = ['tt': 55, *: m]
   57             println x.size()
   58             println x
   59             x = ['tt': 55, 'yy': 77]
   60             println x
   61             x = [*: m, *: m]
   62             println x
   63             assert x == m
   64 
   65             x = [*: x, *: fn(), 100: 'hundred']
   66             println x
   67             println(x.getClass())
   68             assert x instanceof Map
   69 
   70             def y = [1: 1, 2: 2, *: [3: 3, 4: 4, *: [5: 5], 6: 6], 7: 7]
   71             println y
   72             println(y.getClass())
   73             assert y == [1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7]
   74         }
   75         catch (Exception e) {
   76             e.printStackTrace()
   77         }
   78     }
   79 
   80     void testSpreadMapVsWithClosure() {
   81         def m = ['a': 11, 'aa': 22, 'aaa': 33]
   82         def z = ['c': 100, *: m]
   83 
   84         def w = ['c': 100]
   85         m.each { w[it.key] = it.value }
   86 
   87         println z
   88         println w
   89         assert z == w
   90 
   91         def z2 = [*: m, 'c': 100]
   92         def w2 = m
   93         w2['c'] = 100
   94         println z2
   95         println w2
   96         assert z2 == w2
   97         assert z == z2
   98         assert w == w2
   99     }
  100 
  101     void testSpecialSpreadMapIndexNotation() {
  102         assertScript '''
  103             @groovy.transform.ToString
  104             class Person { String name; int age }
  105 
  106             assert Person[ name:'Dave', age:32 ].toString() == 'Person(Dave, 32)'
  107 
  108             def timMap = [ name:'Tim', age:49 ]
  109             assert Person[ *:timMap ].toString() == 'Person(Tim, 49)'
  110 
  111             assert Person[ *:[ name:'John', age:29 ] ].toString() == 'Person(John, 29)'
  112 
  113             def ppl = [ [ name:'Tim', age:49 ], [ name:'Dave', age:32 ], [ name:'Steve', age:18 ] ]
  114             assert ppl.collect { Person [ *:it ] }*.age == [49, 32, 18]
  115         '''
  116     }
  117 
  118     // GROOVY-3421
  119     void testSpreadMapSingleEval() {
  120         assertScript '''
  121             int i = 1
  122             Map map = [a:i, *:[b:++i]]
  123             assert map == [a: 1, b: 2]
  124         '''
  125     }
  126 
  127     void testSpreadMapFunctionCall() {
  128         def m = ['a': 10, 'b': 20, 'c': 30]
  129         f(*: m)                 // Call with only one spread map argument
  130         f(*: m, 'e': 50)      // Call with one spread map argument and one named argument
  131         f('e': 100, *: m)     // Call with one named argument and one spread map argument
  132 
  133         func('e': 100, 1, 2, 3, *: m)
  134         // Call with one named argument, three usual arguments,  and one spread map argument
  135 
  136         def l = [4, 5]
  137         func('e': 100, *l, *: m, 6)
  138         // Call with one named argument, one spread list argument, one spread map argument, and  one usual argument
  139         func(7, 'e': 100, *l, *: m)
  140         // Call with one usual argument, one named argument, one spread list argument, and one spread map argument
  141     }
  142 }