robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 5local eq =t . eq 6local pcall_err =t . pcall_err 7local clear =n . clear 8local api =n . api 9local fn =n . fn 10local rmdir =n . rmdir 11local write_file =t . write_file 12local mkdir =t . mkdir 13 14local testdir ='Xtest-functional-spell-spellfile.d' 15 16describe ( 'spellfile' , function () 17before_each ( function () 18clear ( { env ={ XDG_DATA_HOME =testdir .. '/xdg_data' } } ) 19rmdir ( testdir ) 20mkdir ( testdir ) 21mkdir ( testdir .. '/spell' ) 22end ) 23after_each ( function () 24rmdir ( testdir ) 25end ) 26-- ┌ Magic string (#VIMSPELLMAGIC) 27-- │ ┌ Spell file version (#VIMSPELLVERSION) 28local spellheader ='VIMspell\050' 29it ( 'errors out when prefcond section is truncated' , function () 30api . nvim_set_option_value ( 'runtimepath' , testdir , {} ) 31-- stylua: ignore 32write_file ( testdir .. '/spell/en.ascii.spl' , 33-- ┌ Section identifier (#SN_PREFCOND) 34-- │ ┌ Section flags (#SNF_REQUIRED or zero) 35-- │ │ ┌ Section length (4 bytes, MSB first) 36spellheader .. '\003\001\000\000\000\003' 37-- ┌ Number of regexes in section (2 bytes, MSB first) 38-- │ ┌ Condition length (1 byte) 39-- │ │ ┌ Condition regex (missing!) 40.. '\000\001\001' ) 41api . nvim_set_option_value ( 'spelllang' , 'en' , {} ) 42t . matches ( 'Vim%(set%):E758: Truncated spell file' , pcall_err ( n . command , 'set spell' )) 43end ) 44it ( 'errors out when prefcond regexp contains NUL byte' , function () 45api . nvim_set_option_value ( 'runtimepath' , testdir , {} ) 46-- stylua: ignore 47write_file ( testdir .. '/spell/en.ascii.spl' , 48-- ┌ Section identifier (#SN_PREFCOND) 49-- │ ┌ Section flags (#SNF_REQUIRED or zero) 50-- │ │ ┌ Section length (4 bytes, MSB first) 51spellheader .. '\003\001\000\000\000\008' 52-- ┌ Number of regexes in section (2 bytes, MSB first) 53-- │ ┌ Condition length (1 byte) 54-- │ │ ┌ Condition regex 55-- │ │ │ ┌ End of sections marker 56.. '\000\001\005ab\000cd\255' 57-- ┌ LWORDTREE tree length (4 bytes) 58-- │ ┌ KWORDTREE tree length (4 bytes) 59-- │ │ ┌ PREFIXTREE tree length 60.. '\000\000\000\000\000\000\000\000\000\000\000\000' ) 61api . nvim_set_option_value ( 'spelllang' , 'en' , {} ) 62t . matches ( 'Vim%(set%):E759: Format error in spell file' , pcall_err ( n . command , 'set spell' )) 63end ) 64it ( 'errors out when region contains NUL byte' , function () 65api . nvim_set_option_value ( 'runtimepath' , testdir , {} ) 66-- stylua: ignore 67write_file ( testdir .. '/spell/en.ascii.spl' , 68-- ┌ Section identifier (#SN_REGION) 69-- │ ┌ Section flags (#SNF_REQUIRED or zero) 70-- │ │ ┌ Section length (4 bytes, MSB first) 71spellheader .. '\000\001\000\000\000\008' 72-- ┌ Regions ┌ End of sections marker 73.. '01234\00067\255' 74-- ┌ LWORDTREE tree length (4 bytes) 75-- │ ┌ KWORDTREE tree length (4 bytes) 76-- │ │ ┌ PREFIXTREE tree length 77.. '\000\000\000\000\000\000\000\000\000\000\000\000' ) 78api . nvim_set_option_value ( 'spelllang' , 'en' , {} ) 79t . matches ( 'Vim%(set%):E759: Format error in spell file' , pcall_err ( n . command , 'set spell' )) 80end ) 81it ( 'errors out when SAL section contains NUL byte' , function () 82api . nvim_set_option_value ( 'runtimepath' , testdir , {} ) 83-- stylua: ignore 84write_file ( testdir .. '/spell/en.ascii.spl' , 85-- ┌ Section identifier (#SN_SAL) 86-- │ ┌ Section flags (#SNF_REQUIRED or zero) 87-- │ │ ┌ Section length (4 bytes, MSB first) 88spellheader .. '\005\001\000\000\000\008' 89-- ┌ salflags 90-- │ ┌ salcount (2 bytes, MSB first) 91-- │ │ ┌ salfromlen (1 byte) 92-- │ │ │ ┌ Special character 93-- │ │ │ │┌ salfrom (should not contain NUL) 94-- │ │ │ ││ ┌ saltolen 95-- │ │ │ ││ │ ┌ salto 96-- │ │ │ ││ │ │┌ End of sections marker 97.. '\000\000\001\0024\000\0017\255' 98-- ┌ LWORDTREE tree length (4 bytes) 99-- │ ┌ KWORDTREE tree length (4 bytes) 100-- │ │ ┌ PREFIXTREE tree length 101.. '\000\000\000\000\000\000\000\000\000\000\000\000' ) 102api . nvim_set_option_value ( 'spelllang' , 'en' , {} ) 103t . matches ( 'Vim%(set%):E759: Format error in spell file' , pcall_err ( n . command , 'set spell' )) 104end ) 105it ( 'errors out when spell header contains NUL bytes' , function () 106api . nvim_set_option_value ( 'runtimepath' , testdir , {} ) 107write_file ( testdir .. '/spell/en.ascii.spl' , spellheader : sub ( 1 , - 3 ) .. '\000\000' ) 108api . nvim_set_option_value ( 'spelllang' , 'en' , {} ) 109t . matches ( 110'Vim%(set%):E757: This does not look like a spell file' , 111pcall_err ( n . command , 'set spell' ) 112) 113end ) 114 115it ( 'can be set to a relative path' , function () 116local fname =testdir .. '/spell/spell.add' 117api . nvim_set_option_value ( 'spellfile' , fname , {} ) 118end ) 119 120it ( 'can be set to an absolute path' , function () 121local fname =fn . fnamemodify ( testdir .. '/spell/spell.add' , ':p' ) 122api . nvim_set_option_value ( 'spellfile' , fname , {} ) 123end ) 124 125it ( 'uses the current mkspellmem limits for each build' , function () 126local words =testdir .. '/words' 127write_file ( words , 'one\ntwo\nthree\n' ) 128for _ , case in ipairs ( { 129{ '16,16,1' , 2 } , -- Compress while reading and at the end. 130{ '1000,50,10' , 1 } , -- Compress only at the end. 131} ) do 132api . nvim_set_option_value ( 'mkspellmem' , case [ 1 ], {} ) 133local output =n . exec_capture ( 'mkspell! ' .. testdir .. '/spell/test.spl ' .. words ) 134local _ , count =output : gsub ( 'Compressed case%-folded:' , '' ) 135eq ( case [ 2 ], count ) 136end 137end ) 138 139describe ( 'default location' , function () 140it ( "is stdpath('data')/site/spell/en.utf-8.add" , function () 141n . command ( 'set spell' ) 142n . insert ( 'abc' ) 143n . feed ( 'zg' ) 144eq ( 145( '%s/site/spell/en.utf-8.add' ): format ( fn . stdpath ( 'data' )), 146api . nvim_get_option_value ( 'spellfile' , {} ) 147) 148end ) 149 150it ( "is not set if stdpath('data') is not writable" , function () 151n . command ( 'set spell' ) 152fn . writefile ( { '' } , testdir .. '/xdg_data' ) 153n . insert ( 'abc' ) 154eq ( "Vim(normal):E764: Option 'spellfile' is not set" , pcall_err ( n . command , 'normal! zg' )) 155end ) 156 157it ( "is not set if 'spelllang' is not set" , function () 158n . command ( 'set spell spelllang=' ) 159n . insert ( 'abc' ) 160eq ( "Vim(normal):E764: Option 'spellfile' is not set" , pcall_err ( n . command , 'normal! zg' )) 161end ) 162 163it ( 'accepts overwrites on midword and syllable section, errors on memory leak' , function () 164api . nvim_set_option_value ( 'runtimepath' , testdir , {} ) 165-- stylua: ignore 166 167-- Set SN_MIDWORD and SN_SYLLABLE twice so the loader overwrites lp->sl_midword. Should 168-- not leak memory. 169write_file ( testdir .. '/spell/en.ascii.spl' , 170spellheader 171-- ┌ Section identifier (#SN_MIDWORD) 172-- │ ┌ Section flags (#SNF_REQUIRED or zero) 173-- │ │ ┌ Section length (4 bytes, MSB first) 174.. '\002\001\000\000\000\003abc' -- SN_MIDWORD (midword = "abc") 175.. '\002\001\000\000\000\003def' -- Overwrites SN_MIDWORD (midword = "def") 176-- ┌ Section identifier (#SN_SYLLABLE) 177-- │ ┌ Section flags (#SNF_REQUIRED or zero) 178-- │ │ ┌ Section length (4 bytes, MSB first) 179.. '\009\001\000\000\000\003ghi' -- SN_SYLLABLE (syllable = "ghi") 180.. '\009\001\000\000\000\003jib' -- Overwrites SN_SYLLABLE (syllable = "jib") 181.. '\255' -- End of sections marker 182.. '\000\000\000\000' -- LWORDTREE len 183.. '\000\000\000\000' -- KWORDTREE len 184.. '\000\000\000\000' -- PREFIXTREE len 185) 186 187api . nvim_set_option_value ( 'spelllang' , 'en' , {} ) 188n . command ( 'set spell' ) -- Should not error 189eq ( true , api . nvim_get_option_value ( 'spell' , {} )) 190end ) 191end ) 192end )