"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/org/apache/groovy/ast/tools/ExpressionUtilsTest.groovy" (3 Sep 2021, 4379 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.

    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 org.apache.groovy.ast.tools
   20 
   21 import org.codehaus.groovy.ast.ClassHelper
   22 import org.codehaus.groovy.ast.ClassNode
   23 import org.codehaus.groovy.ast.expr.BinaryExpression
   24 import org.codehaus.groovy.ast.expr.ConstantExpression
   25 import org.codehaus.groovy.syntax.Token
   26 import org.codehaus.groovy.syntax.Types
   27 import org.junit.Test
   28 
   29 import static groovy.test.GroovyAssert.assertScript
   30 import static org.junit.Assert.assertEquals
   31 
   32 /**
   33  * Testing expressions of ExpressionUtils.
   34  */
   35 final class ExpressionUtilsTest {
   36     @Test
   37     void 'test transformBinaryConstantExpression - null + string'() {
   38         ConstantExpression left = new ConstantExpression(null)
   39         ConstantExpression right = new ConstantExpression('abc')
   40         Token token = new Token(Types.PLUS, '+', 1, 1)
   41         BinaryExpression be = new BinaryExpression(left, token, right)
   42         ClassNode targetType = ClassHelper.make(String)
   43         ConstantExpression actual = ExpressionUtils.transformBinaryConstantExpression(be, targetType)
   44         assertEquals('nullabc', actual.value)
   45     }
   46 
   47     @Test
   48     void 'test transformBinaryConstantExpression - string + null'() {
   49         ConstantExpression left = new ConstantExpression('abc')
   50         ConstantExpression right = new ConstantExpression(null)
   51         Token token = new Token(Types.PLUS, '+', 1, 1)
   52         BinaryExpression be = new BinaryExpression(left, token, right)
   53         ClassNode targetType = ClassHelper.make(String)
   54         ConstantExpression actual = ExpressionUtils.transformBinaryConstantExpression(be, targetType)
   55         assertEquals('abcnull', actual.value)
   56     }
   57 
   58     @Test
   59     void 'test transformBinaryConstantExpression - string + string'() {
   60         ConstantExpression left = new ConstantExpression('hello, ')
   61         ConstantExpression right = new ConstantExpression('world!')
   62         Token token = new Token(Types.PLUS, '+', 1, 1)
   63         BinaryExpression be = new BinaryExpression(left, token, right)
   64         ClassNode targetType = ClassHelper.make(String)
   65         ConstantExpression actual = ExpressionUtils.transformBinaryConstantExpression(be, targetType)
   66         assertEquals('hello, world!', actual.value)
   67     }
   68 
   69     @Test
   70     void 'test transformBinaryConstantExpression, integer + integer with target type string'() {
   71         ConstantExpression left = new ConstantExpression(1)
   72         ConstantExpression right = new ConstantExpression(1)
   73         Token token = new Token(Types.PLUS, '+', 1, 1)
   74         BinaryExpression be = new BinaryExpression(left, token, right)
   75         ClassNode targetType = ClassHelper.make(String)
   76         ConstantExpression actual = ExpressionUtils.transformBinaryConstantExpression(be, targetType)
   77         assert !actual // null indicates it could not be transformed (simplified) at compile time
   78         // but should still succeed at runtime as per below script
   79         assertScript '''
   80             class Foo {
   81                 static final String bar = 1 + 1
   82             }
   83             assert Foo.bar == '2'
   84         '''
   85     }
   86 
   87     @Test
   88     void 'test transformBinaryConstantExpression - long + long'() {
   89         ConstantExpression left = new ConstantExpression(11111111L)
   90         ConstantExpression right = new ConstantExpression(11111111L)
   91         Token token = new Token(Types.PLUS, '+', 1, 1)
   92         BinaryExpression be = new BinaryExpression(left, token, right)
   93         ClassNode targetType = ClassHelper.make(Long)
   94         ConstantExpression actual = ExpressionUtils.transformBinaryConstantExpression(be, targetType)
   95         assertEquals(22222222L, actual.value)
   96     }
   97 }