summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-30 09:25:33 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-30 09:25:33 +0100
commit0469bc1b4ebcc22d118b7234bc7b62d3fa2f9ca4 (patch)
treeb6b7b20b26fda5800954199a1ab7541c51f16fb7 /src/test/java
parent93fdddf21b33fb95f249348d6fe8d38f6564243c (diff)
Add constructor to TypeScript AST
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java67
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java41
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.java47
3 files changed, 141 insertions, 14 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 2d9b86f..5e5c57a 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
@@ -32,10 +32,10 @@ public class TypescriptWriterTests {
public void moduleWithStatements() throws IOException {
var typescript = TypescriptModule.arbitrary()
.addBodyElement(TypescriptClassDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("X"))
+ .withName(id("X"))
)
.addBodyElement(TypescriptClassDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("Y"))
+ .withName(id("Y"))
)
.build();
@@ -60,7 +60,7 @@ public class TypescriptWriterTests {
@Test
public void emptyClassDeclaration() throws IOException {
var typescript = TypescriptClassDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("X"))
+ .withName(id("X"))
.build();
var string = write(writer -> writer.writeStatement(typescript));
@@ -74,12 +74,12 @@ public class TypescriptWriterTests {
@Test
public void classDeclarationWithElements() throws IOException {
var typescript = TypescriptClassDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("X"))
+ .withName(id("X"))
.addBodyElement(TypescriptClassFieldDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("a"))
+ .withName(id("a"))
)
.addBodyElement(TypescriptClassFieldDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("b"))
+ .withName(id("b"))
)
.build();
@@ -97,7 +97,7 @@ public class TypescriptWriterTests {
@Test
public void classDeclarationWithDocComment() throws IOException {
var typescript = TypescriptClassDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("X"))
+ .withName(id("X"))
.withDocComment(new DocComment("abc\ndef"))
.build();
@@ -115,12 +115,45 @@ public class TypescriptWriterTests {
// == Class elements ==
+ // === Class constructors ===
+
+ @Test
+ public void emptyClassConstructor() throws IOException {
+ var typescript = TypescriptClassConstructor.arbitrary().build();
+
+ var string = write(writer -> writer.writeClassElement(typescript));
+
+ assertThat(string, equalTo("""
+ constructor() {
+ }"""));
+ }
+
+ @Test
+ public void classConstructorWithParams() throws IOException {
+ var typescript = TypescriptClassConstructor.arbitrary()
+ .addParam(TypescriptParam.arbitrary()
+ .withName(id("a"))
+ .withType(TypescriptRef.arbitrary().withName(id("number")))
+ )
+ .addParam(TypescriptParam.arbitrary()
+ .withName(id("b"))
+ .withType(TypescriptRef.arbitrary().withName(id("string")))
+ )
+ .build();
+
+ var string = write(writer -> writer.writeClassElement(typescript));
+
+ assertThat(string, equalTo("""
+ constructor(a: number, b: string) {
+ }"""));
+ }
+
// === Class field declarations ===
@Test
public void classFieldDeclarationWithoutInitialValue() throws IOException {
var typescript = TypescriptClassFieldDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("a"))
+ .withName(id("a"))
.build();
var string = write(writer -> writer.writeClassElement(typescript));
@@ -131,8 +164,8 @@ public class TypescriptWriterTests {
@Test
public void classFieldDeclarationWithExplicitType() throws IOException {
var typescript = TypescriptClassFieldDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("a"))
- .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number")))
+ .withName(id("a"))
+ .withType(TypescriptRef.arbitrary().withName(id("number")))
.build();
var string = write(writer -> writer.writeClassElement(typescript));
@@ -143,7 +176,7 @@ public class TypescriptWriterTests {
@Test
public void classFieldDeclarationWithInitialValue() throws IOException {
var typescript = TypescriptClassFieldDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("a"))
+ .withName(id("a"))
.withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42))
.build();
@@ -155,8 +188,8 @@ public class TypescriptWriterTests {
@Test
public void classFieldDeclarationWithTypeAndInitialValue() throws IOException {
var typescript = TypescriptClassFieldDeclaration.arbitrary()
- .withName(TypescriptIdentifier.of("a"))
- .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number")))
+ .withName(id("a"))
+ .withType(TypescriptRef.arbitrary().withName(id("number")))
.withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42))
.build();
@@ -185,7 +218,7 @@ public class TypescriptWriterTests {
@Test
public void reference() throws IOException {
var typescript = TypescriptRef.arbitrary()
- .withName(TypescriptIdentifier.of("x"))
+ .withName(id("x"))
.build();
var string = write(writer -> writer.writeExpression(typescript));
@@ -193,6 +226,12 @@ public class TypescriptWriterTests {
assertThat(string, equalTo("x"));
}
+ // == Utilities ==
+
+ private static TypescriptIdentifier id(String name) {
+ return TypescriptIdentifier.of(name);
+ }
+
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/TypescriptClassConstructorMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java
new file mode 100644
index 0000000..081372d
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassConstructorMatcher.java
@@ -0,0 +1,41 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher imports
+
+public final class TypescriptClassConstructorMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher isTypescriptClassConstructor() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor>> submatchers;
+
+ private TypescriptClassConstructorMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor>> 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.TypescriptClassConstructor.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher withParams(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam>> params) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("params", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructor::params, (org.zwobble.precisely.Matcher) params));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassConstructorMatcher body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.java
new file mode 100644
index 0000000..fc921ea
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptParamMatcher.java
@@ -0,0 +1,47 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher imports
+
+public final class TypescriptParamMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher isTypescriptParam() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam>> submatchers;
+
+ private TypescriptParamMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam>> 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.TypescriptParam.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher withName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptIdentifier> name) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam::name, (org.zwobble.precisely.Matcher) name));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher withType(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression>> type) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParam::type, (org.zwobble.precisely.Matcher) type));
+ return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptParamMatcher body
+}