1+ package org .enso .interpreter .node .expression .builtin .meta ;
2+ import static org .hamcrest .MatcherAssert .assertThat ;
3+ import static org .hamcrest .Matchers .is ;
4+ import static org .junit .Assert .assertArrayEquals ;
5+ import static org .junit .Assert .assertEquals ;
6+ import static org .junit .Assert .assertTrue ;
7+ import org .enso .interpreter .runtime .data .Type ;
8+ import org .enso .test .utils .ContextUtils ;
9+ import org .graalvm .polyglot .Value ;
10+ import org .junit .BeforeClass ;
11+ import org .junit .ClassRule ;
12+ import org .junit .Test ;
13+
14+ public class TypeChainTest {
15+ @ ClassRule public static final ContextUtils ctx = ContextUtils .createDefault ();
16+ private static Value typeOf ;
17+ private static Value normalType ;
18+ private static Value singletonType ;
19+ @ BeforeClass
20+ public static void initTypeOf () {
21+ typeOf =
22+ ctx .evalModule (
23+ """
24+ import Standard.Base.Meta
25+ main = Meta.type_of
26+ """ );
27+ normalType =
28+ ctx .evalModule (
29+ """
30+ type Normal_Type
31+ Cons a
32+ main = Normal_Type
33+ """ );
34+ singletonType =
35+ ctx .evalModule (
36+ """
37+ type Singleton_Type
38+ main = Singleton_Type
39+ """ );
40+ }
41+ /** {@code allTypes(Text) == [Text, Any]} */
42+ @ Test
43+ public void textChain () {
44+ var type = typeOf .execute ("Hello World!" );
45+ var raw = (Type ) ctx .unwrapValue (type );
46+ var all = raw .allTypes (ctx .ensoContext ());
47+ var exp1 = ctx .ensoContext ().getBuiltins ().text ();
48+ var exp2 = ctx .ensoContext ().getBuiltins ().any ();
49+ assertArrayEquals ("allTypes(Text) == [Text, Any]" , new Object [] {exp1 , exp2 }, all );
50+ }
51+ /** {@code allTypes(Text.type) == [Text.type, Any]} */
52+ @ Test
53+ public void textTypeChain () {
54+ var textType = typeOf .execute ("Ciao" );
55+ var textTypeType = typeOf .execute (textType );
56+ var raw = (Type ) ctx .unwrapValue (textTypeType );
57+ var all = raw .allTypes (ctx .ensoContext ());
58+ var exp1 = ctx .ensoContext ().getBuiltins ().text ().getEigentype ();
59+ var exp2 = ctx .ensoContext ().getBuiltins ().any ();
60+ assertArrayEquals ("allTypes(Text.type) == [Text.type, Any]" , new Object [] {exp1 , exp2 }, all );
61+ }
62+ /** {@code typeof(Text.type) == Text.type} */
63+ @ Test
64+ public void textEigeintypeChain () {
65+ var textType = typeOf .execute ("Ahoj" );
66+ var textTypeType = typeOf .execute (textType );
67+ var loop = typeOf .execute (textTypeType );
68+ assertEquals ("Eigentype is the last type - then we loop" , textTypeType , loop );
69+ }
70+ @ Test
71+ public void textModuleChain () {
72+ var code =
73+ """
74+ import Standard.Base.Data.Text
75+ main = Text
76+ """ ;
77+ var textModule = ctx .evalModule (code );
78+ assertEquals ("Standard.Base.Data.Text" , textModule .getMetaQualifiedName ());
79+ var rawType = (Type ) ctx .unwrapValue (textModule );
80+ var module = rawType .getDefinitionScope ().getModule ();
81+ var associatedType = rawType .getDefinitionScope ().getAssociatedType ();
82+ assertEquals ("Module's type is its associated type" , rawType , associatedType );
83+ assertTrue ("Module associated type is eigentype" , rawType .isEigenType ());
84+ var exp1 = module .getScope ().getAssociatedType ();
85+ var exp2 = ctx .ensoContext ().getBuiltins ().any ();
86+ assertArrayEquals (
87+ "Text.type and Any" , new Object [] {exp1 , exp2 }, rawType .allTypes (ctx .ensoContext ()));
88+ }
89+ /** {@code allTypes(Integer) == [Integer, Number, Any]} */
90+ @ Test
91+ public void integerChain () {
92+ var numberType = ctx .ensoContext ().getBuiltins ().number ().getNumber ();
93+ var integerType = ctx .ensoContext ().getBuiltins ().number ().getInteger ();
94+ var anyType = ctx .ensoContext ().getBuiltins ().any ();
95+ var allTypes = integerType .allTypes (ctx .ensoContext ());
96+ assertArrayEquals (
97+ "allTypes(Integer) == [Integer, Number, Any]" ,
98+ new Object [] {integerType , numberType , anyType },
99+ allTypes );
100+ }
101+ /** {@code allTypes(Any) == [Any]} */
102+ @ Test
103+ public void anyChain () {
104+ var any = ctx .ensoContext ().getBuiltins ().any ();
105+ var all = any .allTypes (ctx .ensoContext ());
106+ assertArrayEquals ("allTypes(Any) == [Any]" , new Object [] {any }, all );
107+ }
108+ /** {@code allTypes(Any.type) == [Any.type, Any]} */
109+ @ Test
110+ public void anyEigentypeChain () {
111+ var any = ctx .ensoContext ().getBuiltins ().any ();
112+ var anyType = typeOf .execute (any );
113+ assertEquals ("Any.type" , anyType .toString ());
114+ var anyTypeType = typeOf .execute (anyType );
115+ assertEquals ("Type of Any.type is again Any.type" , anyType , anyTypeType );
116+ var raw = (Type ) ctx .unwrapValue (anyTypeType );
117+ var all = raw .allTypes (ctx .ensoContext ());
118+ var anyEigenType = any .getEigentype ();
119+ var anyTypeExpected = ctx .ensoContext ().getBuiltins ().any ();
120+ assertArrayEquals (
121+ "allTypes(Any.type) == [Any.type, Any]" , new Object [] {anyEigenType , anyTypeExpected }, all );
122+ }
123+ /** {@code allTypes(Normal_Type) == [Normal_Type, Any]} */
124+ @ Test
125+ public void normalTypeChain () {
126+ var raw = (Type ) ctx .unwrapValue (normalType );
127+ assertThat ("Is not eigen type" , raw .isEigenType (), is (false ));
128+ var all = raw .allTypes (ctx .ensoContext ());
129+ var exp1 = raw ;
130+ var exp2 = ctx .ensoContext ().getBuiltins ().any ();
131+ assertArrayEquals (
132+ "allTypes(Normal_Type) == [Normal_Type, Any]" , new Object [] {exp1 , exp2 }, all );
133+ }
134+ @ Test
135+ public void singletonTypeChain () {
136+ var raw = (Type ) ctx .unwrapValue (singletonType );
137+ assertThat ("Is eigen type" , raw .isEigenType (), is (true ));
138+ var all = raw .allTypes (ctx .ensoContext ());
139+ var exp1 = raw ;
140+ var exp2 = ctx .ensoContext ().getBuiltins ().any ();
141+ assertArrayEquals (
142+ "allTypes(Singleton_Type.type) == [Singleton_Type.type, Any]" ,
143+ new Object [] {exp1 , exp2 },
144+ all );
145+ }
146+ }
0 commit comments