diff --git a/testsrc/org/mozilla/javascript/tests/Utils.java b/testsrc/org/mozilla/javascript/tests/Utils.java index 76dab93284..558afb7a52 100644 --- a/testsrc/org/mozilla/javascript/tests/Utils.java +++ b/testsrc/org/mozilla/javascript/tests/Utils.java @@ -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. @@ -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; + }); + } } diff --git a/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java b/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java index e23add1c28..d9891febc7 100644 --- a/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java +++ b/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java @@ -5,186 +5,75 @@ /** Test for the Object.hasOwn */ package org.mozilla.javascript.tests.es2022; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.tests.Utils; public class NativeObjectTest { @Test public void hasStringOwn() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ test: '123' }, 'test');\n" - + "'result = ' + result", - "test", - 1, - null); - assertEquals("result = true", result); - - return null; - }); + final String code = + "let result = Object.hasOwn({ test: '123' }, 'test');\n" + "'result = ' + result"; + Utils.assertWithAllOptimizationLevelsES6("result = true", code); } @Test public void hasUndefinedOwn() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ test: undefined }, 'test');\n" - + "'result = ' + result;", - "test", - 1, - null); - assertEquals("result = true", result); - - return null; - }); + final String code = + "let result = Object.hasOwn({ test: undefined }, 'test');\n" + + "'result = ' + result;"; + Utils.assertWithAllOptimizationLevelsES6("result = true", code); } @Test public void hasNullOwn() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ test: null }, 'test');\n" - + "'result = ' + result;", - "test", - 1, - null); - assertEquals("result = true", result); - - return null; - }); + final String code = + "let result = Object.hasOwn({ test: null }, 'test');\n" + "'result = ' + result;"; + Utils.assertWithAllOptimizationLevelsES6("result = true", code); } @Test public void hasArrayPropertyOwn() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "let dessert = [\"cake\", \"coffee\", \"chocolate\"];\n" - + "let result = Object.hasOwn(dessert, 2);\n" - + "'result = ' + result;", - "test", - 1, - null); - assertEquals("result = true", result); - - return null; - }); + final String code = + "let dessert = [\"cake\", \"coffee\", \"chocolate\"];\n" + + "let result = Object.hasOwn(dessert, 2);\n" + + "'result = ' + result;"; + Utils.assertWithAllOptimizationLevelsES6("result = true", code); } @Test - public void hHasNoOwn() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ cake: 123 }, 'test');\n" - + "'result = ' + result", - "test", - 1, - null); - assertEquals("result = false", result); - - return null; - }); + public void hasNoOwn() { + final String code = + "let result = Object.hasOwn({ cake: 123 }, 'test');\n" + "'result = ' + result"; + Utils.assertWithAllOptimizationLevelsES6("result = false", code); } @Test public void createHasOwn() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var foo = Object.create(null);\n" - + "foo.prop = 'test';\n" - + "var result = Object.hasOwn(foo, 'prop');\n" - + "'result = ' + result;", - "test", - 1, - null); - assertEquals("result = true", result); - - return null; - }); + final String code = + "var foo = Object.create(null);\n" + + "foo.prop = 'test';\n" + + "var result = Object.hasOwn(foo, 'prop');\n" + + "'result = ' + result;"; + Utils.assertWithAllOptimizationLevelsES6("result = true", code); } @Test public void createNoHasOwn() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var result = Object.hasOwn(Object.create({ q: 321 }), 'q');\n" - + "'result = ' + result; ", - "test", - 1, - null); - assertEquals("result = false", result); - - return null; - }); + final String code = + "var result = Object.hasOwn(Object.create({ q: 321 }), 'q');\n" + + "'result = ' + result; "; + Utils.assertWithAllOptimizationLevelsES6("result = false", code); } @Test public void calledTest() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var called = false;\n" - + "try {\n" - + " Object.hasOwn(null, { toString() { called = true } });\n" - + "} catch (e) {}\n" - + "'called = ' + called;", - "test", - 1, - null); - assertEquals("called = false", result); - - return null; - }); + final String code = + "var called = false;\n" + + "try {\n" + + " Object.hasOwn(null, { toString() { called = true } });\n" + + "} catch (e) {}\n" + + "'called = ' + called;"; + Utils.assertWithAllOptimizationLevelsES6("called = false", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es5/GeneratorToStringTest.java b/testsrc/org/mozilla/javascript/tests/es5/GeneratorToStringTest.java index 4efc7fc4dc..0cdf31fda1 100644 --- a/testsrc/org/mozilla/javascript/tests/es5/GeneratorToStringTest.java +++ b/testsrc/org/mozilla/javascript/tests/es5/GeneratorToStringTest.java @@ -2,8 +2,6 @@ import static org.junit.Assert.assertEquals; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.mozilla.javascript.Context; import org.mozilla.javascript.ScriptableObject; @@ -11,19 +9,6 @@ import org.mozilla.javascript.tests.Utils; public class GeneratorToStringTest { - private Context cx; - - @Before - public void setUp() { - cx = Context.enter(); - cx.setLanguageVersion(Context.VERSION_ES6); - } - - @After - public void tearDown() { - Context.exit(); - } - @Test public void generatorsTest() { String code = " function* f() {\n" + " yield 1;\n" + " }; f.toString();"; diff --git a/testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java b/testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java index d961ea53e5..aa14cdc499 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java @@ -4,11 +4,7 @@ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.tests.Utils; /** Tests for Arguments support. */ @@ -22,7 +18,7 @@ public void argumentsSymbolIterator() { + "}" + "foo()"; - test(true, code); + Utils.assertWithAllOptimizationLevelsES6(true, code); } @Test @@ -33,7 +29,7 @@ public void argumentsSymbolIterator2() { + "}" + "foo()"; - test(true, code); + Utils.assertWithAllOptimizationLevelsES6(true, code); } @Test @@ -48,19 +44,6 @@ public void argumentsForOf() { + "}" + "foo(1, 2, 3, 5)"; - test("1235", code); - } - - private static void test(Object expected, String js) { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - - Object result = cx.evaluateString(scope, js, "test", 1, null); - assertEquals(expected, result); - - return null; - }); + Utils.assertWithAllOptimizationLevelsES6("1235", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java b/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java index 1424487008..1ac50aaaff 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java @@ -7,32 +7,15 @@ */ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.tests.Utils; public class BoundFunctionTest { @Test public void ctorCallableThis() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - " function foo() {};\n" + " foo.bind({}).name;", - "test", - 1, - null); - assertEquals("bound foo", result); + String code = "function foo() {};\n" + " foo.bind({}).name;"; - return null; - }); + Utils.assertWithAllOptimizationLevelsES6("bound foo", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java b/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java index 0478873f4f..acc118f4ec 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java @@ -4,38 +4,21 @@ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.tests.Utils; public class ES6IteratorTest { @Test public void valueDone() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - " var res = '';\n" - + " var arr = ['x'];\n" - + " var arrIter = arr[Symbol.iterator]();\n" - + " for (var p in arrIter.next()) {\n" - + " res = res + p + ' ';\n" - + " }\n", - "test", - 1, - null); - // this is the order used by all current browsers - assertEquals("value done ", result); + String code = + " var res = '';\n" + + " var arr = ['x'];\n" + + " var arrIter = arr[Symbol.iterator]();\n" + + " for (var p in arrIter.next()) {\n" + + " res = res + p + ' ';\n" + + " }\n"; - return null; - }); + Utils.assertWithAllOptimizationLevelsES6("value done ", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/FunctionsRestParametersTest.java b/testsrc/org/mozilla/javascript/tests/es6/FunctionsRestParametersTest.java index 84c4755d53..db778591fe 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/FunctionsRestParametersTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/FunctionsRestParametersTest.java @@ -4,15 +4,7 @@ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.EvaluatorException; -import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.tests.Utils; /** @@ -22,21 +14,6 @@ */ public class FunctionsRestParametersTest { - private Context cx; - private ScriptableObject scope; - - @Before - public void setUp() { - cx = Context.enter(); - cx.setLanguageVersion(Context.VERSION_ES6); - scope = cx.initStandardObjects(); - } - - @After - public void tearDown() { - Context.exit(); - } - @Test public void oneRestArg() { String code = @@ -45,7 +22,7 @@ public void oneRestArg() { + "}\n" + "rest(1, 'abc', 2, '##').toString();\n"; - test("1,abc,2,##", code); + Utils.assertWithAllOptimizationLevelsES6("1,abc,2,##", code); } @Test @@ -58,7 +35,7 @@ public void oneRestArgActivation() { + "}\n" + " rest(1, 'abc', 2, '##').toString();\n"; - test("1,abc,2,##", code); + Utils.assertWithAllOptimizationLevelsES6("1,abc,2,##", code); } @Test @@ -70,7 +47,7 @@ public void oneRestArgNothingProvided() { + "var r = rest();\n" + "'' + Array.isArray(r) + '-' + r.length;\n"; - test("true-0", code); + Utils.assertWithAllOptimizationLevelsES6("true-0", code); } @Test @@ -84,7 +61,7 @@ public void oneRestArgNothingProvidedActivation() { + "var r = rest();\n" + "'' + Array.isArray(r) + '-' + r.length;\n"; - test("true-0", code); + Utils.assertWithAllOptimizationLevelsES6("true-0", code); } @Test @@ -96,7 +73,7 @@ public void oneRestArgOneProvided() { + "var r = rest('xy');\n" + "'' + Array.isArray(r) + '-' + r.length;\n"; - test("true-1", code); + Utils.assertWithAllOptimizationLevelsES6("true-1", code); } @Test @@ -110,7 +87,7 @@ public void oneRestArgOneProvidedActivation() { + "var r = rest('xy');\n" + "'' + Array.isArray(r) + '-' + r.length;\n"; - test("true-1", code); + Utils.assertWithAllOptimizationLevelsES6("true-1", code); } @Test @@ -121,7 +98,7 @@ public void twoRestArg() { + "}\n" + "rest(1, 'abc', 2, '##').toString();\n"; - test("abc,2,##", code); + Utils.assertWithAllOptimizationLevelsES6("abc,2,##", code); } @Test @@ -134,7 +111,7 @@ public void twoRestArgActivation() { + "}\n" + "rest(1, 'abc', 2, '##').toString();\n"; - test("abc,2,##", code); + Utils.assertWithAllOptimizationLevelsES6("abc,2,##", code); } @Test @@ -145,7 +122,7 @@ public void twoRestArgNothingProvided() { + "}\n" + "rest();\n"; - test("undefined - true-0", code); + Utils.assertWithAllOptimizationLevelsES6("undefined - true-0", code); } @Test @@ -158,7 +135,7 @@ public void twoRestArgNothingProvidedActivation() { + "}\n" + "rest();\n"; - test("undefined - true-0", code); + Utils.assertWithAllOptimizationLevelsES6("undefined - true-0", code); } @Test @@ -169,7 +146,7 @@ public void twoRestArgOneProvided() { + "}\n" + "rest('77');"; - test("77 - true-0", code); + Utils.assertWithAllOptimizationLevelsES6("77 - true-0", code); } @Test @@ -182,7 +159,7 @@ public void twoRestArgOneProvidedActivation() { + "}\n" + "rest('77');"; - test("77 - true-0", code); + Utils.assertWithAllOptimizationLevelsES6("77 - true-0", code); } @Test @@ -193,7 +170,7 @@ public void arguments() { + "}\n" + "'' + rest('77') + '-' + rest(1, 2, 3, 4);\n"; - test("1-4", code); + Utils.assertWithAllOptimizationLevelsES6("1-4", code); } @Test @@ -206,7 +183,7 @@ public void argumentsActivation() { + "}\n" + "'' + rest('77') + '-' + rest(1, 2, 3, 4);\n"; - test("1-4", code); + Utils.assertWithAllOptimizationLevelsES6("1-4", code); } @Test @@ -217,7 +194,7 @@ public void argLength() { + "}\n" + " rest(1,2) + '-' + rest(1) + '-' + rest();\n"; - test("2-1-0", code); + Utils.assertWithAllOptimizationLevelsES6("2-1-0", code); } @Test @@ -230,7 +207,7 @@ public void argLengthActivation() { + "}\n" + " rest(1,2) + '-' + rest(1) + '-' + rest();\n"; - test("2-1-0", code); + Utils.assertWithAllOptimizationLevelsES6("2-1-0", code); } @Test @@ -240,7 +217,7 @@ public void length() { + "function foo2(arg, ...restArgs) {}\n" + "foo1.length + '-' + foo2.length;\n"; - test("0-1", code); + Utils.assertWithAllOptimizationLevelsES6("0-1", code); } @Test @@ -251,7 +228,8 @@ public void string1() { + "}\n" + "rest.toString();\n"; - test("\nfunction rest(...restArgs) {\n return restArgs.length;\n}\n", code); + Utils.assertWithAllOptimizationLevelsES6( + "\nfunction rest(...restArgs) {\n return restArgs.length;\n}\n", code); } @Test @@ -262,57 +240,28 @@ public void string2() { + "}\n" + "rest.toString();\n"; - test("\nfunction rest(arg, ...restArgs) {\n return restArgs.length;\n}\n", code); + Utils.assertWithAllOptimizationLevelsES6( + "\nfunction rest(arg, ...restArgs) {\n return restArgs.length;\n}\n", code); } @Test public void trailingComma() { String code = "function rest(...restArgs,) {\n" + " return restArgs;\n" + "}\n"; - assertEvaluatorException("parameter after rest parameter (test#1)", code); + Utils.assertEvaluatorExceptionES6("parameter after rest parameter (test#1)", code); } @Test public void twoRestParams() { String code = "function rest(...rest1, ...rest2) {\n" + " return restArgs;\n" + "}\n"; - assertEvaluatorException("parameter after rest parameter (test#1)", code); + Utils.assertEvaluatorExceptionES6("parameter after rest parameter (test#1)", code); } @Test public void paramAfterRestParam() { String code = "function rest(...rest1, param) {\n" + " return restArgs;\n" + "}\n"; - assertEvaluatorException("parameter after rest parameter (test#1)", code); - } - - private static void test(Object expected, String js) { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - - Object result = cx.evaluateString(scope, js, "test", 1, null); - assertEquals(expected, result); - - return null; - }); - } - - private static void assertEvaluatorException(String expected, 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(expected, e.getMessage()); - } - - return null; - }); + Utils.assertEvaluatorExceptionES6("parameter after rest parameter (test#1)", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/Issue1297FunctionNameTest.java b/testsrc/org/mozilla/javascript/tests/es6/Issue1297FunctionNameTest.java index 7ea898064d..f9df9af9bf 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/Issue1297FunctionNameTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/Issue1297FunctionNameTest.java @@ -4,28 +4,20 @@ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.tests.Utils; /** Test that we can redefine a function's name. */ public class Issue1297FunctionNameTest { - private static final String source = - "'use strict';" - + "function X() {};\n" - + "Object.defineProperty(X, 'name', {value: 'y', configurable: true, writable: true});" - + "X.name"; @Test public void canSetFunctionName() { - Utils.runWithAllOptimizationLevels( - cx -> { - Scriptable scope = cx.initStandardObjects(null); - Object result = cx.evaluateString(scope, source, "test", 1, null); - assertEquals("y", result); - return null; - }); + final String code = + "'use strict';" + + "function X() {};\n" + + "Object.defineProperty(X, 'name', {value: 'y', configurable: true, writable: true});" + + "X.name"; + + Utils.assertWithAllOptimizationLevels("y", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeArray3Test.java b/testsrc/org/mozilla/javascript/tests/es6/NativeArray3Test.java index 059996c638..558a4bc6e5 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeArray3Test.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeArray3Test.java @@ -4,11 +4,7 @@ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.tests.Utils; /** Tests for NativeArray support. */ @@ -18,28 +14,28 @@ public class NativeArray3Test { public void iteratorPrototype() { String code = "Array.prototype.values === [][Symbol.iterator]"; - test(true, code); + Utils.assertWithAllOptimizationLevelsES6(true, code); } @Test public void iteratorInstances() { String code = "[1, 2][Symbol.iterator] === [][Symbol.iterator]"; - test(true, code); + Utils.assertWithAllOptimizationLevelsES6(true, code); } @Test public void iteratorPrototypeName() { String code = "Array.prototype.values.name;"; - test("values", code); + Utils.assertWithAllOptimizationLevelsES6("values", code); } @Test public void iteratorInstanceName() { String code = "[][Symbol.iterator].name;"; - test("values", code); + Utils.assertWithAllOptimizationLevelsES6("values", code); } @Test @@ -56,19 +52,6 @@ public void redefineIterator() { + "res += arr[Symbol.iterator].toString().includes('return i;');\n" + "res;"; - test("false - true - false", code); - } - - private static void test(Object expected, String js) { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - - Object result = cx.evaluateString(scope, js, "test", 1, null); - assertEquals(expected, result); - - return null; - }); + Utils.assertWithAllOptimizationLevelsES6("false - true - false", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java index 1b5aa513cc..65765f79c4 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java @@ -14,96 +14,48 @@ public class NativeFunctionTest { @Test public void functionPrototypeLength() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var desc=Object.getOwnPropertyDescriptor(Function.prototype, 'length');\n" - + "var res = 'configurable: ' + desc.configurable;\n" - + "res += ' enumerable: ' + desc.enumerable;\n" - + "res += ' writable: ' + desc.writable;", - "test", - 1, - null); - assertEquals("configurable: true enumerable: false writable: false", result); - - return null; - }); + final String code = + "var desc=Object.getOwnPropertyDescriptor(Function.prototype, 'length');\n" + + "var res = 'configurable: ' + desc.configurable;\n" + + "res += ' enumerable: ' + desc.enumerable;\n" + + "res += ' writable: ' + desc.writable;"; + Utils.assertWithAllOptimizationLevelsES6( + "configurable: true enumerable: false writable: false", code); } @Test public void functionPrototypeName() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var desc=Object.getOwnPropertyDescriptor(Function.prototype, 'name');\n" - + "var res = 'configurable: ' + desc.configurable;\n" - + "res += ' enumerable: ' + desc.enumerable;\n" - + "res += ' writable: ' + desc.writable;", - "test", - 1, - null); - assertEquals("configurable: true enumerable: false writable: false", result); - - return null; - }); + final String code = + "var desc=Object.getOwnPropertyDescriptor(Function.prototype, 'name');\n" + + "var res = 'configurable: ' + desc.configurable;\n" + + "res += ' enumerable: ' + desc.enumerable;\n" + + "res += ' writable: ' + desc.writable;"; + Utils.assertWithAllOptimizationLevelsES6( + "configurable: true enumerable: false writable: false", code); } @Test public void functionLength() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var f=function(){};\n" - + "var desc=Object.getOwnPropertyDescriptor(f, 'length');\n" - + "var res = 'configurable: ' + desc.configurable;\n" - + "res += ' enumerable: ' + desc.enumerable;\n" - + "res += ' writable: ' + desc.writable;", - "test", - 1, - null); - assertEquals("configurable: true enumerable: false writable: false", result); - - return null; - }); + final String code = + "var f=function(){};\n" + + "var desc=Object.getOwnPropertyDescriptor(f, 'length');\n" + + "var res = 'configurable: ' + desc.configurable;\n" + + "res += ' enumerable: ' + desc.enumerable;\n" + + "res += ' writable: ' + desc.writable;"; + Utils.assertWithAllOptimizationLevelsES6( + "configurable: true enumerable: false writable: false", code); } @Test public void functionName() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var f=function(){};\n" - + "var desc=Object.getOwnPropertyDescriptor(f, 'name');\n" - + "var res = 'configurable: ' + desc.configurable;\n" - + "res += ' enumerable: ' + desc.enumerable;\n" - + "res += ' writable: ' + desc.writable;", - "test", - 1, - null); - assertEquals("configurable: true enumerable: false writable: false", result); - - return null; - }); + final String code = + "var f=function(){};\n" + + "var desc=Object.getOwnPropertyDescriptor(f, 'name');\n" + + "var res = 'configurable: ' + desc.configurable;\n" + + "res += ' enumerable: ' + desc.enumerable;\n" + + "res += ' writable: ' + desc.writable;"; + Utils.assertWithAllOptimizationLevelsES6( + "configurable: true enumerable: false writable: false", code); } @Test diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java index a314df210b..0bc8e3de59 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java @@ -1,167 +1,85 @@ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.tests.Utils; public class NativeNumberPropertyTest { @Test public void definingAProperty() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var func = function (number) {" - + " number.snippetText = 'abc';" - + " return number.snippetText;" - + "};" - + "try { " - + " '' + func(-334918463);" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("undefined", result); - - return null; - }); + final String code = + "var func = function (number) {" + + " number.snippetText = 'abc';" + + " return number.snippetText;" + + "};" + + "try { " + + " '' + func(-334918463);" + + "} catch (e) { e.message }"; + Utils.assertWithAllOptimizationLevelsES6("undefined", code); } @Test public void definingAPropertyStrict() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var func = function (number) {" - + " 'use strict';" - + " number.snippetText = 'abc';" - + " return number.snippetText;" - + "};" - + "try { " - + " '' + func(-334918463);" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals( - "Cannot set property \"snippetText\" of -334918463 to \"abc\"", result); - - return null; - }); + final String code = + "var func = function (number) {" + + " 'use strict';" + + " number.snippetText = 'abc';" + + " return number.snippetText;" + + "};" + + "try { " + + " '' + func(-334918463);" + + "} catch (e) { e.message }"; + Utils.assertWithAllOptimizationLevelsES6( + "Cannot set property \"snippetText\" of -334918463 to \"abc\"", code); } @Test public void extensible() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var func = function (number) {" - + " return Object.isExtensible(number) + ' ' + Object.isExtensible(new Object(number));" - + "};" - + "try { " - + " func(-334918463);" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("false true", result); - - return null; - }); + final String code = + "var func = function (number) {" + + " return Object.isExtensible(number) + ' ' + Object.isExtensible(new Object(number));" + + "};" + + "try { " + + " func(-334918463);" + + "} catch (e) { e.message }"; + Utils.assertWithAllOptimizationLevelsES6("false true", code); } @Test public void extensibleStrict() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var func = function (number) {" - + " 'use strict';" - + " return Object.isExtensible(number) + ' ' + Object.isExtensible(new Object(number));" - + "};" - + "try { " - + " func(-334918463);" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("false true", result); - - return null; - }); + final String code = + "var func = function (number) {" + + " 'use strict';" + + " return Object.isExtensible(number) + ' ' + Object.isExtensible(new Object(number));" + + "};" + + "try { " + + " func(-334918463);" + + "} catch (e) { e.message }"; + Utils.assertWithAllOptimizationLevelsES6("false true", code); } @Test public void sealed() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var func = function (number) {" - + " return Object.isSealed(number) + ' ' + Object.isSealed(new Object(number));" - + "};" - + "try { " - + " func(-334918463);" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("true false", result); - - return null; - }); + final String code = + "var func = function (number) {" + + " return Object.isSealed(number) + ' ' + Object.isSealed(new Object(number));" + + "};" + + "try { " + + " func(-334918463);" + + "} catch (e) { e.message }"; + Utils.assertWithAllOptimizationLevelsES6("true false", code); } @Test public void sealedStrict() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - - Object result = - cx.evaluateString( - scope, - "var func = function (number) {" - + " 'use strict';" - + " return Object.isSealed(number) + ' ' + Object.isSealed(new Object(number));" - + "};" - + "try { " - + " func(-334918463);" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("true false", result); - - return null; - }); + final String code = + "var func = function (number) {" + + " 'use strict';" + + " return Object.isSealed(number) + ' ' + Object.isSealed(new Object(number));" + + "};" + + "try { " + + " func(-334918463);" + + "} catch (e) { e.message }"; + Utils.assertWithAllOptimizationLevelsES6("true false", code); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java index fe9d1c6e22..bbc5db0eed 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeObjectTest.java @@ -386,14 +386,7 @@ public void issue943Realm() { private static void evaluateAndAssert(final String script, final Object expected) { String[] prefixes = {"", "'use strict;'\n"}; for (final String prefix : prefixes) { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - Object result = cx.evaluateString(scope, prefix + script, "test", 1, null); - assertEquals(expected, result); - return null; - }); + Utils.assertWithAllOptimizationLevelsES6(expected, prefix + script); } } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java b/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java index b31a16bf7f..5dadc89945 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java @@ -7,11 +7,7 @@ */ package org.mozilla.javascript.tests.es6; -import static org.junit.Assert.assertEquals; - import org.junit.Test; -import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.tests.Utils; /** Test for handling const variables. */ @@ -32,49 +28,49 @@ public void getOwnPropertyDescriptorWithIndex() { + " res += desc.configurable;" + " res += ';';" + " res;"; - assertEvaluatesES6("true;h;false;true;false;", js); + Utils.assertWithAllOptimizationLevelsES6("true;h;false;true;false;", js); } @Test public void normalizeNoParam() { - assertEvaluates("123", "'123'.normalize()"); + Utils.assertWithAllOptimizationLevels("123", "'123'.normalize()"); } @Test public void normalizeNoUndefined() { - assertEvaluates("123", "'123'.normalize(undefined)"); + Utils.assertWithAllOptimizationLevels("123", "'123'.normalize(undefined)"); } @Test public void normalizeNoNull() { String js = "try { " + " '123'.normalize(null);" + "} catch (e) { e.message }"; - assertEvaluates( + Utils.assertWithAllOptimizationLevels( "The normalization form should be one of 'NFC', 'NFD', 'NFKC', 'NFKD'.", js); } @Test public void replaceReplacementAsString() { - assertEvaluates("1null3", "'123'.replace('2', /x/);"); - assertEvaluatesES6("1/x/3", "'123'.replace('2', /x/);"); + Utils.assertWithAllOptimizationLevels("1null3", "'123'.replace('2', /x/);"); + Utils.assertWithAllOptimizationLevelsES6("1/x/3", "'123'.replace('2', /x/);"); } @Test public void indexOfEmpty() { - assertEvaluates(0, "'1234'.indexOf('', 0);"); - assertEvaluates(1, "'1234'.indexOf('', 1);"); - assertEvaluates(4, "'1234'.indexOf('', 4);"); - assertEvaluates(4, "'1234'.indexOf('', 5);"); - assertEvaluates(4, "'1234'.indexOf('', 42);"); + Utils.assertWithAllOptimizationLevels(0, "'1234'.indexOf('', 0);"); + Utils.assertWithAllOptimizationLevels(1, "'1234'.indexOf('', 1);"); + Utils.assertWithAllOptimizationLevels(4, "'1234'.indexOf('', 4);"); + Utils.assertWithAllOptimizationLevels(4, "'1234'.indexOf('', 5);"); + Utils.assertWithAllOptimizationLevels(4, "'1234'.indexOf('', 42);"); } @Test public void includesEmpty() { - assertEvaluates(true, "'1234'.includes('');"); - assertEvaluates(true, "'1234'.includes('', 0);"); - assertEvaluates(true, "'1234'.includes('', 1);"); - assertEvaluates(true, "'1234'.includes('', 4);"); - assertEvaluates(true, "'1234'.includes('', 5);"); - assertEvaluates(true, "'1234'.includes('', 42);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.includes('');"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.includes('', 0);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.includes('', 1);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.includes('', 4);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.includes('', 5);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.includes('', 42);"); } @Test @@ -91,19 +87,19 @@ public void includesRegExpMatch() { + "res += ' # ' + '/./'.includes(regExp);\n" + "res;"; - assertEvaluatesES6( + Utils.assertWithAllOptimizationLevelsES6( "TypeError: First argument to String.prototype.includes must not be a regular expression # true", js); } @Test public void startsWithEmpty() { - assertEvaluates(true, "'1234'.startsWith('');"); - assertEvaluates(true, "'1234'.startsWith('', 0);"); - assertEvaluates(true, "'1234'.startsWith('', 1);"); - assertEvaluates(true, "'1234'.startsWith('', 4);"); - assertEvaluates(true, "'1234'.startsWith('', 5);"); - assertEvaluates(true, "'1234'.startsWith('', 42);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.startsWith('');"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.startsWith('', 0);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.startsWith('', 1);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.startsWith('', 4);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.startsWith('', 5);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.startsWith('', 42);"); } @Test @@ -120,19 +116,19 @@ public void startsWithRegExpMatch() { + "res += ' # ' + '/./'.includes(regExp);\n" + "res;"; - assertEvaluatesES6( + Utils.assertWithAllOptimizationLevelsES6( "TypeError: First argument to String.prototype.startsWith must not be a regular expression # true", js); } @Test public void endsWithEmpty() { - assertEvaluates(true, "'1234'.endsWith('');"); - assertEvaluates(true, "'1234'.endsWith('', 0);"); - assertEvaluates(true, "'1234'.endsWith('', 1);"); - assertEvaluates(true, "'1234'.endsWith('', 4);"); - assertEvaluates(true, "'1234'.endsWith('', 5);"); - assertEvaluates(true, "'1234'.endsWith('', 42);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.endsWith('');"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.endsWith('', 0);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.endsWith('', 1);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.endsWith('', 4);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.endsWith('', 5);"); + Utils.assertWithAllOptimizationLevels(true, "'1234'.endsWith('', 42);"); } @Test @@ -149,20 +145,24 @@ public void endsWithRegExpMatch() { + "res += ' # ' + '/./'.includes(regExp);\n" + "res;"; - assertEvaluatesES6( + Utils.assertWithAllOptimizationLevelsES6( "TypeError: First argument to String.prototype.startsWith must not be a regular expression # true", js); } @Test public void tagify() { - assertEvaluates("tester", "'tester'.big()"); - assertEvaluates("\"tester\"", "'\"tester\"'.big()"); - assertEvaluates("tester", "'tester'.fontsize()"); - assertEvaluates("tester", "'tester'.fontsize(null)"); - assertEvaluates("tester", "'tester'.fontsize(undefined)"); - assertEvaluates("tester", "'tester'.fontsize(123)"); - assertEvaluates( + Utils.assertWithAllOptimizationLevels("tester", "'tester'.big()"); + Utils.assertWithAllOptimizationLevels("\"tester\"", "'\"tester\"'.big()"); + Utils.assertWithAllOptimizationLevels( + "tester", "'tester'.fontsize()"); + Utils.assertWithAllOptimizationLevels( + "tester", "'tester'.fontsize(null)"); + Utils.assertWithAllOptimizationLevels( + "tester", "'tester'.fontsize(undefined)"); + Utils.assertWithAllOptimizationLevels( + "tester", "'tester'.fontsize(123)"); + Utils.assertWithAllOptimizationLevels( "tester", "'tester'.fontsize('\"123\"')"); } @@ -186,7 +186,7 @@ public void tagifyPrototypeNull() { String js = "try { String.prototype." + call + ".call(null);} catch (e) { e.message }"; String expected = "String.prototype." + call + " method called on null or undefined"; - assertEvaluatesES6(expected, js); + Utils.assertWithAllOptimizationLevelsES6(expected, js); } } @@ -211,90 +211,71 @@ public void tagifyPrototypeUndefined() { "try { String.prototype." + call + ".call(undefined);} catch (e) { e.message }"; String expected = "String.prototype." + call + " method called on null or undefined"; - assertEvaluatesES6(expected, js); + Utils.assertWithAllOptimizationLevelsES6(expected, js); } } @Test public void stringReplace() { - assertEvaluates("xyz", "''.replace('', 'xyz')"); - assertEvaluates("1", "'121'.replace('21', '')"); - assertEvaluates("xyz121", "'121'.replace('', 'xyz')"); - assertEvaluates("a$c21", "'121'.replace('1', 'a$c')"); - assertEvaluates("a121", "'121'.replace('1', 'a$&')"); - assertEvaluates("a$c21", "'121'.replace('1', 'a$$c')"); - assertEvaluates("abaabe", "'abcde'.replace('cd', 'a$`')"); - assertEvaluates("a21", "'121'.replace('1', 'a$`')"); - assertEvaluates("abaee", "'abcde'.replace('cd', \"a$'\")"); - assertEvaluates("aba", "'abcd'.replace('cd', \"a$'\")"); - assertEvaluates("aba$0", "'abcd'.replace('cd', 'a$0')"); - assertEvaluates("aba$1", "'abcd'.replace('cd', 'a$1')"); - assertEvaluates( + Utils.assertWithAllOptimizationLevels("xyz", "''.replace('', 'xyz')"); + Utils.assertWithAllOptimizationLevels("1", "'121'.replace('21', '')"); + Utils.assertWithAllOptimizationLevels("xyz121", "'121'.replace('', 'xyz')"); + Utils.assertWithAllOptimizationLevels("a$c21", "'121'.replace('1', 'a$c')"); + Utils.assertWithAllOptimizationLevels("a121", "'121'.replace('1', 'a$&')"); + Utils.assertWithAllOptimizationLevels("a$c21", "'121'.replace('1', 'a$$c')"); + Utils.assertWithAllOptimizationLevels("abaabe", "'abcde'.replace('cd', 'a$`')"); + Utils.assertWithAllOptimizationLevels("a21", "'121'.replace('1', 'a$`')"); + Utils.assertWithAllOptimizationLevels("abaee", "'abcde'.replace('cd', \"a$'\")"); + Utils.assertWithAllOptimizationLevels("aba", "'abcd'.replace('cd', \"a$'\")"); + Utils.assertWithAllOptimizationLevels("aba$0", "'abcd'.replace('cd', 'a$0')"); + Utils.assertWithAllOptimizationLevels("aba$1", "'abcd'.replace('cd', 'a$1')"); + Utils.assertWithAllOptimizationLevels( "abCD", "'abcd'.replace('cd', function (matched) { return matched.toUpperCase() })"); - assertEvaluates("", "'123456'.replace(/\\d+/, '')"); - assertEvaluates( + Utils.assertWithAllOptimizationLevels("", "'123456'.replace(/\\d+/, '')"); + Utils.assertWithAllOptimizationLevels( "123ABCD321abcd", "'123abcd321abcd'.replace(/[a-z]+/, function (matched) { return matched.toUpperCase() })"); } @Test public void stringReplaceAll() { - assertEvaluates("xyz", "''.replaceAll('', 'xyz')"); - assertEvaluates("1", "'12121'.replaceAll('21', '')"); - assertEvaluates("xyz1xyz2xyz1xyz", "'121'.replaceAll('', 'xyz')"); - assertEvaluates("a$c2a$c", "'121'.replaceAll('1', 'a$c')"); - assertEvaluates("a12a1", "'121'.replaceAll('1', 'a$&')"); - assertEvaluates("a$c2a$c", "'121'.replaceAll('1', 'a$$c')"); - assertEvaluates("aaadaaabcda", "'abcdabc'.replaceAll('bc', 'a$`')"); - assertEvaluates("a2a12", "'121'.replaceAll('1', 'a$`')"); - assertEvaluates("aadabcdaa", "'abcdabc'.replaceAll('bc', \"a$'\")"); - assertEvaluates("aadabcdaa", "'abcdabc'.replaceAll('bc', \"a$'\")"); - assertEvaluates("aa$0daa$0", "'abcdabc'.replaceAll('bc', 'a$0')"); - assertEvaluates("aa$1daa$1", "'abcdabc'.replaceAll('bc', 'a$1')"); - assertEvaluates("", "'123456'.replaceAll(/\\d+/g, '')"); - assertEvaluates("123456", "'123456'.replaceAll(undefined, '')"); - assertEvaluates("afoobarb", "'afoob'.replaceAll(/(foo)/g, '$1bar')"); - assertEvaluates("foobarb", "'foob'.replaceAll(/(foo)/gy, '$1bar')"); - assertEvaluates("hllo", "'hello'.replaceAll(/(h)e/gy, '$1')"); - assertEvaluates("$1llo", "'hello'.replaceAll(/he/g, '$1')"); - assertEvaluates( + Utils.assertWithAllOptimizationLevels("xyz", "''.replaceAll('', 'xyz')"); + Utils.assertWithAllOptimizationLevels("1", "'12121'.replaceAll('21', '')"); + Utils.assertWithAllOptimizationLevels("xyz1xyz2xyz1xyz", "'121'.replaceAll('', 'xyz')"); + Utils.assertWithAllOptimizationLevels("a$c2a$c", "'121'.replaceAll('1', 'a$c')"); + Utils.assertWithAllOptimizationLevels("a12a1", "'121'.replaceAll('1', 'a$&')"); + Utils.assertWithAllOptimizationLevels("a$c2a$c", "'121'.replaceAll('1', 'a$$c')"); + Utils.assertWithAllOptimizationLevels("aaadaaabcda", "'abcdabc'.replaceAll('bc', 'a$`')"); + Utils.assertWithAllOptimizationLevels("a2a12", "'121'.replaceAll('1', 'a$`')"); + Utils.assertWithAllOptimizationLevels("aadabcdaa", "'abcdabc'.replaceAll('bc', \"a$'\")"); + Utils.assertWithAllOptimizationLevels("aadabcdaa", "'abcdabc'.replaceAll('bc', \"a$'\")"); + Utils.assertWithAllOptimizationLevels("aa$0daa$0", "'abcdabc'.replaceAll('bc', 'a$0')"); + Utils.assertWithAllOptimizationLevels("aa$1daa$1", "'abcdabc'.replaceAll('bc', 'a$1')"); + Utils.assertWithAllOptimizationLevels("", "'123456'.replaceAll(/\\d+/g, '')"); + Utils.assertWithAllOptimizationLevels("123456", "'123456'.replaceAll(undefined, '')"); + Utils.assertWithAllOptimizationLevels("afoobarb", "'afoob'.replaceAll(/(foo)/g, '$1bar')"); + Utils.assertWithAllOptimizationLevels("foobarb", "'foob'.replaceAll(/(foo)/gy, '$1bar')"); + Utils.assertWithAllOptimizationLevels("hllo", "'hello'.replaceAll(/(h)e/gy, '$1')"); + Utils.assertWithAllOptimizationLevels("$1llo", "'hello'.replaceAll(/he/g, '$1')"); + Utils.assertWithAllOptimizationLevels( "I$want$these$periods$to$be$$s", "'I.want.these.periods.to.be.$s'.replaceAll(/\\./g, '$')"); - assertEvaluates("food bar", "'foo bar'.replaceAll(/foo/g, '$&d')"); - assertEvaluates("foo foo ", "'foo bar'.replaceAll(/bar/g, '$`')"); - assertEvaluates(" bar bar", "'foo bar'.replaceAll(/foo/g, '$\\'')"); - assertEvaluates("$' bar", "'foo bar'.replaceAll(/foo/g, '$$\\'')"); - assertEvaluates("ad$0db", "'afoob'.replaceAll(/(foo)/g, 'd$0d')"); - assertEvaluates("ad$0db", "'afkxxxkob'.replace(/(f)k(.*)k(o)/g, 'd$0d')"); - assertEvaluates("ad$0dbd$0dc", "'afoobfuoc'.replaceAll(/(f.o)/g, 'd$0d')"); - assertEvaluates( + Utils.assertWithAllOptimizationLevels("food bar", "'foo bar'.replaceAll(/foo/g, '$&d')"); + Utils.assertWithAllOptimizationLevels("foo foo ", "'foo bar'.replaceAll(/bar/g, '$`')"); + Utils.assertWithAllOptimizationLevels(" bar bar", "'foo bar'.replaceAll(/foo/g, '$\\'')"); + Utils.assertWithAllOptimizationLevels("$' bar", "'foo bar'.replaceAll(/foo/g, '$$\\'')"); + Utils.assertWithAllOptimizationLevels("ad$0db", "'afoob'.replaceAll(/(foo)/g, 'd$0d')"); + Utils.assertWithAllOptimizationLevels( + "ad$0db", "'afkxxxkob'.replace(/(f)k(.*)k(o)/g, 'd$0d')"); + Utils.assertWithAllOptimizationLevels( + "ad$0dbd$0dc", "'afoobfuoc'.replaceAll(/(f.o)/g, 'd$0d')"); + Utils.assertWithAllOptimizationLevels( "123FOOBAR321BARFOO123", "'123foobar321barfoo123'.replace(/[a-z]+/g, function (matched) { return matched.toUpperCase() })"); - assertEvaluates( + Utils.assertWithAllOptimizationLevels( "TypeError: replaceAll must be called with a global RegExp", "try { 'hello'.replaceAll(/he/i, 'x'); } catch (e) { '' + e }"); } - - private static void assertEvaluates(final Object expected, final String source) { - Utils.runWithAllOptimizationLevels( - cx -> { - final Scriptable scope = cx.initStandardObjects(); - final Object rep = cx.evaluateString(scope, source, "test.js", 0, null); - assertEquals(expected, rep); - return null; - }); - } - - private static void assertEvaluatesES6(final Object expected, final String source) { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - final Object rep = cx.evaluateString(scope, source, "test.js", 0, null); - assertEquals(expected, rep); - return null; - }); - } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java b/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java index bb7a449c0b..a4b1c4c89a 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java @@ -18,16 +18,7 @@ public class NumericSeparatorTest { /** Special Tokenizer test for numeric constant at end. */ @Test public void numericAtEndOneDigit() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - - Object result = cx.evaluateString(scope, "1", "test", 1, null); - assertEquals(1.0, result); - - return null; - }); + Utils.assertWithAllOptimizationLevelsES6(1.0, "1"); } /** Special Tokenizer test for numeric constant at end. */ diff --git a/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java b/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java index 32a1ede83c..b81c9538ba 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java +++ b/testsrc/org/mozilla/javascript/tests/es6/Symbol3Test.java @@ -17,39 +17,19 @@ public class Symbol3Test { @Test public void scriptRuntimeTypeofSymbolKey() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - - String code = - "function foo() {" - + " var sym = Object.getOwnPropertySymbols(arguments);" - + " return '' + sym.length + ' ' + typeof sym[0];" - + "}" - + "foo()"; - String result = (String) cx.evaluateString(scope, code, "test", 1, null); - assertEquals("1 symbol", result); - - return null; - }); + final String code = + "function foo() {" + + " var sym = Object.getOwnPropertySymbols(arguments);" + + " return '' + sym.length + ' ' + typeof sym[0];" + + "}" + + "foo()"; + Utils.assertWithAllOptimizationLevelsES6("1 symbol", code); } @Test public void scriptRuntimeTypeofSymbol() { - Utils.runWithAllOptimizationLevels( - cx -> { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - - String result = - (String) - cx.evaluateString( - scope, "typeof Symbol.toStringTag", "test", 1, null); - assertEquals("symbol", result); - - return null; - }); + final String code = "typeof Symbol.toStringTag"; + Utils.assertWithAllOptimizationLevelsES6("symbol", code); } @Test