Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix test utils #1807

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,56 @@
public class ComputedPropertiesTest {
@Test
public void objectWithComputedPropertiesWorks() {
String script =
"function f(x) { return x; }\n"
+ "var o = {\n"
+ " a: 1,\n"
+ " 0: 2,\n"
+ " [-1]: 3\n,"
+ " [f('b')]: 4\n"
+ "};\n"
+ "o.a + o[0] + o['-1'] + o.b";
Utils.assertWithAllModes_ES6(10, script);
Utils.assertWithAllModes_ES6(
10,
Utils.lines(
"function f(x) { return x; }",
"var o = {",
" a: 1,",
" 0: 2,",
" [-1]: 3,",
" [f('b')]: 4",
"};",
"o.a + o[0] + o['-1'] + o.b"));
}

@Test
public void canCoerceFunctionToString() {
String script =
"function f(x) {\n"
+ " var o = {\n"
+ " 1: true,\n"
+ " [2]: false,\n"
+ " [g(x)]: 3\n"
+ " };\n"
+ "}\n"
+ "f.toString()";
Utils.lines(
"function f(x) {",
" var o = {",
" 1: true,",
" [2]: false,",
" [g(x)]: 3",
" };",
"}",
"f.toString()");
String expected =
"function f(x) {\n"
+ " var o = {\n"
+ " 1: true,\n"
+ " [2]: false,\n"
+ " [g(x)]: 3\n"
+ " };\n"
+ "}";
Utils.lines(
"function f(x) {",
" var o = {",
" 1: true,",
" [2]: false,",
" [g(x)]: 3",
" };",
"}");

Utils.assertWithAllModes_ES6(expected, script);
}

@Test
public void computedPropertiesWithSideEffectsWork() {
String script =
"'use strict';\n"
+ "var x = 0;\n"
+ "var o = {\n"
+ " [++x]: 'x',\n"
+ " a: ++x,\n"
+ " [++x]: 'y'\n"
+ "};\n"
+ "o[1] + o.a + o[3]";
Utils.lines(
"'use strict';",
"var x = 0;",
"var o = {",
" [++x]: 'x',",
" a: ++x,",
" [++x]: 'y'",
"};",
"o[1] + o.a + o[3]");
Utils.assertWithAllModes_ES6("x2y", script);
}

Expand All @@ -73,13 +77,14 @@ public void computedPropertyNameAsSymbolForGetterSetterWork() {
@Test
public void yieldWorksForPropertyValues() {
String script =
"function *gen() {\n"
+ " ({x: yield 1});\n"
+ "}\n"
+ "var g = gen()\n"
+ "var res1 = g.next();\n"
+ "var res2 = g.next();\n"
+ "res1.value === 1 && !res1.done && res2.done\n";
Utils.lines(
"function *gen() {",
" ({x: yield 1});",
"}",
"var g = gen()",
"var res1 = g.next();",
"var res2 = g.next();",
"res1.value === 1 && !res1.done && res2.done");

Utils.assertWithAllModes_ES6(Boolean.TRUE, script);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,24 @@ public void testNullishCoalescingPrecedence() {

@Test
public void testNullishCoalescingEvalOnce() {
String script =
"var runs = 0; \n"
+ "function f() { runs++; return 3; } \n"
+ "var eval1 = f() ?? 42; \n"
+ "runs";
Utils.assertWithAllModes_ES6(1, script);
Utils.assertWithAllModes_ES6(
1,
Utils.lines(
"var runs = 0;",
"function f() { runs++; return 3; }",
"var eval1 = f() ?? 42;",
"runs"));
}

@Test
public void testNullishCoalescingDoesNotEvaluateRightHandSideIfNotNecessary() {
String script =
"var runs = 0; \n"
+ "function f() { runs++; return 3; } \n"
+ "var eval1 = 42 ?? f(); \n"
+ "runs";
Utils.assertWithAllModes_ES6(0, script);
Utils.assertWithAllModes_ES6(
0,
Utils.lines(
"var runs = 0;",
"function f() { runs++; return 3; }",
"var eval1 = 42 ?? f();",
"runs"));
}

@Test
Expand Down
13 changes: 12 additions & 1 deletion testutils/src/main/java/org/mozilla/javascript/tests/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public class Utils {
/** The default set of levels to run tests at. */
public static final int[] DEFAULT_OPT_LEVELS = new int[] {-1, 9};

/**
* helper for joining multiple lines into one string, so that you don't need to do {@code
* "line1\n" + "line2\n" + "line3"} by yourself
*/
public static String lines(String... lines) {
return String.join("\n", lines);
}

/**
* Execute the provided script in a fresh context as "myScript.js".
*
Expand Down Expand Up @@ -297,7 +305,10 @@ public static <T extends Exception> void assertException(
final int languageVersion,
final Class<T> expectedThrowable,
final String expectedMessage,
String js) {}
String js) {
assertException(
new ContextFactory(), languageVersion, expectedThrowable, expectedMessage, js);
}

public static <T extends Exception> void assertException(
final ContextFactory contextFactory,
Expand Down