better ScriptGenTest: check more fields
This commit is contained in:
@@ -18,6 +18,7 @@ package org.jetbrains.jet.codegen;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.testFramework.UsefulTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -187,16 +188,35 @@ public abstract class CodegenTestCase extends UsefulTestCase {
|
||||
|
||||
Constructor constructor = getConstructor(scriptClass, state.getScriptConstructorMethod());
|
||||
Object scriptInstance = constructor.newInstance(myFile.getScriptParameterValues().toArray());
|
||||
Field field = scriptClass.getDeclaredField("rv");
|
||||
field.setAccessible(true);
|
||||
Object result = field.get(scriptInstance);
|
||||
r = result != null ? result.toString() : "null";
|
||||
|
||||
assertFalse("expecting at least one expectation", myFile.getExpectedValues().isEmpty());
|
||||
|
||||
for (Pair<String, String> nameValue : myFile.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);
|
||||
}
|
||||
}
|
||||
else {
|
||||
String fqName = NamespaceCodegen.getJVMClassNameForKotlinNs(JetPsiUtil.getFQName(myFile.getPsiFile())).getFqName().getFqName();
|
||||
Class<?> namespaceClass = loader.loadClass(fqName);
|
||||
Method method = namespaceClass.getMethod("box");
|
||||
r = (String) method.invoke(null);
|
||||
assertEquals("OK", r);
|
||||
}
|
||||
} catch (NoClassDefFoundError e) {
|
||||
System.out.println(generateToText());
|
||||
@@ -207,11 +227,6 @@ public abstract class CodegenTestCase extends UsefulTestCase {
|
||||
} finally {
|
||||
loader.dispose();
|
||||
}
|
||||
|
||||
if (!Objects.equal(myFile.getExpectedValue(), r)) {
|
||||
System.out.println(generateToText());
|
||||
}
|
||||
assertEquals(myFile.getExpectedValue(), r);
|
||||
}
|
||||
|
||||
protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) {
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.jet.codegen;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
@@ -38,7 +39,7 @@ public class CodegenTestFile {
|
||||
@NotNull
|
||||
private final JetFile psiFile;
|
||||
@NotNull
|
||||
private final String expectedValue;
|
||||
private final List<Pair<String, String>> expectedValues;
|
||||
@NotNull
|
||||
private final List<AnalyzerScriptParameter> scriptParameterTypes;
|
||||
@NotNull
|
||||
@@ -46,11 +47,11 @@ public class CodegenTestFile {
|
||||
|
||||
private CodegenTestFile(
|
||||
@NotNull JetFile psiFile,
|
||||
@NotNull String expectedValue,
|
||||
@NotNull List<Pair<String, String>> expectedValues,
|
||||
@NotNull List<AnalyzerScriptParameter> scriptParameterTypes,
|
||||
@NotNull List<Object> scriptParameterValues) {
|
||||
this.psiFile = psiFile;
|
||||
this.expectedValue = expectedValue;
|
||||
this.expectedValues = expectedValues;
|
||||
this.scriptParameterTypes = scriptParameterTypes;
|
||||
this.scriptParameterValues = scriptParameterValues;
|
||||
}
|
||||
@@ -61,8 +62,8 @@ public class CodegenTestFile {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getExpectedValue() {
|
||||
return expectedValue;
|
||||
public List<Pair<String, String>> getExpectedValues() {
|
||||
return expectedValues;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -79,8 +80,14 @@ public class CodegenTestFile {
|
||||
public static CodegenTestFile create(@NotNull String fileName, @NotNull String content, @NotNull Project project) {
|
||||
JetFile file = (JetFile) JetTestUtils.createFile(fileName, content, project);
|
||||
|
||||
Matcher matcher = Pattern.compile("// expected: (.*)").matcher(content);
|
||||
String expectedValue = matcher.find() ? matcher.group(1) : "OK";
|
||||
List<Pair<String, String>> expectedValues = Lists.newArrayList();
|
||||
|
||||
Matcher matcher = Pattern.compile("// expected: (\\S+): (.*)").matcher(content);
|
||||
while (matcher.find()) {
|
||||
String fieldName = matcher.group(1);
|
||||
String expectedValue = matcher.group(2);
|
||||
expectedValues.add(Pair.create(fieldName, expectedValue));
|
||||
}
|
||||
|
||||
List<AnalyzerScriptParameter> scriptParameterTypes = Lists.newArrayList();
|
||||
List<Object> scriptParameterValues = Lists.newArrayList();
|
||||
@@ -116,6 +123,6 @@ public class CodegenTestFile {
|
||||
}
|
||||
}
|
||||
|
||||
return new CodegenTestFile(file, expectedValue, scriptParameterTypes, scriptParameterValues);
|
||||
return new CodegenTestFile(file, expectedValues, scriptParameterTypes, scriptParameterValues);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,6 +54,10 @@ public class ScriptGenTest extends CodegenTestCase {
|
||||
blackBoxFile("script/secondLevelFunctionClosure.ktscript");
|
||||
}
|
||||
|
||||
public void testSecondLevelVal() {
|
||||
blackBoxFile("script/secondLevelVal.ktscript");
|
||||
}
|
||||
|
||||
public void testScriptParameter() {
|
||||
blackBoxFile("script/parameter.ktscript");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user