1
0
pretty/test/test_analyze_structure.lua
Jon Michael Aanes 87b12e15b2 Fixed issues with analýse_structure.lua
The most significant was a misclassification of tables with number
exclusive keys, with huge jumps between each one.
Example: { 1, [300] = 300 }

This was caused by logic faults in `get_table_info`, which has been fixed.

When solving above issue, a two new issues appeared:

1.	Due to a logic fault, the count of number of elements in a table was wrong,
	leading to issues with tables with boolean keys. This was fixed with simple
	logic changes.
2.	Some very small tables (1 element) would be classified as sets, and thus
	rendered wrongly. This was fixed by adding a MINIMUM_NUMBER_OF_SET_ELEMENTS
	constant.
2017-04-14 13:06:43 +02:00

228 lines
7.0 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)
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