JetCompilerMessagingTest and K2JSCompilerMessagingTest: tests that launch the cli compiler and check that resulting messages are in correct format

This commit is contained in:
pTalanov
2012-06-15 20:12:32 +04:00
parent af307e3c6b
commit 2e531ee809
16 changed files with 205 additions and 59 deletions
@@ -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);
@@ -1,4 +0,0 @@
package foo
fun f() {
}
@@ -0,0 +1,3 @@
package foo
fun main(args : Array<String>) {}
Binary file not shown.
@@ -0,0 +1,8 @@
package foo
import js.*
fun main(args : Array<String>) {
val c = 4 + libf()
c == 2
}
@@ -2,4 +2,4 @@ package foo
fun f() : Boolean {
}
}
@@ -0,0 +1,5 @@
fun main(args : Array<String>) {
val t = 0
if (t != null) {
}
}
@@ -0,0 +1,3 @@
package foo
fun main(args : Array<String>) {}
@@ -0,0 +1,5 @@
package foo
fun f() : Boolean {
}
@@ -2,4 +2,4 @@ package foo
fun f(s : String) {
s!!
}
}
@@ -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<MessageChecker, Void> whatToExpect, @NotNull TranslatingCompiler compiler) {
String pathToTestDir = TEST_DATA_PATH + "/" + getTestName(true);
protected void performTest(@NotNull Function1<MessageChecker, Void> 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<MessageChecker, Void> 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 extends TranslatingCompiler> T getCompiler(Class<T> aClass) {
T[] compilers = CompilerManager.getInstance(getProject()).getCompilers(aClass);
assert compilers.length == 1;
return compilers[0];
}
}
@@ -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<MessageChecker, Void>() {
// @Override
// public Void invoke(MessageChecker checker) {
// //nothing apart from header
// return null;
// }
//});
doTest(new Function1<MessageChecker, Void>() {
@Override
public Void invoke(MessageChecker checker) {
//nothing apart from header
return null;
}
});
}
public void testSimpleWarning() {
//doTest(new Function1<MessageChecker, Void>() {
// @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<MessageChecker, Void>() {
@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<MessageChecker, Void>() {
// @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<MessageChecker, Void>() {
@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<MessageChecker, Void> 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"));
}
}
@@ -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<MessageChecker, Void>() {
@Override
public Void invoke(MessageChecker checker) {
//nothing apart from header
return null;
}
});
}
public void testSimpleWarning() {
doTest(new Function1<MessageChecker, Void>() {
@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<MessageChecker, Void>() {
@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<MessageChecker, Void>() {
@Override
public Void invoke(MessageChecker checker) {
//nothing apart from header
return null;
}
});
}
private void doTest(@NotNull Function1<MessageChecker, Void> whatToExpect) {
performTest(whatToExpect, getCompiler(K2JSCompiler.class), TEST_DATA_PATH);
}
@Override
protected void checkHeader(@NotNull MessageChecker checker) {
checker.expect(Message.info().textStartsWith("Kotlin Compiler version"));
}
}
@@ -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));
}
@@ -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);
}
}
}
@@ -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