robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
3.9 KiB118 linesraw
1-- Test suite for testing interactions with API bindings
2local t = require('test.testutil')
3local n = require('test.functional.testnvim')()
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear = n.clear
7local eq = t.eq
8local exec_lua = n.exec_lua
9
10describe('lua vim.mpack', function()
11  before_each(clear)
12  it('encodes vim.NIL', function()
13    eq(
14      { true, true, true, true },
15      exec_lua(function()
16        local var = vim.mpack.decode(vim.mpack.encode({ 33, vim.NIL, 77 }))
17        return { var[1] == 33, var[2] == vim.NIL, var[3] == 77, var[4] == nil }
18      end)
19    )
20  end)
21
22  it('encodes vim.empty_dict()', function()
23    eq(
24      { { {}, 'foo', {} }, true, false },
25      exec_lua(function()
26        local var = vim.mpack.decode(vim.mpack.encode({ {}, 'foo', vim.empty_dict() }))
27        return { var, vim.islist(var[1]), vim.islist(var[3]) }
28      end)
29    )
30  end)
31
32  it('encodes negative integers at type boundaries correctly #37202', function()
33    -- Test boundary values between int8/int16/int32
34    -- int8 range: -128 to -33 (fixint handles -32 to -1)
35    -- int16 range: -32768 to -129
36    -- int32 range: -2147483648 to -32769
37    local result = exec_lua(function()
38      local tests = {
39        { -128, -128 }, -- int8 boundary (minimum int8)
40        { -129, -129 }, -- int16 boundary (one past int8)
41        { -32768, -32768 }, -- int16 boundary (minimum int16)
42        { -32769, -32769 }, -- int32 boundary (one past int16)
43      }
44      local results = {}
45      for _, test in ipairs(tests) do
46        local input, expected = test[1], test[2]
47        local decoded = vim.mpack.decode(vim.mpack.encode(input))
48        table.insert(results, { input = input, decoded = decoded, ok = decoded == expected })
49      end
50      return results
51    end)
52    for _, r in ipairs(result) do
53      eq(true, r.ok, string.format('encode/decode %d returned %d', r.input, r.decoded))
54    end
55  end)
56
57  it('encodes negative integers below -2^32 correctly', function()
58    eq(
59      {},
60      exec_lua(function()
61        local failures = {}
62        for _, v in ipairs({ -(2 ^ 32 + 5), -(3 * 2 ^ 32 + 70000), -(2 ^ 53 - 1) }) do
63          local decoded = vim.mpack.decode(vim.mpack.encode(v))
64          if decoded ~= v then
65            table.insert(failures, ('%.17g -> %.17g'):format(v, decoded))
66          end
67        end
68        return failures
69      end)
70    )
71  end)
72
73  it('encodes numbers beyond 2^53 #32216', function()
74    eq(
75      {},
76      exec_lua(function()
77        local failures = {}
78        local tests = {
79          2 ^ 53,
80          -2 ^ 53,
81          2 ^ 64 - 2048, -- largest uint64 double
82          -2 ^ 63, -- smallest int64
83          -2 ^ 63 - 2048, -- below int64, encoded as float
84          -1.5 * 2 ^ 63,
85          2 ^ 64, -- above uint64, encoded as float
86          1e300,
87          math.huge,
88          -math.huge,
89        }
90        for _, v in ipairs(tests) do
91          local decoded = vim.mpack.decode(vim.mpack.encode(v))
92          if decoded ~= v then
93            table.insert(failures, ('%.17g -> %.17g'):format(v, decoded))
94          end
95        end
96        local nan = vim.mpack.decode(vim.mpack.encode(0 / 0))
97        if nan == nan then
98          table.insert(failures, ('nan -> %.17g'):format(nan))
99        end
100        return failures
101      end)
102    )
103  end)
104
105  it('encodes dict keys of length 20-31 as fixstr #32784', function()
106    -- MessagePack fixstr format: 0xa0 | length (for lengths 0-31)
107    -- Before #36737, strings 20-31 bytes were incorrectly encoded as str8 (0xd9, len)
108    for len = 20, 31 do
109      local expected_header = string.char(0xa0 + len) -- fixstr header
110      local result = exec_lua(function(keylen)
111        local key = string.rep('x', keylen)
112        return vim.mpack.encode({ [key] = 1 })
113      end, len)
114      -- Byte 1 is fixmap header (0x81), byte 2 should be fixstr header for the key
115      eq(expected_header, result:sub(2, 2), 'dict key length ' .. len .. ' should use fixstr')
116    end
117  end)
118end)