"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/groovy/transform/stc/ReturnsSTCTest.groovy" (3 Sep 2021, 7370 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 "ReturnsSTCTest.groovy": 3.0.9_vs_4.0.0-rc-1.

    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.transform.stc
   20 
   21 /**
   22  * Unit tests for static type checking : implicit and explicit returns.
   23  */
   24 class ReturnsSTCTest extends StaticTypeCheckingTestCase {
   25 
   26     void testVoidReturn() {
   27         shouldFailWithMessages '''
   28             void method() {
   29             }
   30 
   31             int x = method()
   32         ''', 'Cannot assign value of type void to variable of type int'
   33     }
   34 
   35     void testIncompatibleExplicitReturn() {
   36         shouldFailWithMessages '''
   37             String method() {
   38                 return 'String'
   39             }
   40 
   41             int x = method()
   42         ''', 'Cannot assign value of type java.lang.String to variable of type int'
   43     }
   44 
   45     void testIncompatibleExplicitReturn2() {
   46         shouldFailWithMessages '''
   47             int method() {
   48                 return 'String'
   49             }
   50         ''', 'Cannot return value of type java.lang.String on method returning type int'
   51     }
   52 
   53     void testIncompatibleImplicitReturn2() {
   54         shouldFailWithMessages '''
   55             int method() {
   56                 'String'
   57             }
   58         ''', 'Cannot return value of type java.lang.String on method returning type int'
   59     }
   60 
   61     void testIncompatibleImplicitReturn() {
   62         shouldFailWithMessages '''
   63             String method() {
   64                 'String'
   65             }
   66 
   67             int x = method()
   68         ''', 'Cannot assign value of type java.lang.String to variable of type int'
   69     }
   70 
   71     void testImplicitReturnFailureWithIfElse() {
   72         shouldFailWithMessages '''
   73             int method() {
   74                 if (true) {
   75                     'String'
   76                 } else {
   77                     2
   78                 }
   79             }
   80         ''', 'Cannot return value of type java.lang.String on method returning type int'
   81     }
   82 
   83     void testImplicitReturnFailureWithIfElse2() {
   84         shouldFailWithMessages '''
   85             int method() {
   86                 if (true) {
   87                     2
   88                 } else {
   89                     'String'
   90                 }
   91             }
   92         ''', 'Cannot return value of type java.lang.String on method returning type int'
   93     }
   94 
   95     void testImplicitReturnFailureWithIfElse3() {
   96         shouldFailWithMessages '''
   97             int method() {
   98                 if (true) {
   99                     'String'
  100                 } else {
  101                     'String'
  102                 }
  103             }
  104         ''',
  105             'Cannot return value of type java.lang.String on method returning type int', // first branch
  106             'Cannot return value of type java.lang.String on method returning type int' // second branch
  107     }
  108 
  109     void testImplicitReturnFailureWithSwitch() {
  110          shouldFailWithMessages '''
  111              int method(int x) {
  112                  switch (x) {
  113                     case 1:
  114                         2
  115                         break
  116                     case 2:
  117                         'String'
  118                         break
  119                     default:
  120                         3
  121                  }
  122              }
  123          ''', 'Cannot return value of type java.lang.String on method returning type int'
  124     }
  125 
  126 
  127    void testImplicitReturnFailureWithSwitch2() {
  128          assertScript '''
  129              int method(int x) {
  130                  switch (x) {
  131                     case 1:
  132                         2
  133                         break
  134                     case 2:
  135                         'String'
  136                     default:
  137                         3
  138                  }
  139              }
  140          ''' // should not fail because default case overrides case 2 return type
  141      }
  142 
  143     void testImplicitReturnToString() {
  144         assertScript '''
  145             // automatic toString works
  146             String greeting(String name) {
  147                 def sb = new StringBuilder()
  148                 sb << "Hi" << name
  149             }
  150         '''
  151     }
  152 
  153     void testWrongReturnType() {
  154         shouldFailWithMessages '''
  155             double greeting(String name) {
  156                 new Object()
  157             }
  158         ''', 'Cannot return value of type java.lang.Object on method returning type double'
  159     }
  160 
  161     void testRecursiveTypeInferrence() {
  162         assertScript '''
  163             def fib(int i) {
  164                 i < 2 ? 1 : (fib(i - 2) as int) + (fib(i - 1) as int)
  165             }
  166             fib(2)
  167         '''
  168     }
  169 
  170     void testFindMethodWithInferredReturnType() {
  171         assertScript '''
  172             def square(int i) { i*i }
  173             int foo(int i) {
  174                 (Integer)square(i)
  175             }
  176             assert foo((Integer)square(2))==16
  177         '''
  178     }
  179 
  180     void testReturnTypeInferrenceInSingleClass() {
  181         assertScript '''
  182         class Foo {
  183             int square(int i) { i*i }
  184 
  185             int foo(int i) {
  186                 square(i)
  187             }
  188         }
  189         new Foo().foo(2)
  190         '''
  191     }
  192     
  193     void testInferredReturnTypeWithImplicitConversion() {
  194         shouldFailWithMessages '''
  195             String methodWithImplicitConversion() {
  196                 new Date()
  197             }
  198             methodWithImplicitConversion().years
  199         ''', 'No such property: years for class: java.lang.String'
  200     }
  201 
  202     // GROOVY-10079
  203     void testImplicitReturnToPrimitive() {
  204         assertScript '''
  205             int foo() {
  206                 Integer.valueOf(42)
  207             }
  208             assert foo() == 42
  209         '''
  210 
  211         assertScript '''
  212             long foo() {
  213                 Long.valueOf(1234L)
  214             }
  215             assert foo() == 1234L
  216         '''
  217 
  218         assertScript '''
  219             char foo() {
  220                 Character.valueOf((char)'x')
  221             }
  222             assert foo() == 'x'
  223         '''
  224     }
  225 
  226     // GROOVY-10087
  227     void testImplicitReturnToWrapper() {
  228         assertScript '''
  229             Integer foo() {
  230                 int x = 42
  231                 return x
  232             }
  233             assert foo().intValue() == 42
  234         '''
  235 
  236         assertScript '''
  237             Long foo() {
  238                 long x = 42L
  239                 return x
  240             }
  241             assert foo().longValue() == 42L
  242         '''
  243 
  244         assertScript '''
  245             Character foo() {
  246                 char x = 'x'
  247                 return x
  248             }
  249             assert foo().charValue() == 'x'
  250         '''
  251     }
  252 
  253     // GROOVY-5835
  254     void testReturnInClosureShouldNotBeConsideredAsReturnOfEnclosingMethod() {
  255         assertScript '''
  256             int enclosingMethod() {
  257                 def cl = { return 'String' } // should not think it's a return for the enclosing method
  258                 1
  259             }
  260         '''
  261     }
  262 
  263 }
  264