Skip to content

Commit

Permalink
simplify test code
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jun 6, 2024
1 parent 05c033d commit b03f78c
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 537 deletions.
46 changes: 46 additions & 0 deletions testsrc/org/mozilla/javascript/tests/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

package org.mozilla.javascript.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextAction;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
* Misc utilities to make test code easier.
Expand Down Expand Up @@ -77,4 +82,45 @@ public static int[] getTestOptLevels() {
}
return DEFAULT_OPT_LEVELS;
}

public static void assertWithAllOptimizationLevels(final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
final Scriptable scope = cx.initStandardObjects();
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertWithAllOptimizationLevelsES6(
final Object expected, final String script) {
runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
final Scriptable scope = cx.initStandardObjects();
final Object res = cx.evaluateString(scope, script, "test.js", 0, null);

assertEquals(expected, res);
return null;
});
}

public static void assertEvaluatorExceptionES6(String expectedMessage, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
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());
}

return null;
});
}
}
183 changes: 36 additions & 147 deletions testsrc/org/mozilla/javascript/tests/es2022/NativeObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,186 +5,75 @@
/** Test for the Object.hasOwn */
package org.mozilla.javascript.tests.es2022;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.tests.Utils;

public class NativeObjectTest {

@Test
public void hasStringOwn() {
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;
});
final String code =
"let result = Object.hasOwn({ test: '123' }, 'test');\n" + "'result = ' + result";
Utils.assertWithAllOptimizationLevelsES6("result = true", code);
}

@Test
public void hasUndefinedOwn() {
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;
});
final String code =
"let result = Object.hasOwn({ test: undefined }, 'test');\n"
+ "'result = ' + result;";
Utils.assertWithAllOptimizationLevelsES6("result = true", code);
}

@Test
public void hasNullOwn() {
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;
});
final String code =
"let result = Object.hasOwn({ test: null }, 'test');\n" + "'result = ' + result;";
Utils.assertWithAllOptimizationLevelsES6("result = true", code);
}

@Test
public void hasArrayPropertyOwn() {
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;
});
final String code =
"let dessert = [\"cake\", \"coffee\", \"chocolate\"];\n"
+ "let result = Object.hasOwn(dessert, 2);\n"
+ "'result = ' + result;";
Utils.assertWithAllOptimizationLevelsES6("result = true", code);
}

@Test
public void hHasNoOwn() {
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;
});
public void hasNoOwn() {
final String code =
"let result = Object.hasOwn({ cake: 123 }, 'test');\n" + "'result = ' + result";
Utils.assertWithAllOptimizationLevelsES6("result = false", code);
}

@Test
public void createHasOwn() {
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;
});
final String code =
"var foo = Object.create(null);\n"
+ "foo.prop = 'test';\n"
+ "var result = Object.hasOwn(foo, 'prop');\n"
+ "'result = ' + result;";
Utils.assertWithAllOptimizationLevelsES6("result = true", code);
}

@Test
public void createNoHasOwn() {
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;
});
final String code =
"var result = Object.hasOwn(Object.create({ q: 321 }), 'q');\n"
+ "'result = ' + result; ";
Utils.assertWithAllOptimizationLevelsES6("result = false", code);
}

@Test
public void calledTest() {
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;
});
final String code =
"var called = false;\n"
+ "try {\n"
+ " Object.hasOwn(null, { toString() { called = true } });\n"
+ "} catch (e) {}\n"
+ "'called = ' + called;";
Utils.assertWithAllOptimizationLevelsES6("called = false", code);
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,13 @@
package org.mozilla.javascript.tests.es5;

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 GeneratorToStringTest {
private Context cx;

@Before
public void setUp() {
cx = Context.enter();
cx.setLanguageVersion(Context.VERSION_ES6);
}

@After
public void tearDown() {
Context.exit();
}

@Test
public void generatorsTest() {
String code = " function* f() {\n" + " yield 1;\n" + " }; f.toString();";
test("\n" + "function* f() {\n" + " yield 1;\n" + "}\n", code);
}

private static void test(Object expected, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = new TopLevel();
cx.initStandardObjects(scope);

Object result = cx.evaluateString(scope, js, "test", 1, null);
assertEquals(expected, result);

return null;
});
Utils.assertWithAllOptimizationLevelsES6(
"\n" + "function* f() {\n" + " yield 1;\n" + "}\n", code);
}
}
23 changes: 3 additions & 20 deletions testsrc/org/mozilla/javascript/tests/es6/ArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

package org.mozilla.javascript.tests.es6;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.tests.Utils;

/** Tests for Arguments support. */
Expand All @@ -22,7 +18,7 @@ public void argumentsSymbolIterator() {
+ "}"
+ "foo()";

test(true, code);
Utils.assertWithAllOptimizationLevelsES6(true, code);
}

@Test
Expand All @@ -33,7 +29,7 @@ public void argumentsSymbolIterator2() {
+ "}"
+ "foo()";

test(true, code);
Utils.assertWithAllOptimizationLevelsES6(true, code);
}

@Test
Expand All @@ -48,19 +44,6 @@ public void argumentsForOf() {
+ "}"
+ "foo(1, 2, 3, 5)";

test("1235", code);
}

private static void test(Object expected, String js) {
Utils.runWithAllOptimizationLevels(
cx -> {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject scope = cx.initStandardObjects();

Object result = cx.evaluateString(scope, js, "test", 1, null);
assertEquals(expected, result);

return null;
});
Utils.assertWithAllOptimizationLevelsES6("1235", code);
}
}
Loading

0 comments on commit b03f78c

Please sign in to comment.