Make tests aware of different ecma versions
Introduce version parameters in relevant test methods
This commit is contained in:
@@ -20,9 +20,13 @@ import com.google.common.collect.Lists;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.JetTestUtils;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||
import org.jetbrains.k2js.test.utils.TranslationUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.k2js.test.utils.JsTestUtils.convertFileNameToDotJsFile;
|
||||
@@ -84,6 +88,22 @@ public abstract class BasicTest extends TestWithEnvironment {
|
||||
return Collections.singletonList(KOTLIN_JS_LIB);
|
||||
}
|
||||
|
||||
protected void generateJavaScriptFiles(@NotNull String kotlinFilename,
|
||||
@NotNull MainCallParameters mainCallParameters,
|
||||
@NotNull EnumSet<EcmaVersion> ecmaVersions) throws Exception {
|
||||
generateJavaScriptFiles(Collections.singletonList(getInputFilePath(kotlinFilename)), kotlinFilename, mainCallParameters,
|
||||
ecmaVersions);
|
||||
}
|
||||
|
||||
protected void generateJavaScriptFiles(@NotNull List<String> files, @NotNull String testName,
|
||||
@NotNull MainCallParameters mainCallParameters, @NotNull EnumSet<EcmaVersion> ecmaVersions)
|
||||
throws Exception {
|
||||
for (EcmaVersion version : ecmaVersions) {
|
||||
TranslationUtils.translateFiles(getProject(), files, getOutputFilePath(testName, version), mainCallParameters, version);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected static String casesDirectoryName() {
|
||||
return CASES;
|
||||
}
|
||||
@@ -129,8 +149,17 @@ public abstract class BasicTest extends TestWithEnvironment {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getOutputFilePath(@NotNull String filename) {
|
||||
return getOutputPath() + convertFileNameToDotJsFile(filename);
|
||||
protected List<String> getOutputFilePaths(@NotNull String filename, @NotNull EnumSet<EcmaVersion> ecmaVersions) {
|
||||
List<String> result = Lists.newArrayList();
|
||||
for (EcmaVersion ecmaVersion : ecmaVersions) {
|
||||
result.add(getOutputFilePath(filename, ecmaVersion));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected String getOutputFilePath(@NotNull String filename, @NotNull EcmaVersion ecmaVersion) {
|
||||
return getOutputPath() + convertFileNameToDotJsFile(filename, ecmaVersion);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -17,14 +17,12 @@
|
||||
package org.jetbrains.k2js.test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||
import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest;
|
||||
import static org.jetbrains.k2js.test.utils.JsTestUtils.getAllFilesInDir;
|
||||
import static org.jetbrains.k2js.test.utils.TranslationUtils.translateFiles;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
@@ -37,14 +35,14 @@ 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"), MainCallParameters.noCall());
|
||||
generateJavaScriptFiles(fullFilePaths, dirName, MainCallParameters.noCall(), EcmaVersion.all());
|
||||
}
|
||||
|
||||
protected void runMultiFileTest(@NotNull String dirName, @NotNull String namespaceName,
|
||||
@NotNull String functionName, @NotNull Object expectedResult) throws Exception {
|
||||
generateJsFromDir(dirName);
|
||||
runRhinoTest(withAdditionalFiles(getOutputFilePath(dirName + ".kt")),
|
||||
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
|
||||
//runRhinoTest(withAdditionalFiles(getOutputFilePaths(dirName + ".kt", EcmaVersion.all())),
|
||||
// new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
|
||||
}
|
||||
|
||||
public void checkFooBoxIsTrue(@NotNull String dirName) throws Exception {
|
||||
|
||||
@@ -18,10 +18,13 @@ package org.jetbrains.k2js.test;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
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.EnumSet;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest;
|
||||
import static org.jetbrains.k2js.test.utils.JsTestUtils.readFile;
|
||||
@@ -36,15 +39,15 @@ public abstract class SingleFileTranslationTest extends BasicTest {
|
||||
super(main);
|
||||
}
|
||||
|
||||
public void runFunctionOutputTest(String filename, String namespaceName,
|
||||
public void runFunctionOutputTest(String kotlinFilename, String namespaceName,
|
||||
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, @NotNull MainCallParameters mainCallParameters) throws Exception {
|
||||
TranslationUtils.translateFile(getProject(), getInputFilePath(filename), getOutputFilePath(filename), mainCallParameters);
|
||||
EnumSet<EcmaVersion> ecmaVersions = EcmaVersion.all();
|
||||
generateJavaScriptFiles(kotlinFilename, MainCallParameters.noCall(), ecmaVersions);
|
||||
List<String> outputFilePaths = getOutputFilePaths(kotlinFilename, ecmaVersions);
|
||||
for (String outputFilePath : outputFilePaths) {
|
||||
runRhinoTest(withAdditionalFiles(outputFilePath),
|
||||
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
|
||||
}
|
||||
}
|
||||
|
||||
public void checkFooBoxIsTrue(@NotNull String filename) throws Exception {
|
||||
@@ -59,10 +62,14 @@ public abstract class SingleFileTranslationTest extends BasicTest {
|
||||
runFunctionOutputTest(filename, "foo", "box", "OK");
|
||||
}
|
||||
|
||||
protected void checkOutput(@NotNull String filename, @NotNull String expectedResult, @NotNull String... args) throws Exception {
|
||||
generateJsFromFile(filename, MainCallParameters.mainWithArguments(Lists.newArrayList(args)));
|
||||
runRhinoTest(withAdditionalFiles(getOutputFilePath(filename)),
|
||||
new RhinoSystemOutputChecker(expectedResult));
|
||||
protected void checkOutput(@NotNull String kotlinFilename, @NotNull String expectedResult, @NotNull String... args) throws Exception {
|
||||
EnumSet<EcmaVersion> ecmaVersions = EcmaVersion.all();
|
||||
generateJavaScriptFiles(kotlinFilename, MainCallParameters.mainWithArguments(Lists.newArrayList(args)), ecmaVersions);
|
||||
List<String> outputFilePaths = getOutputFilePaths(kotlinFilename, ecmaVersions);
|
||||
for (String outputFilePath : outputFilePaths) {
|
||||
runRhinoTest(withAdditionalFiles(outputFilePath),
|
||||
new RhinoSystemOutputChecker(expectedResult));
|
||||
}
|
||||
}
|
||||
|
||||
protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception {
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.test.semantics;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.test.SingleFileTranslationTest;
|
||||
|
||||
import java.io.File;
|
||||
@@ -68,7 +69,7 @@ public final class InlineTest extends SingleFileTranslationTest {
|
||||
|
||||
private void checkFooBoxIsTrueAndFunctionNameIsNotReferenced(@NotNull String filename, String funName) throws Exception {
|
||||
fooBoxTest();
|
||||
String generatedJSFilePath = getOutputFilePath(filename);
|
||||
String generatedJSFilePath = getOutputFilePath(filename, EcmaVersion.defaultVersion());
|
||||
String outputFileText = FileUtil.loadFile(new File(generatedJSFilePath));
|
||||
assertTrue(countOccurrences(outputFileText, funName) == 1);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.jetbrains.k2js.test.utils;
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -33,8 +34,13 @@ public final class JsTestUtils {
|
||||
private JsTestUtils() {
|
||||
}
|
||||
|
||||
public static String convertFileNameToDotJsFile(@NotNull String filename) {
|
||||
return filename.substring(0, filename.lastIndexOf('.')) + ".js";
|
||||
public static String convertFileNameToDotJsFile(@NotNull String filename, EcmaVersion ecmaVersion) {
|
||||
String postFix = "_" + ecmaVersion.toString() + ".js";
|
||||
int index = filename.lastIndexOf('.');
|
||||
if (index < 0) {
|
||||
return filename + postFix;
|
||||
}
|
||||
return filename.substring(0, index) + postFix;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.k2js.config.EcmaVersion;
|
||||
import org.jetbrains.k2js.facade.K2JSTranslator;
|
||||
import org.jetbrains.k2js.facade.MainCallParameters;
|
||||
import org.jetbrains.k2js.generate.CodeGenerator;
|
||||
@@ -45,13 +46,13 @@ public final class TranslationUtils {
|
||||
private static /*var*/ K2JSTranslator translator = null;
|
||||
|
||||
public static void translateFile(@NotNull Project project, @NotNull String inputFile,
|
||||
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters) throws Exception {
|
||||
translateFiles(project, Collections.singletonList(inputFile), outputFile, mainCallParameters);
|
||||
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters, @NotNull EcmaVersion version) throws Exception {
|
||||
translateFiles(project, Collections.singletonList(inputFile), outputFile, mainCallParameters, version);
|
||||
}
|
||||
|
||||
public static void translateFiles(@NotNull Project project, @NotNull List<String> inputFiles,
|
||||
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters) throws Exception {
|
||||
|
||||
@NotNull String outputFile, @NotNull MainCallParameters mainCallParameters, @NotNull EcmaVersion version) throws Exception {
|
||||
//TODO: ignoring version for the moment
|
||||
List<JetFile> psiFiles = createPsiFileList(inputFiles, project);
|
||||
JsProgram program = getTranslator(project).generateProgram(psiFiles, mainCallParameters);
|
||||
FileWriter writer = new FileWriter(new File(outputFile));
|
||||
|
||||
@@ -20,6 +20,8 @@ import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.EnumSet;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
*/
|
||||
@@ -35,4 +37,9 @@ public enum EcmaVersion {
|
||||
public static EcmaVersion defaultVersion() {
|
||||
return v3;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static EnumSet<EcmaVersion> all() {
|
||||
return EnumSet.allOf(EcmaVersion.class);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user