mirror of
https://github.com/irmen/prog8.git
synced 2024-12-02 07:49:27 +00:00
141 lines
5.0 KiB
Kotlin
141 lines
5.0 KiB
Kotlin
package prog8tests.ast
|
|
|
|
import io.kotest.assertions.throwables.shouldThrow
|
|
import io.kotest.core.spec.style.AnnotationSpec
|
|
import io.kotest.matchers.shouldBe
|
|
import io.kotest.matchers.string.shouldContain
|
|
import prog8.code.core.SourceCode
|
|
import prog8.code.core.SourceCode.Companion.libraryFilePrefix
|
|
import prog8tests.helpers.Helpers
|
|
import kotlin.io.path.Path
|
|
|
|
|
|
class TestSourceCode: AnnotationSpec() {
|
|
|
|
@Test
|
|
fun testFromString() {
|
|
val text = """
|
|
main { }
|
|
"""
|
|
val src = SourceCode.Text(text)
|
|
|
|
src.origin shouldContain Regex("^<String@[0-9a-f\\-]+>$")
|
|
src.text shouldBe text
|
|
src.isFromResources shouldBe false
|
|
src.isFromFilesystem shouldBe false
|
|
src.toString().startsWith("prog8.code.core.SourceCode") shouldBe true
|
|
}
|
|
|
|
@Test
|
|
fun testFromPathWithNonExistingPath() {
|
|
val filename = "i_do_not_exist.p8"
|
|
val path = Helpers.assumeNotExists(Helpers.fixturesDir, filename)
|
|
shouldThrow<NoSuchFileException> { SourceCode.File(path) }
|
|
}
|
|
|
|
@Test
|
|
fun testFromPathWithMissingExtension_p8() {
|
|
val pathWithoutExt = Helpers.assumeNotExists(Helpers.fixturesDir,"simple_main")
|
|
Helpers.assumeReadableFile(Helpers.fixturesDir,"ast_simple_main.p8")
|
|
shouldThrow<NoSuchFileException> { SourceCode.File(pathWithoutExt) }
|
|
}
|
|
|
|
@Test
|
|
fun testFromPathWithDirectory() {
|
|
shouldThrow<FileSystemException> { SourceCode.File(Helpers.fixturesDir) }
|
|
}
|
|
|
|
@Test
|
|
fun testFromPathWithExistingPath() {
|
|
val filename = "ast_simple_main.p8"
|
|
val path = Helpers.assumeReadableFile(Helpers.fixturesDir, filename)
|
|
val src = SourceCode.File(path)
|
|
val expectedOrigin = SourceCode.relative(path).toString()
|
|
src.origin shouldBe expectedOrigin
|
|
src.text shouldBe path.toFile().readText()
|
|
src.isFromResources shouldBe false
|
|
src.isFromFilesystem shouldBe true
|
|
}
|
|
|
|
@Test
|
|
fun testFromPathWithExistingNonNormalizedPath() {
|
|
val filename = "ast_simple_main.p8"
|
|
val path = Path(".", "test", "..", "test", "fixtures", filename)
|
|
val srcFile = Helpers.assumeReadableFile(path).toFile()
|
|
val src = SourceCode.File(path)
|
|
val expectedOrigin = SourceCode.relative(path).toString()
|
|
src.origin shouldBe expectedOrigin
|
|
src.text shouldBe srcFile.readText()
|
|
}
|
|
|
|
@Test
|
|
fun testFromResourcesWithExistingP8File_withoutLeadingSlash() {
|
|
val pathString = "prog8lib/math.p8"
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString).toFile()
|
|
val src = SourceCode.Resource(pathString)
|
|
|
|
src.origin shouldBe "$libraryFilePrefix/$pathString"
|
|
src.text shouldBe srcFile.readText()
|
|
src.isFromResources shouldBe true
|
|
src.isFromFilesystem shouldBe false
|
|
}
|
|
|
|
@Test
|
|
fun testFromResourcesWithExistingP8File_withLeadingSlash() {
|
|
val pathString = "/prog8lib/math.p8"
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString.substring(1)).toFile()
|
|
val src = SourceCode.Resource(pathString)
|
|
|
|
src.origin shouldBe "$libraryFilePrefix$pathString"
|
|
src.text shouldBe srcFile.readText()
|
|
}
|
|
|
|
@Test
|
|
fun testFromResourcesWithExistingAsmFile_withoutLeadingSlash() {
|
|
val pathString = "prog8lib/math.asm"
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString).toFile()
|
|
val src = SourceCode.Resource(pathString)
|
|
|
|
src.origin shouldBe "$libraryFilePrefix/$pathString"
|
|
src.text shouldBe srcFile.readText()
|
|
src.isFromResources shouldBe true
|
|
}
|
|
|
|
@Test
|
|
fun testFromResourcesWithExistingAsmFile_withLeadingSlash() {
|
|
val pathString = "/prog8lib/math.asm"
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString.substring(1)).toFile()
|
|
val src = SourceCode.Resource(pathString)
|
|
|
|
src.origin shouldBe "$libraryFilePrefix$pathString"
|
|
src.text shouldBe srcFile.readText()
|
|
}
|
|
|
|
@Test
|
|
fun testFromResourcesWithNonNormalizedPath() {
|
|
val pathString = "/prog8lib/../prog8lib/math.p8"
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString.substring(1)).toFile()
|
|
val src = SourceCode.Resource(pathString)
|
|
|
|
src.origin shouldBe "$libraryFilePrefix/prog8lib/math.p8"
|
|
src.text shouldBe srcFile.readText()
|
|
src.isFromResources shouldBe true
|
|
}
|
|
|
|
|
|
@Test
|
|
fun testFromResourcesWithNonExistingFile_withLeadingSlash() {
|
|
val pathString = "/prog8lib/i_do_not_exist"
|
|
Helpers.assumeNotExists(Helpers.resourcesDir, pathString.substring(1))
|
|
|
|
shouldThrow<NoSuchFileException> { SourceCode.Resource(pathString) }
|
|
}
|
|
@Test
|
|
fun testFromResourcesWithNonExistingFile_withoutLeadingSlash() {
|
|
val pathString = "prog8lib/i_do_not_exist"
|
|
Helpers.assumeNotExists(Helpers.resourcesDir, pathString)
|
|
|
|
shouldThrow<NoSuchFileException> { SourceCode.Resource(pathString) }
|
|
}
|
|
}
|