aboutsummaryrefslogtreecommitdiff
path: root/spec/inputs/test/reverse_index_spec.yue
blob: be6726167e521f8db6118ab98ef6d15162e69034 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
describe "reverse index", ->
	it "should get last element", ->
		data = {items: [1, 2, 3, 4, 5]}
		last = data.items[#]
		assert.same last, 5

	it "should get second last element", ->
		data = {items: [1, 2, 3, 4, 5]}
		second_last = data.items[#-1]
		assert.same second_last, 4

	it "should get third last element", ->
		data = {items: [1, 2, 3, 4, 5]}
		third_last = data.items[#-2]
		assert.same third_last, 3

	it "should set last element", ->
		data = {items: [1, 2, 3, 4, 5]}
		data.items[#] = 10
		assert.same data.items[5], 10

	it "should set second last element", ->
		data = {items: [1, 2, 3, 4, 5]}
		data.items[#-1] = 20
		assert.same data.items[4], 20

	it "should work with single element", ->
		tab = {42}
		assert.same tab[#], 42

	it "should work with empty table", ->
		tab = []
		assert.same tab[#], nil

	it "should work in expressions", ->
		tab = [1, 2, 3, 4, 5]
		result = tab[#] + tab[#-1]
		assert.same result, 9

	it "should support chaining", ->
		data = {items: {nested: [1, 2, 3]}}
		last = data.items.nested[#]
		assert.same last, 3

	it "should work with string", ->
		s = "hello"
		assert.same s[#], "o"

	it "should handle negative offsets", ->
		tab = [1, 2, 3, 4, 5]
		assert.same tab[#-3], 2
		assert.same tab[#-4], 1

	it "should work in loops", ->
		tab = [1, 2, 3, 4, 5]
		results = []
		for i = 0, 2
			table.insert results, tab[#-i]
		assert.same results, {5, 4, 3}