From f66906d54d0df7ca17727ecfdff8f56408dafaa5 Mon Sep 17 00:00:00 2001 From: Michael Ficarra Date: Thu, 16 Dec 2010 05:14:57 -0500 Subject: [PATCH] finished converting tests to newer, cleaner format in test_ranges_slices_and_splices.coffee, just need to add some new ones --- test/ranges_slices_and_splices.coffee | 84 ++++++++++++++++++++++ test/test_ranges_slices_and_splices.coffee | 74 ------------------- 2 files changed, 84 insertions(+), 74 deletions(-) create mode 100644 test/ranges_slices_and_splices.coffee delete mode 100644 test/test_ranges_slices_and_splices.coffee diff --git a/test/ranges_slices_and_splices.coffee b/test/ranges_slices_and_splices.coffee new file mode 100644 index 00000000..aecbfa20 --- /dev/null +++ b/test/ranges_slices_and_splices.coffee @@ -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 diff --git a/test/test_ranges_slices_and_splices.coffee b/test/test_ranges_slices_and_splices.coffee deleted file mode 100644 index 0746b1b0..00000000 --- a/test/test_ranges_slices_and_splices.coffee +++ /dev/null @@ -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'