package org.shedlang.compiler.shid; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; import org.shedlang.compiler.types.NamespaceName; import org.shedlang.compiler.types.Types; import java.util.List; import static org.shedlang.compiler.shid.instructions.ShidMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.isSequence; public class ShidCodeGenWhenTests { private final static NamespaceName NAMESPACE_NAME = NamespaceName.of("Example", "Project"); @Test public void whenIsCompiledToSwitchInstruction() { var node = Typed.when( Typed.reference("x"), List.of( Typed.whenCase( Types.struct(NAMESPACE_NAME, "A"), Typed.block(List.of( Typed.blockReturn(Typed.stringLiteral("a")) )) ), Typed.whenCase( Types.struct(NAMESPACE_NAME, "B"), Typed.block(List.of( Typed.blockReturn(Typed.stringLiteral("b")) )) ) ), Types.STRING ); var context = ShidCodeGen.Context.EMPTY .addShedVal("x"); var result = ShidCodeGen.compileExpression(node, context); assertThat(result.instructions(), isSequence( isShidLocalLoad("x__0"), isShidSwitch( isSequence( isShidSwitchCase( Types.struct(NAMESPACE_NAME, "A"), isShidBlock(isSequence( isShidPushString("a") )) ), isShidSwitchCase( Types.struct(NAMESPACE_NAME, "B"), isShidBlock(isSequence( isShidPushString("b") )) ) ) ) )); } }