diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-22 23:18:17 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-22 23:18:17 +0100 |
| commit | c87ffcbab5927c660d339d3b47b75acb50deb085 (patch) | |
| tree | d1bcebfdc82565179fd8718bea37c1dae6bca3b8 /src/test/java | |
| parent | 9c35257a360c8bea20e766b44da7eb4897a223b3 (diff) | |
Check that main function is present
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/backends/ExampleTests.java | 22 | ||||
| -rw-r--r-- | src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java | 56 |
2 files changed, 69 insertions, 9 deletions
diff --git a/src/test/java/org/shedlang/compiler/backends/ExampleTests.java b/src/test/java/org/shedlang/compiler/backends/ExampleTests.java index 037041f3..adfdf5d8 100644 --- a/src/test/java/org/shedlang/compiler/backends/ExampleTests.java +++ b/src/test/java/org/shedlang/compiler/backends/ExampleTests.java @@ -31,19 +31,23 @@ public abstract class ExampleTests { var backend = backend(); var compiler = new ShedCompiler(); - try { - compiler.compile(exampleDirectory.toPath(), backend); - } catch (CompilerError error) { - System.err.println(error.source().describe()); - throw error; - } - - var config = ProjectConfig.read(exampleDirectory.toPath()); for (var example : config.examples()) { + try { + compiler.compile(exampleDirectory.toPath(), backend); + } catch (CompilerError error) { + if (example.expectedError().isPresent()) { + assertThat(error.getMessage(), equalTo(example.expectedError().get())); + return; + } + + System.err.println(error.source().describe()); + throw error; + } + var binPath = config.targetDir().resolve(backend.name()).resolve(example.bin()).toString(); var result = runExampleBinary(binPath).throwOnError(); - assertThat(result.stdout(), equalTo(example.expectedOutput())); + assertThat(result.stdout(), equalTo(example.expectedOutput().orElseThrow())); } } )); diff --git a/src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java b/src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java new file mode 100644 index 00000000..4483c639 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/config/ConfigCheckerTests.java @@ -0,0 +1,56 @@ +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 org.shedlang.compiler.util.P; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +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.fromFields(P.orderedMap( + Map.entry( + "main", + new FunctionType(List.of(), List.of(), Effects.IO, Types.UNIT) + ) + )) + ); + + ConfigChecker.configCheckBin(binConfig, typeInfos); + } + + @Test + public void whenMainFunctionIsMissingThenBinConfigIsInvalid() throws IOException { + var binConfig = new BinConfig("x", NamespaceName.of("Main")); + var typeInfos = TypeInfos.EMPTY + .addNamespaceTypeInfo( + NamespaceName.of("Main"), + NamespaceTypeInfo.fromFields(P.orderedMap( + Map.entry( + "main2", + new FunctionType(List.of(), List.of(), Effects.IO, Types.UNIT) + ) + )) + ); + + var error = assertThrows( + ConfigError.class, + () -> ConfigChecker.configCheckBin(binConfig, typeInfos) + ); + + assertThat(error.getMessage(), equalTo("Namespace Main has no field main")); + } +} |
