From f559b374a30f3615261f7a902fc3428cac6289f4 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Wed, 4 May 2016 01:38:02 +0300 Subject: Add tests for xref-collect-matches * test/automated/xref-tests.el: New file. Add tests for xref-collect-matches. --- test/automated/data/xref/file1.txt | 2 ++ test/automated/data/xref/file2.txt | 2 ++ test/automated/xref-tests.el | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 test/automated/data/xref/file1.txt create mode 100644 test/automated/data/xref/file2.txt create mode 100644 test/automated/xref-tests.el (limited to 'test') diff --git a/test/automated/data/xref/file1.txt b/test/automated/data/xref/file1.txt new file mode 100644 index 00000000000..5d7cc544443 --- /dev/null +++ b/test/automated/data/xref/file1.txt @@ -0,0 +1,2 @@ +foo foo +bar diff --git a/test/automated/data/xref/file2.txt b/test/automated/data/xref/file2.txt new file mode 100644 index 00000000000..9f075f26004 --- /dev/null +++ b/test/automated/data/xref/file2.txt @@ -0,0 +1,2 @@ + +bar diff --git a/test/automated/xref-tests.el b/test/automated/xref-tests.el new file mode 100644 index 00000000000..1b500c3c418 --- /dev/null +++ b/test/automated/xref-tests.el @@ -0,0 +1,37 @@ +(require 'xref) +(require 'cl-lib) + +(defvar xref-tests-data-dir + (expand-file-name "data/xref/" + (file-name-directory (or load-file-name (buffer-file-name))))) + +(ert-deftest xref-collect-matches-finds-none-for-some-regexp () + (should (null (xref-collect-matches "zzz" "*" xref-tests-data-dir nil)))) + +(ert-deftest xref-collect-matches-finds-some-for-bar () + (let* ((matches (xref-collect-matches "bar" "*" xref-tests-data-dir nil)) + (locs (cl-sort (mapcar #'xref-item-location matches) + #'string< + :key #'xref-location-group))) + (should (= 2 (length matches))) + (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-collect-matches-finds-two-matches-on-the-same-line () + (let* ((matches (xref-collect-matches "foo" "*" xref-tests-data-dir nil)) + (locs (mapcar #'xref-item-location matches))) + (should (= 2 (length matches))) + (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)))) + (should (equal 1 (xref-location-line (nth 1 locs)))) + (should (equal 0 (xref-file-location-column (nth 0 locs)))) + (should (equal 4 (xref-file-location-column (nth 1 locs)))))) + +;; (ert-deftest xref-collect-matches-finds-an-empty-line-regexp-match () +;; (let* ((matches (xref-collect-matches "^$" "*" xref-tests-data-dir nil)) +;; (locs (mapcar #'xref-item-location matches))) +;; (should (= 1 (length matches))) +;; (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)))))) -- cgit v1.2.1 From 4d8fd9cf338cf9b5dfc613657adfeabff2d9c14e Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Wed, 4 May 2016 01:59:29 +0300 Subject: Handle "empty line" regexp in xref searches * lisp/progmodes/xref.el (xref--collect-matches-1): Stop after one match if re-search-forward doesn't move point (bug#23426). * test/automated/xref-tests.el (xref-collect-matches-finds-an-empty-line-regexp-match): Uncomment test. --- test/automated/xref-tests.el | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/automated/xref-tests.el b/test/automated/xref-tests.el index 1b500c3c418..28ca3511c22 100644 --- a/test/automated/xref-tests.el +++ b/test/automated/xref-tests.el @@ -28,10 +28,10 @@ (should (equal 0 (xref-file-location-column (nth 0 locs)))) (should (equal 4 (xref-file-location-column (nth 1 locs)))))) -;; (ert-deftest xref-collect-matches-finds-an-empty-line-regexp-match () -;; (let* ((matches (xref-collect-matches "^$" "*" xref-tests-data-dir nil)) -;; (locs (mapcar #'xref-item-location matches))) -;; (should (= 1 (length matches))) -;; (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-collect-matches-finds-an-empty-line-regexp-match () + (let* ((matches (xref-collect-matches "^$" "*" xref-tests-data-dir nil)) + (locs (mapcar #'xref-item-location matches))) + (should (= 1 (length matches))) + (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)))))) -- cgit v1.2.1 From adc80b7e238e09b1b8c392ecf902d2b978d9016d Mon Sep 17 00:00:00 2001 From: Glenn Morris Date: Tue, 3 May 2016 19:36:40 -0700 Subject: ; * test/automated/xref-tests.el: Add copyright and license. --- test/automated/xref-tests.el | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test') diff --git a/test/automated/xref-tests.el b/test/automated/xref-tests.el index 28ca3511c22..b288e2d7584 100644 --- a/test/automated/xref-tests.el +++ b/test/automated/xref-tests.el @@ -1,3 +1,28 @@ +;;; xref-tests.el --- tests for xref + +;; Copyright (C) 2016 Free Software Foundation, Inc. + +;; Author: Dmitry Gutov + +;; 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: + +;;; Code: + (require 'xref) (require 'cl-lib) -- cgit v1.2.1 From 922c7a3e48e649ad67bd12b1f83343b730dd1bc4 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Thu, 5 May 2016 02:52:34 +0300 Subject: Rework xref-query-replace-in-results * lisp/progmodes/xref.el (xref-query-replace-in-results): Collect all xrefs from the buffer first, then delegate most of the processing to the value returned by xref--buf-pairs-iterator. (xref--buf-pairs-iterator): New function. Return an "iterator" which partitions returned markers into buffers, and only processes markers from one buffer at a time. When an xref is out of date, skip it with a message instead of signaling error (bug#23284). (xref--outdated-p): Extract from xref--buf-pairs-iterator. Trim CR from both strings before comparing. (xref--query-replace-1): Remove the variable current-buf, no need to track it anymore. Simplify the filter-predicate and search functions accordingly. Iterate over buffer-markers pairs returned by the iterator, and call `perform-replace' for each of them. Use multi-query-replace-map (bug#23284). Use `switch-to-buffer' every time after the first, in order not to jump between windows. * test/automated/xref-tests.el (xref--buf-pairs-iterator-groups-markers-by-buffers-1) (xref--buf-pairs-iterator-groups-markers-by-buffers-2) (xref--buf-pairs-iterator-cleans-up-markers): New tests. --- test/automated/xref-tests.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test') diff --git a/test/automated/xref-tests.el b/test/automated/xref-tests.el index b288e2d7584..079b196aa8b 100644 --- a/test/automated/xref-tests.el +++ b/test/automated/xref-tests.el @@ -60,3 +60,32 @@ (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-collect-matches "foo" "*" xref-tests-data-dir nil)) + (iter (xref--buf-pairs-iterator xrefs)) + (cons (funcall iter :next))) + (should (null (funcall iter :next))) + (should (string-match "file1\\.txt\\'" (buffer-file-name (car cons)))) + (should (= 2 (length (cdr cons)))))) + +(ert-deftest xref--buf-pairs-iterator-groups-markers-by-buffers-2 () + (let* ((xrefs (xref-collect-matches "bar" "*" xref-tests-data-dir nil)) + (iter (xref--buf-pairs-iterator xrefs)) + (cons1 (funcall iter :next)) + (cons2 (funcall iter :next))) + (should (null (funcall iter :next))) + (should-not (equal (car cons1) (car cons2))) + (should (= 1 (length (cdr cons1)))) + (should (= 1 (length (cdr cons2)))))) + +(ert-deftest xref--buf-pairs-iterator-cleans-up-markers () + (let* ((xrefs (xref-collect-matches "bar" "*" xref-tests-data-dir nil)) + (iter (xref--buf-pairs-iterator xrefs)) + (cons1 (funcall iter :next)) + (cons2 (funcall iter :next))) + (funcall iter :cleanup) + (should (null (marker-position (car (nth 0 (cdr cons1)))))) + (should (null (marker-position (cdr (nth 0 (cdr cons1)))))) + (should (null (marker-position (car (nth 0 (cdr cons2)))))) + (should (null (marker-position (cdr (nth 0 (cdr cons2)))))))) -- cgit v1.2.1