]> code.delx.au - gnu-emacs/blob - test/automated/tildify-tests.el
Update copyright year to 2016
[gnu-emacs] / test / automated / tildify-tests.el
1 ;;; tildify-test.el --- ERT tests for tildify.el -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
4
5 ;; Author: Michal Nazarewicz <mina86@mina86.com>
6 ;; Version: 4.5
7 ;; Keywords: text, TeX, SGML, wp
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 ;;; Commentary:
25
26 ;; This package defines regression tests for the tildify package.
27
28 ;;; Code:
29
30 (require 'ert)
31 (require 'tildify)
32
33 (defun tildify-test--example-sentence (space)
34 "Return an example sentence with SPACE where hard space is required."
35 (concat "Lorem ipsum v" space "dolor sit amet, a" space
36 "consectetur adipiscing elit."))
37
38
39 (defun tildify-test--example-html (sentence &optional with-nbsp is-xml)
40 "Return an example HTML code.
41 SENTENCE is placed where spaces should not be replaced with hard spaces, and
42 WITH-NBSP is placed where spaces should be replaced with hard spaces. If the
43 latter is missing, SENTENCE will be used in all placeholder positions.
44 If IS-XML is non-nil, <pre> tag is not treated specially."
45 (let ((with-nbsp (or with-nbsp sentence)))
46 (concat "<p>" with-nbsp "</p>\n"
47 "<pre>" (if is-xml with-nbsp sentence) "</pre>\n"
48 "<! -- " sentence " -- >\n"
49 "<p>" with-nbsp "</p>\n"
50 "<" sentence ">\n")))
51
52
53 (defun tildify-test--test (modes input expected)
54 "Test tildify running in MODES.
55 INPUT is the initial content of the buffer and EXPECTED is expected result
56 after `tildify-buffer' is run."
57 (with-temp-buffer
58 (setq-local buffer-file-coding-system 'utf-8)
59 (dolist (mode modes)
60 (erase-buffer)
61 (funcall mode)
62 (let ((header (concat "Testing `tildify-buffer' in "
63 (symbol-name mode) "\n")))
64 (insert header input)
65 (tildify-buffer t)
66 (should (string-equal (concat header expected) (buffer-string))))
67 (erase-buffer)
68 (let ((header (concat "Testing `tildify-region' in "
69 (symbol-name mode) "\n")))
70 (insert header input)
71 (tildify-region (point-min) (point-max) t)
72 (should (string-equal (concat header expected) (buffer-string)))))))
73
74 (ert-deftest tildify-test-html ()
75 "Tests tildification in an HTML document"
76 (let* ((sentence (tildify-test--example-sentence " "))
77 (with-nbsp (tildify-test--example-sentence " ")))
78 (tildify-test--test '(html-mode sgml-mode)
79 (tildify-test--example-html sentence sentence)
80 (tildify-test--example-html sentence with-nbsp))))
81
82 (ert-deftest tildify-test-xml ()
83 "Tests tildification in an XML document"
84 (let* ((sentence (tildify-test--example-sentence " "))
85 (with-nbsp (tildify-test--example-sentence " ")))
86 (tildify-test--test '(nxml-mode)
87 (tildify-test--example-html sentence sentence t)
88 (tildify-test--example-html sentence with-nbsp t))))
89
90
91 (defun tildify-test--example-tex (sentence &optional with-nbsp)
92 "Return an example (La)Tex code.
93 SENTENCE is placed where spaces should not be replaced with hard spaces, and
94 WITH-NBSP is placed where spaces should be replaced with hard spaces. If the
95 latter is missing, SENTENCE will be used in all placeholder positions."
96 (let ((with-nbsp (or with-nbsp sentence)))
97 (concat with-nbsp "\n"
98 "\\begin{verbatim}\n" sentence "\n\\end{verbatim}\n"
99 "\\verb#" sentence "#\n"
100 "$$" sentence "$$\n"
101 "$" sentence "$\n"
102 "\\[" sentence "\\]\n"
103 "\\v A % " sentence "\n"
104 with-nbsp "\n")))
105
106 (ert-deftest tildify-test-tex ()
107 "Tests tildification in a (La)TeX document"
108 (let* ((sentence (tildify-test--example-sentence " "))
109 (with-nbsp (tildify-test--example-sentence "~")))
110 (tildify-test--test '(tex-mode latex-mode plain-tex-mode)
111 (tildify-test--example-tex sentence sentence)
112 (tildify-test--example-tex sentence with-nbsp))))
113
114
115 (ert-deftest tildify-test-find-env-end-re-bug ()
116 "Tests generation of end-regex using mix of indexes and strings"
117 (with-temp-buffer
118 (insert "foo whatever end-foo")
119 (goto-char (point-min))
120 (should (string-equal "end-foo"
121 (tildify--find-env "foo\\|bar"
122 '(("foo\\|bar" . ("end-" 0))))))))
123
124
125 (ert-deftest tildify-test-find-env-group-index-bug ()
126 "Tests generation of match-string indexes"
127 (with-temp-buffer
128 (let ((pairs '(("start-\\(foo\\|bar\\)" . ("end-" 1))
129 ("open-\\(foo\\|bar\\)" . ("close-" 1))))
130 (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)"))
131 (insert "open-foo whatever close-foo")
132 (goto-char (point-min))
133 (should (string-equal "close-foo" (tildify--find-env beg-re pairs))))))
134
135
136 (defmacro with-test-foreach (expected &rest body)
137 "Helper macro for testing foreach functions.
138 BODY has access to pairs variable and called lambda."
139 (declare (indent 1))
140 (let ((got (make-symbol "got")))
141 `(with-temp-buffer
142 (insert "1 /- 2 -/ 3 V~ 4 ~ 5 /- 6 -/ 7")
143 (let* ((pairs '(("/-" . "-/") ("V\\(.\\)" . (1))))
144 (,got "")
145 (called (lambda (s e)
146 (setq ,got (concat ,got (buffer-substring s e))))))
147 (setq-local tildify-foreach-region-function
148 (apply-partially 'tildify-foreach-ignore-environments
149 pairs))
150 ,@body
151 (should (string-equal ,expected ,got))))))
152
153 (ert-deftest tildify-test-foreach-ignore-environments ()
154 "Basic test of `tildify-foreach-ignore-environments'"
155 (with-test-foreach "1 3 5 7"
156 (tildify-foreach-ignore-environments pairs called (point-min) (point-max))))
157
158
159 (ert-deftest tildify-test-foreach-ignore-environments-early-return ()
160 "Test whether `tildify-foreach-ignore-environments' returns early
161 The function must terminate as soon as callback returns nil."
162 (with-test-foreach "1 "
163 (tildify-foreach-ignore-environments
164 pairs (lambda (start end) (funcall called start end) nil)
165 (point-min) (point-max))))
166
167 (ert-deftest tildify-test-foreach-region ()
168 "Basic test of `tildify--foreach-region'"
169 (with-test-foreach "1 3 5 7"
170 (tildify--foreach-region called (point-min) (point-max))))
171
172 (ert-deftest tildify-test-foreach-region-early-return ()
173 "Test whether `tildify--foreach-ignore' returns early
174 The function must terminate as soon as callback returns nil."
175 (with-test-foreach "1 "
176 (tildify--foreach-region (lambda (start end) (funcall called start end) nil)
177 (point-min) (point-max))))
178
179 (ert-deftest tildify-test-foreach-region-limit-region ()
180 "Test whether `tildify--foreach-ignore' limits callback to given region"
181 (with-test-foreach "3 "
182 (tildify--foreach-region called
183 (+ (point-min) 10) (+ (point-min) 16))) ; start at "3" end past "4"
184 (with-test-foreach "3 5"
185 (tildify--foreach-region called
186 (+ (point-min) 10) (+ (point-min) 20)))) ; start at "3" end past "5"
187
188
189 (defun tildify-space-test--test (modes nbsp env-open &optional set-space-string)
190 (with-temp-buffer
191 (setq-local buffer-file-coding-system 'utf-8)
192 (dolist (mode modes)
193 (funcall mode)
194 (when set-space-string
195 (setq-local tildify-space-string nbsp))
196 (let ((header (concat "Testing `tildify-space' in "
197 (symbol-name mode) "\n")))
198 ;; Replace space with hard space.
199 (erase-buffer)
200 (insert header "Lorem v ")
201 (should (tildify-space))
202 (should (string-equal (concat header "Lorem v" nbsp) (buffer-string)))
203 ;; Inside and ignore environment, replacing does not happen.
204 (erase-buffer)
205 (insert header env-open "Lorem v ")
206 (should (not (tildify-space)))
207 (should (string-equal (concat header env-open "Lorem v ")
208 (buffer-string)))))))
209
210 (ert-deftest tildify-space-test-html ()
211 "Tests auto-tildification in an HTML document"
212 (tildify-space-test--test '(html-mode sgml-mode) " " "<pre>"))
213
214 (ert-deftest tildify-space-test-html-nbsp ()
215 "Tests auto-tildification in an HTML document"
216 (tildify-space-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>" t))
217
218 (ert-deftest tildify-space-test-xml ()
219 "Tests auto-tildification in an XML document"
220 (tildify-space-test--test '(nxml-mode) " " "<! -- "))
221
222 (ert-deftest tildify-space-test-tex ()
223 "Tests tildification in a TeX document"
224 (tildify-space-test--test '(tex-mode latex-mode plain-tex-mode)
225 "~" "\\verb# "))
226
227
228 (defun tildify-space-undo-test--test
229 (modes nbsp env-open &optional set-space-string)
230 (with-temp-buffer
231 (setq-local buffer-file-coding-system 'utf-8)
232 (dolist (mode modes)
233 (funcall mode)
234 (when set-space-string
235 (setq-local tildify-space-string nbsp))
236 (let ((header (concat "Testing double-space-undos in "
237 (symbol-name mode) "\n")))
238 (erase-buffer)
239 (insert header "Lorem v" nbsp " ")
240 (should (not (tildify-space)))
241 (should (string-equal (concat header "Lorem v ") (buffer-string)))))))
242
243 (ert-deftest tildify-space-undo-test-html ()
244 "Tests auto-tildification in an HTML document"
245 (tildify-space-undo-test--test '(html-mode sgml-mode) " " "<pre>"))
246
247 (ert-deftest tildify-space-undo-test-html-nbsp ()
248 "Tests auto-tildification in an HTML document"
249 (tildify-space-undo-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>" t))
250
251 (ert-deftest tildify-space-undo-test-xml ()
252 "Tests auto-tildification in an XML document"
253 (tildify-space-undo-test--test '(nxml-mode) " " "<! -- "))
254
255 (ert-deftest tildify-space-undo-test-tex ()
256 "Tests tildification in a TeX document"
257 (tildify-space-undo-test--test '(tex-mode latex-mode plain-tex-mode)
258 "~" "\\verb# "))
259
260
261
262 (provide 'tildify-tests)
263
264 ;;; tildify-tests.el ends here