summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-30 09:33:49 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-30 09:33:49 +0100
commit5257c230be9bbbb1dad4d053d80405d29369a1c7 (patch)
tree936cba5877a02ac209f8a5fdb1a4d5ba99296f5f /src/test/java
parent844903922facb51292735c6577296c7d5f17c1a9 (diff)
Add assignment operations to TypeScript AST
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java47
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java53
2 files changed, 87 insertions, 13 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java
index 82b76f0..690a676 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java
@@ -118,7 +118,7 @@ public class TypescriptWriterTests {
@Test
public void expressionStatement() throws IOException {
var typescript = TypescriptExpressionStatement.arbitrary()
- .withExpression(TypescriptNumberLiteral.arbitrary().withValue(42))
+ .withExpression(num(42))
.build();
var string = write(writer -> writer.writeStatement(typescript));
@@ -146,11 +146,11 @@ public class TypescriptWriterTests {
var typescript = TypescriptClassConstructor.arbitrary()
.addParam(TypescriptParam.arbitrary()
.withName(id("a"))
- .withType(TypescriptRef.arbitrary().withName(id("number")))
+ .withType(ref("number"))
)
.addParam(TypescriptParam.arbitrary()
.withName(id("b"))
- .withType(TypescriptRef.arbitrary().withName(id("string")))
+ .withType(ref("string"))
)
.build();
@@ -178,7 +178,7 @@ public class TypescriptWriterTests {
public void classFieldDeclarationWithExplicitType() throws IOException {
var typescript = TypescriptClassFieldDeclaration.arbitrary()
.withName(id("a"))
- .withType(TypescriptRef.arbitrary().withName(id("number")))
+ .withType(ref("number"))
.build();
var string = write(writer -> writer.writeClassElement(typescript));
@@ -190,7 +190,7 @@ public class TypescriptWriterTests {
public void classFieldDeclarationWithInitialValue() throws IOException {
var typescript = TypescriptClassFieldDeclaration.arbitrary()
.withName(id("a"))
- .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42))
+ .withInitialValue(num(42))
.build();
var string = write(writer -> writer.writeClassElement(typescript));
@@ -202,8 +202,8 @@ public class TypescriptWriterTests {
public void classFieldDeclarationWithTypeAndInitialValue() throws IOException {
var typescript = TypescriptClassFieldDeclaration.arbitrary()
.withName(id("a"))
- .withType(TypescriptRef.arbitrary().withName(id("number")))
- .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42))
+ .withType(ref("number"))
+ .withInitialValue(num(42))
.build();
var string = write(writer -> writer.writeClassElement(typescript));
@@ -211,15 +211,32 @@ public class TypescriptWriterTests {
assertThat(string, equalTo("a: number = 42.0;"));
}
+ private static TypescriptRef ref(String value) {
+ return TypescriptRef.arbitrary().withName(id(value)).build();
+ }
+
// == Expressions ==
+ // === Binary operations ===
+
+ @Test
+ public void assign() throws IOException {
+ var typescript = TypescriptBinaryOperation.arbitrary()
+ .withOperator(TypescriptBinaryOperator.ASSIGN)
+ .withLeft(ref("x"))
+ .withRight(num(42))
+ .build();
+
+ var string = write(writer -> writer.writeExpression(typescript));
+
+ assertThat(string, equalTo("x = 42.0"));
+ }
+
// === Number literals ===
@Test
public void numberLiteral() throws IOException {
- var typescript = TypescriptNumberLiteral.arbitrary()
- .withValue(42)
- .build();
+ var typescript = num(42);
var string = write(writer -> writer.writeExpression(typescript));
@@ -230,9 +247,7 @@ public class TypescriptWriterTests {
@Test
public void reference() throws IOException {
- var typescript = TypescriptRef.arbitrary()
- .withName(id("x"))
- .build();
+ var typescript = ref("x");
var string = write(writer -> writer.writeExpression(typescript));
@@ -245,6 +260,12 @@ public class TypescriptWriterTests {
return TypescriptIdentifier.of(name);
}
+ private static TypescriptNumberLiteral num(int value) {
+ return TypescriptNumberLiteral.arbitrary()
+ .withValue(value)
+ .build();
+ }
+
private String write(TypescriptWriter.Write write) throws IOException {
return write(write, Optional.empty());
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java
new file mode 100644
index 0000000..a46c68d
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptBinaryOperationMatcher.java
@@ -0,0 +1,53 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher imports
+
+public final class TypescriptBinaryOperationMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher isTypescriptBinaryOperation() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation>> submatchers;
+
+ private TypescriptBinaryOperationMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation>> 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.output.lang.typescript.ast.TypescriptBinaryOperation.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher withOperator(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperator> operator) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("operator", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation::operator, (org.zwobble.precisely.Matcher) operator));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher withLeft(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> left) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("left", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation::left, (org.zwobble.precisely.Matcher) left));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher withRight(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> right) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("right", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperation::right, (org.zwobble.precisely.Matcher) right));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptBinaryOperationMatcher body
+}