aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioFieldLoadTests.java
blob: 10644e074223e61d72a067211eb68ff2dbbb9621 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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.typechecker.TypeInfos;
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 ShidToPatioFieldLoadTests {
    @Test
    public void canGenerateCodeForNamespaceFieldLoad(Snapshotter snapshotter) throws IOException {
        var namespaceName = new NamespaceName(List.of("Example", "Project"));
        var shidInstruction = Shid.fieldLoad(namespaceName, "y");
        var typeInfos = TypeCheckerContext.EMPTY
            .addNamespaceTypeInfo(
                namespaceName,
                NamespaceTypeInfo.builder()
                    .addField("x", Types.INT)
                    .addField("y", Types.INT)
                    .addField("z", Types.INT)
                    .build()
            )
            .toTypeInfos();
        var context = ShidToPatioTesting.createContext(typeInfos)
            .temporaryPush(new PatioLocalRef("a"))
            .temporaryPush(new PatioLocalRef("b"));

        var result = ShidToPatio.generateInstruction(shidInstruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void canGenerateCodeForStructFieldLoad(Snapshotter snapshotter) throws IOException {
        var namespaceName = NamespaceName.of("Example", "Project");
        var structType = Types.struct(namespaceName, "User");
        var shidInstruction = Shid.fieldLoad(structType, "id");
        var typeInfos = TypeInfos.EMPTY
            .addStructTypeInfo(
                structType,
                StructTypeInfo.builder()
                    .addField("name", Types.STRING)
                    .addField("id", Types.INT)
                    .addField("isActive", Types.BOOL)
                    .build()
            );
        var context = ShidToPatioTesting.createContext(typeInfos)
            .temporaryPush(new PatioLocalRef("a"))
            .temporaryPush(new PatioLocalRef("b"));

        var result = ShidToPatio.generateInstruction(shidInstruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void canGenerateCodeForUserDefinedEffectFieldLoad(Snapshotter snapshotter) throws IOException {
        var namespaceName = NamespaceName.of("Example", "Project");
        var effect = Effects.userDefined(namespaceName, "State");
        var shidInstruction = Shid.fieldLoad(effect, "fence");
        var typeInfos = TypeInfos.EMPTY
            .addUserDefinedEffectInfo(
                effect,
                UserDefinedEffectInfo.builder()
                    .addMethod("write", FunctionType.builder().build())
                    .addMethod("fence", FunctionType.builder().build())
                    .addMethod("read", FunctionType.builder().build())
                    .build()
            );
        var context = ShidToPatioTesting.createContext(typeInfos)
            .temporaryPush(new PatioLocalRef("a"))
            .temporaryPush(new PatioLocalRef("b"));

        var result = ShidToPatio.generateInstruction(shidInstruction, context);

        snapshotter.assertSnapshot(serialize(result));
    }
}