2021-06-21 10:02:36 +00:00
|
|
|
package prog8tests
|
|
|
|
|
2021-10-10 22:22:04 +00:00
|
|
|
import org.junit.jupiter.api.Disabled
|
2021-07-11 17:04:53 +00:00
|
|
|
import org.junit.jupiter.api.Test
|
2021-10-10 22:22:04 +00:00
|
|
|
import org.junit.jupiter.api.TestInstance
|
|
|
|
import org.junit.jupiter.api.assertThrows
|
2021-06-21 10:02:36 +00:00
|
|
|
import prog8.parser.SourceCode
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8tests.helpers.*
|
|
|
|
import kotlin.io.path.Path
|
|
|
|
import kotlin.io.path.absolutePathString
|
|
|
|
import kotlin.test.assertContains
|
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertFailsWith
|
|
|
|
import kotlin.test.assertTrue
|
2021-06-21 10:02:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|
|
|
class TestSourceCode {
|
2021-07-10 18:55:23 +00:00
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
@Test
|
|
|
|
fun testFactoryMethod_Of() {
|
|
|
|
val text = """
|
|
|
|
main { }
|
|
|
|
""".trimIndent()
|
|
|
|
val src = SourceCode.of(text)
|
|
|
|
val actualText = src.getCharStream().toString()
|
|
|
|
|
|
|
|
assertContains(src.origin, Regex("^<String@[0-9a-f]+>$"))
|
|
|
|
assertEquals(text, actualText)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithNonExistingPath() {
|
|
|
|
val filename = "i_do_not_exist.p8"
|
2021-07-31 12:44:02 +00:00
|
|
|
val path = assumeNotExists(fixturesDir, filename)
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<NoSuchFileException> { SourceCode.fromPath(path) }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithMissingExtension_p8() {
|
2021-07-31 12:44:02 +00:00
|
|
|
val pathWithoutExt = assumeNotExists(fixturesDir,"simple_main")
|
|
|
|
assumeReadableFile(fixturesDir,"simple_main.p8")
|
2021-06-21 10:02:36 +00:00
|
|
|
assertFailsWith<NoSuchFileException> { SourceCode.fromPath(pathWithoutExt) }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testFromPathWithDirectory() {
|
2021-07-10 18:55:23 +00:00
|
|
|
assertFailsWith<AccessDeniedException> { SourceCode.fromPath(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-06-21 10:02:36 +00:00
|
|
|
val src = SourceCode.fromPath(path)
|
|
|
|
|
|
|
|
val expectedOrigin = path.normalize().absolutePathString()
|
|
|
|
assertEquals(expectedOrigin, src.origin)
|
2021-07-31 12:44:02 +00:00
|
|
|
assertEquals(path.toFile().readText(), src.asString())
|
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-06-21 10:02:36 +00:00
|
|
|
val src = SourceCode.fromPath(path)
|
|
|
|
|
|
|
|
val expectedOrigin = path.normalize().absolutePathString()
|
|
|
|
assertEquals(expectedOrigin, src.origin)
|
2021-07-31 12:44:02 +00:00
|
|
|
assertEquals(srcFile.readText(), src.asString())
|
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-07-10 18:55:23 +00:00
|
|
|
val src = SourceCode.fromResources(pathString)
|
|
|
|
|
|
|
|
assertEquals("@embedded@/$pathString", src.origin)
|
2021-07-31 12:44:02 +00:00
|
|
|
assertEquals(srcFile.readText(), src.asString())
|
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-07-10 18:55:23 +00:00
|
|
|
val src = SourceCode.fromResources(pathString)
|
|
|
|
|
|
|
|
assertEquals("@embedded@$pathString", src.origin)
|
2021-07-31 12:44:02 +00:00
|
|
|
assertEquals(srcFile.readText(), src.asString())
|
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-07-10 18:55:23 +00:00
|
|
|
val src = SourceCode.fromResources(pathString)
|
|
|
|
|
|
|
|
assertEquals("@embedded@/$pathString", src.origin)
|
2021-07-31 12:44:02 +00:00
|
|
|
assertEquals(srcFile.readText(), src.asString())
|
2021-07-10 18:55:23 +00:00
|
|
|
assertTrue(src.isFromResources, ".isFromResources")
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-07-10 18:55:23 +00:00
|
|
|
val src = SourceCode.fromResources(pathString)
|
|
|
|
|
|
|
|
assertEquals("@embedded@$pathString", src.origin)
|
2021-07-31 12:44:02 +00:00
|
|
|
assertEquals(srcFile.readText(), src.asString())
|
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-07-10 18:55:23 +00:00
|
|
|
val src = SourceCode.fromResources(pathString)
|
|
|
|
|
|
|
|
assertEquals("@embedded@/prog8lib/math.p8", src.origin)
|
2021-07-31 12:44:02 +00:00
|
|
|
assertEquals(srcFile.readText(), src.asString())
|
2021-07-10 18:55:23 +00:00
|
|
|
assertTrue(src.isFromResources, ".isFromResources")
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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-07-10 18:55:23 +00:00
|
|
|
assertThrows<NoSuchFileException> { SourceCode.fromResources(pathString) }
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun testFromResourcesWithNonExistingFile_withoutLeadingSlash() {
|
|
|
|
val pathString = "prog8lib/i_do_not_exist"
|
2021-07-31 12:44:02 +00:00
|
|
|
assumeNotExists(resourcesDir, pathString)
|
|
|
|
|
2021-07-10 18:55:23 +00:00
|
|
|
assertThrows<NoSuchFileException> { SourceCode.fromResources(pathString) }
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:04:53 +00:00
|
|
|
@Test
|
|
|
|
@Disabled("TODO: inside resources: cannot tell apart a folder from a file")
|
2021-07-10 18:55:23 +00:00
|
|
|
fun testFromResourcesWithDirectory() {
|
|
|
|
val pathString = "/prog8lib"
|
2021-07-31 12:44:02 +00:00
|
|
|
assumeDirectory(resourcesDir, pathString.substring(1))
|
2021-07-10 18:55:23 +00:00
|
|
|
assertThrows<AccessDeniedException> { SourceCode.fromResources(pathString) }
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:02:36 +00:00
|
|
|
}
|