aboutsummaryrefslogtreecommitdiffstats
path: root/tests/helpers/lua/luassertx.lua
blob: 06c571845291dae4f2e861cdc6f061509ba68d24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
local a = require "mason-core.async"
local assert = require "luassert"
local match = require "luassert.match"
local uv = vim.uv or vim.loop

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 = uv.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 ((uv.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)