diff options
| author | William Boman <william@redwill.se> | 2022-07-21 14:15:11 +0200 |
|---|---|---|
| committer | William Boman <william@redwill.se> | 2022-07-22 03:03:23 +0200 |
| commit | 1d459b6d19118b9944d5313e4439cb361d607366 (patch) | |
| tree | d48a07eaa5fddebc75ade8bb1d3861992e0c11a3 /tests/helpers/lua/luassertx.lua | |
| download | mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.gz mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.bz2 mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.lz mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.xz mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.tar.zst mason-lspconfig-1d459b6d19118b9944d5313e4439cb361d607366.zip | |
init
Diffstat (limited to 'tests/helpers/lua/luassertx.lua')
| -rw-r--r-- | tests/helpers/lua/luassertx.lua | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/tests/helpers/lua/luassertx.lua b/tests/helpers/lua/luassertx.lua new file mode 100644 index 0000000..55ea0d7 --- /dev/null +++ b/tests/helpers/lua/luassertx.lua @@ -0,0 +1,70 @@ +local assert = require "luassert" +local match = require "luassert.match" +local a = require "mason-core.async" + +local function wait_for(_, arguments) + ---@type (fun()): Function to execute until it does not error. + local assertions_fn = arguments[1] + ---@type number: Timeout in milliseconds. Defaults to 5000. + local timeout = arguments[2] + timeout = timeout or 15000 + + local start = vim.loop.hrtime() + local is_ok, err + repeat + is_ok, err = pcall(assertions_fn) + if not is_ok then + a.sleep(math.min(timeout, 100)) + end + until is_ok or ((vim.loop.hrtime() - start) / 1e6) > timeout + + if not is_ok then + error(err) + end + + return is_ok +end + +local function tbl_containing(_, arguments, _) + return function(value) + local expected = arguments[1] + for key, val in pairs(expected) do + if match.is_matcher(val) then + if not val(value[key]) then + return false + end + elseif value[key] ~= val then + return false + end + end + return true + end +end + +local function list_containing(_, arguments, _) + return function(value) + local expected = arguments[1] + for _, val in pairs(value) do + if match.is_matcher(expected) then + if expected(val) then + return true + end + elseif expected == val then + return true + end + end + return false + end +end + +local function instanceof(_, arguments, _) + return function(value) + local expected_mt = arguments[1] + return getmetatable(value) == expected_mt + end +end + +assert:register("matcher", "tbl_containing", tbl_containing) +assert:register("matcher", "list_containing", list_containing) +assert:register("matcher", "instanceof", instanceof) +assert:register("assertion", "wait_for", wait_for) |
