summaryrefslogtreecommitdiffstats
path: root/src/testdir/test_spellfile.vim
blob: 410553daf4fc199c622268e60541b49d463abb5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
" Test for commands that operate on the spellfile.

source shared.vim
source check.vim

CheckFeature spell
CheckFeature syntax

func Test_spell_normal()
  new
  call append(0, ['1 good', '2 goood', '3 goood'])
  set spell spellfile=./Xspellfile.add spelllang=en
  let oldlang=v:lang
  lang C

  " Test for zg
  1
  norm! ]s
  call assert_equal('2 goood', getline('.'))
  norm! zg
  1
  let a=execute('unsilent :norm! ]s')
  call assert_equal('1 good', getline('.'))
  call assert_equal('search hit BOTTOM, continuing at TOP', a[1:])
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('goood', cnt[0])

  " zg should fail in operator-pending mode
  call assert_beeps('norm! czg')

  " zg fails in visual mode when not able to get the visual text
  call assert_beeps('norm! ggVjzg')
  norm! V

  " zg fails for a non-identifier word
  call append(line('$'), '###')
  call assert_fails('norm! Gzg', 'E349:')
  $d

  " Test for zw
  2
  norm! $zw
  1
  norm! ]s
  call assert_equal('2 goood', getline('.'))
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('#oood', cnt[0])
  call assert_equal('goood/!', cnt[1])

  " Test for :spellrare
  spellrare rare
  let cnt=readfile('./Xspellfile.add')
  call assert_equal(['#oood', 'goood/!', 'rare/?'], cnt)

  " Make sure :spellundo works for rare words.
  spellundo rare
  let cnt=readfile('./Xspellfile.add')
  call assert_equal(['#oood', 'goood/!', '#are/?'], cnt)

  " Test for zg in visual mode
  let a=execute('unsilent :norm! V$zg')
  call assert_equal("Word '2 goood' added to ./Xspellfile.add", a[1:])
  1
  norm! ]s
  call assert_equal('3 goood', getline('.'))
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('2 goood', cnt[3])
  " Remove "2 good" from spellfile
  2
  let a=execute('unsilent norm! V$zw')
  call assert_equal("Word '2 goood' added to ./Xspellfile.add", a[1:])
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('2 goood/!', cnt[4])

  " Test for zG
  let a=execute('unsilent norm! V$zG')
  call assert_match("Word '2 goood' added to .*", a)
  let fname=matchstr(a, 'to\s\+\zs\f\+$')
  let cnt=readfile(fname)
  call assert_equal('2 goood', cnt[0])

  " Test for zW
  let a=execute('unsilent norm! V$zW')
  call assert_match("Word '2 goood' added to .*", a)
  let cnt=readfile(fname)
  call assert_equal('# goood', cnt[0])
  call assert_equal('2 goood/!', cnt[1])

  " Test for zuW
  let a=execute('unsilent norm! V$zuW')
  call assert_match("Word '2 goood' removed from .*", a)
  let cnt=readfile(fname)
  call assert_equal('# goood', cnt[0])
  call assert_equal('# goood/!', cnt[1])

  " Test for zuG
  let a=execute('unsilent norm! $zG')
  call assert_match("Word 'goood' added to .*", a)
  let cnt=readfile(fname)
  call assert_equal('# goood', cnt[0])
  call assert_equal('# goood/!', cnt[1])
  call assert_equal('goood', cnt[2])
  let a=execute('unsilent norm! $zuG')
  let cnt=readfile(fname)
  call assert_match("Word 'goood' removed from .*", a)
  call assert_equal('# goood', cnt[0])
  call assert_equal('# goood/!', cnt[1])
  call assert_equal('#oood', cnt[2])
  " word not found in wordlist
  let a=execute('unsilent norm! V$zuG')
  let cnt=readfile(fname)
  call assert_match("", a)
  call assert_equal('# goood', cnt[0])
  call assert_equal('# goood/!', cnt[1])
  call assert_equal('#oood', cnt[2])

  " Test for zug
  call delete('./Xspellfile.add')
  2
  let a=execute('unsilent norm! $zg')
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('goood', cnt[0])
  let a=execute('unsilent norm! $zug')
  call assert_match("Word 'goood' removed from \./Xspellfile.add", a)
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('#oood', cnt[0])
  " word not in wordlist
  let a=execute('unsilent norm! V$zug')
  call assert_match('', a)
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('#oood', cnt[0])

  " Test for zuw
  call delete('./Xspellfile.add')
  2
  let a=execute('unsilent norm! Vzw')
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('2 goood/!', cnt[0])
  let a=execute('unsilent norm! Vzuw')
  call assert_match("Word '2 goood' removed from \./Xspellfile.add", a)
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('# goood/!', cnt[0])
  " word not in wordlist
  let a=execute('unsilent norm! $zug')
  call assert_match('', a)
  let cnt=readfile('./Xspellfile.add')
  call assert_equal('# goood/!', cnt[0])

  " add second entry to spellfile setting
  set spellfile=./Xspellfile.add,./Xspellfile2.add
  call delete('./Xspellfile.add')
  2
  let a=execute('unsilent norm! $2zg')
  let cnt=readfile('./Xspellfile2.add')
  call assert_match("Word 'goood' added to ./Xspellfile2.add", a)
  call assert_equal('goood', cnt[0])

  " Test for :spellgood!
  let temp = execute(':spe!0/0')
  call assert_match('Invalid region', temp)
  let spellfile = matchstr(temp, 'Invalid region nr in \zs.*\ze line \d: 0')
  call assert_equal(['# goood', '# goood/!', '#oood', '0/0'], readfile(spellfile))

  " Test for :spellrare!
  :spellrare! raare
  call assert_equal(['# goood', '# goood/!', '#oood', '0/0', 'raare/?'], readfile(spellfile))
  call delete(spellfile)

  " clean up
  exe "lang" oldlang
  call delete("./Xspellfile.add")
  call delete("./Xspellfile2.add")
  call delete("./Xspellfile.add.spl")
  call delete("./Xspellfile2.add.spl")

  " zux -> no-op
  2
  norm! $zux
  call assert_equal([], glob('Xspellfile.add',0,1))
  call assert_equal([], glob('Xspellfile2.add',0,1))

  set spellfile= spell& spelllang&
  bw!
