aboutsummaryrefslogtreecommitdiff
path: root/spec/outputs/test/table_append_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/outputs/test/table_append_spec.lua')
-rw-r--r--spec/outputs/test/table_append_spec.lua160
1 files changed, 160 insertions, 0 deletions
diff --git a/spec/outputs/test/table_append_spec.lua b/spec/outputs/test/table_append_spec.lua
new file mode 100644
index 0000000..5ce1156
--- /dev/null
+++ b/spec/outputs/test/table_append_spec.lua
@@ -0,0 +1,160 @@
1return describe("table append", function()
2 it("should append single value", function()
3 local tab = { }
4 tab[#tab + 1] = "Value"
5 assert.same(tab[1], "Value")
6 return assert.same(#tab, 1)
7 end)
8 it("should append multiple values", function()
9 local tab = { }
10 tab[#tab + 1] = 1
11 tab[#tab + 1] = 2
12 tab[#tab + 1] = 3
13 return assert.same(tab, {
14 1,
15 2,
16 3
17 })
18 end)
19 it("should append with spread operator", function()
20 local tbA = {
21 1,
22 2,
23 3
24 }
25 local tbB = {
26 4,
27 5,
28 6
29 }
30 local _len_0 = #tbA + 1
31 for _index_0 = 1, #tbB do
32 local _elm_0 = tbB[_index_0]
33 tbA[_len_0], _len_0 = _elm_0, _len_0 + 1
34 end
35 return assert.same(tbA, {
36 1,
37 2,
38 3,
39 4,
40 5,
41 6
42 })
43 end)
44 it("should append table with single element", function()
45 local tab = {
46 1,
47 2
48 }
49 local tb2 = {
50 3
51 }
52 tab[#tab + 1] = table.unpack(tb2)
53 return assert.same(tab, {
54 1,
55 2,
56 3
57 })
58 end)
59 it("should append empty table", function()
60 local tab = {
61 1,
62 2
63 }
64 local tb2 = { }
65 local _len_0 = #tab + 1
66 for _index_0 = 1, #tb2 do
67 local _elm_0 = tb2[_index_0]
68 tab[_len_0], _len_0 = _elm_0, _len_0 + 1
69 end
70 return assert.same(tab, {
71 1,
72 2
73 })
74 end)
75 it("should append nil values", function()
76 local tab = { }
77 tab[#tab + 1] = nil
78 tab[#tab + 1] = "value"
79 assert.same(tab[1], nil)
80 return assert.same(tab[2], "value")
81 end)
82 it("should work in loop", function()
83 local tab = { }
84 for i = 1, 3 do
85 tab[#tab + 1] = i * 2
86 end
87 return assert.same(tab, {
88 2,
89 4,
90 6
91 })
92 end)
93 it("should append with expressions", function()
94 local tab = { }
95 local x = 10
96 tab[#tab + 1] = x + 5
97 return assert.same(tab[1], 15)
98 end)
99 it("should append mixed types", function()
100 local tab = { }
101 tab[#tab + 1] = "string"
102 tab[#tab + 1] = 123
103 tab[#tab + 1] = true
104 tab[#tab + 1] = nil
105 return assert.same(tab, {
106 "string",
107 123,
108 true,
109 nil
110 })
111 end)
112 it("should append to table with existing elements", function()
113 local tab = {
114 1,
115 2,
116 3
117 }
118 tab[#tab + 1] = 4
119 tab[#tab + 1] = 5
120 return assert.same(tab, {
121 1,
122 2,
123 3,
124 4,
125 5
126 })
127 end)
128 it("should work with nested tables", function()
129 local tab = { }
130 tab[#tab + 1] = {
131 a = 1,
132 b = 2
133 }
134 tab[#tab + 1] = {
135 3,
136 4
137 }
138 assert.same(tab[1], {
139 a = 1,
140 b = 2
141 })
142 return assert.same(tab[2], {
143 3,
144 4
145 })
146 end)
147 return it("should append function results", function()
148 local fn
149 fn = function()
150 return 1, 2, 3
151 end
152 local tab = { }
153 tab[#tab + 1] = fn()
154 return assert.same(tab, {
155 1,
156 2,
157 3
158 })
159 end)
160end)