"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/gls/invocation/DefaultParamTest.groovy" (3 Sep 2021, 8607 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 "DefaultParamTest.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 gls.invocation
   20 
   21 import groovy.test.GroovyTestCase
   22 import groovy.transform.CompileStatic
   23 
   24 @CompileStatic
   25 final class DefaultParamTest extends GroovyTestCase {
   26 
   27     void testDefaultParameterCausingDoubledMethod() {
   28         //GROOVY-2191
   29         shouldFail '''
   30             def foo(String one, String two = "two") {"$one $two"}
   31             def foo(String one, String two = "two", String three = "three") {"$one $two $three"}
   32         '''
   33 
   34         shouldFail '''
   35             def foo(String one, String two = "two", String three = "three") {"$one $two $three"}
   36             def foo(String one, String two = "two") {"$one $two"}
   37         '''
   38 
   39         shouldFail '''
   40             def meth(Closure cl = null) {meth([:], cl)}
   41             def meth(Map args = [:], Closure cl = null) {}
   42         '''
   43     }
   44 
   45     void testDefaultParameters() {
   46         assertScript '''
   47             def doSomething(a, b = 'defB', c = 'defC') {
   48                 return a + "-" + b + "-" + c
   49             }
   50             assert "X-Y-Z" == doSomething("X", "Y", "Z")
   51             assert "X-Y-defC" == doSomething("X", "Y")
   52             assert "X-defB-defC" == doSomething("X")
   53         '''
   54 
   55         shouldFail '''
   56             def doSomething(a, b = 'defB', c = 'defC') {
   57                 return a + "-" + b + "-" + c
   58             }
   59             doSomething()
   60         '''
   61     }
   62 
   63     void testDefaultTypedParameters() {
   64         assertScript '''
   65             String doTypedSomething(String a = 'defA', String b = 'defB', String c = 'defC') {
   66                 a + "-" + b + "-" + c
   67             }
   68             assert "X-Y-Z" == doTypedSomething("X", "Y", "Z")
   69             assert "X-Y-defC" == doTypedSomething("X", "Y")
   70             assert "X-defB-defC" == doTypedSomething("X")
   71             assert "defA-defB-defC" == doTypedSomething()
   72         '''
   73     }
   74 
   75     void testDefaultTypedParametersAnother() {
   76         assertScript '''
   77             String doTypedSomethingAnother(String a = 'defA', String b = 'defB', String c) {
   78                 return a + "-" + b + "-" + c
   79             }
   80             assert "X-Y-Z" == doTypedSomethingAnother("X", "Y", "Z")
   81             assert "X-defB-Z" == doTypedSomethingAnother("X", "Z")
   82             assert "defA-defB-Z" == doTypedSomethingAnother("Z")
   83         '''
   84 
   85         shouldFail '''
   86             String doTypedSomethingAnother(String a = 'defA', String b = 'defB', String c) {
   87                 return a + "-" + b + "-" + c
   88             }
   89             doTypedSomethingAnother()
   90         '''
   91     }
   92 
   93     void testConstructor() {
   94         assertScript '''
   95             class DefaultParamTestTestClass {
   96                 def j
   97                 DefaultParamTestTestClass(int i = 1){j=i}
   98             }
   99             assert DefaultParamTestTestClass.declaredConstructors.size() == 2
  100             def foo = new DefaultParamTestTestClass()
  101             assert foo.j == 1
  102             foo = new DefaultParamTestTestClass(2)
  103             assert foo.j == 2
  104         '''
  105     }
  106 
  107     void testPrecendence() {
  108         // def meth(Closure cl = null) will produce a call meth(null)
  109         // since interfaces are prefered over normal classes and since
  110         // def meth(Map args, Closure cl = null) will produce a method
  111         // meth(Map) a simple call with meth(null) would normally call
  112         // meth(Map). To ensure this will not happen the call has to
  113         // use a cast in the automatically created method.
  114         assertScript '''
  115             def meth(Closure cl = null) {
  116               return '1' +meth([:], cl)
  117             }
  118 
  119             def meth(Map args, Closure cl = null) {
  120                 if(args==null) return "2"
  121                 return '2'+args.size()
  122             }
  123 
  124             assert meth() == "120"
  125             assert meth(null) == "2"
  126             assert meth {} == "120"
  127             assert meth(a:1) == "21"
  128             assert meth(a:1) {} == "21"
  129         '''
  130     }
  131 
  132     // GROOVY-9151
  133     void testMethodWithAllParametersDefaulted() {
  134         assertScript '''
  135             String greet(Object o = 'world', String s = o.toString()) {
  136                 "hello $s"
  137             }
  138             assert greet() == 'hello world'
  139         '''
  140     }
  141 
  142     // GROOVY-6851, GROOVY-9151, GROOVY-10104
  143     void testMethodWithAllParametersDefaultedCS() {
  144         assertScript '''
  145             @groovy.transform.CompileStatic
  146             String greet(Object o = 'world', String s = o.toString()) {
  147                 "hello $s"
  148             }
  149             assert greet() == 'hello world'
  150         '''
  151 
  152         assertScript '''
  153             @groovy.transform.CompileStatic
  154             class Main {
  155                 static main(args) {
  156                     assert new Main().test().isEmpty()
  157                 }
  158                 Map test(Map<String, Object> m = new HashMap<>(Collections.emptyMap())) {
  159                     return m
  160                 }
  161             }
  162         '''
  163     }
  164 
  165     // GROOVY-9151
  166     void testConstructorWithAllParametersDefaulted() {
  167         assertScript '''
  168             class Greeting {
  169                 Greeting(Object o = 'world', String s = o) {
  170                     this.text = "hello $s"
  171                 }
  172                 String text
  173             }
  174             assert new Greeting().text == 'hello world'
  175         '''
  176     }
  177 
  178     // GROOVY-9151
  179     void testConstructorWithAllParametersDefaulted2() {
  180         def err = shouldFail '''
  181             class Greeting {
  182                 Greeting(Object o = 'world', String s = o.toString()) {
  183                     this.text = "hello $s"
  184                 }
  185                 String text
  186             }
  187             assert new Greeting().text == 'hello world'
  188         '''
  189 
  190         assert err =~ /The generated constructor "Greeting\(\)" references parameter 'o' which has been replaced by a default value expression./
  191     }
  192 
  193     // GROOVY-5632
  194     void testClosureSharedVariableRefersToDefaultParameter1() {
  195         assertScript '''
  196             def f1( int x = 3, fn={ -> x } ) {
  197                 return fn()
  198             }
  199 
  200             assert 3 == f1()
  201             assert 42 == f1(42)
  202         '''
  203     }
  204 
  205     // GROOVY-5632
  206     void testClosureSharedVariableRefersToDefaultParameter2() {
  207         assertScript '''
  208            def f2( int x = 3, fn={ -> def c2 = { -> x }; c2.call() } ) {
  209                return fn()
  210            }
  211 
  212            assert 42 == f2(42)
  213            assert 42 == f2(42)
  214            assert 84 == f2(42) { 84 }
  215         '''
  216     }
  217 
  218     // GROOVY-5632
  219     void testClosureSharedVariableRefersToDefaultParameter3() {
  220         assertScript '''
  221            def f3(fn={ -> 42 }, fn2={ -> def c2 = { -> fn() }; c2.call() } ) {
  222                return fn2()
  223            }
  224 
  225            assert 42 == f3()
  226            assert 84 == f3({ -> 84 })
  227         '''
  228     }
  229 
  230     // GROOVY-5632
  231     void testClosureSharedVariableRefersToDefaultParameter4() {
  232         assertScript '''
  233            def f4(def s = [1,2,3], fn = { -> s.size() }) {
  234                fn()
  235            }
  236 
  237            assert 3 == f4()
  238         '''
  239     }
  240 
  241     // GROOVY-5632
  242     void testClosureSharedVariableRefersToDefaultParameter5() {
  243         assertScript '''
  244            static <T extends Number> Integer f5(List<T> s = [1,2,3], fn = { -> (s*.intValue()).sum() }) {
  245                fn()
  246            }
  247 
  248            assert 6 == f5()
  249            assert 6 == f5([1.1, 2.1, 3.1])
  250         '''
  251     }
  252 
  253     // GROOVY-5632
  254     void testClosureSharedVariableRefersToDefaultParameter6() {
  255         assertScript '''
  256            def f6(def s = [1,2,3], fn = { -> s.size() }, fn2 = { fn() + s.size() }) {
  257                fn2()
  258            }
  259 
  260            assert 6 == f6()
  261         '''
  262     }
  263 
  264     // GROOVY-5632
  265     void testClosureSharedVariableRefersToDefaultParameter7() {
  266         assertScript '''
  267             def f7( int x = 3, int y = 39, fn={ -> x + y } ) {
  268                 return fn()
  269             }
  270 
  271             assert 42 == f7()
  272         '''
  273     }
  274 }