|
| 1 | +package com.baeldung.scala.zipvariants |
| 2 | + |
| 3 | +import org.scalatest.wordspec.AnyWordSpec |
| 4 | + |
| 5 | +class ZipVariantUnitTest extends AnyWordSpec { |
| 6 | + |
| 7 | + "zip method" should { |
| 8 | + "combine two collections of equal size correctly" in { |
| 9 | + val numbers = List(1, 2, 3) |
| 10 | + val words = List("one", "two", "three") |
| 11 | + val zipped = numbers.zip(words) |
| 12 | + assert(zipped == List((1, "one"), (2, "two"), (3, "three"))) |
| 13 | + } |
| 14 | + |
| 15 | + "drop elements from larger collection" in { |
| 16 | + val numbers = List(1, 2, 3) |
| 17 | + val words = List("one", "two", "three", "four") |
| 18 | + val zipped = numbers.zip(words) |
| 19 | + assert(zipped == List((1, "one"), (2, "two"), (3, "three"))) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + "zipAll" should { |
| 24 | + "combine two collections of unequal size with default values" in { |
| 25 | + val numbers = List(1, 2, 3) |
| 26 | + val words = List("one", "two", "three", "four") |
| 27 | + val zipped = numbers.zipAll(words, 0, "unknown") |
| 28 | + assert(zipped == List((1, "one"), (2, "two"), (3, "three"), (0, "four"))) |
| 29 | + val zipped2 = numbers.zipAll(words.take(2), 0, "unknown") |
| 30 | + assert(zipped2 == List((1, "one"), (2, "two"), (3, "unknown"))) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + "zipWithIndex" should { |
| 35 | + "combine each element of a list with its corresponding index" in { |
| 36 | + val words = List("one", "two", "three", "four") |
| 37 | + assert( |
| 38 | + words.zipWithIndex == List( |
| 39 | + ("one", 0), |
| 40 | + ("two", 1), |
| 41 | + ("three", 2), |
| 42 | + ("four", 3) |
| 43 | + ) |
| 44 | + ) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments