|
| 1 | +package org.enso.interpreter.test.interop; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.is; |
| 5 | +import static org.hamcrest.Matchers.notNullValue; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Objects; |
| 9 | +import org.enso.interpreter.runtime.data.Type; |
| 10 | +import org.enso.interpreter.test.ValuesGenerator; |
| 11 | +import org.enso.interpreter.test.ValuesGenerator.Language; |
| 12 | +import org.enso.test.utils.ContextUtils; |
| 13 | +import org.graalvm.polyglot.Context; |
| 14 | +import org.graalvm.polyglot.Value; |
| 15 | +import org.junit.AfterClass; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.junit.runners.Parameterized; |
| 19 | +import org.junit.runners.Parameterized.Parameters; |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests that every {@link org.enso.interpreter.runtime.data.Type} exposes its constructors as |
| 23 | + * {@link com.oracle.truffle.api.interop.InteropLibrary#getMembers(Object) members}, and that such |
| 24 | + * members are {@link com.oracle.truffle.api.interop.InteropLibrary#isInstantiable(Object) |
| 25 | + * instantiable}. |
| 26 | + */ |
| 27 | +@RunWith(Parameterized.class) |
| 28 | +public class TypesExposeConstructorsTest { |
| 29 | + private static Context ctx; |
| 30 | + |
| 31 | + private final TypeWithWrapper typeWithWrapper; |
| 32 | + |
| 33 | + public TypesExposeConstructorsTest(TypeWithWrapper typeWithWrapper) { |
| 34 | + this.typeWithWrapper = typeWithWrapper; |
| 35 | + } |
| 36 | + |
| 37 | + private static Context ctx() { |
| 38 | + if (ctx == null) { |
| 39 | + ctx = ContextUtils.createDefaultContext(); |
| 40 | + } |
| 41 | + return ctx; |
| 42 | + } |
| 43 | + |
| 44 | + @AfterClass |
| 45 | + public static void disposeCtx() { |
| 46 | + if (ctx != null) { |
| 47 | + ctx.close(); |
| 48 | + ctx = null; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Parameters(name = "{index}: {0}") |
| 53 | + public static Iterable<TypeWithWrapper> collectTypes() { |
| 54 | + var collectedTypes = new ArrayList<TypeWithWrapper>(); |
| 55 | + ContextUtils.executeInContext( |
| 56 | + ctx(), |
| 57 | + () -> { |
| 58 | + var valuesGenerator = ValuesGenerator.create(ctx(), Language.ENSO); |
| 59 | + valuesGenerator.allTypes().stream() |
| 60 | + .map( |
| 61 | + tp -> { |
| 62 | + var unwrappedTp = ContextUtils.unwrapValue(ctx(), tp); |
| 63 | + if (unwrappedTp instanceof Type type) { |
| 64 | + return new TypeWithWrapper(type, tp); |
| 65 | + } else { |
| 66 | + return null; |
| 67 | + } |
| 68 | + }) |
| 69 | + .filter(Objects::nonNull) |
| 70 | + .filter(tp -> !tp.type.getConstructors().isEmpty()) |
| 71 | + .forEach(collectedTypes::add); |
| 72 | + return null; |
| 73 | + }); |
| 74 | + return collectedTypes; |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void typesExposeConstructorsAsMembers() { |
| 79 | + var type = typeWithWrapper.type; |
| 80 | + var typeValue = typeWithWrapper.typeValue; |
| 81 | + var consNames = type.getConstructors().keySet(); |
| 82 | + for (var consName : consNames) { |
| 83 | + assertThat( |
| 84 | + "Constructor " + consName + " should be exposed as a member", |
| 85 | + typeValue.hasMember(consName), |
| 86 | + is(true)); |
| 87 | + var consMember = typeValue.getMember(consName); |
| 88 | + assertThat(consMember, is(notNullValue())); |
| 89 | + assertThat( |
| 90 | + "Constructor " + consName + " should be instantiable", |
| 91 | + consMember.canInstantiate(), |
| 92 | + is(true)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param type |
| 98 | + * @param typeValue The polyglot value of the type (not an object) |
| 99 | + */ |
| 100 | + public record TypeWithWrapper(Type type, Value typeValue) {} |
| 101 | +} |
0 commit comments