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);
}
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());
}
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);
}
}
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);
}
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"));
}
Aggregations