Search in sources :

Example 26 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ScriptExecutorImpl method doExecuteMain.

private ScriptExports doExecuteMain(final ResourceKey key) {
    final Object exports = executeRequire(key);
    final ScriptValue value = newScriptValue(exports);
    return new ScriptExportsImpl(key, value, exports);
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue)

Example 27 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ScriptValueTest method testValue.

@Test
public void testValue() throws Exception {
    final ScriptValue obj = evalValue("testValue");
    assertEquals(false, obj.isArray());
    assertEquals(false, obj.isFunction());
    assertEquals(false, obj.isObject());
    assertEquals(true, obj.isValue());
    assertEquals(1, obj.getValue());
    assertEquals(0, obj.getKeys().size());
    assertEquals(false, obj.hasMember("a"));
    assertEquals(null, obj.getMember("a"));
    assertEquals(0, obj.getArray().size());
    assertEquals(null, obj.call("a"));
    assertEquals("1", obj.getValue(String.class));
    assertEquals(0, obj.getArray(String.class).size());
    assertEquals(0, obj.getMap().size());
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) Test(org.junit.jupiter.api.Test)

Example 28 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ScriptValueTest method testFunction_error.

@Test
public void testFunction_error() throws Exception {
    final ScriptValue obj = evalValue("testFunctionError");
    try {
        obj.call("a").getValue();
        fail("Should throw exception");
    } catch (final Exception e) {
        assertTrue(e instanceof ResourceProblemException);
    }
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) ResourceProblemException(com.enonic.xp.resource.ResourceProblemException) Test(org.junit.jupiter.api.Test)

Example 29 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ScriptValueFactoryImplTest method newValue_function.

@Test
public void newValue_function() {
    final JSObject obj = Mockito.mock(JSObject.class);
    Mockito.when(obj.isFunction()).thenReturn(true);
    final ScriptValue value = this.factory.newValue(obj);
    assertNotNull(value);
    assertEquals(false, value.isArray());
    assertEquals(true, value.isFunction());
    assertEquals(false, value.isObject());
    assertEquals(false, value.isValue());
    assertNonValue(value);
    assertNonArray(value);
    assertNonObject(value);
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) JSObject(jdk.nashorn.api.scripting.JSObject) Test(org.junit.jupiter.api.Test)

Example 30 with ScriptValue

use of com.enonic.xp.script.ScriptValue in project xp by enonic.

the class ScriptValueFactoryImplTest method newValue_object.

@Test
public void newValue_object() throws Exception {
    final Object obj = execute("var result = {'a':1, 'b':2}; result;");
    final ScriptValue value = this.factory.newValue(obj);
    assertNotNull(value);
    assertEquals(false, value.isArray());
    assertEquals(false, value.isFunction());
    assertEquals(true, value.isObject());
    assertEquals(false, value.isValue());
    assertNonValue(value);
    assertNonArray(value);
    assertNonFunction(value);
    assertNotNull(value.getKeys());
    assertEquals(2, value.getKeys().size());
    assertEquals("a,b", String.join(",", value.getKeys()));
    assertNotNull(value.getMap());
    assertEquals(2, value.getMap().size());
    assertNotNull(value.getMember("a"));
    assertEquals(1, value.getMember("a").getValue());
    assertTrue(value.hasMember("a"));
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) JSObject(jdk.nashorn.api.scripting.JSObject) Test(org.junit.jupiter.api.Test)

Aggregations

ScriptValue (com.enonic.xp.script.ScriptValue)40 Test (org.junit.jupiter.api.Test)20 PortalRequest (com.enonic.xp.portal.PortalRequest)7 PortalResponseSerializer (com.enonic.xp.portal.impl.controller.PortalResponseSerializer)5 PortalRequestMapper (com.enonic.xp.portal.impl.mapper.PortalRequestMapper)5 JSObject (jdk.nashorn.api.scripting.JSObject)4 PortalResponseMapper (com.enonic.xp.portal.impl.mapper.PortalResponseMapper)3 ScriptExports (com.enonic.xp.script.ScriptExports)3 PropertyTree (com.enonic.xp.data.PropertyTree)2 ResourceProblemException (com.enonic.xp.resource.ResourceProblemException)2 WebException (com.enonic.xp.web.WebException)2 ByteSource (com.google.common.io.ByteSource)2 Map (java.util.Map)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 Content (com.enonic.xp.content.Content)1 ContentId (com.enonic.xp.content.ContentId)1 ContentQuery (com.enonic.xp.content.ContentQuery)1 Contents (com.enonic.xp.content.Contents)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)1