local SUITE = require('TestSuite').new('analyze_structure') SUITE:setEnviroment { analyze_structure = require('analyze_structure'), 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('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) SUITE:addTest('Very short/empty table', function () local input = { } local table_info = analyze_structure(input)[input] assert(table_info.is_short == true) end) SUITE:addTest('Very short table', function () local input = { 1 } local table_info = analyze_structure(input)[input] assert(table_info.is_short == true) end) SUITE:addTest('Recursive tables are not simple', function () local input = { {} } local table_info = analyze_structure(input)[input] assert(table_info.is_short == false) end) SUITE:addTest('Short strings are simple', function () local input = { 'hello' } local table_info = analyze_structure(input)[input] assert(table_info.is_short == true) end) SUITE:addTest('Long strings are not', function () local input = { 'hello world' } local table_info = analyze_structure(input)[input] assert(table_info.is_short == false) end) SUITE:addTest('Even maps can be simple!', function () local input = { a = 4 } local table_info = analyze_structure(input)[input] assert(table_info.is_short == true) 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) -------------------------------------------------------------------------------- -- API stuff SUITE:addTest('next_mark does not escape', function () local info = analyze_structure( {} ) assert(not info.next_mark) end) -------------------------------------------------------------------------------- return SUITE