From cdbbc2081ed2da3a641926e76341ed413fb5b9f9 Mon Sep 17 00:00:00 2001 From: Stefan Kangas Date: Sat, 8 Aug 2020 01:20:01 +0200 Subject: [PATCH] Use lexical-binding in saveplace.el and add tests * lisp/saveplace.el: Use lexical-binding. (save-place-to-alist): Doc fix. * test/lisp/saveplace-tests.el: * test/lisp/saveplace-resources/saveplace: New files. --- lisp/saveplace.el | 12 +-- test/lisp/saveplace-resources/saveplace | 4 + test/lisp/saveplace-tests.el | 103 ++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 6 deletions(-) create mode 100644 test/lisp/saveplace-resources/saveplace create mode 100644 test/lisp/saveplace-tests.el diff --git a/lisp/saveplace.el b/lisp/saveplace.el index 46738ab03dc..d420bfb4e9f 100644 --- a/lisp/saveplace.el +++ b/lisp/saveplace.el @@ -1,4 +1,4 @@ -;;; saveplace.el --- automatically save place in files +;;; saveplace.el --- automatically save place in files -*- lexical-binding:t -*- ;; Copyright (C) 1993-1994, 2001-2020 Free Software Foundation, Inc. @@ -42,7 +42,6 @@ "Automatically save place in files." :group 'data) - (defvar save-place-alist nil "Alist of saved places to go back to when revisiting files. Each element looks like (FILENAME . POSITION); @@ -175,10 +174,11 @@ file: (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep)) (defun save-place-to-alist () - ;; put filename and point in a cons box and then cons that onto the - ;; front of the save-place-alist, if save-place-mode is non-nil. - ;; Otherwise, just delete that file from the alist. - ;; first check to make sure alist has been loaded in from the master + "Add current buffer filename and position to `save-place-alist'. +Put filename and point in a cons box and then cons that onto the +front of the `save-place-alist', if `save-place-mode' is non-nil. +Otherwise, just delete that file from the alist." + ;; First check to make sure alist has been loaded in from the master ;; file. If not, do so, then feel free to modify the alist. It ;; will be saved again when Emacs is killed. (or save-place-loaded (load-save-place-alist-from-file)) diff --git a/test/lisp/saveplace-resources/saveplace b/test/lisp/saveplace-resources/saveplace new file mode 100644 index 00000000000..3f3f6d501d6 --- /dev/null +++ b/test/lisp/saveplace-resources/saveplace @@ -0,0 +1,4 @@ +;;; -*- coding: utf-8 -*- +(("/home/skangas/.emacs.d/cache/recentf" . 1306) + ("/home/skangas/wip/emacs/" + (dired-filename . "/home/skangas/wip/emacs/COPYING"))) diff --git a/test/lisp/saveplace-tests.el b/test/lisp/saveplace-tests.el new file mode 100644 index 00000000000..ae7749fe930 --- /dev/null +++ b/test/lisp/saveplace-tests.el @@ -0,0 +1,103 @@ +;;; saveplace-tests.el --- Tests for saveplace.el -*- lexical-binding:t -*- + +;; Copyright (C) 2019-2020 Free Software Foundation, Inc. + +;; Author: Stefan Kangas + +;; 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 . + +;;; Commentary: + +(require 'ert) +(require 'saveplace) + +(defvar saveplace-tests-dir + (file-truename + (expand-file-name "saveplace-resources" + (file-name-directory (or load-file-name + buffer-file-name))))) + +(ert-deftest saveplace-test-save-place-to-alist/dir () + (save-place-mode) + (let* ((save-place-alist nil) + (save-place-loaded t) + (loc saveplace-tests-dir)) + (save-window-excursion + (dired loc) + (save-place-to-alist) + (should (equal save-place-alist + `((,(concat loc "/") + (dired-filename . ,(concat loc "/saveplace"))))))))) + +(ert-deftest saveplace-test-save-place-to-alist/file () + (save-place-mode) + (let* ((tmpfile (make-temp-file "emacs-test-saveplace-")) + (save-place-alist nil) + (save-place-loaded t) + (loc tmpfile) + (pos 4)) + (unwind-protect + (save-window-excursion + (find-file loc) + (insert "abc") ; must insert something + (save-place-to-alist) + (should (equal save-place-alist (list (cons tmpfile pos))))) + (delete-file tmpfile)))) + +(ert-deftest saveplace-test-forget-unreadable-files () + (save-place-mode) + (let* ((save-place-loaded t) + (tmpfile (make-temp-file "emacs-test-saveplace-")) + (alist-orig (list (cons "/this/file/does/not/exist" 10) + (cons tmpfile 1917))) + (save-place-alist alist-orig)) + (unwind-protect + (progn + (save-place-forget-unreadable-files) + (should (equal save-place-alist (cdr alist-orig)))) + (delete-file tmpfile)))) + +(ert-deftest saveplace-test-place-alist-to-file () + (save-place-mode) + (let* ((tmpfile (make-temp-file "emacs-test-saveplace-")) + (tmpfile2 (make-temp-file "emacs-test-saveplace-")) + (save-place-file tmpfile) + (save-place-alist (list (cons tmpfile2 99)))) + (unwind-protect + (progn (save-place-alist-to-file) + (setq save-place-alist nil) + (save-window-excursion + (find-file save-place-file) + (unwind-protect + (should (string-match tmpfile2 (buffer-string))) + (kill-buffer)))) + (delete-file tmpfile) + (delete-file tmpfile2)))) + +(ert-deftest saveplace-test-load-alist-from-file () + (save-place-mode) + (let ((save-place-loaded nil) + (save-place-file + (expand-file-name "saveplace" saveplace-tests-dir)) + (save-place-alist nil)) + (load-save-place-alist-from-file) + (should (equal save-place-alist + '(("/home/skangas/.emacs.d/cache/recentf" . 1306) + ("/home/skangas/wip/emacs/" + (dired-filename . "/home/skangas/wip/emacs/COPYING"))))))) + +(provide 'saveplace-tests) +;;; saveplace-tests.el ends here -- 2.39.2