diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-07-05 22:05:01 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-07-05 22:05:01 +0100 |
| commit | 372ab8fc6035169c7fb902a9e1df397b458d9c0c (patch) | |
| tree | 39d77ac3ce9a9a51f20d44edb1c73a3d83743e1d /src/test/java | |
| parent | 21b6a5c800dda09ba859beea02037718eda108e3 (diff) | |
Use return type in function body
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionDefinitionTests.java | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionDefinitionTests.java index a03a1f44..25b9c158 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionDefinitionTests.java @@ -4,6 +4,8 @@ import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.untyped.Untyped; import org.shedlang.compiler.ast.untyped.UntypedCallNode; import org.shedlang.compiler.ast.untyped.UntypedFunctionDefinitionNode; +import org.shedlang.compiler.typechecker.errors.UnexpectedTypeError; +import org.shedlang.compiler.typechecker.typesets.TypeSets; import org.shedlang.compiler.types.Effects; import org.shedlang.compiler.types.FunctionType; import org.shedlang.compiler.types.NamedParam; @@ -13,6 +15,7 @@ import org.shedlang.compiler.util.P; import java.util.List; import java.util.Map; +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.*; @@ -199,6 +202,26 @@ public class TypeCheckerFunctionDefinitionTests { } @Test + public void bodyHasReturnTypeAsDefinedInSignature() { + var untyped = UntypedFunctionDefinitionNode.builder() + .withReturnType(Untyped.typeLevelReference("String")) + .addStatement(Untyped.return_(Untyped.intLiteral(42))) + .build(); + var context = TypeCheckerContext.EMPTY + .enterNamespaceWithDefinedVariables(P.map( + Map.entry("String", Types.metaType(Types.STRING)) + )); + + var error = assertThrows( + UnexpectedTypeError.class, + () -> TypeChecker.typeCheckNamespaceStatement(untyped, context) + ); + + assertThat(error.expected(), equalTo(TypeSets.type(Types.STRING))); + assertThat(error.actual(), equalTo(Types.INT)); + } + + @Test public void freeVariablesIncludeVariablesReferencedInFunctionButDefinedOutsideOfFunction() { var untyped = UntypedFunctionDefinitionNode.builder() .withReturnType(Untyped.typeLevelReference("Unit")) |
