robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Test for commands that operate on the spellfile. 2 3source shared.vim 4source check.vim 5 6CheckFeature spell 7CheckFeature syntax 8 9func Test_spell_normal () 10new 11call append ( 0 , [ '1 good' , '2 goood' , '3 goood' ]) 12set spell spellfile = ./Xspellfile.addspelllang = en 13let oldlang = v: lang 14lang C 15 16" Test for zg 171 18norm ! ]s 19call assert_equal ( '2 goood' , getline ( '.' )) 20norm ! zg 211 22let a = execute ( 'unsilent :norm! ]s' ) 23call assert_equal ( '1 good' , getline ( '.' )) 24call assert_equal ( 'search hit BOTTOM, continuing at TOP' , a [ 1 :]) 25let cnt = readfile ( './Xspellfile.add' ) 26call assert_equal ( 'goood' , cnt [ 0 ]) 27 28" zg should fail in operator-pending mode 29call assert_beeps ( 'norm! czg' ) 30 31" zg fails in visual mode when not able to get the visual text 32call assert_beeps ( 'norm! ggVjzg' ) 33norm ! V 34 35" zg fails for a non-identifier word 36call append ( line ( '$' ), '###' ) 37call assert_fails ( 'norm! Gzg' , 'E349:' ) 38$ d 39 40" Test for zw 412 42norm ! $zw 431 44norm ! ]s 45call assert_equal ( '2 goood' , getline ( '.' )) 46let cnt = readfile ( './Xspellfile.add' ) 47call assert_equal ( '#oood' , cnt [ 0 ]) 48call assert_equal ( 'goood/!' , cnt [ 1 ]) 49 50" Test for :spellrare 51spellrare rare 52let cnt = readfile ( './Xspellfile.add' ) 53call assert_equal ([ '#oood' , 'goood/!' , 'rare/?' ], cnt ) 54 55" Make sure :spellundo works for rare words. 56spellundo rare 57let cnt = readfile ( './Xspellfile.add' ) 58call assert_equal ([ '#oood' , 'goood/!' , '#are/?' ], cnt ) 59 60" Test for zg in visual mode 61let a = execute ( 'unsilent :norm! V$zg' ) 62call assert_equal ( "Word '2 goood' added to ./Xspellfile.add" , a [ 1 :]) 631 64norm ! ]s 65call assert_equal ( '3 goood' , getline ( '.' )) 66let cnt = readfile ( './Xspellfile.add' ) 67call assert_equal ( '2 goood' , cnt [ 3 ]) 68" Remove "2 good" from spellfile 692 70let a = execute ( 'unsilent norm! V$zw' ) 71call assert_equal ( "Word '2 goood' added to ./Xspellfile.add" , a [ 1 :]) 72let cnt = readfile ( './Xspellfile.add' ) 73call assert_equal ( '2 goood/!' , cnt [ 4 ]) 74 75" Test for zG 76let a = execute ( 'unsilent norm! V$zG' ) 77call assert_match ( "Word '2 goood' added to .*" , a ) 78let fname = matchstr ( a , 'to\s\+\zs\f\+$' ) 79let cnt = readfile ( fname ) 80call assert_equal ( '2 goood' , cnt [ 0 ]) 81 82" Test for zW 83let a = execute ( 'unsilent norm! V$zW' ) 84call assert_match ( "Word '2 goood' added to .*" , a ) 85let cnt = readfile ( fname ) 86call assert_equal ( '# goood' , cnt [ 0 ]) 87call assert_equal ( '2 goood/!' , cnt [ 1 ]) 88 89" Test for zuW 90let a = execute ( 'unsilent norm! V$zuW' ) 91call assert_match ( "Word '2 goood' removed from .*" , a ) 92let cnt = readfile ( fname ) 93call assert_equal ( '# goood' , cnt [ 0 ]) 94call assert_equal ( '# goood/!' , cnt [ 1 ]) 95 96" Test for zuG 97let a = execute ( 'unsilent norm! $zG' ) 98call assert_match ( "Word 'goood' added to .*" , a ) 99let cnt = readfile ( fname ) 100call assert_equal ( '# goood' , cnt [ 0 ]) 101call assert_equal ( '# goood/!' , cnt [ 1 ]) 102call assert_equal ( 'goood' , cnt [ 2 ]) 103let a = execute ( 'unsilent norm! $zuG' ) 104let cnt = readfile ( fname ) 105call assert_match ( "Word 'goood' removed from .*" , a ) 106call assert_equal ( '# goood' , cnt [ 0 ]) 107call assert_equal ( '# goood/!' , cnt [ 1 ]) 108call assert_equal ( '#oood' , cnt [ 2 ]) 109" word not found in wordlist 110let a = execute ( 'unsilent norm! V$zuG' ) 111let cnt = readfile ( fname ) 112call assert_match ( "" , a ) 113call assert_equal ( '# goood' , cnt [ 0 ]) 114call assert_equal ( '# goood/!' , cnt [ 1 ]) 115call assert_equal ( '#oood' , cnt [ 2 ]) 116 117" Test for zug 118call delete ( './Xspellfile.add' ) 1192 120let a = execute ( 'unsilent norm! $zg' ) 121let cnt = readfile ( './Xspellfile.add' ) 122call assert_equal ( 'goood' , cnt [ 0 ]) 123let a = execute ( 'unsilent norm! $zug' ) 124call assert_match ( "Word 'goood' removed from \./Xspellfile.add" , a ) 125let cnt = readfile ( './Xspellfile.add' ) 126call assert_equal ( '#oood' , cnt [ 0 ]) 127" word not in wordlist 128let a = execute ( 'unsilent norm! V$zug' ) 129call assert_match ( '' , a ) 130let cnt = readfile ( './Xspellfile.add' ) 131call assert_equal ( '#oood' , cnt [ 0 ]) 132 133" Test for zuw 134call delete ( './Xspellfile.add' ) 1352 136let a = execute ( 'unsilent norm! Vzw' ) 137let cnt = readfile ( './Xspellfile.add' ) 138call assert_equal ( '2 goood/!' , cnt [ 0 ]) 139let a = execute ( 'unsilent norm! Vzuw' ) 140call assert_match ( "Word '2 goood' removed from \./Xspellfile.add" , a ) 141let cnt = readfile ( './Xspellfile.add' ) 142call assert_equal ( '# goood/!' , cnt [ 0 ]) 143" word not in wordlist 144let a = execute ( 'unsilent norm! $zug' ) 145call assert_match ( '' , a ) 146let cnt = readfile ( './Xspellfile.add' ) 147call assert_equal ( '# goood/!' , cnt [ 0 ]) 148 149" add second entry to spellfile setting 150set spellfile = ./Xspellfile.add,./Xspellfile2.add 151call delete ( './Xspellfile.add' ) 1522 153let a = execute ( 'unsilent norm! $2zg' ) 154let cnt = readfile ( './Xspellfile2.add' ) 155call assert_match ( "Word 'goood' added to ./Xspellfile2.add" , a ) 156call assert_equal ( 'goood' , cnt [ 0 ]) 157 158" Test for :spellgood! 159let temp = execute ( ':spe!0/0' ) 160call assert_match ( 'Invalid region' , temp ) 161let spellfile = matchstr ( temp , 'Invalid region nr in \zs.*\ze line \d: 0' ) 162call assert_equal ([ '# goood' , '# goood/!' , '#oood' , '0/0' ], readfile ( spellfile )) 163 164" Test for :spellrare! 165: spellrare ! raare 166call assert_equal ([ '# goood' , '# goood/!' , '#oood' , '0/0' , 'raare/?' ], readfile ( spellfile )) 167call delete ( spellfile ) 168 169" clean up 170exe "lang" oldlang 171call delete ( "./Xspellfile.add" ) 172call delete ( "./Xspellfile2.add" ) 173call delete ( "./Xspellfile.add.spl" ) 174call delete ( "./Xspellfile2.add.spl" ) 175 176" zux -> no-op 1772 178norm ! $zux 179call assert_equal ([], glob ( 'Xspellfile.add' , 0 , 1 )) 180call assert_equal ([], glob ( 'Xspellfile2.add' , 0 , 1 )) 181 182set spellfile = spell & spelllang & 183bw ! 184endfunc 185 186" Spell file content test. Write 'content' to the spell file prefixed by the 187" spell file header and then enable spell checking. If 'emsg' is not empty, 188" then check for error. 189func Spellfile_Test ( content , emsg ) 190let splfile = './Xtest/spell/Xtest.utf-8.spl' 191" Add the spell file header and version (VIMspell2) 192let v = 0 z56494D7370656C6C32 + a:content 193call writefile ( v , splfile , 'b' ) 194 195" 'encoding' is set before each test to clear the previously loaded suggest 196" file from memory. 197set encoding = utf-8 198 199set runtimepath = ./Xtest 200set spelllang = Xtest 201if a: emsg != '' 202call assert_fails ( 'set spell' , a: emsg ) 203else 204" FIXME: With some invalid spellfile contents, there are no error 205" messages. So don't know how to check for the test result. 206set spell 207endif 208set nospell spelllang & rtp & 209endfunc 210 211" Test for spell file format errors. 212" The spell file format is described in spellfile.c 213func Test_spellfile_format_error () 214let save_rtp = & rtp 215call mkdir ( 'Xtest/spell' , 'pR' ) 216let splfile = './Xtest/spell/Xtest.utf-8.spl' 217 218" empty spell file 219call writefile ([], splfile ) 220set runtimepath = ./Xtest 221set spelllang = Xtest 222call assert_fails ( 'set spell' , 'E757:' ) 223set nospell spelllang & 224 225" invalid file ID 226call writefile ( 0 z56494D, splfile, 'b') 227set runtimepath = ./Xtest 228set spelllang = Xtest 229call assert_fails ( 'set spell' , 'E757:' ) 230set nospell spelllang & 231 232" missing version number 233call writefile ( 0 z56494D7370656C6C, splfile, 'b') 234set runtimepath = ./Xtest 235set spelllang = Xtest 236call assert_fails ( 'set spell' , 'E771:' ) 237set nospell spelllang & 238 239" invalid version number 240call writefile ( 0 z56494D7370656C6C7A, splfile, 'b') 241set runtimepath = ./Xtest 242set spelllang = Xtest 243call assert_fails ( 'set spell' , 'E772:' ) 244set nospell spelllang & 245 246" no sections 247call Spellfile_Test ( 0 z, 'E758:') 248 249" missing section length 250call Spellfile_Test ( 0 z00, 'E758:') 251 252" unsupported required section 253call Spellfile_Test ( 0 z7A0100000004, 'E770:') 254 255" unsupported not-required section 256call Spellfile_Test ( 0 z7A0000000004, 'E758:') 257 258" SN_REGION: invalid number of region names 259call Spellfile_Test ( 0 z0000000000FF, 'E759:') 260 261" SN_CHARFLAGS: missing <charflagslen> length 262call Spellfile_Test ( 0 z010000000004, 'E758:') 263 264" SN_CHARFLAGS: invalid <charflagslen> length 265call Spellfile_Test ( 0 z0100000000010201, '') 266 267" SN_CHARFLAGS: charflagslen == 0 and folcharslen != 0 268call Spellfile_Test ( 0 z01000000000400000101, 'E759:') 269 270" SN_CHARFLAGS: missing <folcharslen> length 271call Spellfile_Test ( 0 z01000000000100, 'E758:') 272 273" SN_PREFCOND: invalid prefcondcnt 274call Spellfile_Test ( 0 z03000000000100, 'E759:') 275 276" SN_PREFCOND: invalid condlen 277call Spellfile_Test ( 0 z0300000000020001, 'E759:') 278 279" SN_REP: invalid repcount 280call Spellfile_Test ( 0 z04000000000100, 'E758:') 281 282" SN_REP: missing rep 283call Spellfile_Test ( 0 z0400000000020004, 'E758:') 284 285" SN_REP: zero repfromlen 286call Spellfile_Test ( 0 z040000000003000100, 'E759:') 287 288" SN_REP: invalid reptolen 289call Spellfile_Test ( 0 z0400000000050001014101, '') 290 291" SN_REP: zero reptolen 292call Spellfile_Test ( 0 z0400000000050001014100, 'E759:') 293 294" SN_SAL: missing salcount 295call Spellfile_Test ( 0 z05000000000102, 'E758:') 296 297" SN_SAL: missing salfromlen 298call Spellfile_Test ( 0 z050000000003080001, 'E758:') 299 300" SN_SAL: missing saltolen 301call Spellfile_Test ( 0 z0500000000050400010161, 'E758:') 302 303" SN_WORDS: non-NUL terminated word 304call Spellfile_Test ( 0 z0D000000000376696D, 'E758:') 305 306" SN_WORDS: very long word 307let v = eval ( '0z0D000000012C' .. repeat ( '41' , 300 )) 308call Spellfile_Test ( v , 'E759:' ) 309 310" SN_SOFO: missing sofofromlen 311call Spellfile_Test ( 0 z06000000000100, 'E758:') 312 313" SN_SOFO: missing sofotolen 314call Spellfile_Test ( 0 z06000000000400016100, 'E758:') 315 316" SN_SOFO: missing sofoto 317call Spellfile_Test ( 0 z0600000000050001610000, 'E759:') 318 319" SN_SOFO: empty sofofrom and sofoto 320call Spellfile_Test ( 0 z06000000000400000000FF000000000000000000000000, '') 321 322" SN_SOFO: multi-byte characters in sofofrom and sofoto 323call Spellfile_Test ( 0 z0600000000080002CF810002CF82FF000000000000000000000000, '') 324 325" SN_SAL (empty) followed by SN_SOFO with two multi-byte 'from' characters 326" sharing the same low byte. A preceding SN_SAL poisons sl_sal_first[], so 327" without a reset set_sofo() under-counts and writes out of bounds. 328call Spellfile_Test ( 0 z05000000000300000006000000000A0004CAABCEAB00024142FF000000000000000000000000, '') 329 330" SN_COMPOUND: compmax is less than 2 331call Spellfile_Test ( 0 z08000000000101, 'E759:') 332 333" SN_COMPOUND: missing compsylmax and other options 334call Spellfile_Test ( 0 z0800000000020401, 'E759:') 335 336" SN_COMPOUND: missing compoptions 337call Spellfile_Test ( 0 z080000000005040101, 'E758:') 338 339" SN_COMPOUND: missing comppattern 340call Spellfile_Test ( 0 z08000000000704010100000001, 'E758:') 341 342" SN_COMPOUND: incorrect comppatlen 343call Spellfile_Test ( 0 z080000000007040101000000020165, 'E758:') 344 345" SN_COMPOUND: oversized sectionlen 346let v = eval ( '0z08004000000803010161' .. repeat ( '61' , 50 ) .. 'FF' ) 347call Spellfile_Test ( v , 'E759:' ) 348 349" SN_INFO: missing info 350call Spellfile_Test ( 0 z0F0000000005040101, '') 351 352" SN_MIDWORD: missing midword 353call Spellfile_Test ( 0 z0200000000040102, '') 354 355" SN_MAP: missing midword 356call Spellfile_Test ( 0 z0700000000040102, '') 357 358" SN_MAP: empty map string 359call Spellfile_Test ( 0 z070000000000FF000000000000000000000000, '') 360 361" SN_MAP: duplicate multibyte character 362call Spellfile_Test ( 0 z070000000004DC81DC81, 'E783:') 363 364" SN_SYLLABLE: missing SYLLABLE item 365call Spellfile_Test ( 0 z0900000000040102, '') 366 367" SN_SYLLABLE: More than SY_MAXLEN size 368let v = eval ( '0z090000000022612F' .. repeat ( '62' , 32 )) 369call Spellfile_Test ( v , '' ) 370 371" LWORDTREE: missing 372call Spellfile_Test ( 0 zFF, 'E758:') 373 374" LWORDTREE: missing tree node 375call Spellfile_Test ( 0 zFF00000004, 'E758:') 376 377" LWORDTREE: missing tree node value 378call Spellfile_Test ( 0 zFF0000000402, 'E758:') 379 380" LWORDTREE: incorrect sibling node count 381call Spellfile_Test ( 0 zFF00000001040000000000000000, 'E759:') 382 383" LWORDTREE: declared nodecount larger than the tree actually fills. 384" Root has two siblings: 'x' (recurses into an end-of-word at idx 3..4) 385" and BY_INDEX targeting position 9. Tree fills positions 0..4, leaving 386" 5..9 unwritten — byts[9] would be uninitialized without the fix. 387call Spellfile_Test ( 0 zFF0000000A02780100000979010000000000000000000000, 'E759:') 388 389" LWORDTREE: recursion depth past MAXWLEN. A linear chain of 254 390" (siblingcount=1, byte='a') frames drives read_tree_node to depth 391" MAXWLEN where the new guard rejects. The trailing (01 00) gives the 392" chain a clean end-of-word so an *unguarded* parser would accept the 393" file silently — that's what makes this a meaningful regression test 394" for the depth check specifically (a deeper chain would also crash 395" unguarded builds via stack overflow, which we don't want in CI). 396let v = eval ( '0zFF00000200' .. repeat ( '0161' , 255 ) 397 \.. '0100' .. repeat ( '00' , 8 )) 398 399call Spellfile_Test ( v , 'E759:' ) 400 401" KWORDTREE: missing tree node 402call Spellfile_Test ( 0 zFF0000000000000004, 'E758:') 403 404" PREFIXTREE: missing tree node 405call Spellfile_Test ( 0 zFF000000000000000000000004, 'E758:') 406 407" PREFIXTREE: incorrect prefcondnr 408call Spellfile_Test ( 0 zFF000000000000000000000002010200000020, 'E759:') 409 410" PREFIXTREE: invalid nodeidx 411call Spellfile_Test ( 0 zFF00000000000000000000000201010000, 'E759:') 412 413let & rtp = save_rtp 414endfunc 415 416" An over-length soundfold() argument must not overflow the MAXWLEN result 417" buffer in the single-byte branch of spell_soundfold_sal(). 418func Test_spellfile_soundfold_sal_overflow () 419throw 'Skipped: Nvim does not support enc=latin1' 420let save_enc = & encoding 421set encoding = latin1 422" A SAL map that appends without collapsing, so the result is not shorter 423" than the input. 424call writefile ([ 'SET ISO8859-1' , 'SAL collapse_result false' , 425 \'SAL a aaaa' , 'SAL b bbbb' ], 'Xsal.aff' ) 426call writefile ([ '2' , 'hello' , 'world' ], 'Xsal.dic' ) 427mkspell ! Xsal Xsal 428set spl = Xsal.latin1.splspell 429 430" 253 input characters hit the buffer boundary; the result must not exceed 431" MAXWLEN - 1. 432call assert_true ( strlen ( soundfold ( repeat ( 'a' , 253 ))) <= 253 ) 433 434set nospell spl & spelllang & 435call delete ( 'Xsal.aff' ) 436call delete ( 'Xsal.dic' ) 437call delete ( 'Xsal.latin1.spl' ) 438let & encoding = save_enc 439endfunc 440 441" Test for format errors in suggest file 442func Test_sugfile_format_error () 443let save_rtp = & rtp 444call mkdir ( 'Xtest/spell' , 'pR' ) 445let splfile = './Xtest/spell/Xtest.utf-8.spl' 446let sugfile = './Xtest/spell/Xtest.utf-8.sug' 447 448" create an empty spell file with a suggest timestamp 449call writefile ( 0 z56494D7370656C6C320B00000000080000000000000044FF000000000000000000000000, splfile, 'b') 450 451" 'encoding' is set before each test to clear the previously loaded suggest 452" file from memory. 453 454" empty suggest file 455set encoding = utf-8 456call writefile ([], sugfile ) 457set runtimepath = ./Xtest 458set spelllang = Xtest 459set spell 460call assert_fails ( "let s = spellsuggest('abc')" , 'E778:' ) 461set nospell spelllang & 462 463" zero suggest version 464set encoding = utf-8 465call writefile ( 0 z56494D73756700, sugfile) 466set runtimepath = ./Xtest 467set spelllang = Xtest 468set spell 469call assert_fails ( "let s = spellsuggest('abc')" , 'E779:' ) 470set nospell spelllang & 471 472" unsupported suggest version 473set encoding = utf-8 474call writefile ( 0 z56494D7375671F, sugfile) 475set runtimepath = ./Xtest 476set spelllang = Xtest 477set spell 478call assert_fails ( "let s = spellsuggest('abc')" , 'E780:' ) 479set nospell spelllang & 480 481" missing suggest timestamp 482set encoding = utf-8 483call writefile ( 0 z56494D73756701, sugfile) 484set runtimepath = ./Xtest 485set spelllang = Xtest 486set spell 487call assert_fails ( "let s = spellsuggest('abc')" , 'E781:' ) 488set nospell spelllang & 489 490" incorrect suggest timestamp 491set encoding = utf-8 492call writefile ( 0 z56494D7375670100000000000000FF, sugfile) 493set runtimepath = ./Xtest 494set spelllang = Xtest 495set spell 496call assert_fails ( "let s = spellsuggest('abc')" , 'E781:' ) 497set nospell spelllang & 498 499" missing suggest wordtree 500set encoding = utf-8 501call writefile ( 0 z56494D737567010000000000000044, sugfile) 502set runtimepath = ./Xtest 503set spelllang = Xtest 504set spell 505call assert_fails ( "let s = spellsuggest('abc')" , 'E782:' ) 506set nospell spelllang & 507 508" invalid suggest word count in SUGTABLE 509set encoding = utf-8 510call writefile ( 0 z56494D7375670100000000000000440000000022, sugfile) 511set runtimepath = ./Xtest 512set spelllang = Xtest 513set spell 514call assert_fails ( "let s = spellsuggest('abc')" , 'E782:' ) 515set nospell spelllang & 516 517" missing sugline in SUGTABLE 518set encoding = utf-8 519call writefile ( 0 z56494D7375670100000000000000440000000000000005, sugfile) 520set runtimepath = ./Xtest 521set spelllang = Xtest 522set spell 523call assert_fails ( "let s = spellsuggest('abc')" , 'E782:' ) 524set nospell spelllang & 525 526let & rtp = save_rtp 527endfunc 528 529" Test for using :mkspell to create a spell file from a list of words 530func Test_wordlist_dic () 531" duplicate encoding 532let lines =<< trim [END] 533# This is an example word list 534 535/encoding=latin1 536/encoding=latin1 537example 538[END] 539call writefile ( lines , 'Xwordlist.dic' , 'D' ) 540let output = execute ( 'mkspell Xwordlist.spl Xwordlist.dic' ) 541call assert_match ( 'Duplicate /encoding= line ignored in Xwordlist.dic line 4: /encoding=latin1' , output ) 542 543" multiple encoding for a word 544let lines =<< trim [END] 545example 546/encoding=latin1 547example 548[END] 549call writefile ( lines , 'Xwordlist.dic' ) 550let output = execute ( 'mkspell! Xwordlist.spl Xwordlist.dic' ) 551call assert_match ( '/encoding= line after word ignored in Xwordlist.dic line 2: /encoding=latin1' , output ) 552 553" unsupported encoding for a word 554let lines =<< trim [END] 555/encoding=Xtest 556example 557[END] 558call writefile ( lines , 'Xwordlist.dic' ) 559let output = execute ( 'mkspell! Xwordlist.spl Xwordlist.dic' ) 560call assert_match ( 'Conversion in Xwordlist.dic not supported: from Xtest to utf-8' , output ) 561 562" duplicate region 563let lines =<< trim [END] 564/regions=usca 565/regions=usca 566example 567[END] 568call writefile ( lines , 'Xwordlist.dic' ) 569let output = execute ( 'mkspell! Xwordlist.spl Xwordlist.dic' ) 570call assert_match ( 'Duplicate /regions= line ignored in Xwordlist.dic line 2: regions=usca' , output ) 571 572" maximum regions 573let lines =<< trim [END] 574/regions=uscauscauscauscausca 575example 576[END] 577call writefile ( lines , 'Xwordlist.dic' ) 578let output = execute ( 'mkspell! Xwordlist.spl Xwordlist.dic' ) 579call assert_match ( 'Too many regions in Xwordlist.dic line 1: uscauscauscauscausca' , output ) 580 581" unsupported '/' value 582let lines =<< trim [END] 583/test=abc 584example 585[END] 586call writefile ( lines , 'Xwordlist.dic' ) 587let output = execute ( 'mkspell! Xwordlist.spl Xwordlist.dic' ) 588call assert_match ( '/ line ignored in Xwordlist.dic line 1: /test=abc' , output ) 589 590" unsupported flag 591let lines =<< trim [END] 592example/+ 593[END] 594call writefile ( lines , 'Xwordlist.dic' ) 595let output = execute ( 'mkspell! Xwordlist.spl Xwordlist.dic' ) 596call assert_match ( 'Unrecognized flags in Xwordlist.dic line 1: +' , output ) 597 598" non-ascii word 599call writefile ([ "ʀʀ" ], 'Xwordlist.dic' ) 600let output = execute ( 'mkspell! -ascii Xwordlist.spl Xwordlist.dic' ) 601call assert_match ( 'Ignored 1 words with non-ASCII characters' , output ) 602 603" keep case of a word 604let lines =<< trim [END] 605example/= 606[END] 607call writefile ( lines , 'Xwordlist.dic' ) 608let output = execute ( 'mkspell! Xwordlist.spl Xwordlist.dic' ) 609call assert_match ( 'Compressed keep-case:' , output ) 610 611call delete ( 'Xwordlist.spl' ) 612endfunc 613 614" Test for the :mkspell command 615func Test_mkspell () 616call assert_fails ( 'mkspell Xtest_us.spl' , 'E751:' ) 617call assert_fails ( 'mkspell Xtest.spl abc' , 'E484:' ) 618call assert_fails ( 'mkspell a b c d e f g h i j k' , 'E754:' ) 619 620" create a .aff file but not the .dic file 621call writefile ([], 'Xtest.aff' ) 622call assert_fails ( 'mkspell Xtest.spl Xtest' , 'E484:' ) 623call delete ( 'Xtest.aff' ) 624 625call writefile ([], 'Xtest.spl' ) 626call writefile ([], 'Xtest.dic' ) 627call assert_fails ( 'mkspell Xtest.spl Xtest.dic' , 'E13:' ) 628call delete ( 'Xtest.spl' ) 629call delete ( 'Xtest.dic' ) 630 631call mkdir ( 'Xtest.spl' ) 632call assert_fails ( 'mkspell! Xtest.spl Xtest.dic' , 'E17:' ) 633call delete ( 'Xtest.spl' , 'rf' ) 634 635" can't write the .spl file as its directory does not exist 636call writefile ([], 'Xtest.aff' ) 637call writefile ([], 'Xtest.dic' ) 638call assert_fails ( 'mkspell DOES_NOT_EXIT/Xtest.spl Xtest.dic' , 'E484:' ) 639call delete ( 'Xtest.aff' ) 640call delete ( 'Xtest.dic' ) 641 642call assert_fails ( 'mkspell en en_US abc_xyz' , 'E755:' ) 643endfunc 644 645" Tests for :mkspell with a .dic and .aff file 646func Test_aff_file_format_error () 647" FIXME: For some reason, the :mkspell command below doesn't fail on the 648" MS-Windows CI build. Disable this test on MS-Windows for now. 649CheckNotMSWindows 650 651" No word count in .dic file 652call writefile ([], 'Xtest.dic' , 'D' ) 653call writefile ([], 'Xtest.aff' , 'D' ) 654call assert_fails ( 'mkspell! Xtest.spl Xtest' , 'E760:' ) 655 656" create a .dic file for the tests below 657call writefile ([ '1' , 'work' ], 'Xtest.dic' ) 658 659" Invalid encoding in .aff file 660call writefile ([ '# comment' , 'SET Xinvalidencoding' ], 'Xtest.aff' ) 661let output = execute ( 'mkspell! Xtest.spl Xtest' ) 662call assert_match ( 'Conversion in Xtest.aff not supported: from xinvalidencoding' , output ) 663 664" Invalid flag in .aff file 665call writefile ([ 'FLAG xxx' ], 'Xtest.aff' ) 666let output = execute ( 'mkspell! Xtest.spl Xtest' ) 667call assert_match ( 'Invalid value for FLAG in Xtest.aff line 1: xxx' , output ) 668 669" set FLAGS after using flag for an affix 670call writefile ([ 'SFX L Y 1' , 'SFX L 0 re [^x]' , 'FLAG long' ], 'Xtest.aff' ) 671let output = execute ( 'mkspell! Xtest.spl Xtest' ) 672call assert_match ( 'FLAG after using flags in Xtest.aff line 3: long' , output ) 673 674" INFO in affix file 675let save_encoding = & encoding 676call mkdir ( 'Xrtp/spell' , 'p' ) 677call writefile ([ '1' , 'work' ], 'Xrtp/spell/Xtest.dic' ) 678call writefile ([ 'NAME klingon' , 'VERSION 1.4' , 'AUTHOR Spock' ], 679 \'Xrtp/spell/Xtest.aff' ) 680silent mkspell ! Xrtp/spell/Xtest.utf-8.spl Xrtp/spell/Xtest 681let save_rtp = & rtp 682set runtimepath = ./Xrtp 683set spelllang = Xtest 684set spell 685let output = split ( execute ( 'spellinfo' ), "\n" ) 686call assert_equal ( "NAME klingon" , output [ 1 ]) 687call assert_equal ( "VERSION 1.4" , output [ 2 ]) 688call assert_equal ( "AUTHOR Spock" , output [ 3 ]) 689let & rtp = save_rtp 690call delete ( 'Xrtp' , 'rf' ) 691set spell & spelllang & spellfile & 692 %bw ! 693" 'encoding' must be set again to clear the spell file in memory 694let & encoding = save_encoding 695 696" COMPOUNDFORBIDFLAG flag after PFX in an affix file 697call writefile ([ 'PFX L Y 1' , 'PFX L 0 re x' , 'COMPOUNDFLAG c' , 'COMPOUNDFORBIDFLAG x' ], 698 \'Xtest.aff' ) 699let output = execute ( 'mkspell! Xtest.spl Xtest' ) 700call assert_match ( 'Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in Xtest.aff line 4' , output ) 701 702" COMPOUNDPERMITFLAG flag after PFX in an affix file 703call writefile ([ 'PFX L Y 1' , 'PFX L 0 re x' , 'COMPOUNDPERMITFLAG c' ], 704 \'Xtest.aff' ) 705let output = execute ( 'mkspell! Xtest.spl Xtest' ) 706call assert_match ( 'Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in Xtest.aff line 3' , output ) 707 708" Wrong COMPOUNDRULES flag value in an affix file 709call writefile ([ 'COMPOUNDRULES a' ], 'Xtest.aff' ) 710let output = execute ( 'mkspell! Xtest.spl Xtest' ) 711call assert_match ( 'Wrong COMPOUNDRULES value in Xtest.aff line 1: a' , output ) 712 713" Wrong COMPOUNDWORDMAX flag value in an affix file 714call writefile ([ 'COMPOUNDWORDMAX 0' ], 'Xtest.aff' ) 715let output = execute ( 'mkspell! Xtest.spl Xtest' ) 716call assert_match ( 'Wrong COMPOUNDWORDMAX value in Xtest.aff line 1: 0' , output ) 717 718" Wrong COMPOUNDMIN flag value in an affix file 719call writefile ([ 'COMPOUNDMIN 0' ], 'Xtest.aff' ) 720let output = execute ( 'mkspell! Xtest.spl Xtest' ) 721call assert_match ( 'Wrong COMPOUNDMIN value in Xtest.aff line 1: 0' , output ) 722 723" Wrong COMPOUNDSYLMAX flag value in an affix file 724call writefile ([ 'COMPOUNDSYLMAX 0' ], 'Xtest.aff' ) 725let output = execute ( 'mkspell! Xtest.spl Xtest' ) 726call assert_match ( 'Wrong COMPOUNDSYLMAX value in Xtest.aff line 1: 0' , output ) 727 728" Wrong CHECKCOMPOUNDPATTERN flag value in an affix file 729call writefile ([ 'CHECKCOMPOUNDPATTERN 0' ], 'Xtest.aff' ) 730let output = execute ( 'mkspell! Xtest.spl Xtest' ) 731call assert_match ( 'Wrong CHECKCOMPOUNDPATTERN value in Xtest.aff line 1: 0' , output ) 732 733" Both compounding and NOBREAK specified 734call writefile ([ 'COMPOUNDFLAG c' , 'NOBREAK' ], 'Xtest.aff' ) 735let output = execute ( 'mkspell! Xtest.spl Xtest' ) 736call assert_match ( 'Warning: both compounding and NOBREAK specified' , output ) 737 738" Duplicate affix entry in an affix file 739call writefile ([ 'PFX L Y 1' , 'PFX L 0 re x' , 'PFX L Y 1' , 'PFX L 0 re x' ], 740 \'Xtest.aff' ) 741let output = execute ( 'mkspell! Xtest.spl Xtest' ) 742call assert_match ( 'Duplicate affix in Xtest.aff line 3: L' , output ) 743 744" Duplicate affix entry in an affix file 745call writefile ([ 'PFX L Y 1' , 'PFX L Y 1' ], 'Xtest.aff' ) 746let output = execute ( 'mkspell! Xtest.spl Xtest' ) 747call assert_match ( 'Unrecognized or duplicate item in Xtest.aff line 2: PFX' , output ) 748 749" Different combining flags in an affix file 750call writefile ([ 'PFX L Y 1' , 'PFX L 0 re x' , 'PFX L N 1' ], 'Xtest.aff' ) 751let output = execute ( 'mkspell! Xtest.spl Xtest' ) 752call assert_match ( 'Different combining flag in continued affix block in Xtest.aff line 3' , output ) 753 754" Try to reuse an affix used for BAD flag 755call writefile ([ 'BAD x' , 'PFX x Y 1' , 'PFX x 0 re x' ], 'Xtest.aff' ) 756let output = execute ( 'mkspell! Xtest.spl Xtest' ) 757call assert_match ( 'Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in Xtest.aff line 2: x' , output ) 758 759" Trailing characters in an affix entry 760call writefile ([ 'PFX L Y 1 Test' , 'PFX L 0 re x' ], 'Xtest.aff' ) 761let output = execute ( 'mkspell! Xtest.spl Xtest' ) 762call assert_match ( 'Trailing text in Xtest.aff line 1: Test' , output ) 763 764" Trailing characters in an affix entry 765call writefile ([ 'PFX L Y 1' , 'PFX L 0 re x Test' ], 'Xtest.aff' ) 766let output = execute ( 'mkspell! Xtest.spl Xtest' ) 767call assert_match ( 'Trailing text in Xtest.aff line 2: Test' , output ) 768 769" Incorrect combine flag in an affix entry 770call writefile ([ 'PFX L X 1' , 'PFX L 0 re x' ], 'Xtest.aff' ) 771let output = execute ( 'mkspell! Xtest.spl Xtest' ) 772call assert_match ( 'Expected Y or N in Xtest.aff line 1: X' , output ) 773 774" Invalid count for REP item 775call writefile ([ 'REP a' ], 'Xtest.aff' ) 776let output = execute ( 'mkspell! Xtest.spl Xtest' ) 777call assert_match ( 'Expected REP(SAL) count in Xtest.aff line 1' , output ) 778 779" Trailing characters in REP item 780call writefile ([ 'REP 1' , 'REP f ph test' ], 'Xtest.aff' ) 781let output = execute ( 'mkspell! Xtest.spl Xtest' ) 782call assert_match ( 'Trailing text in Xtest.aff line 2: test' , output ) 783 784" Invalid count for MAP item 785call writefile ([ 'MAP a' ], 'Xtest.aff' ) 786let output = execute ( 'mkspell! Xtest.spl Xtest' ) 787call assert_match ( 'Expected MAP count in Xtest.aff line 1' , output ) 788 789" Duplicate character in a MAP item 790call writefile ([ 'MAP 2' , 'MAP xx' , 'MAP yy' ], 'Xtest.aff' ) 791let output = execute ( 'mkspell! Xtest.spl Xtest' ) 792call assert_match ( 'Duplicate character in MAP in Xtest.aff line 2' , output ) 793 794" Use COMPOUNDSYLMAX without SYLLABLE 795call writefile ([ 'COMPOUNDSYLMAX 2' ], 'Xtest.aff' ) 796let output = execute ( 'mkspell! Xtest.spl Xtest' ) 797call assert_match ( 'COMPOUNDSYLMAX used without SYLLABLE' , output ) 798 799" Missing SOFOTO 800call writefile ([ 'SOFOFROM abcdef' ], 'Xtest.aff' ) 801let output = execute ( 'mkspell! Xtest.spl Xtest' ) 802call assert_match ( 'Missing SOFOTO line in Xtest.aff' , output ) 803 804" Length of SOFOFROM and SOFOTO differ 805call writefile ([ 'SOFOFROM abcde' , 'SOFOTO ABCD' ], 'Xtest.aff' ) 806call assert_fails ( 'mkspell! Xtest.spl Xtest' , 'E759:' ) 807 808" Both SAL and SOFOFROM/SOFOTO items 809call writefile ([ 'SOFOFROM abcd' , 'SOFOTO ABCD' , 'SAL CIA X' ], 'Xtest.aff' ) 810let output = execute ( 'mkspell! Xtest.spl Xtest' ) 811call assert_match ( 'Both SAL and SOFO lines in Xtest.aff' , output ) 812 813" use an alphabet flag when FLAG is num 814call writefile ([ 'FLAG num' , 'SFX L Y 1' , 'SFX L 0 re [^x]' ], 'Xtest.aff' ) 815let output = execute ( 'mkspell! Xtest.spl Xtest' ) 816call assert_match ( 'Flag is not a number in Xtest.aff line 2: L' , output ) 817 818" use number and alphabet flag when FLAG is num 819call writefile ([ 'FLAG num' , 'SFX 4f Y 1' , 'SFX 4f 0 re [^x]' ], 'Xtest.aff' ) 820let output = execute ( 'mkspell! Xtest.spl Xtest' ) 821call assert_match ( 'Affix name too long in Xtest.aff line 2: 4f' , output ) 822 823" use a single character flag when FLAG is long 824call writefile ([ 'FLAG long' , 'SFX L Y 1' , 'SFX L 0 re [^x]' ], 'Xtest.aff' ) 825let output = execute ( 'mkspell! Xtest.spl Xtest' ) 826call assert_match ( 'Illegal flag in Xtest.aff line 2: L' , output ) 827 828" Nvim: non-utf8 encoding not supported 829" " missing character in UPP entry. The character table is used only in a 830" " non-utf8 encoding 831" call writefile(['FOL abc', 'LOW abc', 'UPP A'], 'Xtest.aff') 832" let save_encoding = &encoding 833" set encoding=cp949 834" call assert_fails('mkspell! Xtest.spl Xtest', 'E761:') 835" let &encoding = save_encoding 836" 837" " character range doesn't match between FOL and LOW entries 838" call writefile(["FOL \u0102bc", 'LOW abc', 'UPP ABC'], 'Xtest.aff') 839" let save_encoding = &encoding 840" set encoding=cp949 841" call assert_fails('mkspell! Xtest.spl Xtest', 'E762:') 842" let &encoding = save_encoding 843" 844" " character range doesn't match between FOL and UPP entries 845" call writefile(["FOL \u0102bc", "LOW \u0102bc", 'UPP ABC'], 'Xtest.aff') 846" let save_encoding = &encoding 847" set encoding=cp949 848" call assert_fails('mkspell! Xtest.spl Xtest', 'E762:') 849" let &encoding = save_encoding 850" 851" " additional characters in LOW and UPP entries 852" call writefile(["FOL ab", "LOW abc", 'UPP ABC'], 'Xtest.aff') 853" let save_encoding = &encoding 854" set encoding=cp949 855" call assert_fails('mkspell! Xtest.spl Xtest', 'E761:') 856" let &encoding = save_encoding 857" 858" " missing UPP entry 859" call writefile(["FOL abc", "LOW abc"], 'Xtest.aff') 860" let save_encoding = &encoding 861" set encoding=cp949 862" let output = execute('mkspell! Xtest.spl Xtest') 863" call assert_match('Missing FOL/LOW/UPP line in Xtest.aff', output) 864" let &encoding = save_encoding 865 866" duplicate word in the .dic file 867call writefile ([ '2' , 'good' , 'good' , 'good' ], 'Xtest.dic' ) 868call writefile ([ 'NAME vim' ], 'Xtest.aff' ) 869let output = execute ( 'mkspell! Xtest.spl Xtest' ) 870call assert_match ( 'First duplicate word in Xtest.dic line 3: good' , output ) 871call assert_match ( '2 duplicate word(s) in Xtest.dic' , output ) 872 873" use multiple .aff files with different values for COMPOUNDWORDMAX and 874" MIDWORD (number and string) 875call writefile ([ '1' , 'world' ], 'Xtest_US.dic' , 'D' ) 876call writefile ([ '1' , 'world' ], 'Xtest_CA.dic' , 'D' ) 877call writefile ([ "COMPOUNDWORDMAX 3" , "MIDWORD '-" ], 'Xtest_US.aff' , 'D' ) 878call writefile ([ "COMPOUNDWORDMAX 4" , "MIDWORD '=" ], 'Xtest_CA.aff' , 'D' ) 879let output = execute ( 'mkspell! Xtest.spl Xtest_US Xtest_CA' ) 880call assert_match ( 'COMPOUNDWORDMAX value differs from what is used in another .aff file' , output ) 881call assert_match ( 'MIDWORD value differs from what is used in another .aff file' , output ) 882 883call delete ( 'Xtest.spl' ) 884call delete ( 'Xtest.sug' ) 885endfunc 886 887func Test_spell_add_word () 888set spellfile = 889call assert_fails ( 'spellgood abc' , 'E764:' ) 890 891set spellfile = Xtest.utf-8.add 892call assert_fails ( '2spellgood abc' , 'E765:' ) 893 894edit Xtest.utf-8.add 895call setline ( 1 , 'sample' ) 896call assert_fails ( 'spellgood abc' , 'E139:' ) 897set spellfile & 898 %bw ! 899endfunc 900 901func Test_spell_add_long_word () 902set spell spellfile = ./Xspellfile.addspelllang = en 903 904let word = repeat ( 'a' , 9000 ) 905let v: errmsg = '' 906" Spell checking doesn't really work for such a long word, 907" but this should not cause an E1510 error. 908exe 'spellgood ' .. word 909call assert_equal ( '' , v: errmsg ) 910call assert_equal ([ word ], readfile ( './Xspellfile.add' )) 911 912set spell & spellfile = spelllang & encoding = utf-8 913call delete ( './Xspellfile.add' ) 914call delete ( './Xspellfile.add.spl' ) 915endfunc 916 917func Test_spellfile_verbose () 918call writefile ([ '1' , 'one' ], 'XtestVerbose.dic' , 'D' ) 919call writefile ([], 'XtestVerbose.aff' , 'D' ) 920mkspell ! XtestVerbose-utf8.spl XtestVerbose 921set spell 922 923" First time: the spl file should be read. 924let a = execute ( '3verbose set spelllang=XtestVerbose-utf8.spl' ) 925call assert_match ( 'Reading spell file "XtestVerbose-utf8.spl"' , a ) 926 927" Second time time: the spl file should not be read (already read). 928let a = execute ( '3verbose set spelllang=XtestVerbose-utf8.spl' ) 929call assert_notmatch ( 'Reading spell file "XtestVerbose-utf8.spl"' , a ) 930 931set spell & spelllang & 932call delete ( 'XtestVerbose-utf8.spl' ) 933endfunc 934 935" Test NOBREAK (see :help spell-NOBREAK) 936func Test_NOBREAK () 937call writefile ([ '3' , 'one' , 'two' , 'three' ], 'XtestNOBREAK.dic' , 'D' ) 938call writefile ([ 'NOBREAK' ], 'XtestNOBREAK.aff' , 'D' ) 939 940mkspell ! XtestNOBREAK-utf8.spl XtestNOBREAK 941set spell spelllang = XtestNOBREAK-utf8.spl 942 943call assert_equal ([ '' , '' ], spellbadword ( 'One two three onetwo onetwothree threetwoone' )) 944 945call assert_equal ([ 'x' , 'bad' ], spellbadword ( 'x' )) 946call assert_equal ([ 'y' , 'bad' ], spellbadword ( 'yone' )) 947call assert_equal ([ 'z' , 'bad' ], spellbadword ( 'onez' )) 948call assert_equal ([ 'zero' , 'bad' ], spellbadword ( 'Onetwozerothree' )) 949 950new 951call setline ( 1 , 'Onetwwothree' ) 952norm ! fw1z= 953call assert_equal ( 'Onetwothree' , getline ( 1 )) 954call setline ( 1 , 'Onetwothre' ) 955norm ! fh1z= 956call assert_equal ( 'Onetwothree' , getline ( 1 )) 957 958bw ! 959set spell & spelllang & 960call delete ( 'XtestNOBREAK-utf8.spl' ) 961endfunc 962 963" Test CHECKCOMPOUNDPATTERN (see :help spell-CHECKCOMPOUNDPATTERN) 964func Test_spellfile_CHECKCOMPOUNDPATTERN () 965call writefile ([ '4' , 966 \'one/c' , 967 \'two/c' , 968 \'three/c' , 969 \'four' ], 'XtestCHECKCOMPOUNDPATTERN.dic' , 'D' ) 970" Forbid compound words where first word ends with 'wo' and second starts with 'on'. 971call writefile ([ 'CHECKCOMPOUNDPATTERN 1' , 972 \'CHECKCOMPOUNDPATTERN wo on' , 973 \'COMPOUNDFLAG c' ], 'XtestCHECKCOMPOUNDPATTERN.aff' , 'D' ) 974 975mkspell ! XtestCHECKCOMPOUNDPATTERN-utf8.spl XtestCHECKCOMPOUNDPATTERN 976set spell spelllang = XtestCHECKCOMPOUNDPATTERN-utf8.spl 977 978" Check valid words with and without valid compounds. 979for goodword in [ 'one' , 'two' , 'three' , 'four' , 980 \'oneone' , 'onetwo' , 'onethree' , 981 \'twotwo' , 'twothree' , 982 \'threeone' , 'threetwo' , 'threethree' , 983 \'onetwothree' , 'onethreetwo' , 'twothreeone' , 'oneoneone' ] 984call assert_equal ([ '' , '' ], spellbadword ( goodword ), goodword ) 985endfor 986 987" Compounds 'twoone' or 'threetwoone' should be forbidden by CHECKCOMPOUNPATTERN. 988" 'four' does not have the 'c' flag in *.aff file so no compound. 989" 'five' is not in the *.dic file. 990for badword in [ 'five' , 'onetwox' , 991 \'twoone' , 'threetwoone' , 992 \'fourone' , 'onefour' ] 993call assert_equal ([ badword , 'bad' ], spellbadword ( badword )) 994endfor 995 996set spell & spelllang & 997call delete ( 'XtestCHECKCOMPOUNDPATTERN-utf8.spl' ) 998endfunc 999 1000" Test NOCOMPOUNDSUGS (see :help spell-NOCOMPOUNDSUGS) 1001func Test_spellfile_NOCOMPOUNDSUGS () 1002call writefile ([ '3' , 1003 \'one/c' , 1004 \'two/c' , 1005 \'three/c' ], 'XtestNOCOMPOUNDSUGS.dic' , 'D' ) 1006 1007" pass 0 tests without NOCOMPOUNDSUGS, pass 1 tests with NOCOMPOUNDSUGS 1008for pass in [ 0 , 1 ] 1009if pass == 0 1010call writefile ([ 'COMPOUNDFLAG c' ], 'XtestNOCOMPOUNDSUGS.aff' , 'D' ) 1011else 1012call writefile ([ 'NOCOMPOUNDSUGS' , 1013 \'COMPOUNDFLAG c' ], 'XtestNOCOMPOUNDSUGS.aff' , 'D' ) 1014endif 1015 1016mkspell ! XtestNOCOMPOUNDSUGS-utf8.spl XtestNOCOMPOUNDSUGS 1017set spell spelllang = XtestNOCOMPOUNDSUGS-utf8.spl 1018 1019for goodword in [ 'one' , 'two' , 'three' , 1020 \'oneone' , 'onetwo' , 'onethree' , 1021 \'twoone' , 'twotwo' , 'twothree' , 1022 \'threeone' , 'threetwo' , 'threethree' , 1023 \'onetwothree' , 'onethreetwo' , 'twothreeone' , 'oneoneone' ] 1024call assert_equal ([ '' , '' ], spellbadword ( goodword ), goodword ) 1025endfor 1026 1027for badword in [ 'four' , 'onetwox' , 'onexone' ] 1028call assert_equal ([ badword , 'bad' ], spellbadword ( badword )) 1029endfor 1030 1031if pass == 0 1032call assert_equal ([ 'one' , 'oneone' ], spellsuggest ( 'onne' , 2 )) 1033call assert_equal ([ 'onethree' , 'one three' ], spellsuggest ( 'onethre' , 2 )) 1034else 1035call assert_equal ([ 'one' , 'one one' ], spellsuggest ( 'onne' , 2 )) 1036call assert_equal ([ 'one three' ], spellsuggest ( 'onethre' , 2 )) 1037endif 1038endfor 1039 1040set spell & spelllang & 1041call delete ( 'XtestNOCOMPOUNDSUGS-utf8.spl' ) 1042endfunc 1043 1044" Test COMMON (better suggestions with common words, see :help spell-COMMON) 1045func Test_spellfile_COMMON () 1046call writefile ([ '7' , 1047 \'and' , 1048 \'ant' , 1049 \'end' , 1050 \'any' , 1051 \'tee' , 1052 \'the' , 1053 \'ted' ], 'XtestCOMMON.dic' , 'D' ) 1054call writefile ([ 'COMMON the and' ], 'XtestCOMMON.aff' , 'D' ) 1055 1056mkspell ! XtestCOMMON-utf8.spl XtestCOMMON 1057set spell spelllang = XtestCOMMON-utf8.spl 1058 1059" COMMON words 'and' and 'the' should be the top suggestions. 1060call assert_equal ([ 'and' , 'ant' ], spellsuggest ( 'anr' , 2 )) 1061call assert_equal ([ 'and' , 'end' ], spellsuggest ( 'ond' , 2 )) 1062call assert_equal ([ 'the' , 'ted' ], spellsuggest ( 'tha' , 2 )) 1063call assert_equal ([ 'the' , 'tee' ], spellsuggest ( 'dhe' , 2 )) 1064 1065set spell & spelllang & 1066call delete ( 'XtestCOMMON-utf8.spl' ) 1067endfunc 1068 1069" Test NOSUGGEST (see :help spell-COMMON) 1070func Test_spellfile_NOSUGGEST () 1071call writefile ([ '2' , 'foo/X' , 'fog' ], 'XtestNOSUGGEST.dic' , 'D' ) 1072call writefile ([ 'NOSUGGEST X' ], 'XtestNOSUGGEST.aff' , 'D' ) 1073 1074mkspell ! XtestNOSUGGEST-utf8.spl XtestNOSUGGEST 1075set spell spelllang = XtestNOSUGGEST-utf8.spl 1076 1077for goodword in [ 'foo' , 'Foo' , 'FOO' , 'fog' , 'Fog' , 'FOG' ] 1078call assert_equal ([ '' , '' ], spellbadword ( goodword ), goodword ) 1079endfor 1080for badword in [ 'foO' , 'fOO' , 'fooo' , 'foog' , 'foofog' , 'fogfoo' ] 1081call assert_equal ([ badword , 'bad' ], spellbadword ( badword )) 1082endfor 1083 1084call assert_equal ([ 'fog' ], spellsuggest ( 'fooo' , 1 )) 1085call assert_equal ([ 'fog' ], spellsuggest ( 'fOo' , 1 )) 1086call assert_equal ([ 'fog' ], spellsuggest ( 'foG' , 1 )) 1087call assert_equal ([ 'fog' ], spellsuggest ( 'fogg' , 1 )) 1088 1089set spell & spelllang & 1090call delete ( 'XtestNOSUGGEST-utf8.spl' ) 1091endfunc 1092 1093 1094" Test CIRCUMFIX (see: :help spell-CIRCUMFIX) 1095func Test_spellfile_CIRCUMFIX () 1096" Example taken verbatim from https://github.com/hunspell/hunspell/tree/master/tests 1097call writefile ([ '1' , 1098 \'nagy/C po:adj' ], 'XtestCIRCUMFIX.dic' , 'D' ) 1099call writefile ([ '# circumfixes: ~ obligate prefix/suffix combinations' , 1100 \'# superlative in Hungarian: leg- (prefix) AND -bb (suffix)' , 1101 \'' , 1102 \'CIRCUMFIX X' , 1103 \'' , 1104 \'PFX A Y 1' , 1105 \'PFX A 0 leg/X .' , 1106 \'' , 1107 \'PFX B Y 1' , 1108 \'PFX B 0 legesleg/X .' , 1109 \'' , 1110 \'SFX C Y 3' , 1111 \'SFX C 0 obb . is:COMPARATIVE' , 1112 \'SFX C 0 obb/AX . is:SUPERLATIVE' , 1113 \'SFX C 0 obb/BX . is:SUPERSUPERLATIVE' ], 'XtestCIRCUMFIX.aff' , 'D' ) 1114 1115mkspell ! XtestCIRCUMFIX-utf8.spl XtestCIRCUMFIX 1116set spell spelllang = XtestCIRCUMFIX-utf8.spl 1117 1118" From https://catalog.ldc.upenn.edu/docs/LDC2008T01/acta04.pdf: 1119" Hungarian English 1120" --------- ------- 1121" nagy great 1122" nagyobb greater 1123" legnagyobb greatest 1124" legeslegnagyob most greatest 1125call assert_equal ([ '' , '' ], spellbadword ( 'nagy nagyobb legnagyobb legeslegnagyobb' )) 1126 1127for badword in [ 'legnagy' , 'legeslegnagy' , 'legobb' , 'legeslegobb' ] 1128call assert_equal ([ badword , 'bad' ], spellbadword ( badword )) 1129endfor 1130 1131set spell & spelllang & 1132call delete ( 'XtestCIRCUMFIX-utf8.spl' ) 1133endfunc 1134 1135" Test SFX that strips/chops characters 1136func Test_spellfile_SFX_strip () 1137" Simplified conjugation of Italian verbs ending in -are (first conjugation). 1138call writefile ([ 'SFX A Y 4' , 1139 \'SFX A are iamo [^icg]are' , 1140 \'SFX A are hiamo [cg]are' , 1141 \'SFX A re mo iare' , 1142 \'SFX A re vamo are' ], 1143 \'XtestSFX.aff' , 'D' ) 1144" Examples of Italian verbs: 1145" - cantare = to sing 1146" - cercare = to search 1147" - odiare = to hate 1148call writefile ([ '3' , 'cantare/A' , 'cercare/A' , 'odiare/A' ], 'XtestSFX.dic' , 'D' ) 1149 1150mkspell ! XtestSFX-utf8.spl XtestSFX 1151set spell spelllang = XtestSFX-utf8.spl 1152 1153" To sing, we're singing, we were singing. 1154call assert_equal ([ '' , '' ], spellbadword ( 'cantare cantiamo cantavamo' )) 1155 1156" To search, we're searching, we were searching. 1157call assert_equal ([ '' , '' ], spellbadword ( 'cercare cerchiamo cercavamo' )) 1158 1159" To hate, we hate, we were hating. 1160call assert_equal ([ '' , '' ], spellbadword ( 'odiare odiamo odiavamo' )) 1161 1162for badword in [ 'canthiamo' , 'cerciamo' , 'cantarevamo' , 'odiiamo' ] 1163call assert_equal ([ badword , 'bad' ], spellbadword ( badword )) 1164endfor 1165 1166call assert_equal ([ 'cantiamo' ], spellsuggest ( 'canthiamo' , 1 )) 1167call assert_equal ([ 'cerchiamo' ], spellsuggest ( 'cerciamo' , 1 )) 1168call assert_equal ([ 'cantavamo' ], spellsuggest ( 'cantarevamo' , 1 )) 1169call assert_equal ([ 'odiamo' ], spellsuggest ( 'odiiamo' , 1 )) 1170 1171set spell & spelllang & 1172call delete ( 'XtestSFX-utf8.spl' ) 1173endfunc 1174 1175" When 'spellfile' is not set, adding a new good word will automatically set 1176" the 'spellfile' 1177func Test_init_spellfile () 1178throw 'Skipped: Nvim defaults spellfile to stdpath("data")/spell/' 1179let save_rtp = & rtp 1180let save_encoding = & encoding 1181call mkdir ( 'Xrtp/spell' , 'pR' ) 1182call writefile ([ 'vim' ], 'Xrtp/spell/Xtest.dic' ) 1183silent mkspell Xrtp/spell/Xtest.utf-8.spl Xrtp/spell/Xtest.dic 1184set runtimepath = ./Xrtp 1185set spelllang = Xtest 1186set spell 1187silent spellgood abc 1188call assert_equal ( './Xrtp/spell/Xtest.utf-8.add' , & spellfile ) 1189call assert_equal ([ 'abc' ], readfile ( 'Xrtp/spell/Xtest.utf-8.add' )) 1190call assert_true ( filereadable ( 'Xrtp/spell/Xtest.utf-8.spl' )) 1191 1192set spell & spelllang & spellfile & 1193let & encoding = save_encoding 1194let & rtp = save_rtp 1195 %bw ! 1196endfunc 1197 1198" Test for the 'mkspellmem' option 1199func Test_mkspellmem_opt () 1200call assert_fails ( 'set mkspellmem=1000' , 'E474:' ) 1201call assert_fails ( 'set mkspellmem=1000,' , 'E474:' ) 1202call assert_fails ( 'set mkspellmem=1000,50' , 'E474:' ) 1203call assert_fails ( 'set mkspellmem=1000,50,' , 'E474:' ) 1204call assert_fails ( 'set mkspellmem=1000,50,10,' , 'E474:' ) 1205call assert_fails ( 'set mkspellmem=1000,50,0' , 'E474:' ) 1206endfunc 1207 1208" 'spellfile' accepts '@' on top of 'isfname'. 1209func Test_spellfile_allow_at_character () 1210call mkdir ( 'Xtest/the foo@bar,dir' , 'p' ) 1211let & spellfile = './Xtest/the foo@bar\,dir/Xspellfile.add' 1212let & spellfile = '' 1213call delete ( 'Xtest' , 'rf' ) 1214endfunc 1215 1216" this was using a NULL pointer 1217func Test_mkspell_empty_dic () 1218call writefile ([ '1' ], 'XtestEmpty.dic' , 'D' ) 1219call writefile ([ 'SOFOFROM abcd' , 'SOFOTO ABCD' , 'SAL CIA X' ], 'XtestEmpty.aff' , 'D' ) 1220mkspell ! XtestEmpty.spl XtestEmpty 1221 1222call delete ( 'XtestEmpty.spl' ) 1223endfunc 1224 1225" This used to cause a buffer overflow 1226func Test_mkspell_no_buffer_overflow () 1227CheckNotMSWindows 1228 1229let aff_lines = [ 'SET ISO8859-1' , 'SFX A Y 1' , 1230 \'SFX A 0 s ' .. repeat ( nr2char ( 0xff ), 491 )] 1231call writefile ( aff_lines , 'Xbof.aff' , 'D' ) 1232call writefile ([ '1' , 'word/A' ], 'Xbof.dic' , 'D' ) 1233" Must not crash; ignore any conversion/regex errors. 1234try 1235mkspell ! Xbof.spl Xbof 1236catch 1237endtry 1238defer delete('Xbof.spl') 1239 1240let long = repeat ( nr2char ( 0xff ), 200 ) 1241let aff2_lines = [ 'SET ISO8859-1' , 'SFX A Y 1' , 1242 \'SFX A 0 ' .. long .. ' .' ] 1243call writefile ( aff2_lines , 'Xbof2.aff' , 'D' ) 1244call writefile ([ '1' , long .. '/A' ], 'Xbof2.dic' , 'D' ) 1245try 1246mkspell ! Xbof2.spl Xbof2 1247catch 1248endtry 1249defer delete('Xbof2.spl') 1250endfunc 1251 1252func Test_mkspell_no_affixlist_overflow () 1253let aff_lines = [ 1254 \'SET ISO8859-1' , 1255 \'PFXPOSTPONE' , 1256 \'PFX A Y 1' , 1257 \'PFX A 0 pre .' , 1258 \] 1259call writefile ( aff_lines , 'Xaffbof.aff' , 'D' ) 1260call writefile ([ '1' , 'word/' .. repeat ( 'A' , 300 )], 'Xaffbof.dic' , 'D' ) 1261 1262call assert_fails ( 'mkspell! Xaffbof.spl Xaffbof' , 1263 \'Too many postponed prefixes and/or compound flags' ) 1264call assert_false ( filereadable ( 'Xaffbof.spl' )) 1265endfunc 1266 1267func Test_mkspell_no_compflag_overflow () 1268" Overflow the compound-flag path in get_compflags(): a word whose 1269" affix list repeats a compound flag many times accumulates one ID per 1270" occurrence, overrunning store_afflist[MAXWLEN]. 1271let aff_lines = [ 1272 \'SET ISO8859-1' , 1273 \'COMPOUNDFLAG c' , 1274 \] 1275call writefile ( aff_lines , 'Xcompbof.aff' , 'D' ) 1276 1277" Repeat the compound flag 'c' far past MAXWLEN. 1278call writefile ([ '1' , 'word/' .. repeat ( 'c' , 300 )], 'Xcompbof.dic' , 'D' ) 1279 1280call assert_fails ( 'mkspell! Xcompbof.spl Xcompbof' , 1281 \'Too many postponed prefixes and/or compound flags' ) 1282call assert_false ( filereadable ( 'Xcompbof.spl' )) 1283endfunc 1284 1285func Test_spell_sug_tree_count_words_overflow () 1286" A crafted .spl/.sug pair with a BY_INDEX self-cycle in the fold word tree 1287" parses cleanly (shared refs aren't recursed, so read_tree_node()'s depth 1288" cap never trips), but drove tree_count_words() past its MAXWLEN-sized depth 1289" arrays -> stack out-of-bounds write. The walk only happens when 1290" spellsuggest() loads the matching .sug. Reaching the assert == no OOB. 1291call mkdir ( 'Xrtp/spell' , 'pR' ) 1292" VIMspell + v50, SN_SUGFILE(ts), SN_END, LWORDTREE{node:1,BY_INDEX->0,'A'}, 1293" empty KWORDTREE/PREFIXTREE 1294let spl = eval ( '0z56494D7370656C6C320B0000000008000000001234' 1295 \.. '5678FF000000020101000000410000000000000000' ) 1296" VIMsug + v1, matching ts, SUGWORDTREE word "a", empty SUGTABLE 1297let sug = 0 z56494D737567010000000012345678000000040161010000000000 1298call writefile ( spl , 'Xrtp/spell/xx.utf-8.spl' , 'b' ) 1299call writefile ( sug , 'Xrtp/spell/xx.utf-8.sug' , 'b' ) 1300 1301new 1302set runtimepath += ./Xrtp 1303set spelllang = xx 1304set spell 1305" Unpatched: OOB write here (ASan abort, or crash). Patched: returns a list. 1306call assert_equal ( v: t_list , type ( spellsuggest ( 'helloo' ))) 1307 1308set spell & spelllang & runtimepath & 1309bwipe ! 1310endfunc 1311 1312" A word longer than MAXWLEN must not overflow the soundfold result buffer in 1313" the single-byte SOFO branch of spell_soundfold_sofo(). 1314func Test_soundfold_overflow () 1315throw 'Skipped: Nvim does not support setting encoding=latin1' 1316let _enc =& enc 1317set enc = latin1 1318call writefile ([ 'SOFOFROM ab' , 'SOFOTO xy' ], 'Xtest.aff' , 'D' ) 1319call writefile ([ '1' , 'foo' ], 'Xtest.dic' , 'D' ) 1320mkspell ! Xtest Xtest 1321defer delete('Xtest.latin1.spl') 1322defer delete('Xtest.latin1.sug') 1323setl spelllang = Xtest.latin1.splspell 1324 1325" Before the fix the copy loop wrote one byte per input byte into a 1326" MAXWLEN (254) stack buffer with no upper bound, smashing the stack. 1327let sound = soundfold ( repeat ( 'ab' , 300 )) 1328call assert_true ( strlen ( sound ) < 254 , 'soundfold result exceeds MAXWLEN' ) 1329 1330set spell & spelllang & 1331let & enc = _enc 1332endfunc 1333 1334func Test_spell_sal_sofo_truncated () 1335call mkdir ( 'Xspelldir/spell' , 'pR' ) 1336 1337" "VIMspell" <ver=0x32> 1338" SN_SAL(5) flags=0 len=7 : <salflags=0><salcount=0,1><a><0><1>a<1>a 1339" SN_SOFO(6) flags=0 len=0 : truncated, no body -> EOF in reader 1340" (28 bytes total) 1341let bytes = 0 z56494d7370656c6c.3205000000000700.000101610161060000.000000 1342call writefile ( bytes , 'Xspelldir/spell/Xx.utf-8.spl' , 'b' ) 1343 1344let save_rtp = & rtp 1345set rtp = ./Xspelldir 1346try 1347set spelllang = Xx 1348silent ! set spell 1349catch 1350" an error message is fine; a crash is not 1351endtry 1352 1353" Reaching this point means Vim did not crash on the crafted file. 1354call assert_true ( v: true ) 1355 1356set nospell 1357set spelllang & 1358let & rtp = save_rtp 1359endfunc 1360 1361" A <prefcondnr> in the prefix tree that is truncated reads as -1, which used to 1362" be stored as the condition index and then used to index sl_prefprog[]. 1363func Test_spellfile_truncated_prefcondnr () 1364" PFXPOSTPONE puts the prefixes in the prefix tree, the conditions are what 1365" make a <prefcondnr> be written for each of them. 1366call writefile ([ 'SET UTF-8' , 'PFXPOSTPONE' , 1367 \'PFX A Y 1' , 'PFX A 0 un [a-z]' , 1368 \'PFX B Y 1' , 'PFX B 0 re [b-d]' , 1369 \'PFX C Y 1' , 'PFX C 0 de [c-f]' ], 'Xpfx.aff' , 'D' ) 1370call writefile ([ '3' , 'apple/A' , 'bread/B' , 'cat/C' ], 'Xpfx.dic' , 'D' ) 1371mkspell ! Xpfx.spl Xpfx 1372defer delete('Xpfx.spl') 1373 1374let bytes = readfile ( 'Xpfx.spl' , 'B' ) 1375 1376" Truncating anywhere in the prefix tree must give an error, not store a 1377" negative condition index. Every truncation needs its own file name, a 1378" spell file that has been read once is cached by its name. 1379for n in range ( len ( bytes ) - 40 , len ( bytes ) - 1 ) 1380let fname = 'Xtrunc' .. n .. '.spl' 1381call writefile ( bytes [ 0 : n - 1 ], fname ) 1382try 1383execute 'set spl=' .. fname 1384set spell 1385catch /E75[789]:/ 1386endtry 1387" Use the prefix tree, in case a bad index was stored. 1388call spellbadword ( 'unapple' ) 1389call spellbadword ( 'rebread' ) 1390set nospell spl & 1391call delete ( fname ) 1392endfor 1393 1394" The valid file still works, prefixed words are recognized. 1395set spl = Xpfx.splspell 1396call assert_equal ([ '' , '' ], spellbadword ( 'unapple' )) 1397call assert_equal ([ '' , '' ], spellbadword ( 'rebread' )) 1398 1399set nospell spl & spelllang & 1400endfunc 1401 1402" vim: shiftwidth=2 sts=2 expandtab