Skip to content

Commit

Permalink
add test for persistance of type metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed May 27, 2024
1 parent 1270940 commit 9a2fcc3
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.compiler.pass.analyse.types;

import org.enso.persist.Persistable;
import org.enso.pkg.QualifiedName;

@Persistable(clazz = InferredType.class, id = 34000)
@Persistable(clazz = TypeRepresentation.TopType.class, id = 34001)
Expand All @@ -11,6 +12,7 @@
@Persistable(clazz = TypeRepresentation.SumType.class, id = 34006)
@Persistable(clazz = TypeRepresentation.UnresolvedSymbol.class, id = 34007)
@Persistable(clazz = AtomTypeInterfaceFromBindingsMap.class, id = 34010)
@Persistable(clazz = QualifiedName.class, id = 34012)
public final class TypeInferencePersistance {
private TypeInferencePersistance() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import scala.Tuple2;
import scala.collection.immutable.List;
import scala.collection.immutable.Seq;
import scala.jdk.javaapi.CollectionConverters$;

@Persistable(clazz = Module.class, id = 201)
@Persistable(clazz = Name.Literal.class, id = 351)
Expand Down Expand Up @@ -240,7 +239,7 @@ protected List readObject(Input in) throws IOException, ClassNotFoundException {
@ServiceProvider(service = Persistance.class)
public static final class PersistJavaListLazy extends Persistance<java.util.List> {
public PersistJavaListLazy() {
super(java.util.List.class, true, 34011);
super(java.util.List.class, true, 4434);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ public void lazyJavaList() throws Exception {
assertNotSame("Lazily deserialized s2", s2, out.get(1));
}

/** The Scala sequence is not lazy because it is actually written (and thus also read) as a List. */
/**
* The Scala sequence is not lazy because it is actually written (and thus also read) as a List.
*/
@Test
public void notLazyScalaSequence() throws Exception {
var s1 = new LazyString("Hello");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.enso.pkg.SourceFile;
import org.openide.util.lookup.ServiceProvider;

@Persistable(clazz = QualifiedName.class, id = 30300)
public final class ImportExportCache
implements Cache.Spi<ImportExportCache.CachedBindings, ImportExportCache.Metadata> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.enso.runtime.test;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.List;
import org.enso.compiler.data.BindingsMap;
import org.enso.compiler.pass.analyse.types.AtomTypeInterfaceFromBindingsMap;
import org.enso.compiler.pass.analyse.types.InferredType;
import org.enso.compiler.pass.analyse.types.TypeRepresentation;
import org.enso.persist.Persistance;
import org.enso.pkg.QualifiedName;
import org.junit.Test;
import scala.jdk.javaapi.CollectionConverters$;

/**
* Currently the static type inference pass is optional and it is not computed as part of the cache
* indexing.
*/
public class TypeMetadataPersistanceTest {
private static <T> scala.collection.immutable.List<T> makeScalaList(List<T> list) {
return CollectionConverters$.MODULE$.asScala(list).toList();
}

private static <T> T serde(Class<T> clazz, T l) throws IOException {
var arr = Persistance.write(l, null);
var ref = Persistance.read(arr, null);
return ref.get(clazz);
}

@Test
public void writeSomeInferredType() throws Exception {
var obj = mockObject();
var type =
new TypeRepresentation.ArrowType(
obj,
new TypeRepresentation.SumType(List.of(TypeRepresentation.ANY, obj.instanceType())));
var inferredType = new InferredType(type);
var out = serde(InferredType.class, inferredType);
assertEquals(inferredType.type(), out.type());
}

private TypeRepresentation.TypeObject mockObject() {
var fqn = new QualifiedName(makeScalaList(List.of("mod")), "Test");
return new TypeRepresentation.TypeObject(fqn, mockAtomType());
}

private AtomTypeInterfaceFromBindingsMap mockAtomType() {
scala.collection.immutable.List<String> params = makeScalaList(List.of());
var ctorArgs =
makeScalaList(
List.of(new BindingsMap.Argument("arg", false, Persistance.Reference.none())));
var constructors = makeScalaList(List.of(new BindingsMap.Cons("ctor", ctorArgs, false)));
return new AtomTypeInterfaceFromBindingsMap(
new BindingsMap.Type("Test", params, constructors, false));
}
}

0 comments on commit 9a2fcc3

Please sign in to comment.