Search in sources :

Example 51 with ScriptContext

use of javax.script.ScriptContext in project zaproxy by zaproxy.

the class ScriptVarsUnitTest method shouldClearScriptVariableWithNullValueUsingScriptContext.

@Test
void shouldClearScriptVariableWithNullValueUsingScriptContext() {
    // Given
    String key = createKey();
    String value = createValue();
    String scriptName = "ScriptName";
    ScriptContext scriptContext = createScriptContextWithName(scriptName);
    ScriptVars.setScriptVar(scriptContext, key, value);
    // When
    ScriptVars.setScriptVar(scriptContext, key, null);
    // Then
    assertThat(ScriptVars.getScriptVars(scriptName), not(hasEntry(key, value)));
    assertThat(ScriptVars.getScriptVar(scriptContext, key), is(nullValue()));
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.junit.jupiter.api.Test)

Example 52 with ScriptContext

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

the class CompilableTest method compileStringWithContextAndSimpleBindings.

@Test
public void compileStringWithContextAndSimpleBindings() throws ScriptException {
    CompiledScript script = compilable.compile("numberVal * 2");
    ScriptContext context = new SimpleScriptContext();
    Bindings bindings = new SimpleBindings();
    bindings.put("numberVal", 9);
    context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
    assertThat(script.eval(context), instanceOfWith(Number.class, is(numberCloseTo(18))));
}
Also used : CompiledScript(javax.script.CompiledScript) 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 53 with ScriptContext

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

the class ScriptEngineTest method evalExplicitContext.

@Test
public void evalExplicitContext() throws ScriptException {
    ScriptContext context = engine.getContext();
    assertThat(context, notNullValue());
    Object value = engine.eval("function f() { return 'vvv' } f()", context);
    assertThat(value, instanceOfWith(String.class, is("vvv")));
}
Also used : ScriptContext(javax.script.ScriptContext) Test(org.junit.Test)

Example 54 with ScriptContext

use of javax.script.ScriptContext 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 55 with ScriptContext

use of javax.script.ScriptContext 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)

Aggregations

ScriptContext (javax.script.ScriptContext)126 SimpleScriptContext (javax.script.SimpleScriptContext)83 Bindings (javax.script.Bindings)33 Test (org.junit.Test)30 SimpleBindings (javax.script.SimpleBindings)28 Test (org.junit.jupiter.api.Test)19 ScriptEngine (javax.script.ScriptEngine)18 ScriptException (javax.script.ScriptException)18 HashMap (java.util.HashMap)13 CompiledScript (javax.script.CompiledScript)11 IOException (java.io.IOException)9 Map (java.util.Map)8 ScriptEngineManager (javax.script.ScriptEngineManager)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 Test (org.testng.annotations.Test)7 StringWriter (java.io.StringWriter)6 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)6 XWikiException (com.xpn.xwiki.XWikiException)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5