You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by cc...@apache.org on 2015/10/07 21:26:41 UTC

[21/37] incubator-groovy git commit: ASTMatcher: Test cases for not/list/map expressions

ASTMatcher: Test cases for not/list/map expressions


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

Branch: refs/heads/master
Commit: 02e53fcfe7f9cc625489ace128761eef4e828392
Parents: 3eba53c
Author: Cedric Champeau <ce...@gmail.com>
Authored: Fri Oct 17 10:38:16 2014 +0200
Committer: Sergei Egorov <bs...@gmail.com>
Committed: Mon Sep 28 14:33:10 2015 +0300

----------------------------------------------------------------------
 .../groovy/macro/matcher/ASTMatcher.groovy      | 58 +++++++++++--
 .../groovy/macro/matcher/ASTMatcherTest.groovy  | 86 ++++++++++++++++++++
 2 files changed, 138 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/02e53fcf/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
index 1bfc366..ffcf09a 100644
--- a/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
+++ b/subprojects/groovy-macro/src/main/groovy/org/codehaus/groovy/macro/matcher/ASTMatcher.groovy
@@ -512,7 +512,13 @@ class ASTMatcher extends ClassCodeVisitorSupport {
 
     @Override
     public void visitNotExpression(final NotExpression expression) {
-        super.visitNotExpression(expression);
+        doWithNode(NotExpression, current) {
+            def expr = expression.expression
+            def cur = ((NotExpression)current).expression
+            doWithNode(expr.class, cur) {
+                expr.visit(this)
+            }
+        }
     }
 
     @Override
@@ -561,7 +567,12 @@ class ASTMatcher extends ClassCodeVisitorSupport {
 
     @Override
     public void visitListExpression(final ListExpression expression) {
-        super.visitListExpression(expression);
+        doWithNode(ListExpression, current) {
+            def exprs = expression.expressions
+            doWithNode(exprs.class, ((ListExpression)current).expressions) {
+                visitListOfExpressions(exprs)
+            }
+        }
     }
 
     @Override
@@ -571,22 +582,57 @@ class ASTMatcher extends ClassCodeVisitorSupport {
 
     @Override
     public void visitMapExpression(final MapExpression expression) {
-        super.visitMapExpression(expression);
+        doWithNode(MapExpression, current) {
+            def entries = expression.mapEntryExpressions
+            def curEntries = ((MapExpression)current).mapEntryExpressions
+            doWithNode(entries.class, curEntries) {
+                visitListOfExpressions(entries)
+            }
+        }
     }
 
     @Override
     public void visitMapEntryExpression(final MapEntryExpression expression) {
-        super.visitMapEntryExpression(expression);
+        doWithNode(MapEntryExpression, current) {
+            def key = expression.keyExpression
+            def value = expression.valueExpression
+            def cur = (MapEntryExpression) current
+            def curKey = cur.keyExpression
+            def curValue = cur.valueExpression
+            doWithNode(key.class, curKey) {
+                key.visit(this)
+            }
+            doWithNode(value.class, curValue) {
+                value.visit(this)
+            }
+        }
     }
 
     @Override
     public void visitRangeExpression(final RangeExpression expression) {
-        super.visitRangeExpression(expression);
+        doWithNode(RangeExpression, current) {
+            def from = expression.from
+            def to = expression.to
+            def cur = (RangeExpression) current
+            def curFrom = cur.from
+            def curTo = cur.to
+            doWithNode(from.class, curFrom) {
+                from.visit(this)
+            }
+            doWithNode(to.class, curTo) {
+                to.visit(this)
+            }
+        }
     }
 
     @Override
     public void visitSpreadExpression(final SpreadExpression expression) {
-        super.visitSpreadExpression(expression);
+        doWithNode(SpreadExpression, current) {
+            def expr = expression.expression
+            doWithNode(expr.class, ((SpreadExpression)current).expression) {
+                expr.visit(this)
+            }
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/02e53fcf/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
index b473d66..064e01c 100644
--- a/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
+++ b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/matcher/ASTMatcherTest.groovy
@@ -17,6 +17,7 @@
 package org.codehaus.groovy.macro.matcher
 
 import org.codehaus.groovy.ast.expr.ClassExpression
+import org.codehaus.groovy.ast.expr.StaticMethodCallExpression
 import org.codehaus.groovy.control.CompilePhase
 
 class ASTMatcherTest extends GroovyTestCase {
@@ -203,4 +204,89 @@ class ASTMatcherTest extends GroovyTestCase {
         assert !ASTMatcher.matches(ast5, ast6)
         assert !ASTMatcher.matches(ast5, ast7)
     }
+
+    void testNotExpression() {
+        def ast1 = macro { !a }
+        def ast2 = macro { !a }
+        def ast3 = macro { a }
+        def ast4 = macro { !b }
+        def ast5 = macro { !(a+b) }
+        assert ASTMatcher.matches(ast1, ast1)
+        assert ASTMatcher.matches(ast1, ast2)
+        assert ASTMatcher.matches(ast2, ast1)
+        assert !ASTMatcher.matches(ast1, ast3)
+        assert !ASTMatcher.matches(ast1, ast4)
+        assert !ASTMatcher.matches(ast1, ast5)
+    }
+
+    void testMapExpression() {
+        def ast1 = macro { [:] }
+        def ast2 = macro { [:] }
+        def ast3 = macro { [a:''] }
+        def ast4 = macro { [b:''] }
+        def ast5 = macro { [a:'a'] }
+        def ast6 = macro { [a:'a',b:'b'] }
+        def ast7 = macro { [b:'b', a:'a'] }
+        def ast8 = macro { [a:'a', *:'b'] }
+        assert ASTMatcher.matches(ast1, ast1)
+        assert ASTMatcher.matches(ast1, ast2)
+        assert ASTMatcher.matches(ast2, ast1)
+        assert !ASTMatcher.matches(ast1, ast3)
+        assert !ASTMatcher.matches(ast1, ast4)
+        assert !ASTMatcher.matches(ast1, ast5)
+        assert !ASTMatcher.matches(ast4, ast5)
+        assert !ASTMatcher.matches(ast5, ast6)
+        assert !ASTMatcher.matches(ast6, ast7)
+        assert !ASTMatcher.matches(ast3, ast8)
+        assert !ASTMatcher.matches(ast6, ast8)
+    }
+
+    void testRangeExpression() {
+        def ast1 = macro { (0..10) }
+        def ast2 = macro { (0..10) }
+        def ast3 = macro { (1..10) }
+        def ast4 = macro { (0..9) }
+        def ast5 = macro { ('a'..'z') }
+        def ast6 = macro { (0.0..10.0) }
+        assert ASTMatcher.matches(ast1, ast1)
+        assert ASTMatcher.matches(ast1, ast2)
+        assert ASTMatcher.matches(ast2, ast1)
+        assert !ASTMatcher.matches(ast1, ast3)
+        assert !ASTMatcher.matches(ast1, ast4)
+        assert !ASTMatcher.matches(ast1, ast5)
+        assert !ASTMatcher.matches(ast1, ast6)
+    }
+
+    void testListExpression() {
+        def ast1 = macro { [] }
+        def ast2 = macro { [] }
+        def ast3 = macro { [a] }
+        def ast4 = macro { [a] }
+        def ast5 = macro { [b] }
+        def ast6 = macro { [a,b] }
+        assert ASTMatcher.matches(ast1, ast1)
+        assert ASTMatcher.matches(ast1, ast2)
+        assert ASTMatcher.matches(ast2, ast1)
+        assert ASTMatcher.matches(ast3, ast3)
+        assert ASTMatcher.matches(ast3, ast4)
+        assert ASTMatcher.matches(ast4, ast3)
+        assert !ASTMatcher.matches(ast1, ast3)
+        assert !ASTMatcher.matches(ast1, ast5)
+        assert !ASTMatcher.matches(ast4, ast5)
+        assert !ASTMatcher.matches(ast5, ast4)
+        assert !ASTMatcher.matches(ast5, ast6)
+        assert !ASTMatcher.matches(ast1, ast6)
+    }
+
+    void testSpreadExpression() {
+        def ast1 = macro { [*a] }
+        def ast2 = macro { [*a] }
+        def ast3 = macro { [*b] }
+        def ast4 = macro { [a] }
+        assert ASTMatcher.matches(ast1, ast1)
+        assert ASTMatcher.matches(ast1, ast2)
+        assert ASTMatcher.matches(ast2, ast1)
+        assert !ASTMatcher.matches(ast1, ast3)
+        assert !ASTMatcher.matches(ast1, ast4)
+    }
 }