aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/reverse_index_spec.lua
blob: 396c3b924d8ef04ec3c2946119da439fa2f5a4f5 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
return describe("reverse index", function()
	it("should get last element", function()
		local data = {
			items = {
				1,
				2,
				3,
				4,
				5
			}
		}
		local last
		do
			local _item_0 = data.items
			last = _item_0[#_item_0]
		end
		return assert.same(last, 5)
	end)
	it("should get second last element", function()
		local data = {
			items = {
				1,
				2,
				3,
				4,
				5
			}
		}
		local second_last
		do
			local _item_0 = data.items
			second_last = _item_0[#_item_0 - 1]
		end
		return assert.same(second_last, 4)
	end)
	it("should get third last element", function()
		local data = {
			items = {
				1,
				2,
				3,
				4,
				5
			}
		}
		local third_last
		do
			local _item_0 = data.items
			third_last = _item_0[#_item_0 - 2]
		end
		return assert.same(third_last, 3)
	end)
	it("should set last element", function()
		local data = {
			items = {
				1,
				2,
				3,
				4,
				5
			}
		}
		local _obj_0 = data.items
		_obj_0[#_obj_0] = 10
		return assert.same(data.items[5], 10)
	end)
	it("should set second last element", function()
		local data = {
			items = {
				1,
				2,
				3,
				4,
				5
			}
		}
		local _obj_0 = data.items
		_obj_0[#_obj_0 - 1] = 20
		return assert.same(data.items[4], 20)
	end)
	it("should work with single element", function()
		local tab = {
			42
		}
		return assert.same(tab[#tab], 42)
	end)
	it("should work with empty table", function()
		local tab = { }
		return assert.same(tab[#tab], nil)
	end)
	it("should work in expressions", function()
		local tab = {
			1,
			2,
			3,
			4,
			5
		}
		local result = tab[#tab] + tab[#tab - 1]
		return assert.same(result, 9)
	end)
	it("should support chaining", function()
		local data = {
			items = {
				nested = {
					1,
					2,
					3
				}
			}
		}
		local last
		do
			local _item_0 = data.items.nested
			last = _item_0[#_item_0]
		end
		return assert.same(last, 3)
	end)
	it("should work with string", function()
		local s = "hello"
		return assert.same(s[#s], "o")
	end)
	it("should handle negative offsets", function()
		local tab = {
			1,
			2,
			3,
			4,
			5
		}
		assert.same(tab[#tab - 3], 2)
		return assert.same(tab[#tab - 4], 1)
	end)
	return it("should work in loops", function()
		local tab = {
			1,
			2,
			3,
			4,
			5
		}
		local results = { }
		for i = 0, 2 do
			table.insert(results, tab[#tab - i])
		end
		return assert.same(results, {
			5,
			4,
			3
		})
	end)
end)