diff options
Diffstat (limited to 'spec/inputs/test/reverse_index_spec.yue')
| -rw-r--r-- | spec/inputs/test/reverse_index_spec.yue | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/spec/inputs/test/reverse_index_spec.yue b/spec/inputs/test/reverse_index_spec.yue new file mode 100644 index 0000000..be67261 --- /dev/null +++ b/spec/inputs/test/reverse_index_spec.yue | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | describe "reverse index", -> | ||
| 2 | it "should get last element", -> | ||
| 3 | data = {items: [1, 2, 3, 4, 5]} | ||
| 4 | last = data.items[#] | ||
| 5 | assert.same last, 5 | ||
| 6 | |||
| 7 | it "should get second last element", -> | ||
| 8 | data = {items: [1, 2, 3, 4, 5]} | ||
| 9 | second_last = data.items[#-1] | ||
| 10 | assert.same second_last, 4 | ||
| 11 | |||
| 12 | it "should get third last element", -> | ||
| 13 | data = {items: [1, 2, 3, 4, 5]} | ||
| 14 | third_last = data.items[#-2] | ||
| 15 | assert.same third_last, 3 | ||
| 16 | |||
| 17 | it "should set last element", -> | ||
| 18 | data = {items: [1, 2, 3, 4, 5]} | ||
| 19 | data.items[#] = 10 | ||
| 20 | assert.same data.items[5], 10 | ||
| 21 | |||
| 22 | it "should set second last element", -> | ||
| 23 | data = {items: [1, 2, 3, 4, 5]} | ||
| 24 | data.items[#-1] = 20 | ||
| 25 | assert.same data.items[4], 20 | ||
| 26 | |||
| 27 | it "should work with single element", -> | ||
| 28 | tab = {42} | ||
| 29 | assert.same tab[#], 42 | ||
| 30 | |||
| 31 | it "should work with empty table", -> | ||
| 32 | tab = [] | ||
| 33 | assert.same tab[#], nil | ||
| 34 | |||
| 35 | it "should work in expressions", -> | ||
| 36 | tab = [1, 2, 3, 4, 5] | ||
| 37 | result = tab[#] + tab[#-1] | ||
| 38 | assert.same result, 9 | ||
| 39 | |||
| 40 | it "should support chaining", -> | ||
| 41 | data = {items: {nested: [1, 2, 3]}} | ||
| 42 | last = data.items.nested[#] | ||
| 43 | assert.same last, 3 | ||
| 44 | |||
| 45 | it "should work with string", -> | ||
| 46 | s = "hello" | ||
| 47 | assert.same s[#], "o" | ||
| 48 | |||
| 49 | it "should handle negative offsets", -> | ||
| 50 | tab = [1, 2, 3, 4, 5] | ||
| 51 | assert.same tab[#-3], 2 | ||
| 52 | assert.same tab[#-4], 1 | ||
| 53 | |||
| 54 | it "should work in loops", -> | ||
| 55 | tab = [1, 2, 3, 4, 5] | ||
| 56 | results = [] | ||
| 57 | for i = 0, 2 | ||
| 58 | table.insert results, tab[#-i] | ||
| 59 | assert.same results, {5, 4, 3} | ||
