prog8/compilerAst/test/TestAstToSourceText.kt

116 lines
3.4 KiB
Kotlin
Raw Normal View History

package prog8tests.ast
import org.junit.jupiter.api.Test
2021-10-10 22:22:04 +00:00
import org.junit.jupiter.api.TestInstance
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
import prog8.ast.internedStringsModuleName
import prog8.parser.ParseError
2021-10-10 22:22:04 +00:00
import prog8.parser.Prog8Parser.parseModule
import prog8.parser.SourceCode
import prog8tests.ast.helpers.DummyFunctions
import prog8tests.ast.helpers.DummyMemsizer
2021-10-10 22:22:04 +00:00
import kotlin.test.assertContains
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2021-10-28 23:06:01 +00:00
class TestAstToSourceText {
private fun generateP8(module: Module) : String {
val program = Program("test", DummyFunctions, DummyMemsizer)
.addModule(module)
var generatedText = ""
2021-10-28 23:06:01 +00:00
val it = AstToSourceTextConverter({ str -> generatedText += str }, program)
it.visit(program)
return generatedText
}
private fun roundTrip(module: Module): Pair<String, Module> {
val generatedText = generateP8(module)
try {
2021-10-13 16:55:56 +00:00
val parsedAgain = parseModule(SourceCode.Text(generatedText))
return Pair(generatedText, parsedAgain)
} catch (e: ParseError) {
assert(false) { "should produce valid Prog8 but threw $e" }
throw e
}
}
@Test
fun testMentionsInternedStringsModule() {
2021-10-13 16:55:56 +00:00
val orig = SourceCode.Text("\n")
val (txt, _) = roundTrip(parseModule(orig))
// assertContains has *actual* first!
assertContains(txt, Regex(";.*$internedStringsModuleName"))
}
@Test
fun testImportDirectiveWithLib() {
2021-10-13 16:55:56 +00:00
val orig = SourceCode.Text("%import textio\n")
val (txt, _) = roundTrip(parseModule(orig))
// assertContains has *actual* first!
assertContains(txt, Regex("%import +textio"))
}
@Test
fun testImportDirectiveWithUserModule() {
2021-10-13 16:55:56 +00:00
val orig = SourceCode.Text("%import my_own_stuff\n")
val (txt, _) = roundTrip(parseModule(orig))
// assertContains has *actual* first!
assertContains(txt, Regex("%import +my_own_stuff"))
}
@Test
fun testStringLiteral_noAlt() {
2021-10-13 16:55:56 +00:00
val orig = SourceCode.Text("""
main {
str s = "fooBar\n"
}
""")
val (txt, _) = roundTrip(parseModule(orig))
// assertContains has *actual* first!
assertContains(txt, Regex("str +s += +\"fooBar\\\\n\""))
}
@Test
fun testStringLiteral_withAlt() {
2021-10-13 16:55:56 +00:00
val orig = SourceCode.Text("""
main {
str sAlt = @"fooBar\n"
}
""")
val (txt, _) = roundTrip(parseModule(orig))
// assertContains has *actual* first!
assertContains(txt, Regex("str +sAlt += +@\"fooBar\\\\n\""))
}
@Test
fun testCharLiteral_noAlt() {
2021-10-13 16:55:56 +00:00
val orig = SourceCode.Text("""
main {
ubyte c = 'x'
}
""")
val (txt, _) = roundTrip(parseModule(orig))
// assertContains has *actual* first!
assertContains(txt, Regex("ubyte +c += +'x'"), "char literal")
}
@Test
fun testCharLiteral_withAlt() {
2021-10-13 16:55:56 +00:00
val orig = SourceCode.Text("""
main {
ubyte cAlt = @'x'
}
""")
val (txt, _) = roundTrip(parseModule(orig))
// assertContains has *actual* first!
assertContains(txt, Regex("ubyte +cAlt += +@'x'"), "alt char literal")
}
}