all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob dd083d655b1f56b8f4cc7aed9d7824a4d5569ed4 6162 bytes (raw)
name: test/lisp/progmodes/xref-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
 
;;; xref-tests.el --- tests for xref  -*- lexical-binding:t -*-

;; Copyright (C) 2016-2021 Free Software Foundation, Inc.

;; 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
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'ert)
(require 'xref)
(require 'cl-lib)

(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-tests--matches-in-data-dir "zzz"))))

(ert-deftest xref-matches-in-directory-finds-some-for-bar ()
  (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 ((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))))
    (should (equal 1 (xref-location-line (nth 1 locs))))
    (should (equal 0 (xref-location-column (nth 0 locs))))
    (should (equal 4 (xref-location-column (nth 1 locs))))))

(ert-deftest xref-matches-in-directory-finds-an-empty-line-regexp-match ()
  (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-location-column (nth 0 locs))))))

(ert-deftest xref--buf-pairs-iterator-groups-markers-by-buffers-1 ()
  (let* ((xrefs (xref-tests--matches-in-data-dir "foo"))
         (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-tests--matches-in-data-dir "bar"))
         (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-tests--matches-in-data-dir "bar"))
         (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))))))))

(ert-deftest xref--xref-file-name-display-is-abs ()
  (let ((xref-file-name-display 'abs)
        ;; BSD find may add an extra '/' to the path.
        (expected (list
                   (concat xref-tests--data-dir "/?file1.txt")
                   (concat xref-tests--data-dir "/?file2.txt")))
        (actual (delete-dups
                 (mapcar 'xref-location-group
                         (xref-tests--locations-in-data-dir "\\(bar\\|foo\\)")))))
    (should (and (= (length expected) (length actual))
                 (cl-every (lambda (e1 e2)
                             (string-match-p e1 e2))
                           expected actual)))))

(ert-deftest xref--xref-file-name-display-is-nondirectory ()
  (let ((xref-file-name-display 'nondirectory))
    (should (equal (delete-dups
                    (mapcar 'xref-location-group
                            (xref-tests--locations-in-data-dir "\\(bar\\|foo\\)")))
                   (list
                    "file1.txt"
                    "file2.txt")))))

(ert-deftest xref--xref-file-name-display-is-relative-to-project-root ()
  (let* ((data-parent-dir
          (file-name-directory (directory-file-name xref-tests--data-dir)))
         (project-find-functions
          #'(lambda (_) (cons 'transient data-parent-dir)))
         (xref-file-name-display 'project-relative)
         ;; BSD find may add an extra '/' to the path.
         (expected (list
                    "xref-resources//?file1.txt"
                    "xref-resources//?file2.txt"))
         (actual (delete-dups
                  (mapcar 'xref-location-group
                          (xref-tests--locations-in-data-dir "\\(bar\\|foo\\)")))))
    (should (and (= (length expected) (length actual))
                 (cl-every (lambda (e1 e2)
                             (string-match-p e1 e2))
                           expected actual)))))

debug log:

solving dd083d655b ...
found dd083d655b in https://yhetil.org/emacs/m1sg7i7xfl.fsf@yahoo.es/
found eaafc5888c in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 eaafc5888c7ef2828b0fad2f45554fae9120da37	test/lisp/progmodes/xref-tests.el

applying [1/1] https://yhetil.org/emacs/m1sg7i7xfl.fsf@yahoo.es/
diff --git a/test/lisp/progmodes/xref-tests.el b/test/lisp/progmodes/xref-tests.el
index eaafc5888c..dd083d655b 100644

Checking patch test/lisp/progmodes/xref-tests.el...
Applied patch test/lisp/progmodes/xref-tests.el cleanly.

index at:
100644 dd083d655b1f56b8f4cc7aed9d7824a4d5569ed4	test/lisp/progmodes/xref-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.