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

simplify test code #1483

Closed
wants to merge 2 commits into from
Closed
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
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
Expand Up @@ -2,28 +2,13 @@

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();";
Expand Down
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);
}
}
21 changes: 2 additions & 19 deletions testsrc/org/mozilla/javascript/tests/es6/BoundFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,15 @@
*/
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.Scriptable;
import org.mozilla.javascript.tests.Utils;

public class BoundFunctionTest {

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

Object result =
cx.evaluateString(
scope,
" function foo() {};\n" + " foo.bind({}).name;",
"test",
1,
null);
assertEquals("bound foo", result);
String code = "function foo() {};\n" + " foo.bind({}).name;";

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