From 2f6a1351355bd1dd9df33439043d3a794fe4f688 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Wed, 10 Dec 2014 14:49:01 +0300 Subject: [PATCH] JS tests: added doTest and checkFooBoxIsOkByPath to BasicTest to simplify using generated tests. --- .../org/jetbrains/k2js/test/BasicTest.java | 41 +++++++++-- .../test/MultipleFilesTranslationTest.java | 5 ++ .../k2js/test/SingleFileTranslationTest.java | 69 ++++++++++--------- .../k2js/test/semantics/AbstractBridgeTest.kt | 4 +- .../semantics/AbstractReservedWordTest.kt | 4 +- .../k2js/test/semantics/SimpleTest.java | 2 +- .../k2js/test/semantics/StdLibTest.java | 8 +-- .../k2js/test/semantics/StringTest.java | 2 +- 8 files changed, 83 insertions(+), 52 deletions(-) diff --git a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java index 044acd5352e..247aac686f5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java @@ -83,6 +83,8 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { this.relativePathToTestDir = relativePathToTestDir; } + protected abstract void checkFooBoxIsOkByPath(String filePath) throws Exception; + @Override protected JetCoreEnvironment createEnvironment() { return JetCoreEnvironment.createForTests(getTestRootDisposable(), new CompilerConfiguration(), EnvironmentConfigFiles.JS_CONFIG_FILES); @@ -116,13 +118,21 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { assert success; } + public void doTest(@NotNull String filePath) { + try { + checkFooBoxIsOkByPath(filePath); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + protected void generateJavaScriptFiles( - @NotNull String kotlinFilename, + @NotNull String kotlinFilePath, @NotNull MainCallParameters mainCallParameters, @NotNull Iterable ecmaVersions ) throws Exception { - generateJavaScriptFiles(Collections.singletonList(getInputFilePath(kotlinFilename)), - kotlinFilename, mainCallParameters, ecmaVersions); + generateJavaScriptFiles(Collections.singletonList(kotlinFilePath), getBaseName(kotlinFilePath), mainCallParameters, ecmaVersions); } protected void generateJavaScriptFiles( @@ -182,12 +192,12 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { } protected void runRhinoTests( - @NotNull String filename, + @NotNull String testName, @NotNull Iterable ecmaVersions, @NotNull RhinoResultChecker checker ) throws Exception { for (EcmaVersion ecmaVersion : ecmaVersions) { - runRhinoTest(withAdditionalJsFiles(getOutputFilePath(filename, ecmaVersion), ecmaVersion), + runRhinoTest(withAdditionalJsFiles(getOutputFilePath(testName, ecmaVersion), ecmaVersion), checker, getRhinoTestVariables(), ecmaVersion); @@ -219,8 +229,8 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { } @NotNull - protected final String getOutputFilePath(@NotNull String filename, @NotNull EcmaVersion ecmaVersion) { - return getOutputPath() + convertFileNameToDotJsFile(filename, ecmaVersion); + protected final String getOutputFilePath(@NotNull String testName, @NotNull EcmaVersion ecmaVersion) { + return getOutputPath() + convertFileNameToDotJsFile(testName, ecmaVersion); } @NotNull @@ -294,4 +304,21 @@ public abstract class BasicTest extends KotlinTestWithEnvironment { String packageName = jetFile.getPackageFqName().asString(); return packageName.isEmpty() ? Namer.getRootPackageName() : packageName; } + + protected static String getBaseName(String path) { + String systemIndependentPath = FileUtil.toSystemIndependentName(path); + + int start = systemIndependentPath.lastIndexOf("/"); + if (start == -1) { + start = 0; + } + + int end = systemIndependentPath.lastIndexOf("."); + if (end == -1) { + end = path.length(); + } + + return path.substring(start, end); + } + } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java index 9afe2069bd0..a18ee1a9d19 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MultipleFilesTranslationTest.java @@ -31,6 +31,11 @@ public abstract class MultipleFilesTranslationTest extends BasicTest { super(main); } + @Override + protected void checkFooBoxIsOkByPath(String filePath) throws Exception { + throw new UnsupportedOperationException("checkFooBoxIsOkByPath not supported yet in MultipleFilesTranslationTest"); + } + protected void generateJsFromDir(@NotNull String dirName, @NotNull Iterable ecmaVersions) throws Exception { List fullFilePaths = getAllFilesInDir(getInputFilePath(dirName)); generateJavaScriptFiles(fullFilePaths, dirName, MainCallParameters.noCall(), ecmaVersions); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java index a810abf6358..9ebda383f0b 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/SingleFileTranslationTest.java @@ -31,8 +31,12 @@ public abstract class SingleFileTranslationTest extends BasicTest { super(main); } - public void runFunctionOutputTest(@NotNull String kotlinFilename, @NotNull String packageName, - @NotNull String functionName, @NotNull Object expectedResult) throws Exception { + protected void runFunctionOutputTest( + @NotNull String kotlinFilename, + @NotNull String packageName, + @NotNull String functionName, + @NotNull Object expectedResult + ) throws Exception { runFunctionOutputTest(DEFAULT_ECMA_VERSIONS, kotlinFilename, packageName, functionName, expectedResult); } @@ -41,29 +45,36 @@ public abstract class SingleFileTranslationTest extends BasicTest { @NotNull String kotlinFilename, @NotNull String packageName, @NotNull String functionName, - @NotNull Object expectedResult) throws Exception { - generateJavaScriptFiles(kotlinFilename, MainCallParameters.noCall(), ecmaVersions); - runRhinoTests(kotlinFilename, ecmaVersions, new RhinoFunctionResultChecker(TEST_MODULE, packageName, functionName, expectedResult)); + @NotNull Object expectedResult + ) throws Exception { + runFunctionOutputTestByPath(ecmaVersions, getInputFilePath(kotlinFilename), packageName, functionName, expectedResult); } - public void checkFooBoxIsTrue(@NotNull String filename, @NotNull Iterable ecmaVersions) throws Exception { + protected void runFunctionOutputTestByPath( + @NotNull Iterable ecmaVersions, + @NotNull String kotlinFilePath, + @NotNull String packageName, + @NotNull String functionName, + @NotNull Object expectedResult + ) throws Exception { + generateJavaScriptFiles(kotlinFilePath, MainCallParameters.noCall(), ecmaVersions); + runRhinoTests(getBaseName(kotlinFilePath), ecmaVersions, new RhinoFunctionResultChecker(TEST_MODULE, packageName, functionName, expectedResult)); + } + + private void checkFooBoxIsTrue(@NotNull String filename, @NotNull Iterable ecmaVersions) throws Exception { runFunctionOutputTest(ecmaVersions, filename, TEST_PACKAGE, TEST_FUNCTION, true); } - public void checkFooBoxIsTrue(@NotNull String filename) throws Exception { + protected void checkFooBoxIsTrue(@NotNull String filename) throws Exception { runFunctionOutputTest(DEFAULT_ECMA_VERSIONS, filename, TEST_PACKAGE, TEST_FUNCTION, true); } - public void checkFooBoxIsValue(@NotNull String filename, @NotNull Iterable ecmaVersions, Object expected) throws Exception { - runFunctionOutputTest(ecmaVersions, filename, TEST_PACKAGE, TEST_FUNCTION, expected); - } - protected void fooBoxTest() throws Exception { checkFooBoxIsTrue(getTestName(true) + ".kt", DEFAULT_ECMA_VERSIONS); } protected void fooBoxIsValue(Object expected) throws Exception { - checkFooBoxIsValue(getTestName(true) + ".kt", DEFAULT_ECMA_VERSIONS, expected); + runFunctionOutputTest(DEFAULT_ECMA_VERSIONS, getTestName(true) + ".kt", TEST_PACKAGE, TEST_FUNCTION, expected); } protected void fooBoxTest(@NotNull Iterable ecmaVersions) throws Exception { @@ -75,19 +86,16 @@ public abstract class SingleFileTranslationTest extends BasicTest { } protected void checkFooBoxIsOk(@NotNull String filename) throws Exception { - checkFooBoxIsOk(DEFAULT_ECMA_VERSIONS, filename); + checkFooBoxIsOkByPath(getInputFilePath(filename)); } - protected void checkFooBoxIsOk(@NotNull Iterable versions, @NotNull String filename) throws Exception { - runFunctionOutputTest(versions, filename, TEST_PACKAGE, TEST_FUNCTION, "OK"); + @Override + protected void checkFooBoxIsOkByPath(@NotNull String filePath) throws Exception { + runFunctionOutputTestByPath(DEFAULT_ECMA_VERSIONS, filePath, TEST_PACKAGE, TEST_FUNCTION, "OK"); } - protected void checkBlackBoxIsOk(@NotNull String filename) throws Exception { - checkBlackBoxIsOk(DEFAULT_ECMA_VERSIONS, filename); - } - - protected void checkBlackBoxIsOk(@NotNull Iterable versions, @NotNull String filename) throws Exception { - runFunctionOutputTest(versions, filename, getPackageName(filename), TEST_FUNCTION, "OK"); + protected void checkBlackBoxIsOkByPath(@NotNull String filePath) throws Exception { + runFunctionOutputTestByPath(DEFAULT_ECMA_VERSIONS, filePath, getPackageName(filePath), TEST_FUNCTION, "OK"); } protected void checkOutput(@NotNull String kotlinFilename, @@ -96,22 +104,17 @@ public abstract class SingleFileTranslationTest extends BasicTest { checkOutput(kotlinFilename, expectedResult, DEFAULT_ECMA_VERSIONS, args); } - protected void checkOutput(@NotNull String kotlinFilename, + private void checkOutput( + @NotNull String kotlinFilename, @NotNull String expectedResult, @NotNull Iterable ecmaVersions, - String... args) throws Exception { - generateJavaScriptFiles(kotlinFilename, MainCallParameters.mainWithArguments(Lists.newArrayList(args)), ecmaVersions); - runRhinoTests(kotlinFilename, ecmaVersions, new RhinoSystemOutputChecker(expectedResult)); - } - - protected void performTestWithMain(@NotNull Iterable ecmaVersions, - @NotNull String testName, - @NotNull String testId, - @NotNull String... args) throws Exception { - checkOutput(testName + ".kt", readFile(expectedFilePath(testName + testId)), ecmaVersions, args); + String... args + ) throws Exception { + generateJavaScriptFiles(getInputFilePath(kotlinFilename), MainCallParameters.mainWithArguments(Lists.newArrayList(args)), ecmaVersions); + runRhinoTests(getBaseName(kotlinFilename), ecmaVersions, new RhinoSystemOutputChecker(expectedResult)); } protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception { - performTestWithMain(DEFAULT_ECMA_VERSIONS, testName, testId, args); + checkOutput(testName + ".kt", readFile(expectedFilePath(testName + testId)), DEFAULT_ECMA_VERSIONS, args); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractBridgeTest.kt b/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractBridgeTest.kt index d96f6bcdca4..8d212cba0e8 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractBridgeTest.kt +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractBridgeTest.kt @@ -20,7 +20,5 @@ import org.jetbrains.k2js.test.SingleFileTranslationTest public abstract class AbstractBridgeTest : SingleFileTranslationTest("bridges/") { - override fun getInputPath(): String = "" - - fun doTest(filename: String) = checkBlackBoxIsOk(filename) + override fun doTest(filename: String) = checkBlackBoxIsOkByPath(filename) } \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractReservedWordTest.kt b/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractReservedWordTest.kt index 13db5efed56..c14f082c4a5 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractReservedWordTest.kt +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractReservedWordTest.kt @@ -18,6 +18,4 @@ package org.jetbrains.k2js.test.semantics import org.jetbrains.k2js.test.SingleFileTranslationTest -public abstract class AbstractReservedWordTest : SingleFileTranslationTest("reservedWords/") { - fun doTest(fileName: String) = checkFooBoxIsOk() -} +public abstract class AbstractReservedWordTest : SingleFileTranslationTest("reservedWords/") diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java index dbbcb2784f3..68335d6fe9a 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/SimpleTest.java @@ -34,7 +34,7 @@ public final class SimpleTest extends SingleFileTranslationTest { @Override public void runTest() throws Exception { - checkFooBoxIsTrue(filename, DEFAULT_ECMA_VERSIONS); + checkFooBoxIsTrue(filename); } public static Test suite() throws Exception { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java index d4a0882b218..74ab9fd2fd1 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTest.java @@ -42,13 +42,13 @@ public final class StdLibTest extends SingleFileTranslationTest { } @Override - protected void generateJavaScriptFiles(@NotNull String kotlinFilename, + protected void generateJavaScriptFiles(@NotNull String kotlinFilePath, @NotNull MainCallParameters mainCallParameters, @NotNull Iterable ecmaVersions) throws Exception { - List files = Arrays.asList(getInputFilePath(kotlinFilename)); + List files = Arrays.asList(getInputFilePath(kotlinFilePath)); - generateJavaScriptFiles(files, kotlinFilename, mainCallParameters, ecmaVersions); - runRhinoTests(kotlinFilename, ecmaVersions, + generateJavaScriptFiles(files, kotlinFilePath, mainCallParameters, ecmaVersions); + runRhinoTests(kotlinFilePath, ecmaVersions, new RhinoFunctionNativeObjectResultChecker(TEST_MODULE, "test.browser", TEST_FUNCTION, "Some Dynamically Created Content!!!")); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java index 8257a090710..43138cfc9fc 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StringTest.java @@ -73,7 +73,7 @@ public final class StringTest extends AbstractExpressionTest { private void checkHasNoToStringCalls() throws IOException { for (EcmaVersion ecmaVersion : DEFAULT_ECMA_VERSIONS) { - String filePath = getOutputFilePath(getTestName(true) + ".kt", ecmaVersion); + String filePath = getOutputFilePath(getTestName(true), ecmaVersion); String text = FileUtil.loadFile(new File(filePath), /*convertLineSeparators = */ true); assertFalse(filePath + " should not contain toString calls", text.contains("toString")); }