From 08e659fc4a9b6d3d8f59429f3915f628c5e50096 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 9 Jun 2017 15:49:15 +0200 Subject: [PATCH] Created temporary stopgap for when __index throws errors. --- analyze_structure.lua | 8 ++++++++ test/test_analyze_structure.lua | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/analyze_structure.lua b/analyze_structure.lua index 4d7a900..9b0571a 100644 --- a/analyze_structure.lua +++ b/analyze_structure.lua @@ -93,6 +93,14 @@ local function nr_elements_in_table (t) end local function nr_elements_in_seq (t) + assert(type(t) == 'table') + + if getmetatable(t) and getmetatable(t).__index then + return 0, false -- FIXME: Temporary stopgap for when __index throws an + -- error. I think we need to clone the numbers part of the table, to + -- fix this. + end + local i, last_elem_i, nr_elems, has_holes = 0, 0, 0, false while i <= last_elem_i + 2 do i = i + 1 diff --git a/test/test_analyze_structure.lua b/test/test_analyze_structure.lua index 1385c56..edba5c7 100644 --- a/test/test_analyze_structure.lua +++ b/test/test_analyze_structure.lua @@ -172,6 +172,23 @@ SUITE:addTest('Recursive Numeration, Through Keys', function () assert(info[input].marker == 1) end) +-------------------------------------------------------------------------------- +-- Metatable shennanigens. + +SUITE:addTest('Wont crash, even though metatable.__index throws errors', function () + local input = setmetatable({ 'hi', 'hello' }, {__index = function (_, k) error('Undefined access on key: '..k) end}) + local info = analyze_structure(input) + + assert(true) +end) + +SUITE:addTest('Can count elements, even though metatable.__index throws errors', function () + local input = setmetatable({ 'hi', 'hello' }, {__index = function (_, k) error('Undefined access on key: '..k) end}) + local info = analyze_structure(input) + + assert_equal(info[input].seq_elems, 2) +end) + -------------------------------------------------------------------------------- -- API stuff