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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
|
;;; tests-ispell.el --- Test ispell.el. -*- lexical-binding: t; -*-
;; Copyright (C) 2025 Lockywolf
;; Author: Lockywolf <for_emacs_1@lockywolf.net>
;; Keywords: languages, text
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Tests for ispell.el.
;;; Code:
(require 'ert)
(require 'ert-x)
(require 'ispell)
(eval-and-compile
(let ((load-path (cons (file-name-directory
(or (macroexp-file-name) load-file-name))
load-path)))
(require 'ispell-tests-common)))
(defconst ispell-tests-emacs-binary-path
(concat invocation-directory invocation-name))
(defun warnings-buffer-exists-p ()
"Check if a buffer named \"*Warnings*\" exists."
(if (get-buffer "*Warnings*")
t
nil))
(ert-deftest ispell/ispell-program-name/nil ()
"Sanity check. Setting a non-string should produce a warning.
Give `ispell-program-name' a wrong type."
(should
(unwind-protect
(progn
(setq ispell-program-name "ispell")
(when (warnings-buffer-exists-p)
(kill-buffer "*Warnings*"))
(setopt ispell-program-name nil)
(if (warnings-buffer-exists-p)
t
nil))
(when (warnings-buffer-exists-p)
(kill-buffer "*Warnings*")))))
(ert-deftest ispell/ispell-program-name/noncommand ()
"Should error or at least warn when `ispell-program-name' is a meaningless string."
:expected-result :failed
(should-error
(setopt ispell-program-name "6c628ac4-63a0-11f0-b37c-e38fc166e3fc") ;; random ispellnonexistent name
))
(ert-deftest ispell/ispell-program-name/noncommand/interactive ()
"Should error or at least warn when `ispell-program-name' is a meaningless string."
(should-error
(progn
(setopt ispell-program-name "baf8cc1a-a811-11f0-abc7-ef02b7a6dda1") ;; random ispellnonexistent name
(ispell-check-version)
)))
(ert-deftest ispell/ispell-program-name/non-executable ()
"Sanity check. Should error or at least warn.
Give `ispell-program-name' a path to a non-executable.
I personally think that this should always fail, but
at the moment only the interactive call fails."
:expected-result :failed
(should-error
(progn
(setopt ispell-program-name null-device))))
(ert-deftest ispell/ispell-program-name/non-executable/interactive ()
"Should error or at least warn if `ispell-program-name' is a path to a non-executable."
(should-error
(progn
(setopt ispell-program-name null-device)
(ispell-check-version t))))
(ert-deftest ispell/ispell-program-name/non-spellchecker ()
"Give `ispell-program-name' a path to a non-spellchecker.
Fails because for non-interactive runs, `ispell-check-version' does
not actually err."
:expected-result :failed
(skip-unless (executable-find "etags"))
(should-error (string-equal "etags" (setopt ispell-ispell-program "etags"))))
(ert-deftest ispell/ispell-program-name/non-spellchecker/interactive ()
"Give `ispell-program-name' a path to a non-spellchecker."
(skip-unless (executable-find "etags"))
(should-error
(progn (setopt ispell-ispell-program "etags")
(ispell-check-version t))
))
(ert-deftest ispell/ispell-program-name/ispell ()
"Sanity check. If at least some ispell is available, should pass.
Give `ispell-program-name' a real spellchecker."
(skip-unless (and (executable-find "ispell")
(with-temp-buffer
(call-process "ispell" nil t nil "-vv")
(search-backward "Ispell"))))
;; should not throw
(should (string-equal "ispell" (setopt ispell-ispell-program "ispell"))))
(ert-deftest ispell/ispell-with-safe-default-directory/bad ()
"Try doing something with a bad default directory."
(should (with-temp-buffer
(let ((default-directory "c296752a-7d7b-4769-a2d4-4bfd96c7ca71"))
(ispell-with-safe-default-directory
(equal default-directory (expand-file-name "~/")))))))
(ert-deftest ispell/ispell-with-safe-default-directory/good ()
"Try doing something with a bad default directory."
(should (with-temp-buffer
(let ((default-directory temporary-file-directory))
(ispell-with-safe-default-directory
(equal default-directory temporary-file-directory))))))
(ert-deftest ispell/ispell-call-process/simple ()
"Check that `ispell-call-process' works.
This test fails, because HOME is not defined.
This should not be the case, because `ispell-call-process'
would be making sure that the directory for running
the backend's process exists."
:expected-result :failed
(should
(with-temp-buffer
(let ((default-directory "86e44985-cfba-43ba-98dc-73be46addbc2"))
(ispell-call-process ispell-tests-emacs-binary-path nil t nil "--batch" "-Q" "--eval" "(progn (message default-directory) (kill-emacs))")
(search-backward (expand-file-name "~"))))))
(ert-deftest ispell/ispell-call-process/simple-writable ()
"Check that `ispell-call-process' works."
(should
(with-temp-buffer
(let ((default-directory temporary-file-directory))
(ispell-call-process
ispell-tests-emacs-binary-path nil t nil "--batch" "-Q"
"--eval" "(message \"%s\" (expand-file-name default-directory))")
(search-backward (directory-file-name temporary-file-directory))))))
(ert-deftest ispell/ispell-call-process-region/cat-empty ()
"Check `ispell-call-process-region' works with unrelated process.
This test is expected to fail, because at the moment, there is
a construction (let ((default-directory `default-directory'))...) in
the `ispell-with-safe-default-directory' function, which effectively
makes it useless."
:expected-result :failed
(should
(with-temp-buffer
(let* ((string-to-send "")
(dir (concat temporary-file-directory
"86e44985-cfba-43ba-98dc-73be46addbc2")))
(make-directory dir t)
(chmod dir 000)
(let ((default-directory dir))
;; (ispell-call-process-region string-to-send nil "cat" nil t nil)
(ispell-call-process-region ispell-tests-emacs-binary-path nil t nil "--batch" "-Q" "--eval" "(progn (setq this-read (ignore-errors (read-from-minibuffer \"\"))) (message \"%s\" this-read))")
;; emacs --batch --eval '(progn (setq this-read (ignore-errors (read-from-minibuffer ""))) (message "%s" this-read))'
(equal (buffer-string) string-to-send))))))
(ert-deftest ispell/ispell-call-process-region/cat-random ()
"Check `ispell-call-process-region' works with unrelated process.
This test is expected to fail, because at the moment, there is
a construction (let ((default-directory `default-directory'))...) in
the `ispell-with-safe-default-directory' function, which effectively
makes it useless."
:expected-result :failed
(should
(with-temp-buffer
(let ((string-to-send (format "%s" (random)))
(default-directory "86e44985-cfba-43ba-98dc-73be46addbc2"))
(ispell-call-process-region ispell-tests-emacs-binary-path nil t nil "--batch" "-Q" "--eval" "(progn (setq this-read (ignore-errors (read-from-minibuffer \"\"))) (message \"%s\" this-read))")
(equal (buffer-string) string-to-send)))))
(ert-deftest ispell/ispell-create-debug-buffer ()
"Make sure that debug buffer creation works."
(when (bufferp (get-buffer "*ispell-debug*"))
(with-current-buffer "*ispell-debug*"
(rename-buffer "*ispell-debug*-test")))
(unwind-protect
(progn
(ispell-create-debug-buffer)
(should (bufferp (get-buffer "*ispell-debug*")))
(kill-buffer "*ispell-debug*") ;; should not error
)
(when (bufferp (get-buffer "*ispell-debug*-test"))
(with-current-buffer "*ispell-debug*-test"
(rename-buffer "*ispell-debug*"))))
)
;; FIXME: this test should probably go into a separate file, dedicated
;; to the hunspell backend, but so far there is not partition between
;; backends, so let us add it here. It is easy to move it.
(ert-deftest ispell/ispell-valid-dictionary-list/hunspell/no-library-directory ()
"If hunspell, `ispell-valid-dictionary-list' returns default.
This function only works for aspell and ispell, for hunspell and
enchant-2 it always returns either default or everything.
I think this is an issue in itself, but this test is added to verify
that changes to third-party code do not break existing behavior."
(skip-unless (executable-find "hunspell"))
(skip-unless (equal 0 (call-process "hunspell" nil nil nil)))
(ispell-tests--letopt
((ispell-program-name "hunspell")
(ispell-library-directory nil))
(ispell-check-version t)
(should
(equal
(sort (ispell-valid-dictionary-list) 'string<)
(sort (cl-substitute "default" nil (mapcar #'car ispell-dictionary-alist)) 'string<))))
)
;; FIXME:
;; `ispell-valid-dictionary-list' should be calling backend-specific code,
;; and not check all possible backends by itself.
(ert-deftest ispell/ispell-valid-dictionary-list/hunspell/library-directory ()
"If hunspell, `ispell-valid-dictionary-list' returns default.
This function only works for aspell and ispell, for hunspell and
enchant-2 it always returns either default or everything.
I think this is an issue in itself, but this test is added to verify
that changes to third-party code do not break existing behavior."
(skip-unless (executable-find "hunspell"))
(skip-unless (equal 0 (call-process "hunspell" nil nil nil)))
(ispell-tests--letopt
((ispell-program-name "hunspell")
(ispell-library-directory temporary-file-directory))
(ispell-check-version t)
(should
(equal
(ispell-valid-dictionary-list)
'("default"))))
)
;; FIXME:
;; `ispell-valid-dictionary-list' should be calling backend-specific code,
;; and not check all possible backends by itself.
(ert-deftest ispell/ispell-valid-dictionary-list/enchant-2/no-library-directory ()
"If enchant-2, `ispell-valid-dictionary-list' returns default.
This function only works for aspell and ispell, for hunspell and
enchant-2 it always returns either default or everything.
I think this is an issue in itself, but this test is added to verify
that changes to third-party code do not break existing behavior."
(skip-unless (executable-find "enchant-2"))
(ispell-tests--letopt
((ispell-program-name "enchant-2")
(ispell-library-directory nil))
(ispell-check-version t)
(should
(equal
(sort (ispell-valid-dictionary-list) 'string<)
(sort (cl-substitute "default" nil (mapcar #'car ispell-dictionary-alist)) 'string<))))
)
;; FIXME:
;; `ispell-valid-dictionary-list' should be calling backend-specific code,
;; and not check all possible backends by itself.
(ert-deftest ispell/ispell-valid-dictionary-list/enchant-2/library-directory ()
"If enchant-2, `ispell-valid-dictionary-list' returns default.
This function only works for aspell and ispell, for hunspell and
enchant-2 it always returns either default or everything.
I think this is an issue in itself, but this test is added to verify
that changes to third-party code do not break existing behavior."
(skip-unless (executable-find "enchant-2"))
(ispell-tests--letopt
((ispell-program-name "enchant-2")
(ispell-library-directory temporary-file-directory))
(ispell-check-version t)
(should
(equal
(ispell-valid-dictionary-list)
'("default"))))
)
(ert-deftest ispell/ispell-valid-dictionary-list/international-ispell ()
"Check that `ispell-valid-dictionary-list' does something useful for ispell.
For ispell, `ispell-valid-dictionary-list' checks that a corresponding
file is present in `ispell-library-directory'."
(skip-unless (executable-find "ispell"))
(skip-unless (let ((libdir (with-temp-buffer
(call-process "ispell" nil t nil "-vv")
(goto-char (point-min))
(when (re-search-forward
"LIBDIR *= *\"\\([^\"]+\\)\"" nil t)
(match-string 1)))))
(file-readable-p (expand-file-name "english.hash" libdir))))
(ispell-tests--letopt
((ispell-library-directory ispell-library-directory)
(ispell-local-dictionary-alist ispell-local-dictionary-alist)
(ispell-program-name "ispell"))
(setopt ispell-program-name "ispell") ;; this should set ispell-library-directory
(ispell-check-version t) ;; sets ispell-library-directory
(should (not (null ispell-library-directory)))
(setopt ispell-local-dictionary-alist
'(("english" "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil utf-8)))
(should (member "english" (ispell-valid-dictionary-list)))
(should (member "default" (ispell-valid-dictionary-list))))
)
(ert-deftest ispell/ispell-valid-dictionary-list/aspell ()
"Check that `ispell-valid-dictionary-list' does something useful for aspell.
For aspell, `ispell-valid-dictionary-list' computes an intersection of
`ispell-dictionary-alist' and `ispell--aspell-found-dictionaries'."
(skip-unless (executable-find "aspell"))
(skip-unless (with-temp-buffer
(call-process "aspell" nil t nil "dicts")
(> (length (buffer-string)) 2)))
(skip-unless
(with-temp-buffer
(call-process "aspell" nil t nil "dicts")
(goto-char 1)
(let ((ht (make-hash-table :test 'equal))
(has-duplicates-p nil))
(while
(progn
(let* ((l (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(_ (setf has-duplicates-p (hash-table-contains-p l ht)))
(_ (puthash l t ht))
(endfile (forward-line)))
(and (not has-duplicates-p) (= 0 endfile))))
t)
(when has-duplicates-p
(message "Your aspell setup is unconventional: %s Cannot run tests."
"it has duplicate dictionaries. "))
(not has-duplicates-p))))
(let ((old-ispell ispell-program-name)
(old-library-directory ispell-library-directory)
(old-ispell-local-dictionary-alist ispell-local-dictionary-alist)
(old-ispell-dictionary-alist ispell-dictionary-alist))
(unwind-protect
(progn
(setopt ispell-program-name "aspell") ;; this should set ispell-library-directory
(ispell-check-version t) ;; sets ispell-library-directory
(setopt ispell-local-dictionary-alist
'(("english" "[A-Za-z]" "[^A-Za-z]" "[']" nil ("-B") nil utf-8)))
(should
(> (length (ispell-valid-dictionary-list))
(length ispell--aspell-found-dictionaries))))
(setopt ispell-library-directory old-library-directory)
(setopt ispell-dictionary-alist old-ispell-dictionary-alist)
(setopt ispell-program-name old-ispell)
(setopt ispell-local-dictionary-alist old-ispell-local-dictionary-alist))))
;; Adding file-local words into the file. (They are _not_ sent to the
;; backend in this function.)
(ert-deftest ispell/ispell-add-per-file-word-list/simple ()
"Adding a per-file word to an empty buffer.
No comment syntax expected."
(with-temp-buffer
(let ((testword (format "%s" (random))))
(ispell-add-per-file-word-list testword)
(should (equal (buffer-string)
(concat "
" ispell-words-keyword " " testword "
"))))))
(ert-deftest ispell/ispell-add-per-file-word-list/comments ()
"Adding a per-file word to an empty buffer.
Uses default emacs-lisp comment syntax."
(with-temp-buffer
(let ((testword (format "%s" (random))))
(emacs-lisp-mode)
(ispell-add-per-file-word-list testword)
(should (equal (buffer-string)
(concat "
; " ispell-words-keyword " " testword "
"))))))
(ert-deftest ispell/ispell-add-per-file-word-list/nxml ()
"Adding a per-file word to an empty buffer.
Uses default
xml comment syntax, which has an opening and a closing marker."
(with-temp-buffer
(let ((testword (format "%s" (random))))
(nxml-mode)
(ispell-add-per-file-word-list testword)
(should (equal (buffer-string)
(concat "
<!--
" ispell-words-keyword " " testword "
-->
"))))))
(ert-deftest ispell/ispell-add-per-file-word-list/keyword-there-space ()
"Adding a per-file word to buffer with keyword.
Uses default
xml comment syntax, which has an opening and a closing marker."
(with-temp-buffer
(let ((testword (format "%s" (random))))
(nxml-mode)
(insert "
<!-- " ispell-words-keyword "
-->
")
(ispell-add-per-file-word-list testword)
(should (equal (buffer-string)
(concat "
<!-- " ispell-words-keyword " " testword "
-->
"))))))
(ert-deftest ispell/ispell-add-per-file-word-list/longline ()
"Adding a per-file word to buffer with keyword.
Uses default
xml comment syntax, which has an opening and a closing marker.
This test fails, because ispell.el does not work well with
nXML comments."
(ispell-tests--letopt ((ispell-program-name "ispell"))
(with-temp-buffer
(let* ((testword (format "%s" (random)))
(fill-column 50))
(nxml-mode)
(insert "
<!-- " ispell-words-keyword (make-string fill-column ?a) "
-->
")
(ispell-add-per-file-word-list testword)
(should (equal (buffer-string)
(concat "
<!-- " ispell-words-keyword (make-string fill-column ?a)
"
" ispell-words-keyword " " testword "
-->
")))))))
;; Adding file-local words from the file's cellar into the backend
;; (@-prefixed, see *man ispell*). (They _are_ sent to the backend in
;; this function.)
(ert-deftest ispell/ispell-buffer-local-words/ispell-words-keyword ()
"Send some words prefixed by @ from the file's footer to backend.
Should pass regardless of the backend and the dictionary, because
presumably nobody will have (`hellooooooo')
`ispell-tests--constants/english/wrong' in their dictionary."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(ispell-tests--letopt ((ispell-program-name (ispell-tests--some-backend)))
(ispell-tests--with-ispell-global-dictionary
nil
(with-temp-buffer
(nxml-mode)
(ignore-errors (ispell-kill-ispell))
(ispell-init-process)
(let ((test-output (ispell--run-on-word ispell-tests--constants/english/wrong)))
(should (listp test-output))
(should-not (equal t test-output)))
(ispell-add-per-file-word-list ispell-tests--constants/english/wrong)
(ispell-buffer-local-words)
(should (equal t (ispell--run-on-word ispell-tests--constants/english/wrong)))))))
)
(ert-deftest
ispell/ispell-buffer-local-words/ispell-buffer-session-localwords ()
"Send some words prefixed by @ from the file's footer to backend.
Should pass regardless of the backend and the dictionary, because
presumably nobody will have `ispell-tests--constants/english/wrong'
\(`hellooooooo') in their dictionary."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(ispell-tests--letopt ((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary nil))
(ispell-tests--with-ispell-global-dictionary
nil
(cd temporary-file-directory)
(with-temp-buffer
(nxml-mode)
(ignore-errors (ispell-kill-ispell))
(ispell-init-process)
(let ((test-output (ispell--run-on-word ispell-tests--constants/english/wrong)))
(should (listp test-output))
(should-not (equal t test-output)))
(let ((ispell-buffer-session-localwords (list ispell-tests--constants/english/wrong)))
(ispell-buffer-local-words)
(should (equal t (ispell--run-on-word ispell-tests--constants/english/wrong))))))))
)
(ert-deftest ispell/ispell-init-process/works-no-home ()
"Simple test to check that `ispell-init-process' works."
:expected-result :failed
(skip-unless (ispell-tests--some-backend-available-p))
(ispell-tests--with-ispell-global-dictionary nil
(ispell-tests--letopt ((ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(with-environment-variables
(("HOME" (make-temp-name temporary-file-directory)))
(ispell-init-process))))
'passed)
)
(ert-deftest ispell/ispell-init-process/works-with-home ()
"Simple test to check that `ispell-init-process' works."
(skip-unless (ispell-tests--some-backend-available-p))
(ispell-tests--letopt ((ispell-program-name (ispell-tests--some-backend)))
(ispell-tests--with-ispell-global-dictionary
nil
(with-temp-buffer
(with-environment-variables (("HOME" temporary-file-directory))
(ispell-init-process))))))
;; Some more tests for buffer-local stuff.
;; `ispell-buffer-local-dict'
(let ((possible-pdict-paths (list "/tmp/lispellnonexistent.txt"
"Q:\\ispellnonexistent\\ispellnonexistent.pdict"
"https://example.text"
"(my-favourite-function)"
(format "%s" (random))
(expand-file-name
(format "%s" (random))
temporary-file-directory))))
(ert-deftest ispell/ispell-buffer-local-dict/no-reload+no-overridden ()
"ispell.el can recognize keyword-defined dictionary and keyword-defined
personal-dictionary."
(with-temp-buffer
(nxml-mode)
(let ((test-dict "ispellnonexistent"))
(seq-map (lambda (test-pdict)
(insert
(car ispell-tests--constants/english/correct-list) "\n\n\n"
"<!-- " ispell-dictionary-keyword test-dict " -->"
"<!-- " ispell-pdict-keyword test-pdict " -->")
(ispell-buffer-local-dict t)
(should (equal ispell-local-dictionary test-dict))
(should (equal ispell-local-pdict test-pdict)))
possible-pdict-paths))))
(ert-deftest ispell/ispell-buffer-local-dict/reload+no-overridden ()
"ispell.el can recognize keyword-defined dictionary and keyword-defined
personal-dictionary."
:expected-result :failed
(with-temp-buffer
(nxml-mode)
(let ((test-dict "ispellnonexistent"))
(seq-map (lambda (test-pdict)
(insert
(car ispell-tests--constants/english/correct-list) "\n\n\n"
"<!-- " ispell-dictionary-keyword test-dict " -->"
"<!-- " ispell-pdict-keyword test-pdict " -->")
(ispell-tests--letopt ((ispell-current-dictionary "ispellnonexistent2"))
(ispell-buffer-local-dict)
(should (equal ispell-current-dictionary test-dict))
(should (equal ispell-current-personal-dictionary test-pdict))))
possible-pdict-paths))))
(ert-deftest ispell/ispell-buffer-local-dict/no-reload+overridden ()
"ispell.el can recognize keyword-defined dictionary and keyword-defined
personal-dictionary. With no-reload it needs no backend at all."
(with-temp-buffer
(nxml-mode)
(let ((test-dict "ispellnonexistent3"))
(seq-map (lambda (test-pdict)
(insert
(car ispell-tests--constants/english/correct-list) "\n\n\n"
"<!-- " ispell-dictionary-keyword test-dict " -->"
"<!-- " ispell-pdict-keyword test-pdict " -->")
(ispell-tests--letopt ((ispell-current-dictionary "ispellnonexistent4"))
(let ((ispell-local-dictionary-overridden t))
(ispell-buffer-local-dict t))
(should-not (equal ispell-local-dictionary test-dict))
(should (equal ispell-local-pdict test-pdict))))
possible-pdict-paths))))
(ert-deftest ispell/ispell-buffer-local-dict/reload+overridden ()
"ispell.el can recognize keyword-defined dictionary and keyword-defined
personal-dictionary. With no-reload it needs no backend at all."
:expected-result :failed
(with-temp-buffer
(nxml-mode)
(let ((test-dict "ispellnonexistent5"))
(seq-map
(lambda (test-pdict)
(insert
(car ispell-tests--constants/english/correct-list) "\n\n\n"
"<!-- " ispell-dictionary-keyword test-dict " -->"
"<!-- " ispell-pdict-keyword test-pdict " -->")
(ispell-tests--letopt ((ispell-current-dictionary "ispellnonexistent4"))
(let ((ispell-local-dictionary-overridden t))
(ispell-buffer-local-dict t))
(should-not (equal ispell-current-dictionary test-dict))
(should (equal ispell-current-personal-dictionary
test-pdict))))
possible-pdict-paths)))))
;; parsing
(ert-deftest ispell/ispell-buffer-local-parsing/local-keyword ()
"Check that ispell.el can successfully pick up a tex parser from a buffer-local keyword."
;; FIXME: what if default dictionary sets
;; (ispell-get-extended-character-mode) ?
(with-temp-buffer
(let ((test-parser "~tex")
(test-dictname "testdict")
(test-extcharmode "~latin3"))
(ispell-tests--letopt
((ispell-parser 'ispellnonexistent)
(ispell-local-dictionary-alist
`((,test-dictname "[A-Za-z]" "[^A-Za-z]" "[']"
nil ("-B") ,test-extcharmode utf-8)))
(ispell-current-dictionary test-dictname))
(insert
(car ispell-tests--constants/english/correct-list)"\n\n\n"
ispell-parsing-keyword test-parser)
(let* ((counter 0))
(cl-labels ((checker (s)
(setq counter (+ 1 counter))
(when (equal counter 1)
(should (string-equal s "!\n")))
(when (equal counter 2)
(should (string-equal s "-\n")))
(when (equal counter 3)
(should (string-equal s (concat test-extcharmode "\n"))))
(when (equal counter 4)
(should (string-equal s (concat test-parser "\n"))))
t))
(unwind-protect
(progn
(advice-add 'ispell-send-string :override
#'checker)
(let ((ispell-really-hunspell nil))
(ispell-buffer-local-parsing)))
(advice-remove 'ispell-send-string #'checker)))))))
)
(ert-deftest ispell/ispell-buffer-local-parsing/local-keyword/hunspell-bug ()
"Check that ispell.el can successfully pick up a tex parser from a buffer-local keyword."
;; FIXME: what if default dictionary sets
;; (ispell-get-extended-character-mode) ?
:expected-result :failed
(with-temp-buffer
(let ((test-parser "~tex")
(test-dictname "testdict")
(test-extcharmode "~latin3"))
(ispell-tests--letopt ((ispell-parser 'ispellnonexistent)
(ispell-local-dictionary-alist
`((,test-dictname "[A-Za-z]" "[^A-Za-z]" "[']"
nil ("-B") ,test-extcharmode utf-8)))
(ispell-current-dictionary test-dictname))
(insert
ispell-tests--constants/english/correct-one "\n\n\n" ispell-parsing-keyword test-parser)
(let* ((counter 0))
(cl-labels
((checker (s)
(setq counter (+ 1 counter))
(when (equal counter 1)
(should (string-equal s "!\n")))
(when (equal counter 2)
(should (string-equal s "-\n")))
(when (equal counter 3)
(should (string-equal s (concat test-extcharmode "\n"))))
(when (equal counter 4)
(should (string-equal s (concat test-parser "\n"))))
t))
(unwind-protect
(progn
(advice-add 'ispell-send-string :override
#'checker)
(let ((ispell-really-hunspell t))
(ispell-buffer-local-parsing)))
(advice-remove 'ispell-send-string #'checker)))))))
)
(ert-deftest ispell/ispell-buffer-local-parsing/mode-tex ()
"Check that ispell.el can successfully pick up a tex parser from tex-based mode-name.
There is another implicit check here: explicit-character-mode
\(argument 7 from the ispell.el dictionary structure) is nil."
(with-temp-buffer
(let ((test-dictname "testdict")
(test-extcharmode nil))
(ispell-tests--letopt
((ispell-check-comments t)
(ispell-parser 'use-mode-name)
(ispell-local-dictionary-alist
`((,test-dictname "[A-Za-z]" "[^A-Za-z]" "[']"
nil ("-B") ,test-extcharmode utf-8)))
(ispell-current-dictionary test-dictname))
(insert
(car ispell-tests--constants/english/correct-list) "\n\n\n")
(tex-mode)
(let* ((counter 0))
(cl-labels
((checker (s)
(setq counter (+ 1 counter))
(when (equal counter 1)
(should (string-equal s "!\n")))
(when (equal counter 2)
(should (string-equal s "+\n")))
(when (equal counter 3)
(error "Should not have a third call to `ispell-send-string'"))
t))
(unwind-protect
(progn
(advice-add 'ispell-send-string :override
#'checker)
(ispell-buffer-local-parsing))
(advice-remove 'ispell-send-string #'checker)))))))
)
(ert-deftest ispell/ispell-buffer-local-parsing/extended-character-mode ()
"Check that ispell.el can successfully pick up an extended character mode from the dictionary."
(with-temp-buffer
(insert
ispell-tests--constants/english/correct-one "\n\n\n")
(let ((test-extcharmode "~latin3"))
(ispell-tests--letopt
((ispell-check-comments t)
(ispell-parser 'use-mode-name)
;; FIXME: what if default dictionary sets
;; (ispell-get-extended-character-mode)?
(ispell-local-dictionary-alist
`(("english" "[A-Za-z]" "[^A-Za-z]" "[']"
nil ("-B") ,test-extcharmode utf-8))))
(tex-mode)
(let* ((counter 0))
(cl-labels
((checker (s)
(setq counter (+ 1 counter))
(when (equal counter 1)
(should (string-equal s "!\n")))
(when (equal counter 2)
(should (string-equal s "+\n")))
(when (equal counter 3)
(should (string-equal s (concat test-extcharmode "\n"))))
(when (equal counter 4)
(error "Should not have a third call to `ispell-send-string'"))
t))
(unwind-protect
(progn
(advice-add 'ispell-send-string :override
#'checker)
(ispell-buffer-local-parsing))
(advice-remove 'ispell-send-string #'checker)))))))
)
;; Let us now test the most important state-related function:
;; `ispell-accept-buffer-local-defs'.
;; Why is it important?
;; Because it is used in emacs' own CI for testing documentation
;; in checkdoc.
;; Indeed, when we are running the checker in batch mode,
;; we do not want to have any global state.
(ert-deftest ispell/ispell-accept-buffer-local-defs/simple ()
"Check that `ispell-accept-buffer-local-defs' works for a batch mode.
1. local words
2. dictionary and pdict
3. parser and extcharmode.
This does not work well on hunspell, because hunspell
lies in their Man page, and in enchant-2 when it is using
hunspell. Hence skipping."
(skip-unless (not (or (equal (ispell-tests--some-backend)
"hunspell")
(equal (ispell-tests--some-backend)
"enchant-2"))))
(with-environment-variables (("HOME" temporary-file-directory))
(with-temp-buffer
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(let ((test-dictname (ispell-tests--some-valid-dictionary ispell-program-name))
(test-extcharmode "~latin3")
(test-parser "~testparser")
(test-localword1 "aaaaaaaaaaaaa")
(test-localword2 "bbbbbbbbbbb")
(test-pdict "test-pdict.pdict"))
(insert
(car ispell-tests--constants/english/correct-list) "\n\n\n"
ispell-dictionary-keyword test-dictname "\n"
ispell-pdict-keyword (expand-file-name test-pdict temporary-file-directory) "\n"
ispell-parsing-keyword test-parser "\n"
ispell-words-keyword " " test-localword1 " " test-localword2 "\n")
(ispell-tests--letopt
((ispell-check-comments t)
(ispell-parser 'tex)
;; FIXME: what if default dictionary sets
;; (ispell-get-extended-character-mode)?
(ispell-local-dictionary-alist
`((,test-dictname "[A-Za-z]" "[^A-Za-z]" "[']"
nil ("-B") ,test-extcharmode utf-8))))
(tex-mode)
(let* ((counter 0))
(cl-labels
((checker-ispell-send-string (s)
(let ((references
(list nil
(concat test-extcharmode "\n")
(concat "@" test-localword1 "\n")
(concat "@" test-localword2 "\n")
"!\n"
"+\n"
(concat test-extcharmode "\n")
(concat test-parser "\n"))))
(setq counter (+ 1 counter))
(should (<= counter (length references)))
(should (string-equal
(concat s)
(concat (nth counter references))))
t)))
(unwind-protect
(progn
(advice-add 'ispell-send-string :before
#'checker-ispell-send-string)
(ignore-errors (ispell-kill-ispell))
(ispell-accept-buffer-local-defs)
(should (equal ispell-local-dictionary test-dictname))
(should (equal ispell-local-pdict (expand-file-name test-pdict temporary-file-directory)))
)
(advice-remove 'ispell-send-string #'checker-ispell-send-string)))))))))
)
(ert-deftest ispell/ispell-accept-buffer-local-defs/received-file ()
"Check that `ispell-accept-buffer-local-defs' is broken when a file has a nonexistent file-local dictionary.
We do not control this data, but this should make ispell.el fail."
:expected-result :failed
(with-environment-variables (("HOME" temporary-file-directory))
(with-temp-buffer
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(let ((test-dictname ispell-tests--constants/nonexistent-dictionary))
(insert
(car ispell-tests--constants/english/correct-list) "\n\n\n"
ispell-dictionary-keyword test-dictname "\n")
(ispell-tests--letopt
((ispell-check-comments t)
(ispell-parser 'tex))
(tex-mode)
(progn
(ignore-errors (ispell-kill-ispell))
;; should not error
(ispell-accept-buffer-local-defs)
)))))))
(ert-deftest ispell/ispell--run-on-word/default ()
"`ispell--run-on-word' should be the simplest interface for checking a word."
(skip-unless (ispell-tests--some-backend-available-p))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary "default"))
(let ((default-directory temporary-file-directory))
(with-temp-buffer
(with-environment-variables (("HOME" temporary-file-directory))
(nxml-mode)
;; t t kills regardless and clears buffer-local words
(ignore-errors (ispell-kill-ispell t t))
(ispell-init-process)
(let ((test-output (ispell--run-on-word ispell-tests--constants/english/wrong)))
(should (listp test-output))
(should-not (equal t test-output))
(setq ispell-filter nil)
(setq ispell-filter-continue nil))
(mapc (lambda (word)
(let ((test-output (ispell--run-on-word word)))
(should-not (listp test-output))
(should (equal t test-output))
(setq ispell-filter nil)
(setq ispell-filter-continue nil)))
ispell-tests--constants/english/correct-list)
))))
)
(ert-deftest ispell/ispell--run-on-word/default/fails ()
"Check `ispell--run-on-word', the simplest interface for checking a word.
This test fails due to what I consider
to be a bug. I am quite convinced that `ispell--run-on-word'
should work twice in a row, without having to call
\(`ispell-init-process') or (setq ispell-filter nil)
before each call."
:expected-result :failed
(skip-unless (ispell-tests--some-backend-available-p))
(skip-unless (equal
0
(call-process (ispell-tests--some-backend) nil nil nil "-vv")))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary "default"))
(let ((default-directory temporary-file-directory))
(with-temp-buffer
(with-environment-variables (("HOME" temporary-file-directory))
(nxml-mode)
;; t t kills regardless and clears buffer-local words
(ignore-errors (ispell-kill-ispell t t))
(ispell-init-process)
(let ((test-output (ispell--run-on-word ispell-tests--constants/english/wrong)))
(should (listp test-output))
(should-not (equal t test-output)))
(let ((test-output (ispell--run-on-word (car ispell-tests--constants/english/correct-list))))
(should-not (listp test-output))
(should (equal t test-output)))
))))
)
(ert-deftest ispell/ispell-word/default/check-only/correct ()
"Check that `ispell-word' works with a default dictionary.
We expect it to be english, as Ispell ships it."
(skip-unless (ispell-tests--some-backend-available-p))
(skip-unless (equal
0
(call-process (ispell-tests--some-backend) nil nil nil "-vv")))
(with-environment-variables (("HOME" temporary-file-directory))
(let ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary nil))
(ignore-errors (ispell-kill-ispell t t))
(with-temp-buffer
(insert
(car ispell-tests--constants/english/correct-list) "\n")
(goto-char 1)
(ert-with-message-capture lres
(ispell-word)
(should (string-match "is correct" lres))))
'passed)))
)
(ert-deftest ispell/ispell-word/default/check-only/correct/add-init ()
"Check that `ispell-word' works with a default dictionary.
We expect it to be english, as Ispell ships it. This test is different
from the previous one in that an explicit init call
to (`ispell-init-process') is added. I had issues with it, so I would like
to test it explicitly."
(skip-unless (ispell-tests--some-backend-available-p))
(skip-unless (equal
0
(call-process (ispell-tests--some-backend) nil nil nil "-vv")))
(with-environment-variables (("HOME" temporary-file-directory))
(let ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary nil)
(ispell-check-only t))
(ignore-errors (ispell-kill-ispell t t))
(with-temp-buffer
(ispell-init-process) ;; this is added
(insert
(car ispell-tests--constants/english/correct-list) "\n")
(goto-char 1)
(ert-with-message-capture lres
(ispell-word)
(should (string-match "is correct" lres)))
'passed
))))
)
(ert-deftest ispell/ispell-word/default/check-only/incorrect ()
"Check that `ispell-word' works with a default dictionary.
We expect it to be english, as Ispell ships it. This test gives it a
word which does not exist."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary nil)
(ispell-check-only t))
(ignore-errors (ispell-kill-ispell t t))
(with-temp-buffer
(insert
ispell-tests--constants/english/wrong "\n")
(goto-char 1)
(ert-with-message-capture lres
(ispell-word)
(should (string-match "is incorrect" lres))
'passed)))))
)
(ert-deftest ispell/ispell-region/correct ()
"The simplest test for `ispell-region'."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(words ispell-tests--constants/english/correct-list)
(text (string-join words " ")))
(ispell-tests--letopt
((ispell-program-name
(ispell-tests--some-backend))
(ispell-dictionary nil))
(ignore-errors (ispell-kill-ispell t t))
(with-temp-buffer
(insert text)
(goto-char (length (nth 0 words)))
(ert-with-message-capture lres
(ispell-region (point) (point-max))
(should (string-match "^Spell-checking region using .* with .* dictionary...done" lres))
'passed)))))
)
(ert-deftest ispell/ispell-region/incorrect ()
"The simplest test for `ispell-region'."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(words (list
(nth 0 ispell-tests--constants/english/correct-list)
ispell-tests--constants/english/wrong
(nth 1 ispell-tests--constants/english/correct-list)))
(text (string-join words " ")))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary nil)
(ispell-check-only t))
(ignore-errors (ispell-kill-ispell t t))
(with-temp-buffer
(insert text)
(goto-char (length (nth 0 words)))
(cl-labels ((checker ()
(user-error "Expected error")))
(unwind-protect
(progn
(advice-add 'ispell-show-choices :override #'checker)
(should-error (ispell-region (point) (point-max))))
(advice-remove 'ispell-show-choices #'checker))))
'passed)))
)
(ert-deftest ispell/ispell-buffer/correct ()
"The simplest test for `ispell-buffer'.
`ispell-buffer' is a very simple wrapper around `ispell-region',
so this test virtually mirrors the previous one."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(words ispell-tests--constants/english/correct-list)
(text (string-join words " ")))
(ispell-tests--letopt
((ispell-dictionary nil)
(ispell-program-name (ispell-tests--some-backend)))
(ignore-errors (ispell-kill-ispell t t))
(with-temp-buffer
(insert text)
(ert-with-message-capture lres
(ispell-buffer)
(should (string-match "^Spell-checking .* using .* with .* dictionary...done" lres))
)))))
)
(ert-deftest ispell/ispell-buffer/incorrect ()
"The simplest test for `ispell-buffer'.
`ispell-buffer' is a very simple wrapper around `ispell-region',
so this test virtually mirrors the previous one."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(words (append ispell-tests--constants/english/correct-list
(list ispell-tests--constants/english/wrong)))
(text (string-join words " ")))
(ispell-tests--letopt
((ispell-dictionary nil)
(ispell-program-name (ispell-tests--some-backend)))
(ignore-errors (ispell-kill-ispell t t))
(with-temp-buffer
(insert text)
;; This is intentional. The incorrect word is not in the region,
;; but `ispell-buffer' should move the point to the beginning
;; of the buffer.
(goto-char (length (nth 0 words)))
(cl-labels
((checker ()
(user-error "Expected error")))
(unwind-protect
(progn
(advice-add 'ispell-show-choices :override
#'checker)
(should-error (ispell-buffer))
(ispell-kill-ispell nil t)
'passed)
(advice-remove 'ispell-show-choices #'checker)))
))))
)
(ert-deftest ispell/ispell-kill-ispell ()
"Test that killing ispell works."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(words (append ispell-tests--constants/english/correct-list
(list ispell-tests--constants/english/wrong)))
(text (string-join words " ")))
(with-temp-buffer
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(ispell-tests--with-ispell-global-dictionary
nil
(insert text)
(ispell-init-process)
(should ispell-async-processp)
(should (eq (ispell-process-status) 'run))
(ispell-kill-ispell nil t)))
'passed
)))
)
(ert-deftest ispell/ispell/buffer ()
"Test `ispell-buffer'.
`ispell' is just a wrapper around `ispell-region'
and `ispell-buffer', which is also a wrapper around
`ispell-buffer'.
This test might seem confusing, as it does not check
for the availability of the backend, but this does
not matter `ispell' function does not use the
backend."
(let ((transient-mark-mode t))
(with-temp-buffer
(insert (string-join ispell-tests--constants/english/correct-list " "))
(goto-char 2)
(set-mark (point))
(goto-char (point-max))
(deactivate-mark)
(cl-labels ((checker-buffer ()
t)
(checker-region (_a _b)
(user-error "Test failed")))
(unwind-protect
(progn
(advice-add 'ispell-buffer :override #'checker-buffer)
(advice-add 'ispell-region :override #'checker-region)
(ispell)
)
(progn
(advice-remove 'ispell-buffer #'checker-buffer)
(advice-remove 'ispell-region #'checker-region))))
))
)
(ert-deftest ispell/ispell/region ()
"Test `ispell-region'.
`ispell' is just a wrapper around `ispell-region'
and `ispell-buffer', which is also a wrapper around
`ispell-buffer'."
(let ((transient-mark-mode t))
(with-temp-buffer
(insert (string-join ispell-tests--constants/english/correct-list " "))
(goto-char 2)
(set-mark (point))
(goto-char (point-max))
(activate-mark)
(cl-labels ((checker-buffer ()
(user-error "Test failed"))
(checker-region (_a _b)
t))
(unwind-protect
(progn
(advice-add 'ispell-buffer :override #'checker-buffer)
(advice-add 'ispell-region :override #'checker-region)
(ispell)
)
(progn
(advice-remove 'ispell-buffer #'checker-buffer)
(advice-remove 'ispell-region #'checker-region))))
))
)
(ert-deftest ispell/ispell-change-dictionary ()
"Simple test for changing a dictionary."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(ispell-change-dictionary "english")
(should (equal ispell-local-dictionary "english"))
(ispell-change-dictionary "default")
(should (equal ispell-local-dictionary nil))
(ispell-change-dictionary "english")
(should (equal ispell-local-dictionary "english"))
'passed
))))
)
(ert-deftest ispell/ispell-comments-and-strings/correct ()
"Test that `ispell-comments-and-strings' does not err on a correct buffer."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-dictionary nil)
(ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(ispell-kill-ispell t t)
(insert "#!/bin/bash\n"
"echo \""
(string-join
ispell-tests--constants/english/correct-list " ")
"\"\n"
"# commented line\n")
(sh-mode)
(ert-with-message-capture lres
(ispell-comments-and-strings)
(should (string-match "Spell-checking .* using .* with .* dictionary...done" lres))))
'passed
)))
)
(ert-deftest ispell/ispell-comments-and-strings/incorrect ()
"Test that `ispell-comments-and-strings' errs on a correct buffer."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(insert "#!/bin/bash\n"
"echo \"" (string-join
ispell-tests--constants/english/correct-list " ")
"\"\n"
"# " ispell-tests--constants/english/wrong "\n")
(sh-mode)
(ert-with-message-capture lres
(should-error (ispell-comments-and-strings)))
'passed
))))
)
(ert-deftest ispell/ispell-comment-or-string-at-point ()
"Test that `ispell-comment-or-string-at-point' runs two tests.
One correct an one incorrect in the same buffer."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-dictionary nil)
(ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(let ((string1 "#!/bin/bash\necho\"")
(string2 (string-join
ispell-tests--constants/english/correct-list " "))
(string3 "\"\n# ")
(string4 ispell-tests--constants/english/wrong)
(string5 "\n"))
(insert string1
string2
string3
string4
string5)
(sh-mode)
(goto-char (+ (length string1) (length (nth 0 ispell-tests--constants/english/correct-list))))
(ert-with-message-capture lres
(ispell-comment-or-string-at-point)
(should (string-match "Spell-checking .* using .* with .* dictionary...done" lres)))
(goto-char (+ (length string1)
(length string2)
(length string3)
1))
(should-error (ispell-comment-or-string-at-point)))
'passed))))
)
(ert-deftest ispell/ispell-pdict-save ()
"Simple `ispell-pdict-save' test."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(insert (format "%s" (random)))
(ispell-kill-ispell t t)
(ispell-pdict-save t t)
'passed))))
)
(ert-deftest ispell/ispell-pdict-save/force ()
"Simple `ispell-pdict-save' test."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(insert (format "%s" (random)))
(goto-char 1)
(ispell-kill-ispell t t)
(ispell-pdict-save t t)
'passed))))
)
(ert-deftest ispell/ispell-pdict-save/modified ()
"Simple `ispell-pdict-save' test."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend-available-p)))
(with-temp-buffer
(insert (format "%s" (random)))
(goto-char 1)
(ispell-kill-ispell t t)
(cl-labels
((checker (s)
(should (equal s "#\n"))))
(unwind-protect
(progn
(advice-add 'ispell-send-string :override
#'checker)
(let ((ispell-pdict-modified-p t))
(ispell-pdict-save t nil)))
(advice-remove 'ispell-send-string #'checker))))
'passed)))
)
(ert-deftest ispell/ispell-pdict-save/unmodified ()
"Simple `ispell-pdict-save' test."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(insert (format "%s" (random)))
(goto-char 1)
(ispell-kill-ispell t t)
(cl-labels ((checker (_s)
(user-error "Test failed")))
(unwind-protect
(progn
(advice-add 'ispell-send-string :override
#'checker)
(let ((ispell-pdict-modified-p nil))
(ispell-pdict-save t nil)))
(advice-remove 'ispell-send-string #'checker))))
'passed)))
)
(ert-deftest ispell/ispell-lookup-words/simple ()
"Test if `ispell-lookup-words' is runnable."
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(tempfile (make-temp-file "emacs-ispell.el-test" nil nil ispell-tests--constants/completion)))
(ispell-tests--letopt
((ispell-complete-word-dict tempfile))
(with-temp-buffer
(insert (substring ispell-tests--constants/completion 0 -2))
(unwind-protect
(progn
(should (equal
(ispell-lookup-words (substring ispell-tests--constants/completion 0 -2))
(list ispell-tests--constants/completion)))
(should (equal
(ispell-lookup-words ispell-tests--constants/english/wrong)
nil)))
(delete-file tempfile)))
'passed)))
)
(ert-deftest ispell/ispell-complete-word/ispell-completion-at-point ()
"Test if `ispell-complete-word' and `ispell-completion-at-point' are runnable."
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(tempfile (make-temp-file "emacs-ispell.el-test" nil nil ispell-tests--constants/completion)))
(ignore-errors (ispell-kill-ispell t t))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-complete-word-dict tempfile))
(ispell-tests--with-ispell-global-dictionary
nil
(with-temp-buffer
(insert (substring ispell-tests--constants/completion 0 -2))
(cl-labels ((my-ispell-command-loop (_p _n _w _s _e)
(car (nth 2 (ispell-completion-at-point)))))
(unwind-protect
(progn
(advice-add 'ispell-command-loop :override
#'my-ispell-command-loop)
(should (equal (car (nth 2 (ispell-completion-at-point)))
ispell-tests--constants/completion))
(ispell-complete-word)
(should (equal ispell-tests--constants/completion (buffer-string))))
(progn
(delete-file tempfile)
(advice-remove
'ispell-command-loop
#'my-ispell-command-loop)))))
'passed))))
)
(ert-deftest ispell/ispell-complete-word-interior-frag/simple ()
"Test if `ispell-complete-word-interior-frag' is runnable."
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory)
(tempfile (make-temp-file "emacs-ispell.el-test" nil nil "waveguides")))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-complete-word-dict tempfile))
(ispell-tests--with-ispell-global-dictionary
nil
(with-temp-buffer
(insert (substring ispell-tests--constants/completion 0 -2))
(cl-labels
((my-ispell-command-loop (_p _n _w _s _e)
(car (nth 2 (ispell-completion-at-point)))))
(unwind-protect
(progn
(advice-add 'ispell-command-loop :override
#'my-ispell-command-loop)
(goto-char 4)
(ispell-complete-word-interior-frag)
(should (equal ispell-tests--constants/completion
(buffer-string))))
(progn
(delete-file tempfile)
(advice-remove
'ispell-command-loop
#'my-ispell-command-loop)))))
'passed))))
)
(ert-deftest ispell/ispell-minor-mode/simple ()
"Try enabling `ispell-minor-mode' and test one test file."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary nil))
(with-temp-buffer
(text-mode)
(ispell-minor-mode)
(insert ispell-tests--constants/english/wrong)
(set--this-command-keys " ")
(ert-with-message-capture lres
(ispell-minor-check)
(should (string-match
(seq-concatenate
'string
(upcase ispell-tests--constants/english/wrong)
" is incorrect")
lres)))
)
'passed)))
)
(ert-deftest ispell/ispell-message/correct ()
"Test that `ispell-message' works.
`ispell-message' is intended to be run before
a message is sent in `message-mode' or `mml-mode'."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend))
(ispell-dictionary nil))
(with-temp-buffer
(insert
(seq-concatenate
'string
"To:
Subject:
From: Anon <anon@anon>
Fcc: /tmp/234234.cb022f1a625b65b2.mainframe:2,S
User-Agent: mu4e 1.12.9; emacs 31.0.50
Date: Tue, 09 Sep 2025 07:43:58 +0800
Message-ID: <878qiov7b5.fsf@mainframe>
--text follows this line--
" (string-join
ispell-tests--constants/english/correct-list " ")
"
--
signature
"))
(message-mode)
(ert-with-message-capture lres
(ispell-message)
(should (not (string-match "is incorrect" lres)))
)
(set-buffer-modified-p nil)
)
'passed)))
)
(ert-deftest ispell/ispell-message/incorrect ()
"Test that `ispell-message' works.
`ispell-message' is intended to be run before
a message is sent in `message-mode' or `mml-mode'."
(skip-unless (ispell-tests--some-backend-available-p))
(with-environment-variables (("HOME" temporary-file-directory))
(let* ((default-directory temporary-file-directory))
(ispell-tests--letopt
((ispell-program-name (ispell-tests--some-backend)))
(with-temp-buffer
(insert
(seq-concatenate
'string
"To:
Subject:
From: Anon <anon@anon>
Fcc: /tmp/234234.cb022f1a625b65b2.mainframe:2,S
User-Agent: mu4e 1.12.9; emacs 31.0.50
Date: Tue, 09 Sep 2025 07:43:58 +0800
Message-ID: <878qiov7b5.fsf@mainframe>
--text follows this line--
<#part sign=pgpmime>
" ispell-tests--constants/english/wrong "
--
signature
"))
(message-mode)
(cl-labels ((checker ()
(user-error "Expected error")))
(unwind-protect
(progn
(advice-add 'ispell-show-choices :override #'checker)
(should-error (ispell-message)))
(progn
(advice-remove 'ispell-show-choices #'checker)
(set-buffer-modified-p nil)))))
'passed)))
)
(provide 'ispell-tests)
;;; ispell-tests.el ends here
|