diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-11-26 11:22:04 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-11-26 11:22:04 +0000 |
| commit | 3ad6a4fa0769d254e7cd6aa2c4e11a0629e6db64 (patch) | |
| tree | 0016097db4bc1ebb0050904582be3b24a36cd816 /src/test/java | |
| parent | 33ce639ae0c9c4e4fcbe8265b381ddaeebc43ccb (diff) | |
Support try without throw
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java index f7ef017a..b0917393 100644 --- a/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java +++ b/src/test/java/org/shedlang/compiler/patio/PatioExampleTests.java @@ -1,5 +1,6 @@ package org.shedlang.compiler.patio; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.shedlang.compiler.patio.ast.PatioCompilationUnit; import org.shedlang.compiler.patio.ast.PatioFunctionDefinition; @@ -182,6 +183,108 @@ public abstract class PatioExampleTests { } @Test + public void whenExceptionIsNotThrownThenTryReturnsFromBody() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.INT) + .addLocal(Patio.val("r1", PatioTypes.INT)) + .addLocal(Patio.val("r2", PatioTypes.INT)) + .addLocal(Patio.val("e1", PatioTypes.INT)) + .addLocal(Patio.val("e2", PatioTypes.INT)) + .addBodyInstruction(Patio.try_( + List.of(Patio.localRef("r1")), + List.of( + Patio.catchClause( + "Error", + Patio.localRef("e1"), + Patio.block( + List.of(), + List.of(Patio.int_(1)) + ) + ) + ), + Patio.block( + List.of(), + List.of(Patio.int_(0)) + ) + )) + .addBodyInstruction(Patio.return_( + Patio.localRef("r1") + )) + .build(); + var patio = Patio.compilationUnit(List.of( + Patio.exceptionTypeDefinition("Error", PatioTypes.INT), + patioFunction + )); + + var result = run(patio); + + assertThat(result, isSilentExit(0)); + } + + @Test + @Disabled("WIP") + public void whenExceptionIsThrownThenFirstMatchingExceptionHandlerIsUsed() throws IOException, InterruptedException { + var patioFunction = PatioFunctionDefinition.builder() + .withName(FUNCTION_NAME) + .withReturnType(PatioTypes.INT) + .addLocal(Patio.val("r1", PatioTypes.INT)) + .addLocal(Patio.val("r2", PatioTypes.INT)) + .addLocal(Patio.val("e1", PatioTypes.INT)) + .addLocal(Patio.val("e2", PatioTypes.INT)) + .addBodyInstruction(Patio.try_( + List.of(Patio.localRef("r1")), + List.of( + Patio.catchClause( + "Error", + Patio.localRef("e1"), + Patio.block( + List.of(), + List.of(Patio.int_(1)) + ) + ) + ), + Patio.block( + List.of( + Patio.try_( + List.of(Patio.localRef("r2")), + List.of( + Patio.catchClause( + "Error", + Patio.localRef("e2"), + Patio.block( + List.of(), + List.of(Patio.int_(2)) + ) + ) + ), + Patio.block( + List.of( + Patio.throw_("Error", Patio.int_(0)) + ), + List.of(Patio.int_(0)) + ) + ) + ), + List.of(Patio.int_(0)) + ) + )) + .addBodyInstruction(Patio.return_( + Patio.localRef("r1") + )) + .build(); + var patio = Patio.compilationUnit(List.of( + Patio.exceptionTypeDefinition("Error", PatioTypes.INT), + Patio.exceptionTypeDefinition("OtherError", PatioTypes.INT), + patioFunction + )); + + var result = run(patio); + + assertThat(result, isSilentExit(47)); + } + + @Test public void vars() throws IOException, InterruptedException { var patioFunction = PatioFunctionDefinition.builder() .withName(FUNCTION_NAME) |
