From 6f75ad86a652d485776c8baae28c811f0481e4be Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Wed, 29 Mar 2017 13:41:37 +0200 Subject: [PATCH] Bael 756 (#1531) * BAEL-756 code for kotlin null-safety article * BAEL-756 fix typo * BAEL-756 increment version of Kotlin * BAEL-756 remove duplicate form pom * BAEL-756 add also and run example --- .../com/baeldung/kotlin/NullSafetyTest.kt | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/NullSafetyTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/NullSafetyTest.kt index 2adc1032bc..0ecc74b6fb 100644 --- a/kotlin/src/test/kotlin/com/baeldung/kotlin/NullSafetyTest.kt +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/NullSafetyTest.kt @@ -66,7 +66,27 @@ class NullSafetyTest { //when var res = listOf() for (item in names) { - item?.let { res = res.plus(it) } + item?.let { res = res.plus(it); it } + ?.also{it -> println("non nullable value: $it")} + } + + //then + assertEquals(2, res.size) + assertTrue { res.contains(firstName) } + assertTrue { res.contains(secondName) } + } + + @Test + fun fivenCollectionOfObject_whenUseRunOperator_thenExecuteActionOnNonNullValue(){ + //given + val firstName = "Tom" + val secondName = "Michael" + val names: List = listOf(firstName, null, secondName) + + //when + var res = listOf() + for (item in names) { + item?.run{res = res.plus(this)} } //then