aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/table_append_spec.lua
blob: 5ce11560cf4105e3931b57b698295eae856ead8a (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
153
154
155
156
157
158
159
160
return describe("table append", function()
	it("should append single value", function()
		local tab = { }
		tab[#tab + 1] = "Value"
		assert.same(tab[1], "Value")
		return assert.same(#tab, 1)
	end)
	it("should append multiple values", function()
		local tab = { }
		tab[#tab + 1] = 1
		tab[#tab + 1] = 2
		tab[#tab + 1] = 3
		return assert.same(tab, {
			1,
			2,
			3
		})
	end)
	it("should append with spread operator", function()
		local tbA = {
			1,
			2,
			3
		}
		local tbB = {
			4,
			5,
			6
		}
		local _len_0 = #tbA + 1
		for _index_0 = 1, #tbB do
			local _elm_0 = tbB[_index_0]
			tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
		end
		return assert.same(tbA, {
			1,
			2,
			3,
			4,
			5,
			6
		})
	end)
	it("should append table with single element", function()
		local tab = {
			1,
			2
		}
		local tb2 = {
			3
		}
		tab[#tab + 1] = table.unpack(tb2)
		return assert.same(tab, {
			1,
			2,
			3
		})
	end)
	it("should append empty table", function()
		local tab = {
			1,
			2
		}
		local tb2 = { }
		local _len_0 = #tab + 1
		for _index_0 = 1, #tb2 do
			local _elm_0 = tb2[_index_0]
			tab[_len_0], _len_0 = _elm_0, _len_0 + 1
		end
		return assert.same(tab, {
			1,
			2
		})
	end)
	it("should append nil values", function()
		local tab = { }
		tab[#tab + 1] = nil
		tab[#tab + 1] = "value"
		assert.same(tab[1], nil)
		return assert.same(tab[2], "value")
	end)
	it("should work in loop", function()
		local tab = { }
		for i = 1, 3 do
			tab[#tab + 1] = i * 2
		end
		return assert.same(tab, {
			2,
			4,
			6
		})
	end)
	it("should append with expressions", function()
		local tab = { }
		local x = 10
		tab[#tab + 1] = x + 5
		return assert.same(tab[1], 15)
	end)
	it("should append mixed types", function()
		local tab = { }
		tab[#tab + 1] = "string"
		tab[#tab + 1] = 123
		tab[#tab + 1] = true
		tab[#tab + 1] = nil
		return assert.same(tab, {
			"string",
			123,
			true,
			nil
		})
	end)
	it("should append to table with existing elements", function()
		local tab = {
			1,
			2,
			3
		}
		tab[#tab + 1] = 4
		tab[#tab + 1] = 5
		return assert.same(tab, {
			1,
			2,
			3,
			4,
			5
		})
	end)
	it("should work with nested tables", function()
		local tab = { }
		tab[#tab + 1] = {
			a = 1,
			b = 2
		}
		tab[#tab + 1] = {
			3,
			4
		}
		assert.same(tab[1], {
			a = 1,
			b = 2
		})
		return assert.same(tab[2], {
			3,
			4
		})
	end)
	return it("should append function results", function()
		local fn
		fn = function()
			return 1, 2, 3
		end
		local tab = { }
		tab[#tab + 1] = fn()
		return assert.same(tab, {
			1,
			2,
			3
		})
	end)
end)