From 00f2fff352e22fb83c0de38e6e5f70f4d1a0fea1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 30 Jul 2026 21:04:30 +0100 Subject: Add tests for custom area behaviour of CodeWriter --- .../compiler/output/lang/CodeWriterTests.java | 95 ++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java (limited to 'src/test/java/org') 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 + """)); + } +} -- cgit v1.2.3