package org.shedlang.compiler.shid; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; import org.shedlang.compiler.ast.typed.TypedTryHandlerMethodNode; import org.shedlang.compiler.ast.typed.TypedTryNode; import org.shedlang.compiler.shid.instructions.ShidDefineFunction; import org.shedlang.compiler.shid.instructions.ShidTry; import org.shedlang.compiler.types.Effects; import org.shedlang.compiler.types.NamespaceName; import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidBlock; import static org.shedlang.compiler.shid.instructions.ShidMatchers.isShidPushInt; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; public class ShidCodeGenTryTests { @Test public void tryNodeIsCompiledToTryInstruction() { var effect = Effects.userDefined(NamespaceName.of("Example"), "State"); var node = TypedTryNode.builder() .addBodyStatement( Typed.blockReturn(Typed.intLiteral(42)) ) .withHandledEffect(Typed.typeLevelReference("State", effect)) .addHandlerMethod( TypedTryHandlerMethodNode.builder() .withName("read") .build() ) .addHandlerMethod( TypedTryHandlerMethodNode.builder() .withName("write") .build() ) .build(); var context = ShidCodeGen.Context.EMPTY .addShedVal("c"); var result = ShidCodeGen.compileExpression(node, context); assertThat(result.instructions(), isSequence( instanceOf( ShidDefineFunction.class, has("name", x -> x.name(), equalTo("read")) ), instanceOf( ShidDefineFunction.class, has("name", x -> x.name(), equalTo("write")) ), instanceOf( ShidTry.class, has("handledEffect", x -> x.handledEffect(), equalTo(effect)), has("handlerMethodNames", x -> x.handlerMethodNames(), isSequence( equalTo("read"), equalTo("write") )), has("body", x -> x.body(), isShidBlock(isSequence( isShidPushInt(42) ))) ) )); } }