-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ch.tutteli.kbox | ||
|
||
/** | ||
* Returns this [String] if it is not blank, null otherwise. | ||
*/ | ||
@Suppress("NOTHING_TO_INLINE") | ||
inline fun <T: CharSequence> T.blankToNull(): T? = if(this.isNotBlank()) this else null |
26 changes: 26 additions & 0 deletions
26
kbox-jvm/src/test/kotlin/ch/tutteli/kbox/BlankToNullSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package ch.tutteli.kbox | ||
|
||
import ch.tutteli.atrium.api.cc.en_GB.toBe | ||
import ch.tutteli.atrium.assert | ||
import org.jetbrains.spek.api.Spek | ||
import org.jetbrains.spek.api.dsl.describe | ||
|
||
object BlankToNullSpec : Spek({ | ||
|
||
describe("fun ${String::blankToNull.name}") { | ||
listOf<Triple<String, CharSequence, CharSequence?>>( | ||
Triple("empty String", "", null), | ||
Triple("empty StringBuilder", StringBuilder(), null), | ||
Triple("String \" \"", " ", null), | ||
Triple("StringBuilder \" \"", StringBuilder(" "), null), | ||
Triple("String \" h\"", " h", " h"), | ||
Triple("StringBuilder \" h\"", StringBuilder(" h"), StringBuilder(" h")), | ||
Triple("String \"h \"", "h ", "h "), | ||
Triple("StringBuilder \"h \"", StringBuilder("h "), StringBuilder("h ")) | ||
).forEach { (description, value, expected) -> | ||
test("given $description, expect $expected") { | ||
assert(value.blankToNull().toString()).toBe(expected.toString()) | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters