aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerFunctionDefinitionTests.java23
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"))