KJS: cleanup RhinoUtils and remove obsolete rhino result checkers

This commit is contained in:
Zalim Bashorov
2017-04-20 16:25:46 +03:00
parent 25dd31a2e0
commit 56810c5100
6 changed files with 14 additions and 331 deletions
@@ -1,35 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.js.test.rhino;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class CompositeRhinoResultsChecker implements RhinoResultChecker {
private final RhinoResultChecker[] children;
public CompositeRhinoResultsChecker(RhinoResultChecker... children) {
this.children = children;
}
@Override
public void runChecks(Context context, Scriptable scope) throws Exception {
for (RhinoResultChecker child : children) {
child.runChecks(context, scope);
}
}
}
@@ -1,48 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.js.test.rhino;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeJavaObject;
/**
* Assert that a Rhino function returns a Native Java object which when unwrapped is equal to the expected result
*/
public class RhinoFunctionNativeObjectResultChecker extends RhinoFunctionResultChecker {
public RhinoFunctionNativeObjectResultChecker(
@NotNull String moduleName,
@Nullable String packageName,
@NotNull String functionName,
@NotNull Object expectedResult
) {
super(moduleName, packageName, functionName, expectedResult);
}
@Override
protected void assertResultValid(Object result, Context context) {
if (result instanceof NativeJavaObject) {
NativeJavaObject nativeJavaObject = (NativeJavaObject) result;
Object unwrap = nativeJavaObject.unwrap();
super.assertResultValid(unwrap, context);
} else {
super.assertResultValid(result, context);
}
}
}
@@ -33,10 +33,6 @@ public class RhinoFunctionResultChecker implements RhinoResultChecker {
private final Object expectedResult;
private final boolean withModuleSystem;
public RhinoFunctionResultChecker(@NotNull String moduleId, @Nullable String packageName, @NotNull String functionName, @NotNull Object expectedResult) {
this(moduleId, packageName, functionName, expectedResult, false);
}
public RhinoFunctionResultChecker(
@NotNull String moduleId,
@Nullable String packageName,
@@ -58,7 +54,7 @@ public class RhinoFunctionResultChecker implements RhinoResultChecker {
assertResultValid(result, context);
}
protected void assertResultValid(Object result, Context context) {
private void assertResultValid(Object result, Context context) {
String ecmaVersion = context.getLanguageVersion() == Context.VERSION_1_8 ? "ecma5" : "ecma3";
assertEquals("Result of " + packageName + "." + functionName + "() is not what expected (" + ecmaVersion + ")!", expectedResult, result);
}
@@ -1,67 +0,0 @@
/*
* Copyright 2010-2015 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.kotlin.js.test.rhino;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.Scriptable;
import static org.jetbrains.kotlin.js.test.rhino.RhinoUtils.flushSystemOut;
import static org.junit.Assert.fail;
/**
* Runs the QUnit test cases in headless mode (without requiring a browser) and asserts they all PASS
*/
public class RhinoQUnitResultChecker implements RhinoResultChecker {
@Override
public void runChecks(Context context, Scriptable scope) throws Exception {
Object result = evaluateFunction(context, scope);
flushSystemOut(context, scope);
assertResultValid(result);
}
protected void assertResultValid(Object result) {
if (result instanceof NativeArray) {
NativeArray array = (NativeArray) result;
StringBuilder buffer = new StringBuilder();
for (Object value : array) {
String text = value.toString();
System.out.println(text);
if (!text.startsWith("PASS")) {
if (buffer.length() > 0) {
buffer.append("\n");
}
buffer.append(text);
}
}
if (buffer.length() > 0) {
fail(buffer.toString());
}
} else {
fail("Unknown QUnit result: " + result);
}
}
private Object evaluateFunction(Context cx, Scriptable scope) {
return cx.evaluateString(scope, functionCallString(), "function call", 0, null);
}
protected String functionCallString() {
return "runQUnitSuite()";
}
}
@@ -51,7 +51,7 @@ public final class RhinoSystemOutputChecker implements RhinoResultChecker {
}
@NotNull
public static String trimSpace(@NotNull String s) {
private static String trimSpace(@NotNull String s) {
String[] choppedUpString = s.trim().split("\\s");
StringBuilder sb = new StringBuilder();
for (String word : choppedUpString) {
@@ -16,38 +16,26 @@
package org.jetbrains.kotlin.js.test.rhino;
import com.google.common.collect.Sets;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.js.config.EcmaVersion;
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
import org.mozilla.javascript.*;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.jetbrains.kotlin.js.test.BasicTest.DIST_DIR_JS_PATH;
import static org.jetbrains.kotlin.js.test.BasicTest.TEST_DATA_DIR_PATH;
import static org.jetbrains.kotlin.js.test.BasicBoxTest.DIST_DIR_JS_PATH;
public final class RhinoUtils {
private static final String KOTLIN_JS_LIB_ECMA_5 = TEST_DATA_DIR_PATH + "kotlin_lib_ecma5.js";
private static final Set<String> IGNORED_JSHINT_WARNINGS = Sets.newHashSet();
private static final NativeObject JSHINT_OPTIONS = new NativeObject();
public static final String OPTIMIZATION_LEVEL_TEST_VARIABLE = "rhinoOptimizationLevel";
public static final int OPTIMIZATION_OFF = -1;
private static final int OPTIMIZATION_DEFAULT = 0;
private static final int OPTIMIZATION_OFF = -1;
private static final String SETUP_KOTLIN_OUTPUT = "kotlin.kotlin.io.output = new kotlin.kotlin.io.BufferedOutput();";
private static final String FLUSH_KOTLIN_OUTPUT = "kotlin.kotlin.io.output.flush();";
@@ -56,49 +44,6 @@ public final class RhinoUtils {
@NotNull
private static final Map<EcmaVersion, ScriptableObject> versionToScope = ContainerUtil.newHashMap();
static {
// don't read JS, use kotlin and idea debugger ;)
//IGNORED_JSHINT_WARNINGS.add(
// "Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.");
//IGNORED_JSHINT_WARNINGS.add("Expected exactly one space between ';' and 'else'.");
//// stupid jslint, see $initializer fun
//IGNORED_JSHINT_WARNINGS.add("Do not wrap function literals in parens unless they are to be immediately invoked.");
//// stupid jslint
//IGNORED_JSHINT_WARNINGS.add("'_' was used before it was defined.");
//IGNORED_JSHINT_WARNINGS.add("Empty block.");
IGNORED_JSHINT_WARNINGS.add("Expected to see a statement and instead saw a block.");
//IGNORED_JSHINT_WARNINGS.add("Unexpected '.'.");
//// todo
//IGNORED_JSHINT_WARNINGS.add("Strange loop.");
//IGNORED_JSHINT_WARNINGS.add("Weird relation.");
//IGNORED_JSHINT_WARNINGS.add("Weird condition.");
//IGNORED_JSHINT_WARNINGS.add("Expected ';' and instead saw ','.");
//IGNORED_JSHINT_WARNINGS.add("Expected an identifier and instead saw ','.");
//// it is normal,
//IGNORED_JSHINT_WARNINGS.add("Unexpected 'else' after 'return'.");
IGNORED_JSHINT_WARNINGS.add("Expected ')' and instead saw 'return'.");
//IGNORED_JSHINT_WARNINGS.add()
// todo fix dart ast?
//JSHINT_OPTIONS.defineProperty("white", true, ScriptableObject.READONLY);
// vars, http://uxebu.com/blog/2010/04/02/one-var-statement-for-one-varia
// ble/
//JSHINT_OPTIONS.defineProperty("vars", true, ScriptableObject.READONLY);
NativeArray globals = new NativeArray(new Object[] {"Kotlin"});
JSHINT_OPTIONS.defineProperty("predef", globals, ScriptableObject.READONLY);
// todo
JSHINT_OPTIONS.defineProperty("expr", true, ScriptableObject.READONLY);
JSHINT_OPTIONS.defineProperty("asi", true, ScriptableObject.READONLY);
JSHINT_OPTIONS.defineProperty("laxcomma", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("nomen", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("continue", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("plusplus", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("evil", true, ScriptableObject.READONLY);
//JSHINT_OPTIONS.defineProperty("indent", 2, ScriptableObject.READONLY);
}
private RhinoUtils() {
}
@@ -117,42 +62,18 @@ public final class RhinoUtils {
}
public static void runRhinoTest(@NotNull List<String> fileNames, @NotNull RhinoResultChecker checker) throws Exception {
runRhinoTest(fileNames, checker, null, EcmaVersion.defaultVersion());
}
public static void runRhinoTest(@NotNull List<String> fileNames,
@NotNull RhinoResultChecker checker,
@Nullable Map<String, Object> variables,
@NotNull EcmaVersion ecmaVersion)
throws Exception {
runRhinoTest(fileNames, checker, variables, ecmaVersion, Collections.emptyList());
}
public static void runRhinoTest(@NotNull List<String> fileNames,
@NotNull RhinoResultChecker checker,
@Nullable Map<String, Object> variables,
@NotNull EcmaVersion ecmaVersion,
@NotNull List<String> jsLibraries) throws Exception {
EcmaVersion ecmaVersion = EcmaVersion.defaultVersion();
Context context = createContext(ecmaVersion);
context.setOptimizationLevel(OPTIMIZATION_OFF);
if (variables != null) {
context.setOptimizationLevel(getOptimizationLevel(variables));
}
try {
Scriptable scope = getScope(ecmaVersion, context, jsLibraries);
putGlobalVariablesIntoScope(scope, variables);
Scriptable scope = getScope(ecmaVersion, context);
context.evaluateString(scope, SETUP_KOTLIN_OUTPUT, "setup kotlin output", 0, null);
for (String filename : fileNames) {
runFileWithRhino(filename, context, scope);
//String problems = lintIt(context, filename, scope);
//if (problems != null) {
// //fail(problems);
// System.out.print(problems);
//}
}
finishScope(scope);
@@ -164,8 +85,8 @@ public final class RhinoUtils {
}
@NotNull
private static Scriptable getScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List<String> jsLibraries) {
Scriptable parentScope = getParentScope(version, context, jsLibraries);
private static Scriptable getScope(@NotNull EcmaVersion version, @NotNull Context context) {
Scriptable parentScope = getParentScope(version, context);
Scriptable scope = context.newObject(parentScope);
scope.put("kotlin-test", scope, parentScope.get("kotlin-test", parentScope));
@@ -178,12 +99,12 @@ public final class RhinoUtils {
}
@NotNull
private static Scriptable getParentScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List<String> jsLibraries) {
return versionToScope.computeIfAbsent(version, v -> initScope(v, context, jsLibraries));
private static Scriptable getParentScope(@NotNull EcmaVersion version, @NotNull Context context) {
return versionToScope.computeIfAbsent(version, v -> initScope(context));
}
@NotNull
private static ScriptableObject initScope(@NotNull EcmaVersion version, @NotNull Context context, @NotNull List<String> jsLibraries) {
private static ScriptableObject initScope(@NotNull Context context) {
ScriptableObject scope = context.initStandardObjects();
try {
runFileWithRhino(DIST_DIR_JS_PATH + "../../js/js.translator/testData/rhino-polyfills.js", context, scope);
@@ -192,16 +113,10 @@ public final class RhinoUtils {
context.evaluateString(scope,
"this['kotlin-test'].kotlin.test.overrideAsserter_wbnzx$(new this['kotlin-test'].kotlin.test.DefaultAsserter());",
"change asserter to DefaultAsserter", 1, null);
//runFileWithRhino(pathToTestFilesRoot() + "jshint.js", context, scope);
for (String jsLibrary : jsLibraries) {
runFileWithRhino(jsLibrary, context, scope);
}
}
catch (Exception e) {
throw ExceptionUtilsKt.rethrow(e);
}
//scope.sealObject();
return scope;
}
@@ -216,85 +131,7 @@ public final class RhinoUtils {
return context;
}
private static void putGlobalVariablesIntoScope(@NotNull Scriptable scope, @Nullable Map<String, Object> variables) {
if (variables == null) {
return;
}
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);
}
}
@NotNull
public static String getKotlinLibFile(@NotNull EcmaVersion ecmaVersion) {
assert ecmaVersion == EcmaVersion.v5 : "Ecma 3 is deprecate";
return KOTLIN_JS_LIB_ECMA_5;
}
static void flushSystemOut(@NotNull Context context, @NotNull Scriptable scope) {
context.evaluateString(scope, FLUSH_KOTLIN_OUTPUT, "test", 0, null);
}
@Nullable
private static String lintIt(Context context, String fileName, ScriptableObject scope) throws IOException {
if (Boolean.valueOf(System.getProperty("test.lint.skip"))) {
return null;
}
Object[] args = {FileUtil.loadFile(new File(fileName), true), JSHINT_OPTIONS};
Function function = (Function) ScriptableObject.getProperty(scope.getParentScope(), "JSHINT");
Object status = function.call(context, scope.getParentScope(), scope.getParentScope(), args);
if (!(Boolean) Context.jsToJava(status, Boolean.class)) {
Object errors = function.get("errors", scope);
StringBuilder sb = new StringBuilder(fileName);
for (Object errorObj : ((NativeArray) errors)) {
if (errorObj == null) {
continue;
}
NativeObject e = (NativeObject) errorObj;
int line = toInt(e.get("line"));
int character = toInt(e.get("character"));
if (line < 0 || character < 0) {
continue;
}
Object reasonObj = e.get("reason");
if (reasonObj instanceof String) {
String reason = (String) reasonObj;
if (IGNORED_JSHINT_WARNINGS.contains(reason) ||
reason.startsWith("Expected exactly one space between ')' and ") ||
reason.startsWith("Expected '}' to match '{' from line ") ||
reason.startsWith("Expected '{' and instead saw ")) {
continue;
}
sb.append('\n').append(line).append(':').append(character).append(' ').append(reason);
}
}
return sb.length() == fileName.length() ? null : sb.toString();
}
return null;
}
private static int toInt(Object obj) {
if (obj instanceof Number) {
return ((Number) obj).intValue();
}
return -1;
}
private static int getOptimizationLevel(@NotNull Map<String, Object> testVariables) {
Object optimizationLevel = testVariables.get(OPTIMIZATION_LEVEL_TEST_VARIABLE);
if (optimizationLevel instanceof Integer) {
return (Integer) optimizationLevel;
}
return OPTIMIZATION_DEFAULT;
}
}