tildify-tests.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ;;; tildify-test.el --- ERT tests for tildify.el -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2014-2017 Free Software Foundation, Inc.
  3. ;; Author: Michal Nazarewicz <mina86@mina86.com>
  4. ;; Version: 4.5
  5. ;; Keywords: text, TeX, SGML, wp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This package defines regression tests for the tildify package.
  19. ;;; Code:
  20. (require 'ert)
  21. (require 'tildify)
  22. (defun tildify-test--example-sentence (space)
  23. "Return an example sentence with SPACE where hard space is required."
  24. (concat "Lorem ipsum v" space "dolor sit amet, a" space
  25. "consectetur adipiscing elit."))
  26. (defun tildify-test--example-html (sentence &optional with-nbsp is-xml)
  27. "Return an example HTML code.
  28. SENTENCE is placed where spaces should not be replaced with hard spaces, and
  29. WITH-NBSP is placed where spaces should be replaced with hard spaces. If the
  30. latter is missing, SENTENCE will be used in all placeholder positions.
  31. If IS-XML is non-nil, <pre> tag is not treated specially."
  32. (let ((with-nbsp (or with-nbsp sentence)))
  33. (concat "<p>" with-nbsp "</p>\n"
  34. "<pre>" (if is-xml with-nbsp sentence) "</pre>\n"
  35. "<! -- " sentence " -- >\n"
  36. "<p>" with-nbsp "</p>\n"
  37. "<" sentence ">\n")))
  38. (defun tildify-test--test (modes input expected)
  39. "Test tildify running in MODES.
  40. INPUT is the initial content of the buffer and EXPECTED is expected result
  41. after `tildify-buffer' is run."
  42. (with-temp-buffer
  43. (setq-local buffer-file-coding-system 'utf-8)
  44. (dolist (mode modes)
  45. (erase-buffer)
  46. (funcall mode)
  47. (let ((header (concat "Testing `tildify-buffer' in "
  48. (symbol-name mode) "\n")))
  49. (insert header input)
  50. (tildify-buffer t)
  51. (should (string-equal (concat header expected) (buffer-string))))
  52. (erase-buffer)
  53. (let ((header (concat "Testing `tildify-region' in "
  54. (symbol-name mode) "\n")))
  55. (insert header input)
  56. (tildify-region (point-min) (point-max) t)
  57. (should (string-equal (concat header expected) (buffer-string)))))))
  58. (ert-deftest tildify-test-html ()
  59. "Tests tildification in an HTML document"
  60. (let* ((sentence (tildify-test--example-sentence " "))
  61. (with-nbsp (tildify-test--example-sentence " ")))
  62. (tildify-test--test '(html-mode sgml-mode)
  63. (tildify-test--example-html sentence sentence)
  64. (tildify-test--example-html sentence with-nbsp))))
  65. (ert-deftest tildify-test-xml ()
  66. "Tests tildification in an XML document"
  67. (let* ((sentence (tildify-test--example-sentence " "))
  68. (with-nbsp (tildify-test--example-sentence " ")))
  69. (tildify-test--test '(nxml-mode)
  70. (tildify-test--example-html sentence sentence t)
  71. (tildify-test--example-html sentence with-nbsp t))))
  72. (defun tildify-test--example-tex (sentence &optional with-nbsp)
  73. "Return an example (La)Tex code.
  74. SENTENCE is placed where spaces should not be replaced with hard spaces, and
  75. WITH-NBSP is placed where spaces should be replaced with hard spaces. If the
  76. latter is missing, SENTENCE will be used in all placeholder positions."
  77. (let ((with-nbsp (or with-nbsp sentence)))
  78. (concat with-nbsp "\n"
  79. "\\begin{verbatim}\n" sentence "\n\\end{verbatim}\n"
  80. "\\verb#" sentence "#\n"
  81. "$$" sentence "$$\n"
  82. "$" sentence "$\n"
  83. "\\[" sentence "\\]\n"
  84. "\\v A % " sentence "\n"
  85. with-nbsp "\n")))
  86. (ert-deftest tildify-test-tex ()
  87. "Tests tildification in a (La)TeX document"
  88. (let* ((sentence (tildify-test--example-sentence " "))
  89. (with-nbsp (tildify-test--example-sentence "~")))
  90. (tildify-test--test '(tex-mode latex-mode plain-tex-mode)
  91. (tildify-test--example-tex sentence sentence)
  92. (tildify-test--example-tex sentence with-nbsp))))
  93. (ert-deftest tildify-test-find-env-end-re-bug ()
  94. "Tests generation of end-regex using mix of indexes and strings"
  95. (with-temp-buffer
  96. (insert "foo whatever end-foo")
  97. (goto-char (point-min))
  98. (should (string-equal "end-foo"
  99. (tildify--find-env "foo\\|bar"
  100. '(("foo\\|bar" . ("end-" 0))))))))
  101. (ert-deftest tildify-test-find-env-group-index-bug ()
  102. "Tests generation of match-string indexes"
  103. (with-temp-buffer
  104. (let ((pairs '(("start-\\(foo\\|bar\\)" . ("end-" 1))
  105. ("open-\\(foo\\|bar\\)" . ("close-" 1))))
  106. (beg-re "start-\\(foo\\|bar\\)\\|open-\\(foo\\|bar\\)"))
  107. (insert "open-foo whatever close-foo")
  108. (goto-char (point-min))
  109. (should (string-equal "close-foo" (tildify--find-env beg-re pairs))))))
  110. (defmacro with-test-foreach (expected &rest body)
  111. "Helper macro for testing foreach functions.
  112. BODY has access to pairs variable and called lambda."
  113. (declare (indent 1))
  114. (let ((got (make-symbol "got")))
  115. `(with-temp-buffer
  116. (insert "1 /- 2 -/ 3 V~ 4 ~ 5 /- 6 -/ 7")
  117. (let* ((pairs '(("/-" . "-/") ("V\\(.\\)" . (1))))
  118. (,got "")
  119. (called (lambda (s e)
  120. (setq ,got (concat ,got (buffer-substring s e))))))
  121. (setq-local tildify-foreach-region-function
  122. (apply-partially 'tildify-foreach-ignore-environments
  123. pairs))
  124. ,@body
  125. (should (string-equal ,expected ,got))))))
  126. (ert-deftest tildify-test-foreach-ignore-environments ()
  127. "Basic test of `tildify-foreach-ignore-environments'"
  128. (with-test-foreach "1 3 5 7"
  129. (tildify-foreach-ignore-environments pairs called (point-min) (point-max))))
  130. (ert-deftest tildify-test-foreach-ignore-environments-early-return ()
  131. "Test whether `tildify-foreach-ignore-environments' returns early
  132. The function must terminate as soon as callback returns nil."
  133. (with-test-foreach "1 "
  134. (tildify-foreach-ignore-environments
  135. pairs (lambda (start end) (funcall called start end) nil)
  136. (point-min) (point-max))))
  137. (ert-deftest tildify-test-foreach-region ()
  138. "Basic test of `tildify--foreach-region'"
  139. (with-test-foreach "1 3 5 7"
  140. (tildify--foreach-region called (point-min) (point-max))))
  141. (ert-deftest tildify-test-foreach-region-early-return ()
  142. "Test whether `tildify--foreach-ignore' returns early
  143. The function must terminate as soon as callback returns nil."
  144. (with-test-foreach "1 "
  145. (tildify--foreach-region (lambda (start end) (funcall called start end) nil)
  146. (point-min) (point-max))))
  147. (ert-deftest tildify-test-foreach-region-limit-region ()
  148. "Test whether `tildify--foreach-ignore' limits callback to given region"
  149. (with-test-foreach "3 "
  150. (tildify--foreach-region called
  151. (+ (point-min) 10) (+ (point-min) 16))) ; start at "3" end past "4"
  152. (with-test-foreach "3 5"
  153. (tildify--foreach-region called
  154. (+ (point-min) 10) (+ (point-min) 20)))) ; start at "3" end past "5"
  155. (defun tildify-space-test--test (modes nbsp env-open &optional set-space-string)
  156. (with-temp-buffer
  157. (setq-local buffer-file-coding-system 'utf-8)
  158. (dolist (mode modes)
  159. (funcall mode)
  160. (when set-space-string
  161. (setq-local tildify-space-string nbsp))
  162. (let ((header (concat "Testing `tildify-space' in "
  163. (symbol-name mode) "\n")))
  164. ;; Replace space with hard space.
  165. (erase-buffer)
  166. (insert header "Lorem v ")
  167. (should (tildify-space))
  168. (should (string-equal (concat header "Lorem v" nbsp) (buffer-string)))
  169. ;; Inside and ignore environment, replacing does not happen.
  170. (erase-buffer)
  171. (insert header env-open "Lorem v ")
  172. (should (not (tildify-space)))
  173. (should (string-equal (concat header env-open "Lorem v ")
  174. (buffer-string)))))))
  175. (ert-deftest tildify-space-test-html ()
  176. "Tests auto-tildification in an HTML document"
  177. (tildify-space-test--test '(html-mode sgml-mode) " " "<pre>"))
  178. (ert-deftest tildify-space-test-html-nbsp ()
  179. "Tests auto-tildification in an HTML document"
  180. (tildify-space-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>" t))
  181. (ert-deftest tildify-space-test-xml ()
  182. "Tests auto-tildification in an XML document"
  183. (tildify-space-test--test '(nxml-mode) " " "<! -- "))
  184. (ert-deftest tildify-space-test-tex ()
  185. "Tests tildification in a TeX document"
  186. (tildify-space-test--test '(tex-mode latex-mode plain-tex-mode)
  187. "~" "\\verb# "))
  188. (defun tildify-space-undo-test--test
  189. (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 double-space-undos in "
  197. (symbol-name mode) "\n")))
  198. (erase-buffer)
  199. (insert header "Lorem v" nbsp " ")
  200. (should (not (tildify-space)))
  201. (should (string-equal (concat header "Lorem v ") (buffer-string)))))))
  202. (ert-deftest tildify-space-undo-test-html ()
  203. "Tests auto-tildification in an HTML document"
  204. (tildify-space-undo-test--test '(html-mode sgml-mode) " " "<pre>"))
  205. (ert-deftest tildify-space-undo-test-html-nbsp ()
  206. "Tests auto-tildification in an HTML document"
  207. (tildify-space-undo-test--test '(html-mode sgml-mode) "&nbsp;" "<pre>" t))
  208. (ert-deftest tildify-space-undo-test-xml ()
  209. "Tests auto-tildification in an XML document"
  210. (tildify-space-undo-test--test '(nxml-mode) " " "<! -- "))
  211. (ert-deftest tildify-space-undo-test-tex ()
  212. "Tests tildification in a TeX document"
  213. (tildify-space-undo-test--test '(tex-mode latex-mode plain-tex-mode)
  214. "~" "\\verb# "))
  215. (provide 'tildify-tests)
  216. ;;; tildify-tests.el ends here