use of org.graalvm.polyglot.Value in project graal by oracle.
the class ConstraintInteropTypesTest method forbidNonPrimitiveObjectReturn.
@Test(expected = PolyglotException.class)
public void forbidNonPrimitiveObjectReturn() {
BrokenTruffleObject obj = new BrokenTruffleObject(this);
polyglot.putMember("value", obj);
Value result = polyglot.getMember("value").execute();
fail("No result, an exception should be thrown: " + result);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ConstraintInteropTypesTest method forbidStringBuilderReturn.
@Test(expected = PolyglotException.class)
public void forbidStringBuilderReturn() {
BrokenTruffleObject obj = new BrokenTruffleObject(new StringBuilder("I am string builder!"));
polyglot.putMember("value", obj);
Value result = polyglot.getMember("value").execute();
fail("No result, an exception should be thrown: " + result);
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueAPITest method testTypeCoercionError.
@Test
public void testTypeCoercionError() {
Value pipe = context.asValue("not a pipe");
assertFails(() -> pipe.as(List.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'java.util.List': Unsupported target type.");
assertFails(() -> pipe.as(Map.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'java.util.Map': Unsupported target type.");
assertFails(() -> pipe.as(Function.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'java.util.function.Function': Unsupported target type.");
assertFails(() -> pipe.as(JavaInterface.class), ClassCastException.class, "Cannot convert 'not a pipe'(language: Java, type: java.lang.String) to Java type 'com.oracle.truffle.api.test.polyglot.ValueAPITest$JavaInterface': Unsupported target type.");
Value other = context.asValue(new EmptyProxy());
assertFails(() -> other.as(List.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'java.util.List': Value must have array elements.");
assertFails(() -> other.as(Map.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'java.util.Map': Value must have members or array elements.");
assertFails(() -> other.as(Function.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'java.util.function.Function': Value must be executable or instantiable.");
assertFails(() -> other.as(JavaInterface.class), ClassCastException.class, "Cannot convert 'proxy'(language: Java, type: com.oracle.truffle.api.test.polyglot.ValueAPITest$EmptyProxy) to Java type 'com.oracle.truffle.api.test.polyglot.ValueAPITest$JavaInterface': Value must have members.");
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueAPITest method objectCoercionTest.
@SuppressWarnings({ "unchecked" })
private <T> void objectCoercionTest(Object value, Class<T> expectedType, Consumer<T> validator) {
Value coerce = context.asValue(new CoerceObject()).getMember("coerce");
T result = (T) context.asValue(value).as(Object.class);
if (result != null) {
assertTrue("expected " + expectedType + " but was " + result.getClass(), expectedType.isInstance(result));
} else if (value != null) {
fail("expected " + expectedType + " but was null");
}
if (validator == null) {
assertEquals(value, result);
} else {
validator.accept(result);
coerce.execute(value, validator);
}
assertValue(context, context.asValue(value));
assertValue(context, context.asValue(result));
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class ValueAPITest method testNullCoercionErrors.
@Test
public void testNullCoercionErrors() {
Value nullValue = context.asValue(null);
assertFails(() -> nullValue.asInt(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'int' using Value.asInt(). " + "You can ensure that the operation is supported using Value.fitsInInt().");
assertFails(() -> nullValue.as(int.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'int'.");
assertFails(() -> nullValue.asByte(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'byte' using Value.asByte(). " + "You can ensure that the operation is supported using Value.fitsInByte().");
assertFails(() -> nullValue.as(byte.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'byte'.");
assertFails(() -> nullValue.asShort(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'short' using Value.asShort(). " + "You can ensure that the operation is supported using Value.fitsInShort().");
assertFails(() -> nullValue.as(short.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'short'.");
assertFails(() -> nullValue.asLong(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'long' using Value.asLong(). " + "You can ensure that the operation is supported using Value.fitsInLong().");
assertFails(() -> nullValue.as(long.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'long'.");
assertFails(() -> nullValue.asFloat(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'float' using Value.asFloat(). " + "You can ensure that the operation is supported using Value.fitsInFloat().");
assertFails(() -> nullValue.as(float.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'float'.");
assertFails(() -> nullValue.asDouble(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'double' using Value.asDouble(). " + "You can ensure that the operation is supported using Value.fitsInDouble().");
assertFails(() -> nullValue.as(double.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'double'.");
assertFails(() -> nullValue.as(char.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'char'.");
assertFails(() -> nullValue.asBoolean(), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'boolean' using Value.asBoolean(). " + "You can ensure that the operation is supported using Value.isBoolean().");
assertFails(() -> nullValue.as(boolean.class), NullPointerException.class, "Cannot convert null value 'null'(language: Java) to Java type 'boolean'.");
}
Aggregations