diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-08-30 08:58:39 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-08-30 08:58:39 +0100 |
| commit | d8771a35c842daab25d6e02df99855ef1e41b740 (patch) | |
| tree | e89034492f73b69b87419c222058ced1ad03ffd8 /src/test/java | |
| parent | 065958bb0bbe55a40a1c3df0748bb0b1625f679f (diff) | |
Add optional type to TypeScript class field declarations
Diffstat (limited to 'src/test/java')
3 files changed, 85 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 acb4fea..2d9b86f 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 @@ -129,6 +129,18 @@ public class TypescriptWriterTests { } @Test + public void classFieldDeclarationWithExplicitType() throws IOException { + var typescript = TypescriptClassFieldDeclaration.arbitrary() + .withName(TypescriptIdentifier.of("a")) + .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number"))) + .build(); + + var string = write(writer -> writer.writeClassElement(typescript)); + + assertThat(string, equalTo("a: number;")); + } + + @Test public void classFieldDeclarationWithInitialValue() throws IOException { var typescript = TypescriptClassFieldDeclaration.arbitrary() .withName(TypescriptIdentifier.of("a")) @@ -140,6 +152,19 @@ public class TypescriptWriterTests { assertThat(string, equalTo("a = 42.0;")); } + @Test + public void classFieldDeclarationWithTypeAndInitialValue() throws IOException { + var typescript = TypescriptClassFieldDeclaration.arbitrary() + .withName(TypescriptIdentifier.of("a")) + .withType(TypescriptRef.arbitrary().withName(TypescriptIdentifier.of("number"))) + .withInitialValue(TypescriptNumberLiteral.arbitrary().withValue(42)) + .build(); + + var string = write(writer -> writer.writeClassElement(typescript)); + + assertThat(string, equalTo("a: number = 42.0;")); + } + // == Expressions == // === Number literals === @@ -155,6 +180,19 @@ public class TypescriptWriterTests { assertThat(string, equalTo("42.0")); } + // === References === + + @Test + public void reference() throws IOException { + var typescript = TypescriptRef.arbitrary() + .withName(TypescriptIdentifier.of("x")) + .build(); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("x")); + } + 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/TypescriptClassFieldDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclarationMatcher.java index 558ad00..3942fb4 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclarationMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptClassFieldDeclarationMatcher.java @@ -36,6 +36,12 @@ public final class TypescriptClassFieldDeclarationMatcher implements org.zwobble return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher 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.TypescriptClassFieldDeclaration>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration::type, (org.zwobble.precisely.Matcher) type)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher(submatchers); + } + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclarationMatcher withInitialValue(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptExpression>> initialValue) { var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration>>(this.submatchers); submatchers.add(org.zwobble.precisely.Matchers.has("initialValue", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptClassFieldDeclaration::initialValue, (org.zwobble.precisely.Matcher) initialValue)); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptRefMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptRefMatcher.java new file mode 100644 index 0000000..de86aff --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptRefMatcher.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.TypescriptRefMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher imports + +public final class TypescriptRefMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher isTypescriptRef() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef>> submatchers; + + private TypescriptRefMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef>> 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.TypescriptRef.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher 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.TypescriptRef>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRef::name, (org.zwobble.precisely.Matcher) name)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptRefMatcher body +} |
