diff options
| author | Stefan Kangas | 2019-07-01 01:21:47 +0200 |
|---|---|---|
| committer | Eli Zaretskii | 2019-07-03 14:49:38 +0300 |
| commit | 22760ab357dd8124c856b76a545e562917872d78 (patch) | |
| tree | 0cc0df574e004ed5a1bda791e2d5c460fe191558 | |
| parent | ecd7d40a3be0b3b51743fc2c2e98dd14c6faca84 (diff) | |
| download | emacs-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.
| -rw-r--r-- | lisp/bookmark.el | 48 | ||||
| -rw-r--r-- | test/lisp/bookmark-resources/example.txt | 3 | ||||
| -rw-r--r-- | test/lisp/bookmark-resources/test.bmk | 10 | ||||
| -rw-r--r-- | test/lisp/bookmark-tests.el | 319 |
4 files changed, 348 insertions, 32 deletions
diff --git a/lisp/bookmark.el b/lisp/bookmark.el index bbef0a927dc..92d0da35949 100644 --- a/lisp/bookmark.el +++ b/lisp/bookmark.el | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | ;; Author: Karl Fogel <kfogel@red-bean.com> | 5 | ;; Author: Karl Fogel <kfogel@red-bean.com> |
| 6 | ;; Created: July, 1993 | 6 | ;; Created: July, 1993 |
| 7 | ;; Keywords: bookmarks, placeholders, annotations | 7 | ;; Keywords: convenience |
| 8 | 8 | ||
| 9 | ;; This file is part of GNU Emacs. | 9 | ;; This file is part of GNU Emacs. |
| 10 | 10 | ||
| @@ -50,8 +50,7 @@ | |||
| 50 | 50 | ||
| 51 | (defcustom bookmark-use-annotations nil | 51 | (defcustom bookmark-use-annotations nil |
| 52 | "If non-nil, setting a bookmark queries for an annotation in a buffer." | 52 | "If non-nil, setting a bookmark queries for an annotation in a buffer." |
| 53 | :type 'boolean | 53 | :type 'boolean) |
| 54 | :group 'bookmark) | ||
| 55 | 54 | ||
| 56 | 55 | ||
| 57 | (defcustom bookmark-save-flag t | 56 | (defcustom bookmark-save-flag t |
| @@ -71,14 +70,11 @@ behavior.) | |||
| 71 | 70 | ||
| 72 | To specify the file in which to save them, modify the variable | 71 | To specify the file in which to save them, modify the variable |
| 73 | `bookmark-default-file'." | 72 | `bookmark-default-file'." |
| 74 | :type '(choice (const nil) integer (other t)) | 73 | :type '(choice (const nil) integer (other t))) |
| 75 | :group 'bookmark) | ||
| 76 | 74 | ||
| 77 | 75 | ||
| 78 | (define-obsolete-variable-alias 'bookmark-old-default-file | 76 | (define-obsolete-variable-alias 'bookmark-old-default-file |
| 79 | 'bookmark-default-file "27.1") | 77 | 'bookmark-default-file "27.1") |
| 80 | |||
| 81 | |||
| 82 | (define-obsolete-variable-alias 'bookmark-file 'bookmark-default-file "27.1") | 78 | (define-obsolete-variable-alias 'bookmark-file 'bookmark-default-file "27.1") |
| 83 | (defcustom bookmark-default-file | 79 | (defcustom bookmark-default-file |
| 84 | (locate-user-emacs-file "bookmarks" ".emacs.bmk") | 80 | (locate-user-emacs-file "bookmarks" ".emacs.bmk") |
| @@ -86,8 +82,7 @@ To specify the file in which to save them, modify the variable | |||
| 86 | ;; The current default file is defined via the internal variable | 82 | ;; The current default file is defined via the internal variable |
| 87 | ;; `bookmark-bookmarks-timestamp'. This does not affect the value | 83 | ;; `bookmark-bookmarks-timestamp'. This does not affect the value |
| 88 | ;; of `bookmark-default-file'. | 84 | ;; of `bookmark-default-file'. |
| 89 | :type 'file | 85 | :type 'file) |
| 90 | :group 'bookmark) | ||
| 91 | 86 | ||
| 92 | (defcustom bookmark-watch-bookmark-file t | 87 | (defcustom bookmark-watch-bookmark-file t |
| 93 | "If non-nil watch the default bookmark file. | 88 | "If non-nil watch the default bookmark file. |
| @@ -108,35 +103,30 @@ just use the value of `version-control'." | |||
| 108 | :type '(choice (const :tag "If existing" nil) | 103 | :type '(choice (const :tag "If existing" nil) |
| 109 | (const :tag "Never" never) | 104 | (const :tag "Never" never) |
| 110 | (const :tag "Use value of option `version-control'" nospecial) | 105 | (const :tag "Use value of option `version-control'" nospecial) |
| 111 | (other :tag "Always" t)) | 106 | (other :tag "Always" t))) |
| 112 | :group 'bookmark) | ||
| 113 | 107 | ||
| 114 | 108 | ||
| 115 | (defcustom bookmark-completion-ignore-case t | 109 | (defcustom bookmark-completion-ignore-case t |
| 116 | "Non-nil means bookmark functions ignore case in completion." | 110 | "Non-nil means bookmark functions ignore case in completion." |
| 117 | :type 'boolean | 111 | :type 'boolean) |
| 118 | :group 'bookmark) | ||
| 119 | 112 | ||
| 120 | 113 | ||
| 121 | (defcustom bookmark-sort-flag t | 114 | (defcustom bookmark-sort-flag t |
| 122 | "Non-nil means that bookmarks will be displayed sorted by bookmark name. | 115 | "Non-nil means that bookmarks will be displayed sorted by bookmark name. |
| 123 | Otherwise they will be displayed in LIFO order (that is, most | 116 | Otherwise they will be displayed in LIFO order (that is, most |
| 124 | recently set ones come first, oldest ones come last)." | 117 | recently set ones come first, oldest ones come last)." |
| 125 | :type 'boolean | 118 | :type 'boolean) |
| 126 | :group 'bookmark) | ||
| 127 | 119 | ||
| 128 | 120 | ||
| 129 | (defcustom bookmark-automatically-show-annotations t | 121 | (defcustom bookmark-automatically-show-annotations t |
| 130 | "Non-nil means show annotations when jumping to a bookmark." | 122 | "Non-nil means show annotations when jumping to a bookmark." |
| 131 | :type 'boolean | 123 | :type 'boolean) |
| 132 | :group 'bookmark) | ||
| 133 | 124 | ||
| 134 | (defcustom bookmark-bmenu-use-header-line t | 125 | (defcustom bookmark-bmenu-use-header-line t |
| 135 | "Non-nil means to use an immovable header line. | 126 | "Non-nil means to use an immovable header line. |
| 136 | This is as opposed to inline text at the top of the buffer." | 127 | This is as opposed to inline text at the top of the buffer." |
| 137 | :version "24.4" | 128 | :version "24.4" |
| 138 | :type 'boolean | 129 | :type 'boolean) |
| 139 | :group 'bookmark) | ||
| 140 | 130 | ||
| 141 | (defconst bookmark-bmenu-inline-header-height 2 | 131 | (defconst bookmark-bmenu-inline-header-height 2 |
| 142 | "Number of lines used for the *Bookmark List* header. | 132 | "Number of lines used for the *Bookmark List* header. |
| @@ -150,36 +140,30 @@ This includes the annotations column.") | |||
| 150 | (defcustom bookmark-bmenu-file-column 30 | 140 | (defcustom bookmark-bmenu-file-column 30 |
| 151 | "Column at which to display filenames in a buffer listing bookmarks. | 141 | "Column at which to display filenames in a buffer listing bookmarks. |
| 152 | You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]." | 142 | You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]." |
| 153 | :type 'integer | 143 | :type 'integer) |
| 154 | :group 'bookmark) | ||
| 155 | 144 | ||
| 156 | 145 | ||
| 157 | (defcustom bookmark-bmenu-toggle-filenames t | 146 | (defcustom bookmark-bmenu-toggle-filenames t |
| 158 | "Non-nil means show filenames when listing bookmarks. | 147 | "Non-nil means show filenames when listing bookmarks. |
| 159 | A non-nil value may result in truncated bookmark names." | 148 | A non-nil value may result in truncated bookmark names." |
| 160 | :type 'boolean | 149 | :type 'boolean) |
| 161 | :group 'bookmark) | ||
| 162 | 150 | ||
| 163 | (defface bookmark-menu-bookmark | 151 | (defface bookmark-menu-bookmark |
| 164 | '((t (:weight bold))) | 152 | '((t (:weight bold))) |
| 165 | "Face used to highlight bookmark names in bookmark menu buffers." | 153 | "Face used to highlight bookmark names in bookmark menu buffers.") |
| 166 | :group 'bookmark) | ||
| 167 | 154 | ||
| 168 | (defcustom bookmark-menu-length 70 | 155 | (defcustom bookmark-menu-length 70 |
| 169 | "Maximum length of a bookmark name displayed on a popup menu." | 156 | "Maximum length of a bookmark name displayed on a popup menu." |
| 170 | :type 'integer | 157 | :type 'integer) |
| 171 | :group 'bookmark) | ||
| 172 | 158 | ||
| 173 | ;; FIXME: Is it really worth a customization option? | 159 | ;; FIXME: Is it really worth a customization option? |
| 174 | (defcustom bookmark-search-delay 0.2 | 160 | (defcustom bookmark-search-delay 0.2 |
| 175 | "Time before `bookmark-bmenu-search' updates the display." | 161 | "Time before `bookmark-bmenu-search' updates the display." |
| 176 | :group 'bookmark | ||
| 177 | :type 'number) | 162 | :type 'number) |
| 178 | 163 | ||
| 179 | (defface bookmark-menu-heading | 164 | (defface bookmark-menu-heading |
| 180 | '((t (:inherit font-lock-type-face))) | 165 | '((t (:inherit font-lock-type-face))) |
| 181 | "Face used to highlight the heading in bookmark menu buffers." | 166 | "Face used to highlight the heading in bookmark menu buffers." |
| 182 | :group 'bookmark | ||
| 183 | :version "22.1") | 167 | :version "22.1") |
| 184 | 168 | ||
| 185 | 169 | ||
| @@ -279,8 +263,6 @@ defaults to `bookmark-default-file' and MODTIME is its modification time.") | |||
| 279 | (defvar bookmark-file-coding-system nil | 263 | (defvar bookmark-file-coding-system nil |
| 280 | "The coding-system of the last loaded or saved bookmark file.") | 264 | "The coding-system of the last loaded or saved bookmark file.") |
| 281 | 265 | ||
| 282 | ;; more stuff added by db. | ||
| 283 | |||
| 284 | (defvar bookmark-current-bookmark nil | 266 | (defvar bookmark-current-bookmark nil |
| 285 | "Name of bookmark most recently used in the current file. | 267 | "Name of bookmark most recently used in the current file. |
| 286 | It is buffer local, used to make moving a bookmark forward | 268 | It is buffer local, used to make moving a bookmark forward |
| @@ -937,6 +919,8 @@ It takes one argument, the name of the bookmark, as a string.") | |||
| 937 | 919 | ||
| 938 | (defun bookmark-insert-annotation (bookmark-name-or-record) | 920 | (defun bookmark-insert-annotation (bookmark-name-or-record) |
| 939 | "Insert annotation for BOOKMARK-NAME-OR-RECORD at point." | 921 | "Insert annotation for BOOKMARK-NAME-OR-RECORD at point." |
| 922 | (when (not (bookmark-get-bookmark bookmark-name-or-record t)) | ||
| 923 | (error "Invalid bookmark: %s" bookmark-name-or-record)) | ||
| 940 | (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record)) | 924 | (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record)) |
| 941 | (let ((annotation (bookmark-get-annotation bookmark-name-or-record))) | 925 | (let ((annotation (bookmark-get-annotation bookmark-name-or-record))) |
| 942 | (if (and annotation (not (string-equal annotation ""))) | 926 | (if (and annotation (not (string-equal annotation ""))) |
| @@ -1449,7 +1433,7 @@ for a file, defaulting to the file defined by variable | |||
| 1449 | ;; Apparently `pp' has a poor algorithmic complexity, so this | 1433 | ;; Apparently `pp' has a poor algorithmic complexity, so this |
| 1450 | ;; scales a lot better. bug#4485. | 1434 | ;; scales a lot better. bug#4485. |
| 1451 | (dolist (i bookmark-alist) (pp i (current-buffer))) | 1435 | (dolist (i bookmark-alist) (pp i (current-buffer))) |
| 1452 | (insert ")") | 1436 | (insert ")\n") |
| 1453 | ;; Make sure the specified encoding can safely encode the | 1437 | ;; Make sure the specified encoding can safely encode the |
| 1454 | ;; bookmarks. If it cannot, suggest utf-8-emacs as default. | 1438 | ;; bookmarks. If it cannot, suggest utf-8-emacs as default. |
| 1455 | (with-coding-priority '(utf-8-emacs) | 1439 | (with-coding-priority '(utf-8-emacs) |
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 @@ | |||
| 1 | This 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. | ||
| 65 | Ensure a clean environment for testing, and do not change user | ||
| 66 | data 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. | ||
| 76 | Same as `with-bookmark-test' but also opens the resource file | ||
| 77 | example.txt in a buffer, which can be accessed by callers through | ||
| 78 | the 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. | ||
| 259 | Same as `with-bookmark-test' but also sets a temporary | ||
| 260 | `bookmark-default-file', evaluates BODY, and then runs the test | ||
| 261 | that 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 | ||