forked from enso-org/enso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Giving TypeOfNode richer query API (enso-org#11618)
- Loading branch information
1 parent
d611939
commit 7c5e692
Showing
22 changed files
with
366 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...test/java/org/enso/interpreter/node/expression/builtin/meta/TypeOfNodeMultiValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package org.enso.interpreter.node.expression.builtin.meta; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.oracle.truffle.api.RootCallTarget; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import org.enso.interpreter.runtime.data.EnsoMultiValue; | ||
import org.enso.interpreter.runtime.data.Type; | ||
import org.enso.interpreter.runtime.error.DataflowError; | ||
import org.enso.interpreter.runtime.library.dispatch.TypeOfNode; | ||
import org.enso.interpreter.test.ValuesGenerator; | ||
import org.enso.test.utils.ContextUtils; | ||
import org.enso.test.utils.TestRootNode; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Value; | ||
import org.junit.AfterClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
|
||
@RunWith(Parameterized.class) | ||
public class TypeOfNodeMultiValueTest { | ||
|
||
private static RootCallTarget testTypesCall; | ||
|
||
@Parameterized.Parameter(0) | ||
public Object value; | ||
|
||
@Parameterized.Parameter(1) | ||
public String type; | ||
|
||
@Parameterized.Parameter(2) | ||
public int typeIndex; | ||
|
||
private static Context ctx; | ||
|
||
private static Context ctx() { | ||
if (ctx == null) { | ||
ctx = ContextUtils.defaultContextBuilder().build(); | ||
ContextUtils.executeInContext( | ||
ctx, | ||
() -> { | ||
var node = TypeOfNode.create(); | ||
var root = | ||
new TestRootNode( | ||
(frame) -> { | ||
var arg = frame.getArguments()[0]; | ||
var t = node.findTypeOrError(arg); | ||
var all = node.findAllTypesOrNull(arg); | ||
return new Object[] {t, all}; | ||
}); | ||
root.insertChildren(node); | ||
testTypesCall = root.getCallTarget(); | ||
return null; | ||
}); | ||
} | ||
assertNotNull("Test types call initialized", testTypesCall); | ||
return ctx; | ||
} | ||
|
||
@Parameterized.Parameters | ||
public static Object[][] allPossibleEnsoInterpreterValues() throws Exception { | ||
var g = ValuesGenerator.create(ctx()); | ||
var typeOf = | ||
ContextUtils.evalModule( | ||
ctx(), | ||
""" | ||
from Standard.Base import all | ||
typ obj = Meta.type_of obj | ||
main = typ | ||
"""); | ||
var data = new ArrayList<Object[]>(); | ||
for (var polyValue : g.allValues()) { | ||
registerValue(g, typeOf, polyValue, data); | ||
} | ||
return data.toArray(new Object[0][]); | ||
} | ||
|
||
private static void registerValue( | ||
ValuesGenerator g, Value typeOf, Value polyValue, ArrayList<Object[]> data) { | ||
var t = typeOf.execute(polyValue); | ||
if (!polyValue.isNull()) { | ||
assertTrue("Type of " + polyValue + " is " + t, t.isMetaObject()); | ||
var rawValue = ContextUtils.unwrapValue(ctx(), polyValue); | ||
var rawType = ContextUtils.unwrapValue(ctx(), t); | ||
if (rawType instanceof Type type) { | ||
var singleMultiValue = EnsoMultiValue.create(new Type[] {type}, new Object[] {rawValue}); | ||
var n = t.getMetaSimpleName(); | ||
data.add(new Object[] {singleMultiValue, n, 0}); | ||
var rawInt = (Type) ContextUtils.unwrapValue(ctx(), g.typeInteger()); | ||
var secondMultiValue = | ||
EnsoMultiValue.create(new Type[] {rawInt, type}, new Object[] {5L, rawValue}); | ||
data.add(new Object[] {secondMultiValue, n, 1}); | ||
var firstMultiValue = | ||
EnsoMultiValue.create(new Type[] {type, rawInt}, new Object[] {rawValue, 6L}); | ||
data.add(new Object[] {firstMultiValue, n, 0}); | ||
} else { | ||
if (!t.isHostObject()) { | ||
data.add(new Object[] {rawValue, null, -1}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void disposeCtx() throws Exception { | ||
if (ctx != null) { | ||
ctx.close(); | ||
ctx = null; | ||
} | ||
} | ||
|
||
@Test | ||
public void typeOfCheck() { | ||
assertType(value, type, typeIndex); | ||
} | ||
|
||
private static void assertType(Object value, String expectedTypeName, int typeIndex) { | ||
assertNotNull("Value " + value + " should have a type", expectedTypeName); | ||
ContextUtils.executeInContext( | ||
ctx(), | ||
() -> { | ||
var pairResult = (Object[]) testTypesCall.call(value); | ||
var t = pairResult[0]; | ||
var all = (Object[]) pairResult[1]; | ||
|
||
Object symbolType; | ||
if (t instanceof DataflowError) { | ||
assertNull("No types for errors", all); | ||
symbolType = t; | ||
} else { | ||
assertNotNull("All types found for " + value, all); | ||
assertTrue( | ||
"Size is at least " + typeIndex + " but was: " + Arrays.toString(all), | ||
all.length >= typeIndex); | ||
assertEquals("Major type is the same with first of allTypes for" + value, t, all[0]); | ||
symbolType = all[typeIndex]; | ||
} | ||
|
||
var symbolTypeValue = ctx.asValue(symbolType); | ||
assertTrue("It is meta object: " + symbolTypeValue, symbolTypeValue.isMetaObject()); | ||
assertEquals(expectedTypeName, symbolTypeValue.getMetaSimpleName()); | ||
return null; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.