From 55e5e6bca2e6d922b61bfb2d70835861c764816a Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Sat, 15 May 2021 12:38:53 +0200 Subject: [PATCH] Added a test for OutputFileManager. This fixes the problem with -o not being honored always. Closes #644 --- .idea/compiler.xml | 2 +- .../Maven__org_antlr_antlr_runtime_3_5_2.xml | 13 ++ .../kickc/test/TestOutputFileManager.java | 122 ++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 .idea/libraries/Maven__org_antlr_antlr_runtime_3_5_2.xml create mode 100644 src/test/java/dk/camelot64/kickc/test/TestOutputFileManager.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 418269bdb..2f7a93d0e 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -24,7 +24,7 @@ - + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_antlr_antlr_runtime_3_5_2.xml b/.idea/libraries/Maven__org_antlr_antlr_runtime_3_5_2.xml new file mode 100644 index 000000000..4fc5cbb6b --- /dev/null +++ b/.idea/libraries/Maven__org_antlr_antlr_runtime_3_5_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/dk/camelot64/kickc/test/TestOutputFileManager.java b/src/test/java/dk/camelot64/kickc/test/TestOutputFileManager.java new file mode 100644 index 000000000..d9e83acfe --- /dev/null +++ b/src/test/java/dk/camelot64/kickc/test/TestOutputFileManager.java @@ -0,0 +1,122 @@ +package dk.camelot64.kickc.test; + +import dk.camelot64.kickc.OutputFileManager; +import org.junit.jupiter.api.Test; + +import java.nio.file.FileSystems; +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestOutputFileManager { + + private static Path path(String s) { + return FileSystems.getDefault().getPath(s); + } + + @Test + /** The default file is current-dir / primary-file-basename . extension */ + void testDefaultFileNames() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("test.c")); + assertEquals(path("test.prg"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("test.asm"), outputFileManager.getAsmOutputFile()); + } + + @Test + /** If the input file is in a folder the output file is input-dir / primary-file-basename . extension */ + void testInputFolder() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("qwe/test.c")); + assertEquals(path("qwe/test.prg"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("qwe/test.asm"), outputFileManager.getAsmOutputFile()); + } + + @Test + /** If -odir is passed this overwrites the input folder odir / primary-file-basename . extension */ + void testOutputDirFolder() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("qwe/test.c")); + outputFileManager.setOutputDir(path("asd")); + assertEquals(path("asd/test.prg"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("asd/test.asm"), outputFileManager.getAsmOutputFile()); + } + + @Test + /** If -o is passed with a folder name this overwrites the input folder and odir - output-dir / primary-file-basename . extension */ + void testOutputFileFolder() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("qwe/test.c")); + outputFileManager.setOutputDir(path("asd")); + outputFileManager.setAssembleOutput(true); + outputFileManager.setOutputFileName("zxc/test.prg"); + assertEquals(path("zxc/test.prg"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("zxc/test.asm"), outputFileManager.getAsmOutputFile()); + } + + @Test + /** If -o is passed without a folder name we fall back to odir - odir / primary-file-basename . extension */ + void testOutputFileNoFolder() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("qwe/test.c")); + outputFileManager.setOutputDir(path("asd")); + outputFileManager.setAssembleOutput(true); + outputFileManager.setOutputFileName("test.prg"); + assertEquals(path("asd/test.prg"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("asd/test.asm"), outputFileManager.getAsmOutputFile()); + } + + @Test + /** If -o is passed this overwrites the basename of the input file - output-dir / primary-file-basename . extension */ + void testOutputFileBaseName() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("qwe/test.c")); + outputFileManager.setOutputDir(path("asd")); + outputFileManager.setAssembleOutput(true); + outputFileManager.setOutputFileName("zxc/prod.prg"); + assertEquals(path("zxc/prod.prg"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("zxc/prod.asm"), outputFileManager.getAsmOutputFile()); + } + + @Test + /** If -o is passed and assembleout=true this overwrites the file binary extension - output-dir / primary-file-basename . output-extension */ + void testOutputFileExtension() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("qwe/test.c")); + outputFileManager.setOutputDir(path("asd")); + outputFileManager.setAssembleOutput(true); + outputFileManager.setOutputFileName("zxc/prod.bin"); + assertEquals(path("zxc/prod.bin"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("zxc/prod.asm"), outputFileManager.getAsmOutputFile()); + } + + @Test + /** If -o is passed and assembleout=false this overwrites the ASM file extension - output-dir / primary-file-basename . output-extension */ + void testOutputFileAsmExtension() { + final OutputFileManager outputFileManager = new OutputFileManager(); + outputFileManager.setCurrentDir(path(".")); + outputFileManager.setBinaryExtension("prg"); + outputFileManager.setPrimaryCFile(path("qwe/test.c")); + outputFileManager.setOutputDir(path("asd")); + outputFileManager.setAssembleOutput(false); + outputFileManager.setOutputFileName("zxc/prod.s"); + assertEquals(path("zxc/prod.prg"), outputFileManager.getBinaryOutputFile()); + assertEquals(path("zxc/prod.s"), outputFileManager.getAsmOutputFile()); + } + + +}