From 163c9b405953634e1c3da15c7f11cdf6c7c11fee Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 19 Jul 2018 12:40:25 -0400 Subject: [PATCH] Add utils.OrderedTable, make test order consistent Also added OrderedTable tests and comment support to expect.*() --- docs/changelog.txt | 3 +++ library/lua/utils.lua | 36 ++++++++++++++++++++++++++++++++++++ test/library/utils.lua | 15 +++++++++++++++ test/main.lua | 29 +++++++++++++++++++---------- 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 test/library/utils.lua diff --git a/docs/changelog.txt b/docs/changelog.txt index 998a20a77..5f422ff5f 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -41,6 +41,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - Core: various thread safety and memory management improvements - Fixed cmake build dependencies for generated header files +## Lua +- ``utils``: new ``OrderedTable`` class + ================================================================================ # 0.44.12-r1 diff --git a/library/lua/utils.lua b/library/lua/utils.lua index 60830e82f..660cbd374 100644 --- a/library/lua/utils.lua +++ b/library/lua/utils.lua @@ -682,4 +682,40 @@ function addressof(obj) return select(2, obj:sizeof()) end +function OrderedTable() + -- store values in a separate table to ensure that __index and __newindex + -- run on every table index operation + local t = {} + local key_to_index = {} + local index_to_key = {} + + local mt = {} + function mt:__index(k) + return t[k] + end + function mt:__newindex(k, v) + if not key_to_index[k] then + table.insert(index_to_key, k) + key_to_index[k] = #index_to_key + end + t[k] = v + end + function mt:__pairs() + return function(_, k) + if k then + k = index_to_key[key_to_index[k] + 1] + else + k = index_to_key[1] + end + if k then + return k, t[k] + end + end, nil, nil + end + + local self = {} + setmetatable(self, mt) + return self +end + return _ENV diff --git a/test/library/utils.lua b/test/library/utils.lua new file mode 100644 index 000000000..309736892 --- /dev/null +++ b/test/library/utils.lua @@ -0,0 +1,15 @@ +local utils = require 'utils' + +function test.OrderedTable() + local t = utils.OrderedTable() + local keys = {'a', 'c', 'e', 'd', 'b', 'q', 58, -1.2} + for i = 1, #keys do + t[keys[i]] = i + end + local i = 1 + for k, v in pairs(t) do + expect.eq(k, keys[i], 'key order') + expect.eq(v, i, 'correct value') + i = i + 1 + end +end diff --git a/test/main.lua b/test/main.lua index d30a26008..5e9d09302 100644 --- a/test/main.lua +++ b/test/main.lua @@ -1,20 +1,21 @@ local script = require 'gui.script' +local utils = require 'utils' local args = {...} local done_command = args[1] expect = {} -function expect.true_(value) - return not not value +function expect.true_(value, comment) + return not not value, comment, 'expected true' end -function expect.false_(value) - return not value +function expect.false_(value, comment) + return not value, comment, 'expected false' end -function expect.eq(a, b) - return a == b +function expect.eq(a, b, comment) + return a == b, comment, ('%s ~= %s'):format(a, b) end -function expect.ne(a, b) - return a ~= b +function expect.ne(a, b, comment) + return a ~= b, comment, ('%s == %s'):format(a, b) end function expect.error(func, ...) local ok, ret = pcall(func, ...) @@ -32,7 +33,7 @@ end function build_test_env() local env = { - test = {}, + test = utils.OrderedTable(), expect = {}, delay = delay, } @@ -43,7 +44,15 @@ function build_test_env() for name, func in pairs(expect) do env.expect[name] = function(...) private.checks = private.checks + 1 - local ok, msg = func(...) + local ret = {func(...)} + local ok = table.remove(ret, 1) + local msg = '' + for _, part in pairs(ret) do + if part then + msg = msg .. ': ' .. tostring(part) + end + end + msg = msg:sub(3) -- strip leading ': ' if ok then private.checks_ok = private.checks_ok + 1 else