aboutsummaryrefslogtreecommitdiff
path: root/unit_tests/scripts/_utils.lua
diff options
context:
space:
mode:
Diffstat (limited to 'unit_tests/scripts/_utils.lua')
-rw-r--r--unit_tests/scripts/_utils.lua75
1 files changed, 75 insertions, 0 deletions
diff --git a/unit_tests/scripts/_utils.lua b/unit_tests/scripts/_utils.lua
new file mode 100644
index 0000000..d710702
--- /dev/null
+++ b/unit_tests/scripts/_utils.lua
@@ -0,0 +1,75 @@
1local io = assert(io)
2local pairs = assert(pairs)
3local select = assert(select)
4local string_format = assert(string.format)
5local tostring = assert(tostring)
6local type = assert(type)
7
8local print_id = 0
9local P = function(whence_, ...)
10 if not io then return end
11 print_id = print_id + 1
12 local _str = string_format("%s: %02d.\t", whence_, print_id)
13 for _i = 1, select('#', ...) do
14 _str = _str .. tostring(select(_i, ...)) .. "\t"
15 end
16 if io then
17 io.stderr:write(_str .. "\n")
18 end
19end
20
21local MAKE_PRINT = function()
22 local _whence = lane_threadname and lane_threadname() or "main"
23 return function(...)
24 P(_whence, ...)
25 end
26end
27
28local tables_match
29
30-- true if 'a' is a subtable of 'b'
31--
32local function subtable(a, b)
33 --
34 assert(type(a)=="table" and type(b)=="table")
35
36 for k,v in pairs(b) do
37 if type(v)~=type(a[k]) then
38 return false -- not subtable (different types, or missing key)
39 elseif type(v)=="table" then
40 if not tables_match(v,a[k]) then return false end
41 else
42 if a[k] ~= v then return false end
43 end
44 end
45 return true -- is a subtable
46end
47
48-- true when contents of 'a' and 'b' are identical
49--
50tables_match = function(a, b)
51 return subtable(a, b) and subtable(b, a)
52end
53
54local function dump_error_stack(error_reporting_mode_, stack)
55 local PRINT = MAKE_PRINT()
56 if error_reporting_mode_ == "minimal" then
57 assert(stack == nil, table.concat{"stack is a ", type(stack)})
58 elseif error_reporting_mode_ == "basic" then -- each stack line is a string in basic mode
59 PRINT("STACK:\n", table.concat(stack,"\n\t"));
60 else -- each stack line is a table in extended mode
61 PRINT "STACK:"
62 for line, details in pairs(stack) do
63 PRINT("\t", line);
64 for detail, value in pairs(details) do
65 PRINT("\t\t", detail, ": ", value)
66 end
67 end
68 end
69end
70
71return {
72 MAKE_PRINT = MAKE_PRINT,
73 tables_match = tables_match,
74 dump_error_stack = dump_error_stack
75}