Refactor CodegenTestCase
- prohibit main(Array<String>) in favor of box(): String - move all script-related code to ScriptGenTest - remove unused environment-creating methods - inline trivial methods & other minor stuff
This commit is contained in:
@@ -16,23 +16,24 @@
|
||||
|
||||
package org.jetbrains.jet.codegen;
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.asm4.Opcodes;
|
||||
import org.jetbrains.asm4.Type;
|
||||
import org.jetbrains.jet.ConfigurationKind;
|
||||
import org.jetbrains.jet.lang.parsing.JetParserDefinition;
|
||||
import org.jetbrains.jet.codegen.state.GenerationState;
|
||||
import org.jetbrains.jet.lang.parsing.JetScriptDefinition;
|
||||
import org.jetbrains.jet.lang.parsing.JetScriptDefinitionProvider;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
|
||||
import org.jetbrains.jet.lang.resolve.ScriptNameUtil;
|
||||
import org.jetbrains.jet.utils.ExceptionUtils;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class ScriptGenTest extends CodegenTestCase {
|
||||
protected Object scriptInstance;
|
||||
|
||||
public static final JetScriptDefinition FIB_SCRIPT_DEFINITION =
|
||||
new JetScriptDefinition(".lang.kt", new AnalyzerScriptParameter("num", "jet.Int"));
|
||||
@@ -43,59 +44,146 @@ public class ScriptGenTest extends CodegenTestCase {
|
||||
createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
scriptInstance = null;
|
||||
}
|
||||
|
||||
private void blackBoxScript(String filename) {
|
||||
loadFile(filename);
|
||||
|
||||
GenerationState state = generateClassesInFileGetState();
|
||||
|
||||
GeneratedClassLoader loader = createClassLoader(state.getFactory(), false);
|
||||
|
||||
String scriptClassName = ScriptNameUtil.classNameForScript(myFiles.getPsiFile());
|
||||
|
||||
try {
|
||||
Class<?> scriptClass = loader.loadClass(scriptClassName);
|
||||
|
||||
Constructor constructor = getConstructor(scriptClass, state.getScriptCodegen().getScriptConstructorMethod());
|
||||
scriptInstance = constructor.newInstance(myFiles.getScriptParameterValues().toArray());
|
||||
|
||||
assertFalse("expecting at least one expectation", myFiles.getExpectedValues().isEmpty());
|
||||
|
||||
for (Pair<String, String> nameValue : myFiles.getExpectedValues()) {
|
||||
String fieldName = nameValue.first;
|
||||
String expectedValue = nameValue.second;
|
||||
|
||||
if (expectedValue.equals("<nofield>")) {
|
||||
try {
|
||||
scriptClass.getDeclaredField(fieldName);
|
||||
fail("must have no field " + fieldName);
|
||||
} catch (NoSuchFieldException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Field field = scriptClass.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
Object result = field.get(scriptInstance);
|
||||
String resultString = result != null ? result.toString() : "null";
|
||||
assertEquals("comparing field " + fieldName, expectedValue, resultString);
|
||||
}
|
||||
}
|
||||
catch (Throwable e) {
|
||||
System.out.println(generateToText());
|
||||
ExceptionUtils.rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected Constructor getConstructor(@NotNull Class<?> clazz, org.jetbrains.asm4.commons.Method method) {
|
||||
if (!method.getName().equals("<init>")) {
|
||||
throw new IllegalArgumentException("not constructor: " + method);
|
||||
}
|
||||
Class[] classes = new Class[method.getArgumentTypes().length];
|
||||
for (int i = 0; i < classes.length; ++i) {
|
||||
classes[i] = loadClassFromType(method.getArgumentTypes()[i]);
|
||||
}
|
||||
try {
|
||||
return clazz.getConstructor(classes);
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Class<?> loadClassFromType(@NotNull Type type) {
|
||||
try {
|
||||
switch (type.getSort()) {
|
||||
case Type.OBJECT:
|
||||
return Class.forName(type.getClassName());
|
||||
case Type.INT:
|
||||
return int.class;
|
||||
case Type.LONG:
|
||||
return long.class;
|
||||
default:
|
||||
// AFAIK there is no way to create array class from class
|
||||
if (type.getDescriptor().equals("[Ljava/lang/String;")) {
|
||||
return String[].class;
|
||||
}
|
||||
throw new IllegalStateException("not implemented: " + type.getDescriptor());
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw ExceptionUtils.rethrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void testHelloWorld() {
|
||||
blackBoxFile("script/helloWorld.ktscript");
|
||||
blackBoxScript("script/helloWorld.ktscript");
|
||||
}
|
||||
|
||||
public void testString() {
|
||||
blackBoxFile("script/string.ktscript");
|
||||
blackBoxScript("script/string.ktscript");
|
||||
}
|
||||
|
||||
public void testTopLevelFunction() throws Exception {
|
||||
blackBoxFile("script/topLevelFunction.ktscript");
|
||||
blackBoxScript("script/topLevelFunction.ktscript");
|
||||
Method method = scriptInstance.getClass().getMethod("factorial", new Class<?>[]{ int.class });
|
||||
Object r = method.invoke(scriptInstance, 4);
|
||||
assertEquals(24, r);
|
||||
}
|
||||
|
||||
public void testTopLevelFunctionClosure() {
|
||||
blackBoxFile("script/topLevelFunctionClosure.ktscript");
|
||||
blackBoxScript("script/topLevelFunctionClosure.ktscript");
|
||||
}
|
||||
|
||||
public void testSecondLevelFunction() {
|
||||
blackBoxFile("script/secondLevelFunction.ktscript");
|
||||
blackBoxScript("script/secondLevelFunction.ktscript");
|
||||
}
|
||||
|
||||
public void testSecondLevelFunctionClosure() {
|
||||
blackBoxFile("script/secondLevelFunctionClosure.ktscript");
|
||||
blackBoxScript("script/secondLevelFunctionClosure.ktscript");
|
||||
}
|
||||
|
||||
public void testSecondLevelVal() {
|
||||
blackBoxFile("script/secondLevelVal.ktscript");
|
||||
blackBoxScript("script/secondLevelVal.ktscript");
|
||||
}
|
||||
|
||||
public void testTopLevelProperty() {
|
||||
blackBoxFile("script/topLevelProperty.ktscript");
|
||||
blackBoxScript("script/topLevelProperty.ktscript");
|
||||
}
|
||||
|
||||
public void testScriptParameter() {
|
||||
blackBoxFile("script/parameter.ktscript");
|
||||
blackBoxScript("script/parameter.ktscript");
|
||||
}
|
||||
|
||||
public void testScriptParameterLong() {
|
||||
blackBoxFile("script/parameterLong.ktscript");
|
||||
blackBoxScript("script/parameterLong.ktscript");
|
||||
}
|
||||
|
||||
public void testScriptParameterArray() {
|
||||
blackBoxFile("script/parameterArray.ktscript");
|
||||
blackBoxScript("script/parameterArray.ktscript");
|
||||
}
|
||||
|
||||
public void testScriptParameterClosure() {
|
||||
blackBoxFile("script/parameterClosure.ktscript");
|
||||
blackBoxScript("script/parameterClosure.ktscript");
|
||||
}
|
||||
|
||||
public void testEmpty() {
|
||||
blackBoxFile("script/empty.ktscript");
|
||||
blackBoxScript("script/empty.ktscript");
|
||||
}
|
||||
|
||||
public void testLanguage() {
|
||||
|
||||
Reference in New Issue
Block a user