2020-04-28 13:54:00 +02:00
|
|
|
;;; xref-tests.el --- tests for xref -*- lexical-binding:t -*-
|
2016-05-03 19:36:40 -07:00
|
|
|
|
2020-01-01 00:19:43 +00:00
|
|
|
;; Copyright (C) 2016-2020 Free Software Foundation, Inc.
|
2016-05-03 19:36:40 -07:00
|
|
|
|
|
|
|
;; Author: Dmitry Gutov <dgutov@yandex.ru>
|
|
|
|
|
|
|
|
;; 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
|
2017-09-13 15:52:52 -07:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2016-05-03 19:36:40 -07:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2020-10-23 16:29:46 +02:00
|
|
|
(require 'ert)
|
2016-05-04 01:38:02 +03:00
|
|
|
(require 'xref)
|
|
|
|
(require 'cl-lib)
|
|
|
|
|
2020-12-08 09:59:15 +01:00
|
|
|
(defvar xref-tests--data-dir
|
2020-10-25 11:24:11 +01:00
|
|
|
(expand-file-name "xref-resources/"
|
|
|
|
(file-name-directory
|
|
|
|
(or load-file-name buffer-file-name))))
|
2016-05-04 01:38:02 +03:00
|
|
|
|
2020-12-08 09:59:15 +01:00
|
|
|
(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)))
|
|
|
|
|
2019-12-29 15:44:08 +03:00
|
|
|
(ert-deftest xref-matches-in-directory-finds-none-for-some-regexp ()
|
2020-12-08 09:59:15 +01:00
|
|
|
(should (null (xref-tests--matches-in-data-dir "zzz"))))
|
2016-05-04 01:38:02 +03:00
|
|
|
|
2019-12-29 15:44:08 +03:00
|
|
|
(ert-deftest xref-matches-in-directory-finds-some-for-bar ()
|
2020-12-08 09:59:15 +01:00
|
|
|
(let ((locs (xref-tests--locations-in-data-dir "bar")))
|
|
|
|
(should (= 2 (length locs)))
|
2016-05-04 01:38:02 +03:00
|
|
|
(should (string-match-p "file1\\.txt\\'" (xref-location-group (nth 0 locs))))
|
|
|
|
(should (string-match-p "file2\\.txt\\'" (xref-location-group (nth 1 locs))))))
|
|
|
|
|
2019-12-29 15:44:08 +03:00
|
|
|
(ert-deftest xref-matches-in-directory-finds-two-matches-on-the-same-line ()
|
2020-12-08 09:59:15 +01:00
|
|
|
(let ((locs (xref-tests--locations-in-data-dir "foo")))
|
|
|
|
(should (= 2 (length locs)))
|
2016-05-04 01:38:02 +03:00
|
|
|
(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))))
|
2020-12-21 03:38:37 +02:00
|
|
|
(should (equal 0 (xref-location-column (nth 0 locs))))
|
|
|
|
(should (equal 4 (xref-location-column (nth 1 locs))))))
|
2016-05-04 01:38:02 +03:00
|
|
|
|
2019-12-29 15:44:08 +03:00
|
|
|
(ert-deftest xref-matches-in-directory-finds-an-empty-line-regexp-match ()
|
2020-12-08 09:59:15 +01:00
|
|
|
(let ((locs (xref-tests--locations-in-data-dir "^$")))
|
|
|
|
(should (= 1 (length locs)))
|
2016-05-04 01:59:29 +03:00
|
|
|
(should (string-match-p "file2\\.txt\\'" (xref-location-group (nth 0 locs))))
|
|
|
|
(should (equal 1 (xref-location-line (nth 0 locs))))
|
2020-12-21 03:38:37 +02:00
|
|
|
(should (equal 0 (xref-location-column (nth 0 locs))))))
|
2016-05-05 02:52:34 +03:00
|
|
|
|
|
|
|
(ert-deftest xref--buf-pairs-iterator-groups-markers-by-buffers-1 ()
|
2020-12-08 09:59:15 +01:00
|
|
|
(let* ((xrefs (xref-tests--matches-in-data-dir "foo"))
|
2016-05-05 02:52:34 +03:00
|
|
|
(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 ()
|
2020-12-08 09:59:15 +01:00
|
|
|
(let* ((xrefs (xref-tests--matches-in-data-dir "bar"))
|
2016-05-05 02:52:34 +03:00
|
|
|
(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 ()
|
2020-12-08 09:59:15 +01:00
|
|
|
(let* ((xrefs (xref-tests--matches-in-data-dir "bar"))
|
2016-05-05 02:52:34 +03:00
|
|
|
(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))))))))
|