aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioDefineStructTests.java
blob: f206fb09c5933b32a7ec6ee4850ad0c7ff8b0b52 (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
89
90
91
92
93
94
package org.shedlang.compiler.shidtopatio;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.TypeInfos;
import org.shedlang.compiler.types.NamespaceName;
import org.shedlang.compiler.types.StructTypeInfo;
import org.shedlang.compiler.types.Types;

import java.io.IOException;
import java.util.List;

import static org.shedlang.compiler.shidtopatio.ShidToPatioTesting.serialize;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;

@ExtendWith({SnapshotResolver.class})
public class ShidToPatioDefineStructTests {
    @Test
    public void canGenerateCodeForDefineStructNonPositional(Snapshotter snapshotter) throws IOException {
        var structType = Types.struct(NamespaceName.of("Example", "Project"), "User");
        var instruction = Shid.defineStruct(
            structType,
            List.of(
                Shid.defineStructField("name"),
                Shid.defineStructField("isActive")
            )
        );
        var typeInfos = TypeInfos.EMPTY
            .addStructTypeInfo(
                structType,
                StructTypeInfo.builder()
                    .addField("name", Types.STRING)
                    .addField("isActive", Types.BOOL)
                    .withFieldsArePositional(false)
                    .build()
            );
        var context = ShidToPatioTesting.createContext(typeInfos);

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

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void canGenerateCodeForDefineStructPositional(Snapshotter snapshotter) throws IOException {
        var structType = Types.struct(NamespaceName.of("Example", "Project"), "User");
        var instruction = Shid.defineStruct(
            structType,
            List.of(
                Shid.defineStructField("name"),
                Shid.defineStructField("isActive")
            )
        );
        var typeInfos = TypeInfos.EMPTY
            .addStructTypeInfo(
                structType,
                StructTypeInfo.builder()
                    .addField("name", Types.STRING)
                    .addField("isActive", Types.BOOL)
                    .withFieldsArePositional(true)
                    .build()
            );
        var context = ShidToPatioTesting.createContext(typeInfos);

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

        snapshotter.assertSnapshot(serialize(result));
    }

    @Test
    public void structsAreAssignedSequentialTags(Snapshotter snapshotter) throws IOException {
        // TODO: we can probably optimise this to ensure that (for instance)
        // variants of the same sum type have sequential tags, but source order
        // will suffice for now
        var structType1 = Types.struct(NamespaceName.of("Example", "Project"), "User");
        var structType2 = Types.struct(NamespaceName.of("Example", "Project"), "Id");
        var instruction1 = Shid.defineStruct(structType1, List.of());
        var instruction2 = Shid.defineStruct(structType1, List.of());
        var typeInfos = TypeInfos.EMPTY
            .addStructTypeInfo(structType1, StructTypeInfo.builder().build())
            .addStructTypeInfo(structType2, StructTypeInfo.builder().build());
        var context = ShidToPatioTesting.createContext(typeInfos);

        context = ShidToPatio.generateInstruction(instruction1, context);
        context = ShidToPatio.generateInstruction(instruction2, context);

        assertThat(context.structTag(structType1), equalTo(3));
        assertThat(context.structTag(structType2), equalTo(4));
    }
}