Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rbri simplify rebase #1492

Merged
merged 9 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,14 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ast.AstRoot;

/** This is a set of tests for parsing and using BigInts. */
public class BigIntTest {

private Context cx;
private Scriptable global;

@Before
public void init() {
cx = Context.enter();
cx.setLanguageVersion(Context.VERSION_ES6);
cx.getWrapFactory().setJavaPrimitiveWrap(false);
global = cx.initStandardObjects();
}

@After
public void terminate() {
Context.exit();
}

@Test
public void parse() throws IOException {
String[] INPUTS =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
Expand All @@ -26,20 +24,6 @@

public class JavaIterableTest {

private Context cx;
private Scriptable global;

@Before
public void init() {
cx = Context.enter();
global = cx.initStandardObjects();
}

@After
public void cleanup() {
Context.exit();
}

@Test
public void map() {
Map<Object, Object> map = new LinkedHashMap<>();
Expand Down Expand Up @@ -151,18 +135,15 @@ public void noIterable() {
Object o = new Object();
String script = "for (var e of value) ;";

assertThrows(
EcmaError.class,
() -> {
runScript(script, o);
});
assertThrows(EcmaError.class, () -> runScript(script, o));
}

private Object runScript(String scriptSourceText, Object value) {
return ContextFactory.getGlobal()
.call(
context -> {
context.setLanguageVersion(Context.VERSION_ES6);
Scriptable global = context.initStandardObjects();
Scriptable scope = context.newObject(global);
scope.put("value", scope, Context.javaToJS(value, scope));
return context.evaluateString(scope, scriptSourceText, "", 1, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

package org.mozilla.javascript.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextAction;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
* Misc utilities to make test code easier.
Expand Down Expand Up @@ -77,4 +82,45 @@ public static int[] getTestOptLevels() {
}
return DEFAULT_OPT_LEVELS;
}

public static void assertWithAllOptimizationLevels(final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
final Scriptable scope = cx.initStandardObjects();
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertWithAllOptimizationLevelsES6(
final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
final Scriptable scope = cx.initStandardObjects();
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertEvaluatorExceptionES6(String expectedMessage, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = cx.initStandardObjects();

try {
cx.evaluateString(scope, js, "test", 1, null);
fail("EvaluatorException expected");
} catch (EvaluatorException e) {
assertEquals(expectedMessage, e.getMessage());
}

return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@

/** @author hatanaka */
public class WrapFactoryTest {
/** javascript code */
private static String script =
"var result = typeof test;" //
+ "var mapResult = typeof map.get('test');" //
+ "var getResult = typeof object.get();";

/** for your reference default setting (javaPrimitiveWrap = true) */
@Test
Expand Down Expand Up @@ -73,6 +68,11 @@ private static void test(
ScriptableObject.putProperty(scope, "object", Optional.of(object));
ScriptableObject.putProperty(scope, "test", object);

final String script =
"var result = typeof test;" //
+ "var mapResult = typeof map.get('test');" //
+ "var getResult = typeof object.get();";

// execute script
cx.evaluateString(scope, script, "", 1, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import static org.junit.Assert.assertTrue;

import org.junit.Assert;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
Expand All @@ -21,20 +20,12 @@ public class NativeObjectTest {
*/
@Test
public void freeze_captureStackTrace() throws Exception {
try (Context cx = Context.enter()) {
Scriptable global = cx.initStandardObjects();
Object result =
cx.evaluateString(
global,
"var myError = {};\n"
+ "Error.captureStackTrace(myError);\n"
+ "Object.freeze(myError);"
+ "myError.stack;",
"",
1,
null);
Assert.assertTrue(result instanceof String);
}
final String script =
"var myError = {};\n"
+ "Error.captureStackTrace(myError);\n"
+ "Object.freeze(myError);\n"
+ "myError.stack.trim();";
Utils.assertWithAllOptimizationLevelsTopLevelScopeES6("at test.js:1", script);
}

/**
Expand All @@ -45,22 +36,14 @@ public void freeze_captureStackTrace() throws Exception {
*/
@Test
public void getOwnPropertyDescriptor_captureStackTrace() throws Exception {
try (Context cx = Context.enter()) {
Scriptable global = cx.initStandardObjects();
Object result =
cx.evaluateString(
global,
"var myError = {};\n"
+ "Error.captureStackTrace(myError);\n"
+ "var desc = Object.getOwnPropertyDescriptor(myError, 'stack');"
+ "'' + desc.get + '-' + desc.set + '-' + desc.value;",
"",
1,
null);
Assert.assertTrue(result instanceof String);
Assert.assertEquals(
"undefined-undefined-\tat :2", ((String) result).replaceAll("\\r|\\n", ""));
}
final String script =
"var myError = {};\n"
+ "Error.captureStackTrace(myError);\n"
+ "var desc = Object.getOwnPropertyDescriptor(myError, 'stack');\n"
+ "var res = '' + desc.get + '-' + desc.set + '-' + desc.value;\n"
+ "res = res.replace(/(\\n|\\r)/gm, '');";
Utils.assertWithAllOptimizationLevelsTopLevelScopeES6(
"undefined-undefined-\tat test.js:1", script);
}

/**
Expand All @@ -71,20 +54,12 @@ public void getOwnPropertyDescriptor_captureStackTrace() throws Exception {
*/
@Test
public void getOwnPropertyDescriptorAttributes_captureStackTrace() throws Exception {
try (Context cx = Context.enter()) {
Scriptable global = cx.initStandardObjects();
Object result =
cx.evaluateString(
global,
"var myError = {};\n"
+ "Error.captureStackTrace(myError);\n"
+ "var desc = Object.getOwnPropertyDescriptor(myError, 'stack');"
+ "desc.writable + ' ' + desc.configurable + ' ' + desc.enumerable",
"",
1,
null);
Assert.assertEquals("true true false", result);
}
final String script =
"var myError = {};\n"
+ "Error.captureStackTrace(myError);\n"
+ "var desc = Object.getOwnPropertyDescriptor(myError, 'stack');\n"
+ "desc.writable + ' ' + desc.configurable + ' ' + desc.enumerable";
Utils.assertWithAllOptimizationLevelsTopLevelScopeES6("true true false", script);
}

public static class JavaObj {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public class Test262SuiteTest {
private static String getOverriddenLevel() {
String optLevel = System.getProperty("TEST_OPTLEVEL");

if (optLevel == null || optLevel.equals("")) {
if (optLevel == null || optLevel.isEmpty()) {
optLevel = System.getenv("TEST_262_OPTLEVEL");
}
return optLevel;
Expand Down Expand Up @@ -200,7 +200,7 @@ public static void tearDownClass() {
try {
Path previousReportingDir = null;
Path currentReportingDir;
List<String> failures = new ArrayList<String>();
List<String> failures = new ArrayList<>();
int testCount = 0;
Path previousTestFileParentPath =
testDir.toPath(); // tracks the current directory for which files are
Expand Down Expand Up @@ -256,7 +256,7 @@ public static void tearDownClass() {
// in
// the
// previous directory failed
// If so, dont list all failing files, but list only the folder path
// If so, don't list all failing files, but list only the folder path
if (rollUpEnabled
&& (!testFilePath.startsWith(previousTestFileParentPath)
|| !testFilePath
Expand Down
72 changes: 72 additions & 0 deletions tests/src/test/java/org/mozilla/javascript/tests/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

package org.mozilla.javascript.tests;

import static org.junit.Assert.*;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextAction;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.TopLevel;

/**
* Misc utilities to make test code easier.
Expand Down Expand Up @@ -83,4 +88,71 @@ public static boolean isJavaVersionAtLeast(int desiredVersion) {
int version = Integer.parseInt(v[0]);
return version >= desiredVersion;
}

public static void assertWithAllOptimizationLevels(final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
final Scriptable scope = cx.initStandardObjects();
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertWithAllOptimizationLevels_1_8(
final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_1_8);
final Scriptable scope = cx.initStandardObjects();
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertWithAllOptimizationLevelsES6(
final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
final Scriptable scope = cx.initStandardObjects();
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertWithAllOptimizationLevelsTopLevelScopeES6(
final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
Scriptable scope = cx.initStandardObjects(new TopLevel());
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertEvaluatorExceptionES6(String expectedMessage, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = cx.initStandardObjects();

try {
cx.evaluateString(scope, js, "test", 1, null);
fail("EvaluatorException expected");
} catch (EvaluatorException e) {
assertEquals(expectedMessage, e.getMessage());
}

return null;
});
}
}
Loading
Loading