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 (!t9(0)) return "t9 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"
return "OK"
}
}
@@ -133,20 +133,7 @@ public abstract class CodegenTestCase extends UsefulTestCase {
protected void blackBoxFile(String filename) {
loadFile(filename);
Object actual;
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);
blackBox();
}
@NotNull
@@ -188,11 +175,12 @@ public abstract class CodegenTestCase extends UsefulTestCase {
}
}
@NotNull
protected String blackBox() throws Exception {
protected void blackBox() {
GenerationState state = generateClassesInFileGetState();
GeneratedClassLoader loader = createClassLoader(state.getFactory());
String r;
try {
if (myFile.getPsiFile().isScript()) {
Class<?> scriptClass = loader.loadClass("Script");
@@ -202,23 +190,38 @@ public abstract class CodegenTestCase extends UsefulTestCase {
Field field = scriptClass.getDeclaredField("rv");
field.setAccessible(true);
Object result = field.get(scriptInstance);
return result != null ? result.toString() : "null";
r = result != null ? result.toString() : "null";
}
else {
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile.getPsiFile())).getFqName().getFqName();
Class<?> namespaceClass = loader.loadClass(fqName);
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 {
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();
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());
return new GeneratedClassLoader(codegens, parentClassLoader);
@@ -255,8 +258,6 @@ public abstract class CodegenTestCase extends UsefulTestCase {
return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
@@ -265,7 +266,6 @@ public abstract class CodegenTestCase extends UsefulTestCase {
try {
return createClassLoader(state).loadClass(fqName);
} catch (ClassNotFoundException e) {
} catch (MalformedURLException e) {
}
fail("No classfile was generated for: " + fqName);
@@ -337,7 +337,7 @@ public class PrimitiveTypesTest extends CodegenTestCase {
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\"");
assertEquals("OK", blackBox());
blackBox();
}
public void testKt665() throws Exception {
@@ -47,11 +47,15 @@ public class StdlibTest extends CodegenTestCase {
}
@Override
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) throws MalformedURLException {
return new GeneratedClassLoader(
codegens,
new URLClassLoader(new URL[]{ForTestCompileRuntime.runtimeJarForTests().toURI().toURL()},
getClass().getClassLoader()));
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) {
try {
return new GeneratedClassLoader(
codegens,
new URLClassLoader(new URL[]{ForTestCompileRuntime.runtimeJarForTests().toURI().toURL()},
getClass().getClassLoader()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public void testKt533 () {