diff options
Diffstat (limited to 'spec/inputs/test/slicing_spec.yue')
| -rw-r--r-- | spec/inputs/test/slicing_spec.yue | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/spec/inputs/test/slicing_spec.yue b/spec/inputs/test/slicing_spec.yue new file mode 100644 index 0000000..b0a686b --- /dev/null +++ b/spec/inputs/test/slicing_spec.yue | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | describe "slicing", -> | ||
| 2 | it "should slice array with basic syntax", -> | ||
| 3 | items = [1, 2, 3, 4, 5] | ||
| 4 | result = items[1..3] | ||
| 5 | assert.same result, {1, 2, 3} | ||
| 6 | |||
| 7 | it "should slice from beginning", -> | ||
| 8 | items = [1, 2, 3, 4, 5] | ||
| 9 | result = items[1..#items] | ||
| 10 | assert.same result, {1, 2, 3, 4, 5} | ||
| 11 | |||
| 12 | it "should slice to end", -> | ||
| 13 | items = [1, 2, 3, 4, 5] | ||
| 14 | result = items[3..5] | ||
| 15 | assert.same result, {3, 4, 5} | ||
| 16 | |||
| 17 | it "should handle negative indices", -> | ||
| 18 | items = [1, 2, 3, 4, 5] | ||
| 19 | result = items[#items-2..#items] | ||
| 20 | assert.same result, {3, 4, 5} | ||
| 21 | |||
| 22 | it "should slice single element", -> | ||
| 23 | items = [1, 2, 3, 4, 5] | ||
| 24 | result = items[2..2] | ||
| 25 | assert.same result, {2} | ||
| 26 | |||
| 27 | it "should work with strings", -> | ||
| 28 | s = "hello" | ||
| 29 | result = s\sub 1, 3 | ||
| 30 | assert.same result, "hel" | ||
| 31 | |||
| 32 | it "should handle out of bounds", -> | ||
| 33 | items = [1, 2, 3] | ||
| 34 | result = items[1..10] | ||
| 35 | assert.same result, {1, 2, 3} | ||
| 36 | |||
| 37 | it "should create new table", -> | ||
| 38 | original = [1, 2, 3, 4, 5] | ||
| 39 | sliced = original[2..4] | ||
| 40 | sliced[1] = 99 | ||
| 41 | assert.same original[2], 2 -- original unchanged | ||
| 42 | |||
| 43 | it "should work with nested arrays", -> | ||
| 44 | nested = [[1, 2], [3, 4], [5, 6]] | ||
| 45 | result = nested[1..2] | ||
| 46 | assert.same result, {{1, 2}, {3, 4}} | ||
| 47 | |||
| 48 | it "should slice with step simulation", -> | ||
| 49 | items = [1, 2, 3, 4, 5] | ||
| 50 | result = [items[i] for i = 1, #items, 2] | ||
| 51 | assert.same result, {1, 3, 5} | ||
| 52 | |||
| 53 | it "should handle empty slice range", -> | ||
| 54 | items = [1, 2, 3, 4, 5] | ||
| 55 | result = items[6..10] | ||
| 56 | assert.same result, nil | ||
| 57 | |||
| 58 | it "should work with reverse indexing", -> | ||
| 59 | items = [1, 2, 3, 4, 5] | ||
| 60 | last = items[#] | ||
| 61 | second_last = items[#-1] | ||
| 62 | assert.same last, 5 | ||
| 63 | assert.same second_last, 4 | ||
| 64 | |||
| 65 | it "should support slice in assignment", -> | ||
| 66 | items = [1, 2, 3, 4, 5] | ||
| 67 | [a, b, c] = [items[i] for i in *{1, 2, 3}] | ||
| 68 | assert.same a, 1 | ||
| 69 | assert.same b, 2 | ||
| 70 | assert.same c, 3 | ||
| 71 | |||
| 72 | it "should work with table comprehensions", -> | ||
| 73 | items = [1, 2, 3, 4, 5] | ||
| 74 | result = {i, items[i] for i = 2, 4} | ||
| 75 | assert.same result[2], 2 | ||
| 76 | assert.same result[3], 3 | ||
| 77 | assert.same result[4], 4 | ||