endfunc

" Spell file content test. Write 'content' to the spell file prefixed by the
" spell file header and then enable spell checking. If 'emsg' is not empty,
" then check for error.
func Spellfile_Test(content, emsg)
  let splfile = './Xtest/spell/Xtest.utf-8.spl'
  " Add the spell file header and version (VIMspell2)
  let v = 0z56494D7370656C6C32 + a:content
  call writefile(v, splfile, 'b')

  " 'encoding' is set before each test to clear the previously loaded suggest
  " file from memory.
  set encoding=utf-8

  set runtimepath=./Xtest
  set spelllang=Xtest
  if a:emsg != ''
    call assert_fails('set spell', a:emsg)
  else
    " FIXME: With some invalid spellfile contents, there are no error
    " messages. So don't know how to check for the test result.
    set spell
  endif
  set nospell spelllang& rtp&
endfunc

" Test for spell file format errors.
" The spell file format is described in spellfile.c
func Test_spellfile_format_error()
  let save_rtp = &rtp
  call mkdir('Xtest/spell', 'p')
  let splfile = './Xtest/spell/Xtest.utf-8.spl'

  " empty spell file
  call writefile([], splfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  call assert_fails('set spell', 'E757:')
  set nospell spelllang&

  " invalid file ID
  call writefile(0z56494D, splfile, 'b')
  set runtimepath=./Xtest
  set spelllang=Xtest
  call assert_fails('set spell', 'E757:')
  set nospell spelllang&

  " missing version number
  call writefile(0z56494D7370656C6C, splfile, 'b')
  set runtimepath=./Xtest
  set spelllang=Xtest
  call assert_fails('set spell', 'E771:')
  set nospell spelllang&

  " invalid version number
  call writefile(0z56494D7370656C6C7A, splfile, 'b')
  set runtimepath=./Xtest
  set spelllang=Xtest
  call assert_fails('set spell', 'E772:')
  set nospell spelllang&

  " no sections
  call Spellfile_Test(0z, 'E758:')

  " missing section length
  call Spellfile_Test(0z00, 'E758:')

  " unsupported required section
  call Spellfile_Test(0z7A0100000004, 'E770:')

  " unsupported not-required section
  call Spellfile_Test(0z7A0000000004, 'E758:')

  " SN_REGION: invalid number of region names
  call Spellfile_Test(0z0000000000FF, 'E759:')

  " SN_CHARFLAGS: missing <charflagslen> length
  call Spellfile_Test(0z010000000004, 'E758:')

  " SN_CHARFLAGS: invalid <charflagslen> length
  call Spellfile_Test(0z0100000000010201, '')

  " SN_CHARFLAGS: charflagslen == 0 and folcharslen != 0
  call Spellfile_Test(0z01000000000400000101, 'E759:')

  " SN_CHARFLAGS: missing <folcharslen> length
  call Spellfile_Test(0z01000000000100, 'E758:')

  " SN_PREFCOND: invalid prefcondcnt
  call Spellfile_Test(0z03000000000100, 'E759:')

  " SN_PREFCOND: invalid condlen
  call Spellfile_Test(0z0300000000020001, 'E759:')

  " SN_REP: invalid repcount
  call Spellfile_Test(0z04000000000100, 'E758:')

  " SN_REP: missing rep
  call Spellfile_Test(0z0400000000020004, 'E758:')

  " SN_REP: zero repfromlen
  call Spellfile_Test(0z040000000003000100, 'E759:')

  " SN_REP: invalid reptolen
  call Spellfile_Test(0z0400000000050001014101, '')

  " SN_REP: zero reptolen
  call Spellfile_Test(0z0400000000050001014100, 'E759:')

  " SN_SAL: missing salcount
  call Spellfile_Test(0z05000000000102, 'E758:')

  " SN_SAL: missing salfromlen
  call Spellfile_Test(0z050000000003080001, 'E758:')

  " SN_SAL: missing saltolen
  call Spellfile_Test(0z0500000000050400010161, 'E758:')

  " SN_WORDS: non-NUL terminated word
  call Spellfile_Test(0z0D000000000376696D, 'E758:')

  " SN_WORDS: very long word
  let v = eval('0z0D000000012C' .. repeat('41', 300))
  call Spellfile_Test(v, 'E759:')

  " SN_SOFO: missing sofofromlen
  call Spellfile_Test(0z06000000000100, 'E758:')

  " SN_SOFO: missing sofotolen
  call Spellfile_Test(0z06000000000400016100, 'E758:')

  " SN_SOFO: missing sofoto
  call Spellfile_Test(0z0600000000050001610000, 'E759:')

  " SN_SOFO: empty sofofrom and sofoto
  call Spellfile_Test(0z06000000000400000000FF000000000000000000000000, '')

  " SN_SOFO: multi-byte characters in sofofrom and sofoto
  call Spellfile_Test(0z0600000000080002CF810002CF82FF000000000000000000000000, '')

  " SN_COMPOUND: compmax is less than 2
  call Spellfile_Test(0z08000000000101, 'E759:')

  " SN_COMPOUND: missing compsylmax and other options
  call Spellfile_Test(0z0800000000020401, 'E759:')

  " SN_COMPOUND: missing compoptions
  call Spellfile_Test(0z080000000005040101, 'E758:')

  " SN_COMPOUND: missing comppattern
  call Spellfile_Test(0z08000000000704010100000001, 'E758:')

  " SN_COMPOUND: incorrect comppatlen
  call Spellfile_Test(0z080000000007040101000000020165, 'E758:')

  " SN_INFO: missing info
  call Spellfile_Test(0z0F0000000005040101, '')

  " SN_MIDWORD: missing midword
  call Spellfile_Test(0z0200000000040102, '')

  " SN_MAP: missing midword
  call Spellfile_Test(0z0700000000040102, '')

  " SN_MAP: empty map string
  call Spellfile_Test(0z070000000000FF000000000000000000000000, '')

  " SN_MAP: duplicate multibyte character
  call Spellfile_Test(0z070000000004DC81DC81, 'E783:')

  " SN_SYLLABLE: missing SYLLABLE item
  call Spellfile_Test(0z0900000000040102, '')

  " SN_SYLLABLE: More than SY_MAXLEN size
  let v = eval('0z090000000022612F' .. repeat('62', 32))
  call Spellfile_Test(v, '')

  " LWORDTREE: missing
  call Spellfile_Test(0zFF, 'E758:')

  " LWORDTREE: missing tree node
  call Spellfile_Test(0zFF00000004, 'E758:')

  " LWORDTREE: missing tree node value
  call Spellfile_Test(0zFF0000000402, 'E758:')

  " LWORDTREE: incorrect sibling node count
  call Spellfile_Test(0zFF00000001040000000000000000, 'E759:')

  " KWORDTREE: missing tree node
  call Spellfile_Test(0zFF0000000000000004, 'E758:')

  " PREFIXTREE: missing tree node
  call Spellfile_Test(0zFF000000000000000000000004, 'E758:')

  " PREFIXTREE: incorrect prefcondnr
  call Spellfile_Test(0zFF000000000000000000000002010200000020, 'E759:')

  " PREFIXTREE: invalid nodeidx
  call Spellfile_Test(0zFF00000000000000000000000201010000, 'E759:')

  let &rtp = save_rtp
  call delete('Xtest', 'rf')
endfunc

" Test for format errors in suggest file
func Test_sugfile_format_error()
  let save_rtp = &rtp
  call mkdir('Xtest/spell', 'p')
  let splfile = './Xtest/spell/Xtest.utf-8.spl'
  let sugfile = './Xtest/spell/Xtest.utf-8.sug'

  " create an empty spell file with a suggest timestamp
  call writefile(0z56494D7370656C6C320B00000000080000000000000044FF000000000000000000000000, splfile, 'b')

  " 'encoding' is set before each test to clear the previously loaded suggest
  " file from memory.

  " empty suggest file
  set encoding=utf-8
  call writefile([], sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E778:')
  set nospell spelllang&

  " zero suggest version
  set encoding=utf-8
  call writefile(0z56494D73756700, sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E779:')
  set nospell spelllang&

  " unsupported suggest version
  set encoding=utf-8
  call writefile(0z56494D7375671F, sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E780:')
  set nospell spelllang&

  " missing suggest timestamp
  set encoding=utf-8
  call writefile(0z56494D73756701, sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E781:')
  set nospell spelllang&

  " incorrect suggest timestamp
  set encoding=utf-8
  call writefile(0z56494D7375670100000000000000FF, sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E781:')
  set nospell spelllang&

  " missing suggest wordtree
  set encoding=utf-8
  call writefile(0z56494D737567010000000000000044, sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E782:')
  set nospell spelllang&

  " invalid suggest word count in SUGTABLE
  set encoding=utf-8
  call writefile(0z56494D7375670100000000000000440000000022, sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E782:')
  set nospell spelllang&

  " missing sugline in SUGTABLE
  set encoding=utf-8
  call writefile(0z56494D7375670100000000000000440000000000000005, sugfile)
  set runtimepath=./Xtest
  set spelllang=Xtest
  set spell
  call assert_fails("let s = spellsuggest('abc')", 'E782:')
  set nospell spelllang&

  let &rtp = save_rtp
  call delete('Xtest', 'rf')
endfunc

" Test for using :mkspell to create a spell file from a list of words
func Test_wordlist_dic()
  " duplicate encoding
  let lines =<< trim [END]
    # This is an example word list

    /encoding=latin1
    /encoding=latin1
    example
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell Xwordlist.spl Xwordlist.dic')
  call assert_match('Duplicate /encoding= line ignored in Xwordlist.dic line 4: /encoding=latin1', output)

  " multiple encoding for a word
  let lines =<< trim [END]
    example
    /encoding=latin1
    example
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
  call assert_match('/encoding= line after word ignored in Xwordlist.dic line 2: /encoding=latin1', output)

  " unsupported encoding for a word
  let lines =<< trim [END]
    /encoding=Xtest
    example
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
  call assert_match('Conversion in Xwordlist.dic not supported: from Xtest to utf-8', output)

  " duplicate region
  let lines =<< trim [END]
    /regions=usca
    /regions=usca
    example
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
  call assert_match('Duplicate /regions= line ignored in Xwordlist.dic line 2: regions=usca', output)

  " maximum regions
  let lines =<< trim [END]
    /regions=uscauscauscauscausca
    example
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
  call assert_match('Too many regions in Xwordlist.dic line 1: uscauscauscauscausca', output)

  " unsupported '/' value
  let lines =<< trim [END]
    /test=abc
    example
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
  call assert_match('/ line ignored in Xwordlist.dic line 1: /test=abc', output)

  " unsupported flag
  let lines =<< trim [END]
    example/+
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
  call assert_match('Unrecognized flags in Xwordlist.dic line 1: +', output)

  " non-ascii word
  call writefile(["ʀʀ"], 'Xwordlist.dic')
  let output = execute('mkspell! -ascii Xwordlist.spl Xwordlist.dic')
  call assert_match('Ignored 1 words with non-ASCII characters', output)

  " keep case of a word
  let lines =<< trim [END]
    example/=
  [END]
  call writefile(lines, 'Xwordlist.dic')
  let output = execute('mkspell! Xwordlist.spl Xwordlist.dic')
  call assert_match('Compressed keep-case:', output)

  call delete('Xwordlist.spl')
  call delete('Xwordlist.dic')
endfunc

" Test for the :mkspell command
func Test_mkspell()
  call assert_fails('mkspell Xtest_us.spl', 'E751:')
  call assert_fails('mkspell Xtest.spl abc', 'E484:')
  call assert_fails('mkspell a b c d e f g h i j k', 'E754:')

  " create a .aff file but not the .dic file
  call writefile([], 'Xtest.aff')
  call assert_fails('mkspell Xtest.spl Xtest', 'E484:')
  call delete('Xtest.aff')

  call writefile([], 'Xtest.spl')
  call writefile([], 'Xtest.dic')
  call assert_fails('mkspell Xtest.spl Xtest.dic', 'E13:')
  call delete('Xtest.spl')
  call delete('Xtest.dic')

  call mkdir('Xtest.spl')
  call assert_fails('mkspell! Xtest.spl Xtest.dic', 'E17:')
  call delete('Xtest.spl', 'rf')

  call assert_fails('mkspell en en_US abc_xyz', 'E755:')
endfunc

" Tests for :mkspell with a .dic and .aff file
func Test_aff_file_format_error()
  " FIXME: For some reason, the :mkspell command below doesn't fail on the
  " MS-Windows CI build. Disable this test on MS-Windows for now.
  CheckNotMSWindows

  " No word count in .dic file
  call writefile([], 'Xtest.dic')
  call writefile([], 'Xtest.aff')
  call assert_fails('mkspell! Xtest.spl Xtest', 'E760:')

  " create a .dic file for the tests below
  call writefile(['1', 'work'], 'Xtest.dic')

  " Invalid encoding in .aff file
  call writefile(['# comment', 'SET Xinvalidencoding'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Conversion in Xtest.aff not supported: from xinvalidencoding', output)

  " Invalid flag in .aff file
  call writefile(['FLAG xxx'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Invalid value for FLAG in Xtest.aff line 1: xxx', output)

  " set FLAGS after using flag for an affix
  call writefile(['SFX L Y 1', 'SFX L 0 re [^x]', 'FLAG long'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('FLAG after using flags in Xtest.aff line 3: long', output)

  " INFO in affix file
  let save_encoding = &encoding
  call mkdir('Xrtp/spell', 'p')
  call writefile(['1', 'work'], 'Xrtp/spell/Xtest.dic')
  call writefile(['NAME klingon', 'VERSION 1.4', 'AUTHOR Spock'],
        \ 'Xrtp/spell/Xtest.aff')
  silent mkspell! Xrtp/spell/Xtest.utf-8.spl Xrtp/spell/Xtest
  let save_rtp = &rtp
  set runtimepath=./Xrtp
  set spelllang=Xtest
  set spell
  let output = split(execute('spellinfo'), "\n")
  call assert_equal("NAME klingon", output[1])
  call assert_equal("VERSION 1.4", output[2])
  call assert_equal("AUTHOR Spock", output[3])
  let &rtp = save_rtp
  call delete('Xrtp', 'rf')
  set spell& spelllang& spellfile&
  %bw!
  " 'encoding' must be set again to clear the spell file in memory
  let &encoding = save_encoding

  " COMPOUNDFORBIDFLAG flag after PFX in an affix file
  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'COMPOUNDFLAG c', 'COMPOUNDFORBIDFLAG x'],
        \ 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Defining COMPOUNDFORBIDFLAG after PFX item may give wrong results in Xtest.aff line 4', output)

  " COMPOUNDPERMITFLAG flag after PFX in an affix file
  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'COMPOUNDPERMITFLAG c'],
        \ 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Defining COMPOUNDPERMITFLAG after PFX item may give wrong results in Xtest.aff line 3', output)

  " Wrong COMPOUNDRULES flag value in an affix file
  call writefile(['COMPOUNDRULES a'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Wrong COMPOUNDRULES value in Xtest.aff line 1: a', output)

  " Wrong COMPOUNDWORDMAX flag value in an affix file
  call writefile(['COMPOUNDWORDMAX 0'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Wrong COMPOUNDWORDMAX value in Xtest.aff line 1: 0', output)

  " Wrong COMPOUNDMIN flag value in an affix file
  call writefile(['COMPOUNDMIN 0'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Wrong COMPOUNDMIN value in Xtest.aff line 1: 0', output)

  " Wrong COMPOUNDSYLMAX flag value in an affix file
  call writefile(['COMPOUNDSYLMAX 0'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Wrong COMPOUNDSYLMAX value in Xtest.aff line 1: 0', output)

  " Wrong CHECKCOMPOUNDPATTERN flag value in an affix file
  call writefile(['CHECKCOMPOUNDPATTERN 0'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Wrong CHECKCOMPOUNDPATTERN value in Xtest.aff line 1: 0', output)

  " Duplicate affix entry in an affix file
  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'PFX L Y 1', 'PFX L 0 re x'],
        \ 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Duplicate affix in Xtest.aff line 3: L', output)

  " Duplicate affix entry in an affix file
  call writefile(['PFX L Y 1', 'PFX L Y 1'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Unrecognized or duplicate item in Xtest.aff line 2: PFX', output)

  " Different combining flags in an affix file
  call writefile(['PFX L Y 1', 'PFX L 0 re x', 'PFX L N 1'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Different combining flag in continued affix block in Xtest.aff line 3', output)

  " Try to reuse a affix used for BAD flag
  call writefile(['BAD x', 'PFX x Y 1', 'PFX x 0 re x'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Affix also used for BAD/RARE/KEEPCASE/NEEDAFFIX/NEEDCOMPOUND/NOSUGGEST in Xtest.aff line 2: x', output)

  " Trailing characters in an affix entry
  call writefile(['PFX L Y 1 Test', 'PFX L 0 re x'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Trailing text in Xtest.aff line 1: Test', output)

  " Trailing characters in an affix entry
  call writefile(['PFX L Y 1', 'PFX L 0 re x Test'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Trailing text in Xtest.aff line 2: Test', output)

  " Incorrect combine flag in an affix entry
  call writefile(['PFX L X 1', 'PFX L 0 re x'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Expected Y or N in Xtest.aff line 1: X', output)

  " Invalid count for REP item
  call writefile(['REP a'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Expected REP(SAL) count in Xtest.aff line 1', output)

  " Trailing characters in REP item
  call writefile(['REP 1', 'REP f ph test'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Trailing text in Xtest.aff line 2: test', output)

  " Invalid count for MAP item
  call writefile(['MAP a'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Expected MAP count in Xtest.aff line 1', output)

  " Duplicate character in a MAP item
  call writefile(['MAP 2', 'MAP xx', 'MAP yy'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Duplicate character in MAP in Xtest.aff line 2', output)

  " Use COMPOUNDSYLMAX without SYLLABLE
  call writefile(['COMPOUNDSYLMAX 2'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('COMPOUNDSYLMAX used without SYLLABLE', output)

  " Missing SOFOTO
  call writefile(['SOFOFROM abcdef'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Missing SOFOTO line in Xtest.aff', output)

  " Length of SOFOFROM and SOFOTO differ
  call writefile(['SOFOFROM abcde', 'SOFOTO ABCD'], 'Xtest.aff')
  call assert_fails('mkspell! Xtest.spl Xtest', 'E759:')

  " Both SAL and SOFOFROM/SOFOTO items
  call writefile(['SOFOFROM abcd', 'SOFOTO ABCD', 'SAL CIA X'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Both SAL and SOFO lines in Xtest.aff', output)

  " use an alphabet flag when FLAG is num
  call writefile(['FLAG num', 'SFX L Y 1', 'SFX L 0 re [^x]'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Flag is not a number in Xtest.aff line 2: L', output)

  " use number and alphabet flag when FLAG is num
  call writefile(['FLAG num', 'SFX 4f Y 1', 'SFX 4f 0 re [^x]'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Affix name too long in Xtest.aff line 2: 4f', output)

  " use a single character flag when FLAG is long
  call writefile(['FLAG long', 'SFX L Y 1', 'SFX L 0 re [^x]'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Illegal flag in Xtest.aff line 2: L', output)

  " missing character in UPP entry. The character table is used only in a
  " non-utf8 encoding
  call writefile(['FOL abc', 'LOW abc', 'UPP A'], 'Xtest.aff')
  let save_encoding = &encoding
  set encoding=cp949
  call assert_fails('mkspell! Xtest.spl Xtest', 'E761:')
  let &encoding = save_encoding

  " character range doesn't match between FOL and LOW entries
  call writefile(["FOL \u0102bc", 'LOW abc', 'UPP ABC'], 'Xtest.aff')
  let save_encoding = &encoding
  set encoding=cp949
  call assert_fails('mkspell! Xtest.spl Xtest', 'E762:')
  let &encoding = save_encoding

  " character range doesn't match between FOL and UPP entries
  call writefile(["FOL \u0102bc", "LOW \u0102bc", 'UPP ABC'], 'Xtest.aff')
  let save_encoding = &encoding
  set encoding=cp949
  call assert_fails('mkspell! Xtest.spl Xtest', 'E762:')
  let &encoding = save_encoding

  " additional characters in LOW and UPP entries
  call writefile(["FOL ab", "LOW abc", 'UPP ABC'], 'Xtest.aff')
  let save_encoding = &encoding
  set encoding=cp949
  call assert_fails('mkspell! Xtest.spl Xtest', 'E761:')
  let &encoding = save_encoding

  " missing UPP entry
  call writefile(["FOL abc", "LOW abc"], 'Xtest.aff')
  let save_encoding = &encoding
  set encoding=cp949
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('Missing FOL/LOW/UPP line in Xtest.aff', output)
  let &encoding = save_encoding

  " duplicate word in the .dic file
  call writefile(['2', 'good', 'good', 'good'], 'Xtest.dic')
  call writefile(['NAME vim'], 'Xtest.aff')
  let output = execute('mkspell! Xtest.spl Xtest')
  call assert_match('First duplicate word in Xtest.dic line 3: good', output)
  call assert_match('2 duplicate word(s) in Xtest.dic', output)

  " use multiple .aff files with different values for COMPOUNDWORDMAX and
  " MIDWORD (number and string)
  call writefile(['1', 'world'], 'Xtest_US.dic')
  call writefile(['1', 'world'], 'Xtest_CA.dic')
  call writefile(["COMPOUNDWORDMAX 3", "MIDWORD '-"], 'Xtest_US.aff')
  call writefile(["COMPOUNDWORDMAX 4", "MIDWORD '="], 'Xtest_CA.aff')
  let output = execute('mkspell! Xtest.spl Xtest_US Xtest_CA')
  call assert_match('COMPOUNDWORDMAX value differs from what is used in another .aff file', output)
  call assert_match('MIDWORD value differs from what is used in another .aff file', output)
  call delete('Xtest_US.dic')
  call delete('Xtest_CA.dic')
  call delete('Xtest_US.aff')
  call delete('Xtest_CA.aff')

  call delete('Xtest.dic')
  call delete('Xtest.aff')
  call delete('Xtest.spl')
  call delete('Xtest.sug')
endfunc

func Test_spell_add_word()
  set spellfile=
  call assert_fails('spellgood abc', 'E764:')

  set spellfile=Xtest.utf-8.add
  call assert_fails('2spellgood abc', 'E765:')

  edit Xtest.utf-8.add
  call setline(1, 'sample')
  call assert_fails('spellgood abc', 'E139:')
  set spellfile&
  %bw!
endfunc

" When 'spellfile' is not set, adding a new good word will automatically set
" the 'spellfile'
func Test_init_spellfile()
  let save_rtp = &rtp
  let save_encoding = &encoding
  call mkdir('Xrtp/spell', 'p')
  call writefile(['vim'], 'Xrtp/spell/Xtest.dic')
  silent mkspell Xrtp/spell/Xtest.utf-8.spl Xrtp/spell/Xtest.dic
  set runtimepath=./Xrtp
  set spelllang=Xtest
  set spell
  silent spellgood abc
  call assert_equal('./Xrtp/spell/Xtest.utf-8.add', &spellfile)
  call assert_equal(['abc'], readfile('Xrtp/spell/Xtest.utf-8.add'))
  call assert_true(filereadable('Xrtp/spell/Xtest.utf-8.spl'))
  set spell& spelllang& spellfile&
  call delete('Xrtp', 'rf')
  let &encoding = save_encoding
  let &rtp = save_rtp
  %bw!
endfunc

" Test for the 'mkspellmem' option
func Test_mkspellmem_opt()
  call assert_fails('set mkspellmem=1000', 'E474:')
  call assert_fails('set mkspellmem=1000,', 'E474:')
  call assert_fails('set mkspellmem=1000,50', 'E474:')
  call assert_fails('set mkspellmem=1000,50,', 'E474:')
  call assert_fails('set mkspellmem=1000,50,10,', 'E474:')
  call assert_fails('set mkspellmem=1000,50,0', 'E474:')
endfunc

" vim: shiftwidth=2 sts=2 expandtab