1
0

Compare commits

...

3 Commits

8 changed files with 209 additions and 125 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.*.swp

View File

@ -1,7 +1,4 @@
-- TODO: I don't like to have such tiny modules. Either merge into another
-- module or provide the functionality with another approach.
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Enum -- Enum
@ -27,13 +24,13 @@ local UNICODE_ZERO_WIDTH_CHARACTERS = {}
for i = 128, 191 do UNICODE_ZERO_WIDTH_CHARACTERS['\204'..string.char(i)] = true end for i = 128, 191 do UNICODE_ZERO_WIDTH_CHARACTERS['\204'..string.char(i)] = true end
for i = 128, 175 do UNICODE_ZERO_WIDTH_CHARACTERS['\205'..string.char(i)] = true end for i = 128, 175 do UNICODE_ZERO_WIDTH_CHARACTERS['\205'..string.char(i)] = true end
local function iterate_utf8_chars (str) local function iterate_utf8_chars (str)
-- TODO: Detect invalid codepoints. -- TODO: Detect invalid codepoints.
return str:gmatch(UNICODE_CHAR_PATTERN) return str:gmatch(UNICODE_CHAR_PATTERN)
end end
local function utf8_string_length (str) local function utf8_string_length (str)
assert(type(str) == 'string')
local len = 0 local len = 0
for char in iterate_utf8_chars(str) do for char in iterate_utf8_chars(str) do
if not UNICODE_ZERO_WIDTH_CHARACTERS[char] then if not UNICODE_ZERO_WIDTH_CHARACTERS[char] then
@ -43,10 +40,43 @@ local function utf8_string_length (str)
return len return len
end end
--------------------------------------------------------------------------------
-- L utility
local function width_of_strings_in_l (l, start_i, stop_i)
-- Argument fixing and Error Checking
assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(stop_i) == 'number')
-- Do stuff
local width = 0
for i = start_i, stop_i do
local item_width = 0
if type(l[i]) == 'string' then
item_width = utf8_string_length(l[i])
elseif l[i].est_width then
item_width = l[i].est_width
end
width = width + item_width
end
-- Return
return width
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return { return {
TABLE_TYPE = enum { 'EMPTY', 'SEQUENCE', 'STRING_MAP', 'PURE_MAP', 'MIXED', 'SET' }, TABLE_TYPE = enum { 'EMPTY', 'SEQUENCE', 'STRING_MAP', 'PURE_MAP', 'MIXED', 'SET' },
DISPLAY = { HIDE = 1, SMALL = 2, INLINE = 3, EXPAND = 4 }, DISPLAY = { HIDE = 1, SMALL = 2, INLINE = 3, EXPAND = 4 },
utf8_string_length = utf8_string_length, utf8_string_length = utf8_string_length,
width_of_strings_in_l = width_of_strings_in_l,
} }

View File

@ -61,6 +61,7 @@ simplest, and move towards abstraction.
local LIBRARY = require((... and select('1', ...):match('.+%.') or '')..'library') or {} local LIBRARY = require((... and select('1', ...):match('.+%.') or '')..'library') or {}
local DISPLAY = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . DISPLAY local DISPLAY = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . DISPLAY
local utf8_string_length = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . utf8_string_length local utf8_string_length = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . utf8_string_length
local width_of_strings_in_l = assert(require((... and select('1', ...):match('.+%.') or '')..'common'), '[pretty]: Could not load vital library: common') . width_of_strings_in_l
-- Constants -- Constants
@ -108,10 +109,10 @@ local function get_function_info (f)
end end
end end
if info.source:sub(1,1) == '=' then info.defined_how = 'C' if info.source:sub(1,1) == '=' then info.defined_how = 'C'
elseif info.source:sub(1,1) == '@' then info.defined_how = 'file' elseif info.source:sub(1,1) == '@' then info.defined_how = 'file'
elseif info.source:find(LUA_FILE_PATTERN) then info.defined_how = 'file' -- Fix for when someone has misunderstood the source format is for. elseif info.source:find(LUA_FILE_PATTERN) then info.defined_how = 'file' -- Fix for when someone has misunderstood the source format is for.
else info.defined_how = 'string' else info.defined_how = 'string'
end end
if info.builtin and LIBRARY[f] then if info.builtin and LIBRARY[f] then
@ -235,15 +236,6 @@ end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Text handling -- Text handling
local function width_of_strings_in_l (l, start_i, end_i)
-- FIXME: Copy of the one in pretty.lua
local width = 0
for i = start_i or 1, (end_i or #l) do
width = width + utf8_string_length(l[i])
end
return width
end
local function add_indent_to_string (str, indent) local function add_indent_to_string (str, indent)
-- Indents `str` by `indent`. -- Indents `str` by `indent`.
@ -380,3 +372,4 @@ return function (value, display, l, format_value)
end end
l[#l+1] = '\nend' l[#l+1] = '\nend'
end end

View File

@ -72,12 +72,17 @@ local ERROR_UNKNOWN_TYPE = [[
We are attempting to cover all Lua features, so please report this bug, so we can improve. We are attempting to cover all Lua features, so please report this bug, so we can improve.
]] ]]
local TERMINAL_WIDTH = 80
local MAX_WIDTH_FOR_SINGLE_LINE_TABLE = 38 local MAX_WIDTH_FOR_SINGLE_LINE_TABLE = 38
if io and io.popen then if io and io.popen then
local f = io.popen "tput cols" local f = io.popen "tput cols"
local term_width = f:read '*n' local term_width = f:read '*n'
f:close() f:close()
-- if term_width then MAX_WIDTH_FOR_SINGLE_LINE_TABLE = term_width * 3 / 2 end if term_width then
TERMINAL_WIDTH = term_width
--MAX_WIDTH_FOR_SINGLE_LINE_TABLE = term_width * (2 / 3)
end
end end
local KEY_TYPE_SORT_ORDER = { local KEY_TYPE_SORT_ORDER = {
@ -101,6 +106,8 @@ local VALUE_TYPE_SORT_ORDER = {
['function'] = 7, ['function'] = 7,
} }
local COLUMN_SEPERATION = 1
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Key-value-pair Util -- Key-value-pair Util
@ -202,24 +209,7 @@ end
-- Formatting Util -- Formatting Util
local length_of_utf8_string = import 'common' . utf8_string_length local length_of_utf8_string = import 'common' . utf8_string_length
local width_of_strings_in_l = import 'common' . width_of_strings_in_l
local function width_of_strings_in_l (l, start_i, stop_i)
-- Argument fixing and Error Checking
assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(start_i) == 'number')
-- Do stuff
local width = 0
for i = start_i, stop_i do
width = width + ((type(l[i]) ~= 'string') and 1 or length_of_utf8_string(l[i]))
end
return width
end
local function ignore_alignment_info (l, start_i, stop_i) local function ignore_alignment_info (l, start_i, stop_i)
@ -229,7 +219,7 @@ local function ignore_alignment_info (l, start_i, stop_i)
local start_i, stop_i = start_i or 1, stop_i or #l local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number') assert(type(start_i) == 'number')
assert(type(start_i) == 'number') assert(type(stop_i) == 'number')
-- Do stuff -- Do stuff
for i = start_i, stop_i do for i = start_i, stop_i do
@ -239,15 +229,14 @@ local function ignore_alignment_info (l, start_i, stop_i)
end end
end end
local function fix_alignment (l, start_i, stop_i) local function insert_alignment_estimations (l, start_i, stop_i)
-- Argument fixing and Error Checking -- Argument fixing and Error Checking
assert(type(l) == 'table') assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number') assert(type(start_i) == 'number')
assert(type(start_i) == 'number') assert(type(stop_i) == 'number')
-- Find maximums -- Find maximums
local max = {} local max = {}
@ -260,7 +249,28 @@ local function fix_alignment (l, start_i, stop_i)
-- Insert the proper whitespace -- Insert the proper whitespace
for i = start_i, stop_i do for i = start_i, stop_i do
if type(l[i]) == 'table' and l[i][1] == 'align' then if type(l[i]) == 'table' and l[i][1] == 'align' then
l[i] = string.rep(' ', max[ l[i][2] ] - l[i][3]) l[i].est_width = max[ l[i][2] ] - l[i][3]
end
end
end
local function fix_alignment (l, start_i, stop_i)
-- Argument fixing and Error Checking
assert(type(l) == 'table')
local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(start_i) == 'number')
assert(type(stop_i) == 'number')
-- Find whitespace to insert
insert_alignment_estimations(l, start_i, stop_i)
-- Insert whitespace
for i = start_i, stop_i do
if type(l[i]) == 'table' and l[i][1] == 'align' then
l[i] = string.rep(' ', l[i].est_width)
end end
end end
end end
@ -268,7 +278,7 @@ end
local function attempt_to_align_into_columns (l, start_i, stop_i, nr_items_pr_row) local function attempt_to_align_into_columns (l, start_i, stop_i, nr_items_pr_row)
assert(type(l) == 'table') assert(type(l) == 'table')
assert(type(start_i) == 'number') assert(type(start_i) == 'number')
assert(type(stop_i) == 'number') assert(type(stop_i) == 'number')
assert(type(nr_items_pr_row) == 'number') assert(type(nr_items_pr_row) == 'number')
local column = {} local column = {}
@ -301,12 +311,14 @@ local function align_into_columns (l, start_i, stop_i)
assert(type(start_i) == 'number') assert(type(start_i) == 'number')
assert(type(stop_i) == 'number') assert(type(stop_i) == 'number')
insert_alignment_estimations(l, start_i, stop_i)
-- Find columns -- Find columns
local columns = nil local columns = nil
for nr_items_pr_row = 10, 1, -1 do -- TODO: Do this more intelligently. for nr_items_pr_row = 10, 1, -1 do -- TODO: Do this more intelligently.
local column_width local column_width
column_width, columns = attempt_to_align_into_columns(l, start_i, stop_i, nr_items_pr_row) column_width, columns = attempt_to_align_into_columns(l, start_i, stop_i, nr_items_pr_row)
if column_width <= MAX_WIDTH_FOR_SINGLE_LINE_TABLE then break end if column_width <= l.options.max_width_for_single_line_table then break end
end end
-- Change alignment of columns -- Change alignment of columns
@ -336,7 +348,7 @@ local function align_into_columns (l, start_i, stop_i)
local column_i = (item_nr-1)%#columns+1 local column_i = (item_nr-1)%#columns+1
if column_i ~= #columns then if column_i ~= #columns then
local width_of_item = width_of_strings_in_l(l, start_of_item_i, i-1) local width_of_item = width_of_strings_in_l(l, start_of_item_i, i-1)
l[i] = l[i][2] .. ' ' .. (' '):rep(columns[column_i]-width_of_item) l[i] = l[i][2] .. (' '):rep(COLUMN_SEPERATION+columns[column_i]-width_of_item)
end end
end end
start_of_item_i, item_nr = i + 1, item_nr + 1 start_of_item_i, item_nr = i + 1, item_nr + 1
@ -345,16 +357,16 @@ local function align_into_columns (l, start_i, stop_i)
end end
local function align_into_tabular_style (l, start_i, stop_i) local function align_into_tabular_style (l, start_i, stop_i)
-- Adds alignment after seperators, to create nicely aligned tabular-format. -- Adds alignment after separators, to create nicely aligned tabular-format.
-- Argument fixing and Error Checking -- Argument fixing and Error Checking
local start_i, stop_i = start_i or 1, stop_i or #l local start_i, stop_i = start_i or 1, stop_i or #l
assert(type(l) == 'table') assert(type(l) == 'table')
assert(type(start_i) == 'number') assert(type(start_i) == 'number')
assert(type(stop_i) == 'number') assert(type(stop_i) == 'number')
assert(type(l[start_i]) == 'table' and l[start_i][1] == 'indent') assert(type(l[start_i]) == 'table' and l[start_i][1] == 'indent')
assert(type(l[stop_i]) == 'table' and l[stop_i][1] == 'unindent') assert(type(l[stop_i]) == 'table' and l[stop_i][1] == 'unindent')
-- Calculate where to insert new alignment. -- Calculate where to insert new alignment.
local indent, key_nr, index_of_last_meta, insert_later = 0, 0, 1, {} local indent, key_nr, index_of_last_meta, insert_later = 0, 0, 1, {}
@ -494,12 +506,14 @@ local function format_table (t, display, l, format_value)
-- Decide for short or long table formatting. -- Decide for short or long table formatting.
local table_width = width_of_strings_in_l(l, start_of_table_i) local table_width = width_of_strings_in_l(l, start_of_table_i)
if table_width <= MAX_WIDTH_FOR_SINGLE_LINE_TABLE then
if table_width <= l.options.max_width_for_single_line_table then
-- Is short table: Ignore "width of key". -- Is short table: Ignore "width of key".
l[start_of_table_i][3] = 'inline' l[start_of_table_i][3] = 'inline'
ignore_alignment_info(l, start_of_table_i) ignore_alignment_info(l, start_of_table_i)
elseif table_info.is_leaf_node then elseif table_info.is_leaf_node then
-- Is leaf node: Can format into columns. -- Is leaf node: Can format into columns.
-- Only if long or sequence.
-- NOTE: Currently we only allow leaf-nodes to format into columns, due -- NOTE: Currently we only allow leaf-nodes to format into columns, due
-- to issues with table alignment. -- to issues with table alignment.
align_into_columns(l, start_of_table_i) align_into_columns(l, start_of_table_i)
@ -555,7 +569,6 @@ local TYPE_TO_FORMAT_FUNC = {
local function format_value (value, display, l) local function format_value (value, display, l)
assert(type(display) == 'number' and type(l) == 'table') assert(type(display) == 'number' and type(l) == 'table')
local formatting = TYPE_TO_FORMAT_FUNC[type(value)] local formatting = TYPE_TO_FORMAT_FUNC[type(value)]
--print(value, formatting)
if formatting then if formatting then
formatting(value, display, l, format_value) formatting(value, display, l, format_value)
else else
@ -581,6 +594,8 @@ local KNOWN_OPTIONS = {
_table_addr_comment = { type = 'boolean', default = false, debug = 'debug' }, -- TODO: Maybe automatically display table address when display = 0? _table_addr_comment = { type = 'boolean', default = false, debug = 'debug' }, -- TODO: Maybe automatically display table address when display = 0?
indent = { type = 'string', default = ' ' }, indent = { type = 'string', default = ' ' },
max_output_width = { type = 'number', default = TERMINAL_WIDTH }
} }
local function ensure_that_all_options_are_known (input_options) local function ensure_that_all_options_are_known (input_options)
@ -615,10 +630,36 @@ local function ensure_that_all_options_are_known (input_options)
output_options[option_name] = option_info.default output_options[option_name] = option_info.default
end end
end end
-- Calculate derived settings
output_options.max_width_for_single_line_table = MAX_WIDTH_FOR_SINGLE_LINE_TABLE -- TODO: Make dynamic
-- Returns input_options -- Returns input_options
return output_options return output_options
end end
local function length_of_longest_line_in_text (text)
assert(type(text) == 'string')
local longest_line_len = text:match '([^\n]*)$' : len()
for line in text:gmatch '(.-)\n' do
longest_line_len = math.max(longest_line_len, line:len())
end
return longest_line_len
end
local function internal_warning (fmt, ...)
io.stderr:write('[pretty/internal]: '..string.format(fmt, ...))
end
local function assert_pretty_result (repr, options)
assert(type(repr) == 'string')
assert(type(options) == 'table')
-- Determine length of longest line in output
local max_width = length_of_longest_line_in_text(repr)
if max_width > options.max_output_width then
internal_warning('Internal assertion failed. Width of output was %i, but should be less than %i.', max_width, options.max_output_width)
end
end
local function pretty_format (value, options) local function pretty_format (value, options)
-- Error checking -- Error checking
local options = ensure_that_all_options_are_known(options or {}) local options = ensure_that_all_options_are_known(options or {})
@ -635,7 +676,13 @@ local function pretty_format (value, options)
fix_seperator_info(l, l.options.indent) fix_seperator_info(l, l.options.indent)
ignore_alignment_info(l) ignore_alignment_info(l)
return table.concat(l, '') -- Concat and perform last assertions.
local repr = table.concat(l, '')
assert_pretty_result(repr, options)
-- Return
return repr
end end
return pretty_format return pretty_format

View File

@ -109,14 +109,27 @@ local function safe_cut (str, si, ei)
-- Calculate -- Calculate
local cut_str = str:sub(si, ei) local cut_str = str:sub(si, ei)
-- Search for the number of backslashes just before the send of the string. -- Search for the number of backslashes and digits at the end of the string.
-- If that number is even, it's a sequence of backslashes, if not it's a -- If the number of backslashes is even, it's a sequence of backslashes, if
-- broken escape string. -- not it's a broken escape string.
local start_of_backslashes, start_of_digits = cut_str:match '()\\*()%d?%d?$' local start_of_backslashes, start_of_digits = cut_str:match '()\\*()%d?%d?$'
local nr_backslashes_before_end = start_of_digits - start_of_backslashes local nr_backslashes_before_end = start_of_digits - start_of_backslashes
if nr_backslashes_before_end % 2 == 1 then cut_str = cut_str:sub(1, start_of_backslashes - 1) end if nr_backslashes_before_end % 2 == 1 then
-- Lets see if we can't shorten the escape code, to fit within the
-- cut limit.
local space_left = #cut_str - (start_of_digits - 2)
cut_str = cut_str:sub(1, start_of_digits - 2)
ei = ei - space_left
return cut_str local digits, after_digits = str:match('^\\(%d?%d?%d?)()', si - 1 + start_of_digits - 1)
if space_left >= 1 + 3 - #digits:match '0*' then
ei = after_digits - 1
cut_str = cut_str .. ('\\%0'..(space_left-1)..'i'):format(digits)
end
end
return cut_str, ei
end end
@ -149,8 +162,8 @@ local function format_concatted_string (str, _, l)
-- Cut strings -- Cut strings
local sub_strings, str_i = {}, 1 local sub_strings, str_i = {}, 1
repeat repeat
local sub_str = safe_cut(str, str_i, str_i + width_without_overhead - 1) local sub_str, ei = safe_cut(str, str_i, str_i + width_without_overhead - 1)
str_i = str_i + #sub_str str_i = ei + 1
sub_strings[#sub_strings+1] = sub_str sub_strings[#sub_strings+1] = sub_str
until str_i >= #str until str_i >= #str

View File

@ -34,6 +34,15 @@ local function format_test (t)
end, { line = debug.getinfo(2).currentline }) end, { line = debug.getinfo(2).currentline })
end end
local function format_parsable_test (t)
local stripped = t.text:match '^%s*(.-)%s*$'
return format_test {
name = t.name,
input = loadstring('return '..stripped)(),
expect = stripped
}
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Primitive types -- Primitive types
@ -263,13 +272,24 @@ format_test {
expect = '{\n \'hello\', \'world\', \'how\',\n \'is\', \'it\', \'going?\',\n \'Im\', \'doing great\', \'thanks\',\n \'that\', \'was\', \'what\',\n \'I\'\n}', expect = '{\n \'hello\', \'world\', \'how\',\n \'is\', \'it\', \'going?\',\n \'Im\', \'doing great\', \'thanks\',\n \'that\', \'was\', \'what\',\n \'I\'\n}',
} }
format_parsable_test {
name = 'Column style with aligned numbers',
text = [[
{
200, -522, 423, 516, 523, 126, 2912,
523, -620, 0, 0, 0, -5, 2,
72, 6
}
]] }
format_test { format_test {
name = 'Tabular style with strings left aligned', name = 'Tabular style with strings left aligned',
input = { input = {
{ a = 'hello', b = 'hi' }, { a = 'hello', b = 'hi' },
{ a = 'hi', b = 'hello' } { a = 'hi', b = 'hello' },
{ a = 'hi', b = 'hi' },
}, },
expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' }\n}', expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' },\n { a = \'hi\', b = \'hi\' }\n}',
} }
format_test { format_test {
@ -398,53 +418,6 @@ end)
local BIG_EXAMPLE_TABLE = [[ local BIG_EXAMPLE_TABLE = [[
return { return {
[0] = 21082, [1] = 696, [0] = 21082, [1] = 696,
[2] = 463, [3] = 235,
[4] = 315, [5] = 312,
[6] = 204, [7] = 124,
[8] = 692, [9] = 84,
[10] = 248, [11] = 148,
[12] = 108, [13] = 109,
[14] = 1019, [15] = 1211,
[16] = 470, [17] = 73,
[18] = 121, [19] = 36,
[20] = 149, [21] = 514,
[22] = 38, [23] = 45,
[24] = 353, [25] = 27,
[26] = 27, [27] = 51,
[28] = 84, [29] = 61,
[30] = 29, [31] = 448,
[32] = 2064, [33] = 65,
[34] = 34, [35] = 20,
[36] = 859, [37] = 239,
[38] = 24, [39] = 41,
[40] = 297, [41] = 95,
[42] = 43, [43] = 30,
[44] = 202, [45] = 123,
[46] = 243, [47] = 98,
[48] = 207, [49] = 484,
[50] = 31, [51] = 59,
[52] = 51, [53] = 118,
[54] = 27, [55] = 22,
[56] = 227, [57] = 168,
[58] = 55, [59] = 38,
[60] = 74, [61] = 106,
[62] = 62, [63] = 40,
[64] = 170, [65] = 857,
[66] = 412, [67] = 136,
[68] = 737, [69] = 238,
[70] = 64, [71] = 119,
[72] = 2567, [73] = 481,
[74] = 50, [75] = 55,
[76] = 714, [77] = 189,
[78] = 61, [79] = 55,
[80] = 114, [81] = 26,
[82] = 69, [83] = 150,
[84] = 238, [85] = 172,
[86] = 65, [87] = 81,
[88] = 102, [89] = 39,
[90] = 30, [91] = 154,
[92] = 155, [93] = 191,
[94] = 75, [95] = 185,
[96] = 62, [97] = 334, [96] = 62, [97] = 334,
[98] = 119, [99] = 217, [98] = 119, [99] = 217,
[100] = 261 [100] = 261
@ -458,3 +431,4 @@ end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return SUITE return SUITE

View File

@ -130,6 +130,13 @@ format_test {
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\t\'...\n}', expect = '{\n \'Lorem ipsum dolor sit amet, consec\\t\'...\n}',
} }
format_test {
name = 'Cut strings are not cut in the middle of backslash sequence',
not_idempotent = true,
input = {'Lorem ipsum dolor sit amet, consec\\\\\\\\\\\\\\tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\\\\'...\n}',
}
format_test { format_test {
name = 'Cut strings are not cut in the middle of escaping \\', name = 'Cut strings are not cut in the middle of escaping \\',
not_idempotent = true, not_idempotent = true,
@ -145,13 +152,19 @@ format_test {
} }
format_test { format_test {
-- NOTE: Not priority functionallity. name = 'Cut strings can shorten decimal escape codes, if necessary and possible',
name = 'Cut strings can shorten decimal escape codes, if nessesary and possible',
not_idempotent = true, not_idempotent = true,
input = {'Lorem ipsum dolor sit amet, consec\014tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'}, input = {'Lorem ipsum dolor sit amet, consec\014tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\14\'...\n}', expect = '{\n \'Lorem ipsum dolor sit amet, consec\\14\'...\n}',
} }
format_test {
name = 'Cut strings can shorten decimal escape codes, if necessary and possible, but will keep them as long as possible',
not_idempotent = true,
input = {'Lorem ipsum dolor sit amet, consec\004tetur adipiscing elit. Nunc vestibulum tempus ligula. Sed ac lobortis mi.'},
expect = '{\n \'Lorem ipsum dolor sit amet, consec\\04\'...\n}',
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Concatted Strings -- Concatted Strings
@ -167,6 +180,18 @@ format_test {
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc ve' ..]]..'\n'..[['stibulum tempus ligula. Sed ac lobortis mi.']], expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc ve' ..]]..'\n'..[['stibulum tempus ligula. Sed ac lobortis mi.']],
} }
format_test {
name = 'Concatted string with decimal escape at border',
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004\255\000lum tempus ligula. Sed ac lobortis mi.',
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004' ..]]..'\n'..[['\255\000lum tempus ligula. Sed ac lobortis mi.']],
}
format_test {
name = 'Concatted string with decimal escape at border 2',
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004\002\000lum tempus ligula. Sed ac lobortis mi.',
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.Nunc vestibu\004\02' ..]]..'\n'..[['\000lum tempus ligula. Sed ac lobortis mi.']],
}
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Longform Strings -- Longform Strings

View File

@ -50,7 +50,7 @@ format_test {
format_test { format_test {
name = 'Keys should be sorted such that uppercase and lowercase lies near each other', 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 }, 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}', expect = '{\n Adam = 5, adam = 6, Dave = 1,\n dave = 2, Mary = 4, mary = 3\n}',
} }
format_test { format_test {
@ -82,13 +82,13 @@ format_test {
format_test { format_test {
name = 'Proper sorting of number keys', name = 'Proper sorting of number keys',
input = { [-1/0] = 'a', [-100] = 'b', [-1] = 'c', [0] = 'd', [1] = 'e', [100] = 'f', [1/0] = 'g' }, 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}', expect = '{\n [-1/0] = \'a\', [-100] = \'b\',\n [-1] = \'c\', [0] = \'d\',\n [1] = \'e\', [100] = \'f\',\n [1/0] = \'g\'\n}',
} }
format_test { format_test {
name = 'Proper sorting of number strings keys', name = 'Proper sorting of number strings keys',
input = { ['-10'] = 'a', ['-0.5'] = 'b', ['0'] = 'c', ['1'] = 'd', ['10.1'] = 'e' }, 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}', expect = '{\n [\'-10\'] = \'a\', [\'-0.5\'] = \'b\',\n [\'0\'] = \'c\', [\'1\'] = \'d\',\n [\'10.1\'] = \'e\'\n}',
} }
@ -131,17 +131,17 @@ local EXAMPLE_1_INPUT = {
} }
local EXAMPLE_1_OUTPUT = ([[{ local EXAMPLE_1_OUTPUT = ([[{
['z1.doc'] = 1, ['z2.doc'] = 1, ['z1.doc'] = 1, ['z2.doc'] = 1,
['z3.doc'] = 1, ['z4.doc'] = 1, ['z3.doc'] = 1, ['z4.doc'] = 1,
['z5.doc'] = 1, ['z6.doc'] = 1, ['z5.doc'] = 1, ['z6.doc'] = 1,
['z7.doc'] = 1, ['z8.doc'] = 1, ['z7.doc'] = 1, ['z8.doc'] = 1,
['z9.doc'] = 1, ['z10.doc'] = 1, ['z9.doc'] = 1, ['z10.doc'] = 1,
['z11.doc'] = 1, ['z12.doc'] = 1, ['z11.doc'] = 1, ['z12.doc'] = 1,
['z13.doc'] = 1, ['z14.doc'] = 1, ['z13.doc'] = 1, ['z14.doc'] = 1,
['z15.doc'] = 1, ['z16.doc'] = 1, ['z15.doc'] = 1, ['z16.doc'] = 1,
['z17.doc'] = 1, ['z18.doc'] = 1, ['z17.doc'] = 1, ['z18.doc'] = 1,
['z19.doc'] = 1, ['z20.doc'] = 1, ['z19.doc'] = 1, ['z20.doc'] = 1,
['z100.doc'] = 1, ['z101.doc'] = 1, ['z100.doc'] = 1, ['z101.doc'] = 1,
['z102.doc'] = 1 ['z102.doc'] = 1
}]]):gsub('\t', ' ') }]]):gsub('\t', ' ')
@ -240,10 +240,11 @@ end)
SUITE:addTest('alphanum algorithm extension 1', function () SUITE:addTest('alphanum algorithm extension 1', function ()
-- This is a test-case taken from http://www.davekoelle.com/alphanum.html -- 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}" 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 }) assert_equal(OUTPUT, pretty { ['z2.z'] = 1, ['z2.0'] = 1, ['z2.'] = 1, ['z2'] = 1 })
end) end)
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
return SUITE return SUITE