"Fossies" - the Fresh Open Source Software Archive

Member "groovy-3.0.9/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy" (3 Sep 2021, 17713 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 "SyntaxErrorTest.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 groovy.test.NotYetImplemented
   23 import groovy.transform.AutoFinal
   24 import org.codehaus.groovy.control.CompilationUnit
   25 import org.codehaus.groovy.control.Phases
   26 
   27 import static org.apache.groovy.parser.antlr4.util.ASTComparatorCategory.LOCATION_IGNORE_LIST
   28 
   29 /**
   30  * Some syntax error test cases for the new parser.
   31  */
   32 @AutoFinal
   33 final class SyntaxErrorTest extends GroovyTestCase {
   34 
   35     void 'test groovy core - List'() {
   36         TestUtils.shouldFail('fail/List_01.groovy')
   37     }
   38 
   39     void 'test groovy core - Expression'() {
   40         TestUtils.shouldFail('fail/Expression_01.groovy')
   41         TestUtils.shouldFail('fail/Expression_02.groovy')
   42         TestUtils.shouldFail('fail/Expression_03.groovy')
   43       //TestUtils.shouldFail('fail/Expression_04.groovy')
   44       //TestUtils.shouldFail('fail/Expression_05.groovy')
   45         TestUtils.shouldFail('fail/Expression_06.groovy')
   46         TestUtils.shouldFail('fail/Expression_07.groovy')
   47         TestUtils.shouldFail('fail/Expression_08.groovy')
   48         TestUtils.shouldFail('fail/Expression_09.groovy')
   49     }
   50 
   51     void 'test groovy core - CommandExpression'() {
   52         TestUtils.doRunAndShouldFail('fail/CommandExpression_01x.groovy')
   53     }
   54 
   55     void 'test groovy core - Switch'() {
   56         TestUtils.shouldFail('fail/Switch_01.groovy')
   57     }
   58 
   59     @NotYetImplemented
   60     void 'test groovy core - LocalVariableDeclaration'() {
   61         TestUtils.shouldFail('fail/LocalVariableDeclaration_01.groovy')
   62     }
   63 
   64     void 'test groovy core - Continue'() {
   65         TestUtils.doRunAndShouldFail('fail/Continue_01x.groovy')
   66         TestUtils.doRunAndShouldFail('fail/Continue_02x.groovy')
   67     }
   68 
   69     void 'test groovy core - Break'() {
   70         TestUtils.doRunAndShouldFail('fail/Break_01x.groovy')
   71         TestUtils.doRunAndShouldFail('fail/Break_02x.groovy')
   72     }
   73 
   74     void 'test groovy core - UnexpectedCharacter 1'() {
   75         TestUtils.doRunAndShouldFail('fail/UnexpectedCharacter_01x.groovy')
   76     }
   77 
   78     void 'test groovy core - UnexpectedCharacter 2'() {
   79         def err = expectParseError '''\
   80             |def \u200Bname = null
   81             |'''.stripMargin()
   82 
   83         // TODO: Could the character be escaped in the error message?
   84         assert err == '''\
   85             |startup failed:
   86             |test.groovy: 1: Unexpected character: '\u200B' @ line 1, column 5.
   87             |   def \u200Bname = null
   88             |       ^
   89             |
   90             |1 error
   91             |'''.stripMargin()
   92 
   93         //
   94 
   95         err = expectParseError '''\
   96             |def na\u200Bme = null
   97             |'''.stripMargin()
   98 
   99         // TODO: Could the character be escaped in the error message?
  100         assert err == '''\
  101             |startup failed:
  102             |test.groovy: 1: Unexpected character: '\u200B' @ line 1, column 7.
  103             |   def na\u200Bme = null
  104             |         ^
  105             |
  106             |1 error
  107             |'''.stripMargin()
  108 
  109         //
  110 
  111         err = expectParseError '''\
  112             |def na\u000Cme = null
  113             |'''.stripMargin()
  114 
  115         // TODO: Could the character be escaped in the error message?
  116         assert err == '''\
  117             |startup failed:
  118             |test.groovy: 1: Unexpected character: '\u000C' @ line 1, column 7.
  119             |   def na\u000Cme = null
  120             |         ^
  121             |
  122             |1 error
  123             |'''.stripMargin()
  124     }
  125 
  126     void 'test groovy core - UnexpectedCharacter 3'() {
  127         def err = expectParseError '''\
  128             |foo.bar {
  129             |  println 'Hello
  130             |}
  131             |'''.stripMargin()
  132 
  133         assert err == '''\
  134             |startup failed:
  135             |test.groovy: 2: Unexpected character: '\\'' @ line 2, column 11.
  136             |     println 'Hello
  137             |             ^
  138             |
  139             |1 error
  140             |'''.stripMargin()
  141     }
  142 
  143     void 'test groovy core - ParExpression'() {
  144         TestUtils.doRunAndShouldFail('fail/ParExpression_01x.groovy')
  145         TestUtils.doRunAndShouldFail('fail/ParExpression_02x.groovy')
  146         TestUtils.doRunAndShouldFail('fail/ParExpression_03x.groovy')
  147     }
  148 
  149     void 'test groovy core - Parentheses'() {
  150         TestUtils.shouldFail('fail/Parentheses_01.groovy')
  151     }
  152 
  153     void 'test groovy core - This'() {
  154         TestUtils.doRunAndShouldFail('fail/This_01x.groovy')
  155     }
  156 
  157     void 'test groovy core - Super'() {
  158         TestUtils.doRunAndShouldFail('fail/Super_01x.groovy')
  159     }
  160 
  161     void 'test groovy core - AbstractMethod'() {
  162         TestUtils.doRunAndShouldFail('fail/AbstractMethod_01x.groovy')
  163         TestUtils.doRunAndShouldFail('fail/AbstractMethod_02x.groovy')
  164         TestUtils.doRunAndShouldFail('fail/AbstractMethod_03x.groovy')
  165         TestUtils.doRunAndShouldFail('fail/AbstractMethod_04x.groovy')
  166         TestUtils.doRunAndShouldFail('fail/AbstractMethod_05x.groovy')
  167         TestUtils.doRunAndShouldFail('fail/AbstractMethod_06x.groovy')
  168     }
  169 
  170     void 'test groovy core - BUGs'() {
  171         TestUtils.doRunAndShouldFail('bugs/BUG-GROOVY-5318.groovy')
  172         TestUtils.doRunAndShouldFail('bugs/BUG-GROOVY-8150.groovy')
  173         TestUtils.doRunAndShouldFail('bugs/BUG-GROOVY-8216.groovy')
  174     }
  175 
  176     void 'test groovy core - DoWhile'() {
  177         TestUtils.doRunAndShouldFail('fail/DoWhile_01x.groovy')
  178     }
  179 
  180     void 'test groovy core - For'() {
  181         TestUtils.shouldFail('fail/For_01.groovy')
  182         TestUtils.shouldFail('fail/For_02.groovy')
  183     }
  184 
  185     void 'test groovy core - Modifier'() {
  186         TestUtils.doRunAndShouldFail('fail/Modifier_01x.groovy')
  187         TestUtils.doRunAndShouldFail('fail/Modifier_02x.groovy')
  188         TestUtils.doRunAndShouldFail('fail/Modifier_03x.groovy')
  189         TestUtils.doRunAndShouldFail('fail/Modifier_04x.groovy')
  190         TestUtils.doRunAndShouldFail('fail/Modifier_05x.groovy')
  191         TestUtils.shouldFail('fail/Modifier_07.groovy')
  192     }
  193 
  194     void 'test groovy core - ClassDeclaration 1'() {
  195         TestUtils.doRunAndShouldFail('fail/ClassDeclaration_02x.groovy')
  196     }
  197 
  198     void 'test groovy core - ClassDeclaration 2'() {
  199         def err = expectParseError '''\
  200             |class C extends Object, Number {}
  201             |'''.stripMargin()
  202 
  203         assert err == '''\
  204             |startup failed:
  205             |test.groovy: 1: Cannot extend multiple classes @ line 1, column 9.
  206             |   class C extends Object, Number {}
  207             |           ^
  208             |
  209             |1 error
  210             |'''.stripMargin()
  211     }
  212 
  213     void 'test groovy core - EnumDeclaration 1'() {
  214         def err = expectParseError '''\
  215             |enum E<T> {}
  216             |'''.stripMargin()
  217 
  218         assert err == '''\
  219             |startup failed:
  220             |test.groovy: 1: enum declaration cannot have type parameters @ line 1, column 7.
  221             |   enum E<T> {}
  222             |         ^
  223             |
  224             |1 error
  225             |'''.stripMargin()
  226     }
  227 
  228     void 'test groovy core - EnumDeclaration 2'() {
  229         def err = expectParseError '''\
  230             |enum E extends Object {}
  231             |'''.stripMargin()
  232 
  233         assert err == '''\
  234             |startup failed:
  235             |test.groovy: 1: No extends clause allowed for enum declaration @ line 1, column 8.
  236             |   enum E extends Object {}
  237             |          ^
  238             |
  239             |1 error
  240             |'''.stripMargin()
  241     }
  242 
  243     void 'test groovy core - AnnotationDeclaration 1'() {
  244         TestUtils.doRunAndShouldFail('fail/AnnotationDeclaration_01x.groovy');
  245     }
  246 
  247     void 'test groovy core - AnnotationDeclaration 2'() {
  248         def err = expectParseError '''\
  249             |@interface A<T> {}
  250             |'''.stripMargin()
  251 
  252         assert err == '''\
  253             |startup failed:
  254             |test.groovy: 1: annotation declaration cannot have type parameters @ line 1, column 13.
  255             |   @interface A<T> {}
  256             |               ^
  257             |
  258             |1 error
  259             |'''.stripMargin()
  260     }
  261 
  262     void 'test groovy core - AnnotationDeclaration 3'() {
  263         def err = expectParseError '''\
  264             |@interface A extends Object {}
  265             |'''.stripMargin()
  266 
  267         assert err == '''\
  268             |startup failed:
  269             |test.groovy: 1: No extends clause allowed for annotation declaration @ line 1, column 14.
  270             |   @interface A extends Object {}
  271             |                ^
  272             |
  273             |1 error
  274             |'''.stripMargin()
  275     }
  276 
  277     void 'test groovy core - AnnotationDeclaration 4'() {
  278         def err = expectParseError '''\
  279             |@interface A implements Serializable {}
  280             |'''.stripMargin()
  281 
  282         assert err == '''\
  283             |startup failed:
  284             |test.groovy: 1: No implements clause allowed for annotation declaration @ line 1, column 14.
  285             |   @interface A implements Serializable {}
  286             |                ^
  287             |
  288             |1 error
  289             |'''.stripMargin()
  290     }
  291 
  292     void 'test groovy core - AnnotationDeclaration 5'() {
  293         def err = expectParseError '''\
  294             |@interface A {
  295             |    String a() {
  296             |    }
  297             |}'''.stripMargin()
  298         println err
  299         assert err == '''\
  300             |startup failed:
  301             |test.groovy: 2: Annotation type element should not have body @ line 2, column 5.
  302             |       String a() {
  303             |       ^
  304             |
  305             |1 error
  306             |'''.stripMargin()
  307     }
  308 
  309     void 'test groovy core - MethodDeclaration'() {
  310         TestUtils.shouldFail('fail/MethodDeclaration_01.groovy')
  311         TestUtils.doRunAndShouldFail('fail/MethodDeclaration_02x.groovy')
  312         TestUtils.doRunAndShouldFail('fail/MethodDeclaration_03x.groovy')
  313         TestUtils.doRunAndShouldFail('fail/MethodDeclaration_04x.groovy')
  314         TestUtils.doRunAndShouldFail('fail/MethodDeclaration_05x.groovy')
  315     }
  316 
  317     void 'test groovy core - ConstructorDeclaration'() {
  318         TestUtils.shouldFail('fail/ConstructorDeclaration_01.groovy')
  319     }
  320 
  321     void 'test groovy core - ClosureListExpression'() {
  322         TestUtils.shouldFail('fail/ClosureListExpression_01.groovy')
  323         TestUtils.shouldFail('fail/ClosureListExpression_02.groovy')
  324         TestUtils.shouldFail('fail/ClosureListExpression_03.groovy')
  325         TestUtils.shouldFail('fail/ClosureListExpression_04.groovy')
  326     }
  327 
  328     void 'test groovy core - InterfaceDeclaration 1'() {
  329         TestUtils.shouldFail('fail/InterfaceDeclaration_01.groovy')
  330     }
  331 
  332     void 'test groovy core - InterfaceDeclaration 2'() {
  333         def err = expectParseError '''\
  334             |interface I implements Serializable {}
  335             |'''.stripMargin()
  336 
  337         assert err == '''\
  338             |startup failed:
  339             |test.groovy: 1: No implements clause allowed for interface declaration @ line 1, column 13.
  340             |   interface I implements Serializable {}
  341             |               ^
  342             |
  343             |1 error
  344             |'''.stripMargin()
  345     }
  346 
  347     void 'test groovy core - void'() {
  348         TestUtils.doRunAndShouldFail('fail/Void_01x.groovy')
  349         TestUtils.doRunAndShouldFail('fail/Void_02x.groovy')
  350     }
  351 
  352     void 'test groovy core - FieldDeclaration'() {
  353         TestUtils.doRunAndShouldFail('fail/FieldDeclaration_01x.groovy')
  354         TestUtils.doRunAndShouldFail('fail/FieldDeclaration_02x.groovy')
  355         TestUtils.doRunAndShouldFail('fail/FieldDeclaration_03x.groovy')
  356         TestUtils.doRunAndShouldFail('fail/FieldDeclaration_04x.groovy')
  357     }
  358 
  359     void 'test groovy core - Assert'() {
  360         TestUtils.doRunAndShouldFail('fail/Assert_01x.groovy')
  361     }
  362 
  363     void 'test groovy core - DuplicatedNamedParameter'() {
  364         TestUtils.doRunAndShouldFail('fail/DuplicatedNamedParameter_01x.groovy')
  365         TestUtils.doRunAndShouldFail('fail/DuplicatedNamedParameter_02x.groovy')
  366     }
  367 
  368     void 'test groovy core - threadsafe'() {
  369         TestUtils.doRunAndShouldFail('fail/ThreadSafe_01x.groovy')
  370     }
  371 
  372     void 'test groovy core - VarArgParameter'() {
  373         TestUtils.doRunAndShouldFail('fail/VarArgParameter_01x.groovy')
  374     }
  375 
  376     void 'test groovy core - Number'() {
  377         TestUtils.doRunAndShouldFail('fail/Number_01x.groovy')
  378         TestUtils.doRunAndShouldFail('fail/Number_02x.groovy')
  379         TestUtils.doRunAndShouldFail('fail/Number_03x.groovy')
  380         TestUtils.doRunAndShouldFail('fail/Number_04x.groovy')
  381         TestUtils.doRunAndShouldFail('fail/Number_05x.groovy')
  382     }
  383 
  384     void 'test groovy core - MethodCall'() {
  385         TestUtils.doRunAndShouldFail('fail/MethodCall_01x.groovy')
  386     }
  387 
  388     void 'test groovy core - var'() {
  389         TestUtils.doRunAndShouldFail('fail/Var_01x.groovy')
  390         TestUtils.doRunAndShouldFail('fail/Var_02x.groovy')
  391     }
  392 
  393     void 'test groovy core - String'() {
  394         TestUtils.doRunAndShouldFail('fail/String_01x.groovy')
  395         TestUtils.doRunAndShouldFail('fail/String_02x.groovy')
  396         TestUtils.shouldFail('fail/String_03.groovy')
  397         TestUtils.shouldFail('fail/String_04.groovy')
  398     }
  399 
  400     void 'test groovy core - NonStaticClass'() {
  401         TestUtils.doRunAndShouldFail('fail/NonStaticClass_01x.groovy')
  402     }
  403 
  404     void 'test groovy core - Import'() {
  405         TestUtils.doRunAndShouldFail('fail/Import_01x.groovy')
  406         TestUtils.doRunAndShouldFail('fail/Import_02x.groovy')
  407     }
  408 
  409     void 'test groovy core - UnaryOperator'() {
  410         TestUtils.doRunAndShouldFail('fail/UnaryOperator_01x.groovy')
  411         TestUtils.doRunAndShouldFail('fail/UnaryOperator_02x.groovy')
  412     }
  413 
  414     void 'test groovy core - Trait'() {
  415         TestUtils.shouldFail('fail/Trait_01.groovy')
  416     }
  417 
  418     void 'test groovy core - Array'() {
  419         TestUtils.doRunAndShouldFail('fail/Array_01x.groovy')
  420         TestUtils.doRunAndShouldFail('fail/Array_02x.groovy')
  421     }
  422 
  423     @NotYetImplemented
  424     void 'test error alternative - Missing ")" 1'() {
  425         def err = expectParseError '''\
  426             |println ((int 123)
  427             |'''.stripMargin()
  428 
  429         assert err == '''\
  430             |startup failed:
  431             |test.groovy: 1: Missing ')' @ line 1, column 15.
  432             |   println ((int 123)
  433             |                 ^
  434             |
  435             |1 error
  436             |'''.stripMargin()
  437     }
  438 
  439     @NotYetImplemented
  440     void 'test error alternative - Missing ")" 2'() {
  441         def err = expectParseError '''\
  442             |def x() {
  443             |    println((int) 123
  444             |}
  445             |'''.stripMargin()
  446 
  447         assert err == '''\
  448             |startup failed:
  449             |test.groovy: 2: Missing ')' @ line 2, column 22.
  450             |       println((int) 123
  451             |                        ^
  452             |
  453             |1 error
  454             |'''.stripMargin()
  455     }
  456 
  457     @NotYetImplemented
  458     void 'test error alternative - Missing ")" 3'() {
  459         def err = expectParseError '''\
  460             |def m( {
  461             |}
  462             |'''.stripMargin()
  463 
  464         assert err == '''\
  465             |startup failed:
  466             |test.groovy: 1: Missing ')' @ line 1, column 8.
  467             |   def m( {
  468             |          ^
  469             |
  470             |1 error
  471             |'''.stripMargin()
  472     }
  473 
  474     @NotYetImplemented
  475     void 'test CompilerErrorTest_001'() {
  476         unzipScriptAndShouldFail('scripts/CompilerErrorTest_001.groovy', [])
  477     }
  478 
  479     @NotYetImplemented
  480     void 'test CompilerErrorTest_002'() {
  481         unzipScriptAndShouldFail('scripts/CompilerErrorTest_002.groovy', [])
  482     }
  483 
  484     @NotYetImplemented
  485     void 'test DifferencesFromJavaTest_002'() {
  486         unzipScriptAndShouldFail('scripts/DifferencesFromJavaTest_002.groovy', [])
  487     }
  488 
  489     @NotYetImplemented
  490     void 'test Groovy5212Bug_001'() {
  491         unzipScriptAndShouldFail('scripts/Groovy5212Bug_001.groovy', [])
  492     }
  493 
  494     @NotYetImplemented
  495     void 'test GStringEndTest_001'() {
  496         unzipScriptAndShouldFail('scripts/GStringEndTest_001.groovy', [])
  497     }
  498 
  499     //--------------------------------------------------------------------------
  500 
  501     private static String expectParseError(String source) {
  502         try {
  503             new CompilationUnit().with {
  504                 addSource('test.groovy', source)
  505                 compile(Phases.CONVERSION)
  506                 getAST()
  507             }
  508 
  509             fail('expected parse to fail')
  510         } catch (e) {
  511             return e.message.replace('\r\n', '\n')
  512         }
  513     }
  514 
  515     private static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String, String> replacementsMap = [:], boolean toCheckNewParserOnly = false) {
  516         ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST)
  517 
  518         TestUtils.unzipAndFail(SCRIPT_ZIP_PATH, entryName, TestUtils.addIgnore(ignoreClazzList, LOCATION_IGNORE_LIST), replacementsMap, toCheckNewParserOnly)
  519     }
  520 
  521     private static final String SCRIPT_ZIP_PATH = "${TestUtils.RESOURCES_PATH}/groovy-2.5.0/groovy-2.5.0-SNAPSHOT-20160921-allscripts.zip"
  522 }