Search in sources :

Example 36 with SimpleBindings

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

the class CompilableTest method compileStringWithSimpleBindings.

@Test
public void compileStringWithSimpleBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    Bindings bindings = new SimpleBindings();
    bindings.put("numberVal", 6);
    assertThat(script.eval(bindings), instanceOfWith(Number.class, is(numberCloseTo(12))));
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 37 with SimpleBindings

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

the class ScriptEngineScopeTest method isolatedBindingsWithContext.

@Test
public void isolatedBindingsWithContext() throws ScriptException {
    ScriptContext context = new SimpleScriptContext();
    Bindings binding = engine.createBindings();
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    engine.eval("var value = 'Alnilam'", context);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Alnilam")));
    context.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("typeof value", context), instanceOfWith(String.class, is("undefined")));
    context.setBindings(binding, ScriptContext.ENGINE_SCOPE);
    assertThat(engine.eval("value", context), instanceOfWith(String.class, is("Alnilam")));
}
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 38 with SimpleBindings

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

the class ScriptEngineScopeTest method simpleBindingsAndEval.

@Test
public void simpleBindingsAndEval() throws ScriptException {
    Bindings bindings = new SimpleBindings();
    assertThat(bindings, notNullValue());
    assertThat(bindings.entrySet(), is(empty()));
    bindings.put("nullValue", null);
    bindings.put("intValue", 1);
    bindings.put("doubleValue", 1.5);
    bindings.put("booleanValue", true);
    bindings.put("stringValue", "Cepheus");
    Object nullValue = engine.eval("nullValue", bindings);
    Object intValue = engine.eval("intValue", bindings);
    Object doubleValue = engine.eval("doubleValue", bindings);
    Object stringValue = engine.eval("stringValue", bindings);
    Object booleanValue = engine.eval("booleanValue", bindings);
    assertThat(nullValue, nullValue());
    assertThat(intValue, instanceOfWith(Number.class, is(numberCloseTo(1))));
    assertThat(doubleValue, instanceOfWith(Number.class, is(numberCloseTo(1.5))));
    assertThat(stringValue, instanceOfWith(String.class, is("Cepheus")));
    assertThat(booleanValue, instanceOfWith(Boolean.class, sameInstance(Boolean.TRUE)));
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Example 39 with SimpleBindings

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

the class ScriptEngineScopeTest method simpleBindingsRetrieval.

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

Example 40 with SimpleBindings

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

the class ScriptEngineScopeTest method isolatedBindingsAndSimpleBindings.

@Test
public void isolatedBindingsAndSimpleBindings() throws ScriptException {
    Bindings binding = new SimpleBindings();
    engine.eval("var value = 'Alnitak'", binding);
    assertThat(engine.eval("value", binding), instanceOfWith(String.class, is("Alnitak")));
    assertThat(engine.eval("typeof value"), instanceOfWith(String.class, is("undefined")));
    assertThat(engine.eval("typeof value", engine.createBindings()), instanceOfWith(String.class, is("undefined")));
    assertThat(engine.eval("typeof value", new SimpleBindings()), instanceOfWith(String.class, is("undefined")));
}
Also used : SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings) Test(org.junit.Test)

Aggregations

SimpleBindings (javax.script.SimpleBindings)73 Bindings (javax.script.Bindings)50 Test (org.junit.Test)20 ScriptContext (javax.script.ScriptContext)15 SimpleScriptContext (javax.script.SimpleScriptContext)13 Test (org.testng.annotations.Test)11 ScriptException (javax.script.ScriptException)10 ScriptEngine (javax.script.ScriptEngine)8 ScriptEngineManager (javax.script.ScriptEngineManager)7 SlingBindings (org.apache.sling.api.scripting.SlingBindings)7 StringWriter (java.io.StringWriter)6 PrintWriter (java.io.PrintWriter)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 StringReader (java.io.StringReader)4 CompiledScript (javax.script.CompiledScript)4 RenderContext (org.apache.sling.scripting.sightly.render.RenderContext)4 List (java.util.List)3 Resource (org.apache.sling.api.resource.Resource)3 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)3