2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.jetbrains
.plugins
.groovy
.compiler
;
19 import com
.intellij
.compiler
.CompilerConfiguration
;
22 import com
.intellij
.openapi
.module
.Module
;
23 import com
.intellij
.openapi
.vfs
.VirtualFile
;
24 import com
.intellij
.psi
.PsiFile
;
25 import junit
.framework
.AssertionFailedError
;
30 public class GroovyCompilerTest
extends GroovyCompilerTestCase
{
32 public void testPlainGroovy() throws Throwable
{
33 myFixture
.addFileToProject("A.groovy", "println '239'");
35 assertOutput("A", "239");
38 public void testJavaDependsOnGroovy() throws Throwable
{
39 myFixture
.addClass("public class Foo {" +
40 "public static void main(String[] args) { " +
41 " System.out.println(new Bar().foo());" +
44 myFixture
.addFileToProject("Bar.groovy", "class Bar {" +
50 assertOutput("Foo", "239");
53 public void testCorrectFailAndCorrect() throws Exception
{
54 myFixture
.addClass("public class Foo {" +
55 "public static void main(String[] args) { " +
56 " System.out.println(new Bar().foo());" +
59 final String barText
= "class Bar {" + " def foo() { 239 }" + "}";
60 final PsiFile file
= myFixture
.addFileToProject("Bar.groovy", barText
);
62 assertOutput("Foo", "239");
64 setFileText(file
, "class Bar {}");
67 fail("Make should fail");
69 catch (RuntimeException e
) {
70 if (!(e
.getCause() instanceof AssertionFailedError
)) {
75 setFileText(file
, barText
);
77 assertOutput("Foo", "239");
80 public void testRenameToJava() throws Throwable
{
81 myFixture
.addClass("public class Foo {" +
82 "public static void main(String[] args) { " +
83 " System.out.println(new Bar().foo());" +
88 myFixture
.addFileToProject("Bar.groovy", "public class Bar {" + "public int foo() { " + " return 239;" + "}" + "}");
91 assertOutput("Foo", "239");
93 setFileName bar
, "Bar.java"
96 assertOutput("Foo", "239");
99 public void testTransitiveJavaDependency() throws Throwable
{
100 final VirtualFile ifoo
= myFixture
.addClass("public interface IFoo { int foo(); }").getContainingFile().getVirtualFile();
101 myFixture
.addClass("public class Foo implements IFoo {" +
102 " public int foo() { return 239; }" +
104 final PsiFile bar
= myFixture
.addFileToProject("Bar.groovy", "class Bar {" +
106 "public static void main(String[] args) { " +
107 " System.out.println(new Foo().foo());" +
111 assertOutput("Bar", "239");
114 touch(bar
.getVirtualFile());
116 //assertTrue(assertOneElement(make()).contains("WARNING: Groovyc stub generation failed"));
118 assertOutput("Bar", "239");
121 public void testTransitiveJavaDependencyThroughGroovy() throws Throwable
{
122 myFixture
.addClass("public class IFoo { void foo() {} }").getContainingFile().getVirtualFile();
123 myFixture
.addFileToProject("Foo.groovy", "class Foo {\n" +
125 " public int foo() { return 239; }\n" +
127 final PsiFile bar
= myFixture
.addFileToProject("Bar.groovy", "class Bar extends Foo {" +
128 "public static void main(String[] args) { " +
129 " System.out.println(new Foo().foo());" +
133 assertOutput("Bar", "239");
135 deleteClassFile("IFoo");
136 touch(bar
.getVirtualFile());
138 //assertTrue(assertOneElement(make()).contains("WARNING: Groovyc error"));
140 assertOutput("Bar", "239");
143 public void testDeleteTransitiveJavaClass() throws Throwable
{
144 myFixture
.addClass("public interface IFoo { int foo(); }");
145 myFixture
.addClass("public class Foo implements IFoo {" +
146 " public int foo() { return 239; }" +
148 final PsiFile bar
= myFixture
.addFileToProject("Bar.groovy", "class Bar {" +
150 "public static void main(String[] args) { " +
151 " System.out.println(new Foo().foo());" +
155 assertOutput("Bar", "239");
157 deleteClassFile("IFoo");
158 touch(bar
.getVirtualFile());
160 //assertTrue(assertOneElement(make()).contains("WARNING: Groovyc stub generation failed"));
162 assertOutput("Bar", "239");
165 public void testGroovyDependsOnGroovy() throws Throwable
{
166 myFixture
.addClass("public class JustToMakeGroovyGenerateStubs {}");
167 myFixture
.addFileToProject("Foo.groovy", "class Foo { }");
168 final PsiFile bar
= myFixture
.addFileToProject("Bar.groovy", "class Bar {" +
169 "def foo(Foo f) {}\n" +
170 "public static void main(String[] args) { " +
171 " System.out.println(239);" +
175 assertOutput("Bar", "239");
177 touch(bar
.getVirtualFile());
180 assertOutput("Bar", "239");
183 public void testMakeInTests() throws Throwable
{
185 myFixture
.addFileToProject("tests/Super.groovy", "class Super {}");
188 myFixture
.addFileToProject("tests/Sub.groovy", "class Sub {\n" +
189 " Super xxx() {}\n" +
190 " static void main(String[] args) {" +
194 myFixture
.addFileToProject("tests/Java.java", "public class Java {}");
196 assertOutput("Sub", "hello");
199 public void testTestsDependOnProduction() throws Throwable
{
201 myFixture
.addFileToProject("src/com/Bar.groovy", "package com\n" +
203 myFixture
.addFileToProject("src/com/ToGenerateStubs.java", "package com;\n" +
204 "public class ToGenerateStubs {}");
205 myFixture
.addFileToProject("tests/com/BarTest.groovy", "package com\n" +
206 "class BarTest extends Bar {}");
210 public void testStubForGroovyExtendingJava() throws Exception
{
211 myFixture
.addClass("public class Foo {}");
212 myFixture
.addFileToProject("Bar.groovy", "class Bar extends Foo {}");
213 myFixture
.addClass("public class Goo extends Bar {}");
218 public void testDontApplyTransformsFromSameModule() throws Exception
{
221 myFixture
.addClass("public class JavaClassToGenerateStubs {}");
227 private void addTransform() throws IOException
{
228 myFixture
.addFileToProject("Transf.groovy",
229 "import org.codehaus.groovy.ast.*\n" +
230 "import org.codehaus.groovy.control.*\n" +
231 "import org.codehaus.groovy.transform.*\n" +
232 "@GroovyASTTransformation(phase = CompilePhase.CONVERSION)\n" +
233 "public class Transf implements ASTTransformation {\n" +
234 " void visit(ASTNode[] nodes, SourceUnit sourceUnit) {\n" +
235 " ModuleNode module = nodes[0]\n" +
236 " for (clazz in module.classes) {\n" +
237 " if (clazz.name.contains('Bar')) " +
238 " module.addStaticImportClass('Foo', ClassHelper.makeWithoutCaching(Foo.class));\n" +
243 myFixture
.addFileToProject("Foo.groovy", "class Foo {\n" +
244 "static def autoImported() { 239 }\n" +
247 CompilerConfiguration
.getInstance(getProject()).addResourceFilePattern("*.ASTTransformation");
249 myFixture
.addFileToProject("META-INF/services/org.codehaus.groovy.transform.ASTTransformation", "Transf");
252 public void testApplyTransformsFromDependencies() throws Exception
{
255 myFixture
.addFileToProject("dependent/Bar.groovy", "class Bar {\n" +
256 " static Object zzz = autoImported()\n" +
257 " static void main(String[] args) {\n" +
262 myFixture
.addFileToProject("dependent/AJavaClass.java", "class AJavaClass {}");
264 Module dep
= addDependentModule();
266 addGroovyLibrary(dep
, false);
269 assertOutput("Bar", "239", dep
);
272 public void testExtendFromGroovyAbstractClass() throws Exception
{
273 myFixture
.addFileToProject
"Super.groovy", "abstract class Super {}"
274 myFixture
.addFileToProject
"AJava.java", "public class AJava {}"
277 myFixture
.addFileToProject
"Sub.groovy", "class Sub extends Super {}"
281 public void test1_7InnerClass() throws Exception
{
282 myFixture
.addFileToProject
"Foo.groovy", """
286 myFixture
.addFileToProject
"AJava.java", "public class AJava extends Foo.Bar {}"
290 public void testRecompileDependentClass() throws Exception
{
291 def cloud
= myFixture
.addFileToProject("Cloud.groovy", """
293 def accessFooProperty(Foo c) {
298 myFixture
.addFileToProject
"Foo.groovy", """
300 def withGooParameter(Goo x) {}
302 def goo
= myFixture
.addFileToProject("Goo.groovy", "class Goo {}")
306 touch(cloud
.virtualFile
)
307 touch(goo
.virtualFile
)