]> code.delx.au - gnu-emacs/blob - test/automated/tildify-tests.el
55d2d576721a3a47cfe71aa3cd3a67954926e643
[gnu-emacs] / test / automated / tildify-tests.el
1 ;;; tildify-test.el --- ERT tests for tildify.el -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014 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 (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)))))))
72
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))))
80
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))))
88
89
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")))
104
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))))
112
113
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))))))))
122
123
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))))))
133
134
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))))))
151
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))))
156
157
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))))
165
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))))
170
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))))
177
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"
186
187
188 (provide 'tildify-tests)
189
190 ;;; tildify-tests.el ends here