From 5c83ab1e74bf8877433c085340f7bf25d2079f77 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 24 Oct 2026 23:05:44 +0100 Subject: Fix package for CodeWriterTests --- .../hobgoblin/compiler/output/CodeWriterTests.java | 137 ++++++++++++++++++++ .../compiler/output/lang/CodeWriterTests.java | 138 --------------------- 2 files changed, 137 insertions(+), 138 deletions(-) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/CodeWriterTests.java delete mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java (limited to 'src/test/java') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/CodeWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/CodeWriterTests.java new file mode 100644 index 0000000..decea99 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/CodeWriterTests.java @@ -0,0 +1,137 @@ +package org.zwobble.hobgoblin.compiler.output; + +import org.junit.jupiter.api.Test; + +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 file = new CodeWriter.InMemoryCodeWriterFile(Optional.empty(), stringWriter); + + CodeWriter.writeTo( + file, + new CodeWriter.CodeWriterConfig("//"), + 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 + After + """)); + } + + @Test + public void whenExistingFileIsMissingCustomAreaThenCustomAreaIsEmpty() throws IOException { + var stringWriter = new StringWriter(); + var originalContents = """ + // Generated by hobgoblin. + + """; + var file = new CodeWriter.InMemoryCodeWriterFile(Optional.of(originalContents), stringWriter); + + CodeWriter.writeTo( + file, + new CodeWriter.CodeWriterConfig("//"), + 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 + After + """)); + } + + @Test + public void whenExistingFileHasCustomAreasThenCustomAreaContentsArePreserved() throws IOException { + var stringWriter = new StringWriter(); + var originalContents = """ + // Generated by hobgoblin. + + // 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, + new CodeWriter.CodeWriterConfig("//"), + 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 + // Custom area start: body + Custom body. + // 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, + new CodeWriter.CodeWriterConfig("//"), + 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/CodeWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java deleted file mode 100644 index ae61cb8..0000000 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java +++ /dev/null @@ -1,138 +0,0 @@ -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 file = new CodeWriter.InMemoryCodeWriterFile(Optional.empty(), stringWriter); - - CodeWriter.writeTo( - file, - new CodeWriter.CodeWriterConfig("//"), - 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 - After - """)); - } - - @Test - public void whenExistingFileIsMissingCustomAreaThenCustomAreaIsEmpty() throws IOException { - var stringWriter = new StringWriter(); - var originalContents = """ - // Generated by hobgoblin. - - """; - var file = new CodeWriter.InMemoryCodeWriterFile(Optional.of(originalContents), stringWriter); - - CodeWriter.writeTo( - file, - new CodeWriter.CodeWriterConfig("//"), - 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 - After - """)); - } - - @Test - public void whenExistingFileHasCustomAreasThenCustomAreaContentsArePreserved() throws IOException { - var stringWriter = new StringWriter(); - var originalContents = """ - // Generated by hobgoblin. - - // 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, - new CodeWriter.CodeWriterConfig("//"), - 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 - // Custom area start: body - Custom body. - // 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, - new CodeWriter.CodeWriterConfig("//"), - codeWriter -> { - codeWriter.writeHeader(); - codeWriter.writeCustomArea("head"); - codeWriter.writeCustomArea("body"); - } - ); - - assertThat(stringWriter.toString(), equalTo("")); - assertThat(file.isWriterOpened(), equalTo(false)); - } -} -- cgit v1.2.3