spellfile_spec.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq = t.eq
  4. local clear = n.clear
  5. local api = n.api
  6. local exc_exec = n.exc_exec
  7. local fn = n.fn
  8. local rmdir = n.rmdir
  9. local write_file = t.write_file
  10. local mkdir = t.mkdir
  11. local testdir = 'Xtest-functional-spell-spellfile.d'
  12. describe('spellfile', function()
  13. before_each(function()
  14. clear()
  15. rmdir(testdir)
  16. mkdir(testdir)
  17. mkdir(testdir .. '/spell')
  18. end)
  19. after_each(function()
  20. rmdir(testdir)
  21. end)
  22. -- ┌ Magic string (#VIMSPELLMAGIC)
  23. -- │ ┌ Spell file version (#VIMSPELLVERSION)
  24. local spellheader = 'VIMspell\050'
  25. it('errors out when prefcond section is truncated', function()
  26. api.nvim_set_option_value('runtimepath', testdir, {})
  27. -- stylua: ignore
  28. write_file(testdir .. '/spell/en.ascii.spl',
  29. -- ┌ Section identifier (#SN_PREFCOND)
  30. -- │ ┌ Section flags (#SNF_REQUIRED or zero)
  31. -- │ │ ┌ Section length (4 bytes, MSB first)
  32. spellheader .. '\003\001\000\000\000\003'
  33. -- ┌ Number of regexes in section (2 bytes, MSB first)
  34. -- │ ┌ Condition length (1 byte)
  35. -- │ │ ┌ Condition regex (missing!)
  36. .. '\000\001\001')
  37. api.nvim_set_option_value('spelllang', 'en', {})
  38. eq('Vim(set):E758: Truncated spell file', exc_exec('set spell'))
  39. end)
  40. it('errors out when prefcond regexp contains NUL byte', function()
  41. api.nvim_set_option_value('runtimepath', testdir, {})
  42. -- stylua: ignore
  43. write_file(testdir .. '/spell/en.ascii.spl',
  44. -- ┌ Section identifier (#SN_PREFCOND)
  45. -- │ ┌ Section flags (#SNF_REQUIRED or zero)
  46. -- │ │ ┌ Section length (4 bytes, MSB first)
  47. spellheader .. '\003\001\000\000\000\008'
  48. -- ┌ Number of regexes in section (2 bytes, MSB first)
  49. -- │ ┌ Condition length (1 byte)
  50. -- │ │ ┌ Condition regex
  51. -- │ │ │ ┌ End of sections marker
  52. .. '\000\001\005ab\000cd\255'
  53. -- ┌ LWORDTREE tree length (4 bytes)
  54. -- │ ┌ KWORDTREE tree length (4 bytes)
  55. -- │ │ ┌ PREFIXTREE tree length
  56. .. '\000\000\000\000\000\000\000\000\000\000\000\000')
  57. api.nvim_set_option_value('spelllang', 'en', {})
  58. eq('Vim(set):E759: Format error in spell file', exc_exec('set spell'))
  59. end)
  60. it('errors out when region contains NUL byte', function()
  61. api.nvim_set_option_value('runtimepath', testdir, {})
  62. -- stylua: ignore
  63. write_file(testdir .. '/spell/en.ascii.spl',
  64. -- ┌ Section identifier (#SN_REGION)
  65. -- │ ┌ Section flags (#SNF_REQUIRED or zero)
  66. -- │ │ ┌ Section length (4 bytes, MSB first)
  67. spellheader .. '\000\001\000\000\000\008'
  68. -- ┌ Regions ┌ End of sections marker
  69. .. '01234\00067\255'
  70. -- ┌ LWORDTREE tree length (4 bytes)
  71. -- │ ┌ KWORDTREE tree length (4 bytes)
  72. -- │ │ ┌ PREFIXTREE tree length
  73. .. '\000\000\000\000\000\000\000\000\000\000\000\000')
  74. api.nvim_set_option_value('spelllang', 'en', {})
  75. eq('Vim(set):E759: Format error in spell file', exc_exec('set spell'))
  76. end)
  77. it('errors out when SAL section contains NUL byte', function()
  78. api.nvim_set_option_value('runtimepath', testdir, {})
  79. -- stylua: ignore
  80. write_file(testdir .. '/spell/en.ascii.spl',
  81. -- ┌ Section identifier (#SN_SAL)
  82. -- │ ┌ Section flags (#SNF_REQUIRED or zero)
  83. -- │ │ ┌ Section length (4 bytes, MSB first)
  84. spellheader .. '\005\001\000\000\000\008'
  85. -- ┌ salflags
  86. -- │ ┌ salcount (2 bytes, MSB first)
  87. -- │ │ ┌ salfromlen (1 byte)
  88. -- │ │ │ ┌ Special character
  89. -- │ │ │ │┌ salfrom (should not contain NUL)
  90. -- │ │ │ ││ ┌ saltolen
  91. -- │ │ │ ││ │ ┌ salto
  92. -- │ │ │ ││ │ │┌ End of sections marker
  93. .. '\000\000\001\0024\000\0017\255'
  94. -- ┌ LWORDTREE tree length (4 bytes)
  95. -- │ ┌ KWORDTREE tree length (4 bytes)
  96. -- │ │ ┌ PREFIXTREE tree length
  97. .. '\000\000\000\000\000\000\000\000\000\000\000\000')
  98. api.nvim_set_option_value('spelllang', 'en', {})
  99. eq('Vim(set):E759: Format error in spell file', exc_exec('set spell'))
  100. end)
  101. it('errors out when spell header contains NUL bytes', function()
  102. api.nvim_set_option_value('runtimepath', testdir, {})
  103. write_file(testdir .. '/spell/en.ascii.spl', spellheader:sub(1, -3) .. '\000\000')
  104. api.nvim_set_option_value('spelllang', 'en', {})
  105. eq('Vim(set):E757: This does not look like a spell file', exc_exec('set spell'))
  106. end)
  107. it('can be set to a relative path', function()
  108. local fname = testdir .. '/spell/spell.add'
  109. api.nvim_set_option_value('spellfile', fname, {})
  110. end)
  111. it('can be set to an absolute path', function()
  112. local fname = fn.fnamemodify(testdir .. '/spell/spell.add', ':p')
  113. api.nvim_set_option_value('spellfile', fname, {})
  114. end)
  115. end)