aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-17 22:49:34 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-17 22:49:34 +0100
commitdbcedbdd60d5bf9be5536699e83272879afa81eb (patch)
tree910d5e926c5264e4371510c2c97c6bc34cc4a6ce /src/test/java
parentecbe7b4a85c23ba0520d5ff6e2682efd889848e9 (diff)
Check that variable is declared and defined in same scope
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerContextTests.java17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerContextTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerContextTests.java
index 2be22340..13f2486b 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerContextTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerContextTests.java
@@ -2,6 +2,7 @@ package org.shedlang.compiler.typechecker;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.sources.Sources;
+import org.shedlang.compiler.typechecker.errors.CannotDefineVariableDeclaredInOuterScopeError;
import org.shedlang.compiler.typechecker.errors.UnknownVariableError;
import org.shedlang.compiler.typechecker.errors.VariableIsAlreadyDefinedError;
import org.shedlang.compiler.types.Types;
@@ -58,4 +59,20 @@ public class TypeCheckerContextTests {
assertThat(error.variableName(), equalTo("x"));
}
+
+ @Test
+ public void givenVariableIsBoundInOuterScopeWhenVariableIsDefinedThenErrorIsThrown() {
+ var context = TypeCheckerContext.EMPTY
+ .enterNamespace(P.map(
+ Map.entry("x", TypeCheckerContext.undefined())
+ ))
+ .enterNamespace(P.map());
+
+ var error = assertThrows(
+ CannotDefineVariableDeclaredInOuterScopeError.class,
+ () -> context.define("x", Types.STRING, Sources.NULL)
+ );
+
+ assertThat(error.variableName(), equalTo("x"));
+ }
}