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.TypeCheckerContext; import org.shedlang.compiler.types.*; import java.io.IOException; import java.util.List; import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize; @ExtendWith({SnapshotResolver.class}) public class ShidToPatioTryTests { @Test public void canGenerateCodeForTry(Snapshotter snapshotter) throws IOException { var namespaceName = new NamespaceName(List.of("Example", "Project")); var effect = Effects.userDefined(namespaceName, "State"); var shidInstruction = Shid.try_( effect, List.of("a", "c", "b"), Shid.block(List.of( Shid.pushInt(42) )) ); var typeInfos = TypeCheckerContext.EMPTY .addUserDefinedEffectInfo( effect, UserDefinedEffectInfo.builder() .addMethod("c", FunctionType.builder().build()) .addMethod("a", FunctionType.builder().build()) .addMethod("b", FunctionType.builder().build()) .build() ) .toTypeInfos(); var context = ShidToPatioTesting.createContext(typeInfos) .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("handlerA")) .temporaryPush(new PatioLocalRef("handlerC")) .temporaryPush(new PatioLocalRef("handlerB")); var result = ShidToPatio.generateInstruction(shidInstruction, context); snapshotter.assertSnapshot(serialize(result)); } @Test public void effectHandlerIsPoppedBeforeReturn(Snapshotter snapshotter) throws IOException { var namespaceName = new NamespaceName(List.of("Example", "Project")); var effect = Effects.userDefined(namespaceName, "State"); var shidInstruction = Shid.try_( effect, List.of("a"), Shid.block(List.of( Shid.pushInt(42), Shid.return_() )) ); var typeInfos = TypeCheckerContext.EMPTY .addUserDefinedEffectInfo( effect, UserDefinedEffectInfo.builder() .addMethod("a", FunctionType.builder().build()) .build() ) .toTypeInfos(); var context = ShidToPatioTesting.createContext(typeInfos) .stashGcRootStackIndex(new PatioLocalRef("gcRootStackIndex")) .temporaryPush(new PatioLocalRef("other")) .temporaryPush(new PatioLocalRef("handlerA")); var result = ShidToPatio.generateInstruction(shidInstruction, context); snapshotter.assertSnapshot(serialize(result)); } }