K2 Scripting: add support for imported scripts

This commit is contained in:
Ilya Chernikov
2023-06-19 10:13:03 +02:00
committed by Space Team
parent d24fc3b581
commit 96bde033e1
21 changed files with 306 additions and 38 deletions
@@ -42,7 +42,7 @@ class CachingTest : TestCase() {
}
@Test
fun testSimpleImportWithMemoryCache() = expectTestToFailOnK2 {
fun testSimpleImportWithMemoryCache() {
val cache = SimpleMemoryScriptsCache()
checkWithCache(
cache, scriptWithImport, scriptWithImportExpectedOutput,
@@ -62,7 +62,7 @@ class CachingTest : TestCase() {
}
@Test
fun testSimpleImportWithFileCache() = expectTestToFailOnK2 {
fun testSimpleImportWithFileCache() {
withTempDir("scriptingTestCache") { cacheDir ->
val cache = FileBasedScriptCache(cacheDir)
Assert.assertEquals(true, cache.baseDir.listFiles()?.isEmpty())
@@ -89,7 +89,7 @@ class CachingTest : TestCase() {
}
@Test
fun testSimpleImportWithJarCache() = expectTestToFailOnK2 {
fun testSimpleImportWithJarCache() {
withTempDir("scriptingTestJarCache") { cacheDir ->
val cache = TestCompiledScriptJarsCache(cacheDir)
Assert.assertTrue(cache.baseDir.listFiles()!!.isEmpty())
@@ -191,7 +191,7 @@ class ScriptingHostTest : TestCase() {
}
@Test
fun testSimpleImport() = expectTestToFailOnK2 {
fun testSimpleImport() {
val greeting = listOf("Hello from helloWithVal script!", "Hello from imported helloWithVal script!")
val script = "println(\"Hello from imported \$helloScriptName script!\")"
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate> {
@@ -204,7 +204,7 @@ class ScriptingHostTest : TestCase() {
}
@Test
fun testSimpleImportWithImplicitReceiver() = expectTestToFailOnK2 {
fun testSimpleImportWithImplicitReceiver() {
val greeting = listOf("Hello from helloWithVal script!", "Hello from imported helloWithVal script!")
val script = "println(\"Hello from imported \$helloScriptName script!\")"
val definition = createJvmScriptDefinitionFromTemplate<SimpleScriptTemplate>(
@@ -224,6 +224,27 @@ class ScriptingHostTest : TestCase() {
Assert.assertEquals(greeting, output)
}
@Test
fun testSimpleImportWithImplicitReceiverRef() {
val greeting = listOf("Hello from helloWithVal script!", "Hello from imported helloWithVal script!")
val script = "println(\"Hello from imported \${(::helloScriptName).get()} script!\")"
val definition = createJvmScriptDefinitionFromTemplate<SimpleScriptTemplate>(
compilation = {
makeSimpleConfigurationWithTestImport()
implicitReceivers(String::class)
},
evaluation = {
implicitReceivers("abc")
}
)
val output = captureOut {
BasicJvmScriptingHost().eval(
script.toScriptSource(), definition.compilationConfiguration, definition.evaluationConfiguration
).throwOnFailure()
}.lines()
Assert.assertEquals(greeting, output)
}
@Test
fun testSimpleScriptWithImplicitReceiver() {
val greeting = listOf("3")
@@ -295,14 +316,14 @@ class ScriptingHostTest : TestCase() {
}
@Test
fun testDiamondImportWithoutSharing() = expectTestToFailOnK2 {
fun testDiamondImportWithoutSharing() {
val greeting = listOf("Hi from common", "Hi from middle", "Hi from common", "sharedVar == 3")
val output = doDiamondImportTest()
Assert.assertEquals(greeting, output)
}
@Test
fun testDiamondImportWithSharing() = expectTestToFailOnK2 {
fun testDiamondImportWithSharing() {
val greeting = listOf("Hi from common", "Hi from middle", "sharedVar == 5")
val output = doDiamondImportTest(
ScriptEvaluationConfiguration {