"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/org/codehaus/groovy/ast/tools/WideningCategoriesTest.groovy" (3 Sep 2021, 17112 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 "WideningCategoriesTest.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 org.codehaus.groovy.ast.tools
   20 
   21 import org.codehaus.groovy.ast.ClassNode
   22 import org.codehaus.groovy.ast.GenericsTestCase
   23 
   24 import static org.codehaus.groovy.ast.ClassHelper.*
   25 import static org.codehaus.groovy.ast.tools.WideningCategories.*
   26 
   27 final class WideningCategoriesTest extends GenericsTestCase {
   28 
   29     void testBuildCommonTypeWithNullClassNode() {
   30         ClassNode a = null
   31         ClassNode b = make(Serializable)
   32         assert lowestUpperBound(a,b) == null
   33         assert lowestUpperBound(b,a) == null
   34     }
   35 
   36     void testBuildCommonTypeWithObjectClassNode() {
   37         ClassNode a = OBJECT_TYPE
   38         ClassNode b = make(Serializable)
   39         assert lowestUpperBound(a,b) == OBJECT_TYPE
   40         assert lowestUpperBound(b,a) == OBJECT_TYPE
   41     }
   42 
   43     void testBuildCommonTypeWithVoidClassNode() {
   44         ClassNode a = VOID_TYPE
   45         ClassNode b = VOID_TYPE
   46         assert lowestUpperBound(a,b) == VOID_TYPE
   47         assert lowestUpperBound(b,a) == VOID_TYPE
   48     }
   49 
   50     void testBuildCommonTypeWithVoidClassNodeAndAnyNode() {
   51         ClassNode a = VOID_TYPE
   52         ClassNode b = make(Set)
   53         assert lowestUpperBound(a,b) == OBJECT_TYPE
   54         assert lowestUpperBound(b,a) == OBJECT_TYPE
   55     }
   56 
   57     void testBuildCommonTypeWithIdenticalInterfaces() {
   58         ClassNode a = make(Serializable)
   59         ClassNode b = make(Serializable)
   60         assert lowestUpperBound(a,b) == make(Serializable)
   61     }
   62 
   63     void testBuildCommonTypeWithOneInterfaceInheritsFromOther() {
   64         ClassNode a = make(Set)
   65         ClassNode b = make(SortedSet)
   66         assert lowestUpperBound(a,b) == make(Set)
   67         assert lowestUpperBound(b,a) == make(Set)
   68     }
   69 
   70     void testBuildCommonTypeWithTwoIncompatibleInterfaces() {
   71         ClassNode a = make(Set)
   72         ClassNode b = make(Map)
   73         assert lowestUpperBound(a,b) == OBJECT_TYPE
   74         assert lowestUpperBound(b,a) == OBJECT_TYPE
   75     }
   76 
   77     void testBuildCommonTypeWithOneClassAndOneImplementedInterface() {
   78         ClassNode a = make(Set)
   79         ClassNode b = make(HashSet)
   80         assert lowestUpperBound(a,b) == make(Set)
   81         assert lowestUpperBound(b,a) == make(Set)
   82     }
   83 
   84     void testBuildCommonTypeWithOneClassAndNoImplementedInterface() {
   85         ClassNode a = make(Map)
   86         ClassNode b = make(HashSet)
   87         assert lowestUpperBound(a,b) == OBJECT_TYPE
   88         assert lowestUpperBound(b,a) == OBJECT_TYPE
   89     }
   90 
   91     void testBuildCommonTypeWithTwoClassesWithoutSuperClass() {
   92         ClassNode a = make(ClassA)
   93         ClassNode b = make(ClassB)
   94         assert lowestUpperBound(a,b) == make(GroovyObject) // GroovyObject because Groovy classes implicitly implement GroovyObject
   95         assert lowestUpperBound(b,a) == make(GroovyObject)
   96     }
   97 
   98     void testBuildCommonTypeWithIdenticalPrimitiveTypes() {
   99         [int_TYPE, long_TYPE, short_TYPE, boolean_TYPE, float_TYPE, double_TYPE].each {
  100             ClassNode a = it
  101             ClassNode b = it
  102             assert lowestUpperBound(a,b) == it
  103             assert lowestUpperBound(b,a) == it
  104         }
  105     }
  106 
  107     void testBuildCommonTypeWithPrimitiveTypeAndItsBoxedVersion() {
  108         [int_TYPE, long_TYPE, short_TYPE, boolean_TYPE, float_TYPE, double_TYPE].each {
  109             ClassNode a = it
  110             ClassNode b = getWrapper(it)
  111             assert lowestUpperBound(a,b) == getWrapper(it)
  112             assert lowestUpperBound(b,a) == getWrapper(it)
  113         }
  114     }
  115 
  116     void testBuildCommonTypeWithTwoIdenticalClasses() {
  117         ClassNode a = make(HashSet)
  118         ClassNode b = make(HashSet)
  119         assert lowestUpperBound(a,b) == make(HashSet)
  120         assert lowestUpperBound(b,a) == make(HashSet)
  121     }
  122 
  123     void testBuildCommonTypeWithOneClassInheritsFromAnother() {
  124         ClassNode a = make(HashSet)
  125         ClassNode b = make(LinkedHashSet)
  126         assert lowestUpperBound(a,b) == make(HashSet)
  127         assert lowestUpperBound(b,a) == make(HashSet)
  128     }
  129 
  130     void testBuildCommonTypeWithTwoInterfacesSharingOneParent() {
  131         ClassNode a = make(InterfaceCA)
  132         ClassNode b = make(InterfaceDA)
  133         assert lowestUpperBound(a,b) == make(InterfaceA)
  134         assert lowestUpperBound(b,a) == make(InterfaceA)
  135     }
  136 
  137     void testBuildCommonTypeWithTwoInterfacesSharingTwoParents() {
  138         ClassNode a = make(InterfaceCAB)
  139         ClassNode b = make(InterfaceDAB)
  140         assert lowestUpperBound(a,b).interfaces as Set == [make(InterfaceA), make(InterfaceB)] as Set
  141         assert lowestUpperBound(b,a).interfaces as Set == [make(InterfaceA), make(InterfaceB)] as Set
  142     }
  143 
  144     void testBuildCommonTypeWithTwoInterfacesSharingTwoParentsAndOneDifferent() {
  145         ClassNode a = make(InterfaceCAB)
  146         ClassNode b = make(InterfaceDABE)
  147         assert lowestUpperBound(a,b).interfaces as Set == [make(InterfaceA), make(InterfaceB)] as Set
  148         assert lowestUpperBound(b,a).interfaces as Set == [make(InterfaceA), make(InterfaceB)] as Set
  149     }
  150 
  151     void testBuildCommonTypeFromTwoClassesInDifferentBranches() {
  152         ClassNode a = make(ClassA1)
  153         ClassNode b = make(ClassB1)
  154         assert lowestUpperBound(a,b) == make(GroovyObject)
  155         assert lowestUpperBound(b,a) == make(GroovyObject)
  156     }
  157 
  158     void testBuildCommonTypeFromTwoClassesInDifferentBranchesAndOneCommonInterface() {
  159         ClassNode a = make(ClassA1_Serializable)
  160         ClassNode b = make(ClassB1_Serializable)
  161         assert lowestUpperBound(a,b).interfaces as Set == [make(Serializable), make(GroovyObject)] as Set
  162         assert lowestUpperBound(b,a).interfaces as Set == [make(Serializable), make(GroovyObject)] as Set
  163     }
  164 
  165     void testBuildCommonTypeFromTwoClassesWithCommonSuperClassAndOneCommonInterface() {
  166         ClassNode a = make(BottomA)
  167         ClassNode b = make(BottomB)
  168         ClassNode type = lowestUpperBound(a, b)
  169         assert type.name =~ /.*Top/
  170         assert type.superClass == make(Top) // includes interface GroovyObject
  171         assert type.interfaces as Set == [make(Serializable)] as Set // extra interface
  172         type = lowestUpperBound(b, a)
  173         assert type.name =~ /.*Top/
  174         assert type.superClass == make(Top)
  175         assert type.interfaces as Set == [make(Serializable)] as Set
  176     }
  177 
  178     // GROOVY-8111
  179     void testBuildCommonTypeFromTwoClassesWithTwoCommonInterfacesOneIsSelfReferential() {
  180         ClassNode a = boolean_TYPE
  181         ClassNode b = extractTypesFromCode("${getClass().getName()}.Pair<String,String> type").type
  182         ClassNode lub = lowestUpperBound(a, b)
  183 
  184         assert lub.superClass == OBJECT_TYPE
  185         assert lub.interfaces as Set == [make(Comparable), make(Serializable)] as Set
  186 
  187         lub = lowestUpperBound(b, a)
  188         assert lub.superClass == OBJECT_TYPE
  189         assert lub.interfaces as Set == [make(Comparable), make(Serializable)] as Set
  190     }
  191 
  192     void testStringWithGString() {
  193         ClassNode a = make(String)
  194         ClassNode b = make(GString)
  195         ClassNode type = lowestUpperBound(a,b)
  196         assert type.interfaces as Set == [make(CharSequence), make(Comparable), make(Serializable)] as Set
  197     }
  198 
  199     void testDistinctPrimitiveTypes() {
  200         ClassNode a = int_TYPE // primitive int
  201         ClassNode b = long_TYPE // primitive long
  202         assert lowestUpperBound(a,b) == long_TYPE
  203     }
  204 
  205     void testIdenticalPrimitiveTypes() {
  206         ClassNode a = int_TYPE // primitive int
  207         ClassNode b = int_TYPE // primitive int
  208         assert lowestUpperBound(a,b) == int_TYPE
  209         assert lowestUpperBound(b,a) == int_TYPE
  210     }
  211 
  212     void testLUBWithTwoInterfacesAndSameGenericArg() {
  213         ClassNode a = extractTypesFromCode("List<String> type").type
  214         ClassNode b = extractTypesFromCode("List<String> type").type
  215         ClassNode lub = lowestUpperBound(a,b)
  216         assert lub == make(List)
  217         assert lub.genericsTypes.length == 1
  218         assert lub.genericsTypes[0].type == STRING_TYPE
  219     }
  220 
  221     void testLUBWithTwoInterfacesAndCommonSuperClassGenericArg() {
  222         ClassNode a = extractTypesFromCode("List<Integer> type").type
  223         ClassNode b = extractTypesFromCode("List<Long> type").type
  224         ClassNode lub = lowestUpperBound(a,b)
  225         assert lub == make(List)
  226         assert lub.genericsTypes.length == 1
  227         assert lub.genericsTypes[0].wildcard
  228         assert lub.genericsTypes[0].upperBounds[0].superClass == Number_TYPE
  229         assert make(Comparable) in lub.genericsTypes[0].upperBounds[0].interfaces
  230     }
  231 
  232     void testLUBWithTwoInterfacesAndSingleCommonInterface() {
  233         ClassNode a = extractTypesFromCode("List<Set> type").type
  234         ClassNode b = extractTypesFromCode("List<List> type").type
  235         ClassNode lub = lowestUpperBound(a,b)
  236         assert lub == make(List)
  237         assert lub.genericsTypes.length == 1
  238         assert lub.genericsTypes[0].wildcard
  239         assert lub.genericsTypes[0].upperBounds[0] == make(Collection)
  240     }
  241 
  242     void testLUBWithTwoInterfacesAndNestedSingleCommonInterface() {
  243         ClassNode a = extractTypesFromCode("Collection<List<Set>> type").type
  244         ClassNode b = extractTypesFromCode("Collection<List<SortedSet>> type").type
  245         ClassNode lub = lowestUpperBound(a,b)
  246         assert lub == make(Collection)
  247         assert lub.genericsTypes.length == 1
  248         def nestedType = lub.genericsTypes[0].type
  249         assert nestedType == make(List)
  250         assert nestedType.genericsTypes.length==1
  251         assert nestedType.genericsTypes[0].wildcard
  252         assert nestedType.genericsTypes[0].upperBounds[0] == make(Set)
  253     }
  254 
  255     void testLUBWithTwoArgumentTypesSharingOneInterfaceNotImplementedBySuperClass() {
  256         // BottomA extends Top implements Serializable
  257         // BottomB extends Top implements Serializable
  258         // Top does not implement Serializable
  259         ClassNode a = extractTypesFromCode('List<org.codehaus.groovy.ast.tools.WideningCategoriesTest.BottomA> type').type
  260         ClassNode b = extractTypesFromCode('List<org.codehaus.groovy.ast.tools.WideningCategoriesTest.BottomB> type').type
  261         ClassNode lub = lowestUpperBound(a,b)
  262         assert lub == LIST_TYPE // List<? extends Top>
  263         assert lub.genericsTypes.length == 1
  264         assert lub.genericsTypes[0].wildcard
  265         ClassNode genericType = lub.genericsTypes[0].upperBounds[0]
  266         assert genericType instanceof LowestUpperBoundClassNode
  267         assert genericType.superClass == make(Top)
  268         assert genericType.interfaces == [make(Serializable)]
  269     }
  270 
  271     void testLUBWithTwoParameterizedTypesSharingOneInterfaceNotImplementedBySuperClass() {
  272         // PTopInt extends PTop<Integer> implements Serializable
  273         // PTopLong extends PTop<Long> implements Serializable
  274         // PTop<E> does not implement Serializable
  275         ClassNode a = extractTypesFromCode('org.codehaus.groovy.ast.tools.WideningCategoriesTest.PTopInt type').type
  276         ClassNode b = extractTypesFromCode('org.codehaus.groovy.ast.tools.WideningCategoriesTest.PTopLong type').type
  277         ClassNode lub = lowestUpperBound(a,b)
  278         assert lub instanceof LowestUpperBoundClassNode // a virtual class which extends PTop<? extends Number> and implements Serializable
  279         assert lub.unresolvedSuperClass == make(PTop)
  280         assert lub.unresolvedSuperClass.genericsTypes.length == 1
  281         assert lub.unresolvedSuperClass.genericsTypes[0].wildcard // ? extends Number
  282         ClassNode genericType = lub.unresolvedSuperClass.genericsTypes[0].upperBounds[0]
  283         assert genericType == Long_TYPE
  284     }
  285 
  286     void testCommonAssignableType() {
  287         def typeA = extractTypesFromCode('LinkedList type').type
  288         def typeB = extractTypesFromCode('List type').type
  289         def superType = lowestUpperBound(typeA, typeB)
  290         assert superType == make(List)
  291     }
  292 
  293     void testCommonAssignableType2() {
  294         def typeA = extractTypesFromCode('LinkedHashSet type').type
  295         def typeB = extractTypesFromCode('List type').type
  296         def superType = lowestUpperBound(typeA, typeB)
  297         assert superType == make(Collection)
  298     }
  299 
  300     void testCommonAssignableTypeWithGenerics() {
  301         def typeA = extractTypesFromCode('LinkedHashSet<String> type').type
  302         def typeB = extractTypesFromCode('List<String> type').type
  303         def superType = lowestUpperBound(typeA, typeB)
  304         assert superType == make(Collection)
  305     }
  306 
  307     void testLUBOfTwoListTypes() {
  308         def typeA = extractTypesFromCode('ArrayList type').type
  309         def typeB = extractTypesFromCode('LinkedList type').type
  310         def superType = lowestUpperBound(typeA, typeB)
  311         assert superType instanceof LowestUpperBoundClassNode
  312         assert superType.superClass == make(AbstractList)
  313         assert superType.interfaces as Set == [make(Serializable), make(Cloneable)] as Set
  314     }
  315 
  316     void testLUBOfTwoListTypesWithSameGenerics() {
  317         def typeA = extractTypesFromCode('ArrayList<String> type').type
  318         def typeB = extractTypesFromCode('LinkedList<String> type').type
  319         def superType = lowestUpperBound(typeA, typeB)
  320         assert superType instanceof LowestUpperBoundClassNode
  321         assert superType.superClass == make(AbstractList)
  322         assert superType.interfaces as Set == [make(Serializable), make(Cloneable)] as Set
  323         assert superType.genericsTypes.length == 1
  324         assert superType.genericsTypes[0].type == STRING_TYPE
  325 
  326     }
  327 
  328     void testLUBOfTwoListTypesWithDistinctGenerics() {
  329         def typeA = extractTypesFromCode('ArrayList<String> type').type
  330         def typeB = extractTypesFromCode('LinkedList<Integer> type').type
  331         def superType = lowestUpperBound(typeA, typeB)
  332         assert superType instanceof LowestUpperBoundClassNode
  333         assert superType.superClass == make(AbstractList)
  334         assert superType.interfaces as Set == [make(Serializable), make(Cloneable)] as Set
  335         assert superType.genericsTypes.length == 1
  336         def type = superType.genericsTypes[0]
  337         assert type.wildcard
  338         assert type.upperBounds[0] instanceof LowestUpperBoundClassNode
  339         [Comparable, Serializable].each {
  340             assert make(it) in type.upperBounds[0].interfaces
  341         }
  342     }
  343 
  344     void testLUBOfArrayTypes() {
  345         def typeA = extractTypesFromCode('Number[] type').type
  346         def typeB = extractTypesFromCode('Integer[] type').type
  347         def superType = lowestUpperBound(typeA, typeB)
  348         assert superType.isArray()
  349         def component = superType.getComponentType()
  350         assert component == make(Number)
  351     }
  352 
  353     // ---------- Classes and Interfaces used in this unit test ----------------
  354 
  355     private static interface InterfaceA {}
  356     private static interface InterfaceB {}
  357     private static interface InterfaceE {}
  358     private static interface InterfaceCA extends InterfaceA {}
  359     private static interface InterfaceDA extends InterfaceA {}
  360     private static interface InterfaceCAB extends InterfaceA, InterfaceB {}
  361     private static interface InterfaceDAB extends InterfaceA, InterfaceB {}
  362     private static interface InterfaceDABE extends InterfaceA, InterfaceB, InterfaceE {}
  363 
  364     private static class ClassA {}
  365     private static class ClassB {}
  366     private static class ClassA1 extends ClassA {}
  367     private static class ClassB1 extends ClassB {}
  368     private static class ClassA1_Serializable extends ClassA implements Serializable {}
  369     private static class ClassB1_Serializable extends ClassB implements Serializable {}
  370 
  371     private static class Top {}
  372     private static class BottomA extends Top implements Serializable {}
  373     private static class BottomB extends Top implements Serializable {}
  374 
  375     private static class PTop<E> {}
  376     private static class PTopInt extends PTop<Integer> implements Serializable {}
  377     private static class PTopLong extends PTop<Long  > implements Serializable {}
  378 
  379     private static class Pair<L,R> implements Map.Entry<L,R>, Comparable<Pair<L,R>>, Serializable {
  380         public final L left
  381         public final R right
  382         private Pair(final L left, final R right) {
  383             this.left = left
  384             this.right = right
  385         }
  386         static <L, R> Pair<L, R> of(final L left, final R right) {
  387             return new Pair<>(left, right)
  388         }
  389         L getKey() { left }
  390         R getValue() { right }
  391         R setValue(R value) { right }
  392         int compareTo(Pair<L,R> that) { 0 }
  393     }
  394 }