robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
47.8 KiB1402 linesraw
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()
10  new
11  call append(0, ['1 good', '2 goood', '3 goood'])
12  set spell spellfile=./Xspellfile.add spelllang=en
13  let oldlang=v:lang
14  lang C
15
16  " Test for zg
17  1
18  norm! ]s
19  call assert_equal('2 goood', getline('.'))
20  norm! zg
21  1
22  let a=execute('unsilent :norm! ]s')
23  call assert_equal('1 good', getline('.'))
24  call assert_equal('search hit BOTTOM, continuing at TOP', a[1:])
25  let cnt=readfile('./Xspellfile.add')
26  call assert_equal('goood', cnt[0])
27
28  " zg should fail in operator-pending mode
29  call assert_beeps('norm! czg')
30
31  " zg fails in visual mode when not able to get the visual text
32  call assert_beeps('norm! ggVjzg')
33  norm! V
34
35  " zg fails for a non-identifier word
36  call append(line('$'), '###')
37  call assert_fails('norm! Gzg', 'E349:')
38  $d
39
40  " Test for zw
41  2
42  norm! $zw
43  1
44  norm! ]s
45  call assert_equal('2 goood', getline('.'))
46  let cnt=readfile('./Xspellfile.add')
47  call assert_equal('#oood', cnt[0])
48  call assert_equal('goood/!', cnt[1])
49
50  " Test for :spellrare
51  spellrare rare
52  let cnt=readfile('./Xspellfile.add')
53  call assert_equal(['#oood', 'goood/!', 'rare/?'], cnt)
54
55  " Make sure :spellundo works for rare words.
56  spellundo rare
57  let cnt=readfile('./Xspellfile.add')
58  call assert_equal(['#oood', 'goood/!', '#are/?'], cnt)
59
60  " Test for zg in visual mode
61  let a=execute('unsilent :norm! V$zg')
62  call assert_equal("Word '2 goood' added to ./Xspellfile.add", a[1:])
63  1
64  norm! ]s
65  call assert_equal('3 goood', getline('.'))
66  let cnt=readfile('./Xspellfile.add')
67  call assert_equal('2 goood', cnt[3])
68  " Remove "2 good" from spellfile
69  2
70  let a=execute('unsilent norm! V$zw')
71  call assert_equal("Word '2 goood' added to ./Xspellfile.add", a[1:])
72  let cnt=readfile('./Xspellfile.add')
73  call assert_equal('2 goood/!', cnt[4])
74
75  " Test for zG
76  let a=execute('unsilent norm! V$zG')
77  call assert_match("Word '2 goood' added to .*", a)
78  let fname=matchstr(a, 'to\s\+\zs\f\+$')
79  let cnt=readfile(fname)
80  call assert_equal('2 goood', cnt[0])
81
82  " Test for zW
83  let a=execute('unsilent norm! V$zW')
84  call assert_match("Word '2 goood' added to .*", a)
85  let cnt=readfile(fname)
86  call assert_equal('# goood', cnt[0])
87  call assert_equal('2 goood/!', cnt[1])
88
89  " Test for zuW
90  let a=execute('unsilent norm! V$zuW')
91  call assert_match("Word '2 goood' removed from .*", a)
92  let cnt=readfile(fname)
93  call assert_equal('# goood', cnt[0])
94  call assert_equal('# goood/!', cnt[1])
95
96  " Test for zuG
97  let a=execute('unsilent norm! $zG')
98  call assert_match("Word 'goood' added to .*", a)
99  let cnt=readfile(fname)
100  call assert_equal('# goood', cnt[0])
101  call assert_equal('# goood/!', cnt[1])
102  call assert_equal('goood', cnt[2])
103  let a=execute('unsilent norm! $zuG')
104  let cnt=readfile(fname)
105  call assert_match("Word 'goood' removed from .*", a)
106  call assert_equal('# goood', cnt[0])
107  call assert_equal('# goood/!', cnt[1])
108  call assert_equal('#oood', cnt[2])
109  " word not found in wordlist
110  let a=execute('unsilent norm! V$zuG')
111  let cnt=readfile(fname)
112  call assert_match("", a)
113  call assert_equal('# goood', cnt[0])
114  call assert_equal('# goood/!', cnt[1])
115  call assert_equal('#oood', cnt[2])
116
117  " Test for zug
118  call delete('./Xspellfile.add')
119  2
120  let a=execute('unsilent norm! $zg')
121  let cnt=readfile('./Xspellfile.add')
122  call assert_equal('goood', cnt[0])
123  let a=execute('unsilent norm! $zug')
124  call assert_match("Word 'goood' removed from \./Xspellfile.add", a)
125  let cnt=readfile('./Xspellfile.add')
126  call assert_equal('#oood', cnt[0])
127  " word not in wordlist
128  let a=execute('unsilent norm! V$zug')
129  call assert_match('', a)
130  let cnt=readfile('./Xspellfile.add')
131  call assert_equal('#oood', cnt[0])
132
133  " Test for zuw
134  call delete('./Xspellfile.add')
135  2
136  let a=execute('unsilent norm! Vzw')
137  let cnt=readfile('./Xspellfile.add')
138  call assert_equal('2 goood/!', cnt[0])
139  let a=execute('unsilent norm! Vzuw')
140  call assert_match("Word '2 goood' removed from \./Xspellfile.add", a)
141  let cnt=readfile('./Xspellfile.add')
142  call assert_equal('# goood/!', cnt[0])
143  " word not in wordlist
144  let a=execute('unsilent norm! $zug')
145  call assert_match('', a)
146  let cnt=readfile('./Xspellfile.add')
147  call assert_equal('# goood/!', cnt[0])
148
149  " add second entry to spellfile setting
150  set spellfile=./Xspellfile.add,./Xspellfile2.add
151  call delete('./Xspellfile.add')
152  2
153  let a=execute('unsilent norm! $2zg')
154  let cnt=readfile('./Xspellfile2.add')
155  call assert_match("Word 'goood' added to ./Xspellfile2.add", a)
156  call assert_equal('goood', cnt[0])
157
158  " Test for :spellgood!
159  let temp = execute(':spe!0/0')
160  call assert_match('Invalid region', temp)
161  let spellfile = matchstr(temp, 'Invalid region nr in \zs.*\ze line \d: 0')
162  call assert_equal(['# goood', '# goood/!', '#oood', '0/0'], readfile(spellfile))
163
164  " Test for :spellrare!
165  :spellrare! raare
166  call assert_equal(['# goood', '# goood/!', '#oood', '0/0', 'raare/?'], readfile(spellfile))
167  call delete(spellfile)
168
169  " clean up
170  exe "lang" oldlang
171  call delete("./Xspellfile.add")
172  call delete("./Xspellfile2.add")
173  call delete("./Xspellfile.add.spl")
174  call delete("./Xspellfile2.add.spl")
175
176  " zux -> no-op
177  2
178  norm! $zux
179  call assert_equal([], glob('Xspellfile.add',0,1))
180  call assert_equal([], glob('Xspellfile2.add',0,1))
181
182  set spellfile= spell& spelllang&
183  bw!
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)
190  let splfile = './Xtest/spell/Xtest.utf-8.spl'
191  " Add the spell file header and version (VIMspell2)
192  let v = 0z56494D7370656C6C32 + a:content
193  call writefile(v, splfile, 'b')
194
195  " 'encoding' is set before each test to clear the previously loaded suggest
196  " file from memory.
197  set encoding=utf-8
198
199  set runtimepath=./Xtest
200  set spelllang=Xtest
201  if a:emsg != ''
202    call assert_fails('set spell', a:emsg)
203  else
204    " FIXME: With some invalid spellfile contents, there are no error
205    " messages. So don't know how to check for the test result.
206    set spell
207  endif
208  set 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()
214  let save_rtp = &rtp
215  call mkdir('Xtest/spell', 'pR')
216  let splfile = './Xtest/spell/Xtest.utf-8.spl'
217
218  " empty spell file
219  call writefile([], splfile)
220  set runtimepath=./Xtest
221  set spelllang=Xtest
222  call assert_fails('set spell', 'E757:')
223  set nospell spelllang&
224
225  " invalid file ID
226  call writefile(0z56494D, splfile, 'b')
227  set runtimepath=./Xtest
228  set spelllang=Xtest
229  call assert_fails('set spell', 'E757:')
230  set nospell spelllang&
231
232  " missing version number
233  call writefile(0z56494D7370656C6C, splfile, 'b')
234  set runtimepath=./Xtest
235  set spelllang=Xtest
236  call assert_fails('set spell', 'E771:')
237  set nospell spelllang&
238
239  " invalid version number
240  call writefile(0z56494D7370656C6C7A, splfile, 'b')
241  set runtimepath=./Xtest
242  set spelllang=Xtest
243  call assert_fails('set spell', 'E772:')
244  set nospell spelllang&
245
246  " no sections
247  call Spellfile_Test(0z, 'E758:')
248
249  " missing section length
250  call Spellfile_Test(0z00, 'E758:')
251
252  " unsupported required section
253  call Spellfile_Test(0z7A0100000004, 'E770:')
254
255  " unsupported not-required section
256  call Spellfile_Test(0z7A0000000004, 'E758:')
257
258  " SN_REGION: invalid number of region names
259  call Spellfile_Test(0z0000000000FF, 'E759:')
260
261  " SN_CHARFLAGS: missing <charflagslen> length
262  call Spellfile_Test(0z010000000004, 'E758:')
263
264  " SN_CHARFLAGS: invalid <charflagslen> length
265  call Spellfile_Test(0z0100000000010201, '')
266
267  " SN_CHARFLAGS: charflagslen == 0 and folcharslen != 0
268  call Spellfile_Test(0z01000000000400000101, 'E759:')
269
270  " SN_CHARFLAGS: missing <folcharslen> length
271  call Spellfile_Test(0z01000000000100, 'E758:')
272
273  " SN_PREFCOND: invalid prefcondcnt
274  call Spellfile_Test(0z03000000000100, 'E759:')
275
276  " SN_PREFCOND: invalid condlen
277  call Spellfile_Test(0z0300000000020001, 'E759:')
278
279  " SN_REP: invalid repcount
280  call Spellfile_Test(0z04000000000100, 'E758:')
281
282  " SN_REP: missing rep
283  call Spellfile_Test(0z0400000000020004, 'E758:')
284
285  " SN_REP: zero repfromlen
286  call Spellfile_Test(0z040000000003000100, 'E759:')
287
288  " SN_REP: invalid reptolen
289  call Spellfile_Test(0z0400000000050001014101, '')
290
291  " SN_REP: zero reptolen
292  call Spellfile_Test(0z0400000000050001014100, 'E759:')
293
294  " SN_SAL: missing salcount
295  call Spellfile_Test(0z05000000000102, 'E758:')
296
297  " SN_SAL: missing salfromlen
298  call Spellfile_Test(0z050000000003080001, 'E758:')
299
300  " SN_SAL: missing saltolen
301  call Spellfile_Test(0z0500000000050400010161, 'E758:')
302
303  " SN_WORDS: non-NUL terminated word
304  call Spellfile_Test(0z0D000000000376696D, 'E758:')
305
306  " SN_WORDS: very long word
307  let v = eval('0z0D000000012C' .. repeat('41', 300))
308  call Spellfile_Test(v, 'E759:')
309
310  " SN_SOFO: missing sofofromlen
311  call Spellfile_Test(0z06000000000100, 'E758:')
312
313  " SN_SOFO: missing sofotolen
314  call Spellfile_Test(0z06000000000400016100, 'E758:')
315
316  " SN_SOFO: missing sofoto
317  call Spellfile_Test(0z0600000000050001610000, 'E759:')
318
319  " SN_SOFO: empty sofofrom and sofoto
320  call Spellfile_Test(0z06000000000400000000FF000000000000000000000000, '')
321
322  " SN_SOFO: multi-byte characters in sofofrom and sofoto
323  call Spellfile_Test(0z0600000000080002CF810002CF82FF000000000000000000000000, '')
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.
328  call Spellfile_Test(0z05000000000300000006000000000A0004CAABCEAB00024142FF000000000000000000000000, '')
329
330  " SN_COMPOUND: compmax is less than 2
331  call Spellfile_Test(0z08000000000101, 'E759:')
332
333  " SN_COMPOUND: missing compsylmax and other options
334  call Spellfile_Test(0z0800000000020401, 'E759:')
335
336  " SN_COMPOUND: missing compoptions
337  call Spellfile_Test(0z080000000005040101, 'E758:')
338
339  " SN_COMPOUND: missing comppattern
340  call Spellfile_Test(0z08000000000704010100000001, 'E758:')
341
342  " SN_COMPOUND: incorrect comppatlen
343  call Spellfile_Test(0z080000000007040101000000020165, 'E758:')
344
345  " SN_COMPOUND: oversized sectionlen
346  let v = eval('0z08004000000803010161' .. repeat('61', 50) .. 'FF')
347  call Spellfile_Test(v, 'E759:')
348
349  " SN_INFO: missing info
350  call Spellfile_Test(0z0F0000000005040101, '')
351
352  " SN_MIDWORD: missing midword
353  call Spellfile_Test(0z0200000000040102, '')
354
355  " SN_MAP: missing midword
356  call Spellfile_Test(0z0700000000040102, '')
357
358  " SN_MAP: empty map string
359  call Spellfile_Test(0z070000000000FF000000000000000000000000, '')
360
361  " SN_MAP: duplicate multibyte character
362  call Spellfile_Test(0z070000000004DC81DC81, 'E783:')
363
364  " SN_SYLLABLE: missing SYLLABLE item
365  call Spellfile_Test(0z0900000000040102, '')
366
367  " SN_SYLLABLE: More than SY_MAXLEN size
368  let v = eval('0z090000000022612F' .. repeat('62', 32))
369  call Spellfile_Test(v, '')
370
371  " LWORDTREE: missing
372  call Spellfile_Test(0zFF, 'E758:')
373
374  " LWORDTREE: missing tree node
375  call Spellfile_Test(0zFF00000004, 'E758:')
376
377  " LWORDTREE: missing tree node value
378  call Spellfile_Test(0zFF0000000402, 'E758:')
379
380  " LWORDTREE: incorrect sibling node count
381  call Spellfile_Test(0zFF00000001040000000000000000, '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.
387  call Spellfile_Test(0zFF0000000A02780100000979010000000000000000000000, '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).
396  let v = eval('0zFF00000200' .. repeat('0161', 255)
397               \ .. '0100' ..  repeat('00', 8))
398
399  call Spellfile_Test(v, 'E759:')
400
401  " KWORDTREE: missing tree node
402  call Spellfile_Test(0zFF0000000000000004, 'E758:')
403
404  " PREFIXTREE: missing tree node
405  call Spellfile_Test(0zFF000000000000000000000004, 'E758:')
406
407  " PREFIXTREE: incorrect prefcondnr
408  call Spellfile_Test(0zFF000000000000000000000002010200000020, 'E759:')
409
410  " PREFIXTREE: invalid nodeidx
411  call Spellfile_Test(0zFF00000000000000000000000201010000, 'E759:')
412
413  let &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()
419  throw 'Skipped: Nvim does not support enc=latin1'
420  let save_enc = &encoding
421  set encoding=latin1
422  " A SAL map that appends without collapsing, so the result is not shorter
423  " than the input.
424  call writefile(['SET ISO8859-1', 'SAL collapse_result false',
425	\ 'SAL a aaaa', 'SAL b bbbb'], 'Xsal.aff')
426  call writefile(['2', 'hello', 'world'], 'Xsal.dic')
427  mkspell! Xsal Xsal
428  set spl=Xsal.latin1.spl spell
429
430  " 253 input characters hit the buffer boundary; the result must not exceed
431  " MAXWLEN - 1.
432  call assert_true(strlen(soundfold(repeat('a', 253))) <= 253)
433
434  set nospell spl& spelllang&
435  call delete('Xsal.aff')
436  call delete('Xsal.dic')
437  call delete('Xsal.latin1.spl')
438  let &encoding = save_enc
439endfunc
440
441" Test for format errors in suggest file
442func Test_sugfile_format_error()
443  let save_rtp = &rtp
444  call mkdir('Xtest/spell', 'pR')
445  let splfile = './Xtest/spell/Xtest.utf-8.spl'
446  let sugfile = './Xtest/spell/Xtest.utf-8.sug'
447
448  " create an empty spell file with a suggest timestamp
449  call writefile(0z56494D7370656C6C320B00000000080000000000000044FF000000000000000000000000, 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
455  set encoding=utf-8
456  call writefile([], sugfile)
457  set runtimepath=./Xtest
458  set spelllang=Xtest
459  set spell
460  call assert_fails("let s = spellsuggest('abc')", 'E778:')
461  set nospell spelllang&
462
463  " zero suggest version
464  set encoding=utf-8
465  call writefile(0z56494D73756700, sugfile)
466  set runtimepath=./Xtest
467  set spelllang=Xtest
468  set spell
469  call assert_fails("let s = spellsuggest('abc')", 'E779:')
470  set nospell spelllang&
471
472  " unsupported suggest version
473  set encoding=utf-8
474  call writefile(0z56494D7375671F, sugfile)
475  set runtimepath=./Xtest
476  set spelllang=Xtest
477  set spell
478  call assert_fails("let s = spellsuggest('abc')", 'E780:')
479  set nospell spelllang&
480
481  " missing suggest timestamp
482  set encoding=utf-8
483  call writefile(0z56494D73756701, sugfile)
484  set runtimepath=./Xtest
485  set spelllang=Xtest
486  set spell
487  call assert_fails("let s = spellsuggest('abc')", 'E781:')
488  set nospell spelllang&
489
490  " incorrect suggest timestamp
491  set encoding=utf-8
492  call writefile(0z56494D7375670100000000000000FF, sugfile)
493  set runtimepath=./Xtest
494  set spelllang=Xtest
495  set spell
496  call assert_fails("let s = spellsuggest('abc')", 'E781:')
497  set nospell spelllang&
498
499  " missing suggest wordtree
500  set encoding=utf-8
501  call writefile(0z56494D737567010000000000000044, sugfile)
502  set runtimepath=./Xtest
503  set spelllang=Xtest
504  set spell
505  call assert_fails("let s = spellsuggest('abc')", 'E782:')
506  set nospell spelllang&
507
508  " invalid suggest word count in SUGTABLE
509  set encoding=utf-8
510  call writefile(0z56494D7375670100000000000000440000000022, sugfile)
511  set runtimepath=./Xtest
512  set spelllang=Xtest
513  set spell
514  call assert_fails("let s = spellsuggest('abc')", 'E782:')
515  set nospell spelllang&
516
517  " missing sugline in SUGTABLE
518  set encoding=utf-8
519  call writefile(0z56494D7375670100000000000000440000000000000005, sugfile)
520  set runtimepath=./Xtest
521  set spelllang=Xtest
522  set spell
523  call assert_fails("let s = spellsuggest('abc')", 'E782:')
524  set nospell spelllang&
525
526  let &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
532  let lines =<< trim [END]
533    # This is an example word list
534
535    /encoding=latin1
536    /encoding=latin1
537    example
538  [END]
539  call writefile(lines, 'Xwordlist.dic', 'D')
540  let output = execute('mkspell Xwordlist.spl Xwordlist.dic')
541  call assert_match('Duplicate /encoding= line ignored in Xwordlist.dic line 4: /encoding=latin1', output)
542
543  " multiple encoding for a word
544  let lines =<< trim [END]
545    example
546    /encoding=latin1
547    example
548  [END]
549  call writefile(lines, 'Xwordlist.dic')
550  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
551  call assert_match('/encoding= line after word ignored in Xwordlist.dic line 2: /encoding=latin1', output)
552
553  " unsupported encoding for a word
554  let lines =<< trim [END]
555    /encoding=Xtest
556    example
557  [END]
558  call writefile(lines, 'Xwordlist.dic')
559  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
560  call assert_match('Conversion in Xwordlist.dic not supported: from Xtest to utf-8', output)
561
562  " duplicate region
563  let lines =<< trim [END]
564    /regions=usca
565    /regions=usca
566    example
567  [END]
568  call writefile(lines, 'Xwordlist.dic')
569  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
570  call assert_match('Duplicate /regions= line ignored in Xwordlist.dic line 2: regions=usca', output)
571
572  " maximum regions
573  let lines =<< trim [END]
574    /regions=uscauscauscauscausca
575    example
576  [END]
577  call writefile(lines, 'Xwordlist.dic')
578  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
579  call assert_match('Too many regions in Xwordlist.dic line 1: uscauscauscauscausca', output)
580
581  " unsupported '/' value
582  let lines =<< trim [END]
583    /test=abc
584    example
585  [END]
586  call writefile(lines, 'Xwordlist.dic')
587  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
588  call assert_match('/ line ignored in Xwordlist.dic line 1: /test=abc', output)
589
590  " unsupported flag
591  let lines =<< trim [END]
592    example/+
593  [END]
594  call writefile(lines, 'Xwordlist.dic')
595  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
596  call assert_match('Unrecognized flags in Xwordlist.dic line 1: +', output)
597
598  " non-ascii word
599  call writefile(["ʀʀ"], 'Xwordlist.dic')
600  let output = execute('mkspell! -ascii Xwordlist.spl Xwordlist.dic')
601  call assert_match('Ignored 1 words with non-ASCII characters', output)
602
603  " keep case of a word
604  let lines =<< trim [END]
605    example/=
606  [END]
607  call writefile(lines, 'Xwordlist.dic')
608  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
609  call assert_match('Compressed keep-case:', output)
610
611  call delete('Xwordlist.spl')
612endfunc
613
614" Test for the :mkspell command
615func Test_mkspell()
616  call assert_fails('mkspell Xtest_us.spl', 'E751:')
617  call assert_fails('mkspell Xtest.spl abc', 'E484:')
618  call 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
621  call writefile([], 'Xtest.aff')
622  call assert_fails('mkspell Xtest.spl Xtest', 'E484:')
623  call delete('Xtest.aff')
624
625  call writefile([], 'Xtest.spl')
626  call writefile([], 'Xtest.dic')
627  call assert_fails('mkspell Xtest.spl Xtest.dic', 'E13:')
628  call delete('Xtest.spl')
629  call delete('Xtest.dic')
630
631  call mkdir('Xtest.spl')
632  call assert_fails('mkspell! Xtest.spl Xtest.dic', 'E17:')
633  call delete('Xtest.spl', 'rf')
634
635  " can't write the .spl file as its directory does not exist
636  call writefile([], 'Xtest.aff')
637  call writefile([], 'Xtest.dic')
638  call assert_fails('mkspell DOES_NOT_EXIT/Xtest.spl Xtest.dic', 'E484:')
639  call delete('Xtest.aff')
640  call delete('Xtest.dic')
641
642  call 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.
649  CheckNotMSWindows
650
651  " No word count in .dic file
652  call writefile([], 'Xtest.dic', 'D')
653  call writefile([], 'Xtest.aff', 'D')
654  call assert_fails('mkspell! Xtest.spl Xtest', 'E760:')
655
656  " create a .dic file for the tests below
657  call writefile(['1', 'work'], 'Xtest.dic')
658
659  " Invalid encoding in .aff file
660  call writefile(['# comment', 'SET Xinvalidencoding'], 'Xtest.aff')
661  let output = execute('mkspell! Xtest.spl Xtest')
662  call assert_match('Conversion in Xtest.aff not supported: from xinvalidencoding', output)
663
664  " Invalid flag in .aff file
665  call writefile(['FLAG xxx'], 'Xtest.aff')
666  let output = execute('mkspell! Xtest.spl Xtest')
667  call assert_match('Invalid value for FLAG in Xtest.aff line 1: xxx', output)
668
669  " set FLAGS after using flag for an affix
670  call writefile(['SFX L Y 1', 'SFX L 0 re [^x]', 'FLAG long'], 'Xtest.aff')
671  let output = execute('mkspell! Xtest.spl Xtest')
672  call assert_match('FLAG after using flags in Xtest.aff line 3: long', output)
673
674  " INFO in affix file
675  let save_encoding = &encoding
676  call mkdir('Xrtp/spell', 'p')
677  call writefile(['1', 'work'], 'Xrtp/spell/Xtest.dic')
678  call writefile(['NAME klingon', 'VERSION 1.4', 'AUTHOR Spock'],
679        \ 'Xrtp/spell/Xtest.aff')
680  silent mkspell! Xrtp/spell/Xtest.utf-8.spl Xrtp/spell/Xtest
681  let save_rtp = &rtp
682  set runtimepath=./Xrtp
683  set spelllang=Xtest
684  set spell
685  let output = split(execute('spellinfo'), "\n")
686  call assert_equal("NAME klingon", output[1])
687  call assert_equal("VERSION 1.4", output[2])
688  call assert_equal("AUTHOR Spock", output[3])
689  let &rtp = save_rtp
690  call delete('Xrtp', 'rf')
691  set spell& spelllang& spellfile&
692  %bw!
693  " 'encoding' must be set again to clear the spell file in memory
694  let &encoding = save_encoding
695
696  " COMPOUNDFORBIDFLAG flag after PFX in an affix file
697  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'COMPOUNDFLAG c', 'COMPOUNDFORBIDFLAG x'],
698        \ 'Xtest.aff')
699  let output = execute('mkspell! Xtest.spl Xtest')
700  call 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
703  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'COMPOUNDPERMITFLAG c'],
704        \ 'Xtest.aff')
705  let output = execute('mkspell! Xtest.spl Xtest')
706  call 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
709  call writefile(['COMPOUNDRULES a'], 'Xtest.aff')
710  let output = execute('mkspell! Xtest.spl Xtest')
711  call assert_match('Wrong COMPOUNDRULES value in Xtest.aff line 1: a', output)
712
713  " Wrong COMPOUNDWORDMAX flag value in an affix file
714  call writefile(['COMPOUNDWORDMAX 0'], 'Xtest.aff')
715  let output = execute('mkspell! Xtest.spl Xtest')
716  call assert_match('Wrong COMPOUNDWORDMAX value in Xtest.aff line 1: 0', output)
717
718  " Wrong COMPOUNDMIN flag value in an affix file
719  call writefile(['COMPOUNDMIN 0'], 'Xtest.aff')
720  let output = execute('mkspell! Xtest.spl Xtest')
721  call assert_match('Wrong COMPOUNDMIN value in Xtest.aff line 1: 0', output)
722
723  " Wrong COMPOUNDSYLMAX flag value in an affix file
724  call writefile(['COMPOUNDSYLMAX 0'], 'Xtest.aff')
725  let output = execute('mkspell! Xtest.spl Xtest')
726  call assert_match('Wrong COMPOUNDSYLMAX value in Xtest.aff line 1: 0', output)
727
728  " Wrong CHECKCOMPOUNDPATTERN flag value in an affix file
729  call writefile(['CHECKCOMPOUNDPATTERN 0'], 'Xtest.aff')
730  let output = execute('mkspell! Xtest.spl Xtest')
731  call assert_match('Wrong CHECKCOMPOUNDPATTERN value in Xtest.aff line 1: 0', output)
732
733  " Both compounding and NOBREAK specified
734  call writefile(['COMPOUNDFLAG c', 'NOBREAK'], 'Xtest.aff')
735  let output = execute('mkspell! Xtest.spl Xtest')
736  call assert_match('Warning: both compounding and NOBREAK specified', output)
737
738  " Duplicate affix entry in an affix file
739  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'PFX L Y 1', 'PFX L 0 re x'],
740        \ 'Xtest.aff')
741  let output = execute('mkspell! Xtest.spl Xtest')
742  call assert_match('Duplicate affix in Xtest.aff line 3: L', output)
743
744  " Duplicate affix entry in an affix file
745  call writefile(['PFX L Y 1', 'PFX L Y 1'], 'Xtest.aff')
746  let output = execute('mkspell! Xtest.spl Xtest')
747  call assert_match('Unrecognized or duplicate item in Xtest.aff line 2: PFX', output)
748
749  " Different combining flags in an affix file
750  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'PFX L N 1'], 'Xtest.aff')
751  let output = execute('mkspell! Xtest.spl Xtest')
752  call 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
755  call writefile(['BAD x', 'PFX x Y 1', 'PFX x 0 re x'], 'Xtest.aff')
756  let output = execute('mkspell! Xtest.spl Xtest')
757  call 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
760  call writefile(['PFX L Y 1 Test', 'PFX L 0 re x'], 'Xtest.aff')
761  let output = execute('mkspell! Xtest.spl Xtest')
762  call assert_match('Trailing text in Xtest.aff line 1: Test', output)
763
764  " Trailing characters in an affix entry
765  call writefile(['PFX L Y 1', 'PFX L 0 re x Test'], 'Xtest.aff')
766  let output = execute('mkspell! Xtest.spl Xtest')
767  call assert_match('Trailing text in Xtest.aff line 2: Test', output)
768
769  " Incorrect combine flag in an affix entry
770  call writefile(['PFX L X 1', 'PFX L 0 re x'], 'Xtest.aff')
771  let output = execute('mkspell! Xtest.spl Xtest')
772  call assert_match('Expected Y or N in Xtest.aff line 1: X', output)
773
774  " Invalid count for REP item
775  call writefile(['REP a'], 'Xtest.aff')
776  let output = execute('mkspell! Xtest.spl Xtest')
777  call assert_match('Expected REP(SAL) count in Xtest.aff line 1', output)
778
779  " Trailing characters in REP item
780  call writefile(['REP 1', 'REP f ph test'], 'Xtest.aff')
781  let output = execute('mkspell! Xtest.spl Xtest')
782  call assert_match('Trailing text in Xtest.aff line 2: test', output)
783
784  " Invalid count for MAP item
785  call writefile(['MAP a'], 'Xtest.aff')
786  let output = execute('mkspell! Xtest.spl Xtest')
787  call assert_match('Expected MAP count in Xtest.aff line 1', output)
788
789  " Duplicate character in a MAP item
790  call writefile(['MAP 2', 'MAP xx', 'MAP yy'], 'Xtest.aff')
791  let output = execute('mkspell! Xtest.spl Xtest')
792  call assert_match('Duplicate character in MAP in Xtest.aff line 2', output)
793
794  " Use COMPOUNDSYLMAX without SYLLABLE
795  call writefile(['COMPOUNDSYLMAX 2'], 'Xtest.aff')
796  let output = execute('mkspell! Xtest.spl Xtest')
797  call assert_match('COMPOUNDSYLMAX used without SYLLABLE', output)
798
799  " Missing SOFOTO
800  call writefile(['SOFOFROM abcdef'], 'Xtest.aff')
801  let output = execute('mkspell! Xtest.spl Xtest')
802  call assert_match('Missing SOFOTO line in Xtest.aff', output)
803
804  " Length of SOFOFROM and SOFOTO differ
805  call writefile(['SOFOFROM abcde', 'SOFOTO ABCD'], 'Xtest.aff')
806  call assert_fails('mkspell! Xtest.spl Xtest', 'E759:')
807
808  " Both SAL and SOFOFROM/SOFOTO items
809  call writefile(['SOFOFROM abcd', 'SOFOTO ABCD', 'SAL CIA X'], 'Xtest.aff')
810  let output = execute('mkspell! Xtest.spl Xtest')
811  call assert_match('Both SAL and SOFO lines in Xtest.aff', output)
812
813  " use an alphabet flag when FLAG is num
814  call writefile(['FLAG num', 'SFX L Y 1', 'SFX L 0 re [^x]'], 'Xtest.aff')
815  let output = execute('mkspell! Xtest.spl Xtest')
816  call 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
819  call writefile(['FLAG num', 'SFX 4f Y 1', 'SFX 4f 0 re [^x]'], 'Xtest.aff')
820  let output = execute('mkspell! Xtest.spl Xtest')
821  call assert_match('Affix name too long in Xtest.aff line 2: 4f', output)
822
823  " use a single character flag when FLAG is long
824  call writefile(['FLAG long', 'SFX L Y 1', 'SFX L 0 re [^x]'], 'Xtest.aff')
825  let output = execute('mkspell! Xtest.spl Xtest')
826  call 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
867  call writefile(['2', 'good', 'good', 'good'], 'Xtest.dic')
868  call writefile(['NAME vim'], 'Xtest.aff')
869  let output = execute('mkspell! Xtest.spl Xtest')
870  call assert_match('First duplicate word in Xtest.dic line 3: good', output)
871  call 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)
875  call writefile(['1', 'world'], 'Xtest_US.dic', 'D')
876  call writefile(['1', 'world'], 'Xtest_CA.dic', 'D')
877  call writefile(["COMPOUNDWORDMAX 3", "MIDWORD '-"], 'Xtest_US.aff', 'D')
878  call writefile(["COMPOUNDWORDMAX 4", "MIDWORD '="], 'Xtest_CA.aff', 'D')
879  let output = execute('mkspell! Xtest.spl Xtest_US Xtest_CA')
880  call assert_match('COMPOUNDWORDMAX value differs from what is used in another .aff file', output)
881  call assert_match('MIDWORD value differs from what is used in another .aff file', output)
882
883  call delete('Xtest.spl')
884  call delete('Xtest.sug')
885endfunc
886
887func Test_spell_add_word()
888  set spellfile=
889  call assert_fails('spellgood abc', 'E764:')
890
891  set spellfile=Xtest.utf-8.add
892  call assert_fails('2spellgood abc', 'E765:')
893
894  edit Xtest.utf-8.add
895  call setline(1, 'sample')
896  call assert_fails('spellgood abc', 'E139:')
897  set spellfile&
898  %bw!
899endfunc
900
901func Test_spell_add_long_word()
902  set spell spellfile=./Xspellfile.add spelllang=en
903
904  let word = repeat('a', 9000)
905  let v:errmsg = ''
906  " Spell checking doesn't really work for such a long word,
907  " but this should not cause an E1510 error.
908  exe 'spellgood ' .. word
909  call assert_equal('', v:errmsg)
910  call assert_equal([word], readfile('./Xspellfile.add'))
911
912  set spell& spellfile= spelllang& encoding=utf-8
913  call delete('./Xspellfile.add')
914  call delete('./Xspellfile.add.spl')
915endfunc
916
917func Test_spellfile_verbose()
918  call writefile(['1', 'one'], 'XtestVerbose.dic', 'D')
919  call writefile([], 'XtestVerbose.aff', 'D')
920  mkspell! XtestVerbose-utf8.spl XtestVerbose
921  set spell
922
923  " First time: the spl file should be read.
924  let a = execute('3verbose set spelllang=XtestVerbose-utf8.spl')
925  call assert_match('Reading spell file "XtestVerbose-utf8.spl"', a)
926
927  " Second time time: the spl file should not be read (already read).
928  let a = execute('3verbose set spelllang=XtestVerbose-utf8.spl')
929  call assert_notmatch('Reading spell file "XtestVerbose-utf8.spl"', a)
930
931  set spell& spelllang&
932  call delete('XtestVerbose-utf8.spl')
933endfunc
934
935" Test NOBREAK (see :help spell-NOBREAK)
936func Test_NOBREAK()
937  call writefile(['3', 'one', 'two', 'three' ], 'XtestNOBREAK.dic', 'D')
938  call writefile(['NOBREAK' ], 'XtestNOBREAK.aff', 'D')
939
940  mkspell! XtestNOBREAK-utf8.spl XtestNOBREAK
941  set spell spelllang=XtestNOBREAK-utf8.spl
942
943  call assert_equal(['', ''], spellbadword('One two three onetwo onetwothree threetwoone'))
944
945  call assert_equal(['x', 'bad'], spellbadword('x'))
946  call assert_equal(['y', 'bad'], spellbadword('yone'))
947  call assert_equal(['z', 'bad'], spellbadword('onez'))
948  call assert_equal(['zero', 'bad'], spellbadword('Onetwozerothree'))
949
950  new
951  call setline(1, 'Onetwwothree')
952  norm! fw1z=
953  call assert_equal('Onetwothree', getline(1))
954  call setline(1, 'Onetwothre')
955  norm! fh1z=
956  call assert_equal('Onetwothree', getline(1))
957
958  bw!
959  set spell& spelllang&
960  call delete('XtestNOBREAK-utf8.spl')
961endfunc
962
963" Test CHECKCOMPOUNDPATTERN (see :help spell-CHECKCOMPOUNDPATTERN)
964func Test_spellfile_CHECKCOMPOUNDPATTERN()
965  call 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'.
971  call writefile(['CHECKCOMPOUNDPATTERN 1',
972        \         'CHECKCOMPOUNDPATTERN wo on',
973        \         'COMPOUNDFLAG c'], 'XtestCHECKCOMPOUNDPATTERN.aff', 'D')
974
975  mkspell! XtestCHECKCOMPOUNDPATTERN-utf8.spl XtestCHECKCOMPOUNDPATTERN
976  set spell spelllang=XtestCHECKCOMPOUNDPATTERN-utf8.spl
977
978  " Check valid words with and without valid compounds.
979  for goodword in ['one', 'two', 'three', 'four',
980        \          'oneone', 'onetwo',  'onethree',
981        \          'twotwo', 'twothree',
982        \          'threeone', 'threetwo', 'threethree',
983        \          'onetwothree', 'onethreetwo', 'twothreeone', 'oneoneone']
984    call assert_equal(['', ''], spellbadword(goodword), goodword)
985  endfor
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.
990  for badword in ['five', 'onetwox',
991        \         'twoone', 'threetwoone',
992        \         'fourone', 'onefour']
993    call assert_equal([badword, 'bad'], spellbadword(badword))
994  endfor
995
996  set spell& spelllang&
997  call delete('XtestCHECKCOMPOUNDPATTERN-utf8.spl')
998endfunc
999
1000" Test NOCOMPOUNDSUGS (see :help spell-NOCOMPOUNDSUGS)
1001func Test_spellfile_NOCOMPOUNDSUGS()
1002  call 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
1008  for pass in [0, 1]
1009    if pass == 0
1010      call writefile(['COMPOUNDFLAG c'], 'XtestNOCOMPOUNDSUGS.aff', 'D')
1011    else
1012      call writefile(['NOCOMPOUNDSUGS',
1013          \           'COMPOUNDFLAG c'], 'XtestNOCOMPOUNDSUGS.aff', 'D')
1014    endif
1015
1016    mkspell! XtestNOCOMPOUNDSUGS-utf8.spl XtestNOCOMPOUNDSUGS
1017    set spell spelllang=XtestNOCOMPOUNDSUGS-utf8.spl
1018
1019    for goodword in ['one', 'two', 'three',
1020          \          'oneone', 'onetwo',  'onethree',
1021          \          'twoone', 'twotwo', 'twothree',
1022          \          'threeone', 'threetwo', 'threethree',
1023          \          'onetwothree', 'onethreetwo', 'twothreeone', 'oneoneone']
1024      call assert_equal(['', ''], spellbadword(goodword), goodword)
1025    endfor
1026
1027    for badword in ['four', 'onetwox', 'onexone']
1028      call assert_equal([badword, 'bad'], spellbadword(badword))
1029    endfor
1030
1031    if pass == 0
1032      call assert_equal(['one', 'oneone'], spellsuggest('onne', 2))
1033      call assert_equal(['onethree', 'one three'], spellsuggest('onethre', 2))
1034    else
1035      call assert_equal(['one', 'one one'], spellsuggest('onne', 2))
1036      call assert_equal(['one three'], spellsuggest('onethre', 2))
1037    endif
1038  endfor
1039
1040  set spell& spelllang&
1041  call delete('XtestNOCOMPOUNDSUGS-utf8.spl')
1042endfunc
1043
1044" Test COMMON (better suggestions with common words, see :help spell-COMMON)
1045func Test_spellfile_COMMON()
1046  call writefile(['7',
1047        \         'and',
1048        \         'ant',
1049        \         'end',
1050        \         'any',
1051        \         'tee',
1052        \         'the',
1053        \         'ted'], 'XtestCOMMON.dic', 'D')
1054  call writefile(['COMMON the and'], 'XtestCOMMON.aff', 'D')
1055
1056  mkspell! XtestCOMMON-utf8.spl XtestCOMMON
1057  set spell spelllang=XtestCOMMON-utf8.spl
1058
1059  " COMMON words 'and' and 'the' should be the top suggestions.
1060  call assert_equal(['and', 'ant'], spellsuggest('anr', 2))
1061  call assert_equal(['and', 'end'], spellsuggest('ond', 2))
1062  call assert_equal(['the', 'ted'], spellsuggest('tha', 2))
1063  call assert_equal(['the', 'tee'], spellsuggest('dhe', 2))
1064
1065  set spell& spelllang&
1066  call delete('XtestCOMMON-utf8.spl')
1067endfunc
1068
1069" Test NOSUGGEST (see :help spell-COMMON)
1070func Test_spellfile_NOSUGGEST()
1071  call writefile(['2', 'foo/X', 'fog'], 'XtestNOSUGGEST.dic', 'D')
1072  call writefile(['NOSUGGEST X'], 'XtestNOSUGGEST.aff', 'D')
1073
1074  mkspell! XtestNOSUGGEST-utf8.spl XtestNOSUGGEST
1075  set spell spelllang=XtestNOSUGGEST-utf8.spl
1076
1077  for goodword in ['foo', 'Foo', 'FOO', 'fog', 'Fog', 'FOG']
1078    call assert_equal(['', ''], spellbadword(goodword), goodword)
1079  endfor
1080  for badword in ['foO', 'fOO', 'fooo', 'foog', 'foofog', 'fogfoo']
1081    call assert_equal([badword, 'bad'], spellbadword(badword))
1082  endfor
1083
1084  call assert_equal(['fog'], spellsuggest('fooo', 1))
1085  call assert_equal(['fog'], spellsuggest('fOo', 1))
1086  call assert_equal(['fog'], spellsuggest('foG', 1))
1087  call assert_equal(['fog'], spellsuggest('fogg', 1))
1088
1089  set spell& spelllang&
1090  call 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
1097  call writefile(['1',
1098        \         'nagy/C	po:adj'], 'XtestCIRCUMFIX.dic', 'D')
1099  call 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
1115  mkspell! XtestCIRCUMFIX-utf8.spl XtestCIRCUMFIX
1116  set 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
1125  call assert_equal(['', ''], spellbadword('nagy nagyobb legnagyobb legeslegnagyobb'))
1126
1127  for badword in ['legnagy', 'legeslegnagy', 'legobb', 'legeslegobb']
1128    call assert_equal([badword, 'bad'], spellbadword(badword))
1129  endfor
1130
1131  set spell& spelllang&
1132  call 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).
1138  call 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
1148  call writefile(['3', 'cantare/A', 'cercare/A', 'odiare/A'], 'XtestSFX.dic', 'D')
1149
1150  mkspell! XtestSFX-utf8.spl XtestSFX
1151  set spell spelllang=XtestSFX-utf8.spl
1152
1153  " To sing, we're singing, we were singing.
1154  call assert_equal(['', ''], spellbadword('cantare cantiamo cantavamo'))
1155
1156  " To search, we're searching, we were searching.
1157  call assert_equal(['', ''], spellbadword('cercare cerchiamo cercavamo'))
1158
1159  " To hate, we hate, we were hating.
1160  call assert_equal(['', ''], spellbadword('odiare odiamo odiavamo'))
1161
1162  for badword in ['canthiamo', 'cerciamo', 'cantarevamo', 'odiiamo']
1163    call assert_equal([badword, 'bad'], spellbadword(badword))
1164  endfor
1165
1166  call assert_equal(['cantiamo'],  spellsuggest('canthiamo', 1))
1167  call assert_equal(['cerchiamo'], spellsuggest('cerciamo', 1))
1168  call assert_equal(['cantavamo'], spellsuggest('cantarevamo', 1))
1169  call assert_equal(['odiamo'],    spellsuggest('odiiamo', 1))
1170
1171  set spell& spelllang&
1172  call 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()
1178  throw 'Skipped: Nvim defaults spellfile to stdpath("data")/spell/'
1179  let save_rtp = &rtp
1180  let save_encoding = &encoding
1181  call mkdir('Xrtp/spell', 'pR')
1182  call writefile(['vim'], 'Xrtp/spell/Xtest.dic')
1183  silent mkspell Xrtp/spell/Xtest.utf-8.spl Xrtp/spell/Xtest.dic
1184  set runtimepath=./Xrtp
1185  set spelllang=Xtest
1186  set spell
1187  silent spellgood abc
1188  call assert_equal('./Xrtp/spell/Xtest.utf-8.add', &spellfile)
1189  call assert_equal(['abc'], readfile('Xrtp/spell/Xtest.utf-8.add'))
1190  call assert_true(filereadable('Xrtp/spell/Xtest.utf-8.spl'))
1191
1192  set spell& spelllang& spellfile&
1193  let &encoding = save_encoding
1194  let &rtp = save_rtp
1195  %bw!
1196endfunc
1197
1198" Test for the 'mkspellmem' option
1199func Test_mkspellmem_opt()
1200  call assert_fails('set mkspellmem=1000', 'E474:')
1201  call assert_fails('set mkspellmem=1000,', 'E474:')
1202  call assert_fails('set mkspellmem=1000,50', 'E474:')
1203  call assert_fails('set mkspellmem=1000,50,', 'E474:')
1204  call assert_fails('set mkspellmem=1000,50,10,', 'E474:')
1205  call assert_fails('set mkspellmem=1000,50,0', 'E474:')
1206endfunc
1207
1208" 'spellfile' accepts '@' on top of 'isfname'.
1209func Test_spellfile_allow_at_character()
1210  call mkdir('Xtest/the foo@bar,dir', 'p')
1211  let &spellfile = './Xtest/the foo@bar\,dir/Xspellfile.add'
1212  let &spellfile = ''
1213  call delete('Xtest', 'rf')
1214endfunc
1215
1216" this was using a NULL pointer
1217func Test_mkspell_empty_dic()
1218  call writefile(['1'], 'XtestEmpty.dic', 'D')
1219  call writefile(['SOFOFROM abcd', 'SOFOTO ABCD', 'SAL CIA X'], 'XtestEmpty.aff', 'D')
1220  mkspell! XtestEmpty.spl XtestEmpty
1221
1222  call delete('XtestEmpty.spl')
1223endfunc
1224
1225" This used to cause a buffer overflow
1226func Test_mkspell_no_buffer_overflow()
1227  CheckNotMSWindows
1228
1229  let aff_lines = ['SET ISO8859-1', 'SFX A Y 1',
1230        \ 'SFX A 0 s ' .. repeat(nr2char(0xff), 491)]
1231  call writefile(aff_lines, 'Xbof.aff', 'D')
1232  call writefile(['1', 'word/A'], 'Xbof.dic', 'D')
1233  " Must not crash; ignore any conversion/regex errors.
1234  try
1235    mkspell! Xbof.spl Xbof
1236  catch
1237  endtry
1238  defer delete('Xbof.spl')
1239
1240  let long = repeat(nr2char(0xff), 200)
1241  let aff2_lines = ['SET ISO8859-1', 'SFX A Y 1',
1242        \ 'SFX A 0 ' .. long .. ' .']
1243  call writefile(aff2_lines, 'Xbof2.aff', 'D')
1244  call writefile(['1', long .. '/A'], 'Xbof2.dic', 'D')
1245  try
1246    mkspell! Xbof2.spl Xbof2
1247  catch
1248  endtry
1249  defer delete('Xbof2.spl')
1250endfunc
1251
1252func Test_mkspell_no_affixlist_overflow()
1253  let aff_lines = [
1254        \ 'SET ISO8859-1',
1255        \ 'PFXPOSTPONE',
1256        \ 'PFX A Y 1',
1257        \ 'PFX A 0 pre .',
1258        \ ]
1259  call writefile(aff_lines, 'Xaffbof.aff', 'D')
1260  call writefile(['1', 'word/' .. repeat('A', 300)], 'Xaffbof.dic', 'D')
1261
1262  call assert_fails('mkspell! Xaffbof.spl Xaffbof',
1263        \ 'Too many postponed prefixes and/or compound flags')
1264  call 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].
1271  let aff_lines = [
1272        \ 'SET ISO8859-1',
1273        \ 'COMPOUNDFLAG c',
1274        \ ]
1275  call writefile(aff_lines, 'Xcompbof.aff', 'D')
1276
1277  " Repeat the compound flag 'c' far past MAXWLEN.
1278  call writefile(['1', 'word/' .. repeat('c', 300)], 'Xcompbof.dic', 'D')
1279
1280  call assert_fails('mkspell! Xcompbof.spl Xcompbof',
1281        \ 'Too many postponed prefixes and/or compound flags')
1282  call 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.
1291  call mkdir('Xrtp/spell', 'pR')
1292  " VIMspell + v50, SN_SUGFILE(ts), SN_END, LWORDTREE{node:1,BY_INDEX->0,'A'},
1293  " empty KWORDTREE/PREFIXTREE
1294  let spl = eval('0z56494D7370656C6C320B0000000008000000001234'
1295        \ .. '5678FF000000020101000000410000000000000000')
1296  " VIMsug + v1, matching ts, SUGWORDTREE word "a", empty SUGTABLE
1297  let sug = 0z56494D737567010000000012345678000000040161010000000000
1298  call writefile(spl, 'Xrtp/spell/xx.utf-8.spl', 'b')
1299  call writefile(sug, 'Xrtp/spell/xx.utf-8.sug', 'b')
1300
1301  new
1302  set runtimepath+=./Xrtp
1303  set spelllang=xx
1304  set spell
1305  " Unpatched: OOB write here (ASan abort, or crash).  Patched: returns a list.
1306  call assert_equal(v:t_list, type(spellsuggest('helloo')))
1307
1308  set spell& spelllang& runtimepath&
1309  bwipe!
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()
1315  throw 'Skipped: Nvim does not support setting encoding=latin1'
1316  let _enc=&enc
1317  set enc=latin1
1318  call writefile(['SOFOFROM ab', 'SOFOTO xy'], 'Xtest.aff', 'D')
1319  call writefile(['1', 'foo'], 'Xtest.dic', 'D')
1320  mkspell! Xtest Xtest
1321  defer delete('Xtest.latin1.spl')
1322  defer delete('Xtest.latin1.sug')
1323  setl spelllang=Xtest.latin1.spl spell
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.
1327  let sound = soundfold(repeat('ab', 300))
1328  call assert_true(strlen(sound) < 254, 'soundfold result exceeds MAXWLEN')
1329
1330  set spell& spelllang&
1331  let &enc = _enc
1332endfunc
1333
1334func Test_spell_sal_sofo_truncated()
1335  call 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)
1341  let bytes = 0z56494d7370656c6c.3205000000000700.000101610161060000.000000
1342  call writefile(bytes, 'Xspelldir/spell/Xx.utf-8.spl', 'b')
1343
1344  let save_rtp = &rtp
1345  set rtp=./Xspelldir
1346  try
1347    set spelllang=Xx
1348    silent! set spell
1349  catch
1350    " an error message is fine; a crash is not
1351  endtry
1352
1353  " Reaching this point means Vim did not crash on the crafted file.
1354  call assert_true(v:true)
1355
1356  set nospell
1357  set spelllang&
1358  let &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.
1366  call 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')
1370  call writefile(['3', 'apple/A', 'bread/B', 'cat/C'], 'Xpfx.dic', 'D')
1371  mkspell! Xpfx.spl Xpfx
1372  defer delete('Xpfx.spl')
1373
1374  let 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.
1379  for n in range(len(bytes) - 40, len(bytes) - 1)
1380    let fname = 'Xtrunc' .. n .. '.spl'
1381    call writefile(bytes[0 : n - 1], fname)
1382    try
1383      execute 'set spl=' .. fname
1384      set spell
1385    catch /E75[789]:/
1386    endtry
1387    " Use the prefix tree, in case a bad index was stored.
1388    call spellbadword('unapple')
1389    call spellbadword('rebread')
1390    set nospell spl&
1391    call delete(fname)
1392  endfor
1393
1394  " The valid file still works, prefixed words are recognized.
1395  set spl=Xpfx.spl spell
1396  call assert_equal(['', ''], spellbadword('unapple'))
1397  call assert_equal(['', ''], spellbadword('rebread'))
1398
1399  set nospell spl& spelllang&
1400endfunc
1401
1402" vim: shiftwidth=2 sts=2 expandtab