|
| 1 | +local GC = require('apicast.gc') |
| 2 | + |
| 3 | +describe('GC', function() |
| 4 | + describe('.set_metatable_gc', function() |
| 5 | + it('enables GC on a table', function() |
| 6 | + local test_table = { 1, 2, 3 } |
| 7 | + local test_metatable = { __gc = function() end } |
| 8 | + spy.on(test_metatable, '__gc') |
| 9 | + |
| 10 | + assert(GC.set_metatable_gc(test_table, test_metatable)) |
| 11 | + collectgarbage() |
| 12 | + |
| 13 | + assert.spy(test_metatable.__gc).was_called() |
| 14 | + end) |
| 15 | + |
| 16 | + it('returns an object where we can access, add, and delete elements', function() |
| 17 | + local test_table = { 1, 2, 3, some_key = 'some_val' } |
| 18 | + local test_metatable = { __gc = function() end } |
| 19 | + |
| 20 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 21 | + |
| 22 | + assert.equals(3, #table_with_gc) |
| 23 | + assert.equals(1, table_with_gc[1]) |
| 24 | + assert.equals('some_val', table_with_gc.some_key) |
| 25 | + |
| 26 | + table_with_gc.new_key = 'new_val' |
| 27 | + assert.equals('new_val', table_with_gc.new_key) |
| 28 | + |
| 29 | + table_with_gc.new_key = nil |
| 30 | + assert.is_nil(table_with_gc.new_key) |
| 31 | + end) |
| 32 | + |
| 33 | + it('returns an object that responds to ipairs', function() |
| 34 | + local test_table = { 1, 2, 3, some_key = 'some_val' } |
| 35 | + local test_metatable = { __gc = function() end } |
| 36 | + |
| 37 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 38 | + |
| 39 | + local res = {} |
| 40 | + for _, val in ipairs(table_with_gc) do |
| 41 | + table.insert(res, val) |
| 42 | + end |
| 43 | + |
| 44 | + assert.same({ 1, 2, 3 }, res) |
| 45 | + end) |
| 46 | + |
| 47 | + it('returns an object that responds to pairs', function() |
| 48 | + local test_table = { 1, 2, 3, some_key = 'some_val' } |
| 49 | + local test_metatable = { __gc = function() end } |
| 50 | + |
| 51 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 52 | + |
| 53 | + local res = {} |
| 54 | + for k, v in pairs(table_with_gc) do |
| 55 | + res[k] = v |
| 56 | + end |
| 57 | + |
| 58 | + assert.same({ [1] = 1, [2] = 2, [3] = 3, some_key = 'some_val' }, res) |
| 59 | + end) |
| 60 | + |
| 61 | + it('returns an object that respects the __call in the mt passed in the params', function() |
| 62 | + local test_table = { 1, 2, 3 } |
| 63 | + local test_metatable = { |
| 64 | + __gc = function() end, |
| 65 | + __call = function(_, ...) |
| 66 | + local res = 0 |
| 67 | + |
| 68 | + for _, val in ipairs(table.pack(...)) do |
| 69 | + res = res + val |
| 70 | + end |
| 71 | + |
| 72 | + return res |
| 73 | + end |
| 74 | + } |
| 75 | + |
| 76 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 77 | + |
| 78 | + assert.equals(3, table_with_gc(1, 2)) |
| 79 | + end) |
| 80 | + |
| 81 | + it('returns an object that respects the __tostring in the mt passed in the params', function() |
| 82 | + local test_table = { 1, 2, 3 } |
| 83 | + local test_metatable = { |
| 84 | + __gc = function() end, |
| 85 | + __tostring = function() return '123' end |
| 86 | + } |
| 87 | + |
| 88 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 89 | + |
| 90 | + assert.equals('123', tostring(table_with_gc)) |
| 91 | + end) |
| 92 | + |
| 93 | + it('returns an object that returns an error when it cannot be called', function() |
| 94 | + local test_table = { 1, 2, 3 } |
| 95 | + local test_metatable = { __gc = function() end } |
| 96 | + |
| 97 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 98 | + |
| 99 | + local ok, err = pcall(table_with_gc, 1, 2) |
| 100 | + |
| 101 | + assert.falsy(ok) |
| 102 | + |
| 103 | + -- Test that the error is meaningful |
| 104 | + assert.equals('attempt to call a table value', err) |
| 105 | + end) |
| 106 | + |
| 107 | + it('returns an object that has as a metatable the one sent in the params', function() |
| 108 | + local test_table = { 1, 2, 3 } |
| 109 | + local test_metatable = { __gc = function() end } |
| 110 | + |
| 111 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 112 | + |
| 113 | + assert.same(test_metatable, getmetatable(table_with_gc)) |
| 114 | + end) |
| 115 | + |
| 116 | + it('returns an object that respects the __index in the mt passed in the params', function() |
| 117 | + local test_table = { 1, 2, 3 } |
| 118 | + local test_metatable = { |
| 119 | + __gc = function() end, |
| 120 | + __index = { some_func = function() return 'abc' end } |
| 121 | + } |
| 122 | + local table_with_gc = GC.set_metatable_gc(test_table, test_metatable) |
| 123 | + |
| 124 | + assert.equals('abc', table_with_gc:some_func()) |
| 125 | + end) |
| 126 | + end) |
| 127 | +end) |
0 commit comments