aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java
blob: b3468c6e6b841cde3e92a81f560aa45434918016 (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
95
96
97
98
99
100
package org.shedlang.compiler.config;

import org.junit.jupiter.api.Test;
import org.shedlang.compiler.config.errors.ConfigError;
import org.shedlang.compiler.typechecker.TypeInfos;
import org.shedlang.compiler.types.*;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;

public class ConfigCheckerTests {
    @Test
    public void whenMainFunctionExistsWithCorrectTypeThenBinConfigIsValid() throws IOException {
        var binConfig = new BinConfig("x", NamespaceName.of("Main"));
        var typeInfos = TypeInfos.EMPTY
            .addNamespaceTypeInfo(
                NamespaceName.of("Main"),
                NamespaceTypeInfo.builder()
                    .addField(
                        "main",
                        FunctionType.builder()
                            .withEffect(Effects.IO)
                            .withReturnType(Types.UNIT)
                            .build()
                    )
                    .build()
            );

        ConfigChecker.configCheckBin(binConfig, typeInfos);
    }

    @Test
    public void whenMainNamespaceSourceIsMissingThenBinConfigIsInvalid() throws IOException {
        var binConfig = new BinConfig("x", NamespaceName.of("Main"));
        var typeInfos = TypeInfos.EMPTY;

        var error = assertThrows(
            ConfigError.class,
            () -> ConfigChecker.configCheckBin(binConfig, typeInfos)
        );

        assertThat(error.getMessage(), equalTo("Namespace Main not found"));
    }

    @Test
    public void whenMainFunctionIsMissingThenBinConfigIsInvalid() throws IOException {
        var binConfig = new BinConfig("x", NamespaceName.of("Main"));
        var typeInfos = TypeInfos.EMPTY
            .addNamespaceTypeInfo(
                NamespaceName.of("Main"),
                NamespaceTypeInfo.builder()
                    .addField(
                        "main2",
                        FunctionType.builder()
                            .withEffect(Effects.IO)
                            .withReturnType(Types.UNIT)
                            .build()
                    )
                    .build()
            );

        var error = assertThrows(
            ConfigError.class,
            () -> ConfigChecker.configCheckBin(binConfig, typeInfos)
        );

        assertThat(error.getMessage(), equalTo("Namespace Main has no field main"));
    }

    @Test
    public void whenMainFunctionHasWrongTypeThenBinConfigIsInvalid() throws IOException {
        var binConfig = new BinConfig("x", NamespaceName.of("Main"));
        var typeInfos = TypeInfos.EMPTY
            .addNamespaceTypeInfo(
                NamespaceName.of("Main"),
                NamespaceTypeInfo.builder()
                    .addField(
                        "main",
                        FunctionType.builder()
                            .withEffect(Effects.IO)
                            .withReturnType(Types.STRING)
                            .build()
                    )
                    .build()
            );

        var error = assertThrows(
            ConfigError.class,
            () -> ConfigChecker.configCheckBin(binConfig, typeInfos)
        );

        assertThat(
            error.getMessage(),
            equalTo("main function should have type fn () ! Io -> Unit, but Main.main has type fn () ! Io -> String")
        );
    }
}