summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-10-16 15:19:03 +0100
committerMichael Williamson <mike@zwobble.org>2026-10-16 15:19:03 +0100
commit6f392b7a68dc47811928463ee4502c9a222a0657 (patch)
treefdebd67c4d21b02207f3133cbc905b95078d2c83 /src/test/java
parent0c89574853f1ebe34bd37346ae21d5586440656d (diff)
Add SQL scripts to SQL AST
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java46
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java41
2 files changed, 82 insertions, 5 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java
index 2d8c7d3..42b075f 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/SqlWriterTests.java
@@ -2,10 +2,7 @@ package org.zwobble.hobgoblin.compiler.output.lang.sql;
import org.junit.jupiter.api.Test;
import org.zwobble.hobgoblin.compiler.output.CodeWriter;
-import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlColumnDefinition;
-import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlCreateTableStatement;
-import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier;
-import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlPrimaryKeyConstraint;
+import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.*;
import java.io.IOException;
import java.io.StringWriter;
@@ -15,7 +12,46 @@ import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;
public class SqlWriterTests {
- // == Create table statements ==
+ // == Scripts ==
+
+ @Test
+ public void script() throws IOException {
+ var sql = SqlScript.arbitrary()
+ .addStatement(SqlCreateTableStatement.arbitrary()
+ .withName(id("authors"))
+ .addColumn(SqlColumnDefinition.arbitrary()
+ .withName(id("id"))
+ .withTypeName(id("INT"))
+ )
+ )
+ .addStatement(SqlCreateTableStatement.arbitrary()
+ .withName(id("posts"))
+ .addColumn(SqlColumnDefinition.arbitrary()
+ .withName(id("name"))
+ .withTypeName(id("TEXT"))
+ )
+ )
+ .build();
+
+ var string = write(writer -> writer.writeScript(sql));
+
+ assertThat(string, equalTo("""
+ -- Generated by hobgoblin.
+
+ CREATE TABLE authors (
+ id INT
+ );
+
+ CREATE TABLE posts (
+ name TEXT
+ );
+ """
+ ));
+ }
+
+ // == Statements ==
+
+ // === Create table statements ===
@Test
public void createTableStatement() throws IOException {
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java
new file mode 100644
index 0000000..6ce14b7
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/sql/ast/SqlScriptMatcher.java
@@ -0,0 +1,41 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.sql.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher imports
+
+public final class SqlScriptMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher isSqlScript() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript>> submatchers;
+
+ private SqlScriptMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript>> 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.sql.ast.SqlScript.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher withStatements(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlStatement>> statements) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("statements", org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScript::statements, (org.zwobble.precisely.Matcher) statements));
+ return new org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlScriptMatcher body
+}