diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-09-04 09:40:50 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-09-04 09:40:50 +0100 |
| commit | 8dd50423a53f87f15464c467874684300b129ba8 (patch) | |
| tree | 8474f64e0494216f8f983506ed7bffd8aed07778 /src/test/java | |
| parent | 57f8396dd1c92e87be342d35880f1ca04a5a6c53 (diff) | |
Add object literal to TypeScript AST
Diffstat (limited to 'src/test/java')
3 files changed, 125 insertions, 0 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 8348743..b45e5c4 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 @@ -7,6 +7,7 @@ import org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.*; import java.io.IOException; import java.io.StringWriter; +import java.util.List; import java.util.Optional; import static org.zwobble.precisely.AssertThat.assertThat; @@ -421,6 +422,42 @@ public class TypescriptWriterTests { assertThat(string, equalTo("42.0")); } + // === Object literals === + + @Test + public void objectLiteralEmpty() throws IOException { + var typescript = TypescriptObjectLiteral.arbitrary() + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("{}")); + } + + @Test + public void objectLiteralWithProperties() throws IOException { + var typescript = TypescriptObjectLiteral.arbitrary() + .withProperties(List.of( + TypescriptObjectLiteralProperty.arbitrary() + .withName(TypescriptIdentifier.of("a")) + .withValue(ref("valA")) + .build(), + TypescriptObjectLiteralProperty.arbitrary() + .withName(TypescriptIdentifier.of("b")) + .withValue(ref("valB")) + .build() + )) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo(""" + { + a: valA, + b: valB, + }""")); + } + // === References === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralMatcher.java new file mode 100644 index 0000000..bd8edc3 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralMatcher.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.TypescriptObjectLiteralMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher imports + +public final class TypescriptObjectLiteralMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher isTypescriptObjectLiteral() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral>> submatchers; + + private TypescriptObjectLiteralMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral>> 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.TypescriptObjectLiteral.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher withProperties(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>> properties) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("properties", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteral::properties, (org.zwobble.precisely.Matcher) properties)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralPropertyMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralPropertyMatcher.java new file mode 100644 index 0000000..86ea550 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptObjectLiteralPropertyMatcher.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.TypescriptObjectLiteralPropertyMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher imports + +public final class TypescriptObjectLiteralPropertyMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher isTypescriptObjectLiteralProperty() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>> submatchers; + + private TypescriptObjectLiteralPropertyMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>> 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.TypescriptObjectLiteralProperty.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher 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.TypescriptObjectLiteralProperty>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher withValue(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression> value) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralProperty::value, (org.zwobble.precisely.Matcher) value)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptObjectLiteralPropertyMatcher body +} |
