Search in sources :

Example 11 with Bindings

use of javax.script.Bindings in project es6draft by anba.

the class ScriptEngineTest method contextAndBindings.

@Test
public void contextAndBindings() {
    ScriptContext context = engine.getContext();
    assertThat(context, notNullValue());
    assertThat(context, sameInstance(engine.getContext()));
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    assertThat(globalScope, notNullValue());
    assertThat(globalScope, sameInstance(engine.getBindings(ScriptContext.GLOBAL_SCOPE)));
    assertThat(globalScope, sameInstance(context.getBindings(ScriptContext.GLOBAL_SCOPE)));
    assertThat(globalScope, sameInstance(manager.getBindings()));
    Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    assertThat(engineScope, notNullValue());
    assertThat(engineScope, sameInstance(engine.getBindings(ScriptContext.ENGINE_SCOPE)));
    assertThat(engineScope, sameInstance(context.getBindings(ScriptContext.ENGINE_SCOPE)));
    assertThat(engineScope, not(sameInstance(manager.getBindings())));
}
Also used : ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) Test(org.junit.Test)

Example 12 with Bindings

use of javax.script.Bindings in project es6draft by anba.

the class ScriptEngineScopeTest method globalScopeAccessWithOverrideNewContextSimpleBindings.

@Test
public void globalScopeAccessWithOverrideNewContextSimpleBindings() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Becrux");
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    context.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
    context.getBindings(ScriptContext.ENGINE_SCOPE).put("globalVar", "Antares");
    assertThat(engine.eval("globalVar", context), instanceOfWith(String.class, is("Antares")));
    engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("globalVar");
    assertThat(engine.eval("globalVar"), instanceOfWith(String.class, is("Becrux")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleBindings(javax.script.SimpleBindings) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 13 with Bindings

use of javax.script.Bindings in project es6draft by anba.

the class ScriptEngineScopeTest method createBindingsRetrieval.

@Test
public void createBindingsRetrieval() throws ScriptException {
    Bindings bindings = engine.createBindings();
    engine.eval("var a = 0; b = 1", bindings);
    assertThat(bindings.get("a"), instanceOfWith(Number.class, is(numberCloseTo(0))));
    assertThat(bindings.get("b"), instanceOfWith(Number.class, is(numberCloseTo(1))));
}
Also used : Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 14 with Bindings

use of javax.script.Bindings in project es6draft by anba.

the class ScriptEngineScopeTest method globalScopeAccessWithOverrideNewContext.

@Test
public void globalScopeAccessWithOverrideNewContext() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Canopus");
    ScriptContext context = new SimpleScriptContext();
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    context.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE);
    context.getBindings(ScriptContext.ENGINE_SCOPE).put("globalVar", "Acrux");
    assertThat(engine.eval("globalVar", context), instanceOfWith(String.class, is("Acrux")));
    engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("globalVar");
    assertThat(engine.eval("globalVar"), instanceOfWith(String.class, is("Canopus")));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 15 with Bindings

use of javax.script.Bindings in project es6draft by anba.

the class ScriptEngineScopeTest method globalScopeAccessWithOverride.

@Test
public void globalScopeAccessWithOverride() throws ScriptException {
    Bindings globalScope = engine.getBindings(ScriptContext.GLOBAL_SCOPE);
    globalScope.put("globalVar", "Bellatrix");
    engine.getBindings(ScriptContext.ENGINE_SCOPE).put("globalVar", "Eta Centauri");
    assertThat(engine.eval("globalVar"), instanceOfWith(String.class, is("Eta Centauri")));
    engine.getBindings(ScriptContext.ENGINE_SCOPE).remove("globalVar");
    assertThat(engine.eval("globalVar"), instanceOfWith(String.class, is("Bellatrix")));
}
Also used : Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Aggregations

Bindings (javax.script.Bindings)142 SimpleBindings (javax.script.SimpleBindings)74 Test (org.junit.Test)36 ScriptException (javax.script.ScriptException)27 ScriptContext (javax.script.ScriptContext)24 SimpleScriptContext (javax.script.SimpleScriptContext)21 SlingBindings (org.apache.sling.api.scripting.SlingBindings)18 Test (org.testng.annotations.Test)17 ScriptEngine (javax.script.ScriptEngine)15 CompiledScript (javax.script.CompiledScript)14 Resource (org.apache.sling.api.resource.Resource)11 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)10 SlingScriptHelper (org.apache.sling.api.scripting.SlingScriptHelper)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)7 ArrayList (java.util.ArrayList)7 PrintWriter (java.io.PrintWriter)6 ScriptEngineManager (javax.script.ScriptEngineManager)6