mirror of
https://github.com/irmen/prog8.git
synced 2024-10-18 16:24:26 +00:00
+ add tests for SourceCode.fromResources; refactor tests
This commit is contained in:
parent
c3e9d4a9f8
commit
ddaef3e5d5
@ -2,6 +2,9 @@ package prog8tests
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
import kotlin.io.path.*
|
||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
import prog8.ast.IFunctionCall
|
import prog8.ast.IFunctionCall
|
||||||
import prog8.ast.base.DataType
|
import prog8.ast.base.DataType
|
||||||
import prog8.ast.base.VarDeclType
|
import prog8.ast.base.VarDeclType
|
||||||
@ -9,12 +12,6 @@ import prog8.ast.expressions.IdentifierReference
|
|||||||
import prog8.ast.expressions.NumericLiteralValue
|
import prog8.ast.expressions.NumericLiteralValue
|
||||||
import prog8.compiler.compileProgram
|
import prog8.compiler.compileProgram
|
||||||
import prog8.compiler.target.Cx16Target
|
import prog8.compiler.target.Cx16Target
|
||||||
import kotlin.io.path.Path
|
|
||||||
import kotlin.io.path.absolute
|
|
||||||
import kotlin.io.path.isDirectory
|
|
||||||
import kotlin.test.assertEquals
|
|
||||||
import kotlin.test.assertIs
|
|
||||||
import kotlin.test.assertTrue
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,7 +26,7 @@ class TestCompilerOnCharLit {
|
|||||||
val outputDir = workingDir.resolve("build/tmp/test")
|
val outputDir = workingDir.resolve("build/tmp/test")
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testDirectoriesSanityCheck() {
|
fun sanityCheckDirectories() {
|
||||||
assertEquals("compiler", workingDir.fileName.toString())
|
assertEquals("compiler", workingDir.fileName.toString())
|
||||||
assertTrue(fixturesDir.isDirectory(), "sanity check; should be directory: $fixturesDir")
|
assertTrue(fixturesDir.isDirectory(), "sanity check; should be directory: $fixturesDir")
|
||||||
assertTrue(outputDir.isDirectory(), "sanity check; should be directory: $outputDir")
|
assertTrue(outputDir.isDirectory(), "sanity check; should be directory: $outputDir")
|
||||||
|
@ -2,8 +2,8 @@ package prog8tests
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
import org.junit.jupiter.api.assertThrows
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
import java.nio.file.Path // TODO: use kotlin.io.path.Path instead
|
|
||||||
import kotlin.io.path.*
|
import kotlin.io.path.*
|
||||||
|
|
||||||
import prog8.parser.SourceCode
|
import prog8.parser.SourceCode
|
||||||
@ -11,6 +11,19 @@ import prog8.parser.SourceCode
|
|||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
class TestSourceCode {
|
class TestSourceCode {
|
||||||
|
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")
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun sanityCheckDirectories() {
|
||||||
|
assertEquals("compilerAst", workingDir.fileName.toString())
|
||||||
|
assertTrue(workingDir.isDirectory(), "sanity check; should be directory: $workingDir")
|
||||||
|
assertTrue(fixturesDir.isDirectory(), "sanity check; should be directory: $fixturesDir")
|
||||||
|
assertTrue(resourcesDir.isDirectory(), "sanity check; should be directory: $resourcesDir")
|
||||||
|
assertTrue(outputDir.isDirectory(), "sanity check; should be directory: $outputDir")
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFactoryMethod_Of() {
|
fun testFactoryMethod_Of() {
|
||||||
@ -27,7 +40,7 @@ class TestSourceCode {
|
|||||||
@Test
|
@Test
|
||||||
fun testFromPathWithNonExistingPath() {
|
fun testFromPathWithNonExistingPath() {
|
||||||
val filename = "i_do_not_exist.p8"
|
val filename = "i_do_not_exist.p8"
|
||||||
val path = Path.of("test", "fixtures", filename)
|
val path = fixturesDir.resolve(filename)
|
||||||
|
|
||||||
assertFalse(path.exists(), "sanity check: file should not exist: ${path.absolute()}")
|
assertFalse(path.exists(), "sanity check: file should not exist: ${path.absolute()}")
|
||||||
assertFailsWith<NoSuchFileException> { SourceCode.fromPath(path) }
|
assertFailsWith<NoSuchFileException> { SourceCode.fromPath(path) }
|
||||||
@ -35,8 +48,8 @@ class TestSourceCode {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFromPathWithMissingExtension_p8() {
|
fun testFromPathWithMissingExtension_p8() {
|
||||||
val pathWithoutExt = Path.of("test", "fixtures", "simple_main")
|
val pathWithoutExt = fixturesDir.resolve("simple_main")
|
||||||
val pathWithExt = Path.of(pathWithoutExt.toString() + ".p8")
|
val pathWithExt = Path(pathWithoutExt.toString() + ".p8")
|
||||||
|
|
||||||
assertTrue(pathWithExt.isRegularFile(), "sanity check: should be normal file: ${pathWithExt.absolute()}")
|
assertTrue(pathWithExt.isRegularFile(), "sanity check: should be normal file: ${pathWithExt.absolute()}")
|
||||||
assertTrue(pathWithExt.isReadable(), "sanity check: should be readable: ${pathWithExt.absolute()}")
|
assertTrue(pathWithExt.isReadable(), "sanity check: should be readable: ${pathWithExt.absolute()}")
|
||||||
@ -45,16 +58,13 @@ class TestSourceCode {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFromPathWithDirectory() {
|
fun testFromPathWithDirectory() {
|
||||||
val path = Path.of("test", "fixtures")
|
assertFailsWith<AccessDeniedException> { SourceCode.fromPath(fixturesDir) }
|
||||||
|
|
||||||
assertTrue(path.isDirectory(), "sanity check: should be a directory")
|
|
||||||
assertFailsWith<AccessDeniedException> { SourceCode.fromPath(path) }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testFromPathWithExistingPath() {
|
fun testFromPathWithExistingPath() {
|
||||||
val filename = "simple_main.p8"
|
val filename = "simple_main.p8"
|
||||||
val path = Path.of("test", "fixtures", filename)
|
val path = fixturesDir.resolve(filename)
|
||||||
val src = SourceCode.fromPath(path)
|
val src = SourceCode.fromPath(path)
|
||||||
|
|
||||||
val expectedOrigin = path.normalize().absolutePathString()
|
val expectedOrigin = path.normalize().absolutePathString()
|
||||||
@ -68,7 +78,7 @@ class TestSourceCode {
|
|||||||
@Test
|
@Test
|
||||||
fun testFromPathWithExistingNonNormalizedPath() {
|
fun testFromPathWithExistingNonNormalizedPath() {
|
||||||
val filename = "simple_main.p8"
|
val filename = "simple_main.p8"
|
||||||
val path = Path.of(".", "test", "..", "test", "fixtures", filename)
|
val path = Path(".", "test", "..", "test", "fixtures", filename)
|
||||||
val src = SourceCode.fromPath(path)
|
val src = SourceCode.fromPath(path)
|
||||||
|
|
||||||
val expectedOrigin = path.normalize().absolutePathString()
|
val expectedOrigin = path.normalize().absolutePathString()
|
||||||
@ -79,4 +89,92 @@ class TestSourceCode {
|
|||||||
assertEquals(expectedSrcText, actualSrcText)
|
assertEquals(expectedSrcText, actualSrcText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFromResourcesWithExistingP8File_withoutLeadingSlash() {
|
||||||
|
val pathString = "prog8lib/math.p8"
|
||||||
|
val src = SourceCode.fromResources(pathString)
|
||||||
|
|
||||||
|
assertEquals("@embedded@/$pathString", src.origin)
|
||||||
|
|
||||||
|
val expectedSrcText = resourcesDir.resolve(pathString).toFile().readText()
|
||||||
|
val actualSrcText = src.asString()
|
||||||
|
assertEquals(expectedSrcText, actualSrcText)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFromResourcesWithExistingP8File_withLeadingSlash() {
|
||||||
|
val pathString = "/prog8lib/math.p8"
|
||||||
|
val src = SourceCode.fromResources(pathString)
|
||||||
|
|
||||||
|
assertEquals("@embedded@$pathString", src.origin)
|
||||||
|
|
||||||
|
val expectedSrcText = resourcesDir.resolve(pathString.substringAfter("/")).toFile().readText()
|
||||||
|
val actualSrcText = src.asString()
|
||||||
|
assertEquals(expectedSrcText, actualSrcText)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFromResourcesWithExistingAsmFile_withoutLeadingSlash() {
|
||||||
|
val pathString = "prog8lib/math.asm"
|
||||||
|
val src = SourceCode.fromResources(pathString)
|
||||||
|
|
||||||
|
assertEquals("@embedded@/$pathString", src.origin)
|
||||||
|
|
||||||
|
val expectedSrcText = resourcesDir.resolve(pathString).toFile().readText()
|
||||||
|
val actualSrcText = src.asString()
|
||||||
|
assertEquals(expectedSrcText, actualSrcText)
|
||||||
|
assertTrue(src.isFromResources, ".isFromResources")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFromResourcesWithExistingAsmFile_withLeadingSlash() {
|
||||||
|
val pathString = "/prog8lib/math.asm"
|
||||||
|
val src = SourceCode.fromResources(pathString)
|
||||||
|
|
||||||
|
assertEquals("@embedded@$pathString", src.origin)
|
||||||
|
|
||||||
|
val expectedSrcText = resourcesDir.resolve(pathString.substringAfter("/")).toFile().readText()
|
||||||
|
val actualSrcText = src.asString()
|
||||||
|
assertEquals(expectedSrcText, actualSrcText)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFromResourcesWithNonNormalizedPath() {
|
||||||
|
val pathString = "/prog8lib/../prog8lib/math.p8"
|
||||||
|
val src = SourceCode.fromResources(pathString)
|
||||||
|
|
||||||
|
assertEquals("@embedded@/prog8lib/math.p8", src.origin)
|
||||||
|
|
||||||
|
val expectedSrcText = Path( "res", pathString).toFile().readText()
|
||||||
|
val actualSrcText = src.asString()
|
||||||
|
assertEquals(expectedSrcText, actualSrcText)
|
||||||
|
assertTrue(src.isFromResources, ".isFromResources")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testFromResourcesWithNonExistingFile_withLeadingSlash() {
|
||||||
|
val pathString = "/prog8lib/i_do_not_exist"
|
||||||
|
val resPath = resourcesDir.resolve(pathString.substringAfter("/"))
|
||||||
|
assertFalse(resPath.exists(), "sanity check: should not exist: $resPath")
|
||||||
|
assertThrows<NoSuchFileException> { SourceCode.fromResources(pathString) }
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
fun testFromResourcesWithNonExistingFile_withoutLeadingSlash() {
|
||||||
|
val pathString = "prog8lib/i_do_not_exist"
|
||||||
|
val resPath = resourcesDir.resolve(pathString)
|
||||||
|
assertFalse(resPath.exists(), "sanity check: should not exist: $resPath")
|
||||||
|
assertThrows<NoSuchFileException> { SourceCode.fromResources(pathString) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO("inside resources: cannot tell apart a folder from a file")
|
||||||
|
*/
|
||||||
|
//@Test
|
||||||
|
fun testFromResourcesWithDirectory() {
|
||||||
|
val pathString = "/prog8lib"
|
||||||
|
assertThrows<AccessDeniedException> { SourceCode.fromResources(pathString) }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user