2021-07-17 08:37:58 +00:00
|
|
|
package prog8tests
|
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.assertions.withClue
|
2021-11-07 23:16:58 +00:00
|
|
|
import io.kotest.core.spec.style.FunSpec
|
2021-11-08 14:50:29 +00:00
|
|
|
import io.kotest.matchers.shouldBe
|
|
|
|
import io.kotest.matchers.shouldNotBe
|
2021-07-17 08:37:58 +00:00
|
|
|
import prog8.ast.expressions.AddressOf
|
|
|
|
import prog8.ast.expressions.IdentifierReference
|
2022-02-10 23:21:40 +00:00
|
|
|
import prog8.ast.expressions.StringLiteral
|
2021-07-17 08:37:58 +00:00
|
|
|
import prog8.ast.statements.FunctionCallStatement
|
|
|
|
import prog8.ast.statements.Label
|
2022-03-11 19:35:25 +00:00
|
|
|
import prog8.code.target.Cx16Target
|
2021-12-04 17:20:22 +00:00
|
|
|
import prog8tests.helpers.*
|
2021-10-10 22:22:04 +00:00
|
|
|
import kotlin.io.path.name
|
2021-07-17 08:37:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ATTENTION: this is just kludge!
|
|
|
|
* They are not really unit tests, but rather tests of the whole process,
|
|
|
|
* from source file loading all the way through to running 64tass.
|
|
|
|
*/
|
2021-11-07 23:16:58 +00:00
|
|
|
class TestCompilerOnImportsAndIncludes: FunSpec({
|
2021-07-17 08:37:58 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("Import") {
|
2021-08-02 06:57:09 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testImportFromSameFolder") {
|
2021-08-02 06:57:09 +00:00
|
|
|
val filepath = assumeReadableFile(fixturesDir, "importFromSameFolder.p8")
|
|
|
|
assumeReadableFile(fixturesDir, "foo_bar.p8")
|
|
|
|
|
2022-02-06 20:29:06 +00:00
|
|
|
val platform = Cx16Target()
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileFile(platform, optimize = false, fixturesDir, filepath.name)!!
|
2021-08-02 06:57:09 +00:00
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-08-02 06:57:09 +00:00
|
|
|
val strLits = startSub.statements
|
|
|
|
.filterIsInstance<FunctionCallStatement>()
|
|
|
|
.map { it.args[0] as IdentifierReference }
|
2022-02-10 23:21:40 +00:00
|
|
|
.map { it.targetVarDecl(program)!!.value as StringLiteral }
|
2021-08-02 06:57:09 +00:00
|
|
|
|
2021-11-08 14:50:29 +00:00
|
|
|
strLits[0].value shouldBe "main.bar"
|
|
|
|
strLits[1].value shouldBe "foo.bar"
|
|
|
|
strLits[0].definingScope.name shouldBe "main"
|
|
|
|
strLits[1].definingScope.name shouldBe "foo"
|
2021-08-02 06:57:09 +00:00
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("AsmInclude") {
|
|
|
|
test("testAsmIncludeFromSameFolder") {
|
2021-08-02 06:57:09 +00:00
|
|
|
val filepath = assumeReadableFile(fixturesDir, "asmIncludeFromSameFolder.p8")
|
|
|
|
assumeReadableFile(fixturesDir, "foo_bar.asm")
|
|
|
|
|
2022-02-06 20:29:06 +00:00
|
|
|
val platform = Cx16Target()
|
2022-03-07 20:41:12 +00:00
|
|
|
val result = compileFile(platform, optimize = false, fixturesDir, filepath.name)!!
|
2021-08-02 06:57:09 +00:00
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val program = result.program
|
2021-10-10 22:01:26 +00:00
|
|
|
val startSub = program.entrypoint
|
2021-08-02 06:57:09 +00:00
|
|
|
val args = startSub.statements
|
|
|
|
.filterIsInstance<FunctionCallStatement>()
|
|
|
|
.map { it.args[0] }
|
|
|
|
|
2022-02-10 23:21:40 +00:00
|
|
|
val str0 = (args[0] as IdentifierReference).targetVarDecl(program)!!.value as StringLiteral
|
2021-11-08 14:50:29 +00:00
|
|
|
str0.value shouldBe "main.bar"
|
|
|
|
str0.definingScope.name shouldBe "main"
|
2021-08-02 06:57:09 +00:00
|
|
|
|
|
|
|
val id1 = (args[1] as AddressOf).identifier
|
|
|
|
val lbl1 = id1.targetStatement(program) as Label
|
2021-11-08 14:50:29 +00:00
|
|
|
lbl1.name shouldBe "foo_bar"
|
2022-02-14 23:25:18 +00:00
|
|
|
lbl1.definingScope.name shouldBe "main"
|
2021-08-02 06:57:09 +00:00
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
context("Asmbinary") {
|
|
|
|
test("testAsmbinaryDirectiveWithNonExistingFile") {
|
2021-10-09 15:59:40 +00:00
|
|
|
val p8Path = assumeReadableFile(fixturesDir, "asmBinaryNonExisting.p8")
|
2021-08-02 06:57:09 +00:00
|
|
|
assumeNotExists(fixturesDir, "i_do_not_exist.bin")
|
2021-07-17 08:37:58 +00:00
|
|
|
|
2022-03-07 20:41:12 +00:00
|
|
|
compileFile(Cx16Target(), false, p8Path.parent, p8Path.name, outputDir) shouldBe null
|
2021-08-02 06:57:09 +00:00
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
test("testAsmbinaryDirectiveWithNonReadableFile") {
|
2021-10-09 15:59:40 +00:00
|
|
|
val p8Path = assumeReadableFile(fixturesDir, "asmBinaryNonReadable.p8")
|
2021-08-02 06:57:09 +00:00
|
|
|
assumeDirectory(fixturesDir, "subFolder")
|
2021-07-17 08:37:58 +00:00
|
|
|
|
2022-03-07 20:41:12 +00:00
|
|
|
compileFile(Cx16Target(), false, p8Path.parent, p8Path.name, outputDir) shouldBe null
|
2021-08-02 06:57:09 +00:00
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
val tests = listOf(
|
2021-08-02 06:57:09 +00:00
|
|
|
Triple("same ", "asmBinaryFromSameFolder.p8", "do_nothing1.bin"),
|
|
|
|
Triple("sub", "asmBinaryFromSubFolder.p8", "subFolder/do_nothing2.bin"),
|
2021-11-07 23:16:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
tests.forEach {
|
2022-01-10 00:48:25 +00:00
|
|
|
val (where, p8Str, _) = it
|
2021-11-07 23:16:58 +00:00
|
|
|
test("%asmbinary from ${where}folder") {
|
|
|
|
val p8Path = assumeReadableFile(fixturesDir, p8Str)
|
|
|
|
// val binPath = assumeReadableFile(fixturesDir, binStr)
|
2021-11-08 14:50:29 +00:00
|
|
|
|
|
|
|
// the bug we're testing for (#54) was hidden if outputDir == workingDir
|
|
|
|
withClue("sanity check: workingDir and outputDir should not be the same folder") {
|
|
|
|
outputDir.normalize().toAbsolutePath() shouldNotBe workingDir.normalize().toAbsolutePath()
|
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
|
2022-03-07 20:41:12 +00:00
|
|
|
withClue("argument to assembler directive .binary " +
|
|
|
|
"should be relative to the generated .asm file (in output dir), " +
|
|
|
|
"NOT relative to .p8 neither current working dir") {
|
|
|
|
compileFile(Cx16Target(), false, p8Path.parent, p8Path.name, outputDir) shouldNotBe null
|
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
}
|
2021-11-07 23:16:58 +00:00
|
|
|
}
|
2021-07-17 08:37:58 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
})
|