package org.shedlang.compiler.shidtopatio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.shedlang.compiler.shid.instructions.Shid; import org.shedlang.compiler.testing.snapshots.SnapshotResolver; import org.shedlang.compiler.testing.snapshots.Snapshotter; import org.shedlang.compiler.typechecker.TypeInfos; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.StructTypeInfo; import org.shedlang.compiler.types.Types; import java.io.IOException; import java.util.List; import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; @ExtendWith({SnapshotResolver.class}) public class ShidToPatioDefineStructTests { @Test public void canGenerateCodeForDefineStructNonPositional(Snapshotter snapshotter) throws IOException { var structType = Types.struct(NamespaceName.of("Example", "Project"), "User"); var instruction = Shid.defineStruct( structType, List.of( Shid.defineStructField("name"), Shid.defineStructField("isActive") ) ); var typeInfos = TypeInfos.EMPTY .addStructTypeInfo( structType, StructTypeInfo.builder() .addField("name", Types.STRING) .addField("isActive", Types.BOOL) .withFieldsArePositional(false) .build() ); var context = ShidToPatioTesting.createContext(typeInfos); var result = ShidToPatio.generateInstruction(instruction, context); snapshotter.assertSnapshot(serialize(result)); } @Test public void canGenerateCodeForDefineStructPositional(Snapshotter snapshotter) throws IOException { var structType = Types.struct(NamespaceName.of("Example", "Project"), "User"); var instruction = Shid.defineStruct( structType, List.of( Shid.defineStructField("name"), Shid.defineStructField("isActive") ) ); var typeInfos = TypeInfos.EMPTY .addStructTypeInfo( structType, StructTypeInfo.builder() .addField("name", Types.STRING) .addField("isActive", Types.BOOL) .withFieldsArePositional(true) .build() ); var context = ShidToPatioTesting.createContext(typeInfos); var result = ShidToPatio.generateInstruction(instruction, context); 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 = Shid.defineStruct(structType1, List.of()); var instruction2 = Shid.defineStruct(structType1, List.of()); var typeInfos = TypeInfos.EMPTY .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); var context = ShidToPatioTesting.createContext(typeInfos); context = ShidToPatio.generateInstruction(instruction1, context); context = ShidToPatio.generateInstruction(instruction2, context); assertThat(context.structTag(structType1), equalTo(3)); assertThat(context.structTag(structType2), equalTo(4)); } }