Skip to content

Commit

Permalink
use our test Utils at more places
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri authored and gbrail committed Oct 5, 2024
1 parent d6afb79 commit 7eac09a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -500,41 +499,34 @@ 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);
}

/**
* @throws Exception if an error occurs
*/
@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;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7eac09a

Please sign in to comment.