"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy" (3 Sep 2021, 18633 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 "GroovyParserTest.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 org.apache.groovy.parser.antlr4
   20 
   21 import groovy.test.GroovyTestCase
   22 import org.apache.groovy.parser.antlr4.util.ASTComparatorCategory
   23 import org.codehaus.groovy.ast.ClassNode
   24 import org.codehaus.groovy.ast.FieldNode
   25 import org.codehaus.groovy.ast.GenericsType
   26 import org.codehaus.groovy.ast.MethodNode
   27 import org.codehaus.groovy.ast.Parameter
   28 import org.codehaus.groovy.ast.PropertyNode
   29 import org.codehaus.groovy.ast.stmt.AssertStatement
   30 import org.codehaus.groovy.ast.stmt.ExpressionStatement
   31 import org.codehaus.groovy.control.CompilerConfiguration
   32 import org.codehaus.groovy.syntax.Token
   33 
   34 import static org.apache.groovy.parser.antlr4.TestUtils.doRunAndTest
   35 import static org.apache.groovy.parser.antlr4.TestUtils.doRunAndTestAntlr4
   36 import static org.apache.groovy.parser.antlr4.TestUtils.doTest
   37 
   38 /**
   39  * Some basic test cases for the new parser
   40  */
   41 final class GroovyParserTest extends GroovyTestCase {
   42 
   43     void "test groovy core - Comments"() {
   44         doTest('core/Comments_01.groovy', [ExpressionStatement])
   45         doTestAttachedComments()
   46     }
   47 
   48     void "test groovy core - Shebang Comments"() {
   49         doRunAndTestAntlr4('core/Comments_03x.groovy')
   50     }
   51 
   52     // java.lang.ClassFormatError: Illegal method name "test IO stream/reader closed by the parser properly" when using Java9
   53     void "test IO reader closed by the parser properly"() {
   54         def f = File.createTempFile("Script${System.nanoTime()}", ".groovy")
   55         f.text = '''
   56             def a = 123
   57         '''
   58 
   59         TestUtils.createAntlr4Shell().evaluate(f)
   60 
   61         boolean deleted = f.delete()
   62         assert deleted: "Failed to delete file: ${f.getAbsolutePath()}"
   63     }
   64 
   65     // java.lang.ClassFormatError: Illegal method name "test IO stream/reader closed by the parser properly" when using Java9
   66     void "test reader closed by the compiler properly"() {
   67         def f = File.createTempFile("Script${System.nanoTime()}", ".groovy")
   68         f.text = '''
   69             def a = 123
   70         '''
   71 
   72         TestUtils.createAntlr4Shell().evaluate(f)
   73 
   74         boolean deleted = f.delete()
   75         assert deleted: "Failed to delete file: ${f.getAbsolutePath()}"
   76     }
   77 
   78     private static doTestAttachedComments() {
   79         CompilerConfiguration compilerConfiguration = new CompilerConfiguration(CompilerConfiguration.DEFAULT)
   80         compilerConfiguration.setOptimizationOptions([(CompilerConfiguration.GROOVYDOC): true])
   81         def (newAST, oldAST) = doTest('core/Comments_02.groovy', ASTComparatorCategory.DEFAULT_CONFIGURATION, compilerConfiguration)
   82         List<ClassNode> classes = new ArrayList<>(newAST.classes).sort { c1, c2 -> c1.name <=> c2.name }
   83         List<MethodNode> methods = new ArrayList<>(newAST.methods).sort { m1, m2 -> m1.name <=> m2.name }
   84 
   85         assert classes[0].groovydoc.content.replaceAll(/\r?\n/, '')            == '/** * test class Comments */'
   86         assert classes[0].fields[0].groovydoc.content.replaceAll(/\r?\n/, '')  == '/**     * test Comments.SOME_VAR     */'
   87         assert classes[0].fields[1].groovydoc.content.replaceAll(/\r?\n/, '')  == '/**     * test Comments.SOME_VAR2     */'
   88         assert !classes[0].fields[2].groovydoc.isPresent()
   89         assert !classes[0].fields[3].groovydoc.isPresent()
   90         assert classes[0].declaredConstructors[0].groovydoc.content.replaceAll(/\r?\n/, '') == '/**     * test Comments.constructor1     */'
   91         assert classes[0].methods[0].groovydoc.content.replaceAll(/\r?\n/, '') == '/**     * test Comments.m1     */'
   92         assert !classes[0].methods[1].groovydoc.isPresent()
   93         assert classes[0].methods[2].groovydoc.content.replaceAll(/\r?\n/, '') == '/**     * test Comments.m3     */'
   94 
   95         assert classes[1].groovydoc.content.replaceAll(/\r?\n/, '')            == '/**     * test class InnerClazz     */'
   96         assert classes[1].fields[0].groovydoc.content.replaceAll(/\r?\n/, '')  == '/**         * test InnerClazz.SOME_VAR3         */'
   97         assert classes[1].fields[1].groovydoc.content.replaceAll(/\r?\n/, '')  == '/**         * test InnerClazz.SOME_VAR4         */'
   98         assert classes[1].methods[0].groovydoc.content.replaceAll(/\r?\n/, '') == '/**         * test Comments.m4         */'
   99         assert classes[1].methods[1].groovydoc.content.replaceAll(/\r?\n/, '') == '/**         * test Comments.m5         */'
  100 
  101         assert classes[2].groovydoc.content.replaceAll(/\r?\n/, '')            == '/**     * test class InnerEnum     */'
  102         assert classes[2].fields[0].groovydoc.content.replaceAll(/\r?\n/, '')  == '/**         * InnerEnum.NEW         */'
  103         assert classes[2].fields[1].groovydoc.content.replaceAll(/\r?\n/, '')  == '/**         * InnerEnum.OLD         */'
  104 
  105         assert !classes[3].groovydoc.isPresent()
  106 
  107         assert !classes[4].fields[0].groovydoc.isPresent()
  108 
  109         assert !classes[5].groovydoc.isPresent()
  110 
  111         assert methods[0].groovydoc.content.replaceAll(/\r?\n/, '') == '/** * test someScriptMethod1 */'
  112         assert !methods[1].groovydoc.isPresent()
  113     }
  114 
  115     void "test groovy core - PackageDeclaration"() {
  116         doTest('core/PackageDeclaration_01.groovy')
  117         doTest('core/PackageDeclaration_02.groovy')
  118         doTest('core/PackageDeclaration_03.groovy')
  119         doTest('core/PackageDeclaration_04.groovy')
  120         doTest('core/PackageDeclaration_05.groovy')
  121         doTest('core/PackageDeclaration_06.groovy')
  122     }
  123 
  124     void "test groovy core - ImportDeclaration"() {
  125         doTest('core/ImportDeclaration_01.groovy')
  126         doTest('core/ImportDeclaration_02.groovy')
  127         doTest('core/ImportDeclaration_03.groovy')
  128         doTest('core/ImportDeclaration_04.groovy')
  129         doTest('core/ImportDeclaration_05.groovy')
  130         doTest('core/ImportDeclaration_06.groovy')
  131         doTest('core/ImportDeclaration_07.groovy')
  132         doTest('core/ImportDeclaration_08.groovy')
  133     }
  134 
  135     void "test groovy core - Annotation"() {
  136         doTest('core/Annotation_01.groovy')
  137         doTest('core/Annotation_02.groovy')
  138         doTest('core/Annotation_03.groovy')
  139         doTest('core/Annotation_04.groovy')
  140         doTest('core/Annotation_05.groovy')
  141         doTest('core/Annotation_06.groovy')
  142         doTest('core/Annotation_07.groovy')
  143         doTest('core/Annotation_08.groovy')
  144         doTest('core/Annotation_09.groovy')
  145         doRunAndTestAntlr4('core/Annotation_10x.groovy')
  146     }
  147 
  148     void "test groovy core - Literal"() {
  149         doTest('core/Literal_01.groovy')
  150         doTest('core/Literal_02.groovy', [ExpressionStatement])
  151         doTest('core/Literal_03.groovy')
  152     }
  153 
  154     void "test groovy core - GString"() {
  155         doTest('core/GString_01.groovy')
  156         doTest('core/GString_02.groovy')
  157         doTest('core/GString_03.groovy')
  158         doTest('core/GString_04.groovy')
  159         doTest('core/GString_05.groovy')
  160         doTest('core/GString_06.groovy')
  161     }
  162 
  163     void "test groovy core - Closure"() {
  164         doTest('core/Closure_01.groovy')
  165         doTest('core/Closure_02.groovy')
  166         doTest('core/Closure_03.groovy')
  167         doTest('core/Closure_04.groovy')
  168         doTest('core/Closure_05.groovy', [Parameter])
  169         doTest('core/Closure_06.groovy', [Parameter])
  170         doTest('core/Closure_07.groovy', [Parameter])
  171         doTest('core/Closure_08.groovy', [Parameter])
  172         doTest('core/Closure_09.groovy', [Parameter])
  173         doTest('core/Closure_10.groovy', [Parameter])
  174     }
  175 
  176     void "test groovy core - Lambda"() {
  177         doRunAndTestAntlr4('core/Lambda_01x.groovy')
  178     }
  179 
  180     void "test groovy core - MethodReference"() {
  181         doRunAndTestAntlr4('core/MethodReference_01x.groovy')
  182     }
  183 
  184     void "test groovy core - MethodPointer"() {
  185         doRunAndTestAntlr4('core/MethodPointer_01x.groovy')
  186     }
  187 
  188     void "test groovy core - ElvisAssignment"() {
  189         doRunAndTestAntlr4('core/ElvisAssignment_01x.groovy')
  190     }
  191 
  192     void "test groovy core - List"() {
  193         doTest('core/List_01.groovy')
  194     }
  195 
  196     void "test groovy core - Map"() {
  197         doTest('core/Map_01.groovy')
  198         doRunAndTestAntlr4('core/Map_02x.groovy')
  199     }
  200 
  201     void "test groovy core - Expression"() {
  202         doTest('core/Expression_01.groovy')
  203         doTest('core/Expression_02.groovy')
  204         doTest('core/Expression_03.groovy')
  205         doTest('core/Expression_04.groovy')
  206         doTest('core/Expression_05.groovy')
  207         doTest('core/Expression_06.groovy')
  208         doTest('core/Expression_07.groovy')
  209         doTest('core/Expression_08.groovy')
  210         doTest('core/Expression_09.groovy')
  211         doTest('core/Expression_10.groovy')
  212         doTest('core/Expression_11.groovy')
  213         doTest('core/Expression_12.groovy')
  214         doTest('core/Expression_13.groovy')
  215         doTest('core/Expression_14.groovy')
  216         doTest('core/Expression_15.groovy')
  217         doTest('core/Expression_16.groovy', [Parameter, ExpressionStatement])
  218         doTest('core/Expression_17.groovy')
  219         doTest('core/Expression_18.groovy')
  220         doTest('core/Expression_19.groovy')
  221         doTest('core/Expression_20.groovy')
  222         doRunAndTestAntlr4('core/Expression_21x.groovy')
  223         doTest('core/Expression_22x.groovy')
  224         doRunAndTestAntlr4('core/Expression_22x.groovy')
  225         doRunAndTestAntlr4('core/Expression_23x.groovy')
  226     }
  227 
  228     void "test groovy core - IdenticalOp"() {
  229         doRunAndTestAntlr4('core/IdenticalOp_01x.groovy')
  230     }
  231 
  232     void "test groovy core - Assert"() {
  233         doTest('core/Assert_01.groovy')
  234         doRunAndTestAntlr4('core/Assert_02x.groovy')
  235         doRunAndTestAntlr4('core/Assert_03x.groovy')
  236         doRunAndTestAntlr4('core/Assert_04x.groovy')
  237     }
  238 
  239     void "test groovy core - IfElse"() {
  240         doTest('core/IfElse_01.groovy', [AssertStatement])
  241     }
  242 
  243     void "test groovy core - For"() {
  244         doTest('core/For_01.groovy', [AssertStatement])
  245         doTest('core/For_02.groovy')
  246         doTest('core/For_03.groovy')
  247         doRunAndTestAntlr4('core/For_04x.groovy')
  248         doRunAndTestAntlr4('core/For_05x.groovy')
  249     }
  250 
  251     void "test groovy core - While"() {
  252         doTest('core/While_01.groovy')
  253         doRunAndTestAntlr4('core/While_02x.groovy')
  254     }
  255 
  256     void "test groovy core - CodeBlock"() {
  257         doRunAndTestAntlr4('core/CodeBlock_01x.groovy')
  258     }
  259 
  260     void "test groovy core - DoWhile"() {
  261         doRunAndTestAntlr4('core/DoWhile_01x.groovy')
  262         doRunAndTestAntlr4('core/DoWhile_02x.groovy')
  263         doRunAndTestAntlr4('core/DoWhile_03x.groovy')
  264         doRunAndTestAntlr4('core/DoWhile_04x.groovy')
  265     }
  266 
  267 
  268     void "test groovy core - TryCatch"() {
  269         doTest('core/TryCatch_01.groovy')
  270     }
  271 
  272     void "test groovy core - TryWithResources"() {
  273         doRunAndTestAntlr4('core/TryWithResources_01x.groovy')
  274         doRunAndTestAntlr4('core/TryWithResources_02x.groovy')
  275     }
  276 
  277     void "test groovy core - SafeIndex"() {
  278         doRunAndTestAntlr4('core/SafeIndex_01x.groovy')
  279         doRunAndTestAntlr4('core/SafeIndex_02x.groovy')
  280         doRunAndTestAntlr4('core/SafeIndex_03x.groovy')
  281         doRunAndTestAntlr4('core/SafeIndex_04x.groovy')
  282     }
  283 
  284     void "test groovy core - NegativeRelationalOperators"() {
  285         doRunAndTestAntlr4('core/NegativeRelationalOperators_01x.groovy')
  286         doRunAndTestAntlr4('core/NegativeRelationalOperators_02x.groovy')
  287     }
  288 
  289     void "test groovy core - DefaultMethod"() {
  290         doRunAndTestAntlr4('core/DefaultMethod_01x.groovy')
  291         doRunAndTestAntlr4('core/DefaultMethod_02x.groovy')
  292     }
  293 
  294 
  295     void "test groovy core - Switch"() {
  296         doTest('core/Switch_01.groovy')
  297     }
  298 
  299     void "test groovy core - Synchronized"() {
  300         doTest('core/Synchronized_01.groovy')
  301     }
  302 
  303     void "test groovy core - Break"() {
  304         doTest('core/Break_01.groovy')
  305     }
  306 
  307     void "test groovy core - Return"() {
  308         doTest('core/Return_01.groovy')
  309     }
  310 
  311     void "test groovy core - Throw"() {
  312         doTest('core/Throw_01.groovy')
  313     }
  314 
  315     void "test groovy core - Label"() {
  316         doTest('core/Label_01.groovy')
  317     }
  318 
  319     void "test groovy core - LocalVariableDeclaration"() {
  320         doTest('core/LocalVariableDeclaration_01.groovy', [Token]) // [class org.codehaus.groovy.syntax.Token][startLine]:: 9 != 8
  321         doRunAndTestAntlr4('core/LocalVariableDeclaration_02x.groovy')
  322     }
  323 
  324     void "test groovy core - MethodDeclaration"() {
  325         doTest('core/MethodDeclaration_01.groovy')
  326         doTest('core/MethodDeclaration_02.groovy')
  327     }
  328 
  329     void "test groovy core - ClassDeclaration"() {
  330         doTest('core/ClassDeclaration_01.groovy')
  331         doTest('core/ClassDeclaration_02.groovy')
  332         doTest('core/ClassDeclaration_03.groovy')
  333         doTest('core/ClassDeclaration_04.groovy', [PropertyNode, FieldNode])
  334         doTest('core/ClassDeclaration_05.groovy', [ExpressionStatement])
  335         doTest('core/ClassDeclaration_06.groovy')
  336         doTest('core/ClassDeclaration_07.groovy')
  337         doTest('core/ClassDeclaration_08.groovy')
  338     }
  339 
  340     void "test groovy core - InterfaceDeclaration"() {
  341         doTest('core/InterfaceDeclaration_01.groovy')
  342         doTest('core/InterfaceDeclaration_02.groovy', [PropertyNode, FieldNode])
  343         doTest('core/InterfaceDeclaration_03.groovy')
  344     }
  345 
  346     void "test groovy core - EnumDeclaration"() {
  347         doTest('core/EnumDeclaration_01.groovy', [PropertyNode, FieldNode])
  348         doTest('core/EnumDeclaration_02.groovy', [ExpressionStatement])
  349         doTest('core/EnumDeclaration_03.groovy')
  350         doTest('core/EnumDeclaration_04.groovy')
  351         doTest('core/EnumDeclaration_05.groovy')
  352     }
  353 
  354     void "test groovy core - TraitDeclaration"() {
  355         doTest('core/TraitDeclaration_01.groovy')
  356         doTest('core/TraitDeclaration_02.groovy')
  357         doTest('core/TraitDeclaration_03.groovy')
  358         doTest('core/TraitDeclaration_04.groovy', [PropertyNode, FieldNode])
  359         doTest('core/TraitDeclaration_05.groovy')
  360     }
  361 
  362     void "test groovy core - AnnotationDeclaration"() {
  363         doTest('core/AnnotationDeclaration_01.groovy')
  364     }
  365 
  366     void "test groovy core - Command"() {
  367         doTest('core/Command_01.groovy')
  368         doTest('core/Command_02.groovy')
  369         doTest('core/Command_03.groovy', [ExpressionStatement, Parameter])
  370         doTest('core/Command_04.groovy', [ExpressionStatement])
  371         doTest('core/Command_05.groovy')
  372         doRunAndTestAntlr4('core/Command_06x.groovy')
  373         doRunAndTestAntlr4('core/Command_07x.groovy')
  374     }
  375 
  376     /*
  377     void "test groovy core - Unicode"() {
  378         doTest('core/Unicode_01.groovy')
  379     }
  380     */
  381 
  382     void "test groovy core - BreakingChanges"() {
  383         doRunAndTestAntlr4('core/BreakingChange_01x.groovy')
  384         doRunAndTestAntlr4('core/BreakingChange_02x.groovy')
  385         doRunAndTestAntlr4('core/BreakingChange_03x.groovy')
  386         doRunAndTestAntlr4('core/BreakingChange_04x.groovy')
  387     }
  388 
  389     void "test groovy core - Array"() {
  390         doRunAndTestAntlr4('core/Array_01x.groovy')
  391     }
  392 
  393     void "test groovy core - Groovydoc"() {
  394         CompilerConfiguration compilerConfiguration = new CompilerConfiguration(CompilerConfiguration.DEFAULT)
  395         compilerConfiguration.setOptimizationOptions([(CompilerConfiguration.RUNTIME_GROOVYDOC): true])
  396 
  397         doRunAndTestAntlr4('core/Groovydoc_01x.groovy', compilerConfiguration)
  398     }
  399 
  400     void "test groovy core - Script"() {
  401         doRunAndTestAntlr4('core/Script_01x.groovy')
  402     }
  403 
  404     void "test groovy core - FieldDeclaration"() {
  405         doRunAndTestAntlr4('core/FieldDeclaration_01x.groovy')
  406     }
  407 
  408     void "test groovy core - Number"() {
  409         doRunAndTestAntlr4('core/Number_01x.groovy')
  410     }
  411 
  412     void "test groovy core - SafeChainOperator"() {
  413         doRunAndTestAntlr4('core/SafeChainOperator.groovy')
  414     }
  415 
  416     void "test groovy core - var"() {
  417         doRunAndTestAntlr4('core/Var_01x.groovy');
  418     }
  419 
  420     void "test groovy core - String"() {
  421         doRunAndTestAntlr4('core/String_01x.groovy');
  422     }
  423 
  424     void "test groovy core - NonStaticClass"() {
  425         doRunAndTestAntlr4('core/NonStaticClass_01x.groovy');
  426     }
  427 
  428     void "test groovy core - BUG"() {
  429         doRunAndTestAntlr4('bugs/BUG-GROOVY-4757.groovy')
  430         doRunAndTestAntlr4('bugs/BUG-GROOVY-5652.groovy')
  431         doRunAndTestAntlr4('bugs/BUG-GROOVY-4762.groovy')
  432         doRunAndTestAntlr4('bugs/BUG-GROOVY-4438.groovy')
  433         doRunAndTestAntlr4('bugs/BUG-GROOVY-6038.groovy')
  434         doRunAndTestAntlr4('bugs/BUG-GROOVY-2324.groovy')
  435         doTest('bugs/BUG-GROOVY-8161.groovy')
  436         doRunAndTestAntlr4('bugs/GROOVY-3898.groovy')
  437         doRunAndTestAntlr4('bugs/GROOVY-8228.groovy')
  438         doRunAndTestAntlr4('bugs/BUG-GROOVY-8311.groovy')
  439         doRunAndTest('bugs/BUG-GROOVY-8426.groovy')
  440         doTest('bugs/BUG-GROOVY-8511.groovy')
  441         doRunAndTestAntlr4('bugs/BUG-GROOVY-8613.groovy')
  442         doTest('bugs/BUG-GROOVY-8641.groovy')
  443         doTest('bugs/BUG-GROOVY-8913.groovy')
  444         doRunAndTestAntlr4('bugs/BUG-GROOVY-8991.groovy')
  445         doTest('bugs/BUG-GROOVY-9399.groovy')
  446         doTest('bugs/BUG-GROOVY-9859.groovy', [GenericsType])
  447     }
  448 
  449     void "test groovy core - GROOVY-9427"() {
  450         doTest('bugs/BUG-GROOVY-9427.groovy');
  451     }
  452 
  453     void "test groovy core - GROOVY-9433"() {
  454         doTest('bugs/BUG-GROOVY-9433.groovy');
  455     }
  456 
  457     void "test groovy core - GROOVY-9449"() {
  458         doTest('bugs/BUG-GROOVY-9449.groovy');
  459     }
  460 
  461     void "test groovy core - GROOVY-9511"() {
  462         doTest('bugs/BUG-GROOVY-9511.groovy', [MethodNode]);
  463     }
  464 
  465     void "test groovy core - GROOVY-9507"() {
  466         doTest('bugs/BUG-GROOVY-9507.groovy');
  467     }
  468 
  469     void "test groovy core - GROOVY-9522"() {
  470         doTest('bugs/BUG-GROOVY-9522.groovy');
  471     }
  472 
  473     void "test groovy core - GROOVY-9692"() {
  474         doTest('bugs/BUG-GROOVY-9692.groovy');
  475     }
  476 
  477     void "test groovy core - GROOVY-10181"() {
  478         doTest('bugs/BUG-GROOVY-10181.groovy');
  479     }
  480 }