1 ;; Tests for various functions handling adornments -*- lexical-binding: t -*-
3 (add-to-list 'load-path
".")
7 (ert-deftest adornment-asserts
()
8 "Check some assertions."
9 (should (equal ert-Buf-point-char
"\^@"))
10 (should (equal ert-Buf-mark-char
"\^?"))
13 (defun ttl-at-point ()
14 "Wrapper for calling `rst-ttl-at-point'."
15 (apply-ttl-match (rst-ttl-at-point)))
17 (ert-deftest rst-ttl-at-point
()
18 "Tests for `rst-ttl-at-point'."
19 (should (ert-equal-buffer-return
23 Du bon vin tous les jours.
28 \^@Du bon vin tous les jours.
31 '(nil 0 nil
"Du bon vin tous les jours." nil
)
33 (should (ert-equal-buffer-return
37 Du bon vin tous les jours.
42 \^@Du bon vin tous les jours.
45 '(nil 0 nil
"Du bon vin tous les jours." nil
)
47 (should (ert-equal-buffer-return
51 Du bon vin tous les jours.
56 \^@Du bon vin tous les jours.
60 (rst-Ado-new-simple ?-
) 0
61 nil
"Du bon vin tous les jours." "-----------")
63 (should (ert-equal-buffer-return
67 Du bon vin tous les jours.
72 \^@Du bon vin tous les jours.
76 (rst-Ado-new-over-and-under ?-
) 0
77 "-----------" "Du bon vin tous les jours." nil
)
79 (should (ert-equal-buffer-return
83 Du bon vin tous les jours.
89 \^@Du bon vin tous les jours.
94 (rst-Ado-new-over-and-under ?-
) 0
95 "-----------" "Du bon vin tous les jours." "-----------")
97 (should (ert-equal-buffer-return
101 Du bon vin tous les jours.
106 Du bon vin tous les jours.
111 (should (ert-equal-buffer-return
114 Du bon vin tous les jours.
116 Du bon vin tous les jours.
121 Du bon vin tous les jours.
123 \^@Du bon vin tous les jours.
126 " ; This is not how the parser works but looks more logical
128 (rst-Ado-new-over-and-under ?-
) 0
129 "-----------" "Du bon vin tous les jours." "-----------")
131 (should (ert-equal-buffer-return
145 (should (ert-equal-buffer-return
151 (should (ert-equal-buffer-return
159 (should (ert-equal-buffer-return
173 '(nil 0 nil
"Line 1" nil
)
175 (should (ert-equal-buffer-return
178 =====================================
179 Project Idea: Panorama Stitcher
180 ====================================
182 :Author: Martin Blais <blais@furius.ca>
188 =====================================
189 Project Idea: Panorama Stitcher
190 ====================================
192 \^@:Author: Martin Blais <blais@furius.ca>
197 '(nil 0 nil
":Author: Martin Blais <blais@furius.ca>" nil
)
202 "===============================
203 Project Idea: My Document
204 ===============================
206 :Author: Martin Blais
211 This is the introduction.
284 (defun find-all-adornments ()
285 "Call `rst-all-ttls' and return conses of line and `rst-Ado'."
286 (mapcar (lambda (ttl)
287 (cons (line-number-at-pos (rst-Ttl-get-title-beginning ttl
))
289 (rst-all-ttls-compute)))
291 (ert-deftest rst-all-ttls
()
292 "Tests for `rst-all-ttls'."
293 (should (ert-equal-buffer-return
294 (find-all-adornments)
299 (should (ert-equal-buffer-return
300 '(find-all-adornments)
302 Not a valid section header because of indentation
303 ===================================================
308 (should (ert-equal-buffer-return
309 '(find-all-adornments)
313 ===================================================
320 (should (ert-equal-buffer-return
321 '(find-all-adornments)
323 =======================================================
324 Not a valid section header because of missing underline
330 (should (ert-equal-buffer-return
331 '(find-all-adornments)
333 =====================================================
334 Not a valid section header because of wrong underline
335 -----------------------------------------------------
340 (should (ert-equal-buffer-return
341 '(find-all-adornments)
343 Valid simple section header
344 ===========================
348 (cons 2 (rst-Ado-new-simple ?
=)))
350 (should (ert-equal-buffer-return
351 '(find-all-adornments)
353 =======================================
354 Valid over and under section header
355 =======================================
359 (cons 3 (rst-Ado-new-over-and-under ?
=)))
361 (should (ert-equal-buffer-return
362 '(find-all-adornments)
366 (cons 2 (rst-Ado-new-over-and-under ?
=))
367 (cons 7 (rst-Ado-new-simple ?
=))
368 (cons 12 (rst-Ado-new-simple ?-
))
369 (cons 17 (rst-Ado-new-simple ?
=))
370 (cons 22 (rst-Ado-new-simple ?-
))
371 (cons 26 (rst-Ado-new-over-and-under ?~
))
372 (cons 31 (rst-Ado-new-simple ?
=)))
374 (should (ert-equal-buffer-return
375 '(find-all-adornments)
379 (cons 3 (rst-Ado-new-simple ?-
))
380 (cons 6 (rst-Ado-new-simple ?~
))
381 (cons 9 (rst-Ado-new-simple ?
+)))
383 (should (ert-equal-buffer-return
384 '(find-all-adornments)
388 (cons 3 (rst-Ado-new-simple ?-
))
389 (cons 6 (rst-Ado-new-simple ?~
)))
391 (should (ert-equal-buffer-return
392 '(find-all-adornments)
417 (cons 2 (rst-Ado-new-over-and-under ?
=))
418 (cons 5 (rst-Ado-new-simple ?
=))
419 (cons 8 (rst-Ado-new-simple ?
=))
420 (cons 11 (rst-Ado-new-simple ?-
))
421 (cons 14 (rst-Ado-new-simple ?~
))
422 (cons 17 (rst-Ado-new-simple ?
=))
423 (cons 20 (rst-Ado-new-simple ?~
)))
427 (ert-deftest rst-hdr-hierarchy
()
428 "Tests for `rst-hdr-hierarchy'."
429 (let ( ;; Set customizable variables to defined values
430 (rst-default-indent 5))
431 (should (ert-equal-buffer-return
436 (rst-Hdr-new (rst-Ado-new-over-and-under ?
=) 3)
437 (rst-Hdr-new (rst-Ado-new-simple ?
=) 0)
438 (rst-Hdr-new (rst-Ado-new-simple ?-
) 0)
439 (rst-Hdr-new (rst-Ado-new-over-and-under ?~
) 1))
441 (should (ert-equal-buffer-return
451 (rst-Hdr-new (rst-Ado-new-over-and-under ?
=) 5)
452 (rst-Hdr-new (rst-Ado-new-simple ?
=) 0)
453 (rst-Hdr-new (rst-Ado-new-simple ?-
) 0)
454 (rst-Hdr-new (rst-Ado-new-over-and-under ?~
) 1))
458 (ert-deftest rst-get-hierarchy-ignore
()
459 "Tests for `rst-hdr-hierarchy' with ignoring a line."
460 (should (ert-equal-buffer-return
461 '(rst-hdr-hierarchy (point))
465 (rst-Hdr-new (rst-Ado-new-over-and-under ?
=) 3)
466 (rst-Hdr-new (rst-Ado-new-simple ?
=) 0)
467 (rst-Hdr-new (rst-Ado-new-simple ?-
) 0))
469 (should (ert-equal-buffer-return
470 '(rst-hdr-hierarchy (point))
474 (rst-Hdr-new (rst-Ado-new-simple ?-
) 0)
475 (rst-Hdr-new (rst-Ado-new-simple ?~
) 0)
476 (rst-Hdr-new (rst-Ado-new-simple ?
+) 0))
480 (ert-deftest rst-adornment-level
()
481 "Tests for `rst-adornment-level'."
482 (should (ert-equal-buffer-return
483 '(rst-adornment-level (rst-Ado-new-transition))
488 (should (ert-equal-buffer-return
489 '(rst-adornment-level (rst-Ado-new-over-and-under ?
=))
494 (should (ert-equal-buffer-return
495 '(rst-adornment-level (rst-Ado-new-simple ?
=))
500 (should (ert-equal-buffer-return
501 '(rst-adornment-level (rst-Ado-new-simple ?-
))
506 (should (ert-equal-buffer-return
507 '(rst-adornment-level (rst-Ado-new-over-and-under ?~
))
512 (should (ert-equal-buffer-return
513 '(rst-adornment-level (rst-Ado-new-simple ?
#))
520 (ert-deftest rst-adornment-complete-p
()
521 "Tests for `rst-adornment-complete-p'."
522 (should (ert-equal-buffer-return
523 '(rst-adornment-complete-p (rst-Ado-new-simple ?
=) 0)
527 (should (ert-equal-buffer-return
528 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
533 (should (ert-equal-buffer-return
534 '(rst-adornment-complete-p (rst-Ado-new-simple ?
=) 0)
542 (should (ert-equal-buffer-return
543 '(rst-adornment-complete-p (rst-Ado-new-simple ?
=) 0)
550 (should (ert-equal-buffer-return
551 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
559 (should (ert-equal-buffer-return
560 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 2)
568 (should (ert-equal-buffer-return
569 '(rst-adornment-complete-p (rst-Ado-new-simple ?
=) 0)
576 (should (ert-equal-buffer-return
577 '(rst-adornment-complete-p (rst-Ado-new-simple ?
=) 0)
584 (should (ert-equal-buffer-return
585 '(rst-adornment-complete-p (rst-Ado-new-simple ?
=) 0)
592 (should (ert-equal-buffer-return
593 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
601 (should (ert-equal-buffer-return
602 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
610 (should (ert-equal-buffer-return
611 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
619 (should (ert-equal-buffer-return
620 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
628 (should (ert-equal-buffer-return
629 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
637 (should (ert-equal-buffer-return
638 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
646 (should (ert-equal-buffer-return
647 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
655 (should (ert-equal-buffer-return
656 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
664 (should (ert-equal-buffer-return
665 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
673 (should (ert-equal-buffer-return
674 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
682 (should (ert-equal-buffer-return
683 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
691 (should (ert-equal-buffer-return
692 '(rst-adornment-complete-p (rst-Ado-new-over-and-under ?
=) 0)
702 (ert-deftest rst-get-previous-hdr
()
703 "Tests for `rst-get-previous-hdr'."
704 (should (ert-equal-buffer-return
705 '(rst-get-previous-hdr)
718 (rst-Hdr-new (rst-Ado-new-simple ?-
) 0)))
719 (should (ert-equal-buffer-return
720 '(rst-get-previous-hdr)
734 (rst-Hdr-new (rst-Ado-new-simple ?-
) 0)))
737 (defun apply-ttl-match (ttl)
738 "Apply the match in TTL to the buffer and return important data.
739 Puts point at the beginning of the title line. Return a list
740 consisting of the `rst-Ado', the indent and the three matched
741 texts. Return nil if TTL is nil. Checks whether embedded match
742 groups match match group 0."
744 (let ((match (rst-Ttl-match ttl
)))
745 (set-match-data match
)
746 (let ((whole (match-string-no-properties 0))
747 (over (match-string-no-properties 1))
748 (text (match-string-no-properties 2))
749 (under (match-string-no-properties 3))
752 (setq gather
(concat gather over
"\n")))
754 (setq gather
(concat gather text
"\n")))
756 (setq gather
(concat gather under
"\n")))
757 (if (not (string= (substring gather
0 -
1) whole
))
758 (error "Match 0 '%s' doesn't match concatenated parts '%s'"
760 (goto-char (match-beginning 2))
761 (list (rst-Ttl-ado ttl
) (rst-Ttl-indent ttl
) over text under
)))))
763 (defun classify-adornment (beg end
)
764 "Wrapper for calling `rst-classify-adornment'."
766 (apply-ttl-match (rst-classify-adornment
767 (buffer-substring-no-properties beg end
) end
)))
769 (defun classify-adornment-accept (beg end
)
770 "Wrapper for calling `rst-classify-adornment'."
772 (apply-ttl-match (rst-classify-adornment
773 (buffer-substring-no-properties beg end
) end t
)))
775 (ert-deftest rst-classify-adornment
()
776 "Tests for `rst-classify-adornment'."
777 (should (ert-equal-buffer-return
778 '(classify-adornment)
781 Du bon vin tous les jours
782 \^@=========================\^?
787 (rst-Ado-new-simple ?
=) 0
788 nil
"Du bon vin tous les jours" "=========================")
790 (should (ert-equal-buffer-return
791 '(classify-adornment)
794 Du bon vin tous les jours
795 \^@====================\^?
800 (rst-Ado-new-simple ?
=) 0
801 nil
"Du bon vin tous les jours" "====================")
803 (should (ert-equal-buffer-return
804 '(classify-adornment)
805 "\^@====================\^?
806 Du bon vin tous les jours"
810 (should (ert-equal-buffer-return
811 '(classify-adornment-accept)
812 "\^@====================\^?
813 Du bon vin tous les jours"
816 (rst-Ado-new-over-and-under ?
=) 0
817 "====================" "Du bon vin tous les jours" nil
)
819 (should (ert-equal-buffer-return
820 '(classify-adornment)
823 Du bon vin tous les jours
824 \^@====================\^?
829 (rst-Ado-new-simple ?
=) 5
830 nil
" Du bon vin tous les jours" "====================")
832 (should (ert-equal-buffer-return
833 '(classify-adornment)
834 " Du bon vin tous les jours
835 \^@====================\^?
839 (rst-Ado-new-simple ?
=) 5
840 nil
" Du bon vin tous les jours" "====================")
842 (should (ert-equal-buffer-return
843 '(classify-adornment)
846 Du bon vin tous les jours
852 (should (ert-equal-buffer-return
853 '(classify-adornment)
856 Du bon vin tous les jours
862 (should (ert-equal-buffer-return
863 '(classify-adornment)
866 Du bon vin tous les jours
871 (rst-Ado-new-simple ?-
) 0
872 nil
"Du bon vin tous les jours" "---")
874 (should (ert-equal-buffer-return
875 '(classify-adornment)
877 \^@~~~~~~~~~~~~~~~~~~~~~~~~~\^?
878 Du bon vin tous les jours
879 ~~~~~~~~~~~~~~~~~~~~~~~~~
884 (rst-Ado-new-over-and-under ?~
) 0
885 "~~~~~~~~~~~~~~~~~~~~~~~~~" "Du bon vin tous les jours" "~~~~~~~~~~~~~~~~~~~~~~~~~")
887 (should (ert-equal-buffer-return
888 '(classify-adornment)
889 "~~~~~~~~~~~~~~~~~~~~~~~~~
890 Du bon vin tous les jours
891 \^@~~~~~~~~~~~~~~~~~~~~~~~~~\^?
896 (rst-Ado-new-over-and-under ?~
) 0
897 "~~~~~~~~~~~~~~~~~~~~~~~~~" "Du bon vin tous les jours" "~~~~~~~~~~~~~~~~~~~~~~~~~")
899 (should (ert-equal-buffer-return
900 '(classify-adornment)
902 \^@~~~~~~~~~~~~~~~~~~~~~~~~~\^?
903 Du bon vin tous les jours
904 ~~~~~~~~~~~~~~~~~~~~~~~~~
909 (rst-Ado-new-over-and-under ?~
) 3
910 "~~~~~~~~~~~~~~~~~~~~~~~~~" " Du bon vin tous les jours" "~~~~~~~~~~~~~~~~~~~~~~~~~")
912 (should (ert-equal-buffer-return
913 '(classify-adornment)
915 \^@~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\^?
916 Du bon vin tous les jours
922 (rst-Ado-new-over-and-under ?~
) 0
923 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" "Du bon vin tous les jours" "~~~~~~~~~~~~~~~~~~~")
925 (should (ert-equal-buffer-return
926 '(classify-adornment)
928 ---------------------------
929 Du bon vin tous les jours
930 \^@~~~~~~~~~~~~~~~~~~~~~~~~~~~\^?
936 (should (ert-equal-buffer-return
937 '(classify-adornment)
938 "\^@---------------------------\^?"
941 (rst-Ado-new-transition) nil
942 nil
"---------------------------" nil
)
944 (should (ert-equal-buffer-return
945 '(classify-adornment)
951 (rst-Ado-new-transition) nil
954 (should (ert-equal-buffer-return
955 '(classify-adornment)
957 \^@---------------------------\^?
958 Du bon vin tous les jours
959 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
965 (should (ert-equal-buffer-return
966 '(classify-adornment)
968 =========================
969 Du bon vin tous les jours
970 \^@=========================\^?
976 (rst-Ado-new-over-and-under ?
=) 0
977 "=========================" "Du bon vin tous les jours" "=========================")
979 (should (ert-equal-buffer-return
980 '(classify-adornment)
982 =========================
983 Du bon vin tous les jours
984 =========================
991 (rst-Ado-new-simple ?-
) 0
992 nil
"Du bon vin" "----------")
994 (should (ert-equal-buffer-return
995 '(classify-adornment)
997 =========================
998 Du bon vin tous les jours
999 =========================
1007 (rst-Ado-new-over-and-under ?-
) 0
1008 "----------" "Du bon vin" "----------")
1010 (should (ert-equal-buffer-return
1011 '(classify-adornment)
1013 =========================
1014 Du bon vin tous les jours
1015 =========================
1018 \^@--------------\^?
1023 (rst-Ado-new-over-and-under ?-
) 2
1024 "--------------" " Du bon vin" "--------------")
1028 (defun display-adornments-hierarchy ()
1029 "Wrapper for calling `rst-display-hdr-hierarchy'."
1030 (rst-display-hdr-hierarchy)
1031 (let ((source (get-buffer "*rest section hierarchy*")))
1032 (delete-region (point-min) (point-max))
1033 (insert (with-current-buffer source
1034 (buffer-substring (point-min) (point-max))))
1035 (kill-buffer source
)))
1037 (ert-deftest rst-display-hdr-hierarchy
()
1038 "Tests for `rst-display-hdr-hierarchy'."
1039 (should (ert-equal-buffer
1040 '(display-adornments-hierarchy)
1045 (should (ert-equal-buffer
1046 '(display-adornments-hierarchy)
1081 (ert-deftest rst-adjust-region
()
1082 "Tests for `rst-adjust-region'."
1083 (let ((rst-preferred-adornments '((?
= over-and-under
1)
1091 (should (ert-equal-buffer
1092 '(rst-adjust-region nil
)
1096 (should (ert-equal-buffer
1097 '(rst-adjust-region nil
)
1135 (should (ert-equal-buffer
1136 '(rst-adjust-region nil
)
1173 (should (ert-equal-buffer
1174 '(rst-adjust-region nil
)
1213 (should (ert-equal-buffer
1214 '(rst-adjust-region t
)
1252 (should (ert-equal-buffer
1253 '(rst-adjust-region t
)
1293 (ert-deftest rst-straighten-sections
()
1294 "Tests for `rst-straighten-sections'."
1295 (let ((rst-preferred-adornments '((?
= over-and-under
1)
1303 (should (ert-equal-buffer
1304 '(rst-straighten-sections)
1308 (should (ert-equal-buffer
1309 '(rst-straighten-sections)
1350 (defun find-all-levels ()
1351 "Call `rst-all-ttls-with-level' and return conses of line and level."
1352 (mapcar (cl-function
1353 (lambda ((ttl . level
))
1354 (cons (line-number-at-pos (rst-Ttl-get-title-beginning ttl
))
1356 (rst-all-ttls-with-level)))
1358 (ert-deftest rst-all-ttls-with-level
()
1359 "Tests for `rst-all-ttls-with-level'."
1360 (should (ert-equal-buffer-return
1366 (should (ert-equal-buffer-return
1369 Not a valid section header because of indentation
1370 ===================================================
1375 (should (ert-equal-buffer-return
1378 Valid simple section header
1379 ===========================
1385 (should (ert-equal-buffer-return
1388 =======================================
1389 Valid over and under section header
1390 =======================================
1396 (should (ert-equal-buffer-return
1409 (should (ert-equal-buffer-return
1418 (should (ert-equal-buffer-return
1428 (defun update-section (char simplep indent
)
1429 "Call `rst-update-section' with proper header."
1431 (rst-Hdr-new (if simplep
1432 (rst-Ado-new-simple char
)
1433 (rst-Ado-new-over-and-under char
)) indent
)))
1435 (ert-deftest rst-update-section
()
1436 "Tests for `rst-update-section'."
1437 (should (ert-equal-buffer
1438 '(update-section ?
= t
0)
1452 (should (ert-equal-buffer
1453 '(update-section ?
= nil
2)
1468 (should (ert-equal-buffer
1469 '(update-section ?
= nil
2)
1488 (should (ert-equal-buffer
1489 '(update-section ?
= nil
2)
1505 (should (ert-equal-buffer
1506 '(update-section ?
= t
0)
1516 (should (ert-equal-buffer
1517 '(update-section ?
= nil
0)
1528 (should (ert-equal-buffer
1529 '(update-section ?
= t
0)
1536 (should (ert-equal-buffer
1537 '(update-section ?
= t
0)
1545 (should (ert-equal-buffer
1546 '(update-section ?
= t
0)
1554 (should (ert-equal-buffer
1555 '(update-section ?
= t
0)