1
;;; buffer.el --- Test the test support for buffers
3
(add-to-list 'load-path ".")
4
(load "ert-buffer" nil t)
6
;; ****************************************************************************
9
(defun roundtrip-ert-Buf (in)
11
(ert-Buf--to-buffer (ert-Buf-from-string in))
12
(ert-Buf-string (ert-Buf-from-buffer))))
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 "")
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)
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")
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")
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")))
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))
65
;; ****************************************************************************
66
;; Advice `ert-completing-read'
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.")
73
(defun insert-reads ()
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)
82
(defun test-reads (inputs fun-args result)
83
(setq read-fun-args fun-args)
84
(ert-equal-buffer (insert-reads) "" result inputs))
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"))
118
;; ****************************************************************************
119
;; Test main functions
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"
130
(concat ert-Buf-point-char ert-Buf-mark-char)
132
(should (ert-equal-buffer (delete-region 1 4)
135
(should-error (ert-equal-buffer (delete-region 0 3)
137
"") :type 'args-out-of-range)
138
(should (ert-equal-buffer (goto-char 4)
140
(concat "foo" ert-Buf-point-char)))
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)
149
(should (ert-equal-buffer-return (delete-and-extract-region 1 4)
153
(should (ert-equal-buffer-return (point)
157
(should (ert-equal-buffer-return (point)
158
(concat " " ert-Buf-point-char)
161
(should (ert-equal-buffer-return (region-beginning)
162
(concat ert-Buf-point-char " "
166
(should (ert-equal-buffer-return (region-end)
167
(concat ert-Buf-mark-char " "
171
(should (ert-equal-buffer-return (following-char)
172
(concat ert-Buf-point-char "A")
175
(should (ert-equal-buffer-return (following-char)
176
(concat "A" ert-Buf-point-char)