2021-10-29 03:00:30 +00:00
|
|
|
package prog8tests.ast
|
2021-07-11 10:53:01 +00:00
|
|
|
|
2021-11-07 23:16:58 +00:00
|
|
|
import io.kotest.assertions.fail
|
2021-11-07 16:25:53 +00:00
|
|
|
import io.kotest.core.spec.style.AnnotationSpec
|
|
|
|
import io.kotest.matchers.string.shouldContain
|
2021-10-28 23:06:01 +00:00
|
|
|
import prog8.ast.AstToSourceTextConverter
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8.ast.Module
|
|
|
|
import prog8.ast.Program
|
2021-07-11 10:53:01 +00:00
|
|
|
import prog8.parser.ParseError
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8.parser.Prog8Parser.parseModule
|
2022-03-21 00:01:21 +00:00
|
|
|
import prog8.code.core.SourceCode
|
|
|
|
import prog8.code.core.internedStringsModuleName
|
2021-12-04 17:20:22 +00:00
|
|
|
import prog8tests.helpers.DummyFunctions
|
|
|
|
import prog8tests.helpers.DummyMemsizer
|
|
|
|
import prog8tests.helpers.DummyStringEncoder
|
2021-07-11 10:53:01 +00:00
|
|
|
|
|
|
|
|
2021-11-07 16:25:53 +00:00
|
|
|
class TestAstToSourceText: AnnotationSpec() {
|
2021-07-11 10:53:01 +00:00
|
|
|
|
2021-07-11 17:04:53 +00:00
|
|
|
private fun generateP8(module: Module) : String {
|
2021-10-29 22:05:55 +00:00
|
|
|
val program = Program("test", DummyFunctions, DummyMemsizer, DummyStringEncoder)
|
2021-08-01 20:47:11 +00:00
|
|
|
.addModule(module)
|
2021-07-11 10:53:01 +00:00
|
|
|
|
|
|
|
var generatedText = ""
|
2021-10-28 23:06:01 +00:00
|
|
|
val it = AstToSourceTextConverter({ str -> generatedText += str }, program)
|
2021-07-11 10:53:01 +00:00
|
|
|
it.visit(program)
|
|
|
|
|
|
|
|
return generatedText
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:04:53 +00:00
|
|
|
private fun roundTrip(module: Module): Pair<String, Module> {
|
2021-07-11 10:53:01 +00:00
|
|
|
val generatedText = generateP8(module)
|
|
|
|
try {
|
2021-10-13 16:55:56 +00:00
|
|
|
val parsedAgain = parseModule(SourceCode.Text(generatedText))
|
2021-07-11 10:53:01 +00:00
|
|
|
return Pair(generatedText, parsedAgain)
|
|
|
|
} catch (e: ParseError) {
|
2021-11-07 23:16:58 +00:00
|
|
|
fail("should produce valid Prog8 but threw $e")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testMentionsInternedStringsModule() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val orig = SourceCode.Text("\n")
|
2021-07-11 17:04:53 +00:00
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
2021-11-07 16:25:53 +00:00
|
|
|
txt shouldContain Regex(";.*$internedStringsModuleName")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testImportDirectiveWithLib() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val orig = SourceCode.Text("%import textio\n")
|
2021-07-11 17:04:53 +00:00
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
2021-11-07 16:25:53 +00:00
|
|
|
txt shouldContain Regex("%import +textio")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testImportDirectiveWithUserModule() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val orig = SourceCode.Text("%import my_own_stuff\n")
|
2021-07-11 17:04:53 +00:00
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
2021-11-07 16:25:53 +00:00
|
|
|
txt shouldContain Regex("%import +my_own_stuff")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
2022-01-19 00:08:24 +00:00
|
|
|
fun testStringLiteral_DefaultEnc() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val orig = SourceCode.Text("""
|
2021-07-11 10:53:01 +00:00
|
|
|
main {
|
|
|
|
str s = "fooBar\n"
|
|
|
|
}
|
|
|
|
""")
|
2021-07-11 17:04:53 +00:00
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
2022-02-21 22:38:53 +00:00
|
|
|
txt shouldContain Regex("str +s += +\"fooBar\\\\n\"")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-01-19 00:08:24 +00:00
|
|
|
fun testStringLiteral_withSc() {
|
|
|
|
val orig = SourceCode.Text("""
|
|
|
|
main {
|
|
|
|
str sAlt = sc:"fooBar\n"
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
|
|
|
txt shouldContain Regex("str +sAlt += +sc:\"fooBar\\\\n\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testStringLiteral_withIso() {
|
|
|
|
val orig = SourceCode.Text("""
|
|
|
|
main {
|
|
|
|
str sAlt = iso:"fooBar\n"
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
|
|
|
txt shouldContain Regex("str +sAlt += +iso:\"fooBar\\\\n\"")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2022-01-19 00:08:24 +00:00
|
|
|
fun testCharLiteral_defaultEnc() {
|
2021-10-13 16:55:56 +00:00
|
|
|
val orig = SourceCode.Text("""
|
2021-07-11 10:53:01 +00:00
|
|
|
main {
|
|
|
|
ubyte c = 'x'
|
|
|
|
}
|
|
|
|
""")
|
2021-07-11 17:04:53 +00:00
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
2022-02-21 22:38:53 +00:00
|
|
|
txt shouldContain Regex("ubyte +c += +'x'")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 00:08:24 +00:00
|
|
|
@Test
|
|
|
|
fun testCharLiteral_Sc() {
|
|
|
|
val orig = SourceCode.Text("""
|
|
|
|
main {
|
|
|
|
ubyte cAlt = sc:'x'
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
|
|
|
txt shouldContain Regex("ubyte +cAlt += +sc:'x'")
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 01:29:55 +00:00
|
|
|
@Test
|
|
|
|
fun testVar_withZpAndShared() {
|
|
|
|
val orig = SourceCode.Text("""
|
|
|
|
main {
|
|
|
|
ubyte @shared @zp qq
|
|
|
|
}
|
|
|
|
""")
|
|
|
|
val (txt, _) = roundTrip(parseModule(orig))
|
|
|
|
txt shouldContain Regex("ubyte +@zp +@shared +qq")
|
|
|
|
}
|
|
|
|
|
2021-07-11 10:53:01 +00:00
|
|
|
}
|