@@ -5,38 +5,42 @@ import org.scalatest.wordspec.AnyWordSpec
55
66class TakeWhileDropWhileUnitTest extends AnyWordSpec with Matchers {
77
8+ private val numbersAscending = List (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 )
9+ private val numbersDescending = List (8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 )
10+ private val numbersMixed = List (1 , 2 , 3 , 4 , 3 , 2 , 1 )
11+
812 " takeWhile" should {
913 " take element from list till condition satisfies" in {
10- val numbers = List (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 )
11- val lessThanFive = numbers.takeWhile(_ < 5 )
14+ val lessThanFive = numbersAscending.takeWhile(_ < 5 )
1215 lessThanFive shouldBe List (1 , 2 , 3 , 4 )
1316 }
1417
1518 " return different elements when the element order changes" in {
16- val numbers = List (8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 )
17- val lessThanFive = numbers.takeWhile(_ < 5 )
19+ val lessThanFive = numbersDescending.takeWhile(_ < 5 )
1820 lessThanFive shouldBe Nil
1921 }
2022
2123 " stop taking as soon as the first failure in predicate" in {
22- val numbers = List (1 , 2 , 3 , 4 , 3 , 2 , 1 )
23- val lessThanThree = numbers.takeWhile(_ < 3 )
24+ val lessThanThree = numbersMixed.takeWhile(_ < 3 )
2425 lessThanThree shouldBe List (1 , 2 )
2526 }
2627
28+ " take all elements if the predicate is satisfied for all" in {
29+ val positive = numbersAscending.takeWhile(_ > 0 )
30+ positive shouldBe numbersAscending
31+ }
32+
2733 }
2834
2935 " dropWhile" should {
3036 " drop the elements from the list until the predicate is true" in {
31- val numbers = List (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 )
32- val dropLessThan5 = numbers.dropWhile(_ < 5 )
37+ val dropLessThan5 = numbersAscending.dropWhile(_ < 5 )
3338 dropLessThan5 shouldBe List (5 , 6 , 7 , 8 )
3439 }
3540
3641 " dropWhile behavior changes with the order" in {
37- val numbers = List (8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 )
38- val dropLessThan5 = numbers.dropWhile(_ < 5 )
39- dropLessThan5 shouldBe numbers
42+ val dropLessThan5 = numbersDescending.dropWhile(_ < 5 )
43+ dropLessThan5 shouldBe numbersDescending
4044 }
4145 }
4246
0 commit comments