From 321f3fdac159ca983a4fc3a6dfc55b47cbc0658f Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Sun, 17 Jun 2018 10:52:18 -0500 Subject: [PATCH] #421 #606 --- .../tests/basic/GroovyCompilerTestSuite.java | 6 + .../core/tests/basic/GroovySimpleTests.java | 27 ++-- .../core/tests/xform/CanonicalTests.java | 119 ++++++++++++++++++ .../core/tests/xform/SingletonTests.java | 113 ++++++----------- .../jdt/groovy/core/tests/xform/package.html | 2 +- .../groovy/alltests/GroovyJDTTests.groovy | 3 +- 6 files changed, 178 insertions(+), 92 deletions(-) create mode 100644 base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/CanonicalTests.java diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovyCompilerTestSuite.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovyCompilerTestSuite.java index 269ac7142e..8f10010b4b 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovyCompilerTestSuite.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovyCompilerTestSuite.java @@ -191,6 +191,12 @@ protected final Map getCompilerOptions() { return ReflectionUtils.executePrivateMethod(AbstractRegressionTest.class, "getCompilerOptions", testDriver); } + protected final File createScript(CharSequence name, CharSequence contents) { + String folder = Util.getOutputDirectory() + File.separator + "resources" + File.separator; + new File(folder).mkdirs(); Util.writeToFile(contents.toString(), folder + name); + return new File(folder + name); + } + protected final void runConformTest(String[] sources) { runConformTest(sources, (String) null, (String) null); } diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java index 5cd1cbe76e..18d4cab967 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/GroovySimpleTests.java @@ -39,7 +39,6 @@ import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration; import org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode; import org.codehaus.jdt.groovy.internal.compiler.ast.JDTResolver; -import org.eclipse.jdt.core.tests.util.Util; import org.eclipse.jdt.core.util.ClassFileBytesDisassembler; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; @@ -5872,8 +5871,8 @@ public void testInfiniteLoop() { //-------------------------------------------------------------------------- private void assertEventCount(int expectedCount, EventListener listener) { - if (listener.eventCount()!=expectedCount) { - fail("Expected "+expectedCount+" events but found "+listener.eventCount()+"\nEvents:\n"+listener.toString()); + if (listener.eventCount() != expectedCount) { + fail("Expected " + expectedCount + " events but found " + listener.eventCount() + "\nEvents:\n" + listener.toString()); } } @@ -5883,30 +5882,24 @@ private void assertEvent(String eventText, EventListener listener) { while (eventIter.hasNext()) { String s = eventIter.next(); if (s.equals(eventText)) { - found=true; + found = true; break; } } if (!found) { - fail("Expected event '"+eventText+"'\nEvents:\n"+listener.toString()); + fail("Expected event '" + eventText + "'\nEvents:\n" + listener.toString()); } } - public File createScript(CharSequence name, CharSequence contents) { - String folder = Util.getOutputDirectory() + File.separator + "resources" + File.separator; - new File(folder).mkdirs(); Util.writeToFile(contents.toString(), folder + name); - return new File(folder + name); - } - /** * Find the named file (which should have just been compiled) and for the named method determine * the ClassNode for the return type and return the name of the classnode. */ - public String getReturnTypeOfMethod(String filename,String methodname) { + public String getReturnTypeOfMethod(String filename, String methodname) { ModuleNode mn = getModuleNode(filename); ClassNode cn = mn.getClasses().get(0); assertNotNull(cn); - MethodNode methodNode = cn.getMethod(methodname,new Parameter[]{}); + MethodNode methodNode = cn.getMethod(methodname, Parameter.EMPTY_ARRAY); assertNotNull(methodNode); ClassNode returnType = methodNode.getReturnType(); return returnType.getName(); @@ -5915,10 +5908,10 @@ public String getReturnTypeOfMethod(String filename,String methodname) { private String stringifyFieldDecl(FieldDeclaration fDecl) { StringBuffer sb = new StringBuffer(); sb.append(fDecl.name); - sb.append(" sourceStart>sourceEnd:"+fDecl.sourceStart+">"+fDecl.sourceEnd); - sb.append(" declSourceStart>declSourceEnd:"+fDecl.declarationSourceStart+">"+fDecl.declarationSourceEnd); - sb.append(" modifiersSourceStart="+fDecl.modifiersSourceStart); // first char of decls modifiers - sb.append(" endPart1Position:"+fDecl.endPart1Position); // char after type decl ('int x,y' is space) + sb.append(" sourceStart>sourceEnd:" + fDecl.sourceStart + ">" + fDecl.sourceEnd); + sb.append(" declSourceStart>declSourceEnd:" + fDecl.declarationSourceStart + ">" + fDecl.declarationSourceEnd); + sb.append(" modifiersSourceStart=" + fDecl.modifiersSourceStart); // first char of decls modifiers + sb.append(" endPart1Position:" + fDecl.endPart1Position); // char after type decl ('int x,y' is space) return sb.toString(); } } diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/CanonicalTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/CanonicalTests.java new file mode 100644 index 0000000000..ae566980cf --- /dev/null +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/CanonicalTests.java @@ -0,0 +1,119 @@ +/* + * Copyright 2009-2018 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.eclipse.jdt.groovy.core.tests.xform; + +import java.util.Map; + +import org.eclipse.jdt.groovy.core.tests.basic.GroovyCompilerTestSuite; +import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; +import org.junit.Ignore; +import org.junit.Test; + +/** + * Test cases for {@link groovy.transform.Canonical}, at al. + */ +public final class CanonicalTests extends GroovyCompilerTestSuite { + + @Test @Ignore("https://github.com/groovy/groovy-eclipse/issues/421") + public void testCanonical1() { + String[] sources = { + "Main.java", + "public class Main {\n" + + " public static void main(String... args) {\n" + + " System.out.print(new Foo(\"one\", \"two\"));\n" + + " }\n" + + "}\n", + + "Foo.groovy", + "@groovy.transform.Canonical\n" + + "class Foo {\n" + + " String bar, baz\n" + + "}\n", + }; + + runConformTest(sources, "Foo(one, two)"); + } + + @Test + public void testCanonical2() { + String[] sources = { + "Main.groovy", + "class Main {\n" + + " static void main(args) {\n" + + " print(new Foo('one', 'two'))\n" + + " }\n" + + "}\n", + + "Foo.groovy", + "@groovy.transform.Canonical\n" + + "class Foo {\n" + + " String bar, baz\n" + + "}\n", + }; + + runConformTest(sources, "Foo(one, two)"); + } + + @Test + public void testCanonical3() { + String[] sources = { + "Main.groovy", + "class Main {\n" + + " static void main(args) {\n" + + " print(new Foo('one', 'two'))\n" + + " }\n" + + "}\n", + + "Foo.groovy", + "import groovy.transform.Canonical\n" + + "@Canonical\n" + + "class Foo {\n" + + " String bar, baz\n" + + "}\n", + }; + + runConformTest(sources, "Foo(one, two)"); + } + + @Test + public void testCanonical4() { + Map options = getCompilerOptions(); + options.put(CompilerOptions.OPTIONG_GroovyCompilerConfigScript, createScript("config.groovy", + "withConfig(configuration) {\n" + + " imports {\n" + + " normal 'groovy.transform.Canonical'\n" + + " }\n" + + "}\n" + ).getAbsolutePath()); + + String[] sources = { + "Main.groovy", + "class Main {\n" + + " static void main(args) {\n" + + " print(new Foo('one', 'two'))\n" + + " }\n" + + "}\n", + + "Foo.groovy", + "@Canonical\n" + + "class Foo {\n" + + " String bar, baz\n" + + "}\n", + }; + + runConformTest(sources, "Foo(one, two)", options); + } +} diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/SingletonTests.java b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/SingletonTests.java index 9ac00d4ffc..004b06b83b 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/SingletonTests.java +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/SingletonTests.java @@ -24,20 +24,13 @@ */ public final class SingletonTests extends GroovyCompilerTestSuite { - @Test + @Test @Ignore("https://github.com/groovy/groovy-eclipse/issues/421") public void testSingleton1() { String[] sources = { - "Goo.groovy", - "class Goo {\n" + - " public static void main(String[] argv) {\n" + - " Run.main(argv)\n" + - " }\n" + - "}\n", - - "Run.groovy", - "public class Run {\n" + - " public static void main(String[] argv) {\n" + - " System.out.println(Wibble.getInstance().field)\n" + + "Main.java", + "public class Main {\n" + + " public static void main(String... args) {\n" + + " System.out.print(Wibble.getInstance().field);\n" + " }\n" + "}\n", @@ -50,94 +43,68 @@ public void testSingleton1() { runConformTest(sources, "abcd"); } - @Test // lazy option set in Singleton + @Test public void testSingleton2() { String[] sources = { - "Goo.groovy", - "class Goo {\n"+ - " public static void main(String[] argv) {\n"+ - " Run.main(argv);\n"+ - " }\n"+ - "}\n", - - "Run.groovy", - "public class Run {\n"+ - " public static void main(String[] argv) {\n"+ - " Wibble.run();\n"+ - " System.out.print(\"running \");\n"+ - " System.out.print(Wibble.getInstance().field);\n"+ - " }\n"+ + "Main.groovy", + "class Main {\n" + + " static void main(args) {\n" + + " print(Wibble.instance.field)\n" + + " }\n" + "}\n", "Wibble.groovy", - "@Singleton(lazy=false, strict=false) class Wibble {" + - " public String field = 'abcd';\n"+ - " private Wibble() { print \"ctor \";}\n"+ - " static void run() {}\n"+ + "@Singleton class Wibble {\n" + + " public String field = 'abcd'\n" + "}\n", }; - runConformTest(sources, "ctor running abcd"); + runConformTest(sources, "abcd"); } @Test public void testSingleton3() { String[] sources = { - "Goo.groovy", - "class Goo {\n"+ - " public static void main(String[] argv) {\n"+ - " Run.main(argv);\n"+ - " }\n"+ - "}\n", - - "Run.groovy", - "public class Run {\n"+ - " public static void main(String[] argv) {\n"+ - " Wibble.run();\n"+ - " System.out.print(\"running \");\n"+ - " System.out.print(Wibble.getInstance().field);\n"+ - " }\n"+ + "Main.groovy", + "class Main {\n" + + " static void main(args) {\n" + + " Wibble.run()\n" + + " print('running ')\n" + + " print(Wibble.instance.field)\n" + + " }\n" + "}\n", "Wibble.groovy", - "@Singleton(lazy=true, strict=false) class Wibble {" + - " public String field = 'abcd';\n"+ - " private Wibble() { print \"ctor \";}\n"+ - " static void run() {}\n"+ + "@Singleton(lazy=false, strict=false) class Wibble {" + + " public String field = 'abcd'\n" + + " private Wibble() { print 'ctor ' }\n" + + " static void run() {}\n" + "}\n", }; - runConformTest(sources, "running ctor abcd"); + runConformTest(sources, "ctor running abcd"); } - /** - * COOL!!! The getInstance() method is added by a late AST Transformation made due to the Singleton annotation - and yet - * still it is referencable from Java. This is not possible with normal joint compilation. - * currently have to 'turn on' support in org.eclipse.jdt.internal.compiler.lookup.Scope#oneLastLook - */ - @Test @Ignore - public void testSingleton_JavaAccessingTransformedGroovy() { + @Test // lazy option set in Singleton + public void testSingleton4() { String[] sources = { - "Goo.groovy", - "class Goo {\n"+ - " public static void main(String[] argv) {\n"+ - " Run.main(argv);\n"+ - " }\n"+ - "}\n", - - "Run.java", - "public class Run {\n"+ - " public static void main(String[] argv) {\n"+ - " System.out.println(Wibble.getInstance().field);\n"+ - " }\n"+ + "Main.groovy", + "public class Main {\n" + + " static void main(args) {\n" + + " Wibble.run()\n" + + " print('running ')\n" + + " print(Wibble.instance.field)\n" + + " }\n" + "}\n", "Wibble.groovy", - "@Singleton class Wibble {\n" + - " public final String field = 'abc'\n"+ + "@Singleton(lazy=true, strict=false) class Wibble {" + + " public String field = 'abcd';\n" + + " private Wibble() { print 'ctor ' }\n" + + " static void run() {}\n" + "}\n", }; - runConformTest(sources, "abc"); + runConformTest(sources, "running ctor abcd"); } } diff --git a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/package.html b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/package.html index f62e06fa4f..78c2662183 100644 --- a/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/package.html +++ b/base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/package.html @@ -27,7 +27,7 @@
  • @groovy.transform.AutoFinal (2.5+)
  • @groovy.transform.AutoImplement (2.5+)
  • @groovy.transform.BaseScript
  • -
  • @groovy.transform.Canonical, et al.
  • +
  • @groovy.transform.Canonical, et al. ✓
    • @groovy.transform.EqualsAndHashCode
    • @groovy.transform.ToString
    • diff --git a/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/GroovyJDTTests.groovy b/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/GroovyJDTTests.groovy index 24a103127a..5c6372e134 100644 --- a/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/GroovyJDTTests.groovy +++ b/ide-test/org.codehaus.groovy.alltests/src/org/codehaus/groovy/alltests/GroovyJDTTests.groovy @@ -30,8 +30,9 @@ import org.junit.runners.Suite org.eclipse.jdt.groovy.core.tests.basic.GroovySimpleTests_Compliance_1_8, org.eclipse.jdt.groovy.core.tests.basic.TraitsTests, - // AST Transform tests + // Xform tests org.eclipse.jdt.groovy.core.tests.xform.AnnotationCollectorTests, + org.eclipse.jdt.groovy.core.tests.xform.CanonicalTests, org.eclipse.jdt.groovy.core.tests.xform.CategoryTests, org.eclipse.jdt.groovy.core.tests.xform.DelegateTests, org.eclipse.jdt.groovy.core.tests.xform.GrabTests,