aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorStefan Kangas2019-07-01 01:21:47 +0200
committerEli Zaretskii2019-07-03 14:49:38 +0300
commit22760ab357dd8124c856b76a545e562917872d78 (patch)
tree0cc0df574e004ed5a1bda791e2d5c460fe191558 /test
parentecd7d40a3be0b3b51743fc2c2e98dd14c6faca84 (diff)
downloademacs-22760ab357dd8124c856b76a545e562917872d78.tar.gz
emacs-22760ab357dd8124c856b76a545e562917872d78.zip
Add tests for bookmark.el (Bug#36452)
* test/lisp/bookmark-resources/example.txt: * test/lisp/bookmark-resources/test.bmk: * test/lisp/bookmark-tests.el: New files. * lisp/bookmark.el: Minor cleanups. (bookmark-insert-annotation): Signal error on invalid bookmark. (bookmark-write-file): Add newline at end of file.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/bookmark-resources/example.txt3
-rw-r--r--test/lisp/bookmark-resources/test.bmk10
-rw-r--r--test/lisp/bookmark-tests.el319
3 files changed, 332 insertions, 0 deletions
diff --git a/test/lisp/bookmark-resources/example.txt b/test/lisp/bookmark-resources/example.txt
new file mode 100644
index 00000000000..b66fc078269
--- /dev/null
+++ b/test/lisp/bookmark-resources/example.txt
@@ -0,0 +1,3 @@
1This text file is used by bookmark-tests.el
2
3;; example.txt ends here.
diff --git a/test/lisp/bookmark-resources/test.bmk b/test/lisp/bookmark-resources/test.bmk
new file mode 100644
index 00000000000..c238fe1c8d4
--- /dev/null
+++ b/test/lisp/bookmark-resources/test.bmk
@@ -0,0 +1,10 @@
1;;;; Emacs Bookmark Format Version 1 ;;;; -*- coding: utf-8-emacs -*-
2;;; This format is meant to be slightly human-readable;
3;;; nevertheless, you probably don't want to edit it.
4;;; -*- End Of Bookmark File Format Version Stamp -*-
5(("name"
6 (filename . "/some/file")
7 (front-context-string . "abc")
8 (rear-context-string . "def")
9 (position . 3))
10)
diff --git a/test/lisp/bookmark-tests.el b/test/lisp/bookmark-tests.el
new file mode 100644
index 00000000000..6fa0b1e98d0
--- /dev/null
+++ b/test/lisp/bookmark-tests.el
@@ -0,0 +1,319 @@
1;;; bookmark-tests.el --- Tests for bookmark.el -*- lexical-binding: t -*-
2
3;; Copyright (C) 2019 Free Software Foundation, Inc.
4
5;; Author: Stefan Kangas <stefankangas@gmail.com>
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software: you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation, either version 3 of the License, or
12;; (at your option) any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
21
22;;; Commentary:
23
24;;; Code:
25
26(require 'ert)
27(require 'bookmark)
28
29(defvar bookmark-tests-data-dir
30 (file-truename
31 (expand-file-name "bookmark-resources/"
32 (file-name-directory (or load-file-name
33 buffer-file-name))))
34 "Base directory of bookmark-tests.el data files.")
35
36(defvar bookmark-tests-bookmark-file
37 (expand-file-name "test.bmk" bookmark-tests-data-dir)
38 "Bookmark file used for testing.")
39
40(defvar bookmark-tests-example-file
41 ;; We use abbreviate-file-name here to match the behaviour of
42 ;; `bookmark-buffer-file-name'.
43 (abbreviate-file-name (expand-file-name "example.txt" bookmark-tests-data-dir))
44 "Example file used for testing.")
45
46;; The values below should match `bookmark-tests-bookmark-file'. We cache
47;; these values to speed up tests by avoiding unnecessary I/O. This
48;; makes tests run 5-10 times faster on my system.
49(eval-and-compile ; needed by `with-bookmark-test' macro
50 (defvar bookmark-tests-bookmark '("name"
51 (filename . "/some/file")
52 (front-context-string . "abc")
53 (rear-context-string . "def")
54 (position . 3))
55 "Cached value used in bookmark-tests.el."))
56
57(defvar bookmark-tests-cache-timestamp
58 (cons bookmark-tests-bookmark-file
59 (nth 5 (file-attributes
60 bookmark-tests-bookmark-file)))
61 "Cached value used in bookmark-tests.el.")
62
63(defmacro with-bookmark-test (&rest body)
64 "Create environment for testing bookmark.el and evaluate BODY.
65Ensure a clean environment for testing, and do not change user
66data when running tests interactively."
67 `(with-temp-buffer
68 (let ((bookmark-alist (quote (,(copy-sequence bookmark-tests-bookmark))))
69 (bookmark-default-file bookmark-tests-bookmark-file)
70 (bookmark-bookmarks-timestamp bookmark-tests-cache-timestamp)
71 bookmark-save-flag)
72 ,@body)))
73
74(defmacro with-bookmark-test-file (&rest body)
75 "Create environment for testing bookmark.el and evaluate BODY.
76Same as `with-bookmark-test' but also opens the resource file
77example.txt in a buffer, which can be accessed by callers through
78the lexically-bound variable `buffer'."
79 `(let ((buffer (find-file-noselect bookmark-tests-example-file)))
80 (unwind-protect
81 (with-bookmark-test
82 ,@body)
83 (kill-buffer buffer))))
84
85(ert-deftest bookmark-tests-all-names ()
86 (with-bookmark-test
87 (should (equal (bookmark-all-names) '("name")))))
88
89(ert-deftest bookmark-tests-get-bookmark ()
90 (with-bookmark-test
91 (should (equal (bookmark-get-bookmark "name") bookmark-tests-bookmark))))
92
93(ert-deftest bookmark-tests-get-bookmark-record ()
94 (with-bookmark-test
95 (should (equal (bookmark-get-bookmark-record "name") (cdr bookmark-tests-bookmark)))))
96
97(ert-deftest bookmark-tests-record-getters-and-setters-new ()
98 (with-temp-buffer
99 (let* ((buffer-file-name "test")
100 (bmk (bookmark-make-record)))
101 (bookmark-set-name bmk "foobar")
102 (should (equal (bookmark-name-from-full-record bmk) "foobar"))
103 (bookmark-set-filename bmk "file/name")
104 (should (equal (bookmark-get-filename bmk) "file/name"))
105 (bookmark-set-position bmk 123)
106 (should (equal (bookmark-get-position bmk) 123))
107 (bookmark-set-front-context-string bmk "front")
108 (should (equal (bookmark-get-front-context-string bmk) "front"))
109 (bookmark-set-rear-context-string bmk "rear")
110 (should (equal (bookmark-get-rear-context-string bmk) "rear"))
111 (bookmark-prop-set bmk 'filename "prop")
112 (should (equal (bookmark-prop-get bmk 'filename) "prop")))))
113
114(ert-deftest bookmark-tests-maybe-historicize-string ()
115 (let ((bookmark-history))
116 (bookmark-maybe-historicize-string "foo")
117 (should (equal (car bookmark-history) "foo"))))
118
119(ert-deftest bookmark-tests-make-record ()
120 (with-bookmark-test-file
121 (let* ((record `("example.txt" (filename . ,bookmark-tests-example-file)
122 (front-context-string . "is text file is ")
123 (rear-context-string)
124 (position . 3)
125 (defaults "example.txt"))))
126 (with-current-buffer buffer
127 (goto-char 3)
128 (should (equal (bookmark-make-record) record))
129 ;; calling twice gives same record
130 (should (equal (bookmark-make-record) record))))))
131
132(ert-deftest bookmark-tests-make-record-function ()
133 (with-bookmark-test
134 (let ((buffer-file-name "test"))
135 ;; Named bookmark
136 (let ((bookmark-make-record-function (lambda () '("<name>"))))
137 (should (equal (bookmark-make-record)
138 '("<name>"))))
139 ;; SECOND format
140 (let ((bookmark-make-record-function (lambda () '(((position . 2))))))
141 (should (equal (bookmark-make-record)
142 '("test" ((position . 2) (defaults "test"))))))
143 ;; CURRENT format
144 (let ((bookmark-make-record-function (lambda () '((position . 2)))))
145 (should (equal (bookmark-make-record)
146 '("test" (position . 2) (defaults "test"))))))))
147
148(ert-deftest bookmark-tests-set ()
149 (with-bookmark-test-file
150 (let ((bmk1 `("foo" (filename . ,bookmark-tests-example-file)
151 (front-context-string . "This text file i")
152 (rear-context-string)
153 (position . 1)))
154 (bmk2 `("foo" (filename . ,bookmark-tests-example-file)
155 (front-context-string)
156 (rear-context-string . ".txt ends here.\n")
157 (position . 72)))
158 bookmark-alist)
159 (with-current-buffer buffer
160 ;; 1. bookmark-set
161 ;; Set first bookmark
162 (goto-char (point-min))
163 (bookmark-set "foo")
164 (should (equal bookmark-alist (list bmk1)))
165 ;; Replace that bookmark
166 (goto-char (point-max))
167 (bookmark-set "foo")
168 (should (equal bookmark-alist (list bmk2)))
169 ;; Push another bookmark with the same name
170 (goto-char (point-min))
171 (bookmark-set "foo" t) ; NO-OVERWRITE is t
172 (should (equal bookmark-alist (list bmk1 bmk2)))
173
174 ;; 2. bookmark-set-no-overwrite
175 ;; Don't overwrite
176 (should-error (bookmark-set-no-overwrite "foo"))
177 ;; Set new bookmark
178 (setq bookmark-alist nil)
179 (bookmark-set-no-overwrite "foo")
180 (should (equal bookmark-alist (list bmk1)))
181 ;; Push another bookmark with the same name
182 (goto-char (point-max))
183 (bookmark-set-no-overwrite "foo" t) ; PUSH-BOOKMARK is t
184 (should (equal bookmark-alist (list bmk2 bmk1)))
185
186 ;; 3. bookmark-set-internal
187 (should-error (bookmark-set-internal "foo" "bar" t))))))
188
189(ert-deftest bookmark-tests-kill-line ()
190 (with-temp-buffer
191 (insert "foobar\n")
192 (goto-char (point-min))
193 (bookmark-kill-line)
194 (should (equal (buffer-string) "\n")))
195 (with-temp-buffer
196 (insert "foobar\n")
197 (goto-char (point-min))
198 (bookmark-kill-line t) ; including newline
199 (should (equal (buffer-string) ""))))
200
201(ert-deftest bookmark-tests-default-annotation-text ()
202 (should (stringp (bookmark-default-annotation-text "foo")))
203 (should (string-match "foo" (bookmark-default-annotation-text "foo"))))
204
205(ert-deftest bookmark-tests-insert-annotation ()
206 (with-bookmark-test
207 (should-error (bookmark-insert-annotation "a missing bookmark"))
208 (bookmark-insert-annotation "name")
209 (should (equal (buffer-string) (bookmark-default-annotation-text "name"))))
210 (with-bookmark-test
211 (bookmark-set-annotation "name" "some stuff")
212 (bookmark-insert-annotation "name")
213 (should (string-match "some stuff" (buffer-string)))))
214
215(ert-deftest bookmark-tests-edit-annotation ()
216 (with-bookmark-test
217 (bookmark-edit-annotation "name")
218 (insert "new text")
219 (bookmark-send-edited-annotation)
220 (should (equal (bookmark-get-annotation "name") "new text"))))
221
222(ert-deftest bookmark-tests-jump ()
223 (with-bookmark-test-file
224 (with-current-buffer buffer
225 (bookmark-set "test"))
226 (bookmark-jump "test")
227 (should (equal (current-buffer) buffer))
228 (should-error (bookmark-jump nil))))
229
230(ert-deftest bookmark-tests-insert-location ()
231 (with-bookmark-test
232 (bookmark-insert-location "name")
233 (should (equal (buffer-string) "/some/file"))))
234
235(ert-deftest bookmark-tests-location ()
236 (with-bookmark-test
237 (should (equal (bookmark-location "name") "/some/file"))))
238
239(ert-deftest bookmark-tests-rename ()
240 (with-bookmark-test
241 (bookmark-rename "name" "newname")
242 (should (equal (bookmark-all-names) '("newname")))))
243
244(ert-deftest bookmark-tests-insert ()
245 (with-bookmark-test-file
246 (with-current-buffer buffer
247 (bookmark-set "test"))
248 (bookmark-insert "test")
249 (should (string-match "^This text file is used by bookmark-tests.el"
250 (buffer-string)))))
251
252(ert-deftest bookmark-tests-delete ()
253 (with-bookmark-test
254 (bookmark-delete "name")
255 (should (equal bookmark-alist nil))))
256
257(defmacro with-bookmark-test-save-load (&rest body)
258 "Create environment for testing bookmark.el and evaluate BODY.
259Same as `with-bookmark-test' but also sets a temporary
260`bookmark-default-file', evaluates BODY, and then runs the test
261that saves and then loads the bookmark file."
262 `(with-bookmark-test
263 (let ((file (make-temp-file "bookmark-tests-")))
264 (unwind-protect
265 (let ((bookmark-default-file file)
266 (old-alist bookmark-alist))
267 ,@body
268 (bookmark-save nil file t)
269 (setq bookmark-alist nil)
270 (bookmark-load file nil t)
271 (should (equal bookmark-alist old-alist)))
272 (delete-file file)))))
273
274(defvar bookmark-tests-non-ascii-data
275 (concat "Здра́вствуйте!" "中文,普通话,汉语" "åäöøñ"
276 "こんにちは" "你好" "Dobrý deň"
277 "∀ p ∈ world • hello p □"
278 ;; These do not yield valid UTF-8 byte sequences.
279 ;; WARNING: If you try to evaluate the first one of these,
280 ;; there is a risk that Emacs will crash. Buyer beware.
281 (decode-coding-string "\xE3\x32\x9A\x36" 'chinese-gb18030)
282 (char-to-string (decode-char 'eight-bit #x81))))
283
284(ert-deftest bookmark-tests-save ()
285 (with-bookmark-test-save-load
286 ;; Just run the default test according to the macro.
287 t))
288
289(ert-deftest bookmark-tests-save/non-ascii-bookmark-name ()
290 (with-bookmark-test-save-load
291 (bookmark-set-name "name" bookmark-tests-non-ascii-data)))
292
293(ert-deftest bookmark-tests-save/non-ascii-annotation ()
294 (with-bookmark-test-save-load
295 (bookmark-set-annotation "name" bookmark-tests-non-ascii-data)))
296
297(ert-deftest bookmark-tests-maybe-rename ()
298 (let ((bookmark '("foo")))
299 (bookmark-maybe-rename bookmark '("foo"))
300 (should (equal bookmark '("foo<2>")))))
301
302(ert-deftest bookmark-tests-load ()
303 (with-bookmark-test
304 (should-error (bookmark-load "no/such/file"))
305 (let* ((bookmark-alist '()))
306 (bookmark-load bookmark-tests-bookmark-file nil t)
307 (should (equal bookmark-alist (list bookmark-tests-bookmark)))
308 (bookmark-load bookmark-tests-bookmark-file t t) ; OVERWRITE is t
309 (should (equal bookmark-alist (list bookmark-tests-bookmark)))
310 ;; Append
311 (bookmark-load bookmark-tests-bookmark-file nil t) ; OVERWRITE is nil
312 (should (equal bookmark-alist
313 (list bookmark-tests-bookmark
314 (cons "name<2>" (cdr bookmark-tests-bookmark))))))))
315
316;; TODO: Add tests for bookmark-bmenu.
317
318(provide 'bookmark-tests)
319;;; bookmark-tests.el ends here