"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/src/test/groovy/lang/MixinAnnotationTest.groovy" (3 Sep 2021, 5866 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 "MixinAnnotationTest.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.lang
   20 
   21 import org.codehaus.groovy.reflection.ReflectionCache
   22 import org.junit.After
   23 import org.junit.Test
   24 
   25 import static groovy.test.GroovyAssert.assertScript
   26 
   27 final class MixinAnnotationTest {
   28 
   29     @Test
   30     void testSingleMixinAnnotation() {
   31         assertScript '''
   32             interface Mixed {
   33                 def getA()
   34             }
   35 
   36             @Category(Mixed)
   37             class CategoryToUse {
   38                 private static String msg = 'under category: '
   39                 String asText() {
   40                     msg + this + ': ' + a
   41                 }
   42             }
   43 
   44             @Mixin(CategoryToUse)
   45             class ClassToExtend implements Mixed {
   46                 String toString() {
   47                     'object of ClassToExtend'
   48                 }
   49                 def a = 'blah'
   50             }
   51 
   52             def result = new ClassToExtend().asText()
   53             assert result == 'under category: object of ClassToExtend: blah'
   54             groovy.test.GroovyAssert.shouldFail(MissingMethodException) {
   55                 new Object().asText()
   56             }
   57         '''
   58     }
   59 
   60     @Test
   61     void testMultipleMixinAnnotation () {
   62         assertScript '''
   63             @Category(Object)
   64             class CategoryToUse1 {
   65                 def asText() {
   66                     'under category: ' + asBiggerText()
   67                 }
   68             }
   69 
   70             @Category(Object)
   71             class CategoryToUse2 {
   72                 def asBiggerText() {
   73                     'under BIG category: ' + this
   74                 }
   75             }
   76 
   77             @Mixin([CategoryToUse1, CategoryToUse2])
   78             class ClassToExtend {
   79                 String toString() {
   80                     'object of ClassToExtend'
   81                 }
   82             }
   83 
   84             def result = new ClassToExtend().asText()
   85             assert result == 'under category: under BIG category: object of ClassToExtend'
   86         '''
   87     }
   88 
   89     @Test // GROOVY-10200
   90     void testStaticInnerMixinAnnotation() {
   91         assertScript '''
   92             class A {
   93                 def getB() {
   94                     'works'
   95                 }
   96             }
   97             class C {
   98                 @Mixin(A)
   99                 static class D {
  100                     def test() {
  101                         return b
  102                     }
  103                 }
  104             }
  105             def result = new C.D().test()
  106             assert result == 'works'
  107         '''
  108     }
  109 
  110     @After
  111     void tearDown() {
  112         ReflectionCache.getCachedClass(ArrayList).setNewMopMethods(null)
  113         ReflectionCache.getCachedClass(List).setNewMopMethods(null)
  114     }
  115 
  116 //    void testOneClass () {
  117 //        List.mixin ListExt
  118 //        ArrayList.mixin ArrayListExt
  119 //        assertEquals 1, [0,1].swap () [0]
  120 //        assertEquals 0, [0,1].swap () [1]
  121 //        assertEquals 0, [0,1].swap ().unswap () [0]
  122 //        assertEquals 1, [0,1].swap ().unswap () [1]
  123 //    }
  124 //
  125 //    void testWithList () {
  126 //        ArrayList.mixin ArrayListExt, ListExt
  127 //        assertEquals 1, [0,1].swap () [0]
  128 //        assertEquals 0, [0,1].swap () [1]
  129 //        assertEquals 0, [0,1].swap ().unswap () [0]
  130 //        assertEquals 1, [0,1].swap ().unswap () [1]
  131 //    }
  132 //
  133 //    void testCombined () {
  134 //        ArrayList.mixin Combined
  135 //        assertEquals 1, [0,1].swap () [0]
  136 //        assertEquals 0, [0,1].swap () [1]
  137 //        assertEquals 0, [0,1].swap ().unswap () [0]
  138 //        assertEquals 1, [0,1].swap ().unswap () [1]
  139 //    }
  140 //
  141 //    void testWithEmc () {
  142 //        ArrayList.metaClass.unswap = {
  143 //            [delegate[1], delegate[0]]
  144 //        }
  145 //        ArrayList.mixin ArrayListExt
  146 //        assertEquals 1, [0,1].swap () [0]
  147 //        assertEquals 0, [0,1].swap () [1]
  148 //        assertEquals 0, [0,1].swap ().unswap () [0]
  149 //        assertEquals 1, [0,1].swap ().unswap () [1]
  150 //    }
  151 //
  152 //    void testGroovyObject () {
  153 //        ObjToTest obj = new ObjToTest ()
  154 //        assertEquals "original", obj.value
  155 //        obj.mixin ObjToTestCategory
  156 //        assertEquals "changed", obj.value
  157 //        assertEquals "original", new ObjToTest ().value
  158 //    }
  159 //
  160 //    void testGroovyObjectWithEmc () {
  161 //        ObjToTest.metaClass.getValue = { ->
  162 //            "emc changed"
  163 //        }
  164 //        ObjToTest obj = new ObjToTest ()
  165 //        assertEquals "emc changed", obj.getValue()
  166 //        obj.mixin ObjToTestCategory
  167 //        assertEquals "changed", obj.value
  168 //        assertEquals "emc changed", new ObjToTest ().value
  169 //    }
  170 //}
  171 //
  172 //class ArrayListExt {
  173 //    static def swap (ArrayList self) {
  174 //        [self[1], self[0]]
  175 //    }
  176 //}
  177 //
  178 //class ListExt {
  179 //    static def unswap (List self) {
  180 //        [self[1], self[0]]
  181 //    }
  182 //}
  183 //
  184 //class Combined {
  185 //    static def swap (ArrayList self) {
  186 //        [self[1], self[0]]
  187 //    }
  188 //
  189 //    static def unswap (List self) {
  190 //        [self[1], self[0]]
  191 //    }
  192 //}
  193 //
  194 //class ObjToTest {
  195 //    def getValue () {
  196 //        "original"
  197 //    }
  198 //}
  199 //
  200 //class ObjToTestCategory {
  201 //    static getValue (ObjToTest self) {
  202 //        "changed"
  203 //    }
  204 }