|
| 1 | +local mods = require("mods") |
| 2 | + |
| 3 | +local repr = mods.repr |
| 4 | +local fmt = string.format |
| 5 | + |
| 6 | +describe("mods.repr", function() |
| 7 | + local fn = function() end |
| 8 | + local co = coroutine.create(fn) |
| 9 | + local keywords = mods.keyword.kwlist() |
| 10 | + |
| 11 | + -- stylua: ignore |
| 12 | + local tests = { |
| 13 | + ---------input--------|---------------------expected--------------------- |
| 14 | + { nil , "nil" }, |
| 15 | + { true , "true" }, |
| 16 | + { false , "false" }, |
| 17 | + { 42 , "42" }, |
| 18 | + { 'He said "hi"' , [['He said "hi"']] }, |
| 19 | + { { hello = "world" } , '{\n hello = "world"\n}' }, |
| 20 | + { { "a", "b", "c" } , '{\n [1] = "a",\n [2] = "b",\n [3] = "c"\n}' }, |
| 21 | + { {} , '{}' }, |
| 22 | + { { { {} } } , '{\n [1] = {\n [1] = {}\n }\n}' }, |
| 23 | + { fn , tostring(fn) }, |
| 24 | + { co , tostring(co) }, |
| 25 | + } |
| 26 | + |
| 27 | + for i = 1, #tests do |
| 28 | + local input, expected = unpack(tests[i], 1, 2) |
| 29 | + it(fmt("repr (%s)", inspect(input)), function() |
| 30 | + local res = repr(input) |
| 31 | + assert.are.equal(expected, res) |
| 32 | + end) |
| 33 | + end |
| 34 | + |
| 35 | + for _, v in ipairs(keywords) do |
| 36 | + it(fmt("repr(%q) brackets reserved keys", v), function() |
| 37 | + local expected = '{\n ["' .. v .. '"] = true\n}' |
| 38 | + assert.are_equal(expected, repr({ [v] = true })) |
| 39 | + end) |
| 40 | + end |
| 41 | + |
| 42 | + it("renders complex nested tables with shared refs and cycles", function() |
| 43 | + local root = { title = "root" } |
| 44 | + local child = { name = "child" } |
| 45 | + local leaf = { value = 99 } |
| 46 | + root.child = child |
| 47 | + root.self = root |
| 48 | + root.shared_a = leaf |
| 49 | + root.shared_b = leaf |
| 50 | + root.list = { child, { back = root } } |
| 51 | + child.parent = root |
| 52 | + child.link = leaf |
| 53 | + leaf.owner = child |
| 54 | + |
| 55 | + local expected = [[ |
| 56 | +{ |
| 57 | + child = { |
| 58 | + link = { |
| 59 | + owner = <cycle>, |
| 60 | + value = 99 |
| 61 | + }, |
| 62 | + name = "child", |
| 63 | + parent = <cycle> |
| 64 | + }, |
| 65 | + list = { |
| 66 | + [1] = { |
| 67 | + link = { |
| 68 | + owner = <cycle>, |
| 69 | + value = 99 |
| 70 | + }, |
| 71 | + name = "child", |
| 72 | + parent = <cycle> |
| 73 | + }, |
| 74 | + [2] = { |
| 75 | + back = <cycle> |
| 76 | + } |
| 77 | + }, |
| 78 | + self = <cycle>, |
| 79 | + shared_a = { |
| 80 | + owner = { |
| 81 | + link = <cycle>, |
| 82 | + name = "child", |
| 83 | + parent = <cycle> |
| 84 | + }, |
| 85 | + value = 99 |
| 86 | + }, |
| 87 | + shared_b = { |
| 88 | + owner = { |
| 89 | + link = <cycle>, |
| 90 | + name = "child", |
| 91 | + parent = <cycle> |
| 92 | + }, |
| 93 | + value = 99 |
| 94 | + }, |
| 95 | + title = "root" |
| 96 | +}]] |
| 97 | + |
| 98 | + local res = repr(root) |
| 99 | + assert.are_equal(expected, res) |
| 100 | + end) |
| 101 | +end) |
0 commit comments