Debian Codegraph @

tools/editors/emacs/tests/buffer.el

raw
1 ;;; buffer.el --- Test the test support for buffers -*- lexical-binding: t -*-
2
3 (add-to-list 'load-path ".")
4 (load "ert-buffer" nil t)
5
6 ;; ****************************************************************************
7 ;; `ert-Buf'
8
9 (defun roundtrip-ert-Buf (in)
10 (with-temp-buffer
11 (ert-Buf--to-buffer (ert-Buf-from-string in))
12 (ert-Buf-string (ert-Buf-from-buffer))))
13
14 (ert-deftest ert-Buf ()
15 "Tests for functions working with `ert-Buf's."
16 (should (equal (concat ert-Buf-point-char "abc\n")
17 (roundtrip-ert-Buf (concat ert-Buf-point-char "abc\n"))))
18 (should (equal (concat "a" ert-Buf-point-char "bc\n")
19 (roundtrip-ert-Buf (concat "a" ert-Buf-point-char "bc\n"))))
20 (should (equal (concat "ab" ert-Buf-point-char "c\n")
21 (roundtrip-ert-Buf (concat "ab" ert-Buf-point-char "c\n"))))
22 (should (equal (concat "abc" ert-Buf-point-char "\n")
23 (roundtrip-ert-Buf (concat "abc" ert-Buf-point-char "\n"))))
24 (should (equal (concat "abc\n" ert-Buf-point-char)
25 (roundtrip-ert-Buf (concat "abc\n" ert-Buf-point-char))))
26 (should (equal (concat ert-Buf-point-char "abc\n" ert-Buf-mark-char "")
27 (roundtrip-ert-Buf
28 (concat ert-Buf-point-char "abc\n" ert-Buf-mark-char ""))))
29 (should (equal (concat ert-Buf-mark-char "abc\n" ert-Buf-point-char)
30 (roundtrip-ert-Buf
31 (concat ert-Buf-mark-char "abc\n" ert-Buf-point-char))))
32 (should (equal (concat "a" ert-Buf-mark-char ert-Buf-point-char "bc\n")
33 (roundtrip-ert-Buf
34 (concat "a" ert-Buf-point-char "" ert-Buf-mark-char "bc\n"))))
35 (should (equal (concat "ab" ert-Buf-mark-char "" ert-Buf-point-char "c\n")
36 (roundtrip-ert-Buf
37 (concat "ab" ert-Buf-mark-char ert-Buf-point-char "c\n"))))
38 (should-error (ert-Buf-from-string
39 (concat "ab" ert-Buf-point-char ert-Buf-point-char "c\n")))
40 (should-error (ert-Buf-from-string
41 (concat "ab" ert-Buf-mark-char ert-Buf-mark-char "c\n")))
42 )
43
44 (ert-deftest ert-Buf--from-argument ()
45 "Test `ert-Buf--from-argument'."
46 (let ((marked-a (ert-Buf-from-string
47 (concat ert-Buf-point-char "a" ert-Buf-mark-char))))
48 (should (not (ert-Buf--from-argument nil nil)))
49 (should (equal (ert-Buf--from-argument ?a nil)
50 (ert-Buf-from-string "a")))
51 (should (equal (ert-Buf--from-argument ert-Buf-point-char nil)
52 (ert-Buf-from-string ert-Buf-point-char)))
53 (should (equal (ert-Buf--from-argument '("a" "b") nil)
54 (ert-Buf-from-string "ab")))
55 (should (equal (ert-Buf--from-argument `("a" ,ert-Buf-point-char "b") nil)
56 (ert-Buf-from-string (concat "a" ert-Buf-point-char "b"))))
57 (should (equal (ert-Buf--from-argument marked-a nil) marked-a))
58 (should-error (ert-Buf--from-argument -1 nil))
59 (should-error (ert-Buf--from-argument [0] nil))
60 (should-error (ert-Buf--from-argument t nil))
61 (should-error (ert-Buf--from-argument t t))
62 (should (eq (ert-Buf--from-argument t marked-a) marked-a))
63 ))
64
65 ;; ****************************************************************************
66 ;; Advice `ert-completing-read'
67
68 (defvar read-fun-args nil
69 "Input for for functions reading the minibuffer.
70 Consists of a list of functions and their argument lists to be
71 run successively. Prompt is omitted.")
72
73 (defun insert-reads ()
74 (interactive)
75 (while read-fun-args
76 (let* ((fun-arg (pop read-fun-args))
77 (result (apply (car fun-arg) "" (cdr fun-arg))))
78 (insert (if (integerp result)
79 (int-to-string result)
80 result) "\n"))))
81
82 (defun test-reads (inputs fun-args result)
83 (setq read-fun-args fun-args)
84 (ert-equal-buffer '(insert-reads) "" result inputs))
85
86 (ert-deftest reads ()
87 "Tests for functions using `completing-read's."
88 (should (test-reads '(5) '((read-number)) "5\n"))
89 (should (test-reads nil nil ""))
90 (should-error (test-reads '("") nil "")) ;; Too much input.
91 (should-error (test-reads '(5) '((read-number)
92 (read-number)) "")) ;; Too less input.
93 (should (test-reads '("") '((completing-read nil)) "\n"))
94 (should (test-reads '("" "") '((completing-read nil)
95 (completing-read nil)) "\n\n"))
96 (should (test-reads '("a" "b") '((completing-read nil)
97 (completing-read nil)) "a\nb\n"))
98 (should (test-reads '("a" "b") '((completing-read ("a" "b"))
99 (completing-read ("a" "b"))) "a\nb\n"))
100 (should (test-reads '("a" "b") '((completing-read ("a" "b"))
101 (completing-read ("a"))) "a\nb\n"))
102 (should-error (test-reads '("a" "b")
103 '((completing-read ("a" "b"))
104 (completing-read ("a") nil t)) "a\nb\n")) ;; Invalid input.
105 (should (test-reads '("a" "")
106 '((completing-read ("a" "b"))
107 (completing-read ("a") nil t)) "a\n\n"))
108 (should-error (test-reads '("a" "")
109 '((completing-read ("a" "b"))
110 (completing-read ("a") nil 'non-empty)) "a\n\n"))
111 (should (test-reads '("x") '((read-string)) "x\n"))
112 (should (test-reads '("") '((read-string nil nil "x")) "x\n"))
113 (should (test-reads '("y") '((read-string nil nil "x")) "y\n"))
114 (should (test-reads '("") '((read-number 5)) "5\n"))
115 (should (test-reads '(0) '((read-number 5)) "0\n"))
116 )
117
118 ;; ****************************************************************************
119 ;; Test main functions
120
121 (ert-deftest ert-equal-buffer ()
122 "Tests for `ert-equal-buffer'."
123 (should (ert-equal-buffer '(insert "foo")
124 (concat ert-Buf-point-char ert-Buf-mark-char)
125 (concat ert-Buf-mark-char "foo"
126 ert-Buf-point-char)))
127 (should (ert-equal-buffer '(delete-region)
128 (concat ert-Buf-mark-char "foo"
129 ert-Buf-point-char)
130 (concat ert-Buf-point-char ert-Buf-mark-char)
131 t))
132 (should (ert-equal-buffer '(delete-region 1 4)
133 "foo"
134 ""))
135 (should-error (ert-equal-buffer '(delete-region 0 3)
136 (concat "foo")
137 "") :type 'args-out-of-range)
138 (should (ert-equal-buffer '(goto-char 4)
139 "foo"
140 (concat "foo" ert-Buf-point-char)))
141 )
142
143 (ert-deftest ert-equal-buffer-return ()
144 "Tests for `ert-equal-buffer-return'."
145 (should (ert-equal-buffer-return '(buffer-substring-no-properties 4 1)
146 "foo"
147 t
148 "foo"))
149 (should (ert-equal-buffer-return '(delete-and-extract-region 1 4)
150 "foo"
151 ""
152 "foo"))
153 (should (ert-equal-buffer-return '(point)
154 ert-Buf-point-char
155 t
156 1))
157 (should (ert-equal-buffer-return '(point)
158 (concat " " ert-Buf-point-char)
159 t
160 2))
161 (should (ert-equal-buffer-return '(region-beginning)
162 (concat ert-Buf-point-char " "
163 ert-Buf-mark-char)
164 t
165 1))
166 (should (ert-equal-buffer-return '(region-end)
167 (concat ert-Buf-mark-char " "
168 ert-Buf-point-char)
169 t
170 2))
171 (should (ert-equal-buffer-return '(following-char)
172 (concat ert-Buf-point-char "A")
173 t
174 ?A))
175 (should (ert-equal-buffer-return '(following-char)
176 (concat "A" ert-Buf-point-char)
177 t
178 0))
179 )