xref-tests.el 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;; xref-tests.el --- tests for xref
  2. ;; Copyright (C) 2016-2017 Free Software Foundation, Inc.
  3. ;; Author: Dmitry Gutov <dgutov@yandex.ru>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;; Code:
  17. (require 'xref)
  18. (require 'cl-lib)
  19. (defvar xref-tests-data-dir
  20. (expand-file-name "data/xref/"
  21. (getenv "EMACS_TEST_DIRECTORY")))
  22. (ert-deftest xref-collect-matches-finds-none-for-some-regexp ()
  23. (should (null (xref-collect-matches "zzz" "*" xref-tests-data-dir nil))))
  24. (ert-deftest xref-collect-matches-finds-some-for-bar ()
  25. (let* ((matches (xref-collect-matches "bar" "*" xref-tests-data-dir nil))
  26. (locs (cl-sort (mapcar #'xref-item-location matches)
  27. #'string<
  28. :key #'xref-location-group)))
  29. (should (= 2 (length matches)))
  30. (should (string-match-p "file1\\.txt\\'" (xref-location-group (nth 0 locs))))
  31. (should (string-match-p "file2\\.txt\\'" (xref-location-group (nth 1 locs))))))
  32. (ert-deftest xref-collect-matches-finds-two-matches-on-the-same-line ()
  33. (let* ((matches (xref-collect-matches "foo" "*" xref-tests-data-dir nil))
  34. (locs (mapcar #'xref-item-location matches)))
  35. (should (= 2 (length matches)))
  36. (should (string-match-p "file1\\.txt\\'" (xref-location-group (nth 0 locs))))
  37. (should (string-match-p "file1\\.txt\\'" (xref-location-group (nth 1 locs))))
  38. (should (equal 1 (xref-location-line (nth 0 locs))))
  39. (should (equal 1 (xref-location-line (nth 1 locs))))
  40. (should (equal 0 (xref-file-location-column (nth 0 locs))))
  41. (should (equal 4 (xref-file-location-column (nth 1 locs))))))
  42. (ert-deftest xref-collect-matches-finds-an-empty-line-regexp-match ()
  43. (let* ((matches (xref-collect-matches "^$" "*" xref-tests-data-dir nil))
  44. (locs (mapcar #'xref-item-location matches)))
  45. (should (= 1 (length matches)))
  46. (should (string-match-p "file2\\.txt\\'" (xref-location-group (nth 0 locs))))
  47. (should (equal 1 (xref-location-line (nth 0 locs))))
  48. (should (equal 0 (xref-file-location-column (nth 0 locs))))))
  49. (ert-deftest xref--buf-pairs-iterator-groups-markers-by-buffers-1 ()
  50. (let* ((xrefs (xref-collect-matches "foo" "*" xref-tests-data-dir nil))
  51. (iter (xref--buf-pairs-iterator xrefs))
  52. (cons (funcall iter :next)))
  53. (should (null (funcall iter :next)))
  54. (should (string-match "file1\\.txt\\'" (buffer-file-name (car cons))))
  55. (should (= 2 (length (cdr cons))))))
  56. (ert-deftest xref--buf-pairs-iterator-groups-markers-by-buffers-2 ()
  57. (let* ((xrefs (xref-collect-matches "bar" "*" xref-tests-data-dir nil))
  58. (iter (xref--buf-pairs-iterator xrefs))
  59. (cons1 (funcall iter :next))
  60. (cons2 (funcall iter :next)))
  61. (should (null (funcall iter :next)))
  62. (should-not (equal (car cons1) (car cons2)))
  63. (should (= 1 (length (cdr cons1))))
  64. (should (= 1 (length (cdr cons2))))))
  65. (ert-deftest xref--buf-pairs-iterator-cleans-up-markers ()
  66. (let* ((xrefs (xref-collect-matches "bar" "*" xref-tests-data-dir nil))
  67. (iter (xref--buf-pairs-iterator xrefs))
  68. (cons1 (funcall iter :next))
  69. (cons2 (funcall iter :next)))
  70. (funcall iter :cleanup)
  71. (should (null (marker-position (car (nth 0 (cdr cons1))))))
  72. (should (null (marker-position (cdr (nth 0 (cdr cons1))))))
  73. (should (null (marker-position (car (nth 0 (cdr cons2))))))
  74. (should (null (marker-position (cdr (nth 0 (cdr cons2))))))))