2021-09-12 16:16:24 +00:00
|
|
|
package prog8tests
|
|
|
|
|
2021-10-10 22:22:04 +00:00
|
|
|
import org.junit.jupiter.api.Test
|
|
|
|
import org.junit.jupiter.api.TestInstance
|
2021-09-12 16:16:24 +00:00
|
|
|
import prog8.ast.internedStringsModuleName
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8.compiler.determineCompilationOptions
|
|
|
|
import prog8.compiler.parseImports
|
2021-09-12 16:16:24 +00:00
|
|
|
import prog8.compiler.target.C64Target
|
2021-10-29 03:28:02 +00:00
|
|
|
import prog8.compilerinterface.ZeropageType
|
2021-10-29 03:00:30 +00:00
|
|
|
import prog8tests.ast.helpers.outputDir
|
2021-10-29 14:46:56 +00:00
|
|
|
import prog8tests.helpers.ErrorReporterForTests
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8tests.helpers.assertSuccess
|
|
|
|
import prog8tests.helpers.compileText
|
2021-09-12 16:16:24 +00:00
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertTrue
|
|
|
|
|
|
|
|
|
|
|
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|
|
|
class TestImportedModulesOrderAndOptions {
|
|
|
|
|
|
|
|
@Test
|
2021-10-19 19:49:05 +00:00
|
|
|
fun testImportedModuleOrderAndMainModuleCorrect() {
|
2021-09-12 16:16:24 +00:00
|
|
|
val result = compileText(C64Target, false, """
|
|
|
|
%import textio
|
|
|
|
%import floats
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
; nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
2021-10-29 22:25:34 +00:00
|
|
|
assertTrue(result.program.toplevelModule.name.startsWith("on_the_fly_test"))
|
2021-09-12 16:16:24 +00:00
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
val moduleNames = result.program.modules.map { it.name }
|
2021-09-12 16:16:24 +00:00
|
|
|
assertTrue(moduleNames[0].startsWith("on_the_fly_test"), "main module must be first")
|
|
|
|
assertEquals(listOf(
|
|
|
|
"prog8_interned_strings",
|
|
|
|
"textio",
|
|
|
|
"syslib",
|
|
|
|
"conv",
|
|
|
|
"floats",
|
|
|
|
"math",
|
|
|
|
"prog8_lib"
|
|
|
|
), moduleNames.drop(1), "module order in parse tree")
|
2021-10-19 19:49:05 +00:00
|
|
|
|
2021-10-29 22:25:34 +00:00
|
|
|
assertTrue(result.program.toplevelModule.name.startsWith("on_the_fly_test"))
|
2021-09-12 16:16:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testCompilationOptionsCorrectFromMain() {
|
|
|
|
val result = compileText(C64Target, false, """
|
|
|
|
%import textio
|
|
|
|
%import floats
|
|
|
|
%zeropage dontuse
|
|
|
|
%option no_sysinit
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
; nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
""").assertSuccess()
|
2021-10-29 22:25:34 +00:00
|
|
|
assertTrue(result.program.toplevelModule.name.startsWith("on_the_fly_test"))
|
|
|
|
val options = determineCompilationOptions(result.program, C64Target)
|
2021-09-12 16:16:24 +00:00
|
|
|
assertTrue(options.floats)
|
|
|
|
assertEquals(ZeropageType.DONTUSE, options.zeropage)
|
|
|
|
assertTrue(options.noSysInit)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testModuleOrderAndCompilationOptionsCorrectWithJustImports() {
|
2021-10-29 14:46:56 +00:00
|
|
|
val errors = ErrorReporterForTests()
|
2021-09-12 16:16:24 +00:00
|
|
|
val sourceText = """
|
|
|
|
%import textio
|
|
|
|
%import floats
|
|
|
|
%option no_sysinit
|
|
|
|
%zeropage dontuse
|
|
|
|
|
|
|
|
main {
|
|
|
|
sub start() {
|
|
|
|
; nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val filenameBase = "on_the_fly_test_" + sourceText.hashCode().toUInt().toString(16)
|
|
|
|
val filepath = outputDir.resolve("$filenameBase.p8")
|
|
|
|
filepath.toFile().writeText(sourceText)
|
|
|
|
val (program, options, importedfiles) = parseImports(filepath, errors, C64Target, emptyList())
|
|
|
|
|
2021-10-19 19:49:05 +00:00
|
|
|
assertEquals(filenameBase, program.toplevelModule.name)
|
2021-09-12 16:16:24 +00:00
|
|
|
assertEquals(1, importedfiles.size, "all imports other than the test source must have been internal resources library files")
|
|
|
|
assertEquals(listOf(
|
|
|
|
internedStringsModuleName,
|
|
|
|
filenameBase,
|
|
|
|
"textio", "syslib", "conv", "floats", "math", "prog8_lib"
|
|
|
|
), program.modules.map {it.name}, "module order in parse tree")
|
|
|
|
assertTrue(options.floats)
|
|
|
|
assertEquals(ZeropageType.DONTUSE, options.zeropage, "zeropage option must be correctly taken from main module, not from float module import logic")
|
|
|
|
assertTrue(options.noSysInit)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|