diff --git a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java new file mode 100644 index 00000000000..b95c496d422 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java @@ -0,0 +1,136 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.test; + +import com.intellij.openapi.util.io.FileUtil; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.util.Arrays; +import java.util.List; + +import static org.jetbrains.k2js.test.utils.JsTestUtils.convertToDotJsFile; + +/** + * @author Pavel Talanov + */ +public abstract class BasicTest extends TestWithEnvironment { + + private static final boolean DELETE_OUT = false; + public static final String TEST_FILES = "js.translator/testFiles/"; + private static final String CASES = "cases/"; + private static final String OUT = "out/"; + private static final String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js"; + private static final String EXPECTED = "expected/"; + + @NotNull + private String mainDirectory = ""; + + @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") + public BasicTest(@NotNull String main) { + this.mainDirectory = main; + } + + @NotNull + public String getMainDirectory() { + return mainDirectory; + } + + protected boolean shouldCreateOut() { + return true; + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + if (!shouldCreateOut()) { + return; + } + File outDir = new File(getOutputPath()); + assert (!outDir.exists() || outDir.isDirectory()) : "If out already exists it should be a directory."; + if (!outDir.exists()) { + boolean success = outDir.mkdir(); + assert success; + } + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + //noinspection ConstantConditions,PointlessBooleanExpression + if (!shouldCreateOut() || !DELETE_OUT) { + return; + } + File outDir = new File(getOutputPath()); + assert outDir.exists(); + boolean success = FileUtil.delete(outDir); + assert success; + } + + protected static String kotlinLibraryPath() { + return KOTLIN_JS_LIB; + } + + protected static String casesDirectoryName() { + return CASES; + } + + private static String outDirectoryName() { + return OUT; + } + + private static String expectedDirectoryName() { + return EXPECTED; + } + + @NotNull + private String pathToTestFiles() { + return TEST_FILES + getMainDirectory(); + } + + private String getOutputPath() { + return pathToTestFiles() + outDirectoryName(); + } + + private String getInputPath() { + return pathToTestFiles() + casesDirectoryName(); + } + + private String getExpectedPath() { + return pathToTestFiles() + expectedDirectoryName(); + } + + protected static List withKotlinJsLib(@NotNull String inputFile) { + return Arrays.asList(kotlinLibraryPath(), inputFile); + } + + protected String getOutputFilePath(@NotNull String filename) { + return getOutputPath() + convertToDotJsFile(filename); + } + + protected String getInputFilePath(@NotNull String filename) { + return getInputPath() + filename; + } + + protected String cases(@NotNull String filename) { + return getInputFilePath(filename); + } + + protected String expected(@NotNull String testName) { + return getExpectedPath() + testName + ".out"; + } +} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java index fba47c22724..3ea0cfe9135 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MultiFileTest.java @@ -38,6 +38,6 @@ public final class MultiFileTest extends TranslationTest { @Override public void checkFooBoxIsTrue(@NotNull String dirName) throws Exception { - testMultiFile(dirName, "foo", "box", true); + runMultiFileTest(dirName, "foo", "box", true); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java b/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java index 6e3de7f9774..9db0c03c951 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/MultiNamespaceTest.java @@ -38,7 +38,7 @@ public class MultiNamespaceTest extends TranslationTest { @Override public void checkFooBoxIsTrue(@NotNull String dirName) throws Exception { - testMultiFile(dirName, "foo", "box", true); + runMultiFileTest(dirName, "foo", "box", true); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/SimpleTestSuite.java b/js/js.tests/test/org/jetbrains/k2js/test/SimpleTest.java similarity index 95% rename from js/js.tests/test/org/jetbrains/k2js/test/SimpleTestSuite.java rename to js/js.tests/test/org/jetbrains/k2js/test/SimpleTest.java index cbc11b0fec8..25d4ed497f7 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/SimpleTestSuite.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/SimpleTest.java @@ -25,11 +25,9 @@ import org.jetbrains.k2js.test.utils.SuiteBuilder; * @author Pavel Talanov */ @SuppressWarnings("JUnitTestCaseWithNoTests") -public final class SimpleTestSuite extends UsefulTestCase { - +public final class SimpleTest extends UsefulTestCase { public static Test suite() throws Exception { return SuiteBuilder.suiteForDirectory("simple/", new SuiteBuilder.Tester() { - @Override public void performTest(@NotNull TranslationTest test, @NotNull String filename) throws Exception { test.checkFooBoxIsTrue(filename); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java b/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java index 483b8d91309..4e7d952c483 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/StandardClassesTest.java @@ -19,7 +19,7 @@ package org.jetbrains.k2js.test; /** * @author Pavel Talanov */ -public class StandardClassesTest extends TranslationTest { +public final class StandardClassesTest extends TranslationTest { public StandardClassesTest() { super("standardClasses/"); diff --git a/js/js.tests/test/org/jetbrains/k2js/test/BaseTest.java b/js/js.tests/test/org/jetbrains/k2js/test/TestWithEnvironment.java similarity index 95% rename from js/js.tests/test/org/jetbrains/k2js/test/BaseTest.java rename to js/js.tests/test/org/jetbrains/k2js/test/TestWithEnvironment.java index e624a8f9f4b..14d23a874ed 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/BaseTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TestWithEnvironment.java @@ -26,7 +26,7 @@ import org.jetbrains.jet.compiler.JetCoreEnvironment; /** * @author Pavel Talanov */ -public abstract class BaseTest extends UsefulTestCase { +public abstract class TestWithEnvironment extends UsefulTestCase { @Nullable protected JetCoreEnvironment myEnvironment; diff --git a/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java b/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java index 9bec40c0de6..cdd2f410013 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/TranslationTest.java @@ -16,7 +16,6 @@ package org.jetbrains.k2js.test; -import com.intellij.openapi.util.io.FileUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker; import org.jetbrains.k2js.test.rhino.RhinoSystemOutputChecker; @@ -28,115 +27,33 @@ import java.util.Arrays; import java.util.List; import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; -import static org.jetbrains.k2js.test.utils.JsTestUtils.convertToDotJsFile; import static org.jetbrains.k2js.test.utils.JsTestUtils.readFile; import static org.jetbrains.k2js.test.utils.TranslationUtils.translateFiles; -//TODO: spread the test* methods amongst classes that actually use them - /** * @author Pavel Talanov */ @SuppressWarnings("JUnitTestCaseWithNonTrivialConstructors") -public abstract class TranslationTest extends BaseTest { - - private static final boolean DELETE_OUT = false; - public static final String TEST_FILES = "js.translator/testFiles/"; - private static final String CASES = "cases/"; - private static final String OUT = "out/"; - private static final String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js"; - private static final String EXPECTED = "expected/"; - - @NotNull - private String mainDirectory = ""; +public abstract class TranslationTest extends BasicTest { public TranslationTest(@NotNull String main) { - this.mainDirectory = main; - } - - @NotNull - public String getMainDirectory() { - return mainDirectory; - } - - @Override - protected void setUp() throws Exception { - super.setUp(); - if (!shouldCreateOut()) { - return; - } - File outDir = new File(getOutputPath()); - assert (!outDir.exists() || outDir.isDirectory()) : "If out already exists it should be a directory."; - if (!outDir.exists()) { - boolean success = outDir.mkdir(); - assert success; - } - } - - @Override - protected void tearDown() throws Exception { - super.tearDown(); - //noinspection ConstantConditions,PointlessBooleanExpression - if (!shouldCreateOut() || !DELETE_OUT) { - return; - } - File outDir = new File(getOutputPath()); - assert outDir.exists(); - boolean success = FileUtil.delete(outDir); - assert success; - } - - protected static String kotlinLibraryPath() { - return KOTLIN_JS_LIB; - } - - protected static String casesDirectoryName() { - return CASES; - } - - private static String outDirectoryName() { - return OUT; - } - - private static String expectedDirectoryName() { - return EXPECTED; - } - - @NotNull - private String pathToTestFiles() { - return TEST_FILES + getMainDirectory(); - } - - private String getOutputPath() { - return pathToTestFiles() + outDirectoryName(); - } - - private String getInputPath() { - return pathToTestFiles() + casesDirectoryName(); - } - - private String getExpectedPath() { - return pathToTestFiles() + expectedDirectoryName(); + super(main); } protected void runFunctionOutputTest(String filename, String namespaceName, String functionName, Object expectedResult) throws Exception { generateJsFromFile(filename); - runRhinoTest(generateFilenameList(getOutputFilePath(filename)), + runRhinoTest(withKotlinJsLib(getOutputFilePath(filename)), new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult)); } - protected void testMultiFile(String dirName, String namespaceName, - String functionName, Object expectedResult) throws Exception { + protected void runMultiFileTest(String dirName, String namespaceName, + String functionName, Object expectedResult) throws Exception { generateJsFromDir(dirName); - runRhinoTest(generateFilenameList(getOutputFilePath(dirName + ".kt")), + runRhinoTest(withKotlinJsLib(getOutputFilePath(dirName + ".kt")), new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult)); } - protected boolean shouldCreateOut() { - return true; - } - protected void generateJsFromFile(@NotNull String filename) throws Exception { TranslationUtils.translateFile(getProject(), getInputFilePath(filename), getOutputFilePath(filename)); } @@ -152,25 +69,6 @@ public abstract class TranslationTest extends BaseTest { translateFiles(getProject(), fullFilePaths, getOutputFilePath(dirName + ".kt")); } - protected static List generateFilenameList(@NotNull String inputFile) { - return Arrays.asList(kotlinLibraryPath(), inputFile); - } - - private String getOutputFilePath(@NotNull String filename) { - return getOutputPath() + convertToDotJsFile(filename); - } - - private String getInputFilePath(@NotNull String filename) { - return getInputPath() + filename; - } - - protected String cases(@NotNull String filename) { - return getInputFilePath(filename); - } - - private String expected(@NotNull String testName) { - return getExpectedPath() + testName + ".out"; - } public void checkFooBoxIsTrue(@NotNull String filename) throws Exception { runFunctionOutputTest(filename, "foo", "box", true); @@ -180,13 +78,13 @@ public abstract class TranslationTest extends BaseTest { runFunctionOutputTest(filename, "foo", "box", "OK"); } - protected void checkOutput(String filename, String expectedResult, String... args) throws Exception { + protected void checkOutput(@NotNull String filename, @NotNull String expectedResult, @NotNull String... args) throws Exception { generateJsFromFile(filename); - runRhinoTest(generateFilenameList(getOutputFilePath(filename)), + runRhinoTest(withKotlinJsLib(getOutputFilePath(filename)), new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args))); } - protected void performTestWithMain(String testName, String testId, String... args) throws Exception { + protected void performTestWithMain(@NotNull String testName, @NotNull String testId, @NotNull String... args) throws Exception { checkOutput(testName + ".kt", readFile(expected(testName + testId)), args); } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/utils/SuiteBuilder.java b/js/js.tests/test/org/jetbrains/k2js/test/utils/SuiteBuilder.java index a04ffabdf60..0802bfe9501 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/utils/SuiteBuilder.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/utils/SuiteBuilder.java @@ -60,7 +60,6 @@ public abstract class SuiteBuilder extends TranslationTest { final SuiteBuilder singleFileTest = new SuiteBuilder(main) { }; singleFileTest.setUp(); - return TranslatorTestCaseBuilder.suiteForDirectory(TEST_FILES + main + casesDirectoryName(), true, new TranslatorTestCaseBuilder.NamedTestFactory() {