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 1, 2024
1 parent e7d0a7d commit b7d13c0
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 273 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
package org.mozilla.javascript.tests;

import org.junit.Assert;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class NullishCoalescingOpTest {
@Test
public void testNullishColascingBasic() {
Utils.runWithAllOptimizationLevels(
cx -> {
Scriptable scope = cx.initStandardObjects();
cx.setLanguageVersion(Context.VERSION_ES6);

String script = "null ?? 'default string'";
Assert.assertEquals(
"default string",
cx.evaluateString(scope, script, "nullish coalescing basic", 0, null));

String script2 = "undefined ?? 'default string'";
Assert.assertEquals(
"default string",
cx.evaluateString(scope, script2, "nullish coalescing basic", 0, null));
return null;
});
Utils.assertWithAllOptimizationLevelsES6("default string", "null ?? 'default string'");
Utils.assertWithAllOptimizationLevelsES6("default string", "undefined ?? 'default string'");
}

@Test
Expand All @@ -43,35 +26,17 @@ public void testNullishColascingShortCircuit() {

@Test
public void testNullishColascingPrecedence() {
Utils.runWithAllOptimizationLevels(
cx -> {
Scriptable scope = cx.initStandardObjects();
cx.setLanguageVersion(Context.VERSION_ES6);

String script1 = "3 == 3 ? 'yes' ?? 'default string' : 'no'";
Assert.assertEquals(
"yes",
cx.evaluateString(scope, script1, "nullish coalescing basic", 0, null));
return null;
});
Utils.assertWithAllOptimizationLevelsES6(
"yes", "3 == 3 ? 'yes' ?? 'default string' : 'no'");
}

@Test
public void testNullishColascingEvalOnce() {
Utils.runWithAllOptimizationLevels(
cx -> {
Scriptable scope = cx.initStandardObjects();
cx.setLanguageVersion(Context.VERSION_ES6);

String script1 =
"var runs = 0; \n"
+ "function f() { runs++; return 3; } \n"
+ "var eval1 = f() ?? 42; \n"
+ "runs";
Assert.assertEquals(
1,
cx.evaluateString(scope, script1, "nullish coalescing basic", 0, null));
return null;
});
String script =
"var runs = 0; \n"
+ "function f() { runs++; return 3; } \n"
+ "var eval1 = f() ?? 42; \n"
+ "runs";
Utils.assertWithAllOptimizationLevelsES6(1, script);
}
}
73 changes: 67 additions & 6 deletions tests/src/test/java/org/mozilla/javascript/tests/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextAction;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.EcmaError;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
Expand Down Expand Up @@ -145,13 +146,73 @@ public static void assertEvaluatorExceptionES6(String expectedMessage, String js
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = cx.initStandardObjects();

try {
cx.evaluateString(scope, js, "test", 1, null);
fail("EvaluatorException expected");
} catch (EvaluatorException e) {
assertEquals(expectedMessage, e.getMessage());
}
EvaluatorException e =
assertThrows(
EvaluatorException.class,
() -> cx.evaluateString(scope, js, "test", 1, null));
assertEquals(expectedMessage, e.getMessage());
return null;
});
}

public static void assertEcmaError(String expectedMessage, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
ScriptableObject scope = cx.initStandardObjects();

EcmaError e =
assertThrows(
EcmaError.class,
() -> cx.evaluateString(scope, js, "test", 1, null));
assertTrue(
"'"
+ e.getMessage()
+ "' does not start with '"
+ expectedMessage
+ "'",
e.getMessage().startsWith(expectedMessage));
return null;
});
}

public static void assertEcmaError_1_8(String expectedMessage, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_1_8);
ScriptableObject scope = cx.initStandardObjects();

EcmaError e =
assertThrows(
EcmaError.class,
() -> cx.evaluateString(scope, js, "test", 1, null));
assertTrue(
"'"
+ e.getMessage()
+ "' does not start with '"
+ expectedMessage
+ "'",
e.getMessage().startsWith(expectedMessage));
return null;
});
}

public static void assertEcmaErrorES6(String expectedMessage, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = cx.initStandardObjects();

EcmaError e =
assertThrows(
EcmaError.class,
() -> cx.evaluateString(scope, js, "test", 1, null));
assertTrue(
"'"
+ e.getMessage()
+ "' does not start with '"
+ expectedMessage
+ "'",
e.getMessage().startsWith(expectedMessage));
return null;
});
}
Expand Down
Loading

0 comments on commit b7d13c0

Please sign in to comment.