* bug#36452: [PATCH] Add tests for bookmark.el @ 2019-06-30 23:32 Stefan Kangas 2019-07-02 16:41 ` Eli Zaretskii 0 siblings, 1 reply; 8+ messages in thread From: Stefan Kangas @ 2019-06-30 23:32 UTC (permalink / raw) To: 36452 [-- Attachment #1: Type: text/plain, Size: 323 bytes --] I've written tests for bookmark.el. Please see the attached patch. I did not write tests for bookmark-bmenu at this time, but I'm considering doing that next. I anyway figured it's better that this gets reviewed and installed before it gets any longer. Comments and suggestions are very welcome. Thanks, Stefan Kangas [-- Attachment #2: 0001-Add-tests-for-bookmark.el.patch --] [-- Type: text/x-patch, Size: 18208 bytes --] From c725363973f3439f4a768901b8ebe8ee9704d99c Mon Sep 17 00:00:00 2001 From: Stefan Kangas <stefankangas@gmail.com> Date: Mon, 1 Jul 2019 01:21:47 +0200 Subject: [PATCH] Add tests for bookmark.el * 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. --- lisp/bookmark.el | 46 ++--- test/lisp/bookmark-resources/example.txt | 3 + test/lisp/bookmark-resources/test.bmk | 10 ++ test/lisp/bookmark-tests.el | 283 +++++++++++++++++++++++++++++++ 4 files changed, 311 insertions(+), 31 deletions(-) create mode 100644 test/lisp/bookmark-resources/example.txt create mode 100644 test/lisp/bookmark-resources/test.bmk create mode 100644 test/lisp/bookmark-tests.el diff --git a/lisp/bookmark.el b/lisp/bookmark.el index bbef0a927d..7a845ed98d 100644 --- a/lisp/bookmark.el +++ b/lisp/bookmark.el @@ -4,7 +4,7 @@ ;; Author: Karl Fogel <kfogel@red-bean.com> ;; Created: July, 1993 -;; Keywords: bookmarks, placeholders, annotations +;; Keywords: convenience ;; This file is part of GNU Emacs. @@ -50,8 +50,7 @@ bookmark (defcustom bookmark-use-annotations nil "If non-nil, setting a bookmark queries for an annotation in a buffer." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-save-flag t @@ -71,14 +70,11 @@ bookmark-save-flag To specify the file in which to save them, modify the variable `bookmark-default-file'." - :type '(choice (const nil) integer (other t)) - :group 'bookmark) + :type '(choice (const nil) integer (other t))) (define-obsolete-variable-alias 'bookmark-old-default-file 'bookmark-default-file "27.1") - - (define-obsolete-variable-alias 'bookmark-file 'bookmark-default-file "27.1") (defcustom bookmark-default-file (locate-user-emacs-file "bookmarks" ".emacs.bmk") @@ -86,8 +82,7 @@ bookmark-default-file ;; The current default file is defined via the internal variable ;; `bookmark-bookmarks-timestamp'. This does not affect the value ;; of `bookmark-default-file'. - :type 'file - :group 'bookmark) + :type 'file) (defcustom bookmark-watch-bookmark-file t "If non-nil watch the default bookmark file. @@ -108,35 +103,30 @@ bookmark-version-control :type '(choice (const :tag "If existing" nil) (const :tag "Never" never) (const :tag "Use value of option `version-control'" nospecial) - (other :tag "Always" t)) - :group 'bookmark) + (other :tag "Always" t))) (defcustom bookmark-completion-ignore-case t "Non-nil means bookmark functions ignore case in completion." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-sort-flag t "Non-nil means that bookmarks will be displayed sorted by bookmark name. Otherwise they will be displayed in LIFO order (that is, most recently set ones come first, oldest ones come last)." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-automatically-show-annotations t "Non-nil means show annotations when jumping to a bookmark." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-bmenu-use-header-line t "Non-nil means to use an immovable header line. This is as opposed to inline text at the top of the buffer." :version "24.4" - :type 'boolean - :group 'bookmark) + :type 'boolean) (defconst bookmark-bmenu-inline-header-height 2 "Number of lines used for the *Bookmark List* header. @@ -150,36 +140,30 @@ bookmark-bmenu-marks-width (defcustom bookmark-bmenu-file-column 30 "Column at which to display filenames in a buffer listing bookmarks. You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]." - :type 'integer - :group 'bookmark) + :type 'integer) (defcustom bookmark-bmenu-toggle-filenames t "Non-nil means show filenames when listing bookmarks. A non-nil value may result in truncated bookmark names." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defface bookmark-menu-bookmark '((t (:weight bold))) - "Face used to highlight bookmark names in bookmark menu buffers." - :group 'bookmark) + "Face used to highlight bookmark names in bookmark menu buffers.") (defcustom bookmark-menu-length 70 "Maximum length of a bookmark name displayed on a popup menu." - :type 'integer - :group 'bookmark) + :type 'integer) ;; FIXME: Is it really worth a customization option? (defcustom bookmark-search-delay 0.2 "Time before `bookmark-bmenu-search' updates the display." - :group 'bookmark :type 'number) (defface bookmark-menu-heading '((t (:inherit font-lock-type-face))) "Face used to highlight the heading in bookmark menu buffers." - :group 'bookmark :version "22.1") @@ -279,8 +263,6 @@ bookmark-bookmarks-timestamp (defvar bookmark-file-coding-system nil "The coding-system of the last loaded or saved bookmark file.") -;; more stuff added by db. - (defvar bookmark-current-bookmark nil "Name of bookmark most recently used in the current file. It is buffer local, used to make moving a bookmark forward @@ -937,6 +919,8 @@ bookmark-edit-annotation-mode-map (defun bookmark-insert-annotation (bookmark-name-or-record) "Insert annotation for BOOKMARK-NAME-OR-RECORD at point." + (when (not (bookmark-get-bookmark bookmark-name-or-record t)) + (error "Invalid bookmark: %s" bookmark-name-or-record)) (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record)) (let ((annotation (bookmark-get-annotation bookmark-name-or-record))) (if (and annotation (not (string-equal annotation ""))) diff --git a/test/lisp/bookmark-resources/example.txt b/test/lisp/bookmark-resources/example.txt new file mode 100644 index 0000000000..b66fc07826 --- /dev/null +++ b/test/lisp/bookmark-resources/example.txt @@ -0,0 +1,3 @@ +This text file is used by bookmark-tests.el + +;; 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 0000000000..b2bff2303c --- /dev/null +++ b/test/lisp/bookmark-resources/test.bmk @@ -0,0 +1,10 @@ +;;;; Emacs Bookmark Format Version 1 ;;;; -*- coding: utf-8-emacs -*- +;;; This format is meant to be slightly human-readable; +;;; nevertheless, you probably don't want to edit it. +;;; -*- End Of Bookmark File Format Version Stamp -*- +(("name" + (filename . "/some/file") + (front-context-string . "abc") + (rear-context-string . "def") + (position . 3)) +) \ No newline at end of file diff --git a/test/lisp/bookmark-tests.el b/test/lisp/bookmark-tests.el new file mode 100644 index 0000000000..a239db8d37 --- /dev/null +++ b/test/lisp/bookmark-tests.el @@ -0,0 +1,283 @@ +;;; bookmark-tests.el --- Tests for bookmark.el -*- lexical-binding: t -*- + +;; Copyright (C) 2019 Free Software Foundation, Inc. + +;; Author: Stefan Kangas <stefankangas@gmail.com> + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;;; Code: + +(require 'ert) +(require 'bookmark) + +(defvar bookmark-tests-data-dir + (file-truename + (expand-file-name "bookmark-resources/" + (file-name-directory (or load-file-name + buffer-file-name)))) + "Base directory of bookmark-tests.el data files.") + +(defvar bookmark-tests-bookmark-file + (expand-file-name "test.bmk" bookmark-tests-data-dir) + "Bookmark file used for testing.") + +(defvar bookmark-tests-example-file + ;; We use abbreviate-file-name here to match the behaviour of + ;; `bookmark-buffer-file-name'. + (abbreviate-file-name (expand-file-name "example.txt" bookmark-tests-data-dir)) + "Example file used for testing.") + +;; The values below should match `bookmark-tests-bookmark-file'. We cache +;; these values to speed up tests by avoiding unnecessary I/O. This +;; makes tests run 5-10 times faster on my system. +(eval-and-compile ; needed by `with-bookmark-test' macro + (defvar bookmark-tests-bookmark '("name" + (filename . "/some/file") + (front-context-string . "abc") + (rear-context-string . "def") + (position . 3)) + "Cached value used in bookmark-tests.el.")) + +(defvar bookmark-tests-cache-timestamp + (cons bookmark-tests-bookmark-file + (nth 5 (file-attributes + bookmark-tests-bookmark-file))) + "Cached value used in bookmark-tests.el.") + +(defmacro with-bookmark-test (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Ensure a clean environment for testing, and do not change user +data when running tests interactively." + `(with-temp-buffer + (let ((bookmark-alist (quote (,(copy-sequence bookmark-tests-bookmark)))) + (bookmark-default-file bookmark-tests-bookmark-file) + (bookmark-bookmarks-timestamp bookmark-tests-cache-timestamp) + bookmark-save-flag) + ,@body))) + +(defmacro with-bookmark-test-file (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Same as `with-bookmark-test' but also opens the resource file +example.txt in a buffer, which can be accessed by callers through +the lexically-bound variable `buffer'." + `(let ((buffer (find-file-noselect bookmark-tests-example-file))) + (unwind-protect + (with-bookmark-test + ,@body) + (kill-buffer buffer)))) + +(ert-deftest bookmark-tests-all-names () + (with-bookmark-test + (should (equal (bookmark-all-names) '("name"))))) + +(ert-deftest bookmark-tests-get-bookmark () + (with-bookmark-test + (should (equal (bookmark-get-bookmark "name") bookmark-tests-bookmark)))) + +(ert-deftest bookmark-tests-get-bookmark-record () + (with-bookmark-test + (should (equal (bookmark-get-bookmark-record "name") (cdr bookmark-tests-bookmark))))) + +(ert-deftest bookmark-tests-record-getters-and-setters-new () + (with-temp-buffer + (let* ((buffer-file-name "test") + (bmk (bookmark-make-record))) + (bookmark-set-name bmk "foobar") + (should (equal (bookmark-name-from-full-record bmk) "foobar")) + (bookmark-set-filename bmk "file/name") + (should (equal (bookmark-get-filename bmk) "file/name")) + (bookmark-set-position bmk 123) + (should (equal (bookmark-get-position bmk) 123)) + (bookmark-set-front-context-string bmk "front") + (should (equal (bookmark-get-front-context-string bmk) "front")) + (bookmark-set-rear-context-string bmk "rear") + (should (equal (bookmark-get-rear-context-string bmk) "rear")) + (bookmark-prop-set bmk 'filename "prop") + (should (equal (bookmark-prop-get bmk 'filename) "prop"))))) + +(ert-deftest bookmark-tests-maybe-historicize-string () + (let ((bookmark-history)) + (bookmark-maybe-historicize-string "foo") + (should (equal (car bookmark-history) "foo")))) + +(ert-deftest bookmark-tests-make-record () + (with-bookmark-test-file + (let* ((record `("example.txt" (filename . ,bookmark-tests-example-file) + (front-context-string . "is text file is ") + (rear-context-string) + (position . 3) + (defaults "example.txt")))) + (with-current-buffer buffer + (goto-char 3) + (should (equal (bookmark-make-record) record)) + ;; calling twice gives same record + (should (equal (bookmark-make-record) record)))))) + +(ert-deftest bookmark-tests-make-record-function () + (with-bookmark-test + (let ((buffer-file-name "test")) + ;; Named bookmark + (let ((bookmark-make-record-function (lambda () '("<name>")))) + (should (equal (bookmark-make-record) + '("<name>")))) + ;; SECOND format + (let ((bookmark-make-record-function (lambda () '(((position . 2)))))) + (should (equal (bookmark-make-record) + '("test" ((position . 2) (defaults "test")))))) + ;; CURRENT format + (let ((bookmark-make-record-function (lambda () '((position . 2))))) + (should (equal (bookmark-make-record) + '("test" (position . 2) (defaults "test")))))))) + +(ert-deftest bookmark-tests-set () + (with-bookmark-test-file + (let ((bmk1 `("foo" (filename . ,bookmark-tests-example-file) + (front-context-string . "This text file i") + (rear-context-string) + (position . 1))) + (bmk2 `("foo" (filename . ,bookmark-tests-example-file) + (front-context-string) + (rear-context-string . ".txt ends here.\n") + (position . 72))) + bookmark-alist) + (with-current-buffer buffer + ;; Set first bookmark + (goto-char (point-min)) + (bookmark-set "foo") + (should (equal bookmark-alist (list bmk1))) + ;; Replace that bookmark + (goto-char (point-max)) + (bookmark-set "foo") + (should (equal bookmark-alist (list bmk2))) + ;; Push another bookmark with the same name + (goto-char (point-min)) + (bookmark-set "foo" t) ; NO-OVERWRITE is t + (should (equal bookmark-alist (list bmk1 bmk2))) + (should-error (bookmark-set-no-overwrite "foo")) + ;; Reset bookmarks; set new bookmark + (setq bookmark-alist nil) + (bookmark-set-no-overwrite "foo") + (should (equal bookmark-alist (list bmk1))) + ;; Push another bookmark with the same name + (goto-char (point-max)) + (bookmark-set-no-overwrite "foo" t) ; PUSH-BOOKMARK is t + (should (equal bookmark-alist (list bmk2 bmk1))))))) + +(ert-deftest bookmark-tests-kill-line () + (with-temp-buffer + (insert "foobar\n") + (goto-char (point-min)) + (bookmark-kill-line) + (should (equal (buffer-string) "\n"))) + (with-temp-buffer + (insert "foobar\n") + (goto-char (point-min)) + (bookmark-kill-line t) ; including newline + (should (equal (buffer-string) "")))) + +(ert-deftest bookmark-tests-default-annotation-text () + (should (stringp (bookmark-default-annotation-text "foo"))) + (should (string-match "foo" (bookmark-default-annotation-text "foo")))) + +(ert-deftest bookmark-tests-insert-annotation () + (with-bookmark-test + (should-error (bookmark-insert-annotation "a missing bookmark")) + (bookmark-insert-annotation "name") + (should (equal (buffer-string) (bookmark-default-annotation-text "name")))) + (with-bookmark-test + (bookmark-set-annotation "name" "some stuff") + (bookmark-insert-annotation "name") + (should (string-match "some stuff" (buffer-string))))) + +(ert-deftest bookmark-tests-edit-annotation () + (with-bookmark-test + (bookmark-edit-annotation "name") + (insert "new text") + (bookmark-send-edited-annotation) + (should (equal (bookmark-get-annotation "name") "new text")))) + +(ert-deftest bookmark-tests-jump () + (with-bookmark-test-file + (with-current-buffer buffer + (bookmark-set "test")) + (bookmark-jump "test") + (should (equal (current-buffer) buffer)))) + +(ert-deftest bookmark-tests-insert-location () + (with-bookmark-test + (bookmark-insert-location "name") + (should (equal (buffer-string) "/some/file")))) + +(ert-deftest bookmark-tests-location () + (with-bookmark-test + (should (equal (bookmark-location "name") "/some/file")))) + +(ert-deftest bookmark-tests-rename () + (with-bookmark-test + (bookmark-rename "name" "newname") + (should (equal (bookmark-all-names) '("newname"))))) + +(ert-deftest bookmark-tests-insert () + (with-bookmark-test-file + (with-current-buffer buffer + (bookmark-set "test")) + (bookmark-insert "test") + (should (string-match "^This text file is used by bookmark-tests.el" + (buffer-string))))) + +(ert-deftest bookmark-tests-delete () + (with-bookmark-test + (bookmark-delete "name") + (should (equal bookmark-alist nil)))) + +(ert-deftest bookmark-tests-save () + (with-bookmark-test + (let ((file (make-temp-file "bookmark-tests"))) + (unwind-protect + (let ((bookmark-default-file file) + (old-alist bookmark-alist)) + (bookmark-save nil file t) + (setq bookmark-alist nil) + (bookmark-load file nil t) + (should (equal bookmark-alist old-alist))) + (delete-file file))))) + +(ert-deftest bookmark-tests-maybe-rename () + (let ((bookmark '("foo"))) + (bookmark-maybe-rename bookmark '("foo")) + (should (equal bookmark '("foo<2>"))))) + +(ert-deftest bookmark-tests-load () + (with-bookmark-test + (should-error (bookmark-load "no/such/file")) + (let* ((bookmark-alist '())) + (bookmark-load bookmark-tests-bookmark-file nil t) + (should (equal bookmark-alist (list bookmark-tests-bookmark))) + (bookmark-load bookmark-tests-bookmark-file t t) ; OVERWRITE is t + (should (equal bookmark-alist (list bookmark-tests-bookmark))) + ;; Append + (bookmark-load bookmark-tests-bookmark-file nil t) ; OVERWRITE is nil + (should (equal bookmark-alist + (list bookmark-tests-bookmark + (cons "name<2>" (cdr bookmark-tests-bookmark)))))))) + +;; TODO: Add tests for bookmark-bmenu. + +(provide 'bookmark-tests) +;;; bookmark-tests.el ends here -- 2.11.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* bug#36452: [PATCH] Add tests for bookmark.el 2019-06-30 23:32 bug#36452: [PATCH] Add tests for bookmark.el Stefan Kangas @ 2019-07-02 16:41 ` Eli Zaretskii 2019-07-03 6:33 ` Stefan Kangas 0 siblings, 1 reply; 8+ messages in thread From: Eli Zaretskii @ 2019-07-02 16:41 UTC (permalink / raw) To: Stefan Kangas; +Cc: 36452 > From: Stefan Kangas <stefan@marxist.se> > Date: Mon, 1 Jul 2019 01:32:56 +0200 > > I've written tests for bookmark.el. Please see the attached patch. Thanks. Can you tell the approximate coverage of bookmark functionality by these tests? One particular aspect of the bookmarks that I'd like to see tested is non-ASCII bookmark names and annotations. The code includes some slightly involved logic for determining how to encode the info on disk, so I wonder how well does that work in practice. > --- /dev/null > +++ b/test/lisp/bookmark-resources/test.bmk > @@ -0,0 +1,10 @@ > +;;;; Emacs Bookmark Format Version 1 ;;;; -*- coding: utf-8-emacs -*- > +;;; This format is meant to be slightly human-readable; > +;;; nevertheless, you probably don't want to edit it. > +;;; -*- End Of Bookmark File Format Version Stamp -*- > +(("name" > + (filename . "/some/file") > + (front-context-string . "abc") > + (rear-context-string . "def") > + (position . 3)) > +) > \ No newline at end of file Is this lack of newline intentional? ^ permalink raw reply [flat|nested] 8+ messages in thread
* bug#36452: [PATCH] Add tests for bookmark.el 2019-07-02 16:41 ` Eli Zaretskii @ 2019-07-03 6:33 ` Stefan Kangas 2019-07-03 6:59 ` Eli Zaretskii 0 siblings, 1 reply; 8+ messages in thread From: Stefan Kangas @ 2019-07-03 6:33 UTC (permalink / raw) To: Eli Zaretskii; +Cc: 36452 [-- Attachment #1: Type: text/plain, Size: 1441 bytes --] Eli Zaretskii <eliz@gnu.org> writes: > Thanks. Can you tell the approximate coverage of bookmark > functionality by these tests? All core functionality is tested. I didn't test interactive stuff like bookmark-completing-read, nor did I bother testing the ancient code that upgrades from the pre-1995 bookmark format. Other than that, I would say that it's comprehensive. Running the tests it with M-x testcover-* unfortunately does not give an exact figure, but I don't see any important code paths that have been missed. > One particular aspect of the bookmarks that I'd like to see tested is > non-ASCII bookmark names and annotations. The code includes some > slightly involved logic for determining how to encode the info on > disk, so I wonder how well does that work in practice. OK. I added tests for that in the attached patch using strings manually copied from HELLO. It works just fine to save and then load bookmarks for the set of languages I chose to include characters from. That is, we have the same before bookmark-save as we get after bookmark-load. > > \ No newline at end of file > > Is this lack of newline intentional? Yes. This is how this file is written by `bookmark-write-file'. However, I think it would be nice if it did add the newline (e.g. for files under VC), so I changed that here. Thank you for your review. Please find attached an updated version of the patch. Best regards, Stefan Kangas [-- Attachment #2: 0001-Add-tests-for-bookmark.el-Bug-36452.patch --] [-- Type: application/octet-stream, Size: 19801 bytes --] From b15feecb41e7b0392bc3d301ff3fa7bc68a7942f Mon Sep 17 00:00:00 2001 From: Stefan Kangas <stefankangas@gmail.com> Date: Mon, 1 Jul 2019 01:21:47 +0200 Subject: [PATCH] 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. --- lisp/bookmark.el | 48 ++-- test/lisp/bookmark-resources/example.txt | 3 + test/lisp/bookmark-resources/test.bmk | 10 + test/lisp/bookmark-tests.el | 314 +++++++++++++++++++++++ 4 files changed, 343 insertions(+), 32 deletions(-) create mode 100644 test/lisp/bookmark-resources/example.txt create mode 100644 test/lisp/bookmark-resources/test.bmk create mode 100644 test/lisp/bookmark-tests.el diff --git a/lisp/bookmark.el b/lisp/bookmark.el index bbef0a927d..92d0da3594 100644 --- a/lisp/bookmark.el +++ b/lisp/bookmark.el @@ -4,7 +4,7 @@ ;; Author: Karl Fogel <kfogel@red-bean.com> ;; Created: July, 1993 -;; Keywords: bookmarks, placeholders, annotations +;; Keywords: convenience ;; This file is part of GNU Emacs. @@ -50,8 +50,7 @@ bookmark (defcustom bookmark-use-annotations nil "If non-nil, setting a bookmark queries for an annotation in a buffer." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-save-flag t @@ -71,14 +70,11 @@ bookmark-save-flag To specify the file in which to save them, modify the variable `bookmark-default-file'." - :type '(choice (const nil) integer (other t)) - :group 'bookmark) + :type '(choice (const nil) integer (other t))) (define-obsolete-variable-alias 'bookmark-old-default-file 'bookmark-default-file "27.1") - - (define-obsolete-variable-alias 'bookmark-file 'bookmark-default-file "27.1") (defcustom bookmark-default-file (locate-user-emacs-file "bookmarks" ".emacs.bmk") @@ -86,8 +82,7 @@ bookmark-default-file ;; The current default file is defined via the internal variable ;; `bookmark-bookmarks-timestamp'. This does not affect the value ;; of `bookmark-default-file'. - :type 'file - :group 'bookmark) + :type 'file) (defcustom bookmark-watch-bookmark-file t "If non-nil watch the default bookmark file. @@ -108,35 +103,30 @@ bookmark-version-control :type '(choice (const :tag "If existing" nil) (const :tag "Never" never) (const :tag "Use value of option `version-control'" nospecial) - (other :tag "Always" t)) - :group 'bookmark) + (other :tag "Always" t))) (defcustom bookmark-completion-ignore-case t "Non-nil means bookmark functions ignore case in completion." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-sort-flag t "Non-nil means that bookmarks will be displayed sorted by bookmark name. Otherwise they will be displayed in LIFO order (that is, most recently set ones come first, oldest ones come last)." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-automatically-show-annotations t "Non-nil means show annotations when jumping to a bookmark." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-bmenu-use-header-line t "Non-nil means to use an immovable header line. This is as opposed to inline text at the top of the buffer." :version "24.4" - :type 'boolean - :group 'bookmark) + :type 'boolean) (defconst bookmark-bmenu-inline-header-height 2 "Number of lines used for the *Bookmark List* header. @@ -150,36 +140,30 @@ bookmark-bmenu-marks-width (defcustom bookmark-bmenu-file-column 30 "Column at which to display filenames in a buffer listing bookmarks. You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]." - :type 'integer - :group 'bookmark) + :type 'integer) (defcustom bookmark-bmenu-toggle-filenames t "Non-nil means show filenames when listing bookmarks. A non-nil value may result in truncated bookmark names." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defface bookmark-menu-bookmark '((t (:weight bold))) - "Face used to highlight bookmark names in bookmark menu buffers." - :group 'bookmark) + "Face used to highlight bookmark names in bookmark menu buffers.") (defcustom bookmark-menu-length 70 "Maximum length of a bookmark name displayed on a popup menu." - :type 'integer - :group 'bookmark) + :type 'integer) ;; FIXME: Is it really worth a customization option? (defcustom bookmark-search-delay 0.2 "Time before `bookmark-bmenu-search' updates the display." - :group 'bookmark :type 'number) (defface bookmark-menu-heading '((t (:inherit font-lock-type-face))) "Face used to highlight the heading in bookmark menu buffers." - :group 'bookmark :version "22.1") @@ -279,8 +263,6 @@ bookmark-bookmarks-timestamp (defvar bookmark-file-coding-system nil "The coding-system of the last loaded or saved bookmark file.") -;; more stuff added by db. - (defvar bookmark-current-bookmark nil "Name of bookmark most recently used in the current file. It is buffer local, used to make moving a bookmark forward @@ -937,6 +919,8 @@ bookmark-edit-annotation-mode-map (defun bookmark-insert-annotation (bookmark-name-or-record) "Insert annotation for BOOKMARK-NAME-OR-RECORD at point." + (when (not (bookmark-get-bookmark bookmark-name-or-record t)) + (error "Invalid bookmark: %s" bookmark-name-or-record)) (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record)) (let ((annotation (bookmark-get-annotation bookmark-name-or-record))) (if (and annotation (not (string-equal annotation ""))) @@ -1449,7 +1433,7 @@ bookmark-write-file ;; Apparently `pp' has a poor algorithmic complexity, so this ;; scales a lot better. bug#4485. (dolist (i bookmark-alist) (pp i (current-buffer))) - (insert ")") + (insert ")\n") ;; Make sure the specified encoding can safely encode the ;; bookmarks. If it cannot, suggest utf-8-emacs as default. (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 0000000000..b66fc07826 --- /dev/null +++ b/test/lisp/bookmark-resources/example.txt @@ -0,0 +1,3 @@ +This text file is used by bookmark-tests.el + +;; 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 0000000000..c238fe1c8d --- /dev/null +++ b/test/lisp/bookmark-resources/test.bmk @@ -0,0 +1,10 @@ +;;;; Emacs Bookmark Format Version 1 ;;;; -*- coding: utf-8-emacs -*- +;;; This format is meant to be slightly human-readable; +;;; nevertheless, you probably don't want to edit it. +;;; -*- End Of Bookmark File Format Version Stamp -*- +(("name" + (filename . "/some/file") + (front-context-string . "abc") + (rear-context-string . "def") + (position . 3)) +) diff --git a/test/lisp/bookmark-tests.el b/test/lisp/bookmark-tests.el new file mode 100644 index 0000000000..a423f7d897 --- /dev/null +++ b/test/lisp/bookmark-tests.el @@ -0,0 +1,314 @@ +;;; bookmark-tests.el --- Tests for bookmark.el -*- lexical-binding: t -*- + +;; Copyright (C) 2019 Free Software Foundation, Inc. + +;; Author: Stefan Kangas <stefankangas@gmail.com> + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;;; Code: + +(require 'ert) +(require 'bookmark) + +(defvar bookmark-tests-data-dir + (file-truename + (expand-file-name "bookmark-resources/" + (file-name-directory (or load-file-name + buffer-file-name)))) + "Base directory of bookmark-tests.el data files.") + +(defvar bookmark-tests-bookmark-file + (expand-file-name "test.bmk" bookmark-tests-data-dir) + "Bookmark file used for testing.") + +(defvar bookmark-tests-example-file + ;; We use abbreviate-file-name here to match the behaviour of + ;; `bookmark-buffer-file-name'. + (abbreviate-file-name (expand-file-name "example.txt" bookmark-tests-data-dir)) + "Example file used for testing.") + +;; The values below should match `bookmark-tests-bookmark-file'. We cache +;; these values to speed up tests by avoiding unnecessary I/O. This +;; makes tests run 5-10 times faster on my system. +(eval-and-compile ; needed by `with-bookmark-test' macro + (defvar bookmark-tests-bookmark '("name" + (filename . "/some/file") + (front-context-string . "abc") + (rear-context-string . "def") + (position . 3)) + "Cached value used in bookmark-tests.el.")) + +(defvar bookmark-tests-cache-timestamp + (cons bookmark-tests-bookmark-file + (nth 5 (file-attributes + bookmark-tests-bookmark-file))) + "Cached value used in bookmark-tests.el.") + +(defmacro with-bookmark-test (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Ensure a clean environment for testing, and do not change user +data when running tests interactively." + `(with-temp-buffer + (let ((bookmark-alist (quote (,(copy-sequence bookmark-tests-bookmark)))) + (bookmark-default-file bookmark-tests-bookmark-file) + (bookmark-bookmarks-timestamp bookmark-tests-cache-timestamp) + bookmark-save-flag) + ,@body))) + +(defmacro with-bookmark-test-file (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Same as `with-bookmark-test' but also opens the resource file +example.txt in a buffer, which can be accessed by callers through +the lexically-bound variable `buffer'." + `(let ((buffer (find-file-noselect bookmark-tests-example-file))) + (unwind-protect + (with-bookmark-test + ,@body) + (kill-buffer buffer)))) + +(ert-deftest bookmark-tests-all-names () + (with-bookmark-test + (should (equal (bookmark-all-names) '("name"))))) + +(ert-deftest bookmark-tests-get-bookmark () + (with-bookmark-test + (should (equal (bookmark-get-bookmark "name") bookmark-tests-bookmark)))) + +(ert-deftest bookmark-tests-get-bookmark-record () + (with-bookmark-test + (should (equal (bookmark-get-bookmark-record "name") (cdr bookmark-tests-bookmark))))) + +(ert-deftest bookmark-tests-record-getters-and-setters-new () + (with-temp-buffer + (let* ((buffer-file-name "test") + (bmk (bookmark-make-record))) + (bookmark-set-name bmk "foobar") + (should (equal (bookmark-name-from-full-record bmk) "foobar")) + (bookmark-set-filename bmk "file/name") + (should (equal (bookmark-get-filename bmk) "file/name")) + (bookmark-set-position bmk 123) + (should (equal (bookmark-get-position bmk) 123)) + (bookmark-set-front-context-string bmk "front") + (should (equal (bookmark-get-front-context-string bmk) "front")) + (bookmark-set-rear-context-string bmk "rear") + (should (equal (bookmark-get-rear-context-string bmk) "rear")) + (bookmark-prop-set bmk 'filename "prop") + (should (equal (bookmark-prop-get bmk 'filename) "prop"))))) + +(ert-deftest bookmark-tests-maybe-historicize-string () + (let ((bookmark-history)) + (bookmark-maybe-historicize-string "foo") + (should (equal (car bookmark-history) "foo")))) + +(ert-deftest bookmark-tests-make-record () + (with-bookmark-test-file + (let* ((record `("example.txt" (filename . ,bookmark-tests-example-file) + (front-context-string . "is text file is ") + (rear-context-string) + (position . 3) + (defaults "example.txt")))) + (with-current-buffer buffer + (goto-char 3) + (should (equal (bookmark-make-record) record)) + ;; calling twice gives same record + (should (equal (bookmark-make-record) record)))))) + +(ert-deftest bookmark-tests-make-record-function () + (with-bookmark-test + (let ((buffer-file-name "test")) + ;; Named bookmark + (let ((bookmark-make-record-function (lambda () '("<name>")))) + (should (equal (bookmark-make-record) + '("<name>")))) + ;; SECOND format + (let ((bookmark-make-record-function (lambda () '(((position . 2)))))) + (should (equal (bookmark-make-record) + '("test" ((position . 2) (defaults "test")))))) + ;; CURRENT format + (let ((bookmark-make-record-function (lambda () '((position . 2))))) + (should (equal (bookmark-make-record) + '("test" (position . 2) (defaults "test")))))))) + +(ert-deftest bookmark-tests-set () + (with-bookmark-test-file + (let ((bmk1 `("foo" (filename . ,bookmark-tests-example-file) + (front-context-string . "This text file i") + (rear-context-string) + (position . 1))) + (bmk2 `("foo" (filename . ,bookmark-tests-example-file) + (front-context-string) + (rear-context-string . ".txt ends here.\n") + (position . 72))) + bookmark-alist) + (with-current-buffer buffer + ;; 1. bookmark-set + ;; Set first bookmark + (goto-char (point-min)) + (bookmark-set "foo") + (should (equal bookmark-alist (list bmk1))) + ;; Replace that bookmark + (goto-char (point-max)) + (bookmark-set "foo") + (should (equal bookmark-alist (list bmk2))) + ;; Push another bookmark with the same name + (goto-char (point-min)) + (bookmark-set "foo" t) ; NO-OVERWRITE is t + (should (equal bookmark-alist (list bmk1 bmk2))) + + ;; 2. bookmark-set-no-overwrite + ;; Don't overwrite + (should-error (bookmark-set-no-overwrite "foo")) + ;; Set new bookmark + (setq bookmark-alist nil) + (bookmark-set-no-overwrite "foo") + (should (equal bookmark-alist (list bmk1))) + ;; Push another bookmark with the same name + (goto-char (point-max)) + (bookmark-set-no-overwrite "foo" t) ; PUSH-BOOKMARK is t + (should (equal bookmark-alist (list bmk2 bmk1))) + + ;; 3. bookmark-set-internal + (should-error (bookmark-set-internal "foo" "bar" t)))))) + +(ert-deftest bookmark-tests-kill-line () + (with-temp-buffer + (insert "foobar\n") + (goto-char (point-min)) + (bookmark-kill-line) + (should (equal (buffer-string) "\n"))) + (with-temp-buffer + (insert "foobar\n") + (goto-char (point-min)) + (bookmark-kill-line t) ; including newline + (should (equal (buffer-string) "")))) + +(ert-deftest bookmark-tests-default-annotation-text () + (should (stringp (bookmark-default-annotation-text "foo"))) + (should (string-match "foo" (bookmark-default-annotation-text "foo")))) + +(ert-deftest bookmark-tests-insert-annotation () + (with-bookmark-test + (should-error (bookmark-insert-annotation "a missing bookmark")) + (bookmark-insert-annotation "name") + (should (equal (buffer-string) (bookmark-default-annotation-text "name")))) + (with-bookmark-test + (bookmark-set-annotation "name" "some stuff") + (bookmark-insert-annotation "name") + (should (string-match "some stuff" (buffer-string))))) + +(ert-deftest bookmark-tests-edit-annotation () + (with-bookmark-test + (bookmark-edit-annotation "name") + (insert "new text") + (bookmark-send-edited-annotation) + (should (equal (bookmark-get-annotation "name") "new text")))) + +(ert-deftest bookmark-tests-jump () + (with-bookmark-test-file + (with-current-buffer buffer + (bookmark-set "test")) + (bookmark-jump "test") + (should (equal (current-buffer) buffer)) + (should-error (bookmark-jump nil)))) + +(ert-deftest bookmark-tests-insert-location () + (with-bookmark-test + (bookmark-insert-location "name") + (should (equal (buffer-string) "/some/file")))) + +(ert-deftest bookmark-tests-location () + (with-bookmark-test + (should (equal (bookmark-location "name") "/some/file")))) + +(ert-deftest bookmark-tests-rename () + (with-bookmark-test + (bookmark-rename "name" "newname") + (should (equal (bookmark-all-names) '("newname"))))) + +(ert-deftest bookmark-tests-insert () + (with-bookmark-test-file + (with-current-buffer buffer + (bookmark-set "test")) + (bookmark-insert "test") + (should (string-match "^This text file is used by bookmark-tests.el" + (buffer-string))))) + +(ert-deftest bookmark-tests-delete () + (with-bookmark-test + (bookmark-delete "name") + (should (equal bookmark-alist nil)))) + +(defmacro with-bookmark-test-save-load (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Same as `with-bookmark-test' but also sets a temporary +`bookmark-default-file', evaluates BODY, and then runs the test +that saves and then loads the bookmark file." + `(with-bookmark-test + (let ((file (make-temp-file "bookmark-tests-"))) + (unwind-protect + (let ((bookmark-default-file file) + (old-alist bookmark-alist)) + ,@body + (bookmark-save nil file t) + (setq bookmark-alist nil) + (bookmark-load file nil t) + (should (equal bookmark-alist old-alist))) + (delete-file file))))) + +(defvar bookmark-tests-non-ascii-data + (concat "Здра́вствуйте!" "中文,普通话,汉语" "åäöøñ" + "こんにちは" "你好" "Dobrý deň" + "∀ p ∈ world • hello p □")) + +(ert-deftest bookmark-tests-save () + (with-bookmark-test-save-load + ;; Just run the default test according to the macro. + t)) + +(ert-deftest bookmark-tests-save/non-ascii-bookmark-name () + (with-bookmark-test-save-load + (bookmark-set-name "name" bookmark-tests-non-ascii-data))) + +(ert-deftest bookmark-tests-save/non-ascii-annotation () + (with-bookmark-test-save-load + (bookmark-set-annotation "name" bookmark-tests-non-ascii-data))) + +(ert-deftest bookmark-tests-maybe-rename () + (let ((bookmark '("foo"))) + (bookmark-maybe-rename bookmark '("foo")) + (should (equal bookmark '("foo<2>"))))) + +(ert-deftest bookmark-tests-load () + (with-bookmark-test + (should-error (bookmark-load "no/such/file")) + (let* ((bookmark-alist '())) + (bookmark-load bookmark-tests-bookmark-file nil t) + (should (equal bookmark-alist (list bookmark-tests-bookmark))) + (bookmark-load bookmark-tests-bookmark-file t t) ; OVERWRITE is t + (should (equal bookmark-alist (list bookmark-tests-bookmark))) + ;; Append + (bookmark-load bookmark-tests-bookmark-file nil t) ; OVERWRITE is nil + (should (equal bookmark-alist + (list bookmark-tests-bookmark + (cons "name<2>" (cdr bookmark-tests-bookmark)))))))) + +;; TODO: Add tests for bookmark-bmenu. + +(provide 'bookmark-tests) +;;; bookmark-tests.el ends here -- 2.21.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* bug#36452: [PATCH] Add tests for bookmark.el 2019-07-03 6:33 ` Stefan Kangas @ 2019-07-03 6:59 ` Eli Zaretskii 2019-07-03 11:01 ` Stefan Kangas 0 siblings, 1 reply; 8+ messages in thread From: Eli Zaretskii @ 2019-07-03 6:59 UTC (permalink / raw) To: Stefan Kangas; +Cc: 36452 > From: Stefan Kangas <stefan@marxist.se> > Date: Wed, 3 Jul 2019 08:33:22 +0200 > Cc: 36452@debbugs.gnu.org > > > One particular aspect of the bookmarks that I'd like to see tested is > > non-ASCII bookmark names and annotations. The code includes some > > slightly involved logic for determining how to encode the info on > > disk, so I wonder how well does that work in practice. > > OK. I added tests for that in the attached patch using strings > manually copied from HELLO. It works just fine to save and then load > bookmarks for the set of languages I chose to include characters from. > That is, we have the same before bookmark-save as we get after > bookmark-load. Thanks. Please add to that text something that doesn't yield valid UTF-8 byte sequence. For example, these two strings: (decode-coding-string "\xE3\x32\x9A\x36" 'chinese-gb18030) (char-to-string (decode-char 'eight-bit #x81)) (Please include these snippets directly, not their result as a literal string, to make sure the unusual characters survive intact.) ^ permalink raw reply [flat|nested] 8+ messages in thread
* bug#36452: [PATCH] Add tests for bookmark.el 2019-07-03 6:59 ` Eli Zaretskii @ 2019-07-03 11:01 ` Stefan Kangas 2019-07-03 11:24 ` Eli Zaretskii 0 siblings, 1 reply; 8+ messages in thread From: Stefan Kangas @ 2019-07-03 11:01 UTC (permalink / raw) To: Eli Zaretskii; +Cc: 36452 [-- Attachment #1: Type: text/plain, Size: 1566 bytes --] Eli Zaretskii <eliz@gnu.org> writes: > > > One particular aspect of the bookmarks that I'd like to see tested is > > > non-ASCII bookmark names and annotations. The code includes some > > > slightly involved logic for determining how to encode the info on > > > disk, so I wonder how well does that work in practice. > > > > OK. I added tests for that in the attached patch using strings > > manually copied from HELLO. It works just fine to save and then load > > bookmarks for the set of languages I chose to include characters from. > > That is, we have the same before bookmark-save as we get after > > bookmark-load. > > Thanks. Please add to that text something that doesn't yield valid > UTF-8 byte sequence. For example, these two strings: > > (decode-coding-string "\xE3\x32\x9A\x36" 'chinese-gb18030) > (char-to-string (decode-char 'eight-bit #x81)) > > (Please include these snippets directly, not their result as a literal > string, to make sure the unusual characters survive intact.) OK, added. The tests still pass (both interactively and in batch mode). The first one, when I evaluate it, makes Emacs segfault. Not sure why it works when I'm running the tests - I'd guess because Emacs is not trying to display it? Let me know if you want a backtrace for this crash and/or a separate bug report. In any case, I've attached an updated patch with the two above snippets included in the save/load test. Not sure if we should keep the first one around, but I added a warning not to evaluate it. What do you think? Thanks, Stefan Kangas [-- Attachment #2: 0001-Add-tests-for-bookmark.el-Bug-36452.patch --] [-- Type: application/octet-stream, Size: 20128 bytes --] From cd7e430f80e528f4ee6496c433ee48d29900fd08 Mon Sep 17 00:00:00 2001 From: Stefan Kangas <stefankangas@gmail.com> Date: Mon, 1 Jul 2019 01:21:47 +0200 Subject: [PATCH] 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. --- lisp/bookmark.el | 48 ++-- test/lisp/bookmark-resources/example.txt | 3 + test/lisp/bookmark-resources/test.bmk | 10 + test/lisp/bookmark-tests.el | 319 +++++++++++++++++++++++ 4 files changed, 348 insertions(+), 32 deletions(-) create mode 100644 test/lisp/bookmark-resources/example.txt create mode 100644 test/lisp/bookmark-resources/test.bmk create mode 100644 test/lisp/bookmark-tests.el diff --git a/lisp/bookmark.el b/lisp/bookmark.el index bbef0a927d..92d0da3594 100644 --- a/lisp/bookmark.el +++ b/lisp/bookmark.el @@ -4,7 +4,7 @@ ;; Author: Karl Fogel <kfogel@red-bean.com> ;; Created: July, 1993 -;; Keywords: bookmarks, placeholders, annotations +;; Keywords: convenience ;; This file is part of GNU Emacs. @@ -50,8 +50,7 @@ bookmark (defcustom bookmark-use-annotations nil "If non-nil, setting a bookmark queries for an annotation in a buffer." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-save-flag t @@ -71,14 +70,11 @@ bookmark-save-flag To specify the file in which to save them, modify the variable `bookmark-default-file'." - :type '(choice (const nil) integer (other t)) - :group 'bookmark) + :type '(choice (const nil) integer (other t))) (define-obsolete-variable-alias 'bookmark-old-default-file 'bookmark-default-file "27.1") - - (define-obsolete-variable-alias 'bookmark-file 'bookmark-default-file "27.1") (defcustom bookmark-default-file (locate-user-emacs-file "bookmarks" ".emacs.bmk") @@ -86,8 +82,7 @@ bookmark-default-file ;; The current default file is defined via the internal variable ;; `bookmark-bookmarks-timestamp'. This does not affect the value ;; of `bookmark-default-file'. - :type 'file - :group 'bookmark) + :type 'file) (defcustom bookmark-watch-bookmark-file t "If non-nil watch the default bookmark file. @@ -108,35 +103,30 @@ bookmark-version-control :type '(choice (const :tag "If existing" nil) (const :tag "Never" never) (const :tag "Use value of option `version-control'" nospecial) - (other :tag "Always" t)) - :group 'bookmark) + (other :tag "Always" t))) (defcustom bookmark-completion-ignore-case t "Non-nil means bookmark functions ignore case in completion." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-sort-flag t "Non-nil means that bookmarks will be displayed sorted by bookmark name. Otherwise they will be displayed in LIFO order (that is, most recently set ones come first, oldest ones come last)." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-automatically-show-annotations t "Non-nil means show annotations when jumping to a bookmark." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defcustom bookmark-bmenu-use-header-line t "Non-nil means to use an immovable header line. This is as opposed to inline text at the top of the buffer." :version "24.4" - :type 'boolean - :group 'bookmark) + :type 'boolean) (defconst bookmark-bmenu-inline-header-height 2 "Number of lines used for the *Bookmark List* header. @@ -150,36 +140,30 @@ bookmark-bmenu-marks-width (defcustom bookmark-bmenu-file-column 30 "Column at which to display filenames in a buffer listing bookmarks. You can toggle whether files are shown with \\<bookmark-bmenu-mode-map>\\[bookmark-bmenu-toggle-filenames]." - :type 'integer - :group 'bookmark) + :type 'integer) (defcustom bookmark-bmenu-toggle-filenames t "Non-nil means show filenames when listing bookmarks. A non-nil value may result in truncated bookmark names." - :type 'boolean - :group 'bookmark) + :type 'boolean) (defface bookmark-menu-bookmark '((t (:weight bold))) - "Face used to highlight bookmark names in bookmark menu buffers." - :group 'bookmark) + "Face used to highlight bookmark names in bookmark menu buffers.") (defcustom bookmark-menu-length 70 "Maximum length of a bookmark name displayed on a popup menu." - :type 'integer - :group 'bookmark) + :type 'integer) ;; FIXME: Is it really worth a customization option? (defcustom bookmark-search-delay 0.2 "Time before `bookmark-bmenu-search' updates the display." - :group 'bookmark :type 'number) (defface bookmark-menu-heading '((t (:inherit font-lock-type-face))) "Face used to highlight the heading in bookmark menu buffers." - :group 'bookmark :version "22.1") @@ -279,8 +263,6 @@ bookmark-bookmarks-timestamp (defvar bookmark-file-coding-system nil "The coding-system of the last loaded or saved bookmark file.") -;; more stuff added by db. - (defvar bookmark-current-bookmark nil "Name of bookmark most recently used in the current file. It is buffer local, used to make moving a bookmark forward @@ -937,6 +919,8 @@ bookmark-edit-annotation-mode-map (defun bookmark-insert-annotation (bookmark-name-or-record) "Insert annotation for BOOKMARK-NAME-OR-RECORD at point." + (when (not (bookmark-get-bookmark bookmark-name-or-record t)) + (error "Invalid bookmark: %s" bookmark-name-or-record)) (insert (funcall bookmark-edit-annotation-text-func bookmark-name-or-record)) (let ((annotation (bookmark-get-annotation bookmark-name-or-record))) (if (and annotation (not (string-equal annotation ""))) @@ -1449,7 +1433,7 @@ bookmark-write-file ;; Apparently `pp' has a poor algorithmic complexity, so this ;; scales a lot better. bug#4485. (dolist (i bookmark-alist) (pp i (current-buffer))) - (insert ")") + (insert ")\n") ;; Make sure the specified encoding can safely encode the ;; bookmarks. If it cannot, suggest utf-8-emacs as default. (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 0000000000..b66fc07826 --- /dev/null +++ b/test/lisp/bookmark-resources/example.txt @@ -0,0 +1,3 @@ +This text file is used by bookmark-tests.el + +;; 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 0000000000..c238fe1c8d --- /dev/null +++ b/test/lisp/bookmark-resources/test.bmk @@ -0,0 +1,10 @@ +;;;; Emacs Bookmark Format Version 1 ;;;; -*- coding: utf-8-emacs -*- +;;; This format is meant to be slightly human-readable; +;;; nevertheless, you probably don't want to edit it. +;;; -*- End Of Bookmark File Format Version Stamp -*- +(("name" + (filename . "/some/file") + (front-context-string . "abc") + (rear-context-string . "def") + (position . 3)) +) diff --git a/test/lisp/bookmark-tests.el b/test/lisp/bookmark-tests.el new file mode 100644 index 0000000000..6fa0b1e98d --- /dev/null +++ b/test/lisp/bookmark-tests.el @@ -0,0 +1,319 @@ +;;; bookmark-tests.el --- Tests for bookmark.el -*- lexical-binding: t -*- + +;; Copyright (C) 2019 Free Software Foundation, Inc. + +;; Author: Stefan Kangas <stefankangas@gmail.com> + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;;; Code: + +(require 'ert) +(require 'bookmark) + +(defvar bookmark-tests-data-dir + (file-truename + (expand-file-name "bookmark-resources/" + (file-name-directory (or load-file-name + buffer-file-name)))) + "Base directory of bookmark-tests.el data files.") + +(defvar bookmark-tests-bookmark-file + (expand-file-name "test.bmk" bookmark-tests-data-dir) + "Bookmark file used for testing.") + +(defvar bookmark-tests-example-file + ;; We use abbreviate-file-name here to match the behaviour of + ;; `bookmark-buffer-file-name'. + (abbreviate-file-name (expand-file-name "example.txt" bookmark-tests-data-dir)) + "Example file used for testing.") + +;; The values below should match `bookmark-tests-bookmark-file'. We cache +;; these values to speed up tests by avoiding unnecessary I/O. This +;; makes tests run 5-10 times faster on my system. +(eval-and-compile ; needed by `with-bookmark-test' macro + (defvar bookmark-tests-bookmark '("name" + (filename . "/some/file") + (front-context-string . "abc") + (rear-context-string . "def") + (position . 3)) + "Cached value used in bookmark-tests.el.")) + +(defvar bookmark-tests-cache-timestamp + (cons bookmark-tests-bookmark-file + (nth 5 (file-attributes + bookmark-tests-bookmark-file))) + "Cached value used in bookmark-tests.el.") + +(defmacro with-bookmark-test (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Ensure a clean environment for testing, and do not change user +data when running tests interactively." + `(with-temp-buffer + (let ((bookmark-alist (quote (,(copy-sequence bookmark-tests-bookmark)))) + (bookmark-default-file bookmark-tests-bookmark-file) + (bookmark-bookmarks-timestamp bookmark-tests-cache-timestamp) + bookmark-save-flag) + ,@body))) + +(defmacro with-bookmark-test-file (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Same as `with-bookmark-test' but also opens the resource file +example.txt in a buffer, which can be accessed by callers through +the lexically-bound variable `buffer'." + `(let ((buffer (find-file-noselect bookmark-tests-example-file))) + (unwind-protect + (with-bookmark-test + ,@body) + (kill-buffer buffer)))) + +(ert-deftest bookmark-tests-all-names () + (with-bookmark-test + (should (equal (bookmark-all-names) '("name"))))) + +(ert-deftest bookmark-tests-get-bookmark () + (with-bookmark-test + (should (equal (bookmark-get-bookmark "name") bookmark-tests-bookmark)))) + +(ert-deftest bookmark-tests-get-bookmark-record () + (with-bookmark-test + (should (equal (bookmark-get-bookmark-record "name") (cdr bookmark-tests-bookmark))))) + +(ert-deftest bookmark-tests-record-getters-and-setters-new () + (with-temp-buffer + (let* ((buffer-file-name "test") + (bmk (bookmark-make-record))) + (bookmark-set-name bmk "foobar") + (should (equal (bookmark-name-from-full-record bmk) "foobar")) + (bookmark-set-filename bmk "file/name") + (should (equal (bookmark-get-filename bmk) "file/name")) + (bookmark-set-position bmk 123) + (should (equal (bookmark-get-position bmk) 123)) + (bookmark-set-front-context-string bmk "front") + (should (equal (bookmark-get-front-context-string bmk) "front")) + (bookmark-set-rear-context-string bmk "rear") + (should (equal (bookmark-get-rear-context-string bmk) "rear")) + (bookmark-prop-set bmk 'filename "prop") + (should (equal (bookmark-prop-get bmk 'filename) "prop"))))) + +(ert-deftest bookmark-tests-maybe-historicize-string () + (let ((bookmark-history)) + (bookmark-maybe-historicize-string "foo") + (should (equal (car bookmark-history) "foo")))) + +(ert-deftest bookmark-tests-make-record () + (with-bookmark-test-file + (let* ((record `("example.txt" (filename . ,bookmark-tests-example-file) + (front-context-string . "is text file is ") + (rear-context-string) + (position . 3) + (defaults "example.txt")))) + (with-current-buffer buffer + (goto-char 3) + (should (equal (bookmark-make-record) record)) + ;; calling twice gives same record + (should (equal (bookmark-make-record) record)))))) + +(ert-deftest bookmark-tests-make-record-function () + (with-bookmark-test + (let ((buffer-file-name "test")) + ;; Named bookmark + (let ((bookmark-make-record-function (lambda () '("<name>")))) + (should (equal (bookmark-make-record) + '("<name>")))) + ;; SECOND format + (let ((bookmark-make-record-function (lambda () '(((position . 2)))))) + (should (equal (bookmark-make-record) + '("test" ((position . 2) (defaults "test")))))) + ;; CURRENT format + (let ((bookmark-make-record-function (lambda () '((position . 2))))) + (should (equal (bookmark-make-record) + '("test" (position . 2) (defaults "test")))))))) + +(ert-deftest bookmark-tests-set () + (with-bookmark-test-file + (let ((bmk1 `("foo" (filename . ,bookmark-tests-example-file) + (front-context-string . "This text file i") + (rear-context-string) + (position . 1))) + (bmk2 `("foo" (filename . ,bookmark-tests-example-file) + (front-context-string) + (rear-context-string . ".txt ends here.\n") + (position . 72))) + bookmark-alist) + (with-current-buffer buffer + ;; 1. bookmark-set + ;; Set first bookmark + (goto-char (point-min)) + (bookmark-set "foo") + (should (equal bookmark-alist (list bmk1))) + ;; Replace that bookmark + (goto-char (point-max)) + (bookmark-set "foo") + (should (equal bookmark-alist (list bmk2))) + ;; Push another bookmark with the same name + (goto-char (point-min)) + (bookmark-set "foo" t) ; NO-OVERWRITE is t + (should (equal bookmark-alist (list bmk1 bmk2))) + + ;; 2. bookmark-set-no-overwrite + ;; Don't overwrite + (should-error (bookmark-set-no-overwrite "foo")) + ;; Set new bookmark + (setq bookmark-alist nil) + (bookmark-set-no-overwrite "foo") + (should (equal bookmark-alist (list bmk1))) + ;; Push another bookmark with the same name + (goto-char (point-max)) + (bookmark-set-no-overwrite "foo" t) ; PUSH-BOOKMARK is t + (should (equal bookmark-alist (list bmk2 bmk1))) + + ;; 3. bookmark-set-internal + (should-error (bookmark-set-internal "foo" "bar" t)))))) + +(ert-deftest bookmark-tests-kill-line () + (with-temp-buffer + (insert "foobar\n") + (goto-char (point-min)) + (bookmark-kill-line) + (should (equal (buffer-string) "\n"))) + (with-temp-buffer + (insert "foobar\n") + (goto-char (point-min)) + (bookmark-kill-line t) ; including newline + (should (equal (buffer-string) "")))) + +(ert-deftest bookmark-tests-default-annotation-text () + (should (stringp (bookmark-default-annotation-text "foo"))) + (should (string-match "foo" (bookmark-default-annotation-text "foo")))) + +(ert-deftest bookmark-tests-insert-annotation () + (with-bookmark-test + (should-error (bookmark-insert-annotation "a missing bookmark")) + (bookmark-insert-annotation "name") + (should (equal (buffer-string) (bookmark-default-annotation-text "name")))) + (with-bookmark-test + (bookmark-set-annotation "name" "some stuff") + (bookmark-insert-annotation "name") + (should (string-match "some stuff" (buffer-string))))) + +(ert-deftest bookmark-tests-edit-annotation () + (with-bookmark-test + (bookmark-edit-annotation "name") + (insert "new text") + (bookmark-send-edited-annotation) + (should (equal (bookmark-get-annotation "name") "new text")))) + +(ert-deftest bookmark-tests-jump () + (with-bookmark-test-file + (with-current-buffer buffer + (bookmark-set "test")) + (bookmark-jump "test") + (should (equal (current-buffer) buffer)) + (should-error (bookmark-jump nil)))) + +(ert-deftest bookmark-tests-insert-location () + (with-bookmark-test + (bookmark-insert-location "name") + (should (equal (buffer-string) "/some/file")))) + +(ert-deftest bookmark-tests-location () + (with-bookmark-test + (should (equal (bookmark-location "name") "/some/file")))) + +(ert-deftest bookmark-tests-rename () + (with-bookmark-test + (bookmark-rename "name" "newname") + (should (equal (bookmark-all-names) '("newname"))))) + +(ert-deftest bookmark-tests-insert () + (with-bookmark-test-file + (with-current-buffer buffer + (bookmark-set "test")) + (bookmark-insert "test") + (should (string-match "^This text file is used by bookmark-tests.el" + (buffer-string))))) + +(ert-deftest bookmark-tests-delete () + (with-bookmark-test + (bookmark-delete "name") + (should (equal bookmark-alist nil)))) + +(defmacro with-bookmark-test-save-load (&rest body) + "Create environment for testing bookmark.el and evaluate BODY. +Same as `with-bookmark-test' but also sets a temporary +`bookmark-default-file', evaluates BODY, and then runs the test +that saves and then loads the bookmark file." + `(with-bookmark-test + (let ((file (make-temp-file "bookmark-tests-"))) + (unwind-protect + (let ((bookmark-default-file file) + (old-alist bookmark-alist)) + ,@body + (bookmark-save nil file t) + (setq bookmark-alist nil) + (bookmark-load file nil t) + (should (equal bookmark-alist old-alist))) + (delete-file file))))) + +(defvar bookmark-tests-non-ascii-data + (concat "Здра́вствуйте!" "中文,普通话,汉语" "åäöøñ" + "こんにちは" "你好" "Dobrý deň" + "∀ p ∈ world • hello p □" + ;; These do not yield valid UTF-8 byte sequences. + ;; WARNING: If you try to evaluate the first one of these, + ;; there is a risk that Emacs will crash. Buyer beware. + (decode-coding-string "\xE3\x32\x9A\x36" 'chinese-gb18030) + (char-to-string (decode-char 'eight-bit #x81)))) + +(ert-deftest bookmark-tests-save () + (with-bookmark-test-save-load + ;; Just run the default test according to the macro. + t)) + +(ert-deftest bookmark-tests-save/non-ascii-bookmark-name () + (with-bookmark-test-save-load + (bookmark-set-name "name" bookmark-tests-non-ascii-data))) + +(ert-deftest bookmark-tests-save/non-ascii-annotation () + (with-bookmark-test-save-load + (bookmark-set-annotation "name" bookmark-tests-non-ascii-data))) + +(ert-deftest bookmark-tests-maybe-rename () + (let ((bookmark '("foo"))) + (bookmark-maybe-rename bookmark '("foo")) + (should (equal bookmark '("foo<2>"))))) + +(ert-deftest bookmark-tests-load () + (with-bookmark-test + (should-error (bookmark-load "no/such/file")) + (let* ((bookmark-alist '())) + (bookmark-load bookmark-tests-bookmark-file nil t) + (should (equal bookmark-alist (list bookmark-tests-bookmark))) + (bookmark-load bookmark-tests-bookmark-file t t) ; OVERWRITE is t + (should (equal bookmark-alist (list bookmark-tests-bookmark))) + ;; Append + (bookmark-load bookmark-tests-bookmark-file nil t) ; OVERWRITE is nil + (should (equal bookmark-alist + (list bookmark-tests-bookmark + (cons "name<2>" (cdr bookmark-tests-bookmark)))))))) + +;; TODO: Add tests for bookmark-bmenu. + +(provide 'bookmark-tests) +;;; bookmark-tests.el ends here -- 2.21.0 ^ permalink raw reply related [flat|nested] 8+ messages in thread
* bug#36452: [PATCH] Add tests for bookmark.el 2019-07-03 11:01 ` Stefan Kangas @ 2019-07-03 11:24 ` Eli Zaretskii 2019-07-03 11:42 ` Stefan Kangas 0 siblings, 1 reply; 8+ messages in thread From: Eli Zaretskii @ 2019-07-03 11:24 UTC (permalink / raw) To: Stefan Kangas; +Cc: 36452 > From: Stefan Kangas <stefan@marxist.se> > Date: Wed, 3 Jul 2019 13:01:13 +0200 > Cc: 36452@debbugs.gnu.org > > > Thanks. Please add to that text something that doesn't yield valid > > UTF-8 byte sequence. For example, these two strings: > > > > (decode-coding-string "\xE3\x32\x9A\x36" 'chinese-gb18030) > > (char-to-string (decode-char 'eight-bit #x81)) > > > > (Please include these snippets directly, not their result as a literal > > string, to make sure the unusual characters survive intact.) > > OK, added. The tests still pass (both interactively and in batch mode). Great, thanks. > The first one, when I evaluate it, makes Emacs segfault. Not sure why > it works when I'm running the tests - I'd guess because Emacs is not > trying to display it? Let me know if you want a backtrace for this > crash and/or a separate bug report. A separate bug report, please. It didn't crash for me, FWIW. If the crash is in the display code, then yes, it's because evaluating the expression will display the resulting string. > In any case, I've attached an updated patch with the two above > snippets included in the save/load test. Not sure if we should keep > the first one around, but I added a warning not to evaluate it. > > What do you think? I think we should keep both, but we should also investigate the crash. Do the tests in this patch depend in some way on the other patches you submitted for bookmark.el? Because if not, I'd like to push this right away, as tests cannot possibly break anything (with the other patches, I'd like to wait for comments for a few more days). Thanks. ^ permalink raw reply [flat|nested] 8+ messages in thread
* bug#36452: [PATCH] Add tests for bookmark.el 2019-07-03 11:24 ` Eli Zaretskii @ 2019-07-03 11:42 ` Stefan Kangas 2019-07-03 11:53 ` Eli Zaretskii 0 siblings, 1 reply; 8+ messages in thread From: Stefan Kangas @ 2019-07-03 11:42 UTC (permalink / raw) To: Eli Zaretskii; +Cc: 36452 Eli Zaretskii <eliz@gnu.org> writes: > > The first one, when I evaluate it, makes Emacs segfault. Not sure why > > it works when I'm running the tests - I'd guess because Emacs is not > > trying to display it? Let me know if you want a backtrace for this > > crash and/or a separate bug report. > > A separate bug report, please. It didn't crash for me, FWIW. If the > crash is in the display code, then yes, it's because evaluating the > expression will display the resulting string. OK, I'll submit a separate bug report. > I think we should keep both, but we should also investigate the crash. Sounds good. > Do the tests in this patch depend in some way on the other patches you > submitted for bookmark.el? Because if not, I'd like to push this > right away, as tests cannot possibly break anything (with the other > patches, I'd like to wait for comments for a few more days). This patch is fully independent from the others. It would be great if it could be installed right away. Thanks, Stefan Kangas ^ permalink raw reply [flat|nested] 8+ messages in thread
* bug#36452: [PATCH] Add tests for bookmark.el 2019-07-03 11:42 ` Stefan Kangas @ 2019-07-03 11:53 ` Eli Zaretskii 0 siblings, 0 replies; 8+ messages in thread From: Eli Zaretskii @ 2019-07-03 11:53 UTC (permalink / raw) To: Stefan Kangas; +Cc: 36452-done > From: Stefan Kangas <stefan@marxist.se> > Date: Wed, 3 Jul 2019 13:42:24 +0200 > Cc: 36452@debbugs.gnu.org > > > Do the tests in this patch depend in some way on the other patches you > > submitted for bookmark.el? Because if not, I'd like to push this > > right away, as tests cannot possibly break anything (with the other > > patches, I'd like to wait for comments for a few more days). > > This patch is fully independent from the others. It would be great if > it could be installed right away. Thanks, pushed. ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2019-07-03 11:53 UTC | newest] Thread overview: 8+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2019-06-30 23:32 bug#36452: [PATCH] Add tests for bookmark.el Stefan Kangas 2019-07-02 16:41 ` Eli Zaretskii 2019-07-03 6:33 ` Stefan Kangas 2019-07-03 6:59 ` Eli Zaretskii 2019-07-03 11:01 ` Stefan Kangas 2019-07-03 11:24 ` Eli Zaretskii 2019-07-03 11:42 ` Stefan Kangas 2019-07-03 11:53 ` Eli Zaretskii
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.