forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-3323 Finding an element in a list using Kotlin - Initial commit
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.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,25 @@ | ||
package com.baeldung.lambda | ||
|
||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertTrue | ||
|
||
class ListsTest { | ||
|
||
var batmans: List<String> = 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")) | ||
} | ||
|
||
} |