diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-30 21:29:52 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-30 21:29:52 +0100 |
| commit | 2ff551c30f62283bd9faca4ad8e105b9a951abab (patch) | |
| tree | 057f06a0b1e720e90aaace48e88e8f881d297889 /src/test/java/org/zwobble | |
| parent | 00f2fff352e22fb83c0de38e6e5f70f4d1a0fea1 (diff) | |
Do not write to code file if header is missing
Diffstat (limited to 'src/test/java/org/zwobble')
3 files changed, 86 insertions, 49 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java index b0ff40c..2b3ad6d 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java @@ -14,17 +14,21 @@ public class CodeWriterTests { @Test public void whenFileDoesNotExistThenCustomAreasAreEmpty() throws IOException { var stringWriter = new StringWriter(); - var codeWriter = CodeWriter.file( - new CodeWriter.InMemoryCodeWriterFile(Optional.empty(), stringWriter), - "//" + var file = new CodeWriter.InMemoryCodeWriterFile(Optional.empty(), stringWriter); + + CodeWriter.writeTo( + file, + "//", + codeWriter -> { + codeWriter.writeHeader(); + codeWriter.write("Before"); + codeWriter.newLine(); + codeWriter.writeCustomArea("body"); + codeWriter.write("After"); + codeWriter.newLine(); + } ); - codeWriter.writeHeader(); - codeWriter.write("Before"); - codeWriter.newLine(); - codeWriter.writeCustomArea("body"); - codeWriter.write("After"); - codeWriter.newLine(); assertThat(stringWriter.toString(), equalTo(""" // Generated by hobgoblin. @@ -39,21 +43,28 @@ public class CodeWriterTests { @Test public void whenExistingFileIsMissingCustomAreaThenCustomAreaIsEmpty() throws IOException { var stringWriter = new StringWriter(); - var codeWriter = CodeWriter.file( - new CodeWriter.InMemoryCodeWriterFile(Optional.of(""), stringWriter), - "//" - ); + var originalContents = """ + // Generated by hobgoblin. - codeWriter.writeHeader(); - codeWriter.write("Before"); - codeWriter.newLine(); - codeWriter.writeCustomArea("body"); - codeWriter.write("After"); - codeWriter.newLine(); + """; + var file = new CodeWriter.InMemoryCodeWriterFile(Optional.of(originalContents), stringWriter); + + CodeWriter.writeTo( + file, + "//", + codeWriter -> { + codeWriter.writeHeader(); + codeWriter.write("Before"); + codeWriter.newLine(); + codeWriter.writeCustomArea("body"); + codeWriter.write("After"); + codeWriter.newLine(); + } + ); assertThat(stringWriter.toString(), equalTo(""" // Generated by hobgoblin. - + Before // Custom area start: body // Custom area end: body @@ -65,6 +76,8 @@ public class CodeWriterTests { public void whenExistingFileHasCustomAreasThenCustomAreaContentsArePreserved() throws IOException { var stringWriter = new StringWriter(); var originalContents = """ + // Generated by hobgoblin. + // Custom area start: head Custom head. // Custom area end: head @@ -72,18 +85,21 @@ public class CodeWriterTests { Custom body. // Custom area end: body """; - var codeWriter = CodeWriter.file( - new CodeWriter.InMemoryCodeWriterFile(Optional.of(originalContents), stringWriter), - "//" - ); + var file = new CodeWriter.InMemoryCodeWriterFile(Optional.of(originalContents), stringWriter); - codeWriter.writeHeader(); - codeWriter.writeCustomArea("head"); - codeWriter.writeCustomArea("body"); + CodeWriter.writeTo( + file, + "//", + codeWriter -> { + codeWriter.writeHeader(); + codeWriter.writeCustomArea("head"); + codeWriter.writeCustomArea("body"); + } + ); assertThat(stringWriter.toString(), equalTo(""" // Generated by hobgoblin. - + // Custom area start: head Custom head. // Custom area end: head @@ -92,4 +108,31 @@ public class CodeWriterTests { // Custom area end: body """)); } + + @Test + public void whenExistingFileIsMissingHeaderThenNothingIsWritten() throws IOException { + var stringWriter = new StringWriter(); + var originalContents = """ + // Custom area start: head + Custom head. + // Custom area end: head + // Custom area start: body + Custom body. + // Custom area end: body + """; + var file = new CodeWriter.InMemoryCodeWriterFile(Optional.of(originalContents), stringWriter); + + CodeWriter.writeTo( + file, + "//", + codeWriter -> { + codeWriter.writeHeader(); + codeWriter.writeCustomArea("head"); + codeWriter.writeCustomArea("body"); + } + ); + + assertThat(stringWriter.toString(), equalTo("")); + assertThat(file.isWriterOpened(), equalTo(false)); + } } 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 2889032..9b16328 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 @@ -1698,7 +1698,9 @@ public class JavaWriterTests { var string = write( writer -> writer.writeTypeDeclaration(java), """ - public class Point { + // Generated by hobgoblin. + + public class Point { // Custom area start: Point body // <custom area> // Custom area end: Point body @@ -1717,24 +1719,20 @@ public class JavaWriterTests { // == Doc comments == - private String write(Write write) throws IOException { + private String write(JavaWriter.Write write) throws IOException { return write(write, Optional.empty()); } - private String write(Write write, String originalContents) throws IOException { + private String write(JavaWriter.Write write, String originalContents) throws IOException { return write(write, Optional.of(originalContents)); } - private String write(Write write, Optional<String> originalContents) throws IOException { + private String write(JavaWriter.Write write, Optional<String> originalContents) throws IOException { var stringWriter = new StringWriter(); - var javaWriter = JavaWriter.file( - new CodeWriter.InMemoryCodeWriterFile(originalContents, stringWriter) + JavaWriter.writeTo( + new CodeWriter.InMemoryCodeWriterFile(originalContents, stringWriter), + write ); - write.write(javaWriter); return stringWriter.toString(); } - - private interface Write { - void write(JavaWriter writer) throws IOException; - } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java index e9edb72..4a81031 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/rust/RustWriterTests.java @@ -1204,24 +1204,20 @@ public class RustWriterTests { assertThat(string, equalTo("r#const")); } - private String write(Write write) throws IOException { + private String write(RustWriter.Write write) throws IOException { return write(write, Optional.empty()); } - private String write(Write write, String originalContents) throws IOException { + private String write(RustWriter.Write write, String originalContents) throws IOException { return write(write, Optional.of(originalContents)); } - private String write(Write write, Optional<String> originalContents) throws IOException { + private String write(RustWriter.Write write, Optional<String> originalContents) throws IOException { var stringWriter = new StringWriter(); - var rustWriter = RustWriter.file( - new CodeWriter.InMemoryCodeWriterFile(originalContents, stringWriter) + RustWriter.writeTo( + new CodeWriter.InMemoryCodeWriterFile(originalContents, stringWriter), + write ); - write.write(rustWriter); return stringWriter.toString(); } - - private interface Write { - void write(RustWriter writer) throws IOException; - } } |
