lyng/lynglib/src/commonTest/kotlin/MIQualifiedDispatchTest.kt

126 lines
3.8 KiB
Kotlin

/*
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.eval
import kotlin.test.Test
class MIQualifiedDispatchTest {
@Test
fun testQualifiedMethodResolution() = runTest {
eval(
"""
class Foo(val a) {
fun common() { "A" }
fun runA() { "ResultA:" + a }
}
class Bar(val b) {
fun common() { "B" }
fun runB() { "ResultB:" + b }
}
class FooBar(a0,b0) : Foo(a0), Bar(b0) { }
val fb = FooBar(1,2)
// unqualified picks leftmost base
assertEquals("A", fb.common())
// cast-based disambiguation
assertEquals("B", (fb as Bar).common())
assertEquals("A", (fb as Foo).common())
// Note: wrappers using this@Type inside FooBar body will be validated later
// when class-body method registration is finalized.
""".trimIndent()
)
}
@Test
fun testQualifiedFieldReadWrite() = runTest {
eval(
"""
class Foo(val a) { var tag = "F" }
class Bar(val b) { var tag = "B" }
class FooBar(a0,b0) : Foo(a0), Bar(b0) { }
val fb = FooBar(1,2)
// unqualified resolves to rightmost base
assertEquals("B", fb.tag)
// qualified reads via casts should respect the ancestor view
assertEquals("F", (fb as Foo).tag)
assertEquals("B", (fb as Bar).tag)
// unqualified write updates rightmost base
fb.tag = "X"
assertEquals("X", fb.tag)
assertEquals("F", (fb as Foo).tag)
assertEquals("X", (fb as Bar).tag)
// qualified write via cast targets Bar
(fb as Bar).tag = "Y"
assertEquals("F", (fb as Foo).tag)
assertEquals("Y", (fb as Bar).tag)
""".trimIndent()
)
}
@Test
fun testCastsUseDistinctAncestorStorage() = runTest {
eval(
"""
class A { var x = 1 }
class B : A { override var x = 2 }
class C : A { override var x = 3 }
class D : B, C { }
val d = D()
assertEquals(2, (d as B).x)
assertEquals(3, (d as C).x)
assertEquals(1, (d as A).x)
(d as B).x = 100
assertEquals(100, (d as B).x)
assertEquals(3, (d as C).x)
assertEquals(1, (d as A).x)
(d as C).x = 200
assertEquals(100, (d as B).x)
assertEquals(200, (d as C).x)
assertEquals(1, (d as A).x)
""".trimIndent()
)
}
@Test
fun testCastsAndSafeCall() = runTest {
eval(
"""
class Foo(val a) { fun runA() { "ResultA:" + a } }
class Bar(val b) { fun runB() { "ResultB:" + b } }
class Buzz : Bar(3)
val buzz = Buzz()
assertEquals("ResultB:3", buzz.runB())
assertEquals("ResultB:3", (buzz as? Bar)?.runB())
assertEquals(null, (buzz as? Foo)?.runA())
""".trimIndent()
)
}
}