Add samples for is(|Not|NullOr)Empty and is(|Not|NullOr)Blank functions
This commit is contained in:
@@ -351,6 +351,8 @@ public inline fun String.intern(): String = (this as java.lang.String).intern()
|
||||
|
||||
/**
|
||||
* Returns `true` if this string is empty or consists solely of whitespace characters.
|
||||
*
|
||||
* @sample samples.text.Strings.stringIsBlank
|
||||
*/
|
||||
public actual fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package samples.text
|
||||
|
||||
import samples.*
|
||||
import kotlin.test.assertTrue
|
||||
import kotlin.test.*
|
||||
|
||||
class Strings {
|
||||
|
||||
@@ -177,6 +177,81 @@ class Strings {
|
||||
assertTrue(nonBlank === sameString)
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun stringIsBlank() {
|
||||
fun validateName(name: String): String {
|
||||
if (name.isBlank()) throw IllegalArgumentException("Name cannot be blank")
|
||||
return name
|
||||
}
|
||||
|
||||
assertPrints(validateName("Adam"), "Adam")
|
||||
assertFails { validateName("") }
|
||||
assertFails { validateName(" \t\n") }
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun stringIsNotBlank() {
|
||||
fun validateName(name: String): String {
|
||||
require(name.isNotBlank()) { "Name cannot be blank" }
|
||||
return name
|
||||
}
|
||||
|
||||
assertPrints(validateName("Adam"), "Adam")
|
||||
assertFails { validateName("") }
|
||||
assertFails { validateName(" \t\n") }
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun stringIsNullOrBlank() {
|
||||
fun validateName(name: String?): String {
|
||||
if (name.isNullOrBlank()) throw IllegalArgumentException("Name cannot be blank")
|
||||
// name is not nullable here anymore due to a smart cast after calling isNullOrBlank
|
||||
return name
|
||||
}
|
||||
|
||||
assertPrints(validateName("Adam"), "Adam")
|
||||
assertFails { validateName(null) }
|
||||
assertFails { validateName("") }
|
||||
assertFails { validateName(" \t\n") }
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun stringIsEmpty() {
|
||||
fun markdownLink(title: String, url: String) =
|
||||
if (title.isEmpty()) url else "[$title]($url)"
|
||||
|
||||
// plain link
|
||||
assertPrints(markdownLink(title = "", url = "https://kotlinlang.org"), "https://kotlinlang.org")
|
||||
|
||||
// link with custom title
|
||||
assertPrints(markdownLink(title = "Kotlin Language", url = "https://kotlinlang.org"), "[Kotlin Language](https://kotlinlang.org)")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun stringIsNotEmpty() {
|
||||
fun markdownLink(title: String, url: String) =
|
||||
if (title.isNotEmpty()) "[$title]($url)" else url
|
||||
|
||||
// plain link
|
||||
assertPrints(markdownLink(title = "", url = "https://kotlinlang.org"), "https://kotlinlang.org")
|
||||
|
||||
// link with custom title
|
||||
assertPrints(markdownLink(title = "Kotlin Language", url = "https://kotlinlang.org"), "[Kotlin Language](https://kotlinlang.org)")
|
||||
}
|
||||
|
||||
|
||||
@Sample
|
||||
fun stringIsNullOrEmpty() {
|
||||
fun markdownLink(title: String?, url: String) =
|
||||
if (title.isNullOrEmpty()) url else "[$title]($url)"
|
||||
|
||||
// plain link
|
||||
assertPrints(markdownLink(title = null, url = "https://kotlinlang.org"), "https://kotlinlang.org")
|
||||
|
||||
// link with custom title
|
||||
assertPrints(markdownLink(title = "Kotlin Language", url = "https://kotlinlang.org"), "[Kotlin Language](https://kotlinlang.org)")
|
||||
}
|
||||
|
||||
@Sample
|
||||
fun commonPrefixWith() {
|
||||
assertPrints("Hot_Coffee".commonPrefixWith("Hot_cocoa"), "Hot_")
|
||||
|
||||
@@ -213,6 +213,8 @@ public fun String.padEnd(length: Int, padChar: Char = ' '): String =
|
||||
|
||||
/**
|
||||
* Returns `true` if this nullable char sequence is either `null` or empty.
|
||||
*
|
||||
* @sample samples.text.Strings.stringIsNullOrEmpty
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharSequence?.isNullOrEmpty(): Boolean {
|
||||
@@ -225,12 +227,16 @@ public inline fun CharSequence?.isNullOrEmpty(): Boolean {
|
||||
|
||||
/**
|
||||
* Returns `true` if this char sequence is empty (contains no characters).
|
||||
*
|
||||
* @sample samples.text.Strings.stringIsEmpty
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharSequence.isEmpty(): Boolean = length == 0
|
||||
|
||||
/**
|
||||
* Returns `true` if this char sequence is not empty.
|
||||
*
|
||||
* @sample samples.text.Strings.stringIsNotEmpty
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharSequence.isNotEmpty(): Boolean = length > 0
|
||||
@@ -241,12 +247,16 @@ public inline fun CharSequence.isNotEmpty(): Boolean = length > 0
|
||||
|
||||
/**
|
||||
* Returns `true` if this char sequence is not empty and contains some characters except of whitespace characters.
|
||||
*
|
||||
* @sample samples.text.Strings.stringIsNotBlank
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharSequence.isNotBlank(): Boolean = !isBlank()
|
||||
|
||||
/**
|
||||
* Returns `true` if this nullable char sequence is either `null` or empty or consists solely of whitespace characters.
|
||||
*
|
||||
* @sample samples.text.Strings.stringIsNullOrBlank
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharSequence?.isNullOrBlank(): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user