Refactoring of K2JSTranslator and neighboring classes

Drop GenerationUtils
Introduce MainCallParameters
Generate calls to main function together with the other code in contrast to as text afterwards
Enhance tests a bit
This commit is contained in:
pTalanov
2012-05-05 18:50:02 +04:00
parent 9681a61fd2
commit 869a8ab1ba
12 changed files with 210 additions and 149 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.k2js.test;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
import java.util.List;
@@ -36,11 +37,11 @@ public abstract class MultipleFilesTranslationTest extends BasicTest {
protected void generateJsFromDir(@NotNull String dirName) throws Exception {
List<String> fullFilePaths = getAllFilesInDir(getInputFilePath(dirName));
translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt"));
translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt"), MainCallParameters.noCall());
}
protected void runMultiFileTest(@NotNull String dirName, @NotNull String namespaceName,
@NotNull String functionName, @NotNull Object expectedResult) throws Exception {
@NotNull String functionName, @NotNull Object expectedResult) throws Exception {
generateJsFromDir(dirName);
runRhinoTest(withAdditionalFiles(getOutputFilePath(dirName + ".kt")),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
@@ -16,13 +16,13 @@
package org.jetbrains.k2js.test;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker;
import org.jetbrains.k2js.test.utils.TranslationUtils;
import java.util.Arrays;
import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest;
import static org.jetbrains.k2js.test.utils.JsTestUtils.readFile;
@@ -37,14 +37,14 @@ public abstract class SingleFileTranslationTest extends BasicTest {
}
public void runFunctionOutputTest(String filename, String namespaceName,
String functionName, Object expectedResult) throws Exception {
generateJsFromFile(filename);
String functionName, Object expectedResult) throws Exception {
generateJsFromFile(filename, MainCallParameters.noCall());
runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)),
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
}
protected void generateJsFromFile(@NotNull String filename) throws Exception {
TranslationUtils.translateFile(getProject(), getInputFilePath(filename), getOutputFilePath(filename));
protected void generateJsFromFile(@NotNull String filename, @NotNull MainCallParameters mainCallParameters) throws Exception {
TranslationUtils.translateFile(getProject(), getInputFilePath(filename), getOutputFilePath(filename), mainCallParameters);
}
public void checkFooBoxIsTrue(@NotNull String filename) throws Exception {
@@ -56,9 +56,9 @@ public abstract class SingleFileTranslationTest extends BasicTest {
}
protected void checkOutput(@NotNull String filename, @NotNull String expectedResult, @NotNull String... args) throws Exception {
generateJsFromFile(filename);
generateJsFromFile(filename, MainCallParameters.mainWithArguments(Lists.newArrayList(args)));
runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)),
new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args)));
new RhinoSystemOutputChecker(expectedResult));
}
protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception {
@@ -17,13 +17,9 @@
package org.jetbrains.k2js.test.rhino;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.k2js.translate.context.Namer;
import org.jetbrains.k2js.utils.GenerationUtils;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import java.util.List;
import static org.junit.Assert.assertTrue;
/**
@@ -31,39 +27,30 @@ import static org.junit.Assert.assertTrue;
*/
public final class RhinoSystemOutputChecker implements RhinoResultChecker {
private final List<String> arguments;
private final String expectedResult;
public RhinoSystemOutputChecker(String expectedResult, List<String> arguments) {
public RhinoSystemOutputChecker(@NotNull String expectedResult) {
this.expectedResult = expectedResult;
this.arguments = arguments;
}
@Override
public void runChecks(@NotNull Context context, @NotNull Scriptable scope)
throws Exception {
runMain(context, scope);
String result = getSystemOutput(context, scope);
String trimmedExpected = trimSpace(expectedResult);
String trimmedActual = trimSpace(result);
// System.out.println(trimmedActual);
// System.out.println(trimmedExpected);
assertTrue("Returned:\n" + trimmedActual + "END_OF_RETURNED\nExpected:\n" + trimmedExpected
+ "END_OF_EXPECTED\n", trimmedExpected.equals(trimmedActual));
}
private String getSystemOutput(@NotNull Context context, @NotNull Scriptable scope) {
private static String getSystemOutput(@NotNull Context context, @NotNull Scriptable scope) {
Object output = context.evaluateString(scope, "Kotlin.System.output()", "test", 0, null);
assertTrue("Output should be a string.", output instanceof String);
return (String) output;
}
private void runMain(Context context, Scriptable scope) {
String callToMain = GenerationUtils.generateCallToMain(Namer.getRootNamespaceName(), arguments);
context.evaluateString(scope, callToMain, "function call", 0, null);
}
public String trimSpace(String s) {
public static String trimSpace(@NotNull String s) {
String[] choppedUpString = s.trim().split("\\s");
StringBuilder sb = new StringBuilder();
for (String word : choppedUpString) {
@@ -22,6 +22,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.facade.K2JSTranslator;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.generate.CodeGenerator;
import org.jetbrains.k2js.test.config.TestConfig;
@@ -44,19 +45,21 @@ public final class TranslationUtils {
private static /*var*/ K2JSTranslator translator = null;
public static void translateFile(@NotNull Project project, @NotNull String inputFile,
@NotNull String outputFile) throws Exception {
translateFiles(project, Collections.singletonList(inputFile), outputFile);
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters) throws Exception {
translateFiles(project, Collections.singletonList(inputFile), outputFile, mainCallParameters);
}
public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles,
@NotNull String outputFile) throws Exception {
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters) throws Exception {
List<JetFile> psiFiles = createPsiFileList(inputFiles, project);
JsProgram program = getTranslator(project).generateProgram(psiFiles);
JsProgram program = getTranslator(project).generateProgram(psiFiles, mainCallParameters);
FileWriter writer = new FileWriter(new File(outputFile));
try {
writer.write("\"use strict\";\n");
writer.write(CodeGenerator.toString(program));
} finally {
}
finally {
writer.close();
}
}