diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-01-11 11:30:27 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-01-11 11:30:27 +0000 |
| commit | 470b1fab1f9d99d6c85f479e28ea1c424e671e0a (patch) | |
| tree | 45c3b520d5dd2b38e3a2671e55068957808763c0 /src/test/java | |
| parent | acb019f53eefaee7dd7ab6e808cab0a0daa0b91f (diff) | |
Create separate config file for examples
Diffstat (limited to 'src/test/java')
5 files changed, 162 insertions, 70 deletions
diff --git a/src/test/java/org/shedlang/compiler/backends/ExampleConfig.java b/src/test/java/org/shedlang/compiler/backends/ExampleConfig.java new file mode 100644 index 00000000..e8e63f40 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/ExampleConfig.java @@ -0,0 +1,26 @@ +package org.shedlang.compiler.backends; + +import org.shedlang.compiler.config.json.Json5ObjectWithSource; +import org.zwobble.json5.values.Json5String; + +import java.nio.file.Path; +import java.util.Optional; + +public record ExampleConfig( + Path projectPath, + String bin, + Optional<String> expectedOutput, + Optional<String> expectedError +) { + public static ExampleConfig fromJson5( + Path root, + Json5ObjectWithSource object + ) { + return new ExampleConfig( + root.resolve(object.getString("project").value()), + object.getString("bin").value(), + object.getStringOrNone("expectedOutput").map(Json5String::value), + object.getStringOrNone("expectedError").map(Json5String::value) + ); + } +} diff --git a/src/test/java/org/shedlang/compiler/backends/ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/ExampleTests.java index be9329d6..d8607d47 100644 --- a/src/test/java/org/shedlang/compiler/backends/ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/ExampleTests.java @@ -6,7 +6,6 @@ import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; import org.shedlang.compiler.ShedCompiler; import org.shedlang.compiler.config.ProfileConfig; -import org.shedlang.compiler.config.ProjectConfig; import org.shedlang.compiler.errors.CompilerError; import org.shedlang.compiler.filesystem.ShedCompilerFiles; import org.shedlang.compiler.testing.ExecutionResult; @@ -43,10 +42,10 @@ public abstract class ExampleTests { var backend = backend(); var compiler = new ShedCompiler(); - var config = ProjectConfig.read(exampleDirectory.toPath()); + var config = ExamplesConfig.read(exampleDirectory.toPath()); for (var example : config.examples()) { try { - compiler.compile(exampleDirectory.toPath(), backend, Optional.empty()); + compiler.compile(example.projectPath(), backend, Optional.empty()); } catch (CompilerError error) { if (example.expectedError().isPresent()) { assertThat(error.getMessage(), equalTo(example.expectedError().get())); @@ -58,7 +57,8 @@ public abstract class ExampleTests { } // TODO: remove duplicated generation of target binary path - var binPath = config.targetDir() + var binPath = example.projectPath() + .resolve("target") .resolve(backend.targetName()) .resolve(ProfileConfig.DEFAULT.name()) .resolve(example.bin()) diff --git a/src/test/java/org/shedlang/compiler/backends/ExamplesConfig.java b/src/test/java/org/shedlang/compiler/backends/ExamplesConfig.java new file mode 100644 index 00000000..c994d4d4 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/ExamplesConfig.java @@ -0,0 +1,44 @@ +package org.shedlang.compiler.backends; + +import org.shedlang.compiler.config.json.Json5ObjectWithSource; +import org.zwobble.sourcetext.SourceText; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public record ExamplesConfig( + List<ExampleConfig> examples +) { + public static ExamplesConfig read(Path projectPath) throws IOException { + var configPath = projectPath.resolve("shed-examples.json5"); + var contents = Files.readString(configPath); + var sourceText = SourceText.fromString(configPath.toString(), contents); + return fromSourceText(projectPath, sourceText); + } + + public static ExamplesConfig fromSourceText(Path projectPath, SourceText sourceText) { + return fromJson5( + projectPath, + Json5ObjectWithSource.parse(sourceText) + ); + } + + public static ExamplesConfig fromJson5(Path root, Json5ObjectWithSource object) { + return new ExamplesConfig( + examplesFromToml(root, object) + ); + } + + public static List<ExampleConfig> examplesFromToml( + Path root, + Json5ObjectWithSource object + ) { + return object.getArrayOfObjects("examples") + .orElseGet(() -> List.of()) + .stream() + .map(example -> ExampleConfig.fromJson5(root, example)) + .toList(); + } +} diff --git a/src/test/java/org/shedlang/compiler/backends/ExamplesConfigTests.java b/src/test/java/org/shedlang/compiler/backends/ExamplesConfigTests.java new file mode 100644 index 00000000..5c6b9b92 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/backends/ExamplesConfigTests.java @@ -0,0 +1,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); + } +} diff --git a/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java b/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java index e7059c34..53951222 100644 --- a/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java +++ b/src/test/java/org/shedlang/compiler/config/ProjectConfigTests.java @@ -120,72 +120,6 @@ public class ProjectConfigTests { } @Test - public void canParseProjectConfigWithNoExamples() { - var config = parseProjectConfig("{}"); - - assertThat(config.examples(), isSequence()); - } - - @Test - public void canParseProjectConfigWithExamples() { - var config = parseProjectConfig(""" - { - examples: [ - {bin: "one"}, - {bin: "two"}, - ], - } - """); - - assertThat(config.examples(), isSequence( - instanceOf( - ExampleConfig.class, - has("bin", x -> x.bin(), equalTo("one")) - ), - instanceOf( - ExampleConfig.class, - has("bin", x -> x.bin(), equalTo("two")) - ) - )); - } - - @Test - public void whenProjectConfigHasExamplesThatArentArrayThenErrorIsThrown() { - var error = assertThrows( - ConfigError.class, - () -> parseProjectConfig(""" - {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 whenProjectConfigHasExampleThatIsntObjectThenErrorIsThrown() { - var error = assertThrows( - ConfigError.class, - () -> parseProjectConfig(""" - { - 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], - ^""" - )); - } - - @Test public void canParseProjectConfigWithNoProfiles() { var config = parseProjectConfig("{}"); |
