imenu-tests.el 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. ;;; imenu-tests.el --- Test suite for imenu.
  2. ;; Copyright (C) 2013-2017 Free Software Foundation, Inc.
  3. ;; Author: Masatake YAMATO <yamato@redhat.com>
  4. ;; Keywords: tools convenience
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (require 'ert)
  18. (require 'imenu)
  19. ;; (imenu-simple-scan-deftest-gather-strings-from-list
  20. ;; '(nil t 'a (0 . "x") ("c" . "d") ("a" 0 "b") ))
  21. ;; => ("b" "a" "d" "c" "x")
  22. (defun imenu-simple-scan-deftest-gather-strings-from-list(input)
  23. "Gather strings from INPUT, a list."
  24. (let ((result ()))
  25. (while input
  26. (cond
  27. ((stringp input)
  28. (setq result (cons input result)
  29. input nil))
  30. ((atom input)
  31. (setq input nil))
  32. ((listp (car input))
  33. (setq result (append
  34. (imenu-simple-scan-deftest-gather-strings-from-list (car input))
  35. result)
  36. input (cdr input)))
  37. ((stringp (car input))
  38. (setq result (cons (car input) result)
  39. input (cdr input)))
  40. (t
  41. (setq input (cdr input)))))
  42. result))
  43. (defmacro imenu-simple-scan-deftest (name doc major-mode content expected-items)
  44. "Generate an ert test for mode-own imenu expression.
  45. Run `imenu-create-index-function' at the buffer which content is
  46. CONTENT with MAJOR-MODE. A generated test runs `imenu-create-index-function'
  47. at the buffer which content is CONTENT with MAJOR-MODE. Then it compares a list
  48. of strings which are picked up from the result with EXPECTED-ITEMS."
  49. (let ((xname (intern (concat "imenu-simple-scan-deftest-" (symbol-name name)))))
  50. `(ert-deftest ,xname ()
  51. ,doc
  52. (with-temp-buffer
  53. (insert ,content)
  54. (funcall ',major-mode)
  55. (let ((result-items (sort (imenu-simple-scan-deftest-gather-strings-from-list
  56. (funcall imenu-create-index-function))
  57. #'string-lessp))
  58. (expected-items (sort (copy-sequence ,expected-items) #'string-lessp)))
  59. (should (equal result-items expected-items))
  60. )))))
  61. (imenu-simple-scan-deftest sh "Test imenu expression for sh-mode." sh-mode "a()
  62. {
  63. }
  64. function b
  65. {
  66. }
  67. function c()
  68. {
  69. }
  70. function ABC_D()
  71. {
  72. }
  73. " '("a" "b" "c" "ABC_D"))
  74. (provide 'imenu-tests)
  75. ;;; imenu-tests.el ends here