text_spec.lua 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear = n.clear
  4. local eq = t.eq
  5. describe('vim.text', function()
  6. before_each(clear)
  7. describe('hexencode() and hexdecode()', function()
  8. it('works', function()
  9. local cases = {
  10. { 'Hello world!', '48656C6C6F20776F726C6421' },
  11. { '😂', 'F09F9882' },
  12. }
  13. for _, v in ipairs(cases) do
  14. local input, output = unpack(v)
  15. eq(output, vim.text.hexencode(input))
  16. eq(input, vim.text.hexdecode(output))
  17. end
  18. end)
  19. it('works with very large strings', function()
  20. local input, output = string.rep('😂', 2 ^ 16), string.rep('F09F9882', 2 ^ 16)
  21. eq(output, vim.text.hexencode(input))
  22. eq(input, vim.text.hexdecode(output))
  23. end)
  24. it('errors on invalid input', function()
  25. -- Odd number of hex characters
  26. do
  27. local res, err = vim.text.hexdecode('ABC')
  28. eq(nil, res)
  29. eq('string must have an even number of hex characters', err)
  30. end
  31. -- Non-hexadecimal input
  32. do
  33. local res, err = vim.text.hexdecode('nothex')
  34. eq(nil, res)
  35. eq('string must contain only hex characters', err)
  36. end
  37. end)
  38. end)
  39. end)