call accessible super constructor from groovy stub & compiler tests together
[fedora-idea.git] / plugins / groovy / test / org / jetbrains / plugins / groovy / compiler / GroovyCompilerTest.groovy
blob8e0dcb284b6dc6e6526594584326e0913c42eb92
1 /*
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;
27 /**
28 * @author peter
30 public class GroovyCompilerTest extends GroovyCompilerTestCase {
32 public void testPlainGroovy() throws Throwable {
33 myFixture.addFileToProject("A.groovy", "println '239'");
34 assertEmpty(make());
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());" +
42 "}" +
43 "}");
44 myFixture.addFileToProject("Bar.groovy", "class Bar {" +
45 " def foo() {" +
46 " 239" +
47 " }" +
48 "}");
49 assertEmpty(make());
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());" +
57 "}" +
58 "}");
59 final String barText = "class Bar {" + " def foo() { 239 }" + "}";
60 final PsiFile file = myFixture.addFileToProject("Bar.groovy", barText);
61 assertEmpty(make());
62 assertOutput("Foo", "239");
64 setFileText(file, "class Bar {}");
65 try {
66 make();
67 fail("Make should fail");
69 catch (RuntimeException e) {
70 if (!(e.getCause() instanceof AssertionFailedError)) {
71 throw e;
75 setFileText(file, barText);
76 assertEmpty(make());
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());" +
84 "}" +
85 "}");
87 final PsiFile bar =
88 myFixture.addFileToProject("Bar.groovy", "public class Bar {" + "public int foo() { " + " return 239;" + "}" + "}");
90 assertEmpty(make());
91 assertOutput("Foo", "239");
93 setFileName bar, "Bar.java"
95 assertEmpty(make());
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; }" +
103 "}");
104 final PsiFile bar = myFixture.addFileToProject("Bar.groovy", "class Bar {" +
105 "Foo foo\n" +
106 "public static void main(String[] args) { " +
107 " System.out.println(new Foo().foo());" +
108 "}" +
109 "}");
110 assertEmpty(make());
111 assertOutput("Bar", "239");
113 touch(ifoo);
114 touch(bar.getVirtualFile());
116 //assertTrue(assertOneElement(make()).contains("WARNING: Groovyc stub generation failed"));
117 assertEmpty make()
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" +
124 " static IFoo f\n" +
125 " public int foo() { return 239; }\n" +
126 "}");
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());" +
130 "}" +
131 "}");
132 assertEmpty(make());
133 assertOutput("Bar", "239");
135 deleteClassFile("IFoo");
136 touch(bar.getVirtualFile());
138 //assertTrue(assertOneElement(make()).contains("WARNING: Groovyc error"));
139 assertEmpty make()
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; }" +
147 "}");
148 final PsiFile bar = myFixture.addFileToProject("Bar.groovy", "class Bar {" +
149 "Foo foo\n" +
150 "public static void main(String[] args) { " +
151 " System.out.println(new Foo().foo());" +
152 "}" +
153 "}");
154 assertEmpty(make());
155 assertOutput("Bar", "239");
157 deleteClassFile("IFoo");
158 touch(bar.getVirtualFile());
160 //assertTrue(assertOneElement(make()).contains("WARNING: Groovyc stub generation failed"));
161 assertEmpty make()
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);" +
172 "}" +
173 "}");
174 assertEmpty(make());
175 assertOutput("Bar", "239");
177 touch(bar.getVirtualFile());
179 assertEmpty(make());
180 assertOutput("Bar", "239");
183 public void testMakeInTests() throws Throwable {
184 setupTestSources();
185 myFixture.addFileToProject("tests/Super.groovy", "class Super {}");
186 assertEmpty(make());
188 myFixture.addFileToProject("tests/Sub.groovy", "class Sub {\n" +
189 " Super xxx() {}\n" +
190 " static void main(String[] args) {" +
191 " println 'hello'" +
192 " }" +
193 "}");
194 myFixture.addFileToProject("tests/Java.java", "public class Java {}");
195 assertEmpty(make());
196 assertOutput("Sub", "hello");
199 public void testTestsDependOnProduction() throws Throwable {
200 setupTestSources();
201 myFixture.addFileToProject("src/com/Bar.groovy", "package com\n" +
202 "class Bar {}");
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 {}");
207 assertEmpty(make());
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 {}");
215 assertEmpty(make());
218 public void testDontApplyTransformsFromSameModule() throws Exception {
219 addTransform();
221 myFixture.addClass("public class JavaClassToGenerateStubs {}");
223 assertEmpty(make());
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" +
239 " }\n" +
240 " }\n" +
241 "}");
243 myFixture.addFileToProject("Foo.groovy", "class Foo {\n" +
244 "static def autoImported() { 239 }\n" +
245 "}");
247 CompilerConfiguration.getInstance(getProject()).addResourceFilePattern("*.ASTTransformation");
249 myFixture.addFileToProject("META-INF/services/org.codehaus.groovy.transform.ASTTransformation", "Transf");
252 public void testApplyTransformsFromDependencies() throws Exception {
253 addTransform();
255 myFixture.addFileToProject("dependent/Bar.groovy", "class Bar {\n" +
256 " static Object zzz = autoImported()\n" +
257 " static void main(String[] args) {\n" +
258 " println zzz\n" +
259 " }\n" +
260 "}");
262 myFixture.addFileToProject("dependent/AJavaClass.java", "class AJavaClass {}");
264 Module dep = addDependentModule();
266 addGroovyLibrary(dep, false);
268 assertEmpty(make());
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 {}"
275 assertEmpty make()
277 myFixture.addFileToProject "Sub.groovy", "class Sub extends Super {}"
278 assertEmpty make()
281 public void test1_7InnerClass() throws Exception {
282 myFixture.addFileToProject "Foo.groovy", """
283 class Foo {
284 static class Bar {}
285 }"""
286 myFixture.addFileToProject "AJava.java", "public class AJava extends Foo.Bar {}"
287 assertEmpty make()
290 public void testRecompileDependentClass() throws Exception {
291 def cloud = myFixture.addFileToProject("Cloud.groovy", """
292 class Cloud {
293 def accessFooProperty(Foo c) {
294 c.prop = 2
297 """)
298 myFixture.addFileToProject "Foo.groovy", """
299 class Foo {
300 def withGooParameter(Goo x) {}
301 }"""
302 def goo = myFixture.addFileToProject("Goo.groovy", "class Goo {}")
304 assertEmpty make()
306 touch(cloud.virtualFile)
307 touch(goo.virtualFile)
308 assertEmpty make()