]> code.delx.au - gnu-emacs/blob - test/automated/reftex-tests.el
Merge from emacs-24; up to 2013-01-03T02:31:36Z!rgm@gnu.org
[gnu-emacs] / test / automated / reftex-tests.el
1 ;;; reftex-tests.el --- Test suite for reftex. -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: RĂ¼diger Sonderfeld <ruediger@c-plusplus.de>
6 ;; Keywords: internal
7 ;; Human-Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Code:
25
26 (require 'ert)
27
28 ;;; reftex
29 (require 'reftex)
30
31 ;;; reftex-parse
32 (require 'reftex-parse)
33
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)))
62
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)
71
72 (should (equal (reftex-what-environment 1) '("equation" . 1)))
73 (should (equal (reftex-what-environment t) '(("equation" . 1))))
74
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))))))
85
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))))
95
96 (ert-deftest reftex-parse-from-file-test ()
97 "Test `reftex-parse-from-file'."
98 (let* ((temp-dir (make-temp-file "reftex-parse" 'dir))
99 (tex-file (expand-file-name "test.tex" temp-dir))
100 (bib-file (expand-file-name "ref.bib" temp-dir)))
101 (with-temp-buffer
102 (insert
103 "\\begin{document}
104 \\section{test}\\label{sec:test}
105 \\subsection{subtest}
106
107 \\begin{align*}\\label{eq:foo}
108 x &= y^2
109 \\end{align*}
110
111 \\bibliographystyle{plain}
112 \\bibliography{ref}
113 \\end{document}")
114 (write-region (point-min) (point-max) tex-file))
115 (with-temp-buffer
116 (insert "test\n")
117 (write-region (point-min) (point-max) bib-file))
118 (reftex-ensure-compiled-variables)
119 (let ((parsed (reftex-parse-from-file tex-file nil temp-dir)))
120 (should (equal (car parsed) `(eof ,tex-file)))
121 (pop parsed)
122 (while parsed
123 (let ((entry (pop parsed)))
124 (cond
125 ((eq (car entry) 'bib)
126 (should (string= (cadr entry) bib-file)))
127 ((eq (car entry) 'toc)) ;; ...
128 ((string= (car entry) "eq:foo"))
129 ((string= (car entry) "sec:test"))
130 ((eq (car entry) 'bof)
131 (should (string= (cadr entry) tex-file))
132 (should (null parsed)))
133 (t (should-not t)))))
134 (delete-directory temp-dir 'recursive))))
135
136 ;;; reftex-cite
137 (require 'reftex-cite)
138
139 (ert-deftest reftex-parse-bibtex-entry-test ()
140 "Test `reftex-parse-bibtex-entry'."
141 (let ((entry "@Book{Stallman12,
142 author = {Richard Stallman\net al.},
143 title = {The Emacs Editor},
144 publisher = {GNU Press},
145 year = 2012,
146 edition = {17th},
147 note = {Updated for Emacs Version 24.2}
148 }")
149 (check (function
150 (lambda (parsed)
151 (should (string= (reftex-get-bib-field "&key" parsed)
152 "Stallman12"))
153 (should (string= (reftex-get-bib-field "&type" parsed)
154 "book"))
155 (should (string= (reftex-get-bib-field "author" parsed)
156 "Richard Stallman et al."))
157 (should (string= (reftex-get-bib-field "title" parsed)
158 "The Emacs Editor"))
159 (should (string= (reftex-get-bib-field "publisher" parsed)
160 "GNU Press"))
161 (should (string= (reftex-get-bib-field "year" parsed)
162 "2012"))
163 (should (string= (reftex-get-bib-field "edition" parsed)
164 "17th"))
165 (should (string= (reftex-get-bib-field "note" parsed)
166 "Updated for Emacs Version 24.2"))))))
167 (funcall check (reftex-parse-bibtex-entry entry))
168 (with-temp-buffer
169 (insert entry)
170 (funcall check (reftex-parse-bibtex-entry nil (point-min)
171 (point-max))))))
172
173 (ert-deftest reftex-get-bib-names-test ()
174 "Test `reftex-get-bib-names'."
175 (let ((entry (reftex-parse-bibtex-entry "@article{Foo123,
176 author = {Jane Roe and\tJohn Doe and W. Public},
177 }")))
178 (should (equal (reftex-get-bib-names "author" entry)
179 '("Jane Roe" "John Doe" "Public"))))
180 (let ((entry (reftex-parse-bibtex-entry "@article{Foo123,
181 editor = {Jane Roe and\tJohn Doe and W. Public},
182 }")))
183 (should (equal (reftex-get-bib-names "author" entry)
184 '("Jane Roe" "John Doe" "Public")))))
185
186 (ert-deftest reftex-format-citation-test ()
187 "Test `reftex-format-citation'."
188 (let ((entry (reftex-parse-bibtex-entry
189 "@article{Foo13,
190 author = {Jane Roe and John Doe and Jane Q. Taxpayer},
191 title = {Some Article},
192 journal = {Some Journal},
193 year = 2013,
194 pages = {1--333}
195 }")))
196 (should (string= (reftex-format-citation entry nil) "\\cite{Foo13}"))
197 (should (string= (reftex-format-citation entry "%l:%A:%y:%t %j %P %a")
198 "Foo13:Jane Roe:2013:Some Article Some Journal 1 Jane Roe, John Doe \\& Jane Taxpayer"))))
199
200 (provide 'reftex-tests)
201 ;;; reftex-tests.el ends here.