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
|
2021-06-21 10:02:36 +00:00
|
|
|
import prog8.parser.SourceCode
|
2021-10-11 23:19:19 +00:00
|
|
|
import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
2021-10-29 03:00:30 +00:00
|
|
|
import prog8tests.ast.helpers.assumeNotExists
|
|
|
|
import prog8tests.ast.helpers.assumeReadableFile
|
|
|
|
import prog8tests.ast.helpers.fixturesDir
|
|
|
|
import prog8tests.ast.helpers.resourcesDir
|
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
|
|
|
val actualText = src.getCharStream().toString()
|
|
|
|
|
2021-11-07 20:18:18 +00:00
|
|
|
src.origin shouldContain Regex("^<String@[0-9a-f\\-]+>$")
|
|
|
|
actualText shouldBe text
|
|
|
|
src.isFromResources shouldBe false
|
|
|
|
src.isFromFilesystem shouldBe false
|
|
|
|
src.toString().startsWith("prog8.parser.SourceCode") shouldBe true
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithNonExistingPath() {
|
|
|
|
val filename = "i_do_not_exist.p8"
|
2021-07-31 12:44:02 +00:00
|
|
|
val path = assumeNotExists(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() {
|
2021-07-31 12:44:02 +00:00
|
|
|
val pathWithoutExt = assumeNotExists(fixturesDir,"simple_main")
|
|
|
|
assumeReadableFile(fixturesDir,"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() {
|
2021-11-07 20:18:18 +00:00
|
|
|
shouldThrow<AccessDeniedException> { SourceCode.File(fixturesDir) }
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithExistingPath() {
|
|
|
|
val filename = "simple_main.p8"
|
2021-07-31 12:44:02 +00:00
|
|
|
val path = assumeReadableFile(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
|
|
|
|
src.readText() shouldBe path.toFile().readText()
|
|
|
|
src.isFromResources shouldBe false
|
|
|
|
src.isFromFilesystem shouldBe true
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithExistingNonNormalizedPath() {
|
|
|
|
val filename = "simple_main.p8"
|
2021-07-10 18:55:23 +00:00
|
|
|
val path = Path(".", "test", "..", "test", "fixtures", filename)
|
2021-07-31 12:44:02 +00:00
|
|
|
val srcFile = 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
|
|
|
|
src.readText() 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"
|
2021-07-31 12:44:02 +00:00
|
|
|
val srcFile = assumeReadableFile(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"
|
|
|
|
src.readText() shouldBe srcFile.readText()
|
|
|
|
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"
|
2021-07-31 12:44:02 +00:00
|
|
|
val srcFile = assumeReadableFile(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"
|
|
|
|
src.readText() shouldBe srcFile.readText()
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithExistingAsmFile_withoutLeadingSlash() {
|
|
|
|
val pathString = "prog8lib/math.asm"
|
2021-07-31 12:44:02 +00:00
|
|
|
val srcFile = assumeReadableFile(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"
|
|
|
|
src.readText() shouldBe srcFile.readText()
|
|
|
|
src.isFromResources shouldBe true
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithExistingAsmFile_withLeadingSlash() {
|
|
|
|
val pathString = "/prog8lib/math.asm"
|
2021-07-31 12:44:02 +00:00
|
|
|
val srcFile = assumeReadableFile(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"
|
|
|
|
src.readText() shouldBe srcFile.readText()
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithNonNormalizedPath() {
|
|
|
|
val pathString = "/prog8lib/../prog8lib/math.p8"
|
2021-07-31 12:44:02 +00:00
|
|
|
val srcFile = assumeReadableFile(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"
|
|
|
|
src.readText() shouldBe srcFile.readText()
|
|
|
|
src.isFromResources shouldBe true
|
2021-07-10 18:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithNonExistingFile_withLeadingSlash() {
|
|
|
|
val pathString = "/prog8lib/i_do_not_exist"
|
2021-07-31 12:44:02 +00:00
|
|
|
assumeNotExists(resourcesDir, pathString.substring(1))
|
|
|
|
|
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"
|
2021-07-31 12:44:02 +00:00
|
|
|
assumeNotExists(resourcesDir, pathString)
|
|
|
|
|
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
|
|
|
}
|