Skip to content

Commit

Permalink
[GR-15244] TypeDescriptor.forValue does not work for an empty array.
Browse files Browse the repository at this point in the history
  • Loading branch information
tzezula committed Apr 16, 2019
1 parent c69c798 commit f4a99ac
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public static TypeDescriptor forValue(final Value value) {
}
switch (contentTypes.size()) {
case 0:
descs.add(intersection(NOTYPE, NULL, BOOLEAN, NUMBER, STRING, HOST_OBJECT, NATIVE_POINTER, OBJECT, ARRAY, EXECUTABLE, INSTANTIABLE));
descs.add(array(intersection(NOTYPE, NULL, BOOLEAN, NUMBER, STRING, HOST_OBJECT, NATIVE_POINTER, OBJECT, ARRAY, EXECUTABLE, INSTANTIABLE)));
break;
case 1:
descs.add(array(contentTypes.iterator().next()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.proxy.ProxyArray;
import org.graalvm.polyglot.proxy.ProxyObject;
import org.graalvm.polyglot.tck.TypeDescriptor;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -606,4 +612,41 @@ public void testInstantiablesWithExecutables() {
Assert.assertFalse(TypeDescriptor.EXECUTABLE_ANY.isAssignable(instantiable));
Assert.assertFalse(TypeDescriptor.INSTANTIABLE_ANY.isAssignable(executable));
}

@Test
public void testForValue() {
try (Context ctx = Context.newBuilder().allowHostAccess(HostAccess.ALL).build()) {
Value v = ctx.asValue(1);
Assert.assertTrue(TypeDescriptor.NUMBER.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(true);
Assert.assertTrue(TypeDescriptor.BOOLEAN.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue("a");
Assert.assertTrue(TypeDescriptor.STRING.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(null);
Assert.assertTrue(TypeDescriptor.NULL.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(ProxyObject.fromMap(Collections.singletonMap("key", "value")));
Assert.assertTrue(TypeDescriptor.OBJECT.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(new Function<Object, Object>() {
@Override
public Object apply(Object t) {
return null;
}
});
Assert.assertTrue(TypeDescriptor.EXECUTABLE.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(Object.class);
Assert.assertTrue(TypeDescriptor.INSTANTIABLE.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(new Object());
Assert.assertTrue(TypeDescriptor.HOST_OBJECT.isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(ProxyArray.fromArray(1));
Assert.assertTrue(TypeDescriptor.array(TypeDescriptor.NUMBER).isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(ProxyArray.fromArray(true));
Assert.assertTrue(TypeDescriptor.array(TypeDescriptor.BOOLEAN).isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(ProxyArray.fromArray(1, true, "value"));
Assert.assertTrue(TypeDescriptor.array(TypeDescriptor.union(TypeDescriptor.NUMBER, TypeDescriptor.BOOLEAN, TypeDescriptor.STRING)).isAssignable(TypeDescriptor.forValue(v)));
v = ctx.asValue(ProxyArray.fromArray());
Assert.assertTrue(TypeDescriptor.array(TypeDescriptor.intersection(TypeDescriptor.NULL, TypeDescriptor.BOOLEAN, TypeDescriptor.NUMBER, TypeDescriptor.STRING, TypeDescriptor.HOST_OBJECT,
TypeDescriptor.NATIVE_POINTER, TypeDescriptor.OBJECT, TypeDescriptor.ARRAY, TypeDescriptor.EXECUTABLE, TypeDescriptor.INSTANTIABLE)).isAssignable(
TypeDescriptor.forValue(v)));
}
}
}

0 comments on commit f4a99ac

Please sign in to comment.