diff --git a/test/test_pretty.lua b/test/test_pretty.lua index e0e716a..5f2c811 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -330,65 +330,8 @@ do options = { max_depth = 5 }, expect = '{\n\ta = { {...} },\n\tb = { {...} }\n}', } - 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 = {} }', -} - -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\t[1] = {},\n\t[2] = {},\n\t[3] = {},\n\t[\'down_info\'] = {},\n\t[\'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 }', -} - --- TODO: Add more tests for sorting. - -------------------------------------------------------------------------------- -- CDATA diff --git a/test/test_sorting.lua b/test/test_sorting.lua new file mode 100644 index 0000000..818d889 --- /dev/null +++ b/test/test_sorting.lua @@ -0,0 +1,226 @@ + +local SUITE = require('TestSuite').new('sorting') +SUITE:setEnviroment{ + pretty = require('pretty') +} + +-------------------------------------------------------------------------------- + +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 = {} }', +} + +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\t[1] = {},\n\t[2] = {},\n\t[3] = {},\n\t[\'down_info\'] = {},\n\t[\'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 }', +} + +-------------------------------------------------------------------------------- +-- 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 +}]] + +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 +}]] + + +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\t['z2'] = 1,\n\t['z2.'] = 1,\n\t['z2.z'] = 1,\n\t['z2.0'] = 1\n}" + assert_equal(OUTPUT, pretty { ['z2.z'] = 1, ['z2.0'] = 1, ['z2.'] = 1, ['z2'] = 1 }) +end) + +-------------------------------------------------------------------------------- + +return SUITE