aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/backends/ExamplesConfigTests.java
blob: 5c6b9b9263330c8eb41c4f165d32a9ca8697fb9c (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.backends;

import org.junit.jupiter.api.Test;
import org.shedlang.compiler.config.errors.ConfigError;
import org.shedlang.compiler.sources.SourceTexts;

import java.nio.file.Path;

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

public class ExamplesConfigTests {
    private static final Path PROJECT_PATH = Path.of("/projects/a");

    @Test
    public void canParseExamplesConfigWithNoExamples() {
        var config = parseExamplesConfig("{}");

        assertThat(config.examples(), isSequence());
    }

    @Test
    public void canParseExamplesConfigWithExamples() {
        var config = parseExamplesConfig("""
            {
                examples: [
                    {project: ".", bin: "one"},
                    {project: "./b", bin: "two"},
                ],
            }
            """);

        assertThat(config.examples(), isSequence(
            instanceOf(
                ExampleConfig.class,
                has("projectPath", x -> x.projectPath().normalize(), equalTo(PROJECT_PATH)),
                has("bin", x -> x.bin(), equalTo("one"))
            ),
            instanceOf(
                ExampleConfig.class,
                has("projectPath", x -> x.projectPath().normalize(), equalTo(PROJECT_PATH.resolve("b"))),
                has("bin", x -> x.bin(), equalTo("two"))
            )
        ));
    }

    @Test
    public void whenExamplesConfigHasExamplesThatArentArrayThenErrorIsThrown() {
        var error = assertThrows(
            ConfigError.class,
            () -> parseExamplesConfig("""
                {examples: true}"""
            )
        );

        assertThat(error.getMessage(), equalTo("Expected $.examples to be array, but was boolean"));
        assertThat(error.source().describe(), equalTo("""
            <string>:1:12
            {examples: true}
                       ^^^^"""
        ));
    }

    @Test
    public void whenExamplesConfigHasExampleThatIsntObjectThenErrorIsThrown() {
        var error = assertThrows(
            ConfigError.class,
            () -> parseExamplesConfig("""
                {
                    examples: [1],
                }"""
            )
        );

        assertThat(error.getMessage(), equalTo("Expected $.examples[0] to be object, but was finite number"));
        assertThat(error.source().describe(), equalTo("""
            <string>:2:16
                examples: [1],
                           ^"""
        ));
    }

    private ExamplesConfig parseExamplesConfig(String text) {
        var sourceText = SourceTexts.string(text);
        return ExamplesConfig.fromSourceText(PROJECT_PATH, sourceText);
    }
}