Repository: groovy Updated Branches: refs/heads/master 014273f2f -> f0dcf82fd
Refine the checking of EmptyStatement and EmptyExpression Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f0dcf82f Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f0dcf82f Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f0dcf82f Branch: refs/heads/master Commit: f0dcf82fdba81448dd81a06c2fc165e8fd8e241d Parents: 014273f Author: sunlan <[email protected]> Authored: Sat Jun 24 17:21:47 2017 +0800 Committer: sunlan <[email protected]> Committed: Sat Jun 24 17:21:47 2017 +0800 ---------------------------------------------------------------------- .../groovy/ast/stmt/EmptyStatement.java | 18 +++++++------- .../groovy/ast/CodeVisitorSupportTest.groovy | 4 ++-- .../groovy/ast/builder/AstAssert.groovy | 25 ++++++++++++++++---- 3 files changed, 32 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/f0dcf82f/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java b/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java index 740cb55..3f6aa93 100644 --- a/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java +++ b/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java @@ -30,8 +30,16 @@ import java.util.Map; * @author <a href="mailto:[email protected]">James Strachan</a> */ public class EmptyStatement extends Statement { + public static final EmptyStatement INSTANCE = new ImmutableStatement(); + + public void visit(GroovyCodeVisitor visitor) { + } + + public boolean isEmpty() { + return true; + } - public static final EmptyStatement INSTANCE = new EmptyStatement() { + public static class ImmutableStatement extends EmptyStatement { @Override public void setStatementLabel(String label) { throw createUnsupportedOperationException(); @@ -95,13 +103,5 @@ public class EmptyStatement extends Statement { private UnsupportedOperationException createUnsupportedOperationException() { return new UnsupportedOperationException("EmptyStatement.INSTANCE is immutable"); } - }; - - public void visit(GroovyCodeVisitor visitor) { } - - public boolean isEmpty() { - return true; - } - } http://git-wip-us.apache.org/repos/asf/groovy/blob/f0dcf82f/src/test/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy b/src/test/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy index a1b351f..8f78aff 100644 --- a/src/test/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy +++ b/src/test/org/codehaus/groovy/ast/CodeVisitorSupportTest.groovy @@ -57,7 +57,7 @@ public class CodeVisitorSupportTest extends GroovyTestCase { assert visitor.history[1] == IfStatement assert visitor.history[2] == BooleanExpression assert visitor.history[3] == BlockStatement - assert visitor.history[4] == EmptyStatement + assert visitor.history[4] instanceof EmptyStatement assert visitor.history.size == 5 } @@ -99,7 +99,7 @@ public class CodeVisitorSupportTest extends GroovyTestCase { assert visitor.history[2] == BlockStatement assert visitor.history[3] == CatchStatement assert visitor.history[4] == BlockStatement - assert visitor.history[5] == EmptyStatement + assert visitor.history[5] instanceof EmptyStatement } } http://git-wip-us.apache.org/repos/asf/groovy/blob/f0dcf82f/src/test/org/codehaus/groovy/ast/builder/AstAssert.groovy ---------------------------------------------------------------------- diff --git a/src/test/org/codehaus/groovy/ast/builder/AstAssert.groovy b/src/test/org/codehaus/groovy/ast/builder/AstAssert.groovy index 7d6a589..af9fb27 100644 --- a/src/test/org/codehaus/groovy/ast/builder/AstAssert.groovy +++ b/src/test/org/codehaus/groovy/ast/builder/AstAssert.groovy @@ -20,6 +20,8 @@ package org.codehaus.groovy.ast.builder import org.codehaus.groovy.ast.ASTNode import org.codehaus.groovy.ast.ClassNode +import org.codehaus.groovy.ast.expr.EmptyExpression +import org.codehaus.groovy.ast.stmt.EmptyStatement import org.junit.Assert import org.codehaus.groovy.ast.stmt.BlockStatement import org.codehaus.groovy.ast.stmt.ExpressionStatement @@ -221,9 +223,14 @@ class AstAssert { assertSyntaxTree([expected.expression], [actual.expression]) assertSyntaxTree([expected.code], [actual.code]) }, + EmptyStatement : { expected, actual -> // always successful }, + 'EmptyStatement.ImmutableStatement' : { expected, actual -> + // always successful + }, + BreakStatement : { expected, actual -> Assert.assertEquals("Wrong label", expected.label, actual.label) }, @@ -374,15 +381,25 @@ class AstAssert { if (actual == null || expected == null || expected.size() != actual?.size()) { Assert.fail("AST comparison failure. \nExpected $expected \nReceived $actual") } + expected.eachWithIndex { item, index -> - if (item.getClass().isArray() && actual[index].getClass().isArray()) { - assertSyntaxTree(item, actual[index]) + def actualNode = actual[index] + + if (item.getClass().isArray() && actualNode.getClass().isArray()) { + assertSyntaxTree(item, actualNode) } else { - Assert.assertEquals("Wrong type in AST Node", item.getClass(), actual[index].getClass()) + try { + Assert.assertEquals("Wrong type in AST Node", item.getClass(), actualNode.getClass()) + } catch (AssertionError e) { + if (!(item instanceof EmptyStatement && actualNode instanceof EmptyStatement) + || !(item instanceof EmptyExpression && actualNode instanceof EmptyExpression)) { + throw e; + } + } if (ASSERTION_MAP.containsKey(item.getClass().getSimpleName())) { Closure assertion = ASSERTION_MAP.get(item.getClass().getSimpleName()) - assertion(item, actual[index]) + assertion(item, actualNode) } else { Assert.fail("Unexpected type: ${item.getClass()} Update the unit test!") }
