203 lines
6.6 KiB
Lua
203 lines
6.6 KiB
Lua
|
|
local SUITE = require('TestSuite').new('analyze_structure')
|
|
SUITE:setEnviroment {
|
|
analyze_structure = require('analyze_structure')[1],
|
|
TABLE_TYPE = require('table_type')
|
|
}
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- analyze_structure
|
|
|
|
SUITE:addTest('Empty Table', function ()
|
|
local input = {} -- Empty!
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.EMPTY, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.has_seq == false)
|
|
assert(table_info.has_map == false)
|
|
end)
|
|
|
|
SUITE:addTest('Sequence', function ()
|
|
local input = { 1, 2, 3 }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.SEQUENCE, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.has_seq == true)
|
|
assert(table_info.has_map == false)
|
|
end)
|
|
|
|
SUITE:addTest('Sequence with holes', function ()
|
|
local input = { 1, nil, 3 }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.SEQUENCE, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.has_seq == true)
|
|
assert(table_info.has_map == false)
|
|
assert(table_info.has_holes == true)
|
|
end)
|
|
|
|
SUITE:addTest('Sequence with hole on start', function ()
|
|
local input = { nil, 2, 3 }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.SEQUENCE, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.has_seq == true)
|
|
assert(table_info.has_map == false)
|
|
assert(table_info.has_holes == true)
|
|
end)
|
|
|
|
SUITE:addTest('Pure Map', function ()
|
|
local input = { a = 1, [true] = 2, c = 3 }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.PURE_MAP, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.has_seq == false)
|
|
assert(table_info.has_map == true)
|
|
end)
|
|
|
|
SUITE:addTest('Boolean set', function ()
|
|
local input = { [true] = true, [false] = false }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.SET, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.has_seq == false)
|
|
assert(table_info.has_map == true)
|
|
end)
|
|
|
|
SUITE:addTest('A Mixed table', function ()
|
|
local input = { 300, [300] = 1 }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.has_seq == true)
|
|
assert(table_info.has_map == true)
|
|
assert(table_info.type == TABLE_TYPE.MIXED, 'Returned bad type: '..table_info.type)
|
|
end)
|
|
|
|
SUITE:addTest('String Map', function ()
|
|
local input = { a = 1, b = 2, c = 3 }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.STRING_MAP, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.has_seq == false)
|
|
assert(table_info.has_map == true)
|
|
end)
|
|
|
|
SUITE:addTest('Set', function ()
|
|
local input = { a = true, b = true, c = true }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.SET, 'Returned bad type: '..table_info.type)
|
|
assert(table_info.is_set == true)
|
|
end)
|
|
|
|
SUITE:addTest('Tabular of sequences', function ()
|
|
local input = { a = {1, 2, 3}, b = {4, 5, 6}, c = {7, 8, 9} }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.is_tabular == true)
|
|
end)
|
|
|
|
SUITE:addTest('Tabular of maps', function ()
|
|
local input = { a = {a = 1, b = 2}, b = {a = 3, b = 4}, c = {a = 2, b = 7} }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.is_tabular == true)
|
|
end)
|
|
|
|
SUITE:addTest('Not Tabular, due to no-sub-tables', function ()
|
|
local input = { a = 1, b = {4}, c = 7 }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.is_tabular == false)
|
|
end)
|
|
|
|
SUITE:addTest('Not Tabular, due to not being identical sub-tables', function ()
|
|
local input = { a = { a = 1 }, b = { b = 2 }, c = { c = 3 } }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.is_tabular == false)
|
|
end)
|
|
|
|
SUITE:addTest('Not Tabular, due to varying lengths', function ()
|
|
local input = { { 1 }, { 2, 3 }, { 4, 5, 6 } }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.is_tabular == false)
|
|
end)
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- Corner cases.
|
|
|
|
SUITE:addTest('goto is special', function ()
|
|
local input = { ['goto'] = 'hi' }
|
|
local table_info = analyze_structure(input)[input]
|
|
|
|
assert(table_info.type == TABLE_TYPE.PURE_MAP, 'Returned bad type: '..table_info.type)
|
|
end)
|
|
|
|
--------------------------------------------------------------------------------
|
|
-- analyze_structure
|
|
|
|
SUITE:addTest('Recursive Numeration, Simple', function ()
|
|
local input = {}
|
|
input[1] = input
|
|
local info = analyze_structure(input)
|
|
|
|
assert(info[input].marker == 1)
|
|
end)
|
|
|
|
SUITE:addTest('Recursive Numeration, Multiple-appear', function ()
|
|
local input = { {}, { {} } }
|
|
input[1][1] = input[2][1]
|
|
local info = analyze_structure(input)
|
|
|
|
assert(info[input[1][1]].marker == 1)
|
|
end)
|
|
|
|
SUITE:addTest('Recursive Numeration, Multiple at once', function ()
|
|
local input = { {}, { {} } }
|
|
input[1][1] = input[2][1]
|
|
input[1][2] = input
|
|
local info = analyze_structure(input)
|
|
|
|
assert(type(info[input[1][1]].marker) == 'number')
|
|
assert(type(info[input].marker) == 'number')
|
|
end)
|
|
|
|
SUITE:addTest('Recursive Numeration, Through Keys', function ()
|
|
local input = { }
|
|
input[input] = input
|
|
local info = analyze_structure(input)
|
|
|
|
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
|
|
|
|
SUITE:addTest('next_mark does not escape', function ()
|
|
local info = analyze_structure( {} )
|
|
assert(not info.next_mark)
|
|
end)
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
return SUITE
|