Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Jun 17, 2018
1 parent e3ed475 commit 321f3fd
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ protected final Map<String, String> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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();
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand All @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<li>@groovy.transform.AutoFinal (2.5+)</li>
<li>@groovy.transform.AutoImplement (2.5+)</li>
<li>@groovy.transform.BaseScript</li>
<li>@groovy.transform.Canonical, et al.</li>
<li>@groovy.transform.Canonical, et al.&nbsp;&#x2713;</li>
<ul style="margin-bottom: 0">
<li>@groovy.transform.EqualsAndHashCode</li>
<li>@groovy.transform.ToString</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 321f3fd

Please sign in to comment.