test for bytecode generation of kotlin code against descriptors loaded from binary classes
test is: 1. compile file A 2. compile file B with binary class A in classpath 3. execute some code from file B
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
package aaa
|
||||
|
||||
fun hello() = 17
|
||||
@@ -0,0 +1,9 @@
|
||||
package bbb
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val h = aaa.hello()
|
||||
if (h != 17) {
|
||||
throw Exception()
|
||||
}
|
||||
System.out?.println("It is 17!")
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package org.jetbrains.jet.compiler;
|
||||
|
||||
import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
import com.intellij.psi.impl.PsiFileFactoryImpl;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.JetTestCaseBuilder;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.codegen.ClassBuilderFactory;
|
||||
import org.jetbrains.jet.codegen.ClassFileFactory;
|
||||
import org.jetbrains.jet.codegen.GenerationState;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
/**
|
||||
* @author Stepan Koltsov
|
||||
*/
|
||||
public class CompileKotlinAgainstKotlinTest extends TestCaseWithTmpdir {
|
||||
|
||||
private final File ktAFile;
|
||||
private final File ktBFile;
|
||||
|
||||
public CompileKotlinAgainstKotlinTest(File ktAFile) {
|
||||
Assert.assertTrue(ktAFile.getName().endsWith("A.kt"));
|
||||
this.ktAFile = ktAFile;
|
||||
this.ktBFile = new File(ktAFile.getPath().replaceFirst("A\\.kt$", "B.kt"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return ktAFile.getName();
|
||||
}
|
||||
|
||||
private File aDir;
|
||||
private File bDir;
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
aDir = new File(tmpdir, "a");
|
||||
bDir = new File(tmpdir, "b");
|
||||
JetTestUtils.mkdirs(aDir);
|
||||
JetTestUtils.mkdirs(bDir);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runTest() throws Throwable {
|
||||
compileA();
|
||||
compileB();
|
||||
|
||||
URLClassLoader classLoader = new URLClassLoader(new URL[]{ aDir.toURI().toURL(), bDir.toURI().toURL() });
|
||||
Class<?> clazz = classLoader.loadClass("bbb.namespace");
|
||||
Method main = clazz.getMethod("main", new Class[] { String[].class });
|
||||
main.invoke(null, new Object[] { new String[0] });
|
||||
}
|
||||
|
||||
private void compileA() throws IOException {
|
||||
JetCoreEnvironment jetCoreEnvironment = JetTestUtils.createEnvironmentWithMockJdk(myTestRootDisposable);
|
||||
|
||||
String text = FileUtil.loadFile(ktAFile);
|
||||
|
||||
LightVirtualFile virtualFile = new LightVirtualFile(ktAFile.getName(), JetLanguage.INSTANCE, text);
|
||||
virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
|
||||
JetFile psiFile = (JetFile) ((PsiFileFactoryImpl) PsiFileFactory.getInstance(jetCoreEnvironment.getProject())).trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
|
||||
|
||||
GenerationState state = new GenerationState(jetCoreEnvironment.getProject(), ClassBuilderFactory.BINARIES);
|
||||
AnalyzingUtils.checkForSyntacticErrors(psiFile);
|
||||
|
||||
state.compile(psiFile);
|
||||
|
||||
ClassFileFactory classFileFactory = state.getFactory();
|
||||
|
||||
CompileEnvironment.writeToOutputDirectory(classFileFactory, aDir.getPath());
|
||||
|
||||
Disposer.dispose(myTestRootDisposable);
|
||||
}
|
||||
|
||||
private void compileB() throws IOException {
|
||||
JetCoreEnvironment jetCoreEnvironment = JetTestUtils.createEnvironmentWithMockJdk(myTestRootDisposable);
|
||||
|
||||
jetCoreEnvironment.addToClasspath(aDir);
|
||||
|
||||
String text = FileUtil.loadFile(ktBFile);
|
||||
|
||||
LightVirtualFile virtualFile = new LightVirtualFile(ktAFile.getName(), JetLanguage.INSTANCE, text);
|
||||
virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
|
||||
JetFile psiFile = (JetFile) ((PsiFileFactoryImpl) PsiFileFactory.getInstance(jetCoreEnvironment.getProject())).trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
|
||||
|
||||
GenerationState state = new GenerationState(jetCoreEnvironment.getProject(), ClassBuilderFactory.BINARIES);
|
||||
AnalyzingUtils.checkForSyntacticErrors(psiFile);
|
||||
state.compile(psiFile);
|
||||
|
||||
ClassFileFactory classFileFactory = state.getFactory();
|
||||
|
||||
CompileEnvironment.writeToOutputDirectory(classFileFactory, bDir.getPath());
|
||||
|
||||
Disposer.dispose(myTestRootDisposable);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
class Filter implements FilenameFilter {
|
||||
@Override
|
||||
public boolean accept(File dir, String name) {
|
||||
return name.endsWith("A.kt");
|
||||
}
|
||||
}
|
||||
return JetTestCaseBuilder.suiteForDirectory(JetTestCaseBuilder.getTestDataPathBase(), "/compileKotlinAgainstKotlin", true, new Filter(), new JetTestCaseBuilder.NamedTestFactory() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Test createTest(@NotNull String dataPath, @NotNull String name, @NotNull File file) {
|
||||
return new CompileKotlinAgainstKotlinTest(file);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user