From e1c58cd188df9ba3c5db1ea43e9ec922cc38e2b7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 14 Jul 2023 21:14:47 +0100 Subject: Check that bodies of if are type checked --- .../compiler/ast/typed/TypedNodeMatchers.java | 23 ++++++++++++-- .../compiler/typechecker/TypeCheckerIfTests.java | 35 ++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java index fa7fe7ba..8ce4b168 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java @@ -1,7 +1,5 @@ package org.shedlang.compiler.ast.typed; -import org.shedlang.compiler.ast.untyped.UntypedBlockNode; -import org.shedlang.compiler.ast.untyped.UntypedFunctionStatementNode; import org.shedlang.compiler.types.Type; import org.shedlang.compiler.types.TypeLevelValue; import org.zwobble.precisely.Matcher; @@ -57,6 +55,16 @@ public class TypedNodeMatchers { return TypedCallNodeMatcher.empty(); } + public static Matcher isTypedConditionalBranchNode( + Matcher condition, + Matcher body + ) { + return allOf( + has("condition", x -> x.condition(), condition), + has("body", x -> x.body(), body) + ); + } + public static Matcher isTypedExpressionStatementNode( Matcher expression ) { @@ -79,6 +87,17 @@ public class TypedNodeMatchers { ); } + public static Matcher isTypedIfNode( + Matcher> conditionalBranches, + Matcher elseBody + ) { + return instanceOf( + TypedIfNode.class, + has("conditionalBranches", x -> x.conditionalBranches(), conditionalBranches), + has("elseBody", x -> x.elseBody(), elseBody) + ); + } + public static Matcher isTypedIntAddNode( Matcher left, Matcher right diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.java index 47a6cc22..8ec1848e 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerIfTests.java @@ -9,10 +9,45 @@ import org.shedlang.compiler.types.Types; import java.util.List; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.isSequence; public class TypeCheckerIfTests { + @Test + public void bodiesAreTypedChecked() { + var untyped = Untyped.if_( + List.of( + Untyped.conditionalBranch( + Untyped.boolLiteral(true), + Untyped.block(List.of( + Untyped.blockReturn(Untyped.stringLiteral("a")) + )) + ) + ), + Untyped.block(List.of( + Untyped.blockReturn(Untyped.stringLiteral("b")) + )) + ); + + var result = TypeChecker.typeCheckExpression(untyped, TypeCheckerContext.EMPTY); + + assertThat(result.value(), isTypedIfNode( + isSequence( + isTypedConditionalBranchNode( + isTypedBoolLiteralNode(true), + isTypedBlockNode(isSequence( + isTypedBlockReturnNode(isTypedStringLiteralNode("a")) + )) + ) + ), + isTypedBlockNode(isSequence( + isTypedBlockReturnNode(isTypedStringLiteralNode("b")) + )) + )); + } + @Test public void canTypeCheckIfWithAllBranchesGivingTheSameType() { var untyped = Untyped.if_( -- cgit v1.2.3