improved lazy delegate; added with(newThis) {} to stdlib
This commit is contained in:
1 parent
f792c73b8f
commit
41a3617850
6 files changed
+85
-8
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
||||
* Copyright 2026 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.
|
||||
@@ -113,4 +113,22 @@ class StdlibTest {
|
||||
assertEquals(5, (1..10).toList().count { it % 2 == 1 } )
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testWith() = runTest {
|
||||
eval("""
|
||||
class Person(val name, var age)
|
||||
val p = Person("Alice", 30)
|
||||
|
||||
val result = with(p) {
|
||||
assertEquals("Alice", name)
|
||||
assertEquals(30, age)
|
||||
age = 31
|
||||
"done"
|
||||
}
|
||||
|
||||
assertEquals("done", result)
|
||||
assertEquals(31, p.age)
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
@@ -287,4 +287,40 @@ class DelegationTest {
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testThisInLazy() = runTest {
|
||||
eval("""
|
||||
class A {
|
||||
val numbers = [1,2,3]
|
||||
val tags by lazy { this.numbers }
|
||||
}
|
||||
class B {
|
||||
val a by lazy { A() }
|
||||
val test by lazy { a.tags + [4] }
|
||||
}
|
||||
assertEquals( [1,2,3], A().tags)
|
||||
assertEquals( [1,2,3,4], B().test)
|
||||
""")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScopeInLazy() = runTest {
|
||||
val s1 = Script.newScope()
|
||||
s1.eval("""
|
||||
class A {
|
||||
val tags by lazy { GLOBAL_NUMBERS }
|
||||
}
|
||||
""".trimIndent())
|
||||
// on the same scope, it will see my GLOBAL_NUMBERS:
|
||||
s1.eval("""
|
||||
val GLOBAL_NUMBERS = [1,2,3]
|
||||
class B {
|
||||
val a by lazy { A() }
|
||||
val test by lazy { a.tags + [4] }
|
||||
}
|
||||
assertEquals( [1,2,3], A().tags)
|
||||
assertEquals( [1,2,3,4], B().test)
|
||||
""")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user