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