neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

mpack_spec.lua (1518B)


      1 -- Test suite for testing interactions with API bindings
      2 local t = require('test.testutil')
      3 local n = require('test.functional.testnvim')()
      4 
      5 local clear = n.clear
      6 local eq = t.eq
      7 local exec_lua = n.exec_lua
      8 
      9 describe('lua vim.mpack', function()
     10  before_each(clear)
     11  it('encodes vim.NIL', function()
     12    eq(
     13      { true, true, true, true },
     14      exec_lua(function()
     15        local var = vim.mpack.decode(vim.mpack.encode({ 33, vim.NIL, 77 }))
     16        return { var[1] == 33, var[2] == vim.NIL, var[3] == 77, var[4] == nil }
     17      end)
     18    )
     19  end)
     20 
     21  it('encodes vim.empty_dict()', function()
     22    eq(
     23      { { {}, 'foo', {} }, true, false },
     24      exec_lua(function()
     25        local var = vim.mpack.decode(vim.mpack.encode({ {}, 'foo', vim.empty_dict() }))
     26        return { var, vim.islist(var[1]), vim.islist(var[3]) }
     27      end)
     28    )
     29  end)
     30 
     31  it('encodes dict keys of length 20-31 as fixstr #32784', function()
     32    -- MessagePack fixstr format: 0xa0 | length (for lengths 0-31)
     33    -- Before #36737, strings 20-31 bytes were incorrectly encoded as str8 (0xd9, len)
     34    for len = 20, 31 do
     35      local expected_header = string.char(0xa0 + len) -- fixstr header
     36      local result = exec_lua(function(keylen)
     37        local key = string.rep('x', keylen)
     38        return vim.mpack.encode({ [key] = 1 })
     39      end, len)
     40      -- Byte 1 is fixmap header (0x81), byte 2 should be fixstr header for the key
     41      eq(expected_header, result:sub(2, 2), 'dict key length ' .. len .. ' should use fixstr')
     42    end
     43  end)
     44 end)