diff --git a/compiler/test/TestCompilerOnCharLit.kt b/compiler/test/TestCompilerOnCharLit.kt index 08d50c272..5f82311bc 100644 --- a/compiler/test/TestCompilerOnCharLit.kt +++ b/compiler/test/TestCompilerOnCharLit.kt @@ -24,11 +24,6 @@ import prog8.compiler.target.Cx16Target @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestCompilerOnCharLit { - @BeforeAll - fun setUp() { - sanityCheckDirectories("compiler") - } - @Test fun testCharLitAsRomsubArg() { val platform = Cx16Target diff --git a/compiler/test/TestCompilerOnExamples.kt b/compiler/test/TestCompilerOnExamples.kt index a98e2bade..e4d9a2254 100644 --- a/compiler/test/TestCompilerOnExamples.kt +++ b/compiler/test/TestCompilerOnExamples.kt @@ -23,20 +23,14 @@ import prog8.compiler.target.ICompilationTarget //@Disabled("to save some time") @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestCompilerOnExamples { - private val examplesDir = workingDir.resolve("../examples") - - @BeforeAll - fun setUp() { - sanityCheckDirectories("compiler") - assumeDirectory(examplesDir) - } + private val examplesDir = assumeDirectory(workingDir, "../examples") // TODO: make assembly stage testable - in case of failure (eg of 64tass) it Process.exit s private fun makeDynamicCompilerTest(name: String, platform: ICompilationTarget, optimize: Boolean) : DynamicTest { val searchIn = mutableListOf(examplesDir) if (platform == Cx16Target) { - searchIn.add(0, examplesDir.resolve("cx16")) + searchIn.add(0, assumeDirectory(examplesDir, "cx16")) } val filepath = searchIn.map { it.resolve("$name.p8") }.first { it.exists() } val displayName = "${examplesDir.relativize(filepath)}: ${platform.name}, optimize=$optimize" diff --git a/compiler/test/TestCompilerOnImportsAndIncludes.kt b/compiler/test/TestCompilerOnImportsAndIncludes.kt index d166f92d3..f08259c11 100644 --- a/compiler/test/TestCompilerOnImportsAndIncludes.kt +++ b/compiler/test/TestCompilerOnImportsAndIncludes.kt @@ -26,17 +26,10 @@ import prog8.compiler.target.Cx16Target @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestCompilerOnImportsAndIncludes { - @BeforeAll - fun setUp() { - sanityCheckDirectories("compiler") - } - @Test fun testImportFromSameFolder() { - val filepath = fixturesDir.resolve("importFromSameFolder.p8") - val imported = fixturesDir.resolve("foo_bar.p8") - assumeReadableFile(filepath) - assumeReadableFile(imported) + val filepath = assumeReadableFile(fixturesDir, "importFromSameFolder.p8") + assumeReadableFile(fixturesDir, "foo_bar.p8") val platform = Cx16Target val result = compileFile(platform, false, fixturesDir, filepath.name) @@ -57,10 +50,8 @@ class TestCompilerOnImportsAndIncludes { @Test fun testAsmIncludeFromSameFolder() { - val filepath = fixturesDir.resolve("asmIncludeFromSameFolder.p8") - val included = fixturesDir.resolve("foo_bar.asm") - assumeReadableFile(filepath) - assumeReadableFile(included) + val filepath = assumeReadableFile(fixturesDir, "asmIncludeFromSameFolder.p8") + assumeReadableFile(fixturesDir,"foo_bar.asm") val platform = Cx16Target val result = compileFile(platform, false, fixturesDir, filepath.name) @@ -84,10 +75,8 @@ class TestCompilerOnImportsAndIncludes { @Test fun testAsmbinaryDirectiveWithNonExistingFile() { - val p8Path = fixturesDir.resolve("asmbinaryNonExisting.p8") - val binPath = fixturesDir.resolve("i_do_not_exist.bin") - assumeReadableFile(p8Path) - assumeNotExists(binPath) + val p8Path = assumeReadableFile(fixturesDir, "asmbinaryNonExisting.p8") + assumeNotExists(fixturesDir,"i_do_not_exist.bin") compileFile(Cx16Target, false, p8Path.parent, p8Path.name, outputDir) .assertFailure() @@ -95,10 +84,8 @@ class TestCompilerOnImportsAndIncludes { @Test fun testAsmbinaryDirectiveWithNonReadableFile() { - val p8Path = fixturesDir.resolve("asmbinaryNonReadable.p8") - val binPath = fixturesDir.resolve("subFolder") - assumeReadableFile(p8Path) - assumeDirectory(binPath) + val p8Path = assumeReadableFile(fixturesDir, "asmbinaryNonReadable.p8") + assumeDirectory(fixturesDir, "subFolder") compileFile(Cx16Target, false, p8Path.parent, p8Path.name, outputDir) .assertFailure() @@ -111,8 +98,8 @@ class TestCompilerOnImportsAndIncludes { Triple("sub", "asmBinaryFromSubFolder.p8", "subFolder/do_nothing2.bin"), ).map { val (where, p8Str, binStr) = it - val p8Path = fixturesDir.resolve(p8Str) - val binPath = fixturesDir.resolve(binStr) + val p8Path = fixturesDir.div(p8Str) // sanity check below, *inside dynamicTest* + val binPath = fixturesDir.div(binStr) val displayName = "%asmbinary from ${where}folder" dynamicTest(displayName) { assumeReadableFile(p8Path) diff --git a/compiler/test/TestCompilerOnRanges.kt b/compiler/test/TestCompilerOnRanges.kt index c6c36cc78..5f5fafdcb 100644 --- a/compiler/test/TestCompilerOnRanges.kt +++ b/compiler/test/TestCompilerOnRanges.kt @@ -28,11 +28,6 @@ import prog8.compiler.target.Cx16Target @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestCompilerOnRanges { - @BeforeAll - fun setUp() { - sanityCheckDirectories("compiler") - } - @Test fun testUByteArrayInitializerWithRange_char_to_char() { val platform = Cx16Target @@ -91,7 +86,7 @@ class TestCompilerOnRanges { assertEquals(expectedEnd - expectedStart + 1, rhsValues.size, "rangeExpr.size()") } - inline fun Subroutine.decl(varName: String): VarDecl { + fun Subroutine.decl(varName: String): VarDecl { return statements.filterIsInstance() .first { it.name == varName } } @@ -231,7 +226,7 @@ class TestCompilerOnRanges { @Test fun testForLoopWithRange_str_downto_str() { - val result = compileText(Cx16Target, true, """ + compileText(Cx16Target, true, """ main { sub start() { ubyte i diff --git a/compilerAst/test/TestModuleImporter.kt b/compilerAst/test/TestModuleImporter.kt index e28a85bb0..0466e8123 100644 --- a/compilerAst/test/TestModuleImporter.kt +++ b/compilerAst/test/TestModuleImporter.kt @@ -20,9 +20,7 @@ class TestModuleImporter { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - - val srcPath = fixturesDir.resolve("i_do_not_exist") - assumeNotExists(srcPath) + val srcPath = assumeNotExists(fixturesDir, "i_do_not_exist") assertFailsWith { importer.importModule(srcPath) } } @@ -33,8 +31,7 @@ class TestModuleImporter { val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val srcPath = fixturesDir - assumeDirectory(srcPath) + val srcPath = assumeDirectory(fixturesDir) // fn importModule(Path) used to check *.isReadable()*, but NOT .isRegularFile(): assumeReadable(srcPath) @@ -49,7 +46,7 @@ class TestModuleImporter { val importer = ModuleImporter(program, "blah", listOf(searchIn)) val filename = "file_with_syntax_error.p8" - val path = fixturesDir.resolve(filename) + val path = assumeReadableFile(fixturesDir, filename) val act = { importer.importModule(path) } assertFailsWith { act() } @@ -69,10 +66,8 @@ class TestModuleImporter { val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val importing = fixturesDir.resolve("import_file_with_syntax_error.p8") - val imported = fixturesDir.resolve("file_with_syntax_error.p8") - assumeReadableFile(importing) - assumeReadableFile(imported) + val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8") + val imported = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8") val act = { importer.importModule(importing) } @@ -93,12 +88,8 @@ class TestModuleImporter { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val filenameNoExt = "i_do_not_exist" - val filenameWithExt = filenameNoExt + ".p8" - val srcPathNoExt = fixturesDir.resolve(filenameNoExt) - val srcPathWithExt = fixturesDir.resolve(filenameWithExt) - assumeNotExists(srcPathNoExt) - assumeNotExists(srcPathWithExt) + val filenameNoExt = assumeNotExists(fixturesDir, "i_do_not_exist").name + val filenameWithExt = assumeNotExists(fixturesDir, "i_do_not_exist.p8").name assertFailsWith { importer.importLibraryModule(filenameNoExt) } assertFailsWith { importer.importLibraryModule(filenameWithExt) } @@ -109,8 +100,7 @@ class TestModuleImporter { val program = Program("foo", mutableListOf(), DummyFunctions, DummyMemsizer) val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val srcPath = fixturesDir.resolve("file_with_syntax_error.p8") - assumeReadableFile(srcPath) + val srcPath = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8") val act = { importer.importLibraryModule(srcPath.nameWithoutExtension) } @@ -132,10 +122,8 @@ class TestModuleImporter { val searchIn = "./" + workingDir.relativize(fixturesDir).toString().replace("\\", "/") val importer = ModuleImporter(program, "blah", listOf(searchIn)) - val importing = fixturesDir.resolve("import_file_with_syntax_error.p8") - val imported = fixturesDir.resolve("file_with_syntax_error.p8") - assumeReadableFile(importing) - assumeReadableFile(imported) + val importing = assumeReadableFile(fixturesDir, "import_file_with_syntax_error.p8") + val imported = assumeReadableFile(fixturesDir,"file_with_syntax_error.p8") val act = { importer.importLibraryModule(importing.nameWithoutExtension) } diff --git a/compilerAst/test/TestProg8Parser.kt b/compilerAst/test/TestProg8Parser.kt index 622198561..55e7da561 100644 --- a/compilerAst/test/TestProg8Parser.kt +++ b/compilerAst/test/TestProg8Parser.kt @@ -1,11 +1,10 @@ package prog8tests +import prog8tests.helpers.* import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.Test import org.junit.jupiter.api.Disabled -import org.junit.jupiter.api.BeforeAll import kotlin.test.* -import prog8tests.helpers.* import kotlin.io.path.* import prog8.parser.ParseError @@ -20,11 +19,6 @@ import prog8.ast.expressions.* @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestProg8Parser { - @BeforeAll - fun setUp() { - sanityCheckDirectories("compilerAst") - } - @Test fun testModuleSourceNeedNotEndWithNewline() { val nl = "\n" // say, Unix-style (different flavours tested elsewhere) @@ -166,11 +160,8 @@ class TestProg8Parser { @Test fun parseModuleShouldNotLookAtImports() { - val importedNoExt = fixturesDir.resolve("i_do_not_exist") - val importedWithExt = fixturesDir.resolve("i_do_not_exist.p8") - assumeNotExists(importedNoExt) - assumeNotExists(importedWithExt) - + val importedNoExt = assumeNotExists(fixturesDir, "i_do_not_exist") + assumeNotExists(fixturesDir, "i_do_not_exist.p8") val text = "%import ${importedNoExt.name}" val module = parseModule(SourceCode.of(text)) @@ -186,9 +177,7 @@ class TestProg8Parser { @Test fun testParseModuleWithEmptyFile() { - val path = fixturesDir.resolve("empty.p8") - assumeReadableFile(path) - + val path = assumeReadableFile(fixturesDir,"empty.p8") val module = parseModule(SourceCode.fromPath(path)) assertEquals(0, module.statements.size) } @@ -207,10 +196,8 @@ class TestProg8Parser { @Test fun testModuleNameForSourceFromPath() { - val path = fixturesDir.resolve("simple_main.p8") - + val path = assumeReadableFile(fixturesDir,"simple_main.p8") val module = parseModule(SourceCode.fromPath(path)) - assertEquals(path.nameWithoutExtension, module.name) } @@ -253,7 +240,7 @@ class TestProg8Parser { @Test fun testErrorLocationForSourceFromPath() { - val path = fixturesDir.resolve("file_with_syntax_error.p8") + val path = assumeReadableFile(fixturesDir, "file_with_syntax_error.p8") assertFailsWith { parseModule(SourceCode.fromPath(path)) } try { @@ -275,7 +262,7 @@ class TestProg8Parser { @Test fun testModulePositionForSourceFromPath() { - val path = fixturesDir.resolve("simple_main.p8") + val path = assumeReadableFile(fixturesDir,"simple_main.p8") val module = parseModule(SourceCode.fromPath(path)) assertPositionOf(module, path.absolutePathString(), 1, 0) // TODO: endCol wrong @@ -283,7 +270,7 @@ class TestProg8Parser { @Test fun testInnerNodePositionsForSourceFromPath() { - val path = fixturesDir.resolve("simple_main.p8") + val path = assumeReadableFile(fixturesDir,"simple_main.p8") val module = parseModule(SourceCode.fromPath(path)) val mpf = module.position.file diff --git a/compilerAst/test/TestSourceCode.kt b/compilerAst/test/TestSourceCode.kt index 571f23eb2..13ce07fe1 100644 --- a/compilerAst/test/TestSourceCode.kt +++ b/compilerAst/test/TestSourceCode.kt @@ -1,11 +1,10 @@ package prog8tests +import prog8tests.helpers.* import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.Test -import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.* import kotlin.test.* -import prog8tests.helpers.* import kotlin.io.path.* import prog8.parser.SourceCode @@ -14,11 +13,6 @@ import prog8.parser.SourceCode @TestInstance(TestInstance.Lifecycle.PER_CLASS) class TestSourceCode { - @BeforeAll - fun setUp() { - sanityCheckDirectories("compilerAst") - } - @Test fun testFactoryMethod_Of() { val text = """ @@ -34,16 +28,14 @@ class TestSourceCode { @Test fun testFromPathWithNonExistingPath() { val filename = "i_do_not_exist.p8" - val path = fixturesDir.resolve(filename) - assumeNotExists(path) + val path = assumeNotExists(fixturesDir, filename) assertFailsWith { SourceCode.fromPath(path) } } @Test fun testFromPathWithMissingExtension_p8() { - val pathWithoutExt = fixturesDir.resolve("simple_main") - val pathWithExt = Path(pathWithoutExt.toString() + ".p8") - assumeReadableFile(pathWithExt) + val pathWithoutExt = assumeNotExists(fixturesDir,"simple_main") + assumeReadableFile(fixturesDir,"simple_main.p8") assertFailsWith { SourceCode.fromPath(pathWithoutExt) } } @@ -55,90 +47,75 @@ class TestSourceCode { @Test fun testFromPathWithExistingPath() { val filename = "simple_main.p8" - val path = fixturesDir.resolve(filename) + val path = assumeReadableFile(fixturesDir, filename) val src = SourceCode.fromPath(path) val expectedOrigin = path.normalize().absolutePathString() assertEquals(expectedOrigin, src.origin) - - val expectedSrcText = path.toFile().readText() - val actualSrcText = src.getCharStream().toString() - assertEquals(expectedSrcText, actualSrcText) + assertEquals(path.toFile().readText(), src.asString()) } @Test fun testFromPathWithExistingNonNormalizedPath() { val filename = "simple_main.p8" val path = Path(".", "test", "..", "test", "fixtures", filename) + val srcFile = assumeReadableFile(path).toFile() val src = SourceCode.fromPath(path) val expectedOrigin = path.normalize().absolutePathString() assertEquals(expectedOrigin, src.origin) - - val expectedSrcText = path.toFile().readText() - val actualSrcText = src.getCharStream().toString() - assertEquals(expectedSrcText, actualSrcText) + assertEquals(srcFile.readText(), src.asString()) } @Test fun testFromResourcesWithExistingP8File_withoutLeadingSlash() { val pathString = "prog8lib/math.p8" + val srcFile = assumeReadableFile(resourcesDir, pathString).toFile() val src = SourceCode.fromResources(pathString) assertEquals("@embedded@/$pathString", src.origin) - - val expectedSrcText = resourcesDir.resolve(pathString).toFile().readText() - val actualSrcText = src.asString() - assertEquals(expectedSrcText, actualSrcText) + assertEquals(srcFile.readText(), src.asString()) } @Test fun testFromResourcesWithExistingP8File_withLeadingSlash() { val pathString = "/prog8lib/math.p8" + val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.fromResources(pathString) assertEquals("@embedded@$pathString", src.origin) - - val expectedSrcText = resourcesDir.resolve(pathString.substring(1)).toFile().readText() - val actualSrcText = src.asString() - assertEquals(expectedSrcText, actualSrcText) + assertEquals(srcFile.readText(), src.asString()) } @Test fun testFromResourcesWithExistingAsmFile_withoutLeadingSlash() { val pathString = "prog8lib/math.asm" + val srcFile = assumeReadableFile(resourcesDir, pathString).toFile() val src = SourceCode.fromResources(pathString) assertEquals("@embedded@/$pathString", src.origin) - - val expectedSrcText = resourcesDir.resolve(pathString).toFile().readText() - val actualSrcText = src.asString() - assertEquals(expectedSrcText, actualSrcText) + assertEquals(srcFile.readText(), src.asString()) assertTrue(src.isFromResources, ".isFromResources") } @Test fun testFromResourcesWithExistingAsmFile_withLeadingSlash() { val pathString = "/prog8lib/math.asm" + val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.fromResources(pathString) assertEquals("@embedded@$pathString", src.origin) - - val expectedSrcText = resourcesDir.resolve(pathString.substring(1)).toFile().readText() - val actualSrcText = src.asString() - assertEquals(expectedSrcText, actualSrcText) + assertEquals(srcFile.readText(), src.asString()) } @Test fun testFromResourcesWithNonNormalizedPath() { val pathString = "/prog8lib/../prog8lib/math.p8" + val srcFile = assumeReadableFile(resourcesDir, pathString.substring(1)).toFile() val src = SourceCode.fromResources(pathString) assertEquals("@embedded@/prog8lib/math.p8", src.origin) - - val expectedSrcText = resourcesDir.resolve(pathString.substring(1)).toFile().readText() - val actualSrcText = src.asString() - assertEquals(expectedSrcText, actualSrcText) + assertEquals(srcFile.readText(), src.asString()) assertTrue(src.isFromResources, ".isFromResources") } @@ -146,15 +123,15 @@ class TestSourceCode { @Test fun testFromResourcesWithNonExistingFile_withLeadingSlash() { val pathString = "/prog8lib/i_do_not_exist" - val resPath = resourcesDir.resolve(pathString.substring(1)) - assumeNotExists(resPath) + assumeNotExists(resourcesDir, pathString.substring(1)) + assertThrows { SourceCode.fromResources(pathString) } } @Test fun testFromResourcesWithNonExistingFile_withoutLeadingSlash() { val pathString = "prog8lib/i_do_not_exist" - val resPath = resourcesDir.resolve(pathString) - assumeNotExists(resPath) + assumeNotExists(resourcesDir, pathString) + assertThrows { SourceCode.fromResources(pathString) } } @@ -162,8 +139,8 @@ class TestSourceCode { @Disabled("TODO: inside resources: cannot tell apart a folder from a file") fun testFromResourcesWithDirectory() { val pathString = "/prog8lib" + assumeDirectory(resourcesDir, pathString.substring(1)) assertThrows { SourceCode.fromResources(pathString) } } - } diff --git a/compilerAst/test/helpers/paths.kt b/compilerAst/test/helpers/paths.kt index 65c5de9fd..0e005cde5 100644 --- a/compilerAst/test/helpers/paths.kt +++ b/compilerAst/test/helpers/paths.kt @@ -6,33 +6,46 @@ import kotlin.io.path.* import java.nio.file.Path -val workingDir = Path("").absolute() // Note: Path(".") does NOT work..! -val fixturesDir = workingDir.resolve("test/fixtures") -val resourcesDir = workingDir.resolve("res") -val outputDir = workingDir.resolve("build/tmp/test") +val workingDir = assumeDirectory("").absolute() // Note: "." does NOT work..! +val fixturesDir = assumeDirectory(workingDir,"test/fixtures") +val resourcesDir = assumeDirectory(workingDir,"res") +val outputDir = assumeDirectory(workingDir, "build/tmp/test") -fun assumeReadable(path: Path) { - assertTrue(path.isReadable(), "sanity check: should be readable: ${path.absolute()}") -} - -fun assumeReadableFile(path: Path) { - assumeReadable(path) - assertTrue(path.isRegularFile(), "sanity check: should be normal file: ${path.absolute()}") -} - -fun assumeDirectory(path: Path) { - assertTrue(path.isDirectory(), "sanity check; should be directory: $path") -} - -fun assumeNotExists(path: Path) { +fun assumeNotExists(path: Path): Path { assertFalse(path.exists(), "sanity check: should not exist: ${path.absolute()}") + return path } -fun sanityCheckDirectories(workingDirName: String? = null) { - if (workingDirName != null) - assertEquals(workingDirName, workingDir.fileName.toString(), "name of current working dir") - assumeDirectory(workingDir) - assumeDirectory(fixturesDir) - assumeDirectory(resourcesDir) - assumeDirectory(outputDir) +fun assumeNotExists(pathStr: String): Path = assumeNotExists(Path(pathStr)) +fun assumeNotExists(path: Path, other: String): Path = assumeNotExists(path.div(other)) + +fun assumeReadable(path: Path): Path { + assertTrue(path.isReadable(), "sanity check: should be readable: ${path.absolute()}") + return path } + +fun assumeReadableFile(path: Path): Path { + assertTrue(path.isRegularFile(), "sanity check: should be normal file: ${path.absolute()}") + return assumeReadable(path) +} + +fun assumeReadableFile(pathStr: String): Path = assumeReadableFile(Path(pathStr)) +fun assumeReadableFile(pathStr: String, other: Path): Path = assumeReadableFile(Path(pathStr), other) +fun assumeReadableFile(pathStr: String, other: String): Path = assumeReadableFile(Path(pathStr), other) +fun assumeReadableFile(path: Path, other: String): Path = assumeReadableFile(path.div(other)) +fun assumeReadableFile(path: Path, other: Path): Path = assumeReadableFile(path.div(other)) + +fun assumeDirectory(path: Path): Path { + assertTrue(path.isDirectory(), "sanity check; should be directory: $path") + return path +} + +fun assumeDirectory(pathStr: String): Path = assumeDirectory(Path(pathStr)) +fun assumeDirectory(path: Path, other: String): Path = assumeDirectory(path.div(other)) + + +@Deprecated("Directories are checked automatically at init.", + ReplaceWith("/* nothing */")) +@Suppress("unused") +fun sanityCheckDirectories(workingDirName: String? = null) { +} \ No newline at end of file diff --git a/compilerAst/test/helpers_pathsTests.kt b/compilerAst/test/helpers_pathsTests.kt new file mode 100644 index 000000000..418946b97 --- /dev/null +++ b/compilerAst/test/helpers_pathsTests.kt @@ -0,0 +1,311 @@ +package prog8tests + +import prog8tests.helpers.* +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.* + +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.assertThrows + +import kotlin.io.path.* +import kotlin.test.assertContains + +// Do not move into folder helpers/! +// This folder is also used by compiler/test +// but the testing of the helpers themselves must be performed ONLY HERE. +// +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class PathsHelpersTests { + + @Nested + inner class AssumeNotExists { + + @Nested + inner class WithOnePathArg { + + @Test + fun testOnNonExistingPath() { + val path = fixturesDir.div("i_do_not_exist") + assertThat("should return the path", + assumeNotExists(path), `is`(path)) + } + + @Test + fun testOnExistingFile() { + assertThrows { + assumeNotExists(fixturesDir.div("simple_main.p8")) + } + } + + @Test + fun testOnExistingDirectory() { + assertThrows { + assumeNotExists(fixturesDir) + } + } + } + + @Nested + inner class WithOneStringArg { + + @Test + fun testOnNonExistingPath() { + val path = fixturesDir.div("i_do_not_exist") + assertThat("should return the path", + assumeNotExists("$path"), `is`(path)) + } + + @Test + fun testOnExistingFile() { + val path = fixturesDir.div("simple_main.p8") + assertThrows { + assumeNotExists("$path") + } + } + + @Test + fun testOnExistingDirectory() { + assertThrows { + assumeNotExists("$fixturesDir") + } + } + } + + @Nested + inner class WithPathAndStringArgs { + + @Test + fun testOnNonExistingPath() { + val path = fixturesDir.div("i_do_not_exist") + assertThat("should return the path", + assumeNotExists(fixturesDir, "i_do_not_exist"), `is`(path)) + } + + @Test + fun testOnExistingFile() { + assertThrows { + assumeNotExists(fixturesDir, "simple_main.p8") + } + } + + @Test + fun testOnExistingDirectory() { + assertThrows { + assumeNotExists(fixturesDir, "..") + } + } + } + } + + @Nested + inner class AssumeDirectory { + + @Nested + inner class WithOnePathArg { + @Test + fun testOnNonExistingPath() { + val path = fixturesDir.div("i_do_not_exist") + assertThrows { + assumeDirectory(path) + } + } + + @Test + fun testOnExistingFile() { + val path = fixturesDir.div("simple_main.p8") + assertThrows { + assumeDirectory(path) + } + } + + @Test + fun testOnExistingDirectory() { + val path = workingDir + assertThat("should return the path", assumeDirectory(path), `is`(path)) + } + } + + @Nested + inner class WithOneStringArg { + @Test + fun testOnNonExistingPath() { + val path = fixturesDir.div("i_do_not_exist") + assertThrows { + assumeDirectory("$path") + } + } + + @Test + fun testOnExistingFile() { + val path = fixturesDir.div("simple_main.p8") + assertThrows { + assumeDirectory("$path") + } + } + + @Test + fun testOnExistingDirectory() { + val path = workingDir + assertThat("should return the path", + assumeDirectory("$path"), `is`(path)) + } + } + + @Nested + inner class WithPathAndStringArgs { + @Test + fun testOnNonExistingPath() { + assertThrows { + assumeDirectory(fixturesDir, "i_do_not_exist") + } + } + + @Test + fun testOnExistingFile() { + assertThrows { + assumeDirectory(fixturesDir, "simple_main.p8") + } + } + + @Test + fun testOnExistingDirectory() { + val path = workingDir.div("..") + assertThat( + "should return resulting path", + assumeDirectory(workingDir, ".."), `is`(path) + ) + } + } + } + + @Nested + inner class AssumeReadableFile { + + @Nested + inner class WithOnePathArg { + + @Test + fun testOnNonExistingPath() { + val path = fixturesDir.div("i_do_not_exist") + assertThrows { + assumeReadableFile(path) + } + } + + @Test + fun testOnReadableFile() { + val path = fixturesDir.div("simple_main.p8") + assertThat("should return the path", + assumeReadableFile(path), `is`(path)) + } + + @Test + fun testOnDirectory() { + assertThrows { + assumeReadableFile(fixturesDir) + } + } + } + + @Nested + inner class WithOneStringArg { + + @Test + fun testOnNonExistingPath() { + val path = fixturesDir.div("i_do_not_exist") + assertThrows { + assumeReadableFile("$path") + } + } + + @Test + fun testOnReadableFile() { + val path = fixturesDir.div("simple_main.p8") + assertThat("should return the resulting path", + assumeReadableFile("$path"), `is`(path)) + } + + @Test + fun testOnDirectory() { + assertThrows { + assumeReadableFile("$fixturesDir") + } + } + } + + @Nested + inner class WithPathAndStringArgs { + @Test + fun testOnNonexistingPath() { + assertThrows { + assumeReadableFile(fixturesDir, "i_do_not_exist") + } + } + + @Test + fun testOnReadableFile() { + val path = fixturesDir.div("simple_main.p8") + assertThat("should return the resulting path", + assumeReadableFile(fixturesDir, "simple_main.p8"), `is`(path)) + } + + @Test + fun testOnDirectory() { + assertThrows { + assumeReadableFile(fixturesDir, "..") + } + } + } + + @Nested + inner class WithPathAndPathArgs { + @Test + fun testOnNonexistingPath() { + assertThrows { + assumeReadableFile(fixturesDir, Path("i_do_not_exist")) + } + } + + @Test fun testOnReadableFile() { + assertThat("should return the resulting path", + assumeReadableFile(fixturesDir, Path("simple_main.p8")), + `is`(fixturesDir.div("simple_main.p8")) + ) + } + + @Test + fun testOnDirectory() { + assertThrows { + assumeReadableFile(fixturesDir, Path("..")) + } + } + } + + @Nested + inner class WithStringAndStringArgs { + @Test + fun testOnNonexistingPath() { + assertThrows { + assumeReadableFile("$fixturesDir", "i_do_not_exist") + } + } + + @Test + fun testOnReadableFile() { + assertThat("should return the resulting path", + assumeReadableFile(fixturesDir.toString(), "simple_main.p8"), + `is`(fixturesDir.div("simple_main.p8")) + ) + } + + @Test + fun testOnDirectory() { + assertThrows { + assumeReadableFile("$fixturesDir", "..") + } + } + } + } +}