From 70751995c2441500a0914ef6ec491c4a14d2e4ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0pa=C4=8Dek?= Date: Tue, 27 Oct 2020 11:25:28 +0100 Subject: [PATCH] tests: cover table_print --- daemon/lua/krprint.test.lua | 121 ++++++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 26 deletions(-) diff --git a/daemon/lua/krprint.test.lua b/daemon/lua/krprint.test.lua index 89bb9f103..319664eec 100644 --- a/daemon/lua/krprint.test.lua +++ b/daemon/lua/krprint.test.lua @@ -42,11 +42,14 @@ end local function test_bool() test_de_serialization_autodesc(true) + same('true', table_print(true), 'table_print handles true') test_de_serialization_autodesc(false) + same('false', table_print(false), 'table_print handles false') end local function test_nil() test_de_serialization_autodesc(nil) + same('nil', table_print(nil), 'table_print handles nil') end local function gen_number_int() @@ -66,13 +69,24 @@ end local function test_number() test_de_serialization_autodesc(0) + same('0', table_print(0), 'table_print handles 0') test_de_serialization_autodesc(-math.huge) + same('-inf', table_print(-math.huge), 'table_print handles -infinity') test_de_serialization_autodesc(math.huge) + same('inf', table_print(math.huge), 'table_print handles +infinity') test_de_serialization_autodesc(tonumber('nan')) + same('nan', table_print(tonumber('nan')), 'table_print handles nan') for _=1,20 do -- integers test_de_serialization_autodesc(gen_number_int()) + -- bigger numbers might end up with non-exact representation + local smallnumber = math.random(-2^32, 2^32) + same(tostring(smallnumber), table_print(smallnumber), + 'table_print handles small numbers') end for _=1,20 do -- floats + local float = math.random() + same(tostring(float), table_print(float), + 'table_print handles floats') test_de_serialization_autodesc(gen_number_float()) end end @@ -134,10 +148,43 @@ local function gen_test_tables_supported(level) return t end -function test_table_supported() +local marker = 'this string must be present somewhere in output' +local function gen_marker() + return marker +end + +local kluautil = require('kluautil') +local function random_modify_table(t, always, generator) + assert(generator) + local tab_len = kluautil.kr_table_len(t) + local modified = false + -- modify some values + for key, val in pairs(t) do + if math.random(1, tab_len) == 1 then + if type(val) == 'table' then + modified = modified or random_modify_table(val, false, generator) + else + t[key] = generator() + modified = true + end + end + end + if always and not modified then + -- fallback, add an unsupported key + t[generator()] = true + modified = true + end + return modified +end + +local function test_table_supported() for i=1,20 do local t = gen_test_tables_supported() test_de_serialization(t, 'random table no. ' .. i) + assert(random_modify_table(t, true, gen_marker)) + local str = table_print(t) + ok(string.find(str, marker, 1, true), + 'table_print works on complex serializable tables') end end @@ -168,37 +215,23 @@ local function test_unsupported(val, desc) local output = serialize_lua(val, 'comment') same('string', type(output), string.format('attempt to serialize %s in ' - .. 'comment mode provides returned a string', + .. 'comment mode returned a string', desc)) - ok(string.find(output, '--'), 'returned string contains a comment') - end -end - -local kluautil = require('kluautil') -local function make_table_unsupported(t, always) - local tab_len = kluautil.kr_table_len(t) - local modified = false - -- modify some values - for key, val in pairs(t) do - if math.random(1, tab_len) == 1 then - if type(val) == 'table' then - modified = modifier or make_table_unsupported(val, false) - else - t[key] = gen_unsupported_atomic() - modified = true - end + ok(string.find(output, '--', 1, true), + 'returned string contains a comment') + output = table_print(val) + same('string', type(output), + string.format('table_print can stringify %s', desc)) + if type(val) ~= 'table' then + ok(string.find(output, type(val), 1, true), + 'exotic type is mentioned in table_print output') end end - if always and not modified then - -- fallback, add an unsupported key - t[gen_unsupported_atomic()] = true - end - return modified end local function gen_test_tables_unsupported() local t = gen_test_tables_supported() - make_table_unsupported(t, true) + random_modify_table(t, true, gen_unsupported_atomic) return t end @@ -206,9 +239,43 @@ local function test_unsupported_table() for i=1,20 do local t = gen_test_tables_unsupported() test_unsupported(t, 'random unsupported table no. ' .. i)() + assert(random_modify_table(t, true, gen_marker)) + local str = table_print(t) + ok(string.find(str, marker, 1, true), + 'table_print works on complex unserializable tables') end end +local function func_2vararg_5ret(arg1, arg2, ...) + return select('#', ...), nil, arg1 + arg2, false, nil +end +local function func_ret_nil() return nil end +local function func_ret_nothing() return end + +local function test_pprint_func() + local t = { [false] = func_2vararg_5ret } + local output = table_print(t) + ok(string.find(output, 'function false(arg1, arg2, ...)', 1, true), + 'function parameters are pretty printed') +end + +local function test_pprint_func_ret() + local output = table_print(func_2vararg_5ret(1, 2, 'bla')) + local exp = [[ +1 -- result # 1 +nil -- result # 2 +3 -- result # 3 +false -- result # 4 +nil -- result # 5]] + same(output, exp, 'multiple return values are pretty printed') + + output = table_print(func_ret_nil()) + same(output, 'nil', 'single return value does not have extra comments') + + output = table_print(func_ret_nothing()) + same(output, nil, 'no return values to be printed cause nil output') +end + return { test_bool, test_nil, @@ -219,5 +286,7 @@ return { test_unsupported(const_thread), test_unsupported(const_userdata), test_unsupported(const_cdata), - test_unsupported_table + test_unsupported_table, + test_pprint_func, + test_pprint_func_ret, } -- 2.47.2