From 7eac09ab342e9c033e36831e55e5a33a134bf3f0 Mon Sep 17 00:00:00 2001 From: Ronald Brill Date: Sat, 5 Oct 2024 13:20:04 +0200 Subject: [PATCH] use our test Utils at more places --- .../javascript/tests/NativeRegExpTest.java | 62 ++++++++----------- .../javascript/tests/NativeStringTest.java | 15 +---- .../javascript/tests/es6/NativeDateTest.java | 22 ++----- 3 files changed, 34 insertions(+), 65 deletions(-) diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/NativeRegExpTest.java b/rhino/src/test/java/org/mozilla/javascript/tests/NativeRegExpTest.java index 10a198a815..ab0d918765 100644 --- a/rhino/src/test/java/org/mozilla/javascript/tests/NativeRegExpTest.java +++ b/rhino/src/test/java/org/mozilla/javascript/tests/NativeRegExpTest.java @@ -8,7 +8,6 @@ import org.junit.Test; import org.mozilla.javascript.Context; -import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; public class NativeRegExpTest { @@ -340,7 +339,7 @@ private static void testEvaluate(final String expected, final String regex) { + "res += regex.sticky;\n" + "res"; - test(expected, script); + Utils.assertWithAllOptimizationLevelsES6(expected, script); } /** @@ -359,7 +358,7 @@ public void stickyTest() throws Exception { + "res = res + '-' + regex.test(str);\n" + "res = res + '-' + regex.lastIndex;\n" + "res;"; - test("true-9-false-0-false-0", script); + Utils.assertWithAllOptimizationLevelsES6("true-9-false-0-false-0", script); } /** @@ -372,7 +371,7 @@ public void stickyStartOfLine() throws Exception { + "regex.lastIndex = 2;\n" + "var res = '' + regex.test('..foo');\n" + "res;"; - test("false", script); + Utils.assertWithAllOptimizationLevelsES6("false", script); } /** @@ -387,7 +386,7 @@ public void stickyStartOfLineMultiline() throws Exception { + "regex.lastIndex = 2;\n" + "res = res + '-' + regex.test('.\\nfoo');\n" + "res;"; - test("false-true", script); + Utils.assertWithAllOptimizationLevelsES6("false-true", script); } /** @@ -402,7 +401,7 @@ public void matchGlobal() throws Exception { + "res = res + '-' + result[1];\n" + "res = res + '-' + result[2];\n" + "res;"; - test("3-a-a-a", script); + Utils.assertWithAllOptimizationLevelsES6("3-a-a-a", script); } /** @@ -417,7 +416,7 @@ public void matchGlobalSymbol() throws Exception { + "res = res + '-' + result[1];\n" + "res = res + '-' + result[2];\n" + "res;"; - test("3-a-a-a", script); + Utils.assertWithAllOptimizationLevelsES6("3-a-a-a", script); } /** @@ -430,7 +429,7 @@ public void matchDotAll() throws Exception { + "var res = '' + result.length;\n" + "res = res + '-' + result[0];\n" + "res;"; - test("1-bar\nfoo", script); + Utils.assertWithAllOptimizationLevelsES6("1-bar\nfoo", script); } /** @@ -443,7 +442,7 @@ public void matchSticky() throws Exception { + "var res = '' + result.length;\n" + "res = res + '-' + result[0];\n" + "res;"; - test("1-a", script); + Utils.assertWithAllOptimizationLevelsES6("1-a", script); } /** @@ -456,7 +455,7 @@ public void matchStickySymbol() throws Exception { + "var res = '' + result.length;\n" + "res = res + '-' + result[0];\n" + "res;"; - test("1-a", script); + Utils.assertWithAllOptimizationLevelsES6("1-a", script); } /** @@ -470,7 +469,7 @@ public void matchStickyAndGlobal() throws Exception { + "res = res + '-' + result[0];\n" + "res = res + '-' + result[1];\n" + "res;"; - test("2-a-a", script); + Utils.assertWithAllOptimizationLevelsES6("2-a-a", script); } /** @@ -484,7 +483,7 @@ public void matchStickyAndGlobalSymbol() throws Exception { + "res = res + '-' + result[0];\n" + "res = res + '-' + result[1];\n" + "res;"; - test("2-a-a", script); + Utils.assertWithAllOptimizationLevelsES6("2-a-a", script); } /** @@ -500,7 +499,7 @@ public void flagsPropery() throws Exception { + "res = res + '-' + get.enumerable;\n" + "res = res + '-' + get.writable;\n" + "res;"; - test("0-undefined-true-false-undefined", script); + Utils.assertWithAllOptimizationLevelsES6("0-undefined-true-false-undefined", script); } /** @@ -508,33 +507,26 @@ public void flagsPropery() throws Exception { */ @Test public void objectToString() throws Exception { - test("/undefined/undefined", "RegExp.prototype.toString.call({})"); - test("/Foo/undefined", "RegExp.prototype.toString.call({source: 'Foo'})"); - test("/undefined/gy", "RegExp.prototype.toString.call({flags: 'gy'})"); - test("/Foo/g", "RegExp.prototype.toString.call({source: 'Foo', flags: 'g'})"); - test("/Foo/g", "RegExp.prototype.toString.call({source: 'Foo', flags: 'g', sticky: true})"); - - test( + Utils.assertWithAllOptimizationLevelsES6( + "/undefined/undefined", "RegExp.prototype.toString.call({})"); + Utils.assertWithAllOptimizationLevelsES6( + "/Foo/undefined", "RegExp.prototype.toString.call({source: 'Foo'})"); + Utils.assertWithAllOptimizationLevelsES6( + "/undefined/gy", "RegExp.prototype.toString.call({flags: 'gy'})"); + Utils.assertWithAllOptimizationLevelsES6( + "/Foo/g", "RegExp.prototype.toString.call({source: 'Foo', flags: 'g'})"); + Utils.assertWithAllOptimizationLevelsES6( + "/Foo/g", + "RegExp.prototype.toString.call({source: 'Foo', flags: 'g', sticky: true})"); + + Utils.assertWithAllOptimizationLevelsES6( "TypeError: Method \"toString\" called on incompatible object", "try { RegExp.prototype.toString.call(''); } catch (e) { ('' + e).substr(0, 58) }"); - test( + Utils.assertWithAllOptimizationLevelsES6( "TypeError: Method \"toString\" called on incompatible object", "try { RegExp.prototype.toString.call(undefined); } catch (e) { ('' + e).substr(0, 58) }"); - test( + Utils.assertWithAllOptimizationLevelsES6( "TypeError: Method \"toString\" called on incompatible object", "var toString = RegExp.prototype.toString; try { toString(); } catch (e) { ('' + e).substr(0, 58) }"); } - - private static void test(final String expected, final String script) { - Utils.runWithAllOptimizationLevels( - cx -> { - // to have symbol available - cx.setLanguageVersion(Context.VERSION_ES6); - final Scriptable scope = cx.initStandardObjects(); - final String res = - (String) cx.evaluateString(scope, script, "test.js", 0, null); - assertEquals(expected, res); - return null; - }); - } } diff --git a/rhino/src/test/java/org/mozilla/javascript/tests/NativeStringTest.java b/rhino/src/test/java/org/mozilla/javascript/tests/NativeStringTest.java index 364f19b2af..94c3b27dad 100644 --- a/rhino/src/test/java/org/mozilla/javascript/tests/NativeStringTest.java +++ b/rhino/src/test/java/org/mozilla/javascript/tests/NativeStringTest.java @@ -25,22 +25,13 @@ public class NativeStringTest { */ @Test public void toLowerCaseApply() { - assertEvaluates("hello", "var x = String.toLowerCase; x.apply('HELLO')"); - assertEvaluates( + Utils.assertWithAllOptimizationLevels( + "hello", "var x = String.toLowerCase; x.apply('HELLO')"); + Utils.assertWithAllOptimizationLevels( "hello", "String.toLowerCase('HELLO')"); // first patch proposed to #492359 was breaking this } - 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; - }); - } - @Test public void toLocaleLowerCase() { String js = "'\\u0130'.toLocaleLowerCase()"; diff --git a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeDateTest.java b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeDateTest.java index 5f3dc6029b..4286fe0df7 100644 --- a/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeDateTest.java +++ b/tests/src/test/java/org/mozilla/javascript/tests/es6/NativeDateTest.java @@ -261,28 +261,14 @@ private static void ctorDate(final String expected, final String tz) { @Test public void ctorInt() { - final String js = "new Date(951782399000).toISOString()"; - Utils.runWithAllOptimizationLevels( - cx -> { - final Scriptable scope = cx.initStandardObjects(); - - final Object res = cx.evaluateString(scope, js, "test.js", 0, null); - assertEquals("2000-02-28T23:59:59.000Z", res); - return null; - }); + Utils.assertWithAllOptimizationLevels( + "2000-02-28T23:59:59.000Z", "new Date(951782399000).toISOString()"); } @Test public void ctorDouble() { - final String js = "new Date(208e10).toISOString()"; - Utils.runWithAllOptimizationLevels( - cx -> { - final Scriptable scope = cx.initStandardObjects(); - - final Object res = cx.evaluateString(scope, js, "test.js", 0, null); - assertEquals("2035-11-30T01:46:40.000Z", res); - return null; - }); + Utils.assertWithAllOptimizationLevels( + "2035-11-30T01:46:40.000Z", "new Date(208e10).toISOString()"); } @Test