2021-10-29 03:00:30 +00:00
|
|
|
package prog8tests.ast
|
2021-06-21 10:02:36 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
import io.kotest.assertions.throwables.shouldThrow
|
2021-11-07 16:25:53 +00:00
|
|
|
import io.kotest.core.spec.style.AnnotationSpec
|
2021-11-07 20:18:18 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
|
|
|
import io.kotest.matchers.string.shouldContain
|
2022-03-21 00:01:21 +00:00
|
|
|
import prog8.code.core.SourceCode
|
|
|
|
import prog8.code.core.SourceCode.Companion.libraryFilePrefix
|
2022-04-15 20:38:32 +00:00
|
|
|
import prog8tests.helpers.Helpers
|
2021-10-10 22:22:04 +00:00
|
|
|
import kotlin.io.path.Path
|
2021-06-21 10:02:36 +00:00
|
|
|
|
|
|
|
|
2021-11-07 16:25:53 +00:00
|
|
|
class TestSourceCode: AnnotationSpec() {
|
2021-07-10 18:55:23 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
@Test
|
2021-10-18 22:26:02 +00:00
|
|
|
fun testFromString() {
|
2021-06-21 10:02:36 +00:00
|
|
|
val text = """
|
|
|
|
main { }
|
2021-10-18 22:26:02 +00:00
|
|
|
"""
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Text(text)
|
2021-06-21 10:02:36 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldContain Regex("^<String@[0-9a-f\\-]+>$")
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe text
|
2021-11-07 20:18:18 +00:00
|
|
|
src.isFromResources shouldBe false
|
|
|
|
src.isFromFilesystem shouldBe false
|
2022-03-21 00:01:21 +00:00
|
|
|
src.toString().startsWith("prog8.code.core.SourceCode") shouldBe true
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithNonExistingPath() {
|
|
|
|
val filename = "i_do_not_exist.p8"
|
2022-04-15 20:38:32 +00:00
|
|
|
val path = Helpers.assumeNotExists(Helpers.fixturesDir, filename)
|
2021-11-07 20:18:18 +00:00
|
|
|
shouldThrow<NoSuchFileException> { SourceCode.File(path) }
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithMissingExtension_p8() {
|
2022-04-15 20:38:32 +00:00
|
|
|
val pathWithoutExt = Helpers.assumeNotExists(Helpers.fixturesDir,"simple_main")
|
|
|
|
Helpers.assumeReadableFile(Helpers.fixturesDir,"ast_simple_main.p8")
|
2021-11-07 20:18:18 +00:00
|
|
|
shouldThrow<NoSuchFileException> { SourceCode.File(pathWithoutExt) }
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithDirectory() {
|
2022-04-15 20:38:32 +00:00
|
|
|
shouldThrow<FileSystemException> { SourceCode.File(Helpers.fixturesDir) }
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithExistingPath() {
|
2021-12-04 17:20:22 +00:00
|
|
|
val filename = "ast_simple_main.p8"
|
2022-04-15 20:38:32 +00:00
|
|
|
val path = Helpers.assumeReadableFile(Helpers.fixturesDir, filename)
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.File(path)
|
2021-10-16 12:26:33 +00:00
|
|
|
val expectedOrigin = SourceCode.relative(path).toString()
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldBe expectedOrigin
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe path.toFile().readText()
|
2021-11-07 20:18:18 +00:00
|
|
|
src.isFromResources shouldBe false
|
|
|
|
src.isFromFilesystem shouldBe true
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithExistingNonNormalizedPath() {
|
2021-12-04 17:20:22 +00:00
|
|
|
val filename = "ast_simple_main.p8"
|
2021-07-10 18:55:23 +00:00
|
|
|
val path = Path(".", "test", "..", "test", "fixtures", filename)
|
2022-04-15 20:38:32 +00:00
|
|
|
val srcFile = Helpers.assumeReadableFile(path).toFile()
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.File(path)
|
2021-10-16 12:26:33 +00:00
|
|
|
val expectedOrigin = SourceCode.relative(path).toString()
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldBe expectedOrigin
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe srcFile.readText()
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 18:55:23 +00:00
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithExistingP8File_withoutLeadingSlash() {
|
|
|
|
val pathString = "prog8lib/math.p8"
|
2022-04-15 20:38:32 +00:00
|
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString).toFile()
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Resource(pathString)
|
2021-07-10 18:55:23 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldBe "$libraryFilePrefix/$pathString"
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe srcFile.readText()
|
2021-11-07 20:18:18 +00:00
|
|
|
src.isFromResources shouldBe true
|
|
|
|
src.isFromFilesystem shouldBe false
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithExistingP8File_withLeadingSlash() {
|
|
|
|
val pathString = "/prog8lib/math.p8"
|
2022-04-15 20:38:32 +00:00
|
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString.substring(1)).toFile()
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Resource(pathString)
|
2021-07-10 18:55:23 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldBe "$libraryFilePrefix$pathString"
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe srcFile.readText()
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithExistingAsmFile_withoutLeadingSlash() {
|
|
|
|
val pathString = "prog8lib/math.asm"
|
2022-04-15 20:38:32 +00:00
|
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString).toFile()
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Resource(pathString)
|
2021-07-10 18:55:23 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldBe "$libraryFilePrefix/$pathString"
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe srcFile.readText()
|
2021-11-07 20:18:18 +00:00
|
|
|
src.isFromResources shouldBe true
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithExistingAsmFile_withLeadingSlash() {
|
|
|
|
val pathString = "/prog8lib/math.asm"
|
2022-04-15 20:38:32 +00:00
|
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString.substring(1)).toFile()
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Resource(pathString)
|
2021-07-10 18:55:23 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldBe "$libraryFilePrefix$pathString"
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe srcFile.readText()
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithNonNormalizedPath() {
|
|
|
|
val pathString = "/prog8lib/../prog8lib/math.p8"
|
2022-04-15 20:38:32 +00:00
|
|
|
val srcFile = Helpers.assumeReadableFile(Helpers.resourcesDir, pathString.substring(1)).toFile()
|
2021-10-13 16:55:56 +00:00
|
|
|
val src = SourceCode.Resource(pathString)
|
2021-07-10 18:55:23 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldBe "$libraryFilePrefix/prog8lib/math.p8"
|
2022-02-05 02:50:54 +00:00
|
|
|
src.text shouldBe srcFile.readText()
|
2021-11-07 20:18:18 +00:00
|
|
|
src.isFromResources shouldBe true
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithNonExistingFile_withLeadingSlash() {
|
|
|
|
val pathString = "/prog8lib/i_do_not_exist"
|
2022-04-15 20:38:32 +00:00
|
|
|
Helpers.assumeNotExists(Helpers.resourcesDir, pathString.substring(1))
|
2021-07-31 12:44:02 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
shouldThrow<NoSuchFileException> { SourceCode.Resource(pathString) }
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithNonExistingFile_withoutLeadingSlash() {
|
|
|
|
val pathString = "prog8lib/i_do_not_exist"
|
2022-04-15 20:38:32 +00:00
|
|
|
Helpers.assumeNotExists(Helpers.resourcesDir, pathString)
|
2021-07-31 12:44:02 +00:00
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
shouldThrow<NoSuchFileException> { SourceCode.Resource(pathString) }
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|