From adf38a0648b54685496d57967b7b76dfa85f204d Mon Sep 17 00:00:00 2001 From: James Strachan Date: Fri, 25 May 2012 20:38:04 +0100 Subject: [PATCH] added a working JS test case using the standard kotlin browser API for interacting with the DOM --- js/js.libraries/src/core/dom.kt | 1 + .../org/jetbrains/k2js/test/BasicTest.java | 7 ++- ...hinoFunctionNativeObjectResultChecker.java | 45 +++++++++++++++++++ .../rhino/RhinoFunctionResultChecker.java | 10 +++-- .../jetbrains/k2js/test/rhino/RhinoUtils.java | 17 +++++++ .../k2js/test/semantics/StdLibTest.java | 32 ++++++++++--- .../stdlib/cases/browserDocumentAccess.kt | 5 ++- 7 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionNativeObjectResultChecker.java diff --git a/js/js.libraries/src/core/dom.kt b/js/js.libraries/src/core/dom.kt index 243323d4aa9..8131d461d72 100644 --- a/js/js.libraries/src/core/dom.kt +++ b/js/js.libraries/src/core/dom.kt @@ -18,6 +18,7 @@ native public trait DOMImplementation {} native public trait DocumentType : Node {} native public trait Element : Node { fun appendChild(child : Node) : Unit = js.noImpl + fun getTextContent() : String = js.noImpl } native public trait Entity : Node {} native public trait EntityReference : Node {} 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 b20a4673a29..408014b2c3d 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/BasicTest.java @@ -29,6 +29,7 @@ import java.io.File; import java.util.Collections; import java.util.EnumSet; import java.util.List; +import java.util.Map; import static org.jetbrains.k2js.test.rhino.RhinoUtils.runRhinoTest; import static org.jetbrains.k2js.test.utils.JsTestUtils.convertFileNameToDotJsFile; @@ -107,10 +108,14 @@ public abstract class BasicTest extends TestWithEnvironment { protected void runRhinoTests(@NotNull List outputFilePaths, @NotNull RhinoResultChecker checker) throws Exception { for (String outputFilePath : outputFilePaths) { - runRhinoTest(withAdditionalFiles(outputFilePath), checker); + runRhinoTest(withAdditionalFiles(outputFilePath), checker, getRhinoTestVariables()); } } + protected Map getRhinoTestVariables() throws Exception { + return null; + } + protected static String casesDirectoryName() { return CASES; diff --git a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionNativeObjectResultChecker.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionNativeObjectResultChecker.java new file mode 100644 index 00000000000..59eba9537cc --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionNativeObjectResultChecker.java @@ -0,0 +1,45 @@ +/* + * Copyright 2010-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.rhino; + +import org.jetbrains.annotations.Nullable; +import org.mozilla.javascript.NativeJavaObject; + +/** + * Assert that a Rhino function returns a Native Java object which when unwrapped is equal to the expecte result + */ +public class RhinoFunctionNativeObjectResultChecker extends RhinoFunctionResultChecker { + + public RhinoFunctionNativeObjectResultChecker(@Nullable String namespaceName, String functionName, Object expectedResult) { + super(namespaceName, functionName, expectedResult); + } + + public RhinoFunctionNativeObjectResultChecker(String functionName, Object expectedResult) { + super(functionName, expectedResult); + } + + @Override + protected void assertResultValid(Object result) { + if (result instanceof NativeJavaObject) { + NativeJavaObject nativeJavaObject = (NativeJavaObject) result; + Object unwrap = nativeJavaObject.unwrap(); + super.assertResultValid(unwrap); + } else { + super.assertResultValid(result); + } + } +} diff --git a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionResultChecker.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionResultChecker.java index 1c991d594f3..d2f04c07e88 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionResultChecker.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoFunctionResultChecker.java @@ -20,12 +20,13 @@ import org.jetbrains.annotations.Nullable; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** * @author Pavel Talanov */ -public final class RhinoFunctionResultChecker implements RhinoResultChecker { +public class RhinoFunctionResultChecker implements RhinoResultChecker { private final String namespaceName; private final String functionName; @@ -44,8 +45,11 @@ public final class RhinoFunctionResultChecker implements RhinoResultChecker { @Override public void runChecks(Context context, Scriptable scope) throws Exception { Object result = evaluateFunction(context, scope); - assertTrue("Result is not what expected! Expected: " + expectedResult + " Evaluated : " + result, - result.equals(expectedResult)); + assertResultValid(result); + } + + protected void assertResultValid(Object result) { + assertEquals("Result of " + namespaceName + "." + functionName + "() is not what expected!", expectedResult, result); String report = namespaceName + "." + functionName + "() = " + Context.toString(result); System.out.println(report); } diff --git a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java index b676ac3f812..330ac850ff3 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/rhino/RhinoUtils.java @@ -22,6 +22,8 @@ import org.mozilla.javascript.Scriptable; import java.io.FileReader; import java.util.List; +import java.util.Map; +import java.util.Set; /** * @author Pavel Talanov @@ -45,8 +47,23 @@ public final class RhinoUtils { public static void runRhinoTest(@NotNull List fileNames, @NotNull RhinoResultChecker checker) throws Exception { + + runRhinoTest(fileNames, checker, null); + } + + public static void runRhinoTest(@NotNull List fileNames, + @NotNull RhinoResultChecker checker, + Map variables) throws Exception { Context context = Context.enter(); Scriptable scope = context.initStandardObjects(); + if (variables != null) { + Set> entries = variables.entrySet(); + for (Map.Entry entry : entries) { + String name = entry.getKey(); + Object value = entry.getValue(); + scope.put(name, scope, value); + } + } for (String filename : fileNames) { runFileWithRhino(filename, context, scope); } 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 1c41c0d5e82..26766d30e0d 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 @@ -20,10 +20,13 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.k2js.config.EcmaVersion; import org.jetbrains.k2js.facade.MainCallParameters; import org.jetbrains.k2js.test.SingleFileTranslationTest; +import org.jetbrains.k2js.test.rhino.RhinoFunctionNativeObjectResultChecker; +import org.jetbrains.k2js.test.rhino.RhinoFunctionResultChecker; +import org.w3c.dom.Document; +import org.w3c.dom.Element; -import java.util.Arrays; -import java.util.EnumSet; -import java.util.List; +import javax.xml.parsers.DocumentBuilderFactory; +import java.util.*; /** * @author Pavel Talanov @@ -34,9 +37,6 @@ public final class StdLibTest extends SingleFileTranslationTest { super("stdlib/"); } - public void testDummy() { - } - public void testBrowserDocumentAccessCompiles() throws Exception { generateJavaScriptFiles("browserDocumentAccess.kt", MainCallParameters.noCall(), EcmaVersion.all()); } @@ -48,5 +48,25 @@ public final class StdLibTest extends SingleFileTranslationTest { List files = Arrays.asList(getInputFilePath(kotlinFilename)); generateJavaScriptFiles(files, kotlinFilename, mainCallParameters, ecmaVersions); + runRhinoTests(getOutputFilePaths(kotlinFilename, ecmaVersions), + new RhinoFunctionNativeObjectResultChecker("test.browser", "foo", "Some Dynamically Created Content!!!")); + } + + @Override + protected Map getRhinoTestVariables() throws Exception { + Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); + Element root = document.createElement("root"); + //root.setIdAttribute("foo", true); + root.setAttribute("id", "foo"); + root.setIdAttribute("id", true); + document.appendChild(root); + + // lets test it actually works + Element foo = document.getElementById("foo"); + assertNotNull(foo); + + Map answer = new HashMap(); + answer.put("document", document); + return answer; } } diff --git a/js/js.translator/testFiles/stdlib/cases/browserDocumentAccess.kt b/js/js.translator/testFiles/stdlib/cases/browserDocumentAccess.kt index f7fc5a8d5ef..e11fff6cadf 100644 --- a/js/js.translator/testFiles/stdlib/cases/browserDocumentAccess.kt +++ b/js/js.translator/testFiles/stdlib/cases/browserDocumentAccess.kt @@ -2,9 +2,10 @@ package test.browser import kotlin.browser.document -fun foo() { +fun foo(): String { val element = document.getElementById("foo") if (element != null) { - element.appendChild(document.createTextNode("Some Dynamically Created Contenet!!!")) + element.appendChild(document.createTextNode("Some Dynamically Created Content!!!")) } + return element.getTextContent() } -- GitLab