From 93dd911a92929ff98fc384b243ab268ce2d033eb Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Oct 2023 11:11:40 +0100 Subject: Assign tags to structs --- .../patio/PatioCodeGenDefineStructTests.java | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java index 6464dfe0..932f626a 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioCodeGenDefineStructTests.java @@ -16,6 +16,8 @@ import java.util.List; import java.util.Map; import static org.shedlang.compiler.patio.PatioTesting.serialize; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; @ExtendWith({SnapshotResolver.class}) public class PatioCodeGenDefineStructTests { @@ -40,4 +42,25 @@ public class PatioCodeGenDefineStructTests { snapshotter.assertSnapshot(serialize(result)); } + + @Test + public void structsAreAssignedSequentialTags(Snapshotter snapshotter) throws IOException { + // TODO: we can probably optimise this to ensure that (for instance) + // variants of the same sum type have sequential tags, but source order + // will suffice for now + var structType1 = Types.struct(NamespaceName.of("Example", "Project"), "User"); + var structType2 = Types.struct(NamespaceName.of("Example", "Project"), "Id"); + var instruction1 = Instructions.defineStruct(structType1, List.of()); + var instruction2 = Instructions.defineStruct(structType1, List.of()); + var typeInfos = TypeInfos.EMPTY + .addStructTypeInfo(structType1, StructTypeInfo.fromFields(P.orderedMap())) + .addStructTypeInfo(structType2, StructTypeInfo.fromFields(P.orderedMap())); + var context = PatioCodeGenContext.forTypeInfos(typeInfos); + + context = PatioCodeGen.generateInstruction(instruction1, context); + context = PatioCodeGen.generateInstruction(instruction2, context); + + assertThat(context.tag(structType1), equalTo(0)); + assertThat(context.tag(structType2), equalTo(1)); + } } -- cgit v1.2.3