summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-05 13:05:30 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-05 13:05:30 +0100
commit114823dba737858fcdd6e3748f58865bf0204784 (patch)
tree799fb7ab1ff2ed2a870dde97800b7cc021229798 /src/test
parent41b24e8269bf7c5a2f3faeb11bca2fe1e6037f3e (diff)
Add ternary conditional to Java AST
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java13
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.java51
2 files changed, 64 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
index 1ce78ea..0b5f959 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
@@ -1016,6 +1016,19 @@ public class JavaWriterTests {
}
@Test
+ public void ternaryConditional() throws IOException {
+ var java = new JavaTernaryConditional(
+ new JavaRef(JavaIdentifier.of("x")),
+ new JavaRef(JavaIdentifier.of("y")),
+ new JavaRef(JavaIdentifier.of("z"))
+ );
+
+ var string = write(writer -> writer.writeTopLevelExpression(java));
+
+ assertThat(string, equalTo("x ? y : z"));
+ }
+
+ @Test
public void whenSubExpressionHasLowerPrecedenceThenSubExpressionIsParenthesized() throws IOException {
var java = JavaBinaryOperation.arbitrary()
.withOperator(JavaBinaryOperator.BITWISE_AND)
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.java
new file mode 100644
index 0000000..5f39a26
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaTernaryConditionalMatcher.java
@@ -0,0 +1,51 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher imports
+
+public class JavaTernaryConditionalMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher isJavaTernaryConditional() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional>> submatchers;
+
+ private JavaTernaryConditionalMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional>> 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.java.ast.JavaTernaryConditional.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher withCondition(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> condition) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("condition", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional::condition, condition));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher withIfTrue(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> ifTrue) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("ifTrue", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional::ifTrue, ifTrue));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher withIfFalse(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> ifFalse) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("ifFalse", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditional::ifFalse, ifFalse));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTernaryConditionalMatcher body
+}