1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

finished converting tests to newer, cleaner format in

test_ranges_slices_and_splices.coffee, just need to add some new ones
This commit is contained in:
Michael Ficarra 2010-12-16 05:14:57 -05:00
parent 85afef9981
commit f66906d54d
2 changed files with 84 additions and 74 deletions

View file

@ -0,0 +1,84 @@
# Ranges, Slices, and Splices
# ---------------------------
# shared array
shared = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#### Ranges
test "basic ranges", ->
arrayEqual [2, 3, 4, 5], [2...6]
test "downward ranges", ->
arrayEqual shared, [9..0].reverse()
arrayEqual [5, 4, 3, 2, 1], [5..1]
test "ranges with variables as enpoints", ->
[a, b] = [1, 5]
arrayEqual [1, 2, 3, 4], [a...b]
b = -5
arrayEqual [1, 0, -1, -2, -3, -4, -5], [a..b]
test "ranges with expressions as endpoints", ->
arrayEqual [6, 7, 8, 9, 10], [(1+5)..1+9]
test "large ranges are generated with looping constructs", ->
ary = [100...0]
ok (len = ary.length) is 100
ok ary[len - 1] is 1
#### Slices
test "basic slicing", ->
arrayEqual [7, 8, 9] , shared[7..9]
arrayEqual [2, 3] , shared[2...4]
arrayEqual [2, 3, 4, 5], shared[2...6]
test "slicing with variables as endpoints", ->
[a, b] = [1, 5]
arrayEqual [1, 2, 3, 4], shared[a...b]
test "slicing with expressions as endpoints", ->
test "unbounded slicing", ->
arrayEqual [7, 8, 9] , shared[7..]
arrayEqual [8, 9] , shared[-2..]
arrayEqual [9] , shared[-1...]
arrayEqual [0, 1, 2] , shared[...3]
arrayEqual [0, 1, 2, 3], shared[..-7]
arrayEqual shared[a..] , shared[a...] for a in [-shared.length..shared.length]
arrayEqual shared , shared[..-1]
arrayEqual shared[0..8], shared[...-1]
arrayEqual shared[..a] , shared[...a] for a in [-shared.length..shared.length] when a isnt -1
test "#930, #835, #831, #746 #624: inclusive slices to -1 should slice to end", ->
test "string slicing", ->
str = "abcdefghijklmnopqrstuvwxyz"
ok str[1...1] is ""
ok str[1..1] is "b"
ok str[1...5] is "bcde"
ok str[0..4] is "abcde"
ok str[-5..] is "vwxyz"
#### Splices
test "basic splicing", ->
ary = [0..9]
ary[5..9] = [0, 0, 0]
arrayEqual [0, 1, 2, 3, 4, 0, 0, 0], ary
test "unbounded splicing", ->
ary = [0..9]
ary[3..] = [9, 8, 7]
arrayEqual [0, 1, 2, 9, 8, 7]. ary
ary[...3] = [7, 8, 9]
arrayEqual [7, 8, 9, 9, 8, 7], ary
# splicing with variables as endpoints
# splicing with expressions as endpoints

View file

@ -1,74 +0,0 @@
# # Slice.
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a = array[7..9]
b = array[2...4]
result = a.concat(b).join(' ')
ok result is "7 8 9 2 3"
a = [0, 1, 2, 3, 4, 5, 6, 7]
eq a[2...6].join(' '), '2 3 4 5'
# Ranges.
countdown = [10..1].join(' ')
ok countdown is "10 9 8 7 6 5 4 3 2 1"
a = 1
b = 5
nums = [a...b]
ok nums.join(' ') is '1 2 3 4'
b = -5
nums = [a..b]
ok nums.join(' ') is '1 0 -1 -2 -3 -4 -5'
# Expression-based range.
array = [(1+5)..1+9]
ok array.join(' ') is "6 7 8 9 10"
array = [5..1]
ok array.join(' ') is '5 4 3 2 1'
array = [30...0]
ok (len = array.length) is 30
ok array[len - 1] is 1
# String slicing (at least on Node).
hello = "Hello World"
ok hello[1...1] is ""
ok hello[1..1] is "e"
ok hello[1...5] is "ello"
ok hello[0..4] is "Hello"
# Splice literals.
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
array[5..10] = [0, 0, 0]
ok array.join(' ') is '0 1 2 3 4 0 0 0'
# Slices and splices that omit their beginning or end.
array = [0..10]
ok array[7..].join(' ') is '7 8 9 10'
ok array[-2..].join(' ') is '9 10'
ok array[...3].join(' ') is '0 1 2'
ok array[..-5].join(' ') is '0 1 2 3 4 5 6'
array[3..] = [9, 8, 7]
ok array.join(' ') is '0 1 2 9 8 7'
array[...3] = [7, 8, 9]
ok array.join(' ') is '7 8 9 9 8 7'