1 | suite 'Slices', ->
|
2 |
|
3 | setup ->
|
4 | @shared = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
5 |
|
6 | test "basic slicing", ->
|
7 | arrayEq [7, 8, 9] , @shared[7..9]
|
8 | arrayEq [2, 3] , @shared[2...4]
|
9 | arrayEq [2, 3, 4, 5], @shared[2...6]
|
10 |
|
11 | test "slicing with variables as endpoints", ->
|
12 | [a, b] = [1, 4]
|
13 | arrayEq [1, 2, 3, 4], @shared[a..b]
|
14 | arrayEq [1, 2, 3] , @shared[a...b]
|
15 |
|
16 | test "slicing with expressions as endpoints", ->
|
17 | [a, b] = [1, 3]
|
18 | arrayEq [2, 3, 4, 5, 6], @shared[(a+1)..2*b]
|
19 | arrayEq [2, 3, 4, 5] , @shared[a+1...(2*b)]
|
20 |
|
21 | test "unbounded slicing", ->
|
22 | arrayEq [7, 8, 9] , @shared[7..]
|
23 | arrayEq [8, 9] , @shared[-2..]
|
24 | arrayEq [9] , @shared[-1...]
|
25 | arrayEq [0, 1, 2] , @shared[...3]
|
26 | arrayEq [0, 1, 2, 3], @shared[..-7]
|
27 |
|
28 | arrayEq @shared , @shared[..-1]
|
29 | arrayEq @shared[0..8], @shared[...-1]
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | arrayEq [1, 2, 3], [1, 2, 3][..]
|
37 |
|
38 | test "#930, #835, #831, #746 #624: inclusive slices to -1 should slice to end", ->
|
39 | arrayEq @shared, @shared[0..-1]
|
40 | arrayEq @shared, @shared[..-1]
|
41 | arrayEq @shared.slice(1,@shared.length), @shared[1..-1]
|
42 |
|
43 | test "string slicing", ->
|
44 | str = "abcdefghijklmnopqrstuvwxyz"
|
45 | ok str[1...1] is ""
|
46 | ok str[1..1] is "b"
|
47 | ok str[1...5] is "bcde"
|
48 | ok str[0..4] is "abcde"
|
49 | ok str[-5..] is "vwxyz"
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|