Add isEmpty, isNotEmpty, and isBlank methods to ObjString, update tests and docs
This commit is contained in:
parent
d4bb186f85
commit
05e15e8e42
@ -1484,6 +1484,9 @@ A typical set of String functions includes:
|
|||||||
| lower(), lowercase() | change case to unicode upper |
|
| lower(), lowercase() | change case to unicode upper |
|
||||||
| upper(), uppercase() | change case to unicode lower |
|
| upper(), uppercase() | change case to unicode lower |
|
||||||
| trim() | trim space chars from both ends |
|
| trim() | trim space chars from both ends |
|
||||||
|
| isEmpty() | true if string is empty |
|
||||||
|
| isNotEmpty() | true if string is not empty |
|
||||||
|
| isBlank() | true if empty or contains only whitespace |
|
||||||
| startsWith(prefix) | true if starts with a prefix |
|
| startsWith(prefix) | true if starts with a prefix |
|
||||||
| endsWith(prefix) | true if ends with a prefix |
|
| endsWith(prefix) | true if ends with a prefix |
|
||||||
| last() | get last character of a string or throw |
|
| last() | get last character of a string or throw |
|
||||||
|
|||||||
@ -301,6 +301,18 @@ data class ObjString(val value: String) : Obj() {
|
|||||||
) {
|
) {
|
||||||
thisAs<ObjString>().value.trim().let(::ObjString)
|
thisAs<ObjString>().value.trim().let(::ObjString)
|
||||||
}
|
}
|
||||||
|
addFnDoc("isBlank", "Whether this string is empty or contains only whitespace characters.",
|
||||||
|
returns = type("lyng.Bool"), moduleName = "lyng.stdlib") {
|
||||||
|
ObjBool(thisAs<ObjString>().value.isBlank())
|
||||||
|
}
|
||||||
|
addFnDoc("isEmpty", "Whether this string is empty.",
|
||||||
|
returns = type("lyng.Bool"), moduleName = "lyng.stdlib") {
|
||||||
|
ObjBool(thisAs<ObjString>().value.isEmpty())
|
||||||
|
}
|
||||||
|
addFnDoc("isNotEmpty", "Whether this string is not empty.",
|
||||||
|
returns = type("lyng.Bool"), moduleName = "lyng.stdlib") {
|
||||||
|
ObjBool(thisAs<ObjString>().value.isNotEmpty())
|
||||||
|
}
|
||||||
addFnDoc(
|
addFnDoc(
|
||||||
name = "matches",
|
name = "matches",
|
||||||
doc = "Whether this string matches the given regular expression or pattern string.",
|
doc = "Whether this string matches the given regular expression or pattern string.",
|
||||||
|
|||||||
@ -779,7 +779,8 @@ class OOTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testOverrideVisibilityRules1() = runTest {
|
fun testOverrideVisibilityRules1() = runTest {
|
||||||
eval("""
|
val scope = Script.newScope()
|
||||||
|
scope.eval("""
|
||||||
interface Base {
|
interface Base {
|
||||||
abstract protected fun foo()
|
abstract protected fun foo()
|
||||||
|
|
||||||
@ -802,12 +803,70 @@ class OOTest {
|
|||||||
class Derived2: Base {
|
class Derived2: Base {
|
||||||
private var value = 42
|
private var value = 42
|
||||||
|
|
||||||
|
private fun fooPrivateImpl() = value
|
||||||
|
|
||||||
override protected fun foo() {
|
override protected fun foo() {
|
||||||
if( value < 10 ) 10 else value
|
fooPrivateImpl()
|
||||||
|
value++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assertEquals("bar!", Derived().bar())
|
||||||
|
val d = Derived2()
|
||||||
|
assertEquals(42, d.bar())
|
||||||
|
assertEquals(43, d.bar())
|
||||||
|
""".trimIndent())
|
||||||
|
scope.createChildScope().eval("""
|
||||||
assertEquals("bar!", Derived().bar())
|
assertEquals("bar!", Derived().bar())
|
||||||
assertEquals(42, Derived2().bar())
|
assertEquals(42, Derived2().bar())
|
||||||
""".trimIndent())
|
""".trimIndent())
|
||||||
}
|
}
|
||||||
|
@Test
|
||||||
|
fun testOverrideVisibilityRules2() = runTest {
|
||||||
|
val scope = Script.newScope()
|
||||||
|
scope.eval("""
|
||||||
|
interface Base {
|
||||||
|
abstract fun foo()
|
||||||
|
|
||||||
|
fun bar() {
|
||||||
|
// it must see foo() as it is protected and
|
||||||
|
// is declared here (even as abstract):
|
||||||
|
foo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Derived : Base {
|
||||||
|
protected val suffix = "!"
|
||||||
|
|
||||||
|
private fun fooPrivateImpl() = "bar"
|
||||||
|
|
||||||
|
override fun foo() {
|
||||||
|
// it should access own private and all protected memberes here:
|
||||||
|
fooPrivateImpl() + suffix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class Derived2: Base {
|
||||||
|
private var value = 42
|
||||||
|
|
||||||
|
private fun fooPrivateImpl() = value
|
||||||
|
|
||||||
|
override fun foo() {
|
||||||
|
fooPrivateImpl()
|
||||||
|
value++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals("bar!", Derived().bar())
|
||||||
|
val d = Derived2()
|
||||||
|
assertEquals(42, d.bar())
|
||||||
|
assertEquals(43, d.bar())
|
||||||
|
""".trimIndent())
|
||||||
|
scope.createChildScope().eval("""
|
||||||
|
assertEquals("bar!", Derived().bar())
|
||||||
|
assertEquals(42, Derived2().bar())
|
||||||
|
import lyng.serialization
|
||||||
|
for( i in 1..100 ) {
|
||||||
|
val d2 = Lynon.decode(Lynon.encode(Derived2()))
|
||||||
|
assertEquals(42, d2.bar())
|
||||||
|
assertEquals(43, d2.bar())
|
||||||
|
}
|
||||||
|
""".trimIndent())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user