use of com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue in project graal by oracle.
the class ProxyAPITest method testProxyExecutable.
@Test
public void testProxyExecutable() {
ProxyExecutableTest proxy = new ProxyExecutableTest();
Value value = context.asValue(proxy);
assertTrue(value.canExecute());
assertSame(proxy, value.asProxyObject());
assertEquals(0, proxy.executeCounter);
proxy.execute = (args) -> {
assertEquals(2, args.length);
assertEquals("a", args[0].asString());
assertEquals('a', args[0].as(Object.class));
ValueAssert.assertValue(context, args[0], Trait.STRING);
assertTrue(args[1].isNumber());
assertEquals((byte) 42, args[1].asByte());
assertEquals((short) 42, args[1].asShort());
assertEquals(42, args[1].asInt());
assertEquals(42L, args[1].asLong());
assertEquals(42, args[1].as(Object.class));
ValueAssert.assertValue(context, args[1], Trait.NUMBER);
return 42;
};
assertEquals(42, value.execute('a', 42).asInt());
assertEquals(1, proxy.executeCounter);
final RuntimeException ex = new RuntimeException();
proxy.execute = (args) -> {
throw ex;
};
try {
value.execute();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertSame(ex, e.asHostException());
assertEquals(2, proxy.executeCounter);
}
assertValue(context, value, Trait.PROXY_OBJECT, Trait.EXECUTABLE);
}
use of com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue in project graal by oracle.
the class ProxyAPITest method testProxyObject.
@Test
public void testProxyObject() {
ProxyObjectTest proxy = new ProxyObjectTest();
Value value = context.asValue(proxy);
assertTrue(value.hasMembers());
assertSame(proxy, value.asProxyObject());
proxy.putMember = (key, v) -> {
assertEquals("foo", key);
assertEquals(42, v.asInt());
ValueAssert.assertValue(context, v, Trait.NUMBER);
return null;
};
value.putMember("foo", 42);
assertEquals(1, proxy.putMemberCounter);
proxy.hasMember = (key) -> {
assertEquals("foo", key);
return true;
};
assertTrue(value.hasMember("foo"));
assertEquals(1, proxy.hasMemberCounter);
proxy.getMember = (key) -> {
assertEquals("foo", key);
return 42;
};
assertEquals(42, value.getMember("foo").asInt());
assertEquals(1, proxy.getMemberCounter);
List<String> testKeys = Arrays.asList("a", "b", "c");
proxy.getMemberKeys = () -> {
return testKeys;
};
assertEquals(new HashSet<>(testKeys), value.getMemberKeys());
assertEquals(1, proxy.getMemberKeysCounter);
proxy.getMemberKeys = () -> {
return testKeys.toArray();
};
assertEquals(new HashSet<>(testKeys), value.getMemberKeys());
assertEquals(2, proxy.getMemberKeysCounter);
proxy.getMemberKeys = () -> {
return ProxyArray.fromArray(testKeys.toArray());
};
assertEquals(new HashSet<>(testKeys), value.getMemberKeys());
assertEquals(3, proxy.getMemberKeysCounter);
proxy.getMemberKeys = () -> {
return null;
};
assertEquals(new HashSet<>(), value.getMemberKeys());
assertEquals(4, proxy.getMemberKeysCounter);
proxy.getMemberKeys = () -> {
return new Object[] { "a", 'c' };
};
assertEquals(new HashSet<>(Arrays.asList("a", "c")), value.getMemberKeys());
assertEquals(5, proxy.getMemberKeysCounter);
proxy.getMemberKeys = () -> {
return new Object[] { 42 };
};
assertFails(() -> value.getMemberKeys().iterator().next(), PolyglotException.class, (e) -> {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof ClassCastException);
});
assertEquals(6, proxy.getMemberKeysCounter);
proxy.getMemberKeys = () -> {
return new Object();
};
assertFails(() -> value.getMemberKeys(), PolyglotException.class, (e) -> {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof IllegalStateException);
});
assertEquals(7, proxy.getMemberKeysCounter);
proxy.getMemberKeys = () -> {
return testKeys.toArray();
};
proxy.hasMember = (key) -> {
return testKeys.contains(key);
};
proxy.getMember = (key) -> {
return 42;
};
proxy.putMember = (key, v) -> {
return null;
};
assertValue(context, value, Trait.PROXY_OBJECT, Trait.MEMBERS);
}
use of com.oracle.truffle.api.test.polyglot.ValueAssert.assertValue in project graal by oracle.
the class ProxyAPITest method testProxyInstantiable.
@Test
public void testProxyInstantiable() {
ProxyInstantiableTest proxy = new ProxyInstantiableTest();
Value value = context.asValue(proxy);
assertTrue(value.canInstantiate());
assertSame(proxy, value.asProxyObject());
assertEquals(0, proxy.newInstanceCounter);
proxy.newInstance = (args) -> {
assertEquals(2, args.length);
assertEquals("a", args[0].asString());
assertEquals('a', args[0].as(Object.class));
ValueAssert.assertValue(context, args[0], Trait.STRING);
assertTrue(args[1].isNumber());
assertEquals((byte) 42, args[1].asByte());
assertEquals((short) 42, args[1].asShort());
assertEquals(42, args[1].asInt());
assertEquals(42L, args[1].asLong());
assertEquals(42, args[1].as(Object.class));
ValueAssert.assertValue(context, args[1], Trait.NUMBER);
return 42;
};
assertEquals(42, value.newInstance('a', 42).asInt());
assertEquals(1, proxy.newInstanceCounter);
final RuntimeException ex = new RuntimeException();
proxy.newInstance = (args) -> {
throw ex;
};
try {
value.newInstance();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertSame(ex, e.asHostException());
assertEquals(2, proxy.newInstanceCounter);
}
assertValue(context, value, Trait.PROXY_OBJECT, Trait.INSTANTIABLE);
}
Aggregations