groovy git commit: Refine the assert statement

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: Refine the assert statement

Daniel.Sun
Repository: groovy
Updated Branches:
  refs/heads/master a72952526 -> a17b9eb85


Refine the assert statement


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/a17b9eb8
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/a17b9eb8
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/a17b9eb8

Branch: refs/heads/master
Commit: a17b9eb85835a5eaa306a893a0a8bcd51a49e299
Parents: a729525
Author: sunlan <[hidden email]>
Authored: Mon Jul 24 22:38:46 2017 +0800
Committer: sunlan <[hidden email]>
Committed: Mon Jul 24 22:38:46 2017 +0800

----------------------------------------------------------------------
 src/main/antlr/GroovyParser.g4                  |  2 +-
 .../apache/groovy/parser/antlr4/AstBuilder.java |  9 ++++++++
 .../parser/antlr4/GroovyParserTest.groovy       |  1 +
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  4 ++++
 .../src/test/resources/core/Assert_04x.groovy   | 23 ++++++++++++++++++++
 .../src/test/resources/fail/Assert_01x.groovy   | 20 +++++++++++++++++
 6 files changed, 58 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a17b9eb8/src/main/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/main/antlr/GroovyParser.g4 b/src/main/antlr/GroovyParser.g4
index c612df1..eca4205 100644
--- a/src/main/antlr/GroovyParser.g4
+++ b/src/main/antlr/GroovyParser.g4
@@ -675,7 +675,7 @@ locals[boolean resourcesExists = false]
 
 assertStatement
 locals[ String footprint = "" ]
-    :   ASSERT ce=expression ((COLON | COMMA) nls me=expression)?
+    :   ASSERT ce=expression (nls (COLON | COMMA) nls me=expression)?
     ;
 
 statement

http://git-wip-us.apache.org/repos/asf/groovy/blob/a17b9eb8/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 2454fc4..fd6609b 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -338,6 +338,15 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     @Override
     public AssertStatement visitAssertStatement(AssertStatementContext ctx) {
         Expression conditionExpression = (Expression) this.visit(ctx.ce);
+
+        if (conditionExpression instanceof BinaryExpression) {
+            BinaryExpression binaryExpression = (BinaryExpression) conditionExpression;
+
+            if (binaryExpression.getOperation().getType() == Types.ASSIGN) {
+                throw createParsingFailedException("Assignment expression is not allowed in the assert statement", conditionExpression);
+            }
+        }
+
         BooleanExpression booleanExpression =
                 this.configureAST(
                         new BooleanExpression(conditionExpression), conditionExpression);

http://git-wip-us.apache.org/repos/asf/groovy/blob/a17b9eb8/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index a87a5c0..350d54d 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -202,6 +202,7 @@ class GroovyParserTest extends GroovyTestCase {
         doTest('core/Assert_01.groovy');
         doRunAndTest('core/Assert_02x.groovy');
         doRunAndTest('core/Assert_03x.groovy');
+        doRunAndTest('core/Assert_04x.groovy');
     }
 
     void "test groovy core - IfElse"() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/a17b9eb8/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index 7d4ffa1..cbf2b17 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -180,6 +180,10 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/FieldDeclaration_04x.groovy');
     }
 
+    void "test groovy core - Assert"() {
+        TestUtils.doRunAndShouldFail('fail/Assert_01x.groovy');
+    }
+
     /**************************************/
     static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String, String> replacementsMap=[:], boolean toCheckNewParserOnly = false) {
         ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST)

http://git-wip-us.apache.org/repos/asf/groovy/blob/a17b9eb8/subprojects/parser-antlr4/src/test/resources/core/Assert_04x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/core/Assert_04x.groovy b/subprojects/parser-antlr4/src/test/resources/core/Assert_04x.groovy
new file mode 100644
index 0000000..2332b6e
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/core/Assert_04x.groovy
@@ -0,0 +1,23 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+assert 1 == 1
+        : 'hello'
+
+assert 1 == 1
+        , 'world'

http://git-wip-us.apache.org/repos/asf/groovy/blob/a17b9eb8/subprojects/parser-antlr4/src/test/resources/fail/Assert_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/Assert_01x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/Assert_01x.groovy
new file mode 100644
index 0000000..ab85018
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Assert_01x.groovy
@@ -0,0 +1,20 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+def a = 1
+assert a = 2

Apache Groovy committer & PMC member

Blog: http://blog.sunlan.me
Twitter: @daniel_sun