Speeding up codegen tests
This commit is contained in:
@@ -170,9 +170,10 @@ public class JetTestUtils {
|
|||||||
deleteOnShutdown(answer);
|
deleteOnShutdown(answer);
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File tmpDir(String name) throws IOException {
|
public static File tmpDir(String name) throws IOException {
|
||||||
File answer = FileUtil.createTempDirectory(name, "");
|
// we should use this form. otherwise directory will be deleted on each test
|
||||||
|
File answer = FileUtil.createTempDirectory(new File(System.getProperty("java.io.tmpdir")), name, "");
|
||||||
deleteOnShutdown(answer);
|
deleteOnShutdown(answer);
|
||||||
return answer;
|
return answer;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ public class ClassGenTest extends CodegenTestCase {
|
|||||||
public void testAbstractClass() throws Exception {
|
public void testAbstractClass() throws Exception {
|
||||||
loadText("abstract class SimpleClass() { }");
|
loadText("abstract class SimpleClass() { }");
|
||||||
|
|
||||||
final Class aClass = loadAllClasses(generateClassesInFile()).get("SimpleClass");
|
final Class aClass = createClassLoader(generateClassesInFile()).loadClass("SimpleClass");
|
||||||
assertTrue((aClass.getModifiers() & Modifier.ABSTRACT) != 0);
|
assertTrue((aClass.getModifiers() & Modifier.ABSTRACT) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,7 +148,7 @@ public class ClassGenTest extends CodegenTestCase {
|
|||||||
|
|
||||||
public void testEnumClass() throws Exception {
|
public void testEnumClass() throws Exception {
|
||||||
loadText("enum class Direction { NORTH; SOUTH; EAST; WEST }");
|
loadText("enum class Direction { NORTH; SOUTH; EAST; WEST }");
|
||||||
final Class direction = loadAllClasses(generateClassesInFile()).get("Direction");
|
final Class direction = createClassLoader(generateClassesInFile()).loadClass("Direction");
|
||||||
// System.out.println(generateToText());
|
// System.out.println(generateToText());
|
||||||
final Field north = direction.getField("NORTH");
|
final Field north = direction.getField("NORTH");
|
||||||
assertEquals(direction, north.getType());
|
assertEquals(direction, north.getType());
|
||||||
@@ -157,7 +157,7 @@ public class ClassGenTest extends CodegenTestCase {
|
|||||||
|
|
||||||
public void testEnumConstantConstructors() throws Exception {
|
public void testEnumConstantConstructors() throws Exception {
|
||||||
loadText("enum class Color(val rgb: Int) { RED: Color(0xFF0000); GREEN: Color(0x00FF00); }");
|
loadText("enum class Color(val rgb: Int) { RED: Color(0xFF0000); GREEN: Color(0x00FF00); }");
|
||||||
final Class colorClass = loadAllClasses(generateClassesInFile()).get("Color");
|
final Class colorClass = createClassLoader(generateClassesInFile()).loadClass("Color");
|
||||||
final Field redField = colorClass.getField("RED");
|
final Field redField = colorClass.getField("RED");
|
||||||
final Object redValue = redField.get(null);
|
final Object redValue = redField.get(null);
|
||||||
final Method rgbMethod = colorClass.getMethod("getRgb");
|
final Method rgbMethod = colorClass.getMethod("getRgb");
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.jetbrains.jet.parsing.JetParsingTest;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -20,7 +21,6 @@ import java.util.Map;
|
|||||||
* @author yole
|
* @author yole
|
||||||
*/
|
*/
|
||||||
public abstract class CodegenTestCase extends JetLiteFixture {
|
public abstract class CodegenTestCase extends JetLiteFixture {
|
||||||
private MyClassLoader myClassLoader;
|
|
||||||
|
|
||||||
protected static void assertThrows(Method foo, Class<? extends Throwable> exceptionClass, Object instance, Object... args) throws IllegalAccessException {
|
protected static void assertThrows(Method foo, Class<? extends Throwable> exceptionClass, Object instance, Object... args) throws IllegalAccessException {
|
||||||
boolean caught = false;
|
boolean caught = false;
|
||||||
@@ -36,13 +36,11 @@ public abstract class CodegenTestCase extends JetLiteFixture {
|
|||||||
@Override
|
@Override
|
||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
myClassLoader = new MyClassLoader(NamespaceGenTest.class.getClassLoader());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void tearDown() throws Exception {
|
protected void tearDown() throws Exception {
|
||||||
myFile = null;
|
myFile = null;
|
||||||
myClassLoader = null;
|
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +87,7 @@ public abstract class CodegenTestCase extends JetLiteFixture {
|
|||||||
|
|
||||||
protected String blackBox() throws Exception {
|
protected String blackBox() throws Exception {
|
||||||
ClassFileFactory codegens = generateClassesInFile();
|
ClassFileFactory codegens = generateClassesInFile();
|
||||||
GeneratedClassLoader loader = new GeneratedClassLoader(codegens);
|
GeneratedClassLoader loader = createClassLoader(codegens);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String fqName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(myFile), true).replace("/", ".");
|
String fqName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(myFile), true).replace("/", ".");
|
||||||
@@ -101,6 +99,10 @@ public abstract class CodegenTestCase extends JetLiteFixture {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) throws MalformedURLException {
|
||||||
|
return new GeneratedClassLoader(codegens);
|
||||||
|
}
|
||||||
|
|
||||||
protected String generateToText() {
|
protected String generateToText() {
|
||||||
GenerationState state = new GenerationState(getProject(), ClassBuilderFactory.TEXT);
|
GenerationState state = new GenerationState(getProject(), ClassBuilderFactory.TEXT);
|
||||||
AnalyzingUtils.checkForSyntacticErrors(myFile);
|
AnalyzingUtils.checkForSyntacticErrors(myFile);
|
||||||
@@ -121,34 +123,27 @@ public abstract class CodegenTestCase extends JetLiteFixture {
|
|||||||
|
|
||||||
protected Class loadRootNamespaceClass(@NotNull ClassFileFactory state) {
|
protected Class loadRootNamespaceClass(@NotNull ClassFileFactory state) {
|
||||||
String fqName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(myFile), true).replace("/", ".");
|
String fqName = NamespaceCodegen.getJVMClassName(JetPsiUtil.getFQName(myFile), true).replace("/", ".");
|
||||||
Map<String, Class> classMap = loadAllClasses(state);
|
try {
|
||||||
return classMap.get(fqName);
|
return createClassLoader(state).loadClass(fqName);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Class loadClass(String fqName, @NotNull ClassFileFactory state) {
|
protected Class loadClass(String fqName, @NotNull ClassFileFactory state) {
|
||||||
List<String> files = state.files();
|
try {
|
||||||
for (String file : files) {
|
return createClassLoader(state).loadClass(fqName);
|
||||||
if (file.equals(fqName.replace('.', '/') + ".class")) {
|
} catch (ClassNotFoundException e) {
|
||||||
final byte[] data = state.asBytes(file);
|
} catch (MalformedURLException e) {
|
||||||
return myClassLoader.doDefineClass(fqName, data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fail("No classfile was generated for: " + fqName);
|
fail("No classfile was generated for: " + fqName);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<String, Class> loadAllClasses(@NotNull ClassFileFactory state) {
|
|
||||||
Map<String, Class> result = new HashMap<String, Class>();
|
|
||||||
for (String fileName : state.files()) {
|
|
||||||
String className = StringUtil.trimEnd(fileName, ".class").replace('/', '.');
|
|
||||||
byte[] data = state.asBytes(fileName);
|
|
||||||
Class aClass = myClassLoader.doDefineClass(className, data);
|
|
||||||
result.put(className, aClass);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
protected ClassFileFactory generateClassesInFile() {
|
protected ClassFileFactory generateClassesInFile() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
package org.jetbrains.jet.codegen;
|
package org.jetbrains.jet.codegen;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.jet.compiler.CompileEnvironment;
|
import org.jetbrains.jet.compiler.CompileEnvironment;
|
||||||
import org.jetbrains.jet.compiler.CompileSession;
|
import org.jetbrains.jet.compiler.CompileSession;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author alex.tkachman
|
* @author alex.tkachman
|
||||||
@@ -14,46 +18,16 @@ public class StdlibTest extends CodegenTestCase {
|
|||||||
protected void setUp() throws Exception {
|
protected void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
createEnvironmentWithFullJdk();
|
createEnvironmentWithFullJdk();
|
||||||
}
|
myEnvironment.addToClasspath(ForTestCompileStdlib.stdlibJarForTests());
|
||||||
|
CompileEnvironment.ensureRuntime(myEnvironment);
|
||||||
protected String generateToText() {
|
|
||||||
CompileSession session = new CompileSession(myEnvironment);
|
|
||||||
|
|
||||||
session.addSources(myFile.getVirtualFile());
|
|
||||||
try {
|
|
||||||
session.addStdLibSources(true);
|
|
||||||
} catch (Throwable e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!session.analyze(System.out)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return session.generateText();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@Override
|
||||||
protected ClassFileFactory generateClassesInFile() {
|
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) throws MalformedURLException {
|
||||||
try {
|
return new GeneratedClassLoader(
|
||||||
CompileSession session = new CompileSession(myEnvironment);
|
codegens,
|
||||||
|
new URLClassLoader(new URL[]{ForTestCompileStdlib.stdlibJarForTests().toURI().toURL()},
|
||||||
session.addStdLibSources(true);
|
getClass().getClassLoader()));
|
||||||
session.addSources(myFile.getVirtualFile());
|
|
||||||
|
|
||||||
CompileEnvironment.ensureRuntime(myEnvironment);
|
|
||||||
|
|
||||||
if (!session.analyze(System.out)) {
|
|
||||||
throw new RuntimeException();
|
|
||||||
}
|
|
||||||
|
|
||||||
return session.generate();
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
System.out.println(generateToText());
|
|
||||||
throw e;
|
|
||||||
} catch (Throwable e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testKt533 () {
|
public void testKt533 () {
|
||||||
|
|||||||
Reference in New Issue
Block a user