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:54 UTC
[34/37] incubator-groovy git commit: tests for MacroGroovy
tests for MacroGroovy
Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/d486abd3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/d486abd3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/d486abd3
Branch: refs/heads/master
Commit: d486abd3e9778bdca0664b18348b779d8420ab62
Parents: 93a42aa
Author: Sergei Egorov <bs...@gmail.com>
Authored: Tue Mar 17 16:50:48 2015 +0200
Committer: Sergei Egorov <bs...@gmail.com>
Committed: Mon Sep 28 14:33:13 2015 +0300
----------------------------------------------------------------------
.../test/groovy/groovy/SimpleMacroTest.groovy | 131 -------------
.../org/codehaus/groovy/macro/MacroTest.groovy | 187 +++++++++++++++++++
2 files changed, 187 insertions(+), 131 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/d486abd3/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy b/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy
deleted file mode 100644
index 9a29fc3..0000000
--- a/subprojects/groovy-macro/src/test/groovy/groovy/SimpleMacroTest.groovy
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * Copyright 2003-2013 the original author or authors.
- *
- * Licensed 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.
- */
-package groovy
-
-import groovy.transform.CompileStatic
-import org.codehaus.groovy.ast.ClassHelper
-import org.codehaus.groovy.ast.VariableScope
-import org.codehaus.groovy.ast.builder.AstAssert
-import org.codehaus.groovy.ast.expr.*
-import org.codehaus.groovy.ast.stmt.*
-import org.codehaus.groovy.control.CompilePhase
-
-import static org.codehaus.groovy.ast.expr.VariableExpression.*;
-
-/**
- *
- * @author Sergei Egorov <bs...@gmail.com>
- */
-@CompileStatic
-class SimpleMacroTest extends GroovyTestCase {
-
- static final String TO_LOWER_CASE_METHOD_NAME = macro { "".toLowerCase() }.getMethodAsString()
-
- public void testMethod() {
-
- def someVariable = new VariableExpression("someVariable");
-
- ReturnStatement result = macro {
- return new NonExistingClass($v{someVariable});
- }
-
- def expected = new ReturnStatement(new ConstructorCallExpression(ClassHelper.make("NonExistingClass"), new ArgumentListExpression(someVariable)));
-
- assertSyntaxTree(expected, result);
- }
-
- public void testAsIs() {
- def expected = new BlockStatement([
- new ExpressionStatement(new MethodCallExpression(THIS_EXPRESSION, "println", new ArgumentListExpression(new ConstantExpression("foo"))))
- ] as List<Statement>, new VariableScope());
-
- BlockStatement result = macro(true) {
- println "foo"
- }
-
- assertSyntaxTree(expected, result);
- }
-
- public void testInception() {
-
- ConstructorCallExpression result = macro {
- new NonExistingClass($v{macro {someVariable}});
- }
-
- def expected = new ConstructorCallExpression(ClassHelper.make("NonExistingClass"), new ArgumentListExpression(new VariableExpression("someVariable")));
-
- assertSyntaxTree(expected, result);
- }
-
- public void testMethodName() {
- // Very useful when you don't want to hardcode method or variable names
- assertEquals("toLowerCase", TO_LOWER_CASE_METHOD_NAME)
- assertEquals("valueOf", macro { String.valueOf() }.getMethodAsString())
- }
-
- public void testBlock() {
-
- def result = macro {
- println "foo"
- println "bar"
- }
-
- def expected = new BlockStatement(
- [
- new ExpressionStatement(new MethodCallExpression(THIS_EXPRESSION, "println", new ArgumentListExpression(new ConstantExpression("foo")))),
- new ExpressionStatement(new MethodCallExpression(THIS_EXPRESSION, "println", new ArgumentListExpression(new ConstantExpression("bar")))),
- ] as List<Statement>,
- new VariableScope()
- )
-
- assertSyntaxTree(expected, result);
- }
-
- public void testCompilePhase() {
-
- def result = macro(CompilePhase.FINALIZATION) {
- println "foo"
- println "bar"
- }
-
- def expected = new BlockStatement(
- [
- new ExpressionStatement(new MethodCallExpression(THIS_EXPRESSION, "println", new ArgumentListExpression(new ConstantExpression("foo")))),
- // In FINALIZATION phase last println will be return statement
- new ReturnStatement(new MethodCallExpression(THIS_EXPRESSION, "println", new ArgumentListExpression(new ConstantExpression("bar")))),
- ] as List<Statement>,
- new VariableScope()
- )
-
- assertSyntaxTree(expected, result);
- }
-
- public void testAsIsWithCompilePhase() {
- def expected = new BlockStatement([
- new ReturnStatement(new MethodCallExpression(THIS_EXPRESSION, "println", new ArgumentListExpression(new ConstantExpression("foo"))))
- ] as List<Statement>, new VariableScope());
-
- def result = macro(CompilePhase.FINALIZATION, true) {
- println "foo"
- }
-
- assertSyntaxTree(expected, result);
- }
-
- protected void assertSyntaxTree(Object expected, Object result) {
- AstAssert.assertSyntaxTree([expected], [result])
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/d486abd3/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/MacroTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/MacroTest.groovy b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/MacroTest.groovy
new file mode 100644
index 0000000..dfff23abb
--- /dev/null
+++ b/subprojects/groovy-macro/src/test/groovy/org/codehaus/groovy/macro/MacroTest.groovy
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2003-2015 the original author or authors.
+ *
+ * Licensed 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.
+ */
+
+package org.codehaus.groovy.macro
+
+import groovy.transform.CompileStatic;
+
+/**
+ *
+ * @author Sergei Egorov <bs...@gmail.com>
+ */
+@CompileStatic
+class MacroTest extends GroovyTestCase {
+
+ void testSimpleCase() {
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.*;
+ import org.codehaus.groovy.ast.stmt.*;
+ import org.codehaus.groovy.ast.ClassHelper;
+ import org.codehaus.groovy.ast.builder.AstAssert;
+
+ import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
+
+ def someVariable = new VariableExpression("someVariable");
+
+ ReturnStatement result = macro {
+ return new NonExistingClass($v{someVariable});
+ }
+
+ def expected = returnS(ctorX(ClassHelper.make("NonExistingClass"), args(someVariable)))
+
+ AstAssert.assertSyntaxTree([expected], [result]);
+'''
+ }
+
+ void testAsIs() {
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.*;
+ import org.codehaus.groovy.ast.stmt.*;
+ import org.codehaus.groovy.ast.ClassHelper;
+ import org.codehaus.groovy.ast.builder.AstAssert;
+
+ import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
+
+ BlockStatement result = macro(true) {
+ println "foo"
+ }
+
+ def expected = block(stmt(callThisX("println", args(constX("foo")))))
+
+ AstAssert.assertSyntaxTree([expected], [result]);
+'''
+ }
+
+ void testInception() {
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.*;
+ import org.codehaus.groovy.ast.stmt.*;
+ import org.codehaus.groovy.ast.ClassHelper;
+ import org.codehaus.groovy.ast.builder.AstAssert;
+
+ import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
+
+ ConstructorCallExpression result = macro {
+ new NonExistingClass($v{macro {someVariable}});
+ }
+
+ def expected = ctorX(ClassHelper.make("NonExistingClass"), args(varX("someVariable")))
+
+ AstAssert.assertSyntaxTree([expected], [result]);
+'''
+ }
+
+ void testMethodNameFromCode() {
+ assertScript '''
+ // Very useful when you don't want to hardcode method or variable names
+ assert "toLowerCase" == macro { "".toLowerCase() }.getMethodAsString()
+ assert "valueOf" == macro { String.valueOf() }.getMethodAsString()
+'''
+ }
+
+ void testBlock() {
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.*;
+ import org.codehaus.groovy.ast.stmt.*;
+ import org.codehaus.groovy.ast.ClassHelper;
+ import org.codehaus.groovy.ast.builder.AstAssert;
+
+ import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
+
+ def result = macro {
+ println "foo"
+ println "bar"
+ }
+
+ def expected = block(
+ stmt(callThisX("println", args(constX("foo")))),
+ stmt(callThisX("println", args(constX("bar")))),
+ )
+
+ AstAssert.assertSyntaxTree([expected], [result]);
+'''
+ }
+
+ void testCompilePhase() {
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.*;
+ import org.codehaus.groovy.ast.stmt.*;
+ import org.codehaus.groovy.ast.ClassHelper;
+ import org.codehaus.groovy.ast.builder.AstAssert;
+ import org.codehaus.groovy.control.CompilePhase;
+
+ import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
+
+
+ def result = macro(CompilePhase.FINALIZATION) {
+ println "foo"
+ println "bar"
+ }
+
+ def expected = block(
+ stmt(callThisX("println", args(constX("foo")))),
+ // In FINALIZATION phase last println will be ReturnStatement
+ returnS(callThisX("println", args(constX("bar")))),
+ )
+
+ AstAssert.assertSyntaxTree([expected], [result]);
+'''
+ }
+
+ void testAsIsWithCompilePhase() {
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.*;
+ import org.codehaus.groovy.ast.stmt.*;
+ import org.codehaus.groovy.ast.ClassHelper;
+ import org.codehaus.groovy.ast.builder.AstAssert;
+ import org.codehaus.groovy.control.CompilePhase;
+
+ import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
+
+ def result = macro(CompilePhase.FINALIZATION, true) {
+ println "foo"
+ }
+
+ def expected = block(
+ returnS(callThisX("println", args(constX("foo"))))
+ )
+
+ AstAssert.assertSyntaxTree([expected], [result]);
+'''
+ }
+
+ void testCompileStatic() {
+ assertScript '''
+ import groovy.transform.CompileStatic
+ import org.codehaus.groovy.ast.stmt.ReturnStatement;
+ import org.codehaus.groovy.ast.ClassHelper;
+ import org.codehaus.groovy.ast.builder.AstAssert;
+
+ import static org.codehaus.groovy.ast.tools.GeneralUtils.*;
+
+ @CompileStatic
+ ReturnStatement getReturnStatement() {
+ return macro {
+ return new NonExistingClass("foo");
+ }
+ }
+
+ def expected = returnS(ctorX(ClassHelper.make("NonExistingClass"), args(constX("foo"))))
+
+ AstAssert.assertSyntaxTree([expected], [getReturnStatement()]);
+'''
+ }
+}