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(""" :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(""" :2:16 examples: [1], ^""" )); } private ExamplesConfig parseExamplesConfig(String text) { var sourceText = SourceTexts.string(text); return ExamplesConfig.fromSourceText(PROJECT_PATH, sourceText); } }