From da4535cd825b5bc854f754fa6762a3e3ded5c850 Mon Sep 17 00:00:00 2001 From: Tobias Rittweiler Date: Tue, 8 Dec 2020 09:59:15 +0100 Subject: [PATCH 1/2] Minor refactoring of xref-tests * test/lisp/progmodes/xref-tests.el (xref-tests-data-dir): Renamed to 'xref-tests--data-dir' (xref-tests--matches-in-data-dir, xref-tests--locations-in-data-dir): New functions. Factored out from test cases and updated usage sites to use them. --- test/lisp/progmodes/xref-tests.el | 38 ++++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/test/lisp/progmodes/xref-tests.el b/test/lisp/progmodes/xref-tests.el index 038f9d0e30..e52cc20053 100644 --- a/test/lisp/progmodes/xref-tests.el +++ b/test/lisp/progmodes/xref-tests.el @@ -27,27 +27,34 @@ (require 'xref) (require 'cl-lib) -(defvar xref-tests-data-dir +(defvar xref-tests--data-dir (expand-file-name "xref-resources/" (file-name-directory (or load-file-name buffer-file-name)))) +(defun xref-tests--matches-in-data-dir (regexp &optional files) + (xref-matches-in-directory regexp (or files "*") xref-tests--data-dir nil)) + +(defun xref-tests--locations-in-data-dir (regexp &optional files) + (let ((matches (xref-tests--matches-in-data-dir regexp files))) + ;; Sort in order to guarantee an order independent from the + ;; filesystem traversal. + (cl-sort (mapcar #'xref-item-location matches) + #'string< + :key #'xref-location-group))) + (ert-deftest xref-matches-in-directory-finds-none-for-some-regexp () - (should (null (xref-matches-in-directory "zzz" "*" xref-tests-data-dir nil)))) + (should (null (xref-tests--matches-in-data-dir "zzz")))) (ert-deftest xref-matches-in-directory-finds-some-for-bar () - (let* ((matches (xref-matches-in-directory "bar" "*" xref-tests-data-dir nil)) - (locs (cl-sort (mapcar #'xref-item-location matches) - #'string< - :key #'xref-location-group))) - (should (= 2 (length matches))) + (let ((locs (xref-tests--locations-in-data-dir "bar"))) + (should (= 2 (length locs))) (should (string-match-p "file1\\.txt\\'" (xref-location-group (nth 0 locs)))) (should (string-match-p "file2\\.txt\\'" (xref-location-group (nth 1 locs)))))) (ert-deftest xref-matches-in-directory-finds-two-matches-on-the-same-line () - (let* ((matches (xref-matches-in-directory "foo" "*" xref-tests-data-dir nil)) - (locs (mapcar #'xref-item-location matches))) - (should (= 2 (length matches))) + (let ((locs (xref-tests--locations-in-data-dir "foo"))) + (should (= 2 (length locs))) (should (string-match-p "file1\\.txt\\'" (xref-location-group (nth 0 locs)))) (should (string-match-p "file1\\.txt\\'" (xref-location-group (nth 1 locs)))) (should (equal 1 (xref-location-line (nth 0 locs)))) @@ -56,15 +63,14 @@ xref-matches-in-directory-finds-two-matches-on-the-same-line (should (equal 4 (xref-file-location-column (nth 1 locs)))))) (ert-deftest xref-matches-in-directory-finds-an-empty-line-regexp-match () - (let* ((matches (xref-matches-in-directory "^$" "*" xref-tests-data-dir nil)) - (locs (mapcar #'xref-item-location matches))) - (should (= 1 (length matches))) + (let ((locs (xref-tests--locations-in-data-dir "^$"))) + (should (= 1 (length locs))) (should (string-match-p "file2\\.txt\\'" (xref-location-group (nth 0 locs)))) (should (equal 1 (xref-location-line (nth 0 locs)))) (should (equal 0 (xref-file-location-column (nth 0 locs)))))) (ert-deftest xref--buf-pairs-iterator-groups-markers-by-buffers-1 () - (let* ((xrefs (xref-matches-in-directory "foo" "*" xref-tests-data-dir nil)) + (let* ((xrefs (xref-tests--matches-in-data-dir "foo")) (iter (xref--buf-pairs-iterator xrefs)) (cons (funcall iter :next))) (should (null (funcall iter :next))) @@ -72,7 +78,7 @@ xref--buf-pairs-iterator-groups-markers-by-buffers-1 (should (= 2 (length (cdr cons)))))) (ert-deftest xref--buf-pairs-iterator-groups-markers-by-buffers-2 () - (let* ((xrefs (xref-matches-in-directory "bar" "*" xref-tests-data-dir nil)) + (let* ((xrefs (xref-tests--matches-in-data-dir "bar")) (iter (xref--buf-pairs-iterator xrefs)) (cons1 (funcall iter :next)) (cons2 (funcall iter :next))) @@ -82,7 +88,7 @@ xref--buf-pairs-iterator-groups-markers-by-buffers-2 (should (= 1 (length (cdr cons2)))))) (ert-deftest xref--buf-pairs-iterator-cleans-up-markers () - (let* ((xrefs (xref-matches-in-directory "bar" "*" xref-tests-data-dir nil)) + (let* ((xrefs (xref-tests--matches-in-data-dir "bar")) (iter (xref--buf-pairs-iterator xrefs)) (cons1 (funcall iter :next)) (cons2 (funcall iter :next))) -- 2.25.1