aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-07-03 12:35:23 +0100
committerMichael Williamson <mike@zwobble.org>2023-07-03 12:35:23 +0100
commit28b744b0039e381e197f46cea0e5e87ac4b8bab2 (patch)
tree2d007b968e11347f7d389bfc28258330180b6119 /src/test/java
parent3b69ffa33d419ee31331582edac427393009e711 (diff)
Check for duplicate variable names
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java
index 7967e442..f09c73e9 100644
--- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerValTests.java
@@ -3,6 +3,7 @@ package org.shedlang.compiler.typechecker;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.untyped.Untyped;
import org.shedlang.compiler.ast.untyped.UntypedFunctionDefinitionNode;
+import org.shedlang.compiler.typechecker.errors.DuplicateVariableName;
import org.shedlang.compiler.typechecker.errors.ReferenceBeforeDefinitionError;
import org.shedlang.compiler.types.Types;
import org.shedlang.compiler.util.P;
@@ -88,4 +89,23 @@ public class TypeCheckerValTests {
assertThat(error.variableName(), equalTo("x"));
}
+
+ @Test
+ public void duplicateVariableNamesAreNotAllowed() {
+ var untyped = UntypedFunctionDefinitionNode.builder()
+ .addStatement(Untyped.val(Untyped.targetVariable("x"), Untyped.stringLiteral("hello")))
+ .addStatement(Untyped.val(Untyped.targetVariable("x"), Untyped.stringLiteral("hello")))
+ .build();
+ var context = TypeCheckerContext.EMPTY
+ .enterBlockWithDefinedVariables(P.map(
+ Map.entry("Unit", Types.metaType(Types.UNIT))
+ ));
+
+ var error = assertThrows(
+ DuplicateVariableName.class,
+ () -> TypeChecker.typeCheckNamespaceStatement(untyped, context)
+ );
+
+ assertThat(error.variableName(), equalTo("x"));
+ }
}