diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-30 21:04:30 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-30 21:04:30 +0100 |
| commit | 00f2fff352e22fb83c0de38e6e5f70f4d1a0fea1 (patch) | |
| tree | 68a3f600677a400548fa0c3220a6e5c51004f227 /src | |
| parent | e7df71bbbdadbd11b7c55b82fc468f9550b5b027 (diff) | |
Add tests for custom area behaviour of CodeWriter
Diffstat (limited to 'src')
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java | 95 |
1 files changed, 95 insertions, 0 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 new file mode 100644 index 0000000..b0ff40c --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java @@ -0,0 +1,95 @@ +package org.zwobble.hobgoblin.compiler.output.lang; + +import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.output.CodeWriter; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.Optional; + +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class CodeWriterTests { + @Test + public void whenFileDoesNotExistThenCustomAreasAreEmpty() throws IOException { + var stringWriter = new StringWriter(); + var codeWriter = CodeWriter.file( + new CodeWriter.InMemoryCodeWriterFile(Optional.empty(), stringWriter), + "//" + ); + + 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 + After + """)); + } + + @Test + public void whenExistingFileIsMissingCustomAreaThenCustomAreaIsEmpty() throws IOException { + var stringWriter = new StringWriter(); + var codeWriter = CodeWriter.file( + new CodeWriter.InMemoryCodeWriterFile(Optional.of(""), stringWriter), + "//" + ); + + 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 + After + """)); + } + + @Test + public void whenExistingFileHasCustomAreasThenCustomAreaContentsArePreserved() 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 codeWriter = CodeWriter.file( + new CodeWriter.InMemoryCodeWriterFile(Optional.of(originalContents), stringWriter), + "//" + ); + + 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 + // Custom area start: body + Custom body. + // Custom area end: body + """)); + } +} |
