Don't reimplement move-if-change badly
[emacs.git] / test / automated / tildify-tests.el
blobb53f58c279e01fbda2ba10a6664c67b483acceb8
1 ;;; tildify-test.el --- ERT tests for tildify.el -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Michal Nazarewicz <mina86@mina86.com>
6 ;; Version: 4.5
7 ;; Keywords: text, TeX, SGML, wp
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 ;;; Commentary:
26 ;; This package defines regression tests for the tildify package.
28 ;;; Code:
30 (require 'ert)
31 (require 'tildify)
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."))
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")))
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 (dolist (mode modes)
59 (erase-buffer)
60 (funcall mode)
61 (let ((header (concat "Testing `tildify-buffer' in "
62 (symbol-name mode) "\n")))
63 (insert header input)
64 (tildify-buffer t)
65 (should (string-equal (concat header expected) (buffer-string))))
66 (erase-buffer)
67 (let ((header (concat "Testing `tildify-region' in "
68 (symbol-name mode) "\n")))
69 (insert header input)
70 (tildify-region (point-min) (point-max) t)
71 (should (string-equal (concat header expected) (buffer-string)))))))
73 (ert-deftest tildify-test-html ()
74 "Tests tildification in an HTML document"
75 (let* ((sentence (tildify-test--example-sentence " "))
76 (with-nbsp (tildify-test--example-sentence " ")))
77 (tildify-test--test '(html-mode sgml-mode)
78 (tildify-test--example-html sentence sentence)
79 (tildify-test--example-html sentence with-nbsp))))
81 (ert-deftest tildify-test-xml ()
82 "Tests tildification in an XML document"
83 (let* ((sentence (tildify-test--example-sentence " "))
84 (with-nbsp (tildify-test--example-sentence " ")))
85 (tildify-test--test '(nxml-mode)
86 (tildify-test--example-html sentence sentence t)
87 (tildify-test--example-html sentence with-nbsp t))))
90 (defun tildify-test--example-tex (sentence &optional with-nbsp)
91 "Return an example (La)Tex code.
92 SENTENCE is placed where spaces should not be replaced with hard spaces, and
93 WITH-NBSP is placed where spaces should be replaced with hard spaces. If the
94 latter is missing, SENTENCE will be used in all placeholder positions."
95 (let ((with-nbsp (or with-nbsp sentence)))
96 (concat with-nbsp "\n"
97 "\\begin{verbatim}\n" sentence "\n\\end{verbatim}\n"
98 "\\verb#" sentence "#\n"
99 "$$" sentence "$$\n"
100 "$" sentence "$\n"
101 "\\[" sentence "\\]\n"
102 "\\v A % " sentence "\n"
103 with-nbsp "\n")))
105 (ert-deftest tildify-test-tex ()
106 "Tests tildification in a (La)TeX document"
107 (let* ((sentence (tildify-test--example-sentence " "))
108 (with-nbsp (tildify-test--example-sentence "~")))
109 (tildify-test--test '(tex-mode latex-mode plain-tex-mode)
110 (tildify-test--example-tex sentence sentence)
111 (tildify-test--example-tex sentence with-nbsp))))
114 (ert-deftest tildify-test-find-env-end-re-bug ()
115 "Tests generation of end-regex using mix of indexes and strings"
116 (with-temp-buffer
117 (insert "foo whatever end-foo")
118 (goto-char (point-min))
119 (should (string-equal "end-foo"
120 (tildify--find-env "foo\\|bar"
121 '(("foo\\|bar" . ("end-" 0))))))))
124 (ert-deftest tildify-test-find-env-group-index-bug ()
125 "Tests generation of match-string indexes"
126 (with-temp-buffer
127 (let ((pairs '(("start-\\(foo\\|bar\\)" . ("end-" 1))
128 ("open-\\(foo\\|bar\\)" . ("close-" 1))))
129 (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)"))
130 (insert "open-foo whatever close-foo")
131 (goto-char (point-min))
132 (should (string-equal "close-foo" (tildify--find-env beg-re pairs))))))
135 (defmacro with-test-foreach (expected &rest body)
136 "Helper macro for testing foreach functions.
137 BODY has access to pairs variable and called lambda."
138 (declare (indent 1))
139 (let ((got (make-symbol "got")))
140 `(with-temp-buffer
141 (insert "1 /- 2 -/ 3 V~ 4 ~ 5 /- 6 -/ 7")
142 (let* ((pairs '(("/-" . "-/") ("V\\(.\\)" . (1))))
143 (,got "")
144 (called (lambda (s e)
145 (setq ,got (concat ,got (buffer-substring s e))))))
146 (setq-local tildify-foreach-region-function
147 (apply-partially 'tildify-foreach-ignore-environments
148 pairs))
149 ,@body
150 (should (string-equal ,expected ,got))))))
152 (ert-deftest tildify-test-foreach-ignore-environments ()
153 "Basic test of `tildify-foreach-ignore-environments'"
154 (with-test-foreach "1 3 5 7"
155 (tildify-foreach-ignore-environments pairs called (point-min) (point-max))))
158 (ert-deftest tildify-test-foreach-ignore-environments-early-return ()
159 "Test whether `tildify-foreach-ignore-environments' returns early
160 The function must terminate as soon as callback returns nil."
161 (with-test-foreach "1 "
162 (tildify-foreach-ignore-environments
163 pairs (lambda (start end) (funcall called start end) nil)
164 (point-min) (point-max))))
166 (ert-deftest tildify-test-foreach-region ()
167 "Basic test of `tildify--foreach-region'"
168 (with-test-foreach "1 3 5 7"
169 (tildify--foreach-region called (point-min) (point-max))))
171 (ert-deftest tildify-test-foreach-region-early-return ()
172 "Test whether `tildify--foreach-ignore' returns early
173 The function must terminate as soon as callback returns nil."
174 (with-test-foreach "1 "
175 (tildify--foreach-region (lambda (start end) (funcall called start end) nil)
176 (point-min) (point-max))))
178 (ert-deftest tildify-test-foreach-region-limit-region ()
179 "Test whether `tildify--foreach-ignore' limits callback to given region"
180 (with-test-foreach "3 "
181 (tildify--foreach-region called
182 (+ (point-min) 10) (+ (point-min) 16))) ; start at "3" end past "4"
183 (with-test-foreach "3 5"
184 (tildify--foreach-region called
185 (+ (point-min) 10) (+ (point-min) 20)))) ; start at "3" end past "5"
188 (defun tildify-space-test--test (modes nbsp env-open &optional set-space-string)
189 (with-temp-buffer
190 (dolist (mode modes)
191 (funcall mode)
192 (when set-space-string
193 (setq-local tildify-space-string nbsp))
194 (let ((header (concat "Testing `tildify-space' in "
195 (symbol-name mode) "\n")))
196 ;; Replace space with hard space.
197 (erase-buffer)
198 (insert header "Lorem v ")
199 (should (tildify-space))
200 (should (string-equal (concat header "Lorem v" nbsp) (buffer-string)))
201 ;; Inside and ignore environment, replacing does not happen.
202 (erase-buffer)
203 (insert header env-open "Lorem v ")
204 (should (not (tildify-space)))
205 (should (string-equal (concat header env-open "Lorem v ")
206 (buffer-string)))))))
208 (ert-deftest tildify-space-test-html ()
209 "Tests auto-tildification in an HTML document"
210 (tildify-space-test--test '(html-mode sgml-mode) " " "<pre>"))
212 (ert-deftest tildify-space-test-html-nbsp ()
213 "Tests auto-tildification in an HTML document"
214 (tildify-space-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>" t))
216 (ert-deftest tildify-space-test-xml ()
217 "Tests auto-tildification in an XML document"
218 (tildify-space-test--test '(nxml-mode) " " "<! -- "))
220 (ert-deftest tildify-space-test-tex ()
221 "Tests tildification in a TeX document"
222 (tildify-space-test--test '(tex-mode latex-mode plain-tex-mode)
223 "~" "\\verb# "))
226 (defun tildify-space-undo-test--test
227 (modes nbsp env-open &optional set-space-string)
228 (with-temp-buffer
229 (dolist (mode modes)
230 (funcall mode)
231 (when set-space-string
232 (setq-local tildify-space-string nbsp))
233 (let ((header (concat "Testing double-space-undos in "
234 (symbol-name mode) "\n")))
235 (erase-buffer)
236 (insert header "Lorem v" nbsp " ")
237 (should (not (tildify-space)))
238 (should (string-equal (concat header "Lorem v ") (buffer-string)))))))
240 (ert-deftest tildify-space-undo-test-html ()
241 "Tests auto-tildification in an HTML document"
242 (tildify-space-undo-test--test '(html-mode sgml-mode) " " "<pre>"))
244 (ert-deftest tildify-space-undo-test-html-nbsp ()
245 "Tests auto-tildification in an HTML document"
246 (tildify-space-undo-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>" t))
248 (ert-deftest tildify-space-undo-test-xml ()
249 "Tests auto-tildification in an XML document"
250 (tildify-space-undo-test--test '(nxml-mode) " " "<! -- "))
252 (ert-deftest tildify-space-undo-test-tex ()
253 "Tests tildification in a TeX document"
254 (tildify-space-undo-test--test '(tex-mode latex-mode plain-tex-mode)
255 "~" "\\verb# "))
259 (provide 'tildify-tests)
261 ;;; tildify-tests.el ends here