1 ;;; reftex-tests.el --- Test suite for reftex. -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
5 ;; Author: RĂ¼diger Sonderfeld <ruediger@c-plusplus.de>
7 ;; Human-Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (require 'reftex-parse
)
34 (ert-deftest reftex-locate-bibliography-files
()
35 "Test `reftex-locate-bibliography-files'."
36 (let ((temp-dir (make-temp-file "reftex-bib" 'dir
))
37 (files '("ref1.bib" "ref2.bib"))
38 (test '(("\\addbibresource{ref1.bib}\n" .
("ref1.bib"))
39 ("\\\\addbibresource[label=x]{ref2.bib}\\n" .
("ref2.bib"))
40 ("\\begin{document}\n\\bibliographystyle{plain}\n
41 \\bibliography{ref1,ref2}\n\\end{document}" .
("ref1.bib" "ref2.bib"))))
42 (reftex-bibliography-commands
43 ;; Default value: See reftex-vars.el `reftex-bibliography-commands'
44 '("bibliography" "nobibliography" "setupbibtex\\[.*?database="
50 (write-region (point-min) (point-max) (expand-file-name file
57 (let ((res (mapcar #'file-name-nondirectory
58 (reftex-locate-bibliography-files temp-dir
))))
59 (should (equal res
(cdr data
))))))
61 (delete-directory temp-dir
'recursive
)))
63 (ert-deftest reftex-what-environment-test
()
64 "Test `reftex-what-environment'."
66 (insert "\\begin{equation}\n x=y^2\n")
69 (insert "\\end{equation}\n")
72 (should (equal (reftex-what-environment 1) '("equation" .
1)))
73 (should (equal (reftex-what-environment t
) '(("equation" .
1))))
75 (insert "\\begin{something}\nxxx")
77 (insert "\\end{something}")
79 (should (equal (reftex-what-environment 1) `("something" .
,pt
)))
80 (should (equal (reftex-what-environment t
) `(("something" .
,pt
)
82 (should (equal (reftex-what-environment t pt
) `(("something" .
,pt
))))
83 (should (equal (reftex-what-environment '("equation"))
84 '("equation" .
1))))))
86 (ert-deftest reftex-roman-number-test
()
87 "Test `reftex-roman-number'."
88 (let ((hindu-arabic '(1 2 4 9 14 1050))
89 (roman '("I" "II" "IV" "IX" "XIV" "ML")))
90 (while (and hindu-arabic roman
)
91 (should (string= (reftex-roman-number (car hindu-arabic
))
96 (ert-deftest reftex-parse-from-file-test
()
97 "Test `reftex-parse-from-file'."
98 ;; Use file-truename to convert 8+3 aliases in $TEMP value on
99 ;; MS-Windows into their long file-name equivalents, which is
100 ;; necessary for the 'equal' and 'string=' comparisons below. This
101 ;; also resolves any symlinks, which cannot be bad for the same
102 ;; reason. (An alternative solution would be to use file-equal-p,
103 ;; but I'm too lazy to do that, as one of the tests compares a
105 (let* ((temp-dir (file-truename (make-temp-file "reftex-parse" 'dir
)))
106 (tex-file (expand-file-name "test.tex" temp-dir
))
107 (bib-file (expand-file-name "ref.bib" temp-dir
)))
111 \\section{test}\\label{sec:test}
112 \\subsection{subtest}
114 \\begin{align*}\\label{eq:foo}
118 \\bibliographystyle{plain}
121 (write-region (point-min) (point-max) tex-file
))
124 (write-region (point-min) (point-max) bib-file
))
125 (reftex-ensure-compiled-variables)
126 (let ((parsed (reftex-parse-from-file tex-file nil temp-dir
)))
127 (should (equal (car parsed
) `(eof ,tex-file
)))
130 (let ((entry (pop parsed
)))
132 ((eq (car entry
) 'bib
)
133 (should (string= (cadr entry
) bib-file
)))
134 ((eq (car entry
) 'toc
)) ;; ...
135 ((string= (car entry
) "eq:foo"))
136 ((string= (car entry
) "sec:test"))
137 ((eq (car entry
) 'bof
)
138 (should (string= (cadr entry
) tex-file
))
139 (should (null parsed
)))
140 (t (should-not t
)))))
141 (delete-directory temp-dir
'recursive
))))
144 (require 'reftex-cite
)
146 (ert-deftest reftex-parse-bibtex-entry-test
()
147 "Test `reftex-parse-bibtex-entry'."
148 (let ((entry "@Book{Stallman12,
149 author = {Richard Stallman\net al.},
150 title = {The Emacs Editor},
151 publisher = {GNU Press},
154 note = {Updated for Emacs Version 24.2}
158 (should (string= (reftex-get-bib-field "&key" parsed
)
160 (should (string= (reftex-get-bib-field "&type" parsed
)
162 (should (string= (reftex-get-bib-field "author" parsed
)
163 "Richard Stallman et al."))
164 (should (string= (reftex-get-bib-field "title" parsed
)
166 (should (string= (reftex-get-bib-field "publisher" parsed
)
168 (should (string= (reftex-get-bib-field "year" parsed
)
170 (should (string= (reftex-get-bib-field "edition" parsed
)
172 (should (string= (reftex-get-bib-field "note" parsed
)
173 "Updated for Emacs Version 24.2"))))))
174 (funcall check
(reftex-parse-bibtex-entry entry
))
177 (funcall check
(reftex-parse-bibtex-entry nil
(point-min)
180 (ert-deftest reftex-get-bib-names-test
()
181 "Test `reftex-get-bib-names'."
182 (let ((entry (reftex-parse-bibtex-entry "@article{Foo123,
183 author = {Jane Roe and\tJohn Doe and W. Public},
185 (should (equal (reftex-get-bib-names "author" entry
)
186 '("Jane Roe" "John Doe" "Public"))))
187 (let ((entry (reftex-parse-bibtex-entry "@article{Foo123,
188 editor = {Jane Roe and\tJohn Doe and W. Public},
190 (should (equal (reftex-get-bib-names "author" entry
)
191 '("Jane Roe" "John Doe" "Public")))))
193 (ert-deftest reftex-format-citation-test
()
194 "Test `reftex-format-citation'."
195 (let ((entry (reftex-parse-bibtex-entry
197 author = {Jane Roe and John Doe and Jane Q. Taxpayer},
198 title = {Some Article},
199 journal = {Some Journal},
203 (should (string= (reftex-format-citation entry nil
) "\\cite{Foo13}"))
204 (should (string= (reftex-format-citation entry
"%l:%A:%y:%t %j %P %a")
205 "Foo13:Jane Roe:2013:Some Article Some Journal 1 Jane Roe, John Doe \\& Jane Taxpayer"))))
207 (provide 'reftex-tests
)
208 ;;; reftex-tests.el ends here.