@@ -5,38 +5,42 @@ import org.scalatest.wordspec.AnyWordSpec
5
5
6
6
class TakeWhileDropWhileUnitTest extends AnyWordSpec with Matchers {
7
7
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
+
8
12
" takeWhile" should {
9
13
" 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 )
12
15
lessThanFive shouldBe List (1 , 2 , 3 , 4 )
13
16
}
14
17
15
18
" 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 )
18
20
lessThanFive shouldBe Nil
19
21
}
20
22
21
23
" 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 )
24
25
lessThanThree shouldBe List (1 , 2 )
25
26
}
26
27
28
+ " take all elements if the predicate is satisfied for all" in {
29
+ val positive = numbersAscending.takeWhile(_ > 0 )
30
+ positive shouldBe numbersAscending
31
+ }
32
+
27
33
}
28
34
29
35
" dropWhile" should {
30
36
" 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 )
33
38
dropLessThan5 shouldBe List (5 , 6 , 7 , 8 )
34
39
}
35
40
36
41
" 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
40
44
}
41
45
}
42
46
0 commit comments