aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shid/ShidCodeGenWhenTests.java
blob: c13b0e13d0a62f6cd85a7ce5d2c47de0d376d87d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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")
                        ))
                    )
                )
            )
        ));
    }
}