package org.shedlang.compiler.shidtopatio; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.shedlang.compiler.patio.operands.PatioLocalRef; 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; @ExtendWith({SnapshotResolver.class}) public class ShidToPatioSwitchTests { @Test public void canGenerateCodeForSwitchWithSingleResult(Snapshotter snapshotter) throws IOException { var namespaceName = NamespaceName.of("Example", "Project"); var structType1 = Types.struct(namespaceName, "A"); var structType2 = Types.struct(namespaceName, "B"); var instruction = Shid.switch_( List.of( Shid.switchCase( structType1, Shid.block(List.of( Shid.pushInt(1), Shid.pushInt(2), Shid.intAdd() )) ), Shid.switchCase( structType2, Shid.block(List.of( Shid.pushInt(101), Shid.pushInt(102), Shid.intAdd() )) ) ) ); var typeInfos = TypeInfos.EMPTY .addStructTypeInfo(structType1, StructTypeInfo.builder().build()) .addStructTypeInfo(structType2, StructTypeInfo.builder().build()); var context = ShidToPatioTesting.createContext(typeInfos) .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("condition")); var result = ShidToPatio.generateInstruction(instruction, context); snapshotter.assertSnapshot(serialize(result)); } }