reftex-tests.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; reftex-tests.el --- Test suite for reftex. -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2013-2017 Free Software Foundation, Inc.
  3. ;; Author: Rüdiger Sonderfeld <ruediger@c-plusplus.de>
  4. ;; Keywords: internal
  5. ;; Human-Keywords: internal
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Code:
  18. (require 'ert)
  19. ;;; reftex
  20. (require 'reftex)
  21. ;;; reftex-parse
  22. (require 'reftex-parse)
  23. (ert-deftest reftex-locate-bibliography-files ()
  24. "Test `reftex-locate-bibliography-files'."
  25. (let ((temp-dir (make-temp-file "reftex-bib" 'dir))
  26. (files '("ref1.bib" "ref2.bib"))
  27. (test '(("\\addbibresource{ref1.bib}\n" . ("ref1.bib"))
  28. ("\\\\addbibresource[label=x]{ref2.bib}\\n" . ("ref2.bib"))
  29. ("\\begin{document}\n\\bibliographystyle{plain}\n
  30. \\bibliography{ref1,ref2}\n\\end{document}" . ("ref1.bib" "ref2.bib"))))
  31. (reftex-bibliography-commands
  32. ;; Default value: See reftex-vars.el `reftex-bibliography-commands'
  33. '("bibliography" "nobibliography" "setupbibtex\\[.*?database="
  34. "addbibresource")))
  35. (with-temp-buffer
  36. (insert "test\n")
  37. (mapc
  38. (lambda (file)
  39. (write-region (point-min) (point-max) (expand-file-name file
  40. temp-dir)))
  41. files))
  42. (mapc
  43. (lambda (data)
  44. (with-temp-buffer
  45. (insert (car data))
  46. (let ((res (mapcar #'file-name-nondirectory
  47. (reftex-locate-bibliography-files temp-dir))))
  48. (should (equal res (cdr data))))))
  49. test)
  50. (delete-directory temp-dir 'recursive)))
  51. (ert-deftest reftex-what-environment-test ()
  52. "Test `reftex-what-environment'."
  53. (with-temp-buffer
  54. (insert "\\begin{equation}\n x=y^2\n")
  55. (let ((pt (point))
  56. pt2)
  57. (insert "\\end{equation}\n")
  58. (goto-char pt)
  59. (should (equal (reftex-what-environment 1) '("equation" . 1)))
  60. (should (equal (reftex-what-environment t) '(("equation" . 1))))
  61. (insert "\\begin{something}\nxxx")
  62. (setq pt2 (point))
  63. (insert "\\end{something}")
  64. (goto-char pt2)
  65. (should (equal (reftex-what-environment 1) `("something" . ,pt)))
  66. (should (equal (reftex-what-environment t) `(("something" . ,pt)
  67. ("equation" . 1))))
  68. (should (equal (reftex-what-environment t pt) `(("something" . ,pt))))
  69. (should (equal (reftex-what-environment '("equation"))
  70. '("equation" . 1))))))
  71. (ert-deftest reftex-roman-number-test ()
  72. "Test `reftex-roman-number'."
  73. (let ((hindu-arabic '(1 2 4 9 14 1050))
  74. (roman '("I" "II" "IV" "IX" "XIV" "ML")))
  75. (while (and hindu-arabic roman)
  76. (should (string= (reftex-roman-number (car hindu-arabic))
  77. (car roman)))
  78. (pop roman)
  79. (pop hindu-arabic))))
  80. (ert-deftest reftex-parse-from-file-test ()
  81. "Test `reftex-parse-from-file'."
  82. ;; Use file-truename to convert 8+3 aliases in $TEMP value on
  83. ;; MS-Windows into their long file-name equivalents, which is
  84. ;; necessary for the 'equal' and 'string=' comparisons below. This
  85. ;; also resolves any symlinks, which cannot be bad for the same
  86. ;; reason. (An alternative solution would be to use file-equal-p,
  87. ;; but I'm too lazy to do that, as one of the tests compares a
  88. ;; list.)
  89. (let* ((temp-dir (file-truename (make-temp-file "reftex-parse" 'dir)))
  90. (tex-file (expand-file-name "test.tex" temp-dir))
  91. (bib-file (expand-file-name "ref.bib" temp-dir)))
  92. (with-temp-buffer
  93. (insert
  94. "\\begin{document}
  95. \\section{test}\\label{sec:test}
  96. \\subsection{subtest}
  97. \\begin{align*}\\label{eq:foo}
  98. x &= y^2
  99. \\end{align*}
  100. \\bibliographystyle{plain}
  101. \\bibliography{ref}
  102. \\end{document}")
  103. (write-region (point-min) (point-max) tex-file))
  104. (with-temp-buffer
  105. (insert "test\n")
  106. (write-region (point-min) (point-max) bib-file))
  107. (reftex-ensure-compiled-variables)
  108. (let ((parsed (reftex-parse-from-file tex-file nil temp-dir)))
  109. (should (equal (car parsed) `(eof ,tex-file)))
  110. (pop parsed)
  111. (while parsed
  112. (let ((entry (pop parsed)))
  113. (cond
  114. ((eq (car entry) 'bib)
  115. (should (string= (cadr entry) bib-file)))
  116. ((eq (car entry) 'toc)) ;; ...
  117. ((string= (car entry) "eq:foo"))
  118. ((string= (car entry) "sec:test"))
  119. ((eq (car entry) 'bof)
  120. (should (string= (cadr entry) tex-file))
  121. (should (null parsed)))
  122. (t (should-not t)))))
  123. (delete-directory temp-dir 'recursive))))
  124. ;;; reftex-cite
  125. (require 'reftex-cite)
  126. (ert-deftest reftex-parse-bibtex-entry-test ()
  127. "Test `reftex-parse-bibtex-entry'."
  128. (let ((entry "@Book{Stallman12,
  129. author = {Richard Stallman\net al.},
  130. title = {The Emacs Editor},
  131. publisher = {GNU Press},
  132. year = 2012,
  133. edition = {17th},
  134. note = {Updated for Emacs Version 24.2}
  135. }")
  136. (check (function
  137. (lambda (parsed)
  138. (should (string= (reftex-get-bib-field "&key" parsed)
  139. "Stallman12"))
  140. (should (string= (reftex-get-bib-field "&type" parsed)
  141. "book"))
  142. (should (string= (reftex-get-bib-field "author" parsed)
  143. "Richard Stallman et al."))
  144. (should (string= (reftex-get-bib-field "title" parsed)
  145. "The Emacs Editor"))
  146. (should (string= (reftex-get-bib-field "publisher" parsed)
  147. "GNU Press"))
  148. (should (string= (reftex-get-bib-field "year" parsed)
  149. "2012"))
  150. (should (string= (reftex-get-bib-field "edition" parsed)
  151. "17th"))
  152. (should (string= (reftex-get-bib-field "note" parsed)
  153. "Updated for Emacs Version 24.2"))))))
  154. (funcall check (reftex-parse-bibtex-entry entry))
  155. (with-temp-buffer
  156. (insert entry)
  157. (funcall check (reftex-parse-bibtex-entry nil (point-min)
  158. (point-max))))))
  159. (ert-deftest reftex-get-bib-names-test ()
  160. "Test `reftex-get-bib-names'."
  161. (let ((entry (reftex-parse-bibtex-entry "@article{Foo123,
  162. author = {Jane Roe and\tJohn Doe and W. Public},
  163. }")))
  164. (should (equal (reftex-get-bib-names "author" entry)
  165. '("Jane Roe" "John Doe" "Public"))))
  166. (let ((entry (reftex-parse-bibtex-entry "@article{Foo123,
  167. editor = {Jane Roe and\tJohn Doe and W. Public},
  168. }")))
  169. (should (equal (reftex-get-bib-names "author" entry)
  170. '("Jane Roe" "John Doe" "Public")))))
  171. (ert-deftest reftex-format-citation-test ()
  172. "Test `reftex-format-citation'."
  173. (let ((entry (reftex-parse-bibtex-entry
  174. "@article{Foo13,
  175. author = {Jane Roe and John Doe and Jane Q. Taxpayer},
  176. title = {Some Article},
  177. journal = {Some Journal},
  178. year = 2013,
  179. pages = {1--333}
  180. }")))
  181. (should (string= (reftex-format-citation entry nil) "\\cite{Foo13}"))
  182. (should (string= (reftex-format-citation entry "%l:%A:%y:%t %j %P %a")
  183. "Foo13:Jane Roe:2013:Some Article Some Journal 1 Jane Roe, John Doe \\& Jane Taxpayer"))))
  184. ;;; Autoload tests
  185. ;; Test to check whether reftex autoloading mechanisms are working
  186. ;; correctly.
  187. (ert-deftest reftex-autoload-auc ()
  188. "Tests to see whether reftex-auc has been autoloaded"
  189. (should
  190. (fboundp 'reftex-arg-label))
  191. (should
  192. (autoloadp
  193. (symbol-function
  194. 'reftex-arg-label))))
  195. (provide 'reftex-tests)
  196. ;;; reftex-tests.el ends here.