From 3ad6a4fa0769d254e7cd6aa2c4e11a0629e6db64 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 26 Nov 2023 11:22:04 +0000 Subject: Support try without throw --- .../shedlang/compiler/patio/PatioExampleTests.java | 103 +++++++++++++++++++++ 1 file changed, 103 insertions(+) (limited to 'src/test/java') 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; @@ -181,6 +182,108 @@ public abstract class PatioExampleTests { assertThat(result, isSilentExit(47)); } + @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() -- cgit v1.2.3