emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] fix-21072-patch 30d7135 1/2: Add elisp-tests-with-temp-buf


From: Marcin Borkowski
Subject: [Emacs-diffs] fix-21072-patch 30d7135 1/2: Add elisp-tests-with-temp-buffer, a new testing macro
Date: Wed, 29 Mar 2017 02:29:56 -0400 (EDT)

branch: fix-21072-patch
commit 30d7135b785d72b23100cb2721af2bf276b78976
Author: Marcin Borkowski <address@hidden>
Commit: Marcin Borkowski <address@hidden>

    Add elisp-tests-with-temp-buffer, a new testing macro
    
    * test/lisp/progmodes/elisp-mode-tests.el
    (elisp-test-point-marker-regex) New variable.
    (elisp-tests-with-temp-buffer): New macro to help test functions
    moving the point and/or mark.
---
 etc/NEWS                                |  5 +++++
 test/lisp/progmodes/elisp-mode-tests.el | 39 +++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index cd98f53..3a8fee1 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1056,6 +1056,11 @@ its window gets deleted by 'delete-other-windows'.
 windows.
 
 
+** New macro 'elisp-tests-with-temp-buffer'
+which helps writing tests for functions that should change buffers in
+specific ways or manipulate point or mark positions.
+
+---
 * Changes in Emacs 26.1 on Non-Free Operating Systems
 
 ** Intercepting hotkeys on Windows 7 and later now works better.
diff --git a/test/lisp/progmodes/elisp-mode-tests.el 
b/test/lisp/progmodes/elisp-mode-tests.el
index 93c428b..a00f6b1 100644
--- a/test/lisp/progmodes/elisp-mode-tests.el
+++ b/test/lisp/progmodes/elisp-mode-tests.el
@@ -2,6 +2,7 @@
 
 ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
 
+;; Author: Marcin Borkowski <address@hidden>
 ;; Author: Dmitry Gutov <address@hidden>
 ;; Author: Stephen Leake <address@hidden>
 
@@ -672,5 +673,43 @@ to (xref-elisp-test-descr-to-target xref)."
     (insert "?\\N{HEAVY CHECK MARK}")
     (should (equal (elisp--preceding-sexp) ?\N{HEAVY CHECK MARK}))))
 
+;;; Helpers
+
+(defvar elisp-test-point-marker-regex "=!\\([a-zA-Z0-9-]+\\)="
+  "A regexp matching placeholders for point position for
+`elisp-tests-with-temp-buffer'.")
+
+;; Copied and heavily modified from `python-tests-with-temp-buffer'
+(defmacro elisp-tests-with-temp-buffer (contents &rest body)
+  "Create an `emacs-lisp-mode' enabled temp buffer with CONTENTS.
+BODY is the code to be executed within the temp buffer.  Point is
+always located at the beginning of buffer.  Special markers of
+the form =!NAME= in CONTENTS are removed, and a for each one
+a variable called NAME is bound to the position of such
+a marker."
+  (declare (indent 1) (debug t))
+  `(with-temp-buffer
+     (emacs-lisp-mode)
+     (insert ,contents)
+     (goto-char (point-min))
+     (while (re-search-forward elisp-test-point-marker-regex nil t)
+       (delete-region (match-beginning 0)
+                     (match-end 0)))
+     (goto-char (point-min))
+     ,(let (marker-list)
+       (with-temp-buffer
+         (insert (cond ((symbolp contents)
+                         (symbol-value contents))
+                        (t contents)))
+         (goto-char (point-min))
+         (while (re-search-forward elisp-test-point-marker-regex nil t)
+           (push (list (intern (match-string-no-properties 1))
+                       (match-beginning 0))
+                 marker-list)
+           (delete-region (match-beginning 0)
+                          (match-end 0))))
+       `(let ,marker-list
+          ,@body))))
+
 (provide 'elisp-mode-tests)
 ;;; elisp-mode-tests.el ends here



reply via email to

[Prev in Thread] Current Thread [Next in Thread]