* doc/lispref/elisp.texi: Fix typo in previous change.
[emacs.git] / test / automated / reftex-tests.el
blob962e39ff38ed071d257a3107072abd7d0901450d
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>
6 ;; Keywords: internal
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/>.
24 ;;; Code:
26 (require 'ert)
28 ;;; reftex
29 (require 'reftex)
31 ;;; reftex-parse
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="
45 "addbibresource")))
46 (with-temp-buffer
47 (insert "test\n")
48 (mapc
49 (lambda (file)
50 (write-region (point-min) (point-max) (expand-file-name file
51 temp-dir)))
52 files))
53 (mapc
54 (lambda (data)
55 (with-temp-buffer
56 (insert (car data))
57 (let ((res (mapcar #'file-name-nondirectory
58 (reftex-locate-bibliography-files temp-dir))))
59 (should (equal res (cdr data))))))
60 test)
61 (delete-directory temp-dir 'recursive)))
63 (ert-deftest reftex-what-environment-test ()
64 "Test `reftex-what-environment'."
65 (with-temp-buffer
66 (insert "\\begin{equation}\n x=y^2\n")
67 (let ((pt (point))
68 pt2)
69 (insert "\\end{equation}\n")
70 (goto-char pt)
72 (should (equal (reftex-what-environment 1) '("equation" . 1)))
73 (should (equal (reftex-what-environment t) '(("equation" . 1))))
75 (insert "\\begin{something}\nxxx")
76 (setq pt2 (point))
77 (insert "\\end{something}")
78 (goto-char pt2)
79 (should (equal (reftex-what-environment 1) `("something" . ,pt)))
80 (should (equal (reftex-what-environment t) `(("something" . ,pt)
81 ("equation" . 1))))
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))
92 (car roman)))
93 (pop roman)
94 (pop 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
104 ;; list.)
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)))
108 (with-temp-buffer
109 (insert
110 "\\begin{document}
111 \\section{test}\\label{sec:test}
112 \\subsection{subtest}
114 \\begin{align*}\\label{eq:foo}
115 x &= y^2
116 \\end{align*}
118 \\bibliographystyle{plain}
119 \\bibliography{ref}
120 \\end{document}")
121 (write-region (point-min) (point-max) tex-file))
122 (with-temp-buffer
123 (insert "test\n")
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)))
128 (pop parsed)
129 (while parsed
130 (let ((entry (pop parsed)))
131 (cond
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))))
143 ;;; reftex-cite
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},
152 year = 2012,
153 edition = {17th},
154 note = {Updated for Emacs Version 24.2}
156 (check (function
157 (lambda (parsed)
158 (should (string= (reftex-get-bib-field "&key" parsed)
159 "Stallman12"))
160 (should (string= (reftex-get-bib-field "&type" parsed)
161 "book"))
162 (should (string= (reftex-get-bib-field "author" parsed)
163 "Richard Stallman et al."))
164 (should (string= (reftex-get-bib-field "title" parsed)
165 "The Emacs Editor"))
166 (should (string= (reftex-get-bib-field "publisher" parsed)
167 "GNU Press"))
168 (should (string= (reftex-get-bib-field "year" parsed)
169 "2012"))
170 (should (string= (reftex-get-bib-field "edition" parsed)
171 "17th"))
172 (should (string= (reftex-get-bib-field "note" parsed)
173 "Updated for Emacs Version 24.2"))))))
174 (funcall check (reftex-parse-bibtex-entry entry))
175 (with-temp-buffer
176 (insert entry)
177 (funcall check (reftex-parse-bibtex-entry nil (point-min)
178 (point-max))))))
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},
184 }")))
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},
189 }")))
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
196 "@article{Foo13,
197 author = {Jane Roe and John Doe and Jane Q. Taxpayer},
198 title = {Some Article},
199 journal = {Some Journal},
200 year = 2013,
201 pages = {1--333}
202 }")))
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.