comparison src/testdir/test_spellfile.vim @ 21887:776c76599617 v8.2.1493

patch 8.2.1493: not enough test coverage for the spell file handling Commit: https://github.com/vim/vim/commit/fc2a47ffc425777704b329c9edbe21163bedf63c Author: Bram Moolenaar <Bram@vim.org> Date: Thu Aug 20 15:41:55 2020 +0200 patch 8.2.1493: not enough test coverage for the spell file handling Problem: Not enough test coverage for the spell file handling. Solution: Add spell file tests. (Yegappan Lakshmanan, closes https://github.com/vim/vim/issues/6728)
author Bram Moolenaar <Bram@vim.org>
date Thu, 20 Aug 2020 15:45:04 +0200
parents 7a6ca887128d
children 9998f4e44a73
comparison
equal deleted inserted replaced
21886:ff9f9ef2296b 21887:776c76599617
169 169
170 set spellfile= spell& spelllang& 170 set spellfile= spell& spelllang&
171 bw! 171 bw!
172 endfunc 172 endfunc
173 173
174 " Test for spell file format errors 174 " Spell file content test. Write 'content' to the spell file prefixed by the
175 " spell file header and then enable spell checking. If 'emsg' is not empty,
176 " then check for error.
177 func Spellfile_Test(content, emsg)
178 let splfile = './Xtest/spell/Xtest.utf-8.spl'
179 " Add the spell file header and version (VIMspell2)
180 let v = 0z56494D7370656C6C32 + a:content
181 call writefile(v, splfile, 'b')
182 set runtimepath=./Xtest
183 set spelllang=Xtest
184 if a:emsg != ''
185 call assert_fails('set spell', a:emsg)
186 else
187 " FIXME: With some invalid spellfile contents, there are no error
188 " messages. So don't know how to check for the test result.
189 set spell
190 endif
191 set nospell spelllang& rtp&
192 endfunc
193
194 " Test for spell file format errors.
195 " The spell file format is described in spellfile.c
175 func Test_spellfile_format_error() 196 func Test_spellfile_format_error()
176 let save_rtp = &rtp 197 let save_rtp = &rtp
177 call mkdir('Xtest/spell', 'p') 198 call mkdir('Xtest/spell', 'p')
199 let splfile = './Xtest/spell/Xtest.utf-8.spl'
178 200
179 " empty spell file 201 " empty spell file
180 call writefile([], './Xtest/spell/Xtest.utf-8.spl') 202 call writefile([], splfile)
181 set runtimepath=./Xtest 203 set runtimepath=./Xtest
182 set spelllang=Xtest 204 set spelllang=Xtest
183 call assert_fails('set spell', 'E757:') 205 call assert_fails('set spell', 'E757:')
184 set nospell spelllang& 206 set nospell spelllang&
185 207
186 " invalid file ID 208 " invalid file ID
187 call writefile(['vim'], './Xtest/spell/Xtest.utf-8.spl') 209 call writefile(0z56494D, splfile, 'b')
188 set runtimepath=./Xtest 210 set runtimepath=./Xtest
189 set spelllang=Xtest 211 set spelllang=Xtest
190 call assert_fails('set spell', 'E757:') 212 call assert_fails('set spell', 'E757:')
191 set nospell spelllang& 213 set nospell spelllang&
192 214
193 " missing version number 215 " missing version number
194 call writefile(['VIMspell'], './Xtest/spell/Xtest.utf-8.spl') 216 call writefile(0z56494D7370656C6C, splfile, 'b')
195 set runtimepath=./Xtest 217 set runtimepath=./Xtest
196 set spelllang=Xtest 218 set spelllang=Xtest
197 call assert_fails('set spell', 'E771:') 219 call assert_fails('set spell', 'E771:')
198 set nospell spelllang& 220 set nospell spelllang&
199 221
200 " invalid version number 222 " invalid version number
201 call writefile(['VIMspellz'], './Xtest/spell/Xtest.utf-8.spl') 223 call writefile(0z56494D7370656C6C7A, splfile, 'b')
202 set runtimepath=./Xtest 224 set runtimepath=./Xtest
203 set spelllang=Xtest 225 set spelllang=Xtest
204 call assert_fails('set spell', 'E772:') 226 call assert_fails('set spell', 'E772:')
205 set nospell spelllang& 227 set nospell spelllang&
206 228
207 " no sections 229 " no sections
208 call writefile(0z56494D7370656C6C32, './Xtest/spell/Xtest.utf-8.spl', 'b') 230 call Spellfile_Test(0z, 'E758:')
209 set runtimepath=./Xtest
210 set spelllang=Xtest
211 call assert_fails('set spell', 'E758:')
212 set nospell spelllang&
213 231
214 " missing section length 232 " missing section length
215 call writefile(['VIMspell200'], './Xtest/spell/Xtest.utf-8.spl') 233 call Spellfile_Test(0z00, 'E758:')
216 set runtimepath=./Xtest
217 set spelllang=Xtest
218 call assert_fails('set spell', 'E758:')
219 set nospell spelllang&
220 234
221 " unsupported required section 235 " unsupported required section
222 call writefile(['VIMspell2z' .. nr2char(1) .. ' ' .. nr2char(4)], 236 call Spellfile_Test(0z7A0100000004, 'E770:')
223 \ './Xtest/spell/Xtest.utf-8.spl')
224 set runtimepath=./Xtest
225 set spelllang=Xtest
226 call assert_fails('set spell', 'E770:')
227 set nospell spelllang&
228 237
229 " unsupported not-required section 238 " unsupported not-required section
230 call writefile(['VIMspell2z' .. nr2char(0) .. ' ' .. nr2char(4)], 239 call Spellfile_Test(0z7A0000000004, 'E758:')
231 \ './Xtest/spell/Xtest.utf-8.spl')
232 set runtimepath=./Xtest
233 set spelllang=Xtest
234 call assert_fails('set spell', 'E758:')
235 set nospell spelllang&
236 240
237 " SN_REGION: invalid number of region names 241 " SN_REGION: invalid number of region names
238 call writefile(0z56494D7370656C6C320000000000FF, 242 call Spellfile_Test(0z0000000000FF, 'E759:')
239 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
240 set runtimepath=./Xtest
241 set spelllang=Xtest
242 call assert_fails('set spell', 'E759:')
243 set nospell spelllang&
244 243
245 " SN_CHARFLAGS: missing <charflagslen> length 244 " SN_CHARFLAGS: missing <charflagslen> length
246 call writefile(0z56494D7370656C6C32010000000004, 245 call Spellfile_Test(0z010000000004, 'E758:')
247 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
248 set runtimepath=./Xtest
249 set spelllang=Xtest
250 call assert_fails('set spell', 'E758:')
251 set nospell spelllang&
252 246
253 " SN_CHARFLAGS: invalid <charflagslen> length 247 " SN_CHARFLAGS: invalid <charflagslen> length
254 call writefile(0z56494D7370656C6C320100000000010201, 248 call Spellfile_Test(0z0100000000010201, '')
255 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
256 set runtimepath=./Xtest
257 set spelllang=Xtest
258 set spell
259 " FIXME: There are no error messages. How to check for the test result?
260 set nospell spelllang&
261 249
262 " SN_CHARFLAGS: charflagslen == 0 and folcharslen != 0 250 " SN_CHARFLAGS: charflagslen == 0 and folcharslen != 0
263 call writefile(0z56494D7370656C6C3201000000000400000101, 251 call Spellfile_Test(0z01000000000400000101, 'E759:')
264 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
265 set runtimepath=./Xtest
266 set spelllang=Xtest
267 call assert_fails('set spell', 'E759:')
268 set nospell spelllang&
269 252
270 " SN_CHARFLAGS: missing <folcharslen> length 253 " SN_CHARFLAGS: missing <folcharslen> length
271 call writefile(0z56494D7370656C6C3201000000000100, 254 call Spellfile_Test(0z01000000000100, 'E758:')
272 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
273 set runtimepath=./Xtest
274 set spelllang=Xtest
275 call assert_fails('set spell', 'E758:')
276 set nospell spelllang&
277 255
278 " SN_PREFCOND: invalid prefcondcnt 256 " SN_PREFCOND: invalid prefcondcnt
279 call writefile(0z56494D7370656C6C3203000000000100, 257 call Spellfile_Test(0z03000000000100, 'E759:')
280 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
281 set runtimepath=./Xtest
282 set spelllang=Xtest
283 call assert_fails('set spell', 'E759:')
284 set nospell spelllang&
285 258
286 " SN_PREFCOND: invalid condlen 259 " SN_PREFCOND: invalid condlen
287 call writefile(0z56494D7370656C6C320300000000020001, 260 call Spellfile_Test(0z0300000000020001, 'E759:')
288 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
289 set runtimepath=./Xtest
290 set spelllang=Xtest
291 call assert_fails('set spell', 'E759:')
292 set nospell spelllang&
293 261
294 " SN_REP: invalid repcount 262 " SN_REP: invalid repcount
295 call writefile(0z56494D7370656C6C3204000000000100, 263 call Spellfile_Test(0z04000000000100, 'E758:')
296 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
297 set runtimepath=./Xtest
298 set spelllang=Xtest
299 call assert_fails('set spell', 'E758:')
300 set nospell spelllang&
301 264
302 " SN_REP: missing rep 265 " SN_REP: missing rep
303 call writefile(0z56494D7370656C6C320400000000020004, 266 call Spellfile_Test(0z0400000000020004, 'E758:')
304 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
305 set runtimepath=./Xtest
306 set spelllang=Xtest
307 call assert_fails('set spell', 'E758:')
308 set nospell spelllang&
309 267
310 " SN_REP: zero repfromlen 268 " SN_REP: zero repfromlen
311 call writefile(0z56494D7370656C6C32040000000003000100, 269 call Spellfile_Test(0z040000000003000100, 'E759:')
312 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
313 set runtimepath=./Xtest
314 set spelllang=Xtest
315 call assert_fails('set spell', 'E759:')
316 set nospell spelllang&
317 270
318 " SN_REP: invalid reptolen 271 " SN_REP: invalid reptolen
319 call writefile(0z56494D7370656C6C320400000000050001014101, 272 call Spellfile_Test(0z0400000000050001014101, '')
320 \ './Xtest/spell/Xtest.utf-8.spl', 'b')
321 set runtimepath=./Xtest
322 set spelllang=Xtest
323 " FIXME: There are no error messages. How to check for the test result?
324 set spell
325 set nospell spelllang&
326 273
327 " SN_REP: zero reptolen 274 " SN_REP: zero reptolen
328 call writefile(0z56494D7370656C6C320400000000050001014100, 275 call Spellfile_Test(0z0400000000050001014100, 'E759:')
329 \ './Xtest/spell/Xtest.utf-8.spl', 'b') 276
330 set runtimepath=./Xtest 277 " SN_SAL: missing salcount
331 set spelllang=Xtest 278 call Spellfile_Test(0z05000000000102, 'E758:')
332 call assert_fails('set spell', 'E759:') 279
333 set nospell spelllang& 280 " SN_SAL: missing salfromlen
281 call Spellfile_Test(0z050000000003080001, 'E758:')
282
283 " SN_SAL: missing saltolen
284 call Spellfile_Test(0z0500000000050400010161, 'E758:')
285
286 " SN_WORDS: non-NUL terminated word
287 call Spellfile_Test(0z0D000000000376696D, 'E758:')
288
289 " SN_WORDS: very long word
290 let v = eval('0z0D000000012C' .. repeat('41', 300))
291 call Spellfile_Test(v, 'E759:')
292
293 " SN_SOFO: missing sofofromlen
294 call Spellfile_Test(0z06000000000100, 'E758:')
295
296 " SN_SOFO: missing sofotolen
297 call Spellfile_Test(0z06000000000400016100, 'E758:')
298
299 " SN_SOFO: missing sofoto
300 call Spellfile_Test(0z0600000000050001610000, 'E759:')
301
302 " SN_COMPOUND: compmax is less than 2
303 call Spellfile_Test(0z08000000000101, 'E759:')
304
305 " SN_COMPOUND: missing compsylmax and other options
306 call Spellfile_Test(0z0800000000020401, 'E759:')
307
308 " SN_COMPOUND: missing compoptions
309 call Spellfile_Test(0z080000000005040101, 'E758:')
310
311 " SN_INFO: missing info
312 call Spellfile_Test(0z0F0000000005040101, '')
313
314 " SN_MIDWORD: missing midword
315 call Spellfile_Test(0z0200000000040102, '')
316
317 " SN_MAP: missing midword
318 call Spellfile_Test(0z0700000000040102, '')
319
320 " SN_SYLLABLE: missing SYLLABLE item
321 call Spellfile_Test(0z0900000000040102, '')
322
323 " SN_SYLLABLE: More than SY_MAXLEN size
324 let v = eval('0z090000000022612F' .. repeat('62', 32))
325 call Spellfile_Test(v, '')
326
327 " LWORDTREE: missing
328 call Spellfile_Test(0zFF, 'E758:')
329
330 " LWORDTREE: missing tree node
331 call Spellfile_Test(0zFF00000004, 'E758:')
332
333 " LWORDTREE: missing tree node value
334 call Spellfile_Test(0zFF0000000402, 'E758:')
335
336 " KWORDTREE: missing tree node
337 call Spellfile_Test(0zFF0000000000000004, 'E758:')
338
339 " PREFIXTREE: missing tree node
340 call Spellfile_Test(0zFF000000000000000000000004, 'E758:')
334 341
335 let &rtp = save_rtp 342 let &rtp = save_rtp
336 call delete('Xtest', 'rf') 343 call delete('Xtest', 'rf')
337 endfunc 344 endfunc
338 345
346 " Test for format errors in suggest file
347 func Test_sugfile_format_error()
348 let save_rtp = &rtp
349 call mkdir('Xtest/spell', 'p')
350 let splfile = './Xtest/spell/Xtest.utf-8.spl'
351 let sugfile = './Xtest/spell/Xtest.utf-8.sug'
352
353 " create an empty spell file with a suggest timestamp
354 call writefile(0z56494D7370656C6C320B00000000080000000000000044FF000000000000000000000000, splfile, 'b')
355
356 " 'encoding' is set before each test to clear the previously loaded suggest
357 " file from memory.
358
359 " empty suggest file
360 set encoding=utf-8
361 call writefile([], sugfile)
362 set runtimepath=./Xtest
363 set spelllang=Xtest
364 set spell
365 call assert_fails("let s = spellsuggest('abc')", 'E778:')
366 set nospell spelllang&
367
368 " zero suggest version
369 set encoding=utf-8
370 call writefile(0z56494D73756700, sugfile)
371 set runtimepath=./Xtest
372 set spelllang=Xtest
373 set spell
374 call assert_fails("let s = spellsuggest('abc')", 'E779:')
375 set nospell spelllang&
376
377 " unsupported suggest version
378 set encoding=utf-8
379 call writefile(0z56494D7375671F, sugfile)
380 set runtimepath=./Xtest
381 set spelllang=Xtest
382 set spell
383 call assert_fails("let s = spellsuggest('abc')", 'E780:')
384 set nospell spelllang&
385
386 " missing suggest timestamp
387 set encoding=utf-8
388 call writefile(0z56494D73756701, sugfile)
389 set runtimepath=./Xtest
390 set spelllang=Xtest
391 set spell
392 call assert_fails("let s = spellsuggest('abc')", 'E781:')
393 set nospell spelllang&
394
395 " incorrect suggest timestamp
396 set encoding=utf-8
397 call writefile(0z56494D7375670100000000000000FF, sugfile)
398 set runtimepath=./Xtest
399 set spelllang=Xtest
400 set spell
401 call assert_fails("let s = spellsuggest('abc')", 'E781:')
402 set nospell spelllang&
403
404 " missing suggest wordtree
405 set encoding=utf-8
406 call writefile(0z56494D737567010000000000000044, sugfile)
407 set runtimepath=./Xtest
408 set spelllang=Xtest
409 set spell
410 call assert_fails("let s = spellsuggest('abc')", 'E782:')
411 set nospell spelllang&
412
413 let &rtp = save_rtp
414 call delete('Xtest', 'rf')
415 endfunc
416
417 " Test for using :mkspell to create a spell file from a list of words
418 func Test_wordlist_dic()
419 " duplicate encoding
420 let lines =<< trim [END]
421 # This is an example word list
422
423 /encoding=latin1
424 /encoding=latin1
425 example
426 [END]
427 call writefile(lines, 'Xwordlist.dic')
428 let output = execute('mkspell Xwordlist.spl Xwordlist.dic')
429 call assert_match('Duplicate /encoding= line ignored in Xwordlist.dic line 4: /encoding=latin1', output)
430
431 " multiple encoding for a word
432 let lines =<< trim [END]
433 example
434 /encoding=latin1
435 example
436 [END]
437 call writefile(lines, 'Xwordlist.dic')
438 let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
439 call assert_match('/encoding= line after word ignored in Xwordlist.dic line 2: /encoding=latin1', output)
440
441 " unsupported encoding for a word
442 let lines =<< trim [END]
443 /encoding=Xtest
444 example
445 [END]
446 call writefile(lines, 'Xwordlist.dic')
447 let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
448 call assert_match('Conversion in Xwordlist.dic not supported: from Xtest to utf-8', output)
449
450 " duplicate region
451 let lines =<< trim [END]
452 /regions=usca
453 /regions=usca
454 example
455 [END]
456 call writefile(lines, 'Xwordlist.dic')
457 let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
458 call assert_match('Duplicate /regions= line ignored in Xwordlist.dic line 2: regions=usca', output)
459
460 " maximum regions
461 let lines =<< trim [END]
462 /regions=uscauscauscauscausca
463 example
464 [END]
465 call writefile(lines, 'Xwordlist.dic')
466 let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
467 call assert_match('Too many regions in Xwordlist.dic line 1: uscauscauscauscausca', output)
468
469 " unsupported '/' value
470 let lines =<< trim [END]
471 /test=abc
472 example
473 [END]
474 call writefile(lines, 'Xwordlist.dic')
475 let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
476 call assert_match('/ line ignored in Xwordlist.dic line 1: /test=abc', output)
477
478 " unsupported flag
479 let lines =<< trim [END]
480 example/+
481 [END]
482 call writefile(lines, 'Xwordlist.dic')
483 let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
484 call assert_match('Unrecognized flags in Xwordlist.dic line 1: +', output)
485
486 " non-ascii word
487 call writefile(["ʀʀ"], 'Xwordlist.dic')
488 let output = execute('mkspell! -ascii Xwordlist.spl Xwordlist.dic')
489 call assert_match('Ignored 1 words with non-ASCII characters', output)
490
491 call delete('Xwordlist.spl')
492 call delete('Xwordlist.dic')
493 endfunc
494
495 " Test for the :mkspell command
496 func Test_mkspell()
497 call assert_fails('mkspell Xtest_us.spl', 'E751:')
498 call assert_fails('mkspell a b c d e f g h i j k', 'E754:')
499
500 call writefile([], 'Xtest.spl')
501 call writefile([], 'Xtest.dic')
502 call assert_fails('mkspell Xtest.spl Xtest.dic', 'E13:')
503 call delete('Xtest.spl')
504 call delete('Xtest.dic')
505
506 call mkdir('Xtest.spl')
507 call assert_fails('mkspell! Xtest.spl Xtest.dic', 'E17:')
508 call delete('Xtest.spl', 'rf')
509
510 call assert_fails('mkspell en en_US abc_xyz', 'E755:')
511 endfunc
512
513 func Test_spell_add_word()
514 set spellfile=
515 call assert_fails('spellgood abc', 'E764:')
516
517 set spellfile=Xtest.utf-8.add
518 call assert_fails('2spellgood abc', 'E765:')
519
520 edit Xtest.utf-8.add
521 call setline(1, 'sample')
522 call assert_fails('spellgood abc', 'E139:')
523 set spellfile&
524 %bw!
525 endfunc
526
339 " vim: shiftwidth=2 sts=2 expandtab 527 " vim: shiftwidth=2 sts=2 expandtab