1
0
pretty/test/test_sorting.lua

251 lines
7.4 KiB
Lua
Raw Permalink Normal View History

2017-07-17 11:36:56 +00:00
local SUITE = require 'TestSuite' 'sorting'
SUITE:setEnvironment{
pretty = require('pretty')
}
--------------------------------------------------------------------------------
-- Compat
if not loadstring then loadstring = load end -- Lua 5.3 compat
local HAS_ADV_GETLOCAL = not not debug.getinfo(1, 'u').nparams -- Lua 5.1 compat
local HAS_UNICODE_IDEN = not not loadstring 'local ϕ = 1; return ϕ' -- Lua 5.1 compat
local HAS_JIT_LIBRARY = type(rawget(_G, 'jit')) == 'table' -- Non-LuaJIT compat
--
local function format_test (t)
SUITE:addTest(t.name or t.expect:gsub('[ \n\t]+', ' '), function ()
assert_equal(t.expect, pretty(t.input, t.options))
end, { line = debug.getinfo(2).currentline })
end
--------------------------------------------------------------------------------
-- Table Sorting
format_test {
input = { 'c', 'b', 'a' },
expect = '{ \'c\', \'b\', \'a\' }',
}
format_test {
input = { a = 1, a1 = 0 },
expect = '{ a = 1, a1 = 0 }',
}
format_test {
input = { a10 = 1, a1 = 0 },
expect = '{ a1 = 0, a10 = 1 }',
}
format_test {
input = { a00 = 0, a1 = 1 },
expect = '{ a00 = 0, a1 = 1 }',
}
format_test {
input = { a = {}, b = true },
expect = '{ b = true, a = {} }',
}
2017-06-25 11:16:34 +00:00
format_test {
name = 'Keys should be sorted such that uppercase and lowercase lies near each other',
input = { Dave = 1, dave = 2, mary = 3, Mary = 4, Adam = 5, adam = 6 },
expect = '{\n Adam = 5, adam = 6, Dave = 1,\n dave = 2, Mary = 4, mary = 3\n}',
2017-06-25 11:16:34 +00:00
}
format_test {
input = { a = {}, b = true, b1 = false },
expect = '{ b = true, b1 = false, a = {} }',
}
format_test {
input = { {}, true, false, 5 },
expect = '{ {}, true, false, 5 }',
}
format_test {
input = { [1] = {}, [2] = {}, [3] = {}, ['down_info'] = {}, ['up_info'] = {} },
expect = '{\n [1] = {},\n [2] = {},\n [3] = {},\n [\'down_info\'] = {},\n [\'up_info\'] = {}\n}',
}
format_test {
input = { 'hello', 123, {}, true },
expect = '{ \'hello\', 123, {}, true }',
}
format_test {
name = 'Small sequence holes should be filled with nil',
input = { 1, nil, 3 },
expect = '{ 1, nil, 3 }',
}
format_test {
name = 'Proper sorting of number keys',
input = { [-1/0] = 'a', [-100] = 'b', [-1] = 'c', [0] = 'd', [1] = 'e', [100] = 'f', [1/0] = 'g' },
expect = '{\n [-1/0] = \'a\', [-100] = \'b\',\n [-1] = \'c\', [0] = \'d\',\n [1] = \'e\', [100] = \'f\',\n [1/0] = \'g\'\n}',
}
format_test {
name = 'Proper sorting of number strings keys',
input = { ['-10'] = 'a', ['-0.5'] = 'b', ['0'] = 'c', ['1'] = 'd', ['10.1'] = 'e' },
expect = '{\n [\'-10\'] = \'a\', [\'-0.5\'] = \'b\',\n [\'0\'] = \'c\', [\'1\'] = \'d\',\n [\'10.1\'] = \'e\'\n}',
}
2017-07-20 08:38:24 +00:00
--[[ Sorting is hard in unicode, and I can't be bothered.
format_test {
name = 'Unicode: ø comes before å in danish',
input = loadstring 'return { øl = 1, ål = 2 }' (),
expect = '{ øl = 1, ål = 2 }',
}
--]]
--------------------------------------------------------------------------------
-- Alphanum Algorithm Example 1
local EXAMPLE_1_INPUT = {
['z1.doc'] = 1,
['z10.doc'] = 1,
['z100.doc'] = 1,
['z101.doc'] = 1,
['z102.doc'] = 1,
['z11.doc'] = 1,
['z12.doc'] = 1,
['z13.doc'] = 1,
['z14.doc'] = 1,
['z15.doc'] = 1,
['z16.doc'] = 1,
['z17.doc'] = 1,
['z18.doc'] = 1,
['z19.doc'] = 1,
['z2.doc'] = 1,
['z20.doc'] = 1,
['z3.doc'] = 1,
['z4.doc'] = 1,
['z5.doc'] = 1,
['z6.doc'] = 1,
['z7.doc'] = 1,
['z8.doc'] = 1,
['z9.doc'] = 1,
}
local EXAMPLE_1_OUTPUT = ([[{
['z1.doc'] = 1, ['z2.doc'] = 1,
['z3.doc'] = 1, ['z4.doc'] = 1,
['z5.doc'] = 1, ['z6.doc'] = 1,
['z7.doc'] = 1, ['z8.doc'] = 1,
['z9.doc'] = 1, ['z10.doc'] = 1,
['z11.doc'] = 1, ['z12.doc'] = 1,
['z13.doc'] = 1, ['z14.doc'] = 1,
['z15.doc'] = 1, ['z16.doc'] = 1,
['z17.doc'] = 1, ['z18.doc'] = 1,
['z19.doc'] = 1, ['z20.doc'] = 1,
['z100.doc'] = 1, ['z101.doc'] = 1,
['z102.doc'] = 1
}]]):gsub('\t', ' ')
SUITE:addTest('alphanum algorithm example 1', function ()
-- This is a test-case taken from http://www.davekoelle.com/alphanum.html
assert_equal(EXAMPLE_1_OUTPUT, pretty(EXAMPLE_1_INPUT))
end)
--------------------------------------------------------------------------------
-- Alphanum Algorithm Example 2
local EXAMPLE_2_INPUT = {
['1000X Radonius Maximus'] = 1,
['10X Radonius'] = 1,
['200X Radonius'] = 1,
['20X Radonius'] = 1,
['20X Radonius Prime'] = 1,
['30X Radonius'] = 1,
['40X Radonius'] = 1,
['Allegia 50 Clasteron'] = 1,
['Allegia 500 Clasteron'] = 1,
['Allegia 50B Clasteron'] = 1,
['Allegia 51 Clasteron'] = 1,
['Allegia 6R Clasteron'] = 1,
['Alpha 100'] = 1,
['Alpha 2'] = 1,
['Alpha 200'] = 1,
['Alpha 2A'] = 1,
['Alpha 2A-8000'] = 1,
['Alpha 2A-900'] = 1,
['Callisto Morphamax'] = 1,
['Callisto Morphamax 500'] = 1,
['Callisto Morphamax 5000'] = 1,
['Callisto Morphamax 600'] = 1,
['Callisto Morphamax 6000 SE'] = 1,
['Callisto Morphamax 6000 SE2'] = 1,
['Callisto Morphamax 700'] = 1,
['Callisto Morphamax 7000'] = 1,
['Xiph Xlater 10000'] = 1,
['Xiph Xlater 2000'] = 1,
['Xiph Xlater 300'] = 1,
['Xiph Xlater 40'] = 1,
['Xiph Xlater 5'] = 1,
['Xiph Xlater 50'] = 1,
['Xiph Xlater 500'] = 1,
['Xiph Xlater 5000'] = 1,
['Xiph Xlater 58'] = 1,
}
local EXAMPLE_2_OUTPUT = ([[{
['10X Radonius'] = 1,
['20X Radonius'] = 1,
['20X Radonius Prime'] = 1,
['30X Radonius'] = 1,
['40X Radonius'] = 1,
['200X Radonius'] = 1,
['1000X Radonius Maximus'] = 1,
['Allegia 6R Clasteron'] = 1,
['Allegia 50 Clasteron'] = 1,
['Allegia 50B Clasteron'] = 1,
['Allegia 51 Clasteron'] = 1,
['Allegia 500 Clasteron'] = 1,
['Alpha 2'] = 1,
['Alpha 2A'] = 1,
['Alpha 2A-900'] = 1,
['Alpha 2A-8000'] = 1,
['Alpha 100'] = 1,
['Alpha 200'] = 1,
['Callisto Morphamax'] = 1,
['Callisto Morphamax 500'] = 1,
['Callisto Morphamax 600'] = 1,
['Callisto Morphamax 700'] = 1,
['Callisto Morphamax 5000'] = 1,
['Callisto Morphamax 6000 SE'] = 1,
['Callisto Morphamax 6000 SE2'] = 1,
['Callisto Morphamax 7000'] = 1,
['Xiph Xlater 5'] = 1,
['Xiph Xlater 40'] = 1,
['Xiph Xlater 50'] = 1,
['Xiph Xlater 58'] = 1,
['Xiph Xlater 300'] = 1,
['Xiph Xlater 500'] = 1,
['Xiph Xlater 2000'] = 1,
['Xiph Xlater 5000'] = 1,
['Xiph Xlater 10000'] = 1
}]]):gsub('\t', ' ')
SUITE:addTest('alphanum algorithm example 2', function ()
-- This is a test-case taken from http://www.davekoelle.com/alphanum.html
assert_equal(EXAMPLE_2_OUTPUT, pretty(EXAMPLE_2_INPUT))
end)
--------------------------------------------------------------------------------
SUITE:addTest('alphanum algorithm extension 1', function ()
-- This is a test-case taken from http://www.davekoelle.com/alphanum.html
local OUTPUT = "{\n ['z2'] = 1, ['z2.'] = 1,\n ['z2.z'] = 1, ['z2.0'] = 1\n}"
assert_equal(OUTPUT, pretty { ['z2.z'] = 1, ['z2.0'] = 1, ['z2.'] = 1, ['z2'] = 1 })
end)
--------------------------------------------------------------------------------
return SUITE