summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/CodeWriterTests.java95
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
+ """));
+ }
+}