diff --git a/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java b/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java index 20b7f6831f6..500318810b5 100644 --- a/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java +++ b/idea/src/org/jetbrains/jet/plugin/compiler/K2JSCompiler.java @@ -17,6 +17,7 @@ package org.jetbrains.jet.plugin.compiler; import com.google.common.collect.Lists; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.compiler.CompileContext; import com.intellij.openapi.compiler.CompileScope; import com.intellij.openapi.compiler.CompilerMessageCategory; @@ -126,7 +127,7 @@ public final class K2JSCompiler implements TranslatingCompiler { String[] commandLineArgs = constructArguments(context.getProject(), outFile, roots[0]); Object rc = invokeExecMethod(environment, out, context, commandLineArgs, "org.jetbrains.jet.cli.js.K2JSCompiler"); - if (outDir != null) { + if (outDir != null && !ApplicationManager.getApplication().isUnitTestMode()) { outDir.refresh(false, true); } return CompilerUtils.getReturnCodeFromObject(rc); diff --git a/idea/testData/compilerMessages/helloWorld/src/test.kt b/idea/testData/compilerMessages/helloWorld/src/test.kt deleted file mode 100644 index a83f3c5862c..00000000000 --- a/idea/testData/compilerMessages/helloWorld/src/test.kt +++ /dev/null @@ -1,4 +0,0 @@ -package foo - -fun f() { -} \ No newline at end of file diff --git a/idea/testData/compilerMessages/k2js/helloWorld/src/test.kt b/idea/testData/compilerMessages/k2js/helloWorld/src/test.kt new file mode 100644 index 00000000000..fcb084924f6 --- /dev/null +++ b/idea/testData/compilerMessages/k2js/helloWorld/src/test.kt @@ -0,0 +1,3 @@ +package foo + +fun main(args : Array) {} diff --git a/idea/testData/compilerMessages/k2js/lib/src/lib.zip b/idea/testData/compilerMessages/k2js/lib/src/lib.zip new file mode 100644 index 00000000000..c3eb8303327 Binary files /dev/null and b/idea/testData/compilerMessages/k2js/lib/src/lib.zip differ diff --git a/idea/testData/compilerMessages/k2js/lib/src/test.kt b/idea/testData/compilerMessages/k2js/lib/src/test.kt new file mode 100644 index 00000000000..8248ddb6c6d --- /dev/null +++ b/idea/testData/compilerMessages/k2js/lib/src/test.kt @@ -0,0 +1,8 @@ +package foo + +import js.* + +fun main(args : Array) { + val c = 4 + libf() + c == 2 +} diff --git a/idea/testData/compilerMessages/simpleError/src/test.kt b/idea/testData/compilerMessages/k2js/simpleError/src/test.kt similarity index 94% rename from idea/testData/compilerMessages/simpleError/src/test.kt rename to idea/testData/compilerMessages/k2js/simpleError/src/test.kt index 60cb2075483..278695cc22b 100644 --- a/idea/testData/compilerMessages/simpleError/src/test.kt +++ b/idea/testData/compilerMessages/k2js/simpleError/src/test.kt @@ -2,4 +2,4 @@ package foo fun f() : Boolean { -} \ No newline at end of file +} diff --git a/idea/testData/compilerMessages/k2js/simpleWarning/src/test.kt b/idea/testData/compilerMessages/k2js/simpleWarning/src/test.kt new file mode 100644 index 00000000000..cb90adc9552 --- /dev/null +++ b/idea/testData/compilerMessages/k2js/simpleWarning/src/test.kt @@ -0,0 +1,5 @@ +fun main(args : Array) { + val t = 0 + if (t != null) { + } +} diff --git a/idea/testData/compilerMessages/k2jvm/helloWorld/src/test.kt b/idea/testData/compilerMessages/k2jvm/helloWorld/src/test.kt new file mode 100644 index 00000000000..fcb084924f6 --- /dev/null +++ b/idea/testData/compilerMessages/k2jvm/helloWorld/src/test.kt @@ -0,0 +1,3 @@ +package foo + +fun main(args : Array) {} diff --git a/idea/testData/compilerMessages/k2jvm/simpleError/src/test.kt b/idea/testData/compilerMessages/k2jvm/simpleError/src/test.kt new file mode 100644 index 00000000000..278695cc22b --- /dev/null +++ b/idea/testData/compilerMessages/k2jvm/simpleError/src/test.kt @@ -0,0 +1,5 @@ +package foo + +fun f() : Boolean { + +} diff --git a/idea/testData/compilerMessages/simpleWarning/src/test.kt b/idea/testData/compilerMessages/k2jvm/simpleWarning/src/test.kt similarity index 95% rename from idea/testData/compilerMessages/simpleWarning/src/test.kt rename to idea/testData/compilerMessages/k2jvm/simpleWarning/src/test.kt index c8e08386dd8..4c54b4c192c 100644 --- a/idea/testData/compilerMessages/simpleWarning/src/test.kt +++ b/idea/testData/compilerMessages/k2jvm/simpleWarning/src/test.kt @@ -2,4 +2,4 @@ package foo fun f(s : String) { s!! -} \ No newline at end of file +} diff --git a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/IDECompilerMessagingTest.java b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/IDECompilerMessagingTest.java index 12bcf7defd8..abeec1abfad 100644 --- a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/IDECompilerMessagingTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/IDECompilerMessagingTest.java @@ -16,44 +16,72 @@ package org.jetbrains.jet.plugin.compilerMessages; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.compiler.CompilerManager; import com.intellij.openapi.compiler.TranslatingCompiler; +import com.intellij.openapi.roots.ContentEntry; +import com.intellij.openapi.roots.ModifiableRootModel; +import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.testFramework.PlatformTestCase; import jet.Function1; +import junit.framework.Assert; import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.plugin.PluginTestCaseBase; -import org.jetbrains.jet.plugin.compiler.JetCompiler; import java.io.IOException; -import static org.jetbrains.jet.plugin.compilerMessages.Message.error; -import static org.jetbrains.jet.plugin.compilerMessages.Message.warning; - /** * @author Pavel Talanov */ public abstract class IDECompilerMessagingTest extends PlatformTestCase { - private static final String TEST_DATA_PATH = PluginTestCaseBase.getTestDataPathBase() + "/compilerMessages"; - - - protected void performTest(Function1 whatToExpect, @NotNull TranslatingCompiler compiler) { - String pathToTestDir = TEST_DATA_PATH + "/" + getTestName(true); + protected void performTest(@NotNull Function1 whatToExpect, + @NotNull TranslatingCompiler compiler, @NotNull String testDataPath) { + final String pathToTestDir = testDataPath + "/" + getTestName(true); VirtualFile testDir = LocalFileSystem.getInstance().findFileByPath(pathToTestDir); + Assert.assertNotNull(testDir); VirtualFile sampleFile = LocalFileSystem.getInstance().findFileByPath(pathToTestDir + "/src/test.kt"); VirtualFile outDirectory = getOutDirectory(pathToTestDir, testDir); - VirtualFile root = LocalFileSystem.getInstance().findFileByPath(pathToTestDir + "/src/"); + final String pathToSrc = pathToTestDir + "/src/"; + VirtualFile root = LocalFileSystem.getInstance().findFileByPath(pathToSrc); + Assert.assertNotNull(root); MockCompileContext mockCompileContext = new MockCompileContext(myModule, outDirectory, root); MockModuleChunk mockModuleChunk = new MockModuleChunk(myModule); - compiler - .compile(mockCompileContext, mockModuleChunk, new VirtualFile[] {sampleFile}, new MockOutputSink()); + setSourceEntryForModule(root); + assert sampleFile != null; + compile(compiler, sampleFile, mockCompileContext, mockModuleChunk); + checkMessages(whatToExpect, mockCompileContext); + } + + private void checkMessages(@NotNull Function1 whatToExpect, @NotNull MockCompileContext mockCompileContext) { MessageChecker checker = new MessageChecker(mockCompileContext); checkHeader(checker); whatToExpect.invoke(checker); checker.finish(); } + private static void compile(@NotNull TranslatingCompiler compiler, + @NotNull VirtualFile sampleFile, + @NotNull MockCompileContext mockCompileContext, + @NotNull MockModuleChunk mockModuleChunk) { + compiler.compile(mockCompileContext, mockModuleChunk, new VirtualFile[] {sampleFile}, new MockOutputSink()); + } + + private void setSourceEntryForModule(final VirtualFile root) { + ApplicationManager.getApplication().runWriteAction(new Runnable() { + @Override + public void run() { + ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel(); + ContentEntry entry = model.addContentEntry(root); + entry.addSourceFolder(root, false); + model.commit(); + } + }); + } + + protected abstract void checkHeader(@NotNull MessageChecker checker); + @NotNull private VirtualFile getOutDirectory(@NotNull String pathToTestDir, @NotNull VirtualFile testDir) { VirtualFile outDirectory = LocalFileSystem.getInstance().findFileByPath(pathToTestDir + "/out"); @@ -68,9 +96,9 @@ public abstract class IDECompilerMessagingTest extends PlatformTestCase { return outDirectory; } - private static void checkHeader(@NotNull MessageChecker checker) { - checker.expect(Message.info().textStartsWith("Using kotlinHome=")); - checker.expect(Message.info().textStartsWith("Invoking in-process compiler")); - checker.expect(Message.info().textStartsWith("Kotlin Compiler version")); + protected T getCompiler(Class aClass) { + T[] compilers = CompilerManager.getInstance(getProject()).getCompilers(aClass); + assert compilers.length == 1; + return compilers[0]; } } diff --git a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/JetCompilerMessagingTest.java b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/JetCompilerMessagingTest.java index 76446a2a00e..0394d11d69a 100644 --- a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/JetCompilerMessagingTest.java +++ b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/JetCompilerMessagingTest.java @@ -18,6 +18,7 @@ package org.jetbrains.jet.plugin.compilerMessages; import jet.Function1; import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.PluginTestCaseBase; import org.jetbrains.jet.plugin.compiler.JetCompiler; import static org.jetbrains.jet.plugin.compilerMessages.Message.error; @@ -28,42 +29,49 @@ import static org.jetbrains.jet.plugin.compilerMessages.Message.warning; */ public final class JetCompilerMessagingTest extends IDECompilerMessagingTest { + private static final String TEST_DATA_PATH = PluginTestCaseBase.getTestDataPathBase() + "/compilerMessages/k2jvm"; public void testHelloWorld() { - //doTest(new Function1() { - // @Override - // public Void invoke(MessageChecker checker) { - // //nothing apart from header - // return null; - // } - //}); + doTest(new Function1() { + @Override + public Void invoke(MessageChecker checker) { + //nothing apart from header + return null; + } + }); } public void testSimpleWarning() { - //doTest(new Function1() { - // @Override - // public Void invoke(MessageChecker checker) { - // checker.expect(warning().text("Unnecessary non-null assertion (!!) on a non-null receiver of type jet.String") - // .at("test.kt", 4, 4)); - // return null; - // } - //}); + doTest(new Function1() { + @Override + public Void invoke(MessageChecker checker) { + checker.expect(warning().text("Unnecessary non-null assertion (!!) on a non-null receiver of type jet.String") + .at("test.kt", 4, 4)); + return null; + } + }); } public void testSimpleError() { - //doTest(new Function1() { - // @Override - // public Void invoke(MessageChecker checker) { - // checker.expect( - // error().text("A 'return' expression required in a function with a block body ('{...}')").at("test.kt", 5, 1)); - // return null; - // } - //}); + doTest(new Function1() { + @Override + public Void invoke(MessageChecker checker) { + checker.expect( + error().text("A 'return' expression required in a function with a block body ('{...}')").at("test.kt", 5, 1)); + return null; + } + }); } private void doTest(@NotNull Function1 whatToExpect) { - JetCompiler compiler = new JetCompiler(); - performTest(whatToExpect, compiler); + performTest(whatToExpect, getCompiler(JetCompiler.class), TEST_DATA_PATH); + } + + @Override + protected void checkHeader(@NotNull MessageChecker checker) { + checker.expect(Message.info().textStartsWith("Using kotlinHome=")); + checker.expect(Message.info().textStartsWith("Invoking in-process compiler")); + checker.expect(Message.info().textStartsWith("Kotlin Compiler version")); } } diff --git a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/K2JSCompilerMessagingTest.java b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/K2JSCompilerMessagingTest.java new file mode 100644 index 00000000000..cb53b4f1777 --- /dev/null +++ b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/K2JSCompilerMessagingTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * 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.jetbrains.jet.plugin.compilerMessages; + +import jet.Function1; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.plugin.PluginTestCaseBase; +import org.jetbrains.jet.plugin.compiler.K2JSCompiler; +import org.jetbrains.jet.plugin.project.K2JSModuleComponent; + +import static org.jetbrains.jet.plugin.compilerMessages.Message.error; +import static org.jetbrains.jet.plugin.compilerMessages.Message.warning; + +/** + * @author Pavel Talanov + */ +public final class K2JSCompilerMessagingTest extends IDECompilerMessagingTest { + + private static final String TEST_DATA_PATH = PluginTestCaseBase.getTestDataPathBase() + "/compilerMessages/k2js"; + + public void testHelloWorld() { + doTest(new Function1() { + @Override + public Void invoke(MessageChecker checker) { + //nothing apart from header + return null; + } + }); + } + + public void testSimpleWarning() { + doTest(new Function1() { + @Override + public Void invoke(MessageChecker checker) { + checker.expect(warning().text("Condition 't != null' is always 'true'") + .at("test.kt", 3, 7)); + return null; + } + }); + } + + public void testSimpleError() { + doTest(new Function1() { + @Override + public Void invoke(MessageChecker checker) { + checker.expect( + error().text("A 'return' expression required in a function with a block body ('{...}')").at("test.kt", 5, 1)); + return null; + } + }); + } + + public void testLib() { + K2JSModuleComponent component = myModule.getComponent(K2JSModuleComponent.class); + component.setJavaScriptModule(true); + component.setPathToJavaScriptLibrary("/lib.zip"); + doTest(new Function1() { + @Override + public Void invoke(MessageChecker checker) { + //nothing apart from header + return null; + } + }); + } + + private void doTest(@NotNull Function1 whatToExpect) { + performTest(whatToExpect, getCompiler(K2JSCompiler.class), TEST_DATA_PATH); + } + + @Override + protected void checkHeader(@NotNull MessageChecker checker) { + checker.expect(Message.info().textStartsWith("Kotlin Compiler version")); + } +} diff --git a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/Message.java b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/Message.java index 7e87696cac7..665592364d4 100644 --- a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/Message.java +++ b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/Message.java @@ -81,10 +81,10 @@ public final class Message { } public void check(@NotNull Message other) { - Assert.assertEquals(this.category, other.category); checkMessages(other); - Assert.assertEquals(this.line, other.line); - Assert.assertEquals(this.column, other.column); + Assert.assertEquals(other.category, this.category); + Assert.assertEquals(other.line, this.line); + Assert.assertEquals(other.column, this.column); if (this.url != null) { Assert.assertTrue(other.url.endsWith(this.url)); } diff --git a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MessageChecker.java b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MessageChecker.java index 03a9af7914f..99e2ac2a8c8 100644 --- a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MessageChecker.java +++ b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MessageChecker.java @@ -17,12 +17,11 @@ package org.jetbrains.jet.plugin.compilerMessages; import org.jetbrains.annotations.NotNull; -import org.junit.Assert; import java.util.Iterator; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Pavel Talanov @@ -37,12 +36,14 @@ public final class MessageChecker { @NotNull public MessageChecker expect(@NotNull Message message) { - assertTrue("More messages expected", iterator.hasNext()); + assertTrue("More messages expected:\n" + message.message, iterator.hasNext()); message.check(iterator.next()); return this; } public void finish() { - assertFalse("More message than expected", iterator.hasNext()); + if (iterator.hasNext()) { + fail("More message than expected:\n" + iterator.next().message); + } } } diff --git a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MockCompileContext.java b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MockCompileContext.java index cdadd26dde8..67a58396df7 100644 --- a/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MockCompileContext.java +++ b/idea/tests/org/jetbrains/jet/plugin/compilerMessages/MockCompileContext.java @@ -53,8 +53,8 @@ public class MockCompileContext implements CompileContextEx { public MockCompileContext(@NotNull Module module, @NotNull VirtualFile outputDir, @NotNull VirtualFile root) { this.module = module; - outputDirectory = outputDir; - sourceRoot = root; + this.outputDirectory = outputDir; + this.sourceRoot = root; } @NotNull @@ -209,7 +209,7 @@ public class MockCompileContext implements CompileContextEx { @Override public Project getProject() { - throw new UnsupportedOperationException("org.jetbrains.jet.plugin.compilerMessages.MockCompileContext#getProject"); + return module.getProject(); } @Override