better ScriptGenTest: check more fields

This commit is contained in:
Stepan Koltsov
2012-06-05 22:56:26 +04:00
parent 9a71cb5e72
commit 68ae2e95a3
13 changed files with 61 additions and 25 deletions
@@ -1,2 +1,2 @@
// expected: null // expected: rv: null
System.out!!.println("hello world") System.out!!.println("hello world")
@@ -1,4 +1,4 @@
// expected: sky color is blue // expected: rv: sky color is blue
// param: what: jet.String: sky // param: what: jet.String: sky
// param: color: jet.String: blue // param: color: jet.String: blue
@@ -1,4 +1,4 @@
// param: args: jet.Array<jet.String>: three little words // param: args: jet.Array<jet.String>: three little words
// expected: little // expected: rv: little
args[1] args[1]
@@ -1,4 +1,4 @@
// expected: 19 // expected: rv: 19
// param: aa: jet.Long: 17 // param: aa: jet.Long: 17
// param: bb: jet.Int: 2 // param: bb: jet.Int: 2
@@ -7,4 +7,4 @@ if (true) {
x x
// expected: 29 // expected: rv: 29
@@ -8,4 +8,4 @@ if (true) {
x x
// expected: 50 // expected: rv: 50
@@ -0,0 +1,8 @@
if (true) {
val archenemy = "Jim Moriarty"
}
true
// expected: rv: true
// expected: archenemy: <nofield>
@@ -1 +1,3 @@
"O" + "K" "O" + "K"
// expected: rv: OK
@@ -1,4 +1,4 @@
// expected: 3628800 // expected: rv: 3628800
fun factorial(n: Int): Int { fun factorial(n: Int): Int {
var product = 1 var product = 1
@@ -4,4 +4,4 @@ fun foo(y: Int) = x + y
foo(33) foo(33)
// expected: 45 // expected: rv: 45
@@ -18,6 +18,7 @@ package org.jetbrains.jet.codegen;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.intellij.openapi.util.Pair;
import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.UsefulTestCase;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -187,16 +188,35 @@ public abstract class CodegenTestCase extends UsefulTestCase {
Constructor constructor = getConstructor(scriptClass, state.getScriptConstructorMethod()); Constructor constructor = getConstructor(scriptClass, state.getScriptConstructorMethod());
Object scriptInstance = constructor.newInstance(myFile.getScriptParameterValues().toArray()); Object scriptInstance = constructor.newInstance(myFile.getScriptParameterValues().toArray());
Field field = scriptClass.getDeclaredField("rv");
field.setAccessible(true); assertFalse("expecting at least one expectation", myFile.getExpectedValues().isEmpty());
Object result = field.get(scriptInstance);
r = result != null ? result.toString() : "null"; 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 { 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");
r = (String) method.invoke(null); r = (String) method.invoke(null);
assertEquals("OK", r);
} }
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
System.out.println(generateToText()); System.out.println(generateToText());
@@ -207,11 +227,6 @@ public abstract class CodegenTestCase extends UsefulTestCase {
} 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) { protected GeneratedClassLoader createClassLoader(ClassFileFactory codegens) {
@@ -18,6 +18,7 @@ package org.jetbrains.jet.codegen;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
@@ -38,7 +39,7 @@ public class CodegenTestFile {
@NotNull @NotNull
private final JetFile psiFile; private final JetFile psiFile;
@NotNull @NotNull
private final String expectedValue; private final List<Pair<String, String>> expectedValues;
@NotNull @NotNull
private final List<AnalyzerScriptParameter> scriptParameterTypes; private final List<AnalyzerScriptParameter> scriptParameterTypes;
@NotNull @NotNull
@@ -46,11 +47,11 @@ public class CodegenTestFile {
private CodegenTestFile( private CodegenTestFile(
@NotNull JetFile psiFile, @NotNull JetFile psiFile,
@NotNull String expectedValue, @NotNull List<Pair<String, String>> expectedValues,
@NotNull List<AnalyzerScriptParameter> scriptParameterTypes, @NotNull List<AnalyzerScriptParameter> scriptParameterTypes,
@NotNull List<Object> scriptParameterValues) { @NotNull List<Object> scriptParameterValues) {
this.psiFile = psiFile; this.psiFile = psiFile;
this.expectedValue = expectedValue; this.expectedValues = expectedValues;
this.scriptParameterTypes = scriptParameterTypes; this.scriptParameterTypes = scriptParameterTypes;
this.scriptParameterValues = scriptParameterValues; this.scriptParameterValues = scriptParameterValues;
} }
@@ -61,8 +62,8 @@ public class CodegenTestFile {
} }
@NotNull @NotNull
public String getExpectedValue() { public List<Pair<String, String>> getExpectedValues() {
return expectedValue; return expectedValues;
} }
@NotNull @NotNull
@@ -79,8 +80,14 @@ public class CodegenTestFile {
public static CodegenTestFile create(@NotNull String fileName, @NotNull String content, @NotNull Project project) { public static CodegenTestFile create(@NotNull String fileName, @NotNull String content, @NotNull Project project) {
JetFile file = (JetFile) JetTestUtils.createFile(fileName, content, project); JetFile file = (JetFile) JetTestUtils.createFile(fileName, content, project);
Matcher matcher = Pattern.compile("// expected: (.*)").matcher(content); List<Pair<String, String>> expectedValues = Lists.newArrayList();
String expectedValue = matcher.find() ? matcher.group(1) : "OK";
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<AnalyzerScriptParameter> scriptParameterTypes = Lists.newArrayList();
List<Object> scriptParameterValues = 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"); blackBoxFile("script/secondLevelFunctionClosure.ktscript");
} }
public void testSecondLevelVal() {
blackBoxFile("script/secondLevelVal.ktscript");
}
public void testScriptParameter() { public void testScriptParameter() {
blackBoxFile("script/parameter.ktscript"); blackBoxFile("script/parameter.ktscript");
} }