From 20f20f94417a1ad2838126a5723f1ceab4d915d5 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Thu, 2 Oct 2014 00:12:38 +0400 Subject: [PATCH] JS tests: refactored StdLibTestBase and derived classes. --- .../semantics/StdLibJsArrayQUnitTest.java | 34 --------- .../semantics/StdLibJsArrayScriptTest.java | 34 ++------- .../semantics/StdLibQUnitTestSupport.java | 32 ++++---- .../k2js/test/semantics/StdLibTestBase.java | 74 +++++-------------- .../test/semantics/StdLibTestToJSTest.java | 27 +++++-- .../k2js/test/semantics/StdLibToJSTest.java | 9 +++ 6 files changed, 68 insertions(+), 142 deletions(-) delete mode 100644 js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayQUnitTest.java diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayQUnitTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayQUnitTest.java deleted file mode 100644 index 76e53ad9e44..00000000000 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayQUnitTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2010-2013 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.semantics; - -/** - */ -public class StdLibJsArrayQUnitTest extends StdLibQUnitTestSupport { - public void testDummy() { - } - - // TODO for some reason this test fails when ran as part of all the js-backend tests, but works stand alone - // generates: ReferenceError: "QUnit" is not defined. (js/js.translator/testData/stdlib/out/ArrayQUnitTest.compiler_v3.js#3058) - // when ran in batch - - public void DISABLED_testArrayQUnitTest() throws Exception { - performStdLibTest(DEFAULT_ECMA_VERSIONS, - "libraries/stdlib/test", - "js/JsArrayTest.kt"); - } -} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayScriptTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayScriptTest.java index 7613b2acfd0..d23885f61a1 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayScriptTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibJsArrayScriptTest.java @@ -16,17 +16,11 @@ package org.jetbrains.k2js.test.semantics; -import com.google.common.collect.Lists; -import com.intellij.util.containers.ContainerUtil; -import org.jetbrains.k2js.config.EcmaVersion; +import org.jetbrains.annotations.NotNull; import org.jetbrains.k2js.test.rhino.CompositeRhinoResultsChecker; import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker; import org.jetbrains.k2js.test.rhino.RhinoResultChecker; -import java.util.Map; - -import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; - /** */ public class StdLibJsArrayScriptTest extends StdLibTestBase { @@ -36,28 +30,12 @@ public class StdLibJsArrayScriptTest extends StdLibTestBase { "js/JsArrayScript.kt"); } + @NotNull @Override - protected void performChecksOnGeneratedJavaScript(String path, EcmaVersion version) throws Exception { - Map variables = ContainerUtil.newHashMap(); - String moduleId = moduleIdFromOutputFile(path); - RhinoResultChecker checker = new CompositeRhinoResultsChecker( - new RhinoFunctionResultChecker(moduleId, "jstest", "testSize", 3.0), - new RhinoFunctionResultChecker(moduleId, "jstest", "testToListToString", "[]-[foo]-[foo, bar]") + protected RhinoResultChecker getResultChecker() { + return new CompositeRhinoResultsChecker( + new RhinoFunctionResultChecker(TEST_MODULE, "jstest", "testSize", 3.0), + new RhinoFunctionResultChecker(TEST_MODULE, "jstest", "testToListToString", "[]-[foo]-[foo, bar]") ); - runRhinoTest(Lists.newArrayList(path), checker, variables, version); - - /* - RhinoResultChecker checker = new RhinoFunctionResultChecker(moduleId, "jstest", "runTest", 2.0) { - - @Override - protected String functionCallString() { - //return "QUnit.test('JsArrayTest.arraySizeAndToList', function(){ (new Kotlin.modules['" + moduleId + "'].jstest.JsArrayTest).arraySizeAndToList(); });"; - return "(new Kotlin.modules['" + moduleId + "'].jstest.JsArrayTest).arraySizeAndToList();"; - } - }; - runRhinoTest(Lists.newArrayList(path), - checker, variables, version, - Lists.newArrayList("js/js.translator/qunit/qunit.js")); - */ } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibQUnitTestSupport.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibQUnitTestSupport.java index 15503860fa2..84b4f28dd0a 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibQUnitTestSupport.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibQUnitTestSupport.java @@ -17,36 +17,32 @@ package org.jetbrains.k2js.test.semantics; import com.google.common.collect.Lists; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.test.rhino.RhinoQUnitResultChecker; import org.jetbrains.k2js.test.rhino.RhinoResultChecker; -import java.util.HashMap; -import java.util.Map; - -import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; +import java.util.ArrayList; +import java.util.List; /** * A base class for any JS compile test cases which should run the generated JS file as a QUnit test case */ public abstract class StdLibQUnitTestSupport extends StdLibTestBase { + @Nullable @Override - protected void performChecksOnGeneratedJavaScript(String path, EcmaVersion version) throws Exception { - runQUnitTestCase(path, version); + protected RhinoResultChecker getResultChecker() { + return new RhinoQUnitResultChecker(); } - protected void runQUnitTestCase(String path, EcmaVersion version) throws Exception { - runQUnitTestCase(path, version, new HashMap()); - } - - protected void runQUnitTestCase(String path, EcmaVersion version, Map variables) throws Exception { - RhinoResultChecker checker = new RhinoQUnitResultChecker(); - runRhinoTest(Lists.newArrayList(path), - checker, variables, version, - Lists.newArrayList( - "js/js.translator/qunit/qunit.js", - "js/js.translator/qunit/headless.js" - )); + @NotNull + @Override + protected List additionalJsFiles(@NotNull EcmaVersion ecmaVersion) { + ArrayList files = Lists.newArrayList(super.additionalJsFiles(ecmaVersion)); + files.add("js/js.translator/qunit/qunit.js"); + files.add("js/js.translator/qunit/headless.js"); + return files; } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestBase.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestBase.java index c791c17d847..48382bc8440 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestBase.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestBase.java @@ -17,75 +17,39 @@ package org.jetbrains.k2js.test.semantics; import com.google.common.collect.Lists; -import com.intellij.util.ArrayUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.jet.cli.common.ExitCode; -import org.jetbrains.jet.cli.js.K2JSCompiler; +import org.jetbrains.annotations.Nullable; import org.jetbrains.k2js.config.EcmaVersion; +import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.test.SingleFileTranslationTest; -import org.jetbrains.k2js.test.utils.LibraryFilePathsUtil; +import org.jetbrains.k2js.test.rhino.RhinoResultChecker; import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +// TODO: should be dropped with derived classes? abstract class StdLibTestBase extends SingleFileTranslationTest { protected StdLibTestBase() { super("stdlib/"); } - protected void performStdLibTest(@NotNull Iterable ecmaVersions, - @NotNull String sourceDir, @NotNull String... stdLibFiles) throws Exception { - List files = constructFilesToCompileList(sourceDir, stdLibFiles); - compileFiles(ecmaVersions, files); - } + @Nullable + protected abstract RhinoResultChecker getResultChecker(); - private void compileFiles(@NotNull Iterable ecmaVersions, @NotNull List files) throws Exception { - List libFiles = LibraryFilePathsUtil.getBasicLibraryFiles(); - for (EcmaVersion version : ecmaVersions) { - String outputFilePath = getOutputFilePath(getTestName(false) + ".compiler.kt", version); - invokeCompiler(files, libFiles, version, outputFilePath); - performChecksOnGeneratedJavaScript(outputFilePath, version); - } - } - - /** - * Strategy method allowing the generated JS file to be invoked - */ - protected void performChecksOnGeneratedJavaScript(String path, EcmaVersion version) throws Exception { - } - - protected String moduleIdFromOutputFile(String path) { - String moduleId = new File(path).getName(); - if (moduleId.endsWith(".js")) { - moduleId = moduleId.substring(0, moduleId.length() - 3); - } - return moduleId; - } - - //TODO: reuse this in CompileMavenGeneratedJSLibrary - private static void invokeCompiler( - @NotNull List files, - @NotNull List libFiles, - @NotNull EcmaVersion version, - @NotNull String outputFilePath - ) { - System.out.println("Compiling with version: " + version + " to: " + outputFilePath); - - List args = new ArrayList(Arrays.asList("-output", outputFilePath, "-verbose", "-library-files")); - args.addAll(libFiles); - args.addAll(files); - ExitCode answer = new K2JSCompiler().exec(System.out, ArrayUtil.toStringArray(args)); - - assertEquals("Compile failed", ExitCode.OK, answer); - } - - @NotNull - private static List constructFilesToCompileList(@NotNull String sourceDir, @NotNull String[] stdLibFiles) { + protected void performStdLibTest( + @NotNull Iterable ecmaVersions, + @NotNull String sourceDir, + @NotNull String... stdLibFiles + ) throws Exception { List files = filesFromDir(sourceDir, stdLibFiles); - files.addAll(LibraryFilePathsUtil.getAdditionalLibraryFiles()); - return files; + String testFileName = getTestName(true) + ".kt"; + + generateJavaScriptFiles(files, testFileName, MainCallParameters.noCall(), ecmaVersions); + + RhinoResultChecker checker = getResultChecker(); + if (checker != null) { + runRhinoTests(testFileName, ecmaVersions, checker); + } } @NotNull diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java index a86a19dc460..9127ec1edfd 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java @@ -16,22 +16,35 @@ package org.jetbrains.k2js.test.semantics; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.k2js.test.rhino.RhinoResultChecker; + /** */ -public class StdLibTestToJSTest extends StdLibTestBase { +public class StdLibTestToJSTest extends StdLibQUnitTestSupport { public void testGenerateTestCase() throws Exception { performStdLibTest(DEFAULT_ECMA_VERSIONS, "libraries/stdlib/test", - "dom/DomTest.kt", + "js/JsArrayTest.kt", "js/MapJsTest.kt", - "js/JsDomTest.kt", - "collections/StreamTest.kt", - "collections/IteratorsTest.kt", "GetOrElseTest.kt", "collections/ListSpecificTest.kt", + "collections/IteratorsTest.kt", + "text/StringTest.kt", + // TODO review: somethings FAILED if run: + "js/JsDomTest.kt", + "dom/DomTest.kt", + "collections/StreamTest.kt", "collections/IterableTests.kt", "language/RangeTest.kt", - "language/RangeIterationTest.kt", - "text/StringTest.kt"); + "language/RangeIterationTest.kt" + ); + } + + @Nullable + @Override + protected RhinoResultChecker getResultChecker() { + // don't run, it's just smoke test this tests should be run in maven build. + return null; } } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java index c795ef845a7..d09300338ae 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java @@ -16,6 +16,9 @@ package org.jetbrains.k2js.test.semantics; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.k2js.test.rhino.RhinoResultChecker; + /** */ public class StdLibToJSTest extends StdLibTestBase { @@ -24,4 +27,10 @@ public class StdLibToJSTest extends StdLibTestBase { performStdLibTest(DEFAULT_ECMA_VERSIONS, "libraries/stdlib/src"); } + + @Nullable + @Override + protected RhinoResultChecker getResultChecker() { + return null; + } }