2021-10-29 03:00:30 +00:00
|
|
|
package prog8tests.ast.ast
|
2021-08-01 20:47:11 +00:00
|
|
|
|
2021-10-10 22:22:04 +00:00
|
|
|
|
2021-08-01 20:47:11 +00:00
|
|
|
import org.hamcrest.MatcherAssert.assertThat
|
|
|
|
import org.hamcrest.Matchers.containsString
|
|
|
|
import org.hamcrest.Matchers.equalTo
|
2021-10-10 22:22:04 +00:00
|
|
|
import org.junit.jupiter.api.Nested
|
2021-08-01 20:47:11 +00:00
|
|
|
import org.junit.jupiter.api.Test
|
2021-10-10 22:22:04 +00:00
|
|
|
import org.junit.jupiter.api.TestInstance
|
2021-08-01 20:47:11 +00:00
|
|
|
import prog8.ast.Module
|
2021-10-10 22:22:04 +00:00
|
|
|
import prog8.ast.Program
|
2021-08-01 20:47:11 +00:00
|
|
|
import prog8.ast.base.Position
|
|
|
|
import prog8.ast.internedStringsModuleName
|
2021-10-13 16:55:56 +00:00
|
|
|
import prog8.parser.SourceCode
|
2021-10-29 03:00:30 +00:00
|
|
|
import prog8tests.ast.helpers.DummyFunctions
|
|
|
|
import prog8tests.ast.helpers.DummyMemsizer
|
2021-08-01 20:47:11 +00:00
|
|
|
import kotlin.test.assertContains
|
2021-10-11 19:20:57 +00:00
|
|
|
import kotlin.test.assertFailsWith
|
2021-08-01 20:47:11 +00:00
|
|
|
import kotlin.test.assertSame
|
|
|
|
|
|
|
|
|
|
|
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
|
|
|
class ProgramTests {
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
inner class Constructor {
|
|
|
|
@Test
|
|
|
|
fun withNameBuiltinsAndMemsizer() {
|
|
|
|
val program = Program("foo", DummyFunctions, DummyMemsizer)
|
|
|
|
assertThat(program.modules.size, equalTo(1))
|
|
|
|
assertThat(program.modules[0].name, equalTo(internedStringsModuleName))
|
|
|
|
assertSame(program, program.modules[0].program)
|
|
|
|
assertSame(program.namespace, program.modules[0].parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
inner class AddModule {
|
|
|
|
@Test
|
|
|
|
fun withEmptyModule() {
|
|
|
|
val program = Program("foo", DummyFunctions, DummyMemsizer)
|
2021-10-14 21:56:23 +00:00
|
|
|
val m1 = Module(mutableListOf(), Position.DUMMY, SourceCode.Generated("bar"))
|
2021-08-01 20:47:11 +00:00
|
|
|
|
|
|
|
val retVal = program.addModule(m1)
|
|
|
|
|
|
|
|
assertSame(program, retVal)
|
|
|
|
assertThat(program.modules.size, equalTo(2))
|
|
|
|
assertContains(program.modules, m1)
|
|
|
|
assertSame(program, m1.program)
|
|
|
|
assertSame(program.namespace, m1.parent)
|
|
|
|
|
2021-10-19 20:12:54 +00:00
|
|
|
assertThat("module may not occur multiple times",
|
|
|
|
assertFailsWith<IllegalArgumentException> { program.addModule(m1) }.message, containsString(m1.name))
|
2021-08-01 20:47:11 +00:00
|
|
|
|
2021-10-14 21:56:23 +00:00
|
|
|
val m2 = Module(mutableListOf(), m1.position, m1.source)
|
2021-10-19 20:12:54 +00:00
|
|
|
assertThat("other module but with same name may not occur multiple times",
|
|
|
|
assertFailsWith<IllegalArgumentException> { program.addModule(m2) }.message, containsString(m2.name))
|
2021-08-01 20:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
inner class MoveModuleToFront {
|
|
|
|
@Test
|
|
|
|
fun withInternedStringsModule() {
|
|
|
|
val program = Program("foo", DummyFunctions, DummyMemsizer)
|
|
|
|
val m = program.modules[0]
|
|
|
|
assertThat(m.name, equalTo(internedStringsModuleName))
|
|
|
|
|
|
|
|
val retVal = program.moveModuleToFront(m)
|
|
|
|
assertSame(program, retVal)
|
|
|
|
assertSame(m, program.modules[0])
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun withForeignModule() {
|
|
|
|
val program = Program("foo", DummyFunctions, DummyMemsizer)
|
2021-10-14 21:56:23 +00:00
|
|
|
val m = Module(mutableListOf(), Position.DUMMY, SourceCode.Generated("bar"))
|
2021-08-01 20:47:11 +00:00
|
|
|
|
2021-10-11 19:20:57 +00:00
|
|
|
assertFailsWith<IllegalArgumentException> { program.moveModuleToFront(m) }
|
2021-08-01 20:47:11 +00:00
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun withFirstOfPreviouslyAddedModules() {
|
|
|
|
val program = Program("foo", DummyFunctions, DummyMemsizer)
|
2021-10-14 21:56:23 +00:00
|
|
|
val m1 = Module(mutableListOf(), Position.DUMMY, SourceCode.Generated("bar"))
|
|
|
|
val m2 = Module(mutableListOf(), Position.DUMMY, SourceCode.Generated("qmbl"))
|
2021-08-01 20:47:11 +00:00
|
|
|
program.addModule(m1)
|
|
|
|
program.addModule(m2)
|
|
|
|
|
|
|
|
val retVal = program.moveModuleToFront(m1)
|
|
|
|
assertSame(program, retVal)
|
|
|
|
assertThat(program.modules.indexOf(m1), equalTo(0))
|
|
|
|
}
|
|
|
|
@Test
|
|
|
|
fun withSecondOfPreviouslyAddedModules() {
|
|
|
|
val program = Program("foo", DummyFunctions, DummyMemsizer)
|
2021-10-14 21:56:23 +00:00
|
|
|
val m1 = Module(mutableListOf(), Position.DUMMY, SourceCode.Generated("bar"))
|
|
|
|
val m2 = Module(mutableListOf(), Position.DUMMY, SourceCode.Generated("qmbl"))
|
2021-08-01 20:47:11 +00:00
|
|
|
program.addModule(m1)
|
|
|
|
program.addModule(m2)
|
|
|
|
|
|
|
|
val retVal = program.moveModuleToFront(m2)
|
|
|
|
assertSame(program, retVal)
|
|
|
|
assertThat(program.modules.indexOf(m2), equalTo(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nested
|
|
|
|
inner class Properties {
|
|
|
|
@Test
|
|
|
|
fun modules() {
|
|
|
|
val program = Program("foo", DummyFunctions, DummyMemsizer)
|
|
|
|
|
|
|
|
val ms1 = program.modules
|
|
|
|
val ms2 = program.modules
|
|
|
|
assertSame(ms1, ms2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|