CodeGenTest: check result and print code in any blackbox

... and fix test
This commit is contained in:
Stepan Koltsov
2012-06-05 22:56:25 +04:00
parent d33c8638d8
commit 9a71cb5e72
4 changed files with 36 additions and 32 deletions
@@ -186,8 +186,8 @@ fun box(): String {
if (!t8()) return "t8 fail" if (!t8()) return "t8 fail"
if (!t9(0)) return "t9 fail" if (!t9(0)) return "t9 fail"
if (!t10()) return "t10 fail" if (!t10()) return "t10 fail"
if (t11(1) != 104) return "t11 fail" if (t11(1) != 101) return "t11 fail"
if (t12(0) != 100) return "t12 fail" if (t12(0) != 100) return "t12 fail"
return "OK" return "OK"
} }
@@ -133,20 +133,7 @@ public abstract class CodegenTestCase extends UsefulTestCase {
protected void blackBoxFile(String filename) { protected void blackBoxFile(String filename) {
loadFile(filename); loadFile(filename);
Object actual; blackBox();
try {
actual = blackBox();
} catch (NoClassDefFoundError e) {
System.out.println(generateToText());
throw e;
} catch (Throwable e) {
System.out.println(generateToText());
throw new RuntimeException(e);
}
if (!Objects.equal(myFile.getExpectedValue(), actual)) {
System.out.println(generateToText());
}
assertEquals(myFile.getExpectedValue(), actual);
} }
@NotNull @NotNull
@@ -188,11 +175,12 @@ public abstract class CodegenTestCase extends UsefulTestCase {
} }
} }
@NotNull protected void blackBox() {
protected String blackBox() throws Exception {
GenerationState state = generateClassesInFileGetState(); GenerationState state = generateClassesInFileGetState();
GeneratedClassLoader loader = createClassLoader(state.getFactory()); GeneratedClassLoader loader = createClassLoader(state.getFactory());
String r;
try { try {
if (myFile.getPsiFile().isScript()) { if (myFile.getPsiFile().isScript()) {
Class<?> scriptClass = loader.loadClass("Script"); Class<?> scriptClass = loader.loadClass("Script");
@@ -202,23 +190,38 @@ public abstract class CodegenTestCase extends UsefulTestCase {
Field field = scriptClass.getDeclaredField("rv"); Field field = scriptClass.getDeclaredField("rv");
field.setAccessible(true); field.setAccessible(true);
Object result = field.get(scriptInstance); Object result = field.get(scriptInstance);
return result != null ? result.toString() : "null"; r = result != null ? result.toString() : "null";
} }
else { else {
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile.getPsiFile())).getFqName().getFqName(); String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile.getPsiFile())).getFqName().getFqName();
Class<?> namespaceClass = loader.loadClass(fqName); Class<?> namespaceClass = loader.loadClass(fqName);
Method method = namespaceClass.getMethod("box"); Method method = namespaceClass.getMethod("box");
return (String) method.invoke(null); r = (String) method.invoke(null);
} }
} catch (NoClassDefFoundError e) {
System.out.println(generateToText());
throw e;
} catch (Throwable e) {
System.out.println(generateToText());
throw new RuntimeException(e);
} finally { } finally {
loader.dispose(); loader.dispose();
} }
if (!Objects.equal(myFile.getExpectedValue(), r)) {
System.out.println(generateToText());
}
assertEquals(myFile.getExpectedValue(), r);
} }
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) throws MalformedURLException { protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) {
List<URL> urls = Lists.newArrayList(); List<URL> urls = Lists.newArrayList();
for (File file : extraClasspath) { for (File file : extraClasspath) {
urls.add(file.toURI().toURL()); try {
urls.add(file.toURI().toURL());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
} }
ClassLoader parentClassLoader = new URLClassLoader(urls.toArray(new URL[0]), CodegenTestCase.class.getClassLoader()); ClassLoader parentClassLoader = new URLClassLoader(urls.toArray(new URL[0]), CodegenTestCase.class.getClassLoader());
return new GeneratedClassLoader(codegens, parentClassLoader); return new GeneratedClassLoader(codegens, parentClassLoader);
@@ -255,8 +258,6 @@ public abstract class CodegenTestCase extends UsefulTestCase {
return createClassLoader(state).loadClass(fqName); return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} }
return null; return null;
} }
@@ -265,7 +266,6 @@ public abstract class CodegenTestCase extends UsefulTestCase {
try { try {
return createClassLoader(state).loadClass(fqName); return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
} catch (MalformedURLException e) {
} }
fail("No classfile was generated for: " + fqName); fail("No classfile was generated for: " + fqName);
@@ -337,7 +337,7 @@ public class PrimitiveTypesTest extends CodegenTestCase {
public void testKt737() throws Exception { public void testKt737() throws Exception {
loadText("fun box() = if(3.compareTo(2) != 1) \"fail\" else if(5.toByte().compareTo(10.toLong()) >= 0) \"fail\" else \"OK\""); loadText("fun box() = if(3.compareTo(2) != 1) \"fail\" else if(5.toByte().compareTo(10.toLong()) >= 0) \"fail\" else \"OK\"");
assertEquals("OK", blackBox()); blackBox();
} }
public void testKt665() throws Exception { public void testKt665() throws Exception {
@@ -47,11 +47,15 @@ public class StdlibTest extends CodegenTestCase {
} }
@Override @Override
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) throws MalformedURLException { protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) {
return new GeneratedClassLoader( try {
codegens, return new GeneratedClassLoader(
new URLClassLoader(new URL[]{ForTestCompileRuntime.runtimeJarForTests().toURI().toURL()}, codegens,
getClass().getClassLoader())); new URLClassLoader(new URL[]{ForTestCompileRuntime.runtimeJarForTests().toURI().toURL()},
getClass().getClassLoader()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
} }
public void testKt533 () { public void testKt533 () {