From dc9a78ab18018f55764b4e64ca26c706211876c5 Mon Sep 17 00:00:00 2001 From: David Calap Date: Tue, 1 Oct 2019 23:33:50 +0200 Subject: [PATCH 1/3] BAEL-3323 Finding an element in a list using Kotlin - Initial commit --- .../kotlin/com/baeldung/lists/ListsTest.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt new file mode 100644 index 0000000000..8515a6f078 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt @@ -0,0 +1,25 @@ +package com.baeldung.lambda + +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class ListsTest { + + var batmans: List = listOf("Christian Bale", "Michael Keaton", "Ben Affleck", "George Clooney") + + @Test + fun whenFindASpecificItem_thenItemIsReturned() { + //Returns the first element matching the given predicate, or null if no such element was found. + val theFirstBatman = batmans.find { actor -> "Michael Keaton".equals(actor) } + assertEquals(theFirstBatman, "Michael Keaton") + } + + @Test + fun whenFilterWithPredicate_thenMatchingItemsAreReturned() { + //Returns a list containing only elements matching the given predicate. + val theCoolestBatmans = batmans.filter { actor -> actor.contains("a") } + assertTrue(theCoolestBatmans.contains("Christian Bale") && theCoolestBatmans.contains("Michael Keaton")) + } + +} \ No newline at end of file From 30bf670ba4514a54d082f726965e1ca092a93f8e Mon Sep 17 00:00:00 2001 From: David Calap Date: Wed, 2 Oct 2019 09:19:21 +0200 Subject: [PATCH 2/3] BAEL-3323 filterNot example added. Rename package, class and file --- .../baeldung/lists/{ListsTest.kt => ListsUnitTest.kt} | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) rename core-kotlin-2/src/test/kotlin/com/baeldung/lists/{ListsTest.kt => ListsUnitTest.kt} (68%) diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt similarity index 68% rename from core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt rename to core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt index 8515a6f078..1e7136d08b 100644 --- a/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt @@ -1,10 +1,10 @@ -package com.baeldung.lambda +package com.baeldung.lists import org.junit.jupiter.api.Test import kotlin.test.assertEquals import kotlin.test.assertTrue -class ListsTest { +class ListsUnitTest { var batmans: List = listOf("Christian Bale", "Michael Keaton", "Ben Affleck", "George Clooney") @@ -22,4 +22,11 @@ class ListsTest { assertTrue(theCoolestBatmans.contains("Christian Bale") && theCoolestBatmans.contains("Michael Keaton")) } + @Test + fun whenFilterNotWithPredicate_thenMatchingItemsAreReturned() { + //Returns a list containing only elements not matching the given predicate. + val theMehBatmans = batmans.filterNot { actor -> actor.contains("a") } + assertTrue(!theMehBatmans.contains("Christian Bale") && !theMehBatmans.contains("Michael Keaton")) + } + } \ No newline at end of file From 0478d4600794329e08b22a67c07daca5dad9404b Mon Sep 17 00:00:00 2001 From: David Calap Date: Wed, 2 Oct 2019 09:22:55 +0200 Subject: [PATCH 3/3] BAEL-3323 new check added to example --- .../src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt index 1e7136d08b..6fa7983689 100644 --- a/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsUnitTest.kt @@ -2,6 +2,7 @@ package com.baeldung.lists import org.junit.jupiter.api.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertTrue class ListsUnitTest { @@ -26,7 +27,8 @@ class ListsUnitTest { fun whenFilterNotWithPredicate_thenMatchingItemsAreReturned() { //Returns a list containing only elements not matching the given predicate. val theMehBatmans = batmans.filterNot { actor -> actor.contains("a") } - assertTrue(!theMehBatmans.contains("Christian Bale") && !theMehBatmans.contains("Michael Keaton")) + assertFalse(theMehBatmans.contains("Christian Bale") && theMehBatmans.contains("Michael Keaton")) + assertTrue(theMehBatmans.contains("Ben Affleck") && theMehBatmans.contains("George Clooney")) } } \ No newline at end of file