Merge from emacs-24; up to 2014-05-29T17:16:00Z!dmantipov@yandex.ru
[bpt/emacs.git] / test / automated / tildify-tests.el
1 ;;; tildify-test.el --- ERT tests for teldify.el
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)
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 (let ((with-nbsp (or with-nbsp sentence)))
45 (concat "<p>" with-nbsp "</p>\n"
46 "<pre>" sentence "</pre>\n"
47 "<! -- " sentence " -- >\n"
48 "<p>" with-nbsp "</p>\n"
49 "<" sentence ">\n")))
50
51
52 (defun tildify-test--test (modes input expected)
53 "Test tildify running in MODES.
54 INPUT is the initial content of the buffer and EXPECTED is expected result
55 after `tildify-buffer' is run."
56 (dolist (mode modes)
57 (with-temp-buffer
58 (funcall mode)
59 (let ((header (concat "Testing `tildify-buffer' in "
60 (symbol-name mode) "\n")))
61 (insert header input)
62 (tildify-buffer t)
63 (should (string-equal (concat header expected) (buffer-string)))))
64 (with-temp-buffer
65 (funcall mode)
66 (let ((header (concat "Testing `tildify-region' in "
67 (symbol-name mode) "\n")))
68 (insert header input)
69 (tildify-region (point-min) (point-max) t)
70 (should (string-equal (concat header expected) (buffer-string)))))))
71
72 (ert-deftest tildify-test-html ()
73 "Tests tildification in an HTML document"
74 (let* ((sentence (tildify-test--example-sentence " "))
75 (with-nbsp (tildify-test--example-sentence "&nbsp;")))
76 (tildify-test--test '(html-mode sgml-mode)
77 (tildify-test--example-html sentence sentence)
78 (tildify-test--example-html sentence with-nbsp))))
79
80
81 (defun tildify-test--example-tex (sentence &optional with-nbsp)
82 "Return an example (La)Tex code.
83 SENTENCE is placed where spaces should not be replaced with hard spaces, and
84 WITH-NBSP is placed where spaces should be replaced with hard spaces. If the
85 latter is missing, SENTENCE will be used in all placeholder positions."
86 (let ((with-nbsp (or with-nbsp sentence)))
87 (concat with-nbsp "\n"
88 "\\begin{verbatim}\n" sentence "\n\\end{verbatim}\n"
89 "\\verb#" sentence "#\n"
90 "$$" sentence "$$\n"
91 "$" sentence "$\n"
92 "\\[" sentence "\\]\n"
93 "\\v A % " sentence "\n"
94 with-nbsp "\n")))
95
96 (ert-deftest tildify-test-tex ()
97 "Tests tildification in a (La)TeX document"
98 (let* ((sentence (tildify-test--example-sentence " "))
99 (with-nbsp (tildify-test--example-sentence "~")))
100 (tildify-test--test '(tex-mode latex-mode plain-tex-mode)
101 (tildify-test--example-tex sentence sentence)
102 (tildify-test--example-tex sentence with-nbsp))))
103
104 (provide 'tildify-tests)
105
106 ;;; tildify-tests.el ends here