From 6387bac4281ba1c4a8050cd055071615593d214c Mon Sep 17 00:00:00 2001 From: RBRi Date: Tue, 25 Apr 2023 23:51:14 +0200 Subject: [PATCH] More consistent test optimization levels (#1317) Ensure that more tests check multiple optimization levels and that they do it in a consistent way. * use Utils.runWithAllOptimizationLevels for the ES6 tests * use Utils.runWithAllOptimizationLevels for the ES2022 tests * support the system property TEST_OPTLEVEL for more tests use Utils.runWithAllOptimizationLevels at more places * spotless --- .../javascript/tests/Bug482203Test.java | 95 ++-- .../org/mozilla/javascript/tests/Utils.java | 28 +- .../tests/es2022/NativeObjectTest.java | 248 +++++---- .../es5/FunctionApplyArrayLikeArguments.java | 102 ++-- .../tests/es6/BoundFunctionTest.java | 41 +- .../javascript/tests/es6/ES6IteratorTest.java | 55 +- .../tests/es6/FunctionNullSetTest.java | 2 +- .../tests/es6/NativeFunctionTest.java | 181 ++++--- .../tests/es6/NativeNumberPropertyTest.java | 234 +++++---- .../tests/es6/NativeRegExpTest.java | 236 +++++---- .../tests/es6/NativeString2Test.java | 492 ++++++++++++------ .../tests/es6/NumericSeparatorTest.java | 85 ++- .../tests/es6/ObjectSealFreezeTest.java | 428 ++++++++------- .../javascript/tests/es6/PromiseTest.java | 88 ++-- .../javascript/tests/es6/PropertyTest.java | 179 ++++--- .../tests/es6/TaggedTemplateLiteralTest.java | 12 +- .../tests/es6/TypedArrayJavaTest.java | 23 +- 17 files changed, 1457 insertions(+), 1072 deletions(-) diff --git a/testsrc/org/mozilla/javascript/tests/Bug482203Test.java b/testsrc/org/mozilla/javascript/tests/Bug482203Test.java index c7a43c65f4..eb7fa37675 100644 --- a/testsrc/org/mozilla/javascript/tests/Bug482203Test.java +++ b/testsrc/org/mozilla/javascript/tests/Bug482203Test.java @@ -4,6 +4,7 @@ package org.mozilla.javascript.tests; +import java.io.IOException; import java.io.InputStreamReader; import junit.framework.TestCase; import org.mozilla.javascript.Callable; @@ -15,46 +16,70 @@ public class Bug482203Test extends TestCase { public void testJsApi() throws Exception { - try (Context cx = Context.enter()) { - cx.setOptimizationLevel(-1); - InputStreamReader in = - new InputStreamReader(Bug482203Test.class.getResourceAsStream("Bug482203.js")); - Script script = cx.compileReader(in, "", 1, null); - Scriptable scope = cx.initStandardObjects(); - script.exec(cx, scope); - int counter = 0; - for (; ; ) { - Object cont = ScriptableObject.getProperty(scope, "c"); - if (cont == null) { - break; - } - counter++; - ((Callable) cont).call(cx, scope, scope, new Object[] {null}); - } - assertEquals(counter, 5); - assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); - } + Utils.runWithAllOptimizationLevels( + cx -> { + Scriptable scope = cx.initStandardObjects(); + + try { + InputStreamReader in = + new InputStreamReader( + Bug482203Test.class.getResourceAsStream("Bug482203.js")); + + Script script = cx.compileReader(in, "", 1, null); + script.exec(cx, scope); + int counter = 0; + for (; ; ) { + Object cont = ScriptableObject.getProperty(scope, "c"); + if (cont == null) { + break; + } + counter++; + ((Callable) cont).call(cx, scope, scope, new Object[] {null}); + } + assertEquals(counter, 5); + assertEquals( + Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); + } catch (IOException e) { + fail(e.getMessage()); + } + + return null; + }); } public void testJavaApi() throws Exception { + Utils.runWithAllOptimizationLevels( + cx -> { + Scriptable scope = cx.initStandardObjects(); + + try { + InputStreamReader in = + new InputStreamReader( + Bug482203Test.class.getResourceAsStream("Bug482203.js")); + + Script script = cx.compileReader(in, "", 1, null); + cx.executeScriptWithContinuations(script, scope); + int counter = 0; + for (; ; ) { + Object cont = ScriptableObject.getProperty(scope, "c"); + if (cont == null) { + break; + } + counter++; + cx.resumeContinuation(cont, scope, null); + } + assertEquals(counter, 5); + assertEquals( + Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); + } catch (IOException e) { + fail(e.getMessage()); + } + + return null; + }); + try (Context cx = Context.enter()) { cx.setOptimizationLevel(-1); - InputStreamReader in = - new InputStreamReader(Bug482203Test.class.getResourceAsStream("Bug482203.js")); - Script script = cx.compileReader(in, "", 1, null); - Scriptable scope = cx.initStandardObjects(); - cx.executeScriptWithContinuations(script, scope); - int counter = 0; - for (; ; ) { - Object cont = ScriptableObject.getProperty(scope, "c"); - if (cont == null) { - break; - } - counter++; - cx.resumeContinuation(cont, scope, null); - } - assertEquals(counter, 5); - assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result")); } } } diff --git a/testsrc/org/mozilla/javascript/tests/Utils.java b/testsrc/org/mozilla/javascript/tests/Utils.java index 7201b2c394..21911a5b07 100644 --- a/testsrc/org/mozilla/javascript/tests/Utils.java +++ b/testsrc/org/mozilla/javascript/tests/Utils.java @@ -16,40 +16,38 @@ */ public class Utils { /** The default set of levels to run tests at. */ - public static final int[] DEFAULT_OPT_LEVELS = new int[] {-1, 0, 9}; + public static final int[] DEFAULT_OPT_LEVELS = new int[] {-1}; /** Runs the action successively with all available optimization levels */ - public static void runWithAllOptimizationLevels(final ContextAction action) { - runWithOptimizationLevel(action, -1); - runWithOptimizationLevel(action, 0); - runWithOptimizationLevel(action, 1); + public static void runWithAllOptimizationLevels(final ContextAction action) { + for (int level : getTestOptLevels()) { + runWithOptimizationLevel(action, level); + } } /** Runs the action successively with all available optimization levels */ public static void runWithAllOptimizationLevels( - final ContextFactory contextFactory, final ContextAction action) { - runWithOptimizationLevel(contextFactory, action, -1); - runWithOptimizationLevel(contextFactory, action, 0); - runWithOptimizationLevel(contextFactory, action, 1); + final ContextFactory contextFactory, final ContextAction action) { + for (int level : getTestOptLevels()) { + runWithOptimizationLevel(contextFactory, action, level); + } } /** Runs the provided action at the given optimization level */ public static void runWithOptimizationLevel( - final ContextAction action, final int optimizationLevel) { + final ContextAction action, final int optimizationLevel) { runWithOptimizationLevel(new ContextFactory(), action, optimizationLevel); } /** Runs the provided action at the given optimization level */ public static void runWithOptimizationLevel( final ContextFactory contextFactory, - final ContextAction action, + final ContextAction action, final int optimizationLevel) { - final Context cx = contextFactory.enterContext(); - try { + + try (final Context cx = contextFactory.enterContext()) { cx.setOptimizationLevel(optimizationLevel); action.run(cx); - } finally { - Context.exit(); } } diff --git a/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java b/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java index 4ee42f8e24..e577560f95 100644 --- a/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java +++ b/testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java @@ -7,144 +7,184 @@ 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; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.tests.Utils; public class NativeObjectTest { - 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 testHasStringOwn() { - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ test: '123' }, 'test');\n" - + "'result = ' + result", - "test", - 1, - null); - - assertEquals("result = true", result); + 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; + }); } @Test public void testHasUndefinedOwn() { - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ test: undefined }, 'test');\n" - + "'result = ' + result;", - "test", - 1, - null); - - assertEquals("result = true", result); + 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; + }); } @Test public void testHasNullOwn() { - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ test: null }, 'test');\n" - + "'result = ' + result;", - "test", - 1, - null); - - assertEquals("result = true", result); + 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; + }); } @Test public void testHasArrayPropertyOwn() { - 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); + 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; + }); } @Test public void testHasNoOwn() { - Object result = - cx.evaluateString( - scope, - "let result = Object.hasOwn({ cake: 123 }, 'test');\n" - + "'result = ' + result", - "test", - 1, - null); - - assertEquals("result = false", result); + 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; + }); } @Test public void testCreateHasOwn() { - 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); + 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; + }); } @Test public void testCreateNoHasOwn() { - Object result = - cx.evaluateString( - scope, - "var result = Object.hasOwn(Object.create({ q: 321 }), 'q');\n" - + "'result = ' + result; ", - "test", - 1, - null); - - assertEquals("result = false", result); + 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; + }); } @Test public void testCalledTest() { - 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); + 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; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es5/FunctionApplyArrayLikeArguments.java b/testsrc/org/mozilla/javascript/tests/es5/FunctionApplyArrayLikeArguments.java index 791d1c1847..6f2e11f5c8 100644 --- a/testsrc/org/mozilla/javascript/tests/es5/FunctionApplyArrayLikeArguments.java +++ b/testsrc/org/mozilla/javascript/tests/es5/FunctionApplyArrayLikeArguments.java @@ -9,76 +9,56 @@ 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.Scriptable; import org.mozilla.javascript.Undefined; +import org.mozilla.javascript.tests.Utils; public class FunctionApplyArrayLikeArguments { - private static final int[] OPTIMIZATIONS = {-1, 0, 1}; - - private Scriptable m_scope; - - @Before - public void init() { - Context cx = Context.enter(); - cx.setOptimizationLevel(-1); - m_scope = cx.initStandardObjects(); - } - - @After - public void cleanup() { - Context.exit(); - } - - private void test(String testCode, Object expected) { - Object result = null; - try { - result = eval(testCode); - } catch (Exception e) { - result = "EXCEPTIONCAUGHT"; - } - assertEquals(expected, result); - } - - private Object eval(String source) { - Context cx = Context.getCurrentContext(); - return cx.evaluateString(m_scope, source, "source", 1, null); + private static void test(String testCode, Object expected) { + Utils.runWithAllOptimizationLevels( + cx -> { + final Scriptable scope = cx.initStandardObjects(); + + Object result = null; + try { + result = cx.evaluateString(scope, testCode, "test", 1, null); + } catch (Exception e) { + result = "EXCEPTIONCAUGHT"; + } + assertEquals(expected, result); + + return null; + }); } @Test public void testArrayLikeArgumentsOfFunctionApply() { - for (int optIdx = 0; optIdx < OPTIMIZATIONS.length; optIdx++) { - test( - "function test() { return arguments[0]; }" + "test.apply(this, {});", - Undefined.instance); - - test( - "function test() { return arguments[0]; }" - + "test.apply(this, {'length':1, '0':'banana'});", - "banana"); - - test( - "function test() { return arguments[0]; }" - + "test.apply(this, {'length':'1', '0':'lala'});", - "lala"); - - test( - "function test() { return arguments[0]; }" + "test.apply(2,2);", - "EXCEPTIONCAUGHT"); - - test( - "function test() { return arguments[0]; }" - + "test.apply(this,{'length':'abc', '0':'banana'});", - Undefined.instance); - - test( - "function test() { return arguments[0]; }" - + "test.apply(this,{'length':function(){return 1;}, '0':'banana'});", - Undefined.instance); - } + test( + "function test() { return arguments[0]; }" + "test.apply(this, {});", + Undefined.instance); + + test( + "function test() { return arguments[0]; }" + + "test.apply(this, {'length':1, '0':'banana'});", + "banana"); + + test( + "function test() { return arguments[0]; }" + + "test.apply(this, {'length':'1', '0':'lala'});", + "lala"); + + test("function test() { return arguments[0]; }" + "test.apply(2,2);", "EXCEPTIONCAUGHT"); + + test( + "function test() { return arguments[0]; }" + + "test.apply(this,{'length':'abc', '0':'banana'});", + Undefined.instance); + + test( + "function test() { return arguments[0]; }" + + "test.apply(this,{'length':function(){return 1;}, '0':'banana'});", + Undefined.instance); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java b/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java index 8a88227320..1424487008 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java @@ -9,35 +9,30 @@ 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; -import org.mozilla.javascript.TopLevel; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.tests.Utils; public class BoundFunctionTest { - private Context cx; - private ScriptableObject scope; - - @Before - public void setUp() { - cx = Context.enter(); - cx.setLanguageVersion(Context.VERSION_ES6); - scope = cx.initSafeStandardObjects(new TopLevel(), false); - } - - @After - public void tearDown() { - Context.exit(); - } - @Test public void ctorCallableThis() { - Object result = - cx.evaluateString( - scope, " function foo() {};\n" + " foo.bind({}).name;", "test", 1, null); - assertEquals("bound foo", result); + 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); + + return null; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java b/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java index 9ef01d8273..0478873f4f 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/ES6IteratorTest.java @@ -6,43 +6,36 @@ 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; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.tests.Utils; public class ES6IteratorTest { - 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 valueDone() { - 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); + 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); + + return null; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/FunctionNullSetTest.java b/testsrc/org/mozilla/javascript/tests/es6/FunctionNullSetTest.java index 68661e129f..6fd3eef304 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/FunctionNullSetTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/FunctionNullSetTest.java @@ -62,7 +62,7 @@ public Object run(final Context cx) { } }; - Utils.runWithOptimizationLevel(action, -1); + Utils.runWithAllOptimizationLevels(action); } public static class MyHostObject extends ScriptableObject { diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java index 1cf71a4eb8..5a07a010ff 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeFunctionTest.java @@ -2,107 +2,136 @@ 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.Scriptable; import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.annotations.JSConstructor; import org.mozilla.javascript.annotations.JSFunction; +import org.mozilla.javascript.tests.Utils; public class NativeFunctionTest { - private Context cx; - private ScriptableObject scope; - - @Before - public void setUp() throws Exception { - cx = Context.enter(); - cx.setLanguageVersion(Context.VERSION_ES6); - scope = cx.initStandardObjects(); - ScriptableObject.defineClass(scope, HelperObject.class); - } - - @After - public void tearDown() { - Context.exit(); - } @Test public void testFunctionPrototypeLength() { - 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); + 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; + }); } @Test public void testFunctionPrototypeName() { - 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); + 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; + }); } @Test public void testFunctionLength() { - 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); + 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; + }); } @Test public void testFunctionName() { - 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); + 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; + }); } @Test - public void testFunctionNameJavaObject() { - Object result = - cx.evaluateString( - scope, - "var f=new HelperObject().foo;\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); + public void testFunctionNameJavaObject() throws Exception { + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + final Scriptable scope = cx.initStandardObjects(); + try { + ScriptableObject.defineClass(scope, HelperObject.class); + } catch (Exception e) { + } + + Object result = + cx.evaluateString( + scope, + "var f=new HelperObject().foo;\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; + }); } public static class HelperObject extends ScriptableObject { diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java index 5766ad6fb3..b78571ce94 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeNumberPropertyTest.java @@ -2,132 +2,166 @@ 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; +import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.tests.Utils; public class NativeNumberPropertyTest { - 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 testDefiningAProperty() { - 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); + 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; + }); } @Test public void testDefiningAPropertyStrict() { - 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); + 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; + }); } @Test public void testExtensible() { - 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); + 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; + }); } @Test public void testExtensibleStrict() { - 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); + 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; + }); } @Test public void testSealed() { - 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); + 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; + }); } @Test public void testSealedStrict() { - 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); + 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; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeRegExpTest.java b/testsrc/org/mozilla/javascript/tests/es6/NativeRegExpTest.java index 7075c75aef..cc8adde16e 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeRegExpTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeRegExpTest.java @@ -20,110 +20,124 @@ public class NativeRegExpTest { @Test public void regExIsCallableForBackwardCompatibility() { - try (Context cx = Context.enter()) { - cx.setLanguageVersion(Context.VERSION_1_8); - ScriptableObject scope = cx.initStandardObjects(); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_1_8); + ScriptableObject scope = cx.initStandardObjects(); + + String source = "var a = new RegExp('1'); a(1).toString();"; + assertEquals("1", cx.evaluateString(scope, source, "test", 0, null)); - String source = "var a = new RegExp('1'); a(1).toString();"; - assertEquals("1", cx.evaluateString(scope, source, "test", 0, null)); + source = "/^\\{(.*)\\}$/('{1234}').toString();"; + assertEquals("{1234},1234", cx.evaluateString(scope, source, "test", 0, null)); - source = "/^\\{(.*)\\}$/('{1234}').toString();"; - assertEquals("{1234},1234", cx.evaluateString(scope, source, "test", 0, null)); + source = "RegExp('a|b','g')()"; + assertNull(cx.evaluateString(scope, source, "test", 0, null)); - source = "RegExp('a|b','g')()"; - assertNull(cx.evaluateString(scope, source, "test", 0, null)); + source = "new /z/();"; + assertNull(cx.evaluateString(scope, source, "test", 0, null)); - source = "new /z/();"; - assertNull(cx.evaluateString(scope, source, "test", 0, null)); + source = "(new new RegExp).toString()"; + assertEquals("", cx.evaluateString(scope, source, "test", 0, null)); - source = "(new new RegExp).toString()"; - assertEquals("", cx.evaluateString(scope, source, "test", 0, null)); - } + return null; + }); } @Test public void regExMinusInRangeBorderCases() { - try (Context cx = Context.enter()) { - cx.setLanguageVersion(Context.VERSION_1_8); - ScriptableObject scope = cx.initStandardObjects(); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_1_8); + ScriptableObject scope = cx.initStandardObjects(); - String source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-]+/g, 'x');"; - assertEquals("axbxc d efg 1 23", cx.evaluateString(scope, source, "test", 0, null)); + String source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-]+/g, 'x');"; + assertEquals( + "axbxc d efg 1 23", cx.evaluateString(scope, source, "test", 0, null)); - source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\s]+/g, 'x');"; - assertEquals("axbxcxdxefgx1x23", cx.evaluateString(scope, source, "test", 0, null)); + source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\s]+/g, 'x');"; + assertEquals( + "axbxcxdxefgx1x23", cx.evaluateString(scope, source, "test", 0, null)); - source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\S]+/g, 'x');"; - assertEquals("x x x x x", cx.evaluateString(scope, source, "test", 0, null)); + source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\S]+/g, 'x');"; + assertEquals("x x x x x", cx.evaluateString(scope, source, "test", 0, null)); - source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\w]+/g, 'x');"; - assertEquals("x x x x x", cx.evaluateString(scope, source, "test", 0, null)); + source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\w]+/g, 'x');"; + assertEquals("x x x x x", cx.evaluateString(scope, source, "test", 0, null)); - source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\W]+/g, 'x');"; - assertEquals("axbxcxdxefgx1x23", cx.evaluateString(scope, source, "test", 0, null)); + source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\W]+/g, 'x');"; + assertEquals( + "axbxcxdxefgx1x23", cx.evaluateString(scope, source, "test", 0, null)); + + source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\d]+/g, 'x');"; + assertEquals( + "axbxc d efg x x", cx.evaluateString(scope, source, "test", 0, null)); - source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\d]+/g, 'x');"; - assertEquals("axbxc d efg x x", cx.evaluateString(scope, source, "test", 0, null)); + source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\D]+/g, 'x');"; + assertEquals("x1x23", cx.evaluateString(scope, source, "test", 0, null)); - source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\D]+/g, 'x');"; - assertEquals("x1x23", cx.evaluateString(scope, source, "test", 0, null)); + source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\a]+/g, 'x');"; + assertEquals( + "x-bxc d efg 1 23", cx.evaluateString(scope, source, "test", 0, null)); - source = "var r = 'a-b_c d efg 1 23';\n" + "r.replace(/[_-\\a]+/g, 'x');"; - assertEquals("x-bxc d efg 1 23", cx.evaluateString(scope, source, "test", 0, null)); - } + return null; + }); } @Test public void regExIsNotCallable() { - try (Context cx = Context.enter()) { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); - - String source = "var a = new RegExp('1'); a(1);"; - try { - cx.evaluateString(scope, source, "test", 0, null); - fail(); - } catch (EcmaError e) { - // expected - assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); - } - - source = "/^\\{(.*)\\}$/('{1234}');"; - try { - cx.evaluateString(scope, source, "test", 0, null); - fail(); - } catch (EcmaError e) { - // expected - assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); - } - - source = "RegExp('a|b','g')();"; - try { - cx.evaluateString(scope, source, "test", 0, null); - fail(); - } catch (EcmaError e) { - // expected - assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); - } - - source = "new /z/();"; - try { - cx.evaluateString(scope, source, "test", 0, null); - fail(); - } catch (EcmaError e) { - // expected - assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); - } - - source = "new new RegExp"; - try { - cx.evaluateString(scope, source, "test", 0, null); - fail(); - } catch (EcmaError e) { - // expected - assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); - } - } + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + String source = "var a = new RegExp('1'); a(1);"; + try { + cx.evaluateString(scope, source, "test", 0, null); + fail(); + } catch (EcmaError e) { + // expected + assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); + } + + source = "/^\\{(.*)\\}$/('{1234}');"; + try { + cx.evaluateString(scope, source, "test", 0, null); + fail(); + } catch (EcmaError e) { + // expected + assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); + } + + source = "RegExp('a|b','g')();"; + try { + cx.evaluateString(scope, source, "test", 0, null); + fail(); + } catch (EcmaError e) { + // expected + assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); + } + + source = "new /z/();"; + try { + cx.evaluateString(scope, source, "test", 0, null); + fail(); + } catch (EcmaError e) { + // expected + assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); + } + + source = "new new RegExp"; + try { + cx.evaluateString(scope, source, "test", 0, null); + fail(); + } catch (EcmaError e) { + // expected + assertTrue(e.getMessage(), e.getMessage().startsWith("TypeError: ")); + } + + return null; + }); } @Test @@ -147,38 +161,44 @@ public void lastIndexReadonly() { @Test public void search() { - try (Context cx = Context.enter()) { - cx.setLanguageVersion(Context.VERSION_ES6); - ScriptableObject scope = cx.initStandardObjects(); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + String source = "'abc'.search(/b/);"; + assertEquals(1, cx.evaluateString(scope, source, "test", 0, null)); - String source = "'abc'.search(/b/);"; - assertEquals(1, cx.evaluateString(scope, source, "test", 0, null)); + source = "/b/[Symbol.search]('abc');"; + assertEquals(1, cx.evaluateString(scope, source, "test", 0, null)); - source = "/b/[Symbol.search]('abc');"; - assertEquals(1, cx.evaluateString(scope, source, "test", 0, null)); + source = "'abc'.search(/d/);"; + assertEquals(-1, cx.evaluateString(scope, source, "test", 0, null)); - source = "'abc'.search(/d/);"; - assertEquals(-1, cx.evaluateString(scope, source, "test", 0, null)); + source = "/d/[Symbol.search]('abc');"; + assertEquals(-1, cx.evaluateString(scope, source, "test", 0, null)); - source = "/d/[Symbol.search]('abc');"; - assertEquals(-1, cx.evaluateString(scope, source, "test", 0, null)); - } + return null; + }); } @Test public void regExWrongQuantifier() { - try (Context cx = Context.enter()) { - ScriptableObject scope = cx.initStandardObjects(); - - String source = "'abc'.search(/b{2,1}/);"; - try { - cx.evaluateString(scope, source, "test", 0, null); - fail("Shoud throw"); - } catch (Exception e) { - assertEquals( - "SyntaxError: Invalid regular expression: The quantifier maximum '1' is less than the minimum '2'.", - e.getMessage()); - } - } + Utils.runWithAllOptimizationLevels( + cx -> { + ScriptableObject scope = cx.initStandardObjects(); + + String source = "'abc'.search(/b{2,1}/);"; + try { + cx.evaluateString(scope, source, "test", 0, null); + fail("Shoud throw"); + } catch (Exception e) { + assertEquals( + "SyntaxError: Invalid regular expression: The quantifier maximum '1' is less than the minimum '2'.", + e.getMessage()); + } + + return null; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java b/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java index 8c447b58fb..42b18e674e 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NativeString2Test.java @@ -10,243 +10,389 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.mozilla.javascript.Context; import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.tests.Utils; /** Test for handling const variables. */ public class NativeString2Test { - 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 testGetOwnPropertyDescriptorWithIndex() { - Object result = - cx.evaluateString( - scope, - " var res = 'hello'.hasOwnProperty('0');" - + " res += ';';" - + " desc = Object.getOwnPropertyDescriptor('hello', '0');" - + " res += desc.value;" - + " res += ';';" - + " res += desc.writable;" - + " res += ';';" - + " res += desc.enumerable;" - + " res += ';';" - + " res += desc.configurable;" - + " res += ';';" - + " res;", - "test", - 1, - null); - assertEquals("true;h;false;true;false;", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + " var res = 'hello'.hasOwnProperty('0');" + + " res += ';';" + + " desc = Object.getOwnPropertyDescriptor('hello', '0');" + + " res += desc.value;" + + " res += ';';" + + " res += desc.writable;" + + " res += ';';" + + " res += desc.enumerable;" + + " res += ';';" + + " res += desc.configurable;" + + " res += ';';" + + " res;", + "test", + 1, + null); + assertEquals("true;h;false;true;false;", result); + + return null; + }); } @Test public void testNormalizeNoParam() { - Object result = cx.evaluateString(scope, "'123'.normalize()", "test", 1, null); - assertEquals("123", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = cx.evaluateString(scope, "'123'.normalize()", "test", 1, null); + assertEquals("123", result); + + return null; + }); } @Test public void testNormalizeNoUndefined() { - Object result = cx.evaluateString(scope, "'123'.normalize(undefined)", "test", 1, null); - assertEquals("123", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString(scope, "'123'.normalize(undefined)", "test", 1, null); + assertEquals("123", result); + + return null; + }); } @Test public void testNormalizeNoNull() { - Object result = - cx.evaluateString( - scope, - "try { " + " '123'.normalize(null);" + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals( - "The normalization form should be one of 'NFC', 'NFD', 'NFKC', 'NFKD'.", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "try { " + + " '123'.normalize(null);" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals( + "The normalization form should be one of 'NFC', 'NFD', 'NFKC', 'NFKD'.", + result); + + return null; + }); } @Test public void testReplaceReplacementAsString() { - Object result = cx.evaluateString(scope, "'123'.replace('2', /x/);", "test", 1, null); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString(scope, "'123'.replace('2', /x/);", "test", 1, null); + assertEquals("1/x/3", result); - assertEquals("1/x/3", result); + return null; + }); } @Test public void testIndexOfEmpty() { - Object result = cx.evaluateString(scope, "'1234'.indexOf('', 0);", "test", 1, null); - assertEquals(0, result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString(scope, "'1234'.indexOf('', 0);", "test", 1, null); + assertEquals(0, result); + + result = cx.evaluateString(scope, "'1234'.indexOf('', 1);", "test", 1, null); + assertEquals(1, result); - result = cx.evaluateString(scope, "'1234'.indexOf('', 1);", "test", 1, null); - assertEquals(1, result); + result = cx.evaluateString(scope, "'1234'.indexOf('', 4);", "test", 1, null); + assertEquals(4, result); - result = cx.evaluateString(scope, "'1234'.indexOf('', 4);", "test", 1, null); - assertEquals(4, result); + result = cx.evaluateString(scope, "'1234'.indexOf('', 5);", "test", 1, null); + assertEquals(4, result); - result = cx.evaluateString(scope, "'1234'.indexOf('', 5);", "test", 1, null); - assertEquals(4, result); + result = cx.evaluateString(scope, "'1234'.indexOf('', 42);", "test", 1, null); + assertEquals(4, result); - result = cx.evaluateString(scope, "'1234'.indexOf('', 42);", "test", 1, null); - assertEquals(4, result); + return null; + }); } @Test public void testIncludesEmpty() { - Boolean result = - (Boolean) cx.evaluateString(scope, "'1234'.includes('');", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.includes('', 0);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.includes('', 1);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.includes('', 4);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.includes('', 5);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.includes('', 42);", "test", 1, null); - assertTrue(result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Boolean result = + (Boolean) + cx.evaluateString( + scope, "'1234'.includes('');", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.includes('', 0);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.includes('', 1);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.includes('', 4);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.includes('', 5);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.includes('', 42);", "test", 1, null); + assertTrue(result); + + return null; + }); } @Test public void testStartsWithEmpty() { - Boolean result = - (Boolean) cx.evaluateString(scope, "'1234'.startsWith('');", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.startsWith('', 0);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.startsWith('', 1);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.startsWith('', 4);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.startsWith('', 5);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.startsWith('', 42);", "test", 1, null); - assertTrue(result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Boolean result = + (Boolean) + cx.evaluateString( + scope, "'1234'.startsWith('');", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.startsWith('', 0);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.startsWith('', 1);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.startsWith('', 4);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.startsWith('', 5);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.startsWith('', 42);", "test", 1, null); + assertTrue(result); + + return null; + }); } @Test public void testEndsWithEmpty() { - Boolean result = - (Boolean) cx.evaluateString(scope, "'1234'.endsWith('');", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.endsWith('', 0);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.endsWith('', 1);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.endsWith('', 4);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.endsWith('', 5);", "test", 1, null); - assertTrue(result); - - result = (Boolean) cx.evaluateString(scope, "'1234'.endsWith('', 42);", "test", 1, null); - assertTrue(result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Boolean result = + (Boolean) + cx.evaluateString( + scope, "'1234'.endsWith('');", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.endsWith('', 0);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.endsWith('', 1);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.endsWith('', 4);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.endsWith('', 5);", "test", 1, null); + assertTrue(result); + + result = + (Boolean) + cx.evaluateString( + scope, "'1234'.endsWith('', 42);", "test", 1, null); + assertTrue(result); + + return null; + }); } @Test public void testTagify() { - Object result = cx.evaluateString(scope, "'tester'.big()", "test", 1, null); - assertEquals("tester", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = cx.evaluateString(scope, "'tester'.big()", "test", 1, null); + assertEquals("tester", result); + + result = cx.evaluateString(scope, "'\"tester\"'.big()", "test", 1, null); + assertEquals("\"tester\"", result); - result = cx.evaluateString(scope, "'\"tester\"'.big()", "test", 1, null); - assertEquals("\"tester\"", result); + result = cx.evaluateString(scope, "'\"tester\"'.big()", "test", 1, null); + assertEquals("\"tester\"", result); - result = cx.evaluateString(scope, "'\"tester\"'.big()", "test", 1, null); - assertEquals("\"tester\"", result); + result = cx.evaluateString(scope, "'tester'.fontsize()", "test", 1, null); + assertEquals("tester", result); - result = cx.evaluateString(scope, "'tester'.fontsize()", "test", 1, null); - assertEquals("tester", result); + result = cx.evaluateString(scope, "'tester'.fontsize(null)", "test", 1, null); + assertEquals("tester", result); - result = cx.evaluateString(scope, "'tester'.fontsize(null)", "test", 1, null); - assertEquals("tester", result); + result = + cx.evaluateString( + scope, "'tester'.fontsize(undefined)", "test", 1, null); + assertEquals("tester", result); - result = cx.evaluateString(scope, "'tester'.fontsize(undefined)", "test", 1, null); - assertEquals("tester", result); + result = cx.evaluateString(scope, "'tester'.fontsize(123)", "test", 1, null); + assertEquals("tester", result); - result = cx.evaluateString(scope, "'tester'.fontsize(123)", "test", 1, null); - assertEquals("tester", result); + result = + cx.evaluateString( + scope, "'tester'.fontsize('\"123\"')", "test", 1, null); + assertEquals("tester", result); - result = cx.evaluateString(scope, "'tester'.fontsize('\"123\"')", "test", 1, null); - assertEquals("tester", result); + return null; + }); } @Test public void testTagifyPrototypeNull() { - for (String call : - new String[] { - "big", - "blink", - "bold", - "fixed", - "fontcolor", - "fontsize", - "italics", - "link", - "small", - "strike", - "sub", - "sup" - }) { - String code = - "try { String.prototype." + call + ".call(null);} catch (e) { e.message }"; - Object result = cx.evaluateString(scope, code, "test", 1, null); - assertEquals( - "String.prototype." + call + " method called on null or undefined", result); - } + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + for (String call : + new String[] { + "big", + "blink", + "bold", + "fixed", + "fontcolor", + "fontsize", + "italics", + "link", + "small", + "strike", + "sub", + "sup" + }) { + String code = + "try { String.prototype." + + call + + ".call(null);} catch (e) { e.message }"; + Object result = cx.evaluateString(scope, code, "test", 1, null); + assertEquals( + "String.prototype." + call + " method called on null or undefined", + result); + } + + return null; + }); } @Test public void testTagifyPrototypeUndefined() { - for (String call : - new String[] { - "big", - "blink", - "bold", - "fixed", - "fontcolor", - "fontsize", - "italics", - "link", - "small", - "strike", - "sub", - "sup" - }) { - String code = - "try { String.prototype." + call + ".call(undefined);} catch (e) { e.message }"; - Object result = cx.evaluateString(scope, code, "test", 1, null); - assertEquals( - "String.prototype." + call + " method called on null or undefined", result); - } + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + for (String call : + new String[] { + "big", + "blink", + "bold", + "fixed", + "fontcolor", + "fontsize", + "italics", + "link", + "small", + "strike", + "sub", + "sup" + }) { + String code = + "try { String.prototype." + + call + + ".call(undefined);} catch (e) { e.message }"; + Object result = cx.evaluateString(scope, code, "test", 1, null); + assertEquals( + "String.prototype." + call + " method called on null or undefined", + result); + } + + return null; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java b/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java index 9d42679064..bb7a449c0b 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/NumericSeparatorTest.java @@ -6,46 +6,87 @@ import static org.junit.Assert.assertEquals; -import org.junit.After; -import org.junit.Before; +import org.junit.Assert; 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; public class NumericSeparatorTest { - 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(); - } /** Special Tokenizer test for numeric constant at end. */ @Test public void numericAtEndOneDigit() { - Object result = cx.evaluateString(scope, "1", "test", 1, null); - assertEquals(1.0, result); + 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; + }); } /** Special Tokenizer test for numeric constant at end. */ @Test public void numericAtEndManyDigits() { - Object result = cx.evaluateString(scope, "1234", "test", 1, null); - assertEquals(1234, result); + Utils.runWithOptimizationLevel( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = cx.evaluateString(scope, "1234", "test", 1, null); + assertEquals(1234.0, result); + + return null; + }, + -1); + + // the byte code generator adds a cast to integer + Utils.runWithOptimizationLevel( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = cx.evaluateString(scope, "1234", "test", 1, null); + assertEquals(1234, result); + + return null; + }, + 0); + + Utils.runWithOptimizationLevel( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = cx.evaluateString(scope, "1234", "test", 1, null); + assertEquals(1234, result); + + return null; + }, + 1); } /** Special Tokenizer test for numeric separator constant at end. */ - @Test(expected = EvaluatorException.class) + @Test public void numericSeparatorAtEnd() { - cx.evaluateString(scope, "1_", "test", 1, null); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + try { + cx.evaluateString(scope, "1_", "test", 1, null); + Assert.fail("EvaluatorException expected"); + } catch (EvaluatorException e) { + // expected + } + + return null; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/ObjectSealFreezeTest.java b/testsrc/org/mozilla/javascript/tests/es6/ObjectSealFreezeTest.java index 9ee2380c1b..d5fe73ed1d 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/ObjectSealFreezeTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/ObjectSealFreezeTest.java @@ -9,228 +9,290 @@ 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; +import org.mozilla.javascript.tests.Utils; public class ObjectSealFreezeTest { - 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 testSealWriteToExistingWritableProperty() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " var r = {};" - + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" - + " Object.seal(r);" - + " r.a = 'Rhino';" - + " return r.a;" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("Rhino", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " var r = {};" + + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" + + " Object.seal(r);" + + " r.a = 'Rhino';" + + " return r.a;" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals("Rhino", result); + + return null; + }); } @Test public void testSealWriteToExistingWritablePropertyStrict() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " 'use strict';" - + " var r = {};" - + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" - + " Object.seal(r);" - + " r.a='Rhino';" - + " return r.a;" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("Rhino", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " 'use strict';" + + " var r = {};" + + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" + + " Object.seal(r);" + + " r.a='Rhino';" + + " return r.a;" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals("Rhino", result); + + return null; + }); } @Test public void testSealWriteToExistingSymbolProperty() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " var sym = Symbol('X');" - + " var r = {};" - + " r[sym] = 'abc';" - + " Object.seal(r);" - + " r[sym] = 'Rhino';" - + " return r[sym];" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("Rhino", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " var sym = Symbol('X');" + + " var r = {};" + + " r[sym] = 'abc';" + + " Object.seal(r);" + + " r[sym] = 'Rhino';" + + " return r[sym];" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals("Rhino", result); + + return null; + }); } @Test public void testSealWriteToExistingSymbolPropertyStrict() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " 'use strict';" - + " var sym = Symbol('X');" - + " var r = {};" - + " r[sym] = 'abc';" - + " Object.seal(r);" - + " r[sym] = 'Rhino';" - + " return r[sym];" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("Rhino", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " 'use strict';" + + " var sym = Symbol('X');" + + " var r = {};" + + " r[sym] = 'abc';" + + " Object.seal(r);" + + " r[sym] = 'Rhino';" + + " return r[sym];" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals("Rhino", result); + + return null; + }); } @Test public void testFreezeWriteToExistingWritableProperty() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " var r = {};" - + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" - + " Object.freeze(r);" - + " r.a = 'Rhino';" - + " return r.a;" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("abc", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " var r = {};" + + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" + + " Object.freeze(r);" + + " r.a = 'Rhino';" + + " return r.a;" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals("abc", result); + + return null; + }); } @Test public void testFreezeWriteToExistingWritablePropertyStrict() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " 'use strict';" - + " var r = {};" - + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" - + " Object.freeze(r);" - + " r.a='Rhino';" - + " return r.a;" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("Cannot add properties to this object because extensible is false.", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " 'use strict';" + + " var r = {};" + + " Object.defineProperties(r, { a: { writable: true, value: 'abc' } });" + + " Object.freeze(r);" + + " r.a='Rhino';" + + " return r.a;" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals( + "Cannot add properties to this object because extensible is false.", + result); + + return null; + }); } @Test public void testFreezeWriteToExistingSymbolProperty() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " var sym = Symbol('X');" - + " var r = {};" - + " r[sym] = 'abc';" - + " Object.freeze(r);" - + " r[sym] = 'Rhino';" - + " return r[sym];" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("abc", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " var sym = Symbol('X');" + + " var r = {};" + + " r[sym] = 'abc';" + + " Object.freeze(r);" + + " r[sym] = 'Rhino';" + + " return r[sym];" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals("abc", result); + + return null; + }); } @Test public void testFreezeWriteToExistingSymbolPropertyStrict() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " 'use strict';" - + " var sym = Symbol('X');" - + " var r = {};" - + " r[sym] = 'abc';" - + " Object.freeze(r);" - + " r[sym] = 'Rhino';" - + " return r[sym];" - + "};" - + "try { " - + " foo();" - + "} catch (e) { e.message }", - "test", - 1, - null); - assertEquals("Cannot add properties to this object because extensible is false.", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " 'use strict';" + + " var sym = Symbol('X');" + + " var r = {};" + + " r[sym] = 'abc';" + + " Object.freeze(r);" + + " r[sym] = 'Rhino';" + + " return r[sym];" + + "};" + + "try { " + + " foo();" + + "} catch (e) { e.message }", + "test", + 1, + null); + assertEquals( + "Cannot add properties to this object because extensible is false.", + result); + + return null; + }); } @Test public void testObjectConstructorForNonExtensibleFunctions() { - Object result = - cx.evaluateString( - scope, - "foo = function() {" - + " var res = '';\n" - + " var a = JSON.stringify;\n" - + " Object.preventExtensions(a);\n" - + " res += 'a.isExtensible = ' + Object.isExtensible(a);\n" - + " res += '\\n';\n" - + " var b = Object(a);\n" - + " res += typeof b;\n" - + " res += '\\n';\n" - + " res += a===b;\n" - + " res += '\\n';\n" - + " res += 'b.isExtensible = ' + Object.isExtensible(b);\n" - + " return res;\n" - + "};" - + " foo();", - "test", - 1, - null); - assertEquals("a.isExtensible = false\nfunction\ntrue\nb.isExtensible = false" + "", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "foo = function() {" + + " var res = '';\n" + + " var a = JSON.stringify;\n" + + " Object.preventExtensions(a);\n" + + " res += 'a.isExtensible = ' + Object.isExtensible(a);\n" + + " res += '\\n';\n" + + " var b = Object(a);\n" + + " res += typeof b;\n" + + " res += '\\n';\n" + + " res += a===b;\n" + + " res += '\\n';\n" + + " res += 'b.isExtensible = ' + Object.isExtensible(b);\n" + + " return res;\n" + + "};" + + " foo();", + "test", + 1, + null); + assertEquals( + "a.isExtensible = false\nfunction\ntrue\nb.isExtensible = false" + "", + result); + + return null; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/PromiseTest.java b/testsrc/org/mozilla/javascript/tests/es6/PromiseTest.java index 9b1edc97fe..be44b5730a 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/PromiseTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/PromiseTest.java @@ -9,62 +9,62 @@ 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; import org.mozilla.javascript.TopLevel; +import org.mozilla.javascript.tests.Utils; public class PromiseTest { - private Context cx; - private ScriptableObject scope; - - @Before - public void setUp() { - cx = Context.enter(); - cx.setLanguageVersion(Context.VERSION_ES6); - scope = cx.initSafeStandardObjects(new TopLevel(), false); - } - - @After - public void tearDown() { - Context.exit(); - } - @Test public void ctorCallableThis() { - Object result = - cx.evaluateString( - scope, - " var r = '';" - + " var p = new Promise(function(resolve, reject) {\n" - + " r += this;\n" - + " });\n" - + " r += ' done';\n" - + " r;", - "test", - 1, - null); - assertEquals("[object global] done", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(new TopLevel(), false); + + Object result = + cx.evaluateString( + scope, + " var r = '';" + + " var p = new Promise(function(resolve, reject) {\n" + + " r += this;\n" + + " });\n" + + " r += ' done';\n" + + " r;", + "test", + 1, + null); + assertEquals("[object global] done", result); + + return null; + }); } @Test public void ctorCallableThisStrict() { - Object result = - cx.evaluateString( - scope, - "'use strict';" - + " var r = '';" - + " var p = new Promise(function(resolve, reject) {\n" - + " r += this === undefined;\n" - + " });\n" - + " r += ' done';\n" - + " r;", - "test", - 1, - null); - assertEquals("true done", result); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + Object result = + cx.evaluateString( + scope, + "'use strict';" + + " var r = '';" + + " var p = new Promise(function(resolve, reject) {\n" + + " r += this === undefined;\n" + + " });\n" + + " r += ' done';\n" + + " r;", + "test", + 1, + null); + assertEquals("true done", result); + + return null; + }); } } diff --git a/testsrc/org/mozilla/javascript/tests/es6/PropertyTest.java b/testsrc/org/mozilla/javascript/tests/es6/PropertyTest.java index 7174c8a3f2..8ea34f50c8 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/PropertyTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/PropertyTest.java @@ -5,100 +5,119 @@ import java.lang.reflect.Method; import org.junit.Test; import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.tests.Utils; public class PropertyTest { - private Scriptable scope; - @Test public void prototypeProperty() throws Exception { - final String expected = "undefined - true - true | function - function"; - - final String script = - "var desc = Object.getOwnPropertyDescriptor(MyHostObject, 'foo');" - + "var result = '' + desc.writable + ' - ' + desc.configurable + ' - ' + desc.enumerable;" - + "result = result + ' | ' + typeof desc.get + ' - ' + typeof desc.set;" - + "result;"; - - Context cx = Context.enter(); - try { - final ScriptableObject topScope = cx.initStandardObjects(); - final MyHostObject myHostObject = new MyHostObject(); - - // define custom getter method - final Method getter = MyHostObject.class.getMethod("getFoo"); - final Method setter = MyHostObject.class.getMethod("setFoo", String.class); - myHostObject.defineProperty("foo", null, getter, setter, ScriptableObject.EMPTY); - topScope.put("MyHostObject", topScope, myHostObject); - - final String result = (String) cx.evaluateString(topScope, script, "myScript", 1, null); - - assertEquals(expected, result); - } finally { - Context.exit(); - } + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + final String expected = "undefined - true - true | function - function"; + + final String script = + "var desc = Object.getOwnPropertyDescriptor(MyHostObject, 'foo');" + + "var result = '' + desc.writable + ' - ' + desc.configurable + ' - ' + desc.enumerable;" + + "result = result + ' | ' + typeof desc.get + ' - ' + typeof desc.set;" + + "result;"; + + try { + final MyHostObject myHostObject = new MyHostObject(); + + // define custom getter method + final Method getter = MyHostObject.class.getMethod("getFoo"); + final Method setter = MyHostObject.class.getMethod("setFoo", String.class); + myHostObject.defineProperty( + "foo", null, getter, setter, ScriptableObject.EMPTY); + scope.put("MyHostObject", scope, myHostObject); + } catch (Exception e) { + } + + final String result = + (String) cx.evaluateString(scope, script, "myScript", 1, null); + + assertEquals(expected, result); + + return null; + }); } @Test public void redefineGetterProperty() throws Exception { - final String expected = "undefined - true - true | function - function"; - - final String script = - "Object.defineProperty(MyHostObject, 'foo', { enumerable: !0, configurable: !0, set: function() { return !0 }});\n" - + "var desc = Object.getOwnPropertyDescriptor(MyHostObject, 'foo');" - + "var result = '' + desc.writable + ' - ' + desc.configurable + ' - ' + desc.enumerable;" - + "result = result + ' | ' + typeof desc.get + ' - ' + typeof desc.set;" - + "result;"; - - Context cx = Context.enter(); - try { - final ScriptableObject topScope = cx.initStandardObjects(); - final MyHostObject myHostObject = new MyHostObject(); - - // define custom getter method - final Method getter = MyHostObject.class.getMethod("getFoo"); - final Method setter = MyHostObject.class.getMethod("setFoo", String.class); - myHostObject.defineProperty("foo", null, getter, setter, ScriptableObject.EMPTY); - topScope.put("MyHostObject", topScope, myHostObject); - - final String result = (String) cx.evaluateString(topScope, script, "myScript", 1, null); - - assertEquals(expected, result); - } finally { - Context.exit(); - } + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + final String expected = "undefined - true - true | function - function"; + + final String script = + "Object.defineProperty(MyHostObject, 'foo', { enumerable: !0, configurable: !0, set: function() { return !0 }});\n" + + "var desc = Object.getOwnPropertyDescriptor(MyHostObject, 'foo');" + + "var result = '' + desc.writable + ' - ' + desc.configurable + ' - ' + desc.enumerable;" + + "result = result + ' | ' + typeof desc.get + ' - ' + typeof desc.set;" + + "result;"; + + try { + final MyHostObject myHostObject = new MyHostObject(); + + // define custom getter method + final Method getter = MyHostObject.class.getMethod("getFoo"); + final Method setter = MyHostObject.class.getMethod("setFoo", String.class); + myHostObject.defineProperty( + "foo", null, getter, setter, ScriptableObject.EMPTY); + scope.put("MyHostObject", scope, myHostObject); + } catch (Exception e) { + } + + final String result = + (String) cx.evaluateString(scope, script, "myScript", 1, null); + + assertEquals(expected, result); + + return null; + }); } @Test public void redefineSetterProperty() throws Exception { - final String expected = "undefined - true - true | function - function"; - - final String script = - "Object.defineProperty(MyHostObject, 'foo', { enumerable: !0, configurable: !0, get: function() { return !0 }});\n" - + "var desc = Object.getOwnPropertyDescriptor(MyHostObject, 'foo');" - + "var result = '' + desc.writable + ' - ' + desc.configurable + ' - ' + desc.enumerable;" - + "result = result + ' | ' + typeof desc.get + ' - ' + typeof desc.set;" - + "result;"; - - Context cx = Context.enter(); - try { - final ScriptableObject topScope = cx.initStandardObjects(); - final MyHostObject myHostObject = new MyHostObject(); - - // define custom getter method - final Method getter = MyHostObject.class.getMethod("getFoo"); - final Method setter = MyHostObject.class.getMethod("setFoo", String.class); - myHostObject.defineProperty("foo", null, getter, setter, ScriptableObject.EMPTY); - topScope.put("MyHostObject", topScope, myHostObject); - - final String result = (String) cx.evaluateString(topScope, script, "myScript", 1, null); - - assertEquals(expected, result); - } finally { - Context.exit(); - } + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); + + final String expected = "undefined - true - true | function - function"; + + final String script = + "Object.defineProperty(MyHostObject, 'foo', { enumerable: !0, configurable: !0, get: function() { return !0 }});\n" + + "var desc = Object.getOwnPropertyDescriptor(MyHostObject, 'foo');" + + "var result = '' + desc.writable + ' - ' + desc.configurable + ' - ' + desc.enumerable;" + + "result = result + ' | ' + typeof desc.get + ' - ' + typeof desc.set;" + + "result;"; + + try { + final MyHostObject myHostObject = new MyHostObject(); + + // define custom getter method + Method getter = MyHostObject.class.getMethod("getFoo"); + final Method setter = MyHostObject.class.getMethod("setFoo", String.class); + myHostObject.defineProperty( + "foo", null, getter, setter, ScriptableObject.EMPTY); + scope.put("MyHostObject", scope, myHostObject); + } catch (Exception e) { + } + + final String result = + (String) cx.evaluateString(scope, script, "myScript", 1, null); + + assertEquals(expected, result); + + return null; + }); } public static class MyHostObject extends ScriptableObject { diff --git a/testsrc/org/mozilla/javascript/tests/es6/TaggedTemplateLiteralTest.java b/testsrc/org/mozilla/javascript/tests/es6/TaggedTemplateLiteralTest.java index fe4687aa8e..da2736d98c 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/TaggedTemplateLiteralTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/TaggedTemplateLiteralTest.java @@ -78,9 +78,9 @@ public TaggedTemplateLiteral getNode() { } @Override - public boolean visit(AstNode node) { - if (node instanceof TaggedTemplateLiteral) { - this.node = (TaggedTemplateLiteral) node; + public boolean visit(AstNode astNode) { + if (astNode instanceof TaggedTemplateLiteral) { + this.node = (TaggedTemplateLiteral) astNode; return false; } return true; @@ -101,9 +101,9 @@ public Name getNode() { } @Override - public boolean visit(AstNode node) { - if (node instanceof Name) { - Name nameNode = (Name) node; + public boolean visit(AstNode astNode) { + if (astNode instanceof Name) { + Name nameNode = (Name) astNode; if (nameNode.getIdentifier().equals(name)) { this.node = nameNode; return false; diff --git a/testsrc/org/mozilla/javascript/tests/es6/TypedArrayJavaTest.java b/testsrc/org/mozilla/javascript/tests/es6/TypedArrayJavaTest.java index ffdc7318fa..c325cf8347 100644 --- a/testsrc/org/mozilla/javascript/tests/es6/TypedArrayJavaTest.java +++ b/testsrc/org/mozilla/javascript/tests/es6/TypedArrayJavaTest.java @@ -4,7 +4,8 @@ import org.junit.Test; import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; +import org.mozilla.javascript.ScriptableObject; +import org.mozilla.javascript.tests.Utils; /** Test for TypedArrays. */ public class TypedArrayJavaTest { @@ -109,16 +110,18 @@ private static void allTypes(String script, String expected) throws Exception { "Uint8ClampedArray" }; - Context cx = Context.enter(); - cx.setLanguageVersion(Context.VERSION_ES6); - Scriptable global = cx.initStandardObjects(); + Utils.runWithAllOptimizationLevels( + cx -> { + cx.setLanguageVersion(Context.VERSION_ES6); + ScriptableObject scope = cx.initStandardObjects(); - for (String type : allNativeTypes) { - script = script.replace("§§type§§", type); - Object obj = cx.evaluateString(global, script, "", 1, null); - assertEquals(expected, obj); - } + for (String type : allNativeTypes) { + String scr = script.replace("§§type§§", type); + Object obj = cx.evaluateString(scope, scr, "", 1, null); + assertEquals(expected, obj); + } - Context.exit(); + return null; + }); } }