UNPKG

1.86 kBtext/coffeescriptView Raw
1suite '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 #for a in [-@shared.length..@shared.length]
32 # arrayEq @shared[a..] , @shared[a...]
33 #for a in [-@shared.length+1...@shared.length]
34 # arrayEq @shared[..a][...-1] , @shared[...a]
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 #test "#1722: operator precedence in unbounded slice compilation", ->
52 # list = [0..9]
53 # n = 2 # some truthy number in `list`
54 # arrayEq [0..n], list[..n]
55 # arrayEq [0..n], list[..n or 0]
56 # arrayEq [0..n], list[..if n then n else 0]
57
58 #test "#2349: inclusive slicing to numeric strings", ->
59 # arrayEq [0, 1], [0..10][.."1"]