Search in sources :

Example 26 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class NativeObjectTest method assignNull.

/**
 * @throws Exception if the test fails
 */
@Test
public void assignNull() throws Exception {
    final String script = "function test() {\n" + "  var copy = Object.assign({}, null);\n" + "  output += copy.a;\n" + "}\n" + "var output = '';\n" + "test();\n" + "output";
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            final Scriptable scope = cx.initSafeStandardObjects();
            final Object result = cx.evaluateString(scope, script, "test.js", 1, null);
            assertEquals("undefined", result);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) Test(org.junit.jupiter.api.Test)

Example 27 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class NativeObjectTest method assign.

/**
 * @throws Exception if the test fails
 */
@Test
public void assign() throws Exception {
    final String script = "function test() {\n" + "  var obj = { a: 1 };\n" + "  var copy = Object.assign({}, obj);\n" + "  output += copy.a;\n" + "}\n" + "var output = '';\n" + "test();\n" + "output";
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            final Scriptable scope = cx.initSafeStandardObjects();
            final Object result = cx.evaluateString(scope, script, "test.js", 1, null);
            assertEquals("1", result);
            return null;
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) Test(org.junit.jupiter.api.Test)

Example 28 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class ParserTest method test.

private static void test(final String script, final Object expected) {
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            try {
                Scriptable scope = cx.initSafeStandardObjects();
                ScriptableObject.defineClass(scope, MyHostObject.class);
                final Object o = cx.evaluateString(scope, script, "test_script", 1, null);
                assertEquals(expected, o);
                return o;
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) EvaluatorException(net.sourceforge.htmlunit.corejs.javascript.EvaluatorException)

Example 29 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class PrimitiveTypeScopeResolutionTest method testWithTwoScopes.

private static void testWithTwoScopes(final String scriptScope1, final String scriptScope2) {
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            final Scriptable scope1 = cx.initSafeStandardObjects(new MySimpleScriptableObject("scope1"));
            final Scriptable scope2 = cx.initSafeStandardObjects(new MySimpleScriptableObject("scope2"));
            cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
            scope1.put("scope2", scope1, scope2);
            return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 30 with Scriptable

use of net.sourceforge.htmlunit.corejs.javascript.Scriptable in project htmlunit-core-js by HtmlUnit.

the class ScriptRuntimeTest method test.

private static void test(final String script, final Object expected) {
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            try {
                Scriptable scope = cx.initSafeStandardObjects();
                final Object o = cx.evaluateString(scope, script, "test_script", 1, null);
                assertEquals(expected, o);
                return o;
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Aggregations

Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)67 Context (net.sourceforge.htmlunit.corejs.javascript.Context)39 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)36 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)27 Test (org.junit.jupiter.api.Test)14 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)12 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)12 Function (net.sourceforge.htmlunit.corejs.javascript.Function)10 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)8 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)8 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)7 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)7 WebClient (com.gargoylesoftware.htmlunit.WebClient)6 LambdaConstructor (net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor)6 LambdaFunction (net.sourceforge.htmlunit.corejs.javascript.LambdaFunction)6 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)5 Method (java.lang.reflect.Method)5 BaseFunction (net.sourceforge.htmlunit.corejs.javascript.BaseFunction)4 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)4 NativeObject (net.sourceforge.htmlunit.corejs.javascript.NativeObject)4