diff options
Diffstat (limited to 'spec/outputs/test/tables_advanced_spec.lua')
| -rw-r--r-- | spec/outputs/test/tables_advanced_spec.lua | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/spec/outputs/test/tables_advanced_spec.lua b/spec/outputs/test/tables_advanced_spec.lua new file mode 100644 index 0000000..de36953 --- /dev/null +++ b/spec/outputs/test/tables_advanced_spec.lua | |||
| @@ -0,0 +1,260 @@ | |||
| 1 | return describe("advanced tables", function() | ||
| 2 | it("should create table with implicit keys", function() | ||
| 3 | local hair = "golden" | ||
| 4 | local height = 200 | ||
| 5 | local person = { | ||
| 6 | hair = hair, | ||
| 7 | height = height, | ||
| 8 | shoe_size = 40 | ||
| 9 | } | ||
| 10 | assert.same(person.hair, "golden") | ||
| 11 | return assert.same(person.height, 200) | ||
| 12 | end) | ||
| 13 | it("should work with computed keys", function() | ||
| 14 | local t = { | ||
| 15 | [1 + 2] = "hello", | ||
| 16 | ["key_" .. "suffix"] = "value" | ||
| 17 | } | ||
| 18 | assert.same(t[3], "hello") | ||
| 19 | return assert.same(t["key_suffix"], "value") | ||
| 20 | end) | ||
| 21 | it("should support keyword keys", function() | ||
| 22 | local tbl = { | ||
| 23 | ["do"] = "something", | ||
| 24 | ["end"] = "hunger", | ||
| 25 | ["function"] = "test" | ||
| 26 | } | ||
| 27 | assert.same(tbl["do"], "something") | ||
| 28 | assert.same(tbl["end"], "hunger") | ||
| 29 | return assert.same(tbl["function"], "test") | ||
| 30 | end) | ||
| 31 | it("should handle array syntax with mixed content", function() | ||
| 32 | local tb = { | ||
| 33 | 1, | ||
| 34 | 2, | ||
| 35 | 3, | ||
| 36 | name = "superman", | ||
| 37 | 4, | ||
| 38 | 5, | ||
| 39 | 6 | ||
| 40 | } | ||
| 41 | assert.same(tb[1], 1) | ||
| 42 | assert.same(tb.name, "superman") | ||
| 43 | return assert.same(tb[4], 4) | ||
| 44 | end) | ||
| 45 | it("should work with single line table literals", function() | ||
| 46 | local tb = { | ||
| 47 | dance = "Tango", | ||
| 48 | partner = "none" | ||
| 49 | } | ||
| 50 | assert.same(tb.dance, "Tango") | ||
| 51 | return assert.same(tb.partner, "none") | ||
| 52 | end) | ||
| 53 | it("should support nested tables", function() | ||
| 54 | local tb = { | ||
| 55 | outer = { | ||
| 56 | inner = { | ||
| 57 | value = 42 | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | return assert.same(tb.outer.inner.value, 42) | ||
| 62 | end) | ||
| 63 | it("should handle table without braces", function() | ||
| 64 | local profile = { | ||
| 65 | height = "4 feet", | ||
| 66 | shoe_size = 13, | ||
| 67 | favorite_foods = { | ||
| 68 | "ice cream", | ||
| 69 | "donuts" | ||
| 70 | } | ||
| 71 | } | ||
| 72 | assert.same(profile.height, "4 feet") | ||
| 73 | return assert.same(profile.shoe_size, 13) | ||
| 74 | end) | ||
| 75 | it("should work with colon syntax for keys", function() | ||
| 76 | local t = { | ||
| 77 | name = "Bill", | ||
| 78 | age = 200, | ||
| 79 | ["favorite food"] = "rice" | ||
| 80 | } | ||
| 81 | assert.same(t.name, "Bill") | ||
| 82 | return assert.same(t["favorite food"], "rice") | ||
| 83 | end) | ||
| 84 | it("should support implicit object in table", function() | ||
| 85 | local tb = { | ||
| 86 | name = "abc", | ||
| 87 | values = { | ||
| 88 | "a", | ||
| 89 | "b", | ||
| 90 | "c" | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return assert.same(tb.values, { | ||
| 94 | "a", | ||
| 95 | "b", | ||
| 96 | "c" | ||
| 97 | }) | ||
| 98 | end) | ||
| 99 | it("should handle array only table", function() | ||
| 100 | local some_values = { | ||
| 101 | 1, | ||
| 102 | 2, | ||
| 103 | 3, | ||
| 104 | 4 | ||
| 105 | } | ||
| 106 | assert.same(some_values[1], 1) | ||
| 107 | return assert.same(some_values[4], 4) | ||
| 108 | end) | ||
| 109 | it("should work with trailing comma", function() | ||
| 110 | local list_with_one = { | ||
| 111 | 1 | ||
| 112 | } | ||
| 113 | return assert.same(list_with_one[1], 1) | ||
| 114 | end) | ||
| 115 | it("should support table spreading", function() | ||
| 116 | local a = { | ||
| 117 | 1, | ||
| 118 | 2, | ||
| 119 | 3, | ||
| 120 | x = 1 | ||
| 121 | } | ||
| 122 | local b = { | ||
| 123 | 4, | ||
| 124 | 5, | ||
| 125 | y = 1 | ||
| 126 | } | ||
| 127 | local merge | ||
| 128 | do | ||
| 129 | local _tab_0 = { } | ||
| 130 | local _idx_0 = 1 | ||
| 131 | for _key_0, _value_0 in pairs(a) do | ||
| 132 | if _idx_0 == _key_0 then | ||
| 133 | _tab_0[#_tab_0 + 1] = _value_0 | ||
| 134 | _idx_0 = _idx_0 + 1 | ||
| 135 | else | ||
| 136 | _tab_0[_key_0] = _value_0 | ||
| 137 | end | ||
| 138 | end | ||
| 139 | local _idx_1 = 1 | ||
| 140 | for _key_0, _value_0 in pairs(b) do | ||
| 141 | if _idx_1 == _key_0 then | ||
| 142 | _tab_0[#_tab_0 + 1] = _value_0 | ||
| 143 | _idx_1 = _idx_1 + 1 | ||
| 144 | else | ||
| 145 | _tab_0[_key_0] = _value_0 | ||
| 146 | end | ||
| 147 | end | ||
| 148 | merge = _tab_0 | ||
| 149 | end | ||
| 150 | assert.same(merge[1], 1) | ||
| 151 | assert.same(merge[4], 4) | ||
| 152 | assert.same(merge.x, 1) | ||
| 153 | return assert.same(merge.y, 1) | ||
| 154 | end) | ||
| 155 | it("should handle mixed spread", function() | ||
| 156 | local parts = { | ||
| 157 | "shoulders", | ||
| 158 | "knees" | ||
| 159 | } | ||
| 160 | local lyrics | ||
| 161 | do | ||
| 162 | local _tab_0 = { | ||
| 163 | "head" | ||
| 164 | } | ||
| 165 | local _idx_0 = 1 | ||
| 166 | for _key_0, _value_0 in pairs(parts) do | ||
| 167 | if _idx_0 == _key_0 then | ||
| 168 | _tab_0[#_tab_0 + 1] = _value_0 | ||
| 169 | _idx_0 = _idx_0 + 1 | ||
| 170 | else | ||
| 171 | _tab_0[_key_0] = _value_0 | ||
| 172 | end | ||
| 173 | end | ||
| 174 | _tab_0[#_tab_0 + 1] = "and" | ||
| 175 | _tab_0[#_tab_0 + 1] = "toes" | ||
| 176 | lyrics = _tab_0 | ||
| 177 | end | ||
| 178 | return assert.same(lyrics, { | ||
| 179 | "head", | ||
| 180 | "shoulders", | ||
| 181 | "knees", | ||
| 182 | "and", | ||
| 183 | "toes" | ||
| 184 | }) | ||
| 185 | end) | ||
| 186 | it("should work with metatable creation", function() | ||
| 187 | local mt = { } | ||
| 188 | local add | ||
| 189 | add = function(self, right) | ||
| 190 | return setmetatable({ | ||
| 191 | value = self.value + right.value | ||
| 192 | }, mt) | ||
| 193 | end | ||
| 194 | mt.__add = add | ||
| 195 | local a = setmetatable({ | ||
| 196 | value = 1 | ||
| 197 | }, mt) | ||
| 198 | local b = { | ||
| 199 | value = 2 | ||
| 200 | } | ||
| 201 | setmetatable(b, mt) | ||
| 202 | local c = a + b | ||
| 203 | return assert.same(c.value, 3) | ||
| 204 | end) | ||
| 205 | it("should support metatable accessing", function() | ||
| 206 | local tb = setmetatable({ }, { | ||
| 207 | ["value"] = 123 | ||
| 208 | }) | ||
| 209 | getmetatable(tb).__index = getmetatable(tb) | ||
| 210 | return assert.same(tb.value, 123) | ||
| 211 | end) | ||
| 212 | it("should handle metatable destructuring", function() | ||
| 213 | local tb = setmetatable({ | ||
| 214 | item = "test", | ||
| 215 | new = function() | ||
| 216 | return "created" | ||
| 217 | end, | ||
| 218 | }, { | ||
| 219 | __close = function() | ||
| 220 | return "closed" | ||
| 221 | end | ||
| 222 | }) | ||
| 223 | local item, new = tb.item, tb.new | ||
| 224 | local close = getmetatable(tb).__close | ||
| 225 | assert.same(item, "test") | ||
| 226 | assert.same(new(), "created") | ||
| 227 | return assert.same(close(), "closed") | ||
| 228 | end) | ||
| 229 | it("should work with string keys directly", function() | ||
| 230 | local t = { | ||
| 231 | ["hello world"] = true, | ||
| 232 | ["test-key"] = "value" | ||
| 233 | } | ||
| 234 | assert.is_true(t["hello world"]) | ||
| 235 | return assert.same(t["test-key"], "value") | ||
| 236 | end) | ||
| 237 | it("should support number keys", function() | ||
| 238 | local t = { | ||
| 239 | [10] = "ten", | ||
| 240 | [20] = "twenty" | ||
| 241 | } | ||
| 242 | assert.same(t[10], "ten") | ||
| 243 | return assert.same(t[20], "twenty") | ||
| 244 | end) | ||
| 245 | it("should handle empty tables", function() | ||
| 246 | local empty = { } | ||
| 247 | return assert.same(#empty, 0) | ||
| 248 | end) | ||
| 249 | return it("should work with table literals in function calls", function() | ||
| 250 | local fn | ||
| 251 | fn = function(tb) | ||
| 252 | return tb.x + tb.y | ||
| 253 | end | ||
| 254 | local result = fn({ | ||
| 255 | x = 10, | ||
| 256 | y = 20 | ||
| 257 | }) | ||
| 258 | return assert.same(result, 30) | ||
| 259 | end) | ||
| 260 | end) | ||
