summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-11-29 10:26:30 +0000
committerMichael Williamson <mike@zwobble.org>2026-11-29 10:26:30 +0000
commit978bf81a82c61e9f2a9aa88d7e2b5882d1a86c45 (patch)
tree52dfeae6b99a31eb5785d823d48388de1eafa436 /src/test
parent8d3f483be2127b04e7e90e06d207701b4b4f5c65 (diff)
Type check binary operations
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java65
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java65
2 files changed, 130 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java
new file mode 100644
index 0000000..4d533fa
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedBinaryOperationNodeMatcher.java
@@ -0,0 +1,65 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.typed;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher imports
+
+public final class TypedBinaryOperationNodeMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher isTypedBinaryOperationNode() {
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode>> submatchers;
+
+ private TypedBinaryOperationNodeMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withOperator(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperator> operator) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("operator", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::operator, (org.zwobble.precisely.Matcher) operator));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withLeft(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode> left) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("left", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::left, (org.zwobble.precisely.Matcher) left));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withRight(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode> right) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("right", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::right, (org.zwobble.precisely.Matcher) right));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.types.Type> type) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::type, (org.zwobble.precisely.Matcher) type));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher withSource(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.sources.Source> source) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNode::source, (org.zwobble.precisely.Matcher) source));
+ return new org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java
new file mode 100644
index 0000000..22584d8
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerBinaryOperationTests.java
@@ -0,0 +1,65 @@
+package org.zwobble.hobgoblin.compiler.typechecker;
+
+import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperator;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode;
+import org.zwobble.hobgoblin.compiler.builtins.NativeTypes;
+import org.zwobble.hobgoblin.compiler.sources.NullSource;
+import org.zwobble.hobgoblin.compiler.typechecker.errors.UnsupportedBinaryOperationError;
+import org.zwobble.hobgoblin.compiler.types.NamespaceName;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.zwobble.hobgoblin.compiler.ast.typed.TypedBinaryOperationNodeMatcher.isTypedBinaryOperationNode;
+import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNodeMatcher.isTypedReferenceNode;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class TypeCheckerBinaryOperationTests {
+ @Test
+ public void whenLeftAndRightOperandsAreNumbersThenEqualityOperationTypeChecks() {
+ var untyped = UntypedBinaryOperationNode.arbitrary()
+ .withOperator(UntypedBinaryOperator.EQUALS)
+ .withLeft(UntypedReferenceNode.arbitrary().withName("a"))
+ .withRight(UntypedReferenceNode.arbitrary().withName("b"))
+ .build();
+ var globalContext = TypeCheckerContextArb.globalContext();
+ var namespaceName = NamespaceName.of("App");
+ var namespaceContext = globalContext.enterNamespace(namespaceName);
+ namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE);
+ namespaceContext.declare("b", NativeTypes.INT_32, NullSource.INSTANCE);
+
+ var typed = TypeChecker.typeCheckExpression(untyped, namespaceContext);
+
+ assertThat(typed, isTypedBinaryOperationNode()
+ .withOperator(equalTo(TypedBinaryOperator.EQUALS))
+ .withLeft(isTypedReferenceNode().withName(equalTo("a")))
+ .withRight(isTypedReferenceNode().withName(equalTo("b")))
+ .withType(equalTo(NativeTypes.INT_32))
+ );
+ }
+
+ @Test
+ public void whenLeftAndRightOperandsAreDifferentTypesThenEqualityOperationFailsToTypeCheck() {
+ var untyped = UntypedBinaryOperationNode.arbitrary()
+ .withOperator(UntypedBinaryOperator.EQUALS)
+ .withLeft(UntypedReferenceNode.arbitrary().withName("a"))
+ .withRight(UntypedReferenceNode.arbitrary().withName("b"))
+ .build();
+ var globalContext = TypeCheckerContextArb.globalContext();
+ var namespaceName = NamespaceName.of("App");
+ var namespaceContext = globalContext.enterNamespace(namespaceName);
+ namespaceContext.declare("a", NativeTypes.INT_32, NullSource.INSTANCE);
+ namespaceContext.declare("b", NativeTypes.STRING, NullSource.INSTANCE);
+
+ var error = assertThrows(
+ UnsupportedBinaryOperationError.class,
+ () -> TypeChecker.typeCheckExpression(untyped, namespaceContext)
+ );
+
+ assertThat(error.operator(), equalTo(UntypedBinaryOperator.EQUALS));
+ assertThat(error.left(), equalTo(NativeTypes.INT_32));
+ assertThat(error.right(), equalTo(NativeTypes.STRING));
+ }
+}