|
| 1 | +package com.baeldung.scala.removeelement |
| 2 | + |
| 3 | +import org.scalatest.flatspec.AnyFlatSpec |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | +import org.scalatest.prop.TableDrivenPropertyChecks |
| 6 | + |
| 7 | +import scala.collection.mutable.{ArrayBuffer, ListBuffer} |
| 8 | + |
| 9 | +class RemoveMatchingElementUnitTest |
| 10 | + extends AnyFlatSpec |
| 11 | + with Matchers |
| 12 | + with TableDrivenPropertyChecks { |
| 13 | + |
| 14 | + //format: off |
| 15 | + val firstMatchTable = Table( |
| 16 | + ("input", "elementToRemove", "expected"), |
| 17 | + (List("abc", "def", "xyz", "abc"), "abc" ,List("def", "xyz", "abc")), |
| 18 | + (List("abc", "def", "xyz", "abc"), "abcd" ,List("abc", "def", "xyz", "abc")), |
| 19 | + (List.empty[String], "abcd" ,List.empty[String]), |
| 20 | + ) |
| 21 | + |
| 22 | + val allMatchTable = Table( |
| 23 | + ("input", "elementToRemove", "expected"), |
| 24 | + (List("abc", "def", "xyz", "abc"), "abc", List("def", "xyz")), |
| 25 | + (List("abc", "def", "xyz", "abc"), "abcd", List("abc", "def", "xyz", "abc")), |
| 26 | + (List.empty[String], "abcd", List.empty[String]), |
| 27 | + ) |
| 28 | + //format on |
| 29 | + |
| 30 | + // on immutable collections |
| 31 | + it should "remove first match using span" in { |
| 32 | + forAll(firstMatchTable) { (input, ele, exp) => |
| 33 | + val (part1, part2)= input.span(_ != ele) |
| 34 | + val result = part1 ++ part2.drop(1) |
| 35 | + result shouldBe exp |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + it should "remove first match using pattern matching" in { |
| 40 | + forAll(firstMatchTable) { (input, ele, exp) => |
| 41 | + val result = input match { |
| 42 | + case `ele` :: sub => sub |
| 43 | + case sub => sub |
| 44 | + } |
| 45 | + result shouldBe exp |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + it should "remove all matches using partition" in { |
| 50 | + forAll(allMatchTable) { (input, ele, exp) => |
| 51 | + val (part1, _) = input.partition(_ != ele) |
| 52 | + val result = part1 |
| 53 | + result shouldBe exp |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // on mutable collections |
| 58 | + it should "remove first match from mutable list" in { |
| 59 | + val buffer = ListBuffer[String]("abc", "def", "xyz", "def") |
| 60 | + buffer -= "def" |
| 61 | + buffer shouldBe ListBuffer[String]("abc", "xyz", "def") |
| 62 | + } |
| 63 | + |
| 64 | + it should "remove all matches from mutable list using filterInPlace" in { |
| 65 | + val buffer = ListBuffer[String]("abc", "def", "xyz", "def") |
| 66 | + buffer.filterInPlace(_ != "def") |
| 67 | + buffer shouldBe ListBuffer[String]("abc", "xyz") |
| 68 | + } |
| 69 | + |
| 70 | + it should "remove all matches from mutable list using filter" in { |
| 71 | + val list = List("abc", "def", "xyz", "def") |
| 72 | + val filteredList = list.filter(_ != "def") |
| 73 | + filteredList shouldBe List("abc", "xyz") |
| 74 | + list.filterNot(_ == "def") shouldBe List("abc", "xyz") |
| 75 | + // on ListBuffer |
| 76 | + val buffer = ListBuffer[String]("abc", "def", "xyz", "def") |
| 77 | + val newBuffer = buffer.filter(_ != "def") |
| 78 | + newBuffer shouldBe ListBuffer[String]("abc", "xyz") |
| 79 | + } |
| 80 | + |
| 81 | + // remove multiple matches |
| 82 | + it should "remove all matches using collect" in { |
| 83 | + forAll(allMatchTable) { (input, ele, exp) => |
| 84 | + val result = input.collect { |
| 85 | + case e if e != ele => e |
| 86 | + } |
| 87 | + result shouldBe exp |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + it should "remove all matches using filter" in { |
| 92 | + forAll(allMatchTable) { (input, ele, exp) => |
| 93 | + val result = input.filter(_ != ele) |
| 94 | + result shouldBe exp |
| 95 | + val result2 = input.filterNot(_ == ele) |
| 96 | + result2 shouldBe exp |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + it should "remove all matches using recursion" in { |
| 101 | + forAll(allMatchTable) { (input, ele, exp) => |
| 102 | + val result = input.foldLeft(List.empty[String])((acc,i) => if(i != ele) acc :+ i else acc) |
| 103 | + result shouldBe exp |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + it should "remove all matches from mutable ListBuffer" in { |
| 108 | + val buffer = ListBuffer[String]("abc", "def", "xyz", "def") |
| 109 | + buffer.filterInPlace(_ != "def") |
| 110 | + buffer shouldBe ListBuffer[String]("abc", "xyz") |
| 111 | + } |
| 112 | + |
| 113 | + it should "remove all matches from mutable ArrayBuffer" in { |
| 114 | + val buffer = ArrayBuffer[String]("abc", "def", "xyz", "def") |
| 115 | + buffer.filterInPlace(_ != "def") |
| 116 | + buffer shouldBe ListBuffer[String]("abc", "xyz") |
| 117 | + } |
| 118 | +} |
0 commit comments