added a working JS test case using the standard kotlin browser API for interacting with the DOM
This commit is contained in:
@@ -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 {}
|
||||
|
||||
@@ -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<String> outputFilePaths, @NotNull RhinoResultChecker checker) throws Exception {
|
||||
for (String outputFilePath : outputFilePaths) {
|
||||
runRhinoTest(withAdditionalFiles(outputFilePath), checker);
|
||||
runRhinoTest(withAdditionalFiles(outputFilePath), checker, getRhinoTestVariables());
|
||||
}
|
||||
}
|
||||
|
||||
protected Map<String,Object> getRhinoTestVariables() throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
protected static String casesDirectoryName() {
|
||||
return CASES;
|
||||
|
||||
+45
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<String> fileNames,
|
||||
@NotNull RhinoResultChecker checker) throws Exception {
|
||||
|
||||
runRhinoTest(fileNames, checker, null);
|
||||
}
|
||||
|
||||
public static void runRhinoTest(@NotNull List<String> fileNames,
|
||||
@NotNull RhinoResultChecker checker,
|
||||
Map<String,Object> variables) throws Exception {
|
||||
Context context = Context.enter();
|
||||
Scriptable scope = context.initStandardObjects();
|
||||
if (variables != null) {
|
||||
Set<Map.Entry<String,Object>> entries = variables.entrySet();
|
||||
for (Map.Entry<String, Object> entry : entries) {
|
||||
String name = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
scope.put(name, scope, value);
|
||||
}
|
||||
}
|
||||
for (String filename : fileNames) {
|
||||
runFileWithRhino(filename, context, scope);
|
||||
}
|
||||
|
||||
@@ -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<String> 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<String, Object> 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<String, Object> answer = new HashMap<String, Object>();
|
||||
answer.put("document", document);
|
||||
return answer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user