diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-13 17:22:17 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-12-13 17:22:17 +0100 |
commit | dc7a2bc3a9c8316e17902493b832ca117805d29f (patch) | |
tree | 3c1a03edf586869119ef0de03631f6f603650720 /unit_tests/scripts/_utils.lua | |
parent | 7500a80fc06c5311c46df8f1761f25ae67277fc4 (diff) | |
download | lanes-dc7a2bc3a9c8316e17902493b832ca117805d29f.tar.gz lanes-dc7a2bc3a9c8316e17902493b832ca117805d29f.tar.bz2 lanes-dc7a2bc3a9c8316e17902493b832ca117805d29f.zip |
Append all unit tests to depot
Diffstat (limited to 'unit_tests/scripts/_utils.lua')
-rw-r--r-- | unit_tests/scripts/_utils.lua | 75 |
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 @@ | |||
1 | local io = assert(io) | ||
2 | local pairs = assert(pairs) | ||
3 | local select = assert(select) | ||
4 | local string_format = assert(string.format) | ||
5 | local tostring = assert(tostring) | ||
6 | local type = assert(type) | ||
7 | |||
8 | local print_id = 0 | ||
9 | local 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 | ||
19 | end | ||
20 | |||
21 | local MAKE_PRINT = function() | ||
22 | local _whence = lane_threadname and lane_threadname() or "main" | ||
23 | return function(...) | ||
24 | P(_whence, ...) | ||
25 | end | ||
26 | end | ||
27 | |||
28 | local tables_match | ||
29 | |||
30 | -- true if 'a' is a subtable of 'b' | ||
31 | -- | ||
32 | local 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 | ||
46 | end | ||
47 | |||
48 | -- true when contents of 'a' and 'b' are identical | ||
49 | -- | ||
50 | tables_match = function(a, b) | ||
51 | return subtable(a, b) and subtable(b, a) | ||
52 | end | ||
53 | |||
54 | local 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 | ||
69 | end | ||
70 | |||
71 | return { | ||
72 | MAKE_PRINT = MAKE_PRINT, | ||
73 | tables_match = tables_match, | ||
74 | dump_error_stack = dump_error_stack | ||
75 | } | ||