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 Jun 1, 2024
1 parent 49a702d commit 514c6a9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.enso.compiler.pass.analyse.types;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.enso.persist.Persistable;
import org.enso.persist.Persistance;
import org.openide.util.lookup.ServiceProvider;
import org.enso.pkg.QualifiedName;

@Persistable(clazz = InferredType.class, id = 34000)
@Persistable(clazz = TypeRepresentation.TopType.class, id = 34001)
Expand All @@ -16,33 +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() {}

@ServiceProvider(service = Persistance.class)
public static final class PersistJavaList extends Persistance<List> {
public PersistJavaList() {
super(List.class, true, 34011);
}

@Override
protected void writeObject(List list, Output out) throws IOException {
var size = list.size();
out.writeInt(size);
for (var i = 0; i < size; i++) {
out.writeObject(list.get(i));
}
}

@Override
protected List readObject(Input in) throws IOException, ClassNotFoundException {
var size = in.readInt();
var list = new ArrayList<>(size);
for (var i = 0; i < size; i++) {
var elem = in.readObject();
list.add(elem);
}
return list;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,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 @@ -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 514c6a9

Please sign in to comment.