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)); } @Test public void strippingDoesNothingToFileWithoutHeader() { var originalContents = """ Hello, world! """; var result = CodeWriter.stripFileContents( originalContents, new CodeWriter.CodeWriterConfig("//") ); assertThat(result, equalTo(new CodeWriter.StripResult.Unchanged())); } @Test public void whenFileHasNoCustomAreasThenFileIsDeleted() { var originalContents = """ // Generated by hobgoblin. Hello, world! """; var result = CodeWriter.stripFileContents( originalContents, new CodeWriter.CodeWriterConfig("//") ); assertThat(result, equalTo(new CodeWriter.StripResult.Delete())); } @Test public void whenCustomAreasAreAllEmptyThenFileIsDeleted() { var originalContents = """ // Generated by hobgoblin. Before // Custom area start: body // Custom area end: body After """; var result = CodeWriter.stripFileContents( originalContents, new CodeWriter.CodeWriterConfig("//") ); assertThat(result, equalTo(new CodeWriter.StripResult.Delete())); } @Test public void nonEmptyCustomAreasArePreserved() { var originalContents = """ // Generated by hobgoblin. Before // Custom area start: head Custom head. // Custom area end: head // Custom area start: body // Custom area end: body After """; var result = CodeWriter.stripFileContents( originalContents, new CodeWriter.CodeWriterConfig("//") ); assertThat(result, equalTo(new CodeWriter.StripResult.NewContent(""" // Generated by hobgoblin. // Custom area start: head Custom head. // Custom area end: head """))); } }