aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shidtopatio/ShidToPatioNamespaceTests.java
blob: 67a97d96079edd3640780695400e6c986fbffc5c (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
package org.shedlang.compiler.shidtopatio;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.shedlang.compiler.ast.typed.FreeVariables;
import org.shedlang.compiler.shid.ShidNamespace;
import org.shedlang.compiler.shid.instructions.Shid;
import org.shedlang.compiler.shid.instructions.ShidDefineFunction;
import org.shedlang.compiler.sources.Sources;
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.NamespaceTypeInfo;
import org.shedlang.compiler.types.Types;

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

import static org.shedlang.compiler.patio.PatioTesting.serializeCompilationUnit;

@ExtendWith({SnapshotResolver.class})
public class ShidToPatioNamespaceTests {
    @Test
    public void canDefineNamespace(Snapshotter snapshotter) throws IOException {
        var instruction = new ShidDefineFunction(
            "f",
            List.of(),
            List.of(),
            FreeVariables.empty(),
            Shid.block(List.of(
                Shid.pushUnit(),
                Shid.return_()
            )),
            ShidDefineFunction.Variant.FUNCTION,
            Sources.NULL
        );
        var instructions = Shid.block(List.of(instruction, Shid.discard()));
        var namespaceName = NamespaceName.of("An","Example");
        var typeInfos = TypeInfos.EMPTY
            .addNamespaceTypeInfo(
                namespaceName,
                NamespaceTypeInfo.builder()
                    .addField("x", Types.INT)
                    .addField("y", Types.STRING)
                    .build()
            );
        var context = ShidToPatioTesting.createContext(typeInfos);
        var shidNamespace = new ShidNamespace(namespaceName, instructions, Sources.NULL);

        var result = ShidToPatio.generateNamespace(shidNamespace, context);

        snapshotter.assertSnapshot(serializeCompilationUnit(result.value()));
    }

}