Remove call to string-to-multibyte from nndoc.el
[emacs.git] / test / lisp / abbrev-tests.el
blobfacf097815e574725863cb1e09cdec4f3179c65b
1 ;;; abbrev-tests.el --- Test suite for abbrevs -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015-2018 Free Software Foundation, Inc.
5 ;; Author: Eli Zaretskii <eliz@gnu.org>
6 ;; Keywords: abbrevs
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; `kill-all-abbrevs-test' will remove all user *and* system abbrevs
26 ;; if called noninteractively with the init file loaded.
28 ;;; Code:
30 (require 'ert)
31 (require 'abbrev)
32 (require 'seq)
34 ;; set up test abbrev table and abbrev entry
35 (defun setup-test-abbrev-table ()
36 (defvar ert-test-abbrevs nil)
37 (define-abbrev-table 'ert-test-abbrevs '(("a-e-t" "abbrev-ert-test")))
38 (abbrev-table-put ert-test-abbrevs :ert-test "ert-test-value")
39 ert-test-abbrevs)
41 (defun setup-test-abbrev-table-with-props ()
42 (defvar ert-test-abbrevs nil)
43 (define-abbrev-table 'ert-test-abbrevs '(("fb" "fooBar" nil :case-fixed t)))
44 (abbrev-table-put ert-test-abbrevs :ert-test "ert-test-value")
45 ert-test-abbrevs)
47 (ert-deftest abbrev-table-p-test ()
48 (should-not (abbrev-table-p 42))
49 (should-not (abbrev-table-p "aoeu"))
50 (should-not (abbrev-table-p '()))
51 (should-not (abbrev-table-p []))
52 ;; Missing :abbrev-table-modiff counter:
53 (should-not (abbrev-table-p (obarray-make)))
54 (should (abbrev-table-empty-p (make-abbrev-table))))
56 (ert-deftest abbrev-make-abbrev-table-test ()
57 ;; Table without properties:
58 (let ((table (make-abbrev-table)))
59 (should (abbrev-table-p table))
60 (should (= (length table) obarray-default-size)))
61 ;; Table with one property 'foo with value 'bar:
62 (let ((table (make-abbrev-table '(foo bar))))
63 (should (abbrev-table-p table))
64 (should (= (length table) obarray-default-size))
65 (should (eq (abbrev-table-get table 'foo) 'bar))))
67 (ert-deftest abbrev-table-get-put-test ()
68 (let ((table (make-abbrev-table)))
69 (should-not (abbrev-table-get table 'foo))
70 (should (= (abbrev-table-put table 'foo 42) 42))
71 (should (= (abbrev-table-get table 'foo) 42))
72 (should (eq (abbrev-table-put table 'foo 'bar) 'bar))
73 (should (eq (abbrev-table-get table 'foo) 'bar))))
75 (ert-deftest copy-abbrev-table-test ()
76 (defvar foo-abbrev-table nil) ; Avoid compiler warning
77 (define-abbrev-table 'foo-abbrev-table
78 '())
79 (should (abbrev-table-p foo-abbrev-table))
80 ;; Bug 21828
81 (let ((new-foo-abbrev-table
82 (condition-case nil
83 (copy-abbrev-table foo-abbrev-table)
84 (error nil))))
85 (should (abbrev-table-p new-foo-abbrev-table)))
86 (should-not (string-equal (buffer-name) "*Backtrace*")))
88 (ert-deftest abbrev-table-empty-p-test ()
89 (should-error (abbrev-table-empty-p 42))
90 (should-error (abbrev-table-empty-p "aoeu"))
91 (should-error (abbrev-table-empty-p '()))
92 (should-error (abbrev-table-empty-p []))
93 ;; Missing :abbrev-table-modiff counter:
94 (should-error (abbrev-table-empty-p (obarray-make)))
95 (let* ((table (obarray-make)))
96 (abbrev-table-put table :abbrev-table-modiff 42)
97 (should (abbrev-table-empty-p table))))
99 (ert-deftest kill-all-abbrevs-test ()
100 "Test undefining all defined abbrevs"
101 (unless noninteractive
102 (ert-skip "Cannot test kill-all-abbrevs in interactive mode"))
104 (let ((num-tables 0))
105 ;; ensure at least one abbrev exists
106 (should (abbrev-table-p (setup-test-abbrev-table)))
107 (setf num-tables (length abbrev-table-name-list))
108 (kill-all-abbrevs)
110 ;; no tables should have been removed/added
111 (should (= num-tables (length abbrev-table-name-list)))
112 ;; number of empty tables should be the same as number of tables
113 (should (= num-tables (length (seq-filter
114 (lambda (table)
115 (abbrev-table-empty-p (symbol-value table)))
116 abbrev-table-name-list))))))
118 (ert-deftest abbrev-table-name-test ()
119 "Test returning name of abbrev-table"
120 (let ((ert-test-abbrevs (setup-test-abbrev-table))
121 (no-such-table nil))
122 (should (equal 'ert-test-abbrevs (abbrev-table-name ert-test-abbrevs)))
123 (should (equal nil (abbrev-table-name no-such-table)))))
125 (ert-deftest clear-abbrev-table-test ()
126 "Test clearing single abbrev table"
127 (let ((ert-test-abbrevs (setup-test-abbrev-table)))
128 (should (equal "abbrev-ert-test" (abbrev-expansion "a-e-t" ert-test-abbrevs)))
129 (clear-abbrev-table ert-test-abbrevs)
130 (should (equal nil (abbrev-expansion "a-e-t" ert-test-abbrevs)))
131 (should (equal t (abbrev-table-empty-p ert-test-abbrevs)))))
133 (ert-deftest list-abbrevs-test ()
134 "Test generation of abbrev list buffer"
135 ;; Somewhat redundant as prepare-abbrev-list-buffer is also tested.
136 ;; all abbrevs
137 (let ((abbrev-buffer (prepare-abbrev-list-buffer)))
138 (should (equal "*Abbrevs*" (buffer-name abbrev-buffer)))
139 (kill-buffer abbrev-buffer))
140 ;; mode-specific abbrevs
141 (let ((abbrev-buffer (prepare-abbrev-list-buffer t)))
142 (should (equal "*Abbrevs*" (buffer-name abbrev-buffer)))
143 (kill-buffer abbrev-buffer)))
145 (ert-deftest prepare-abbrev-list-buffer-test ()
146 "Test generation of abbrev list buffer"
147 ;; all abbrevs
148 (let ((ert-test-abbrevs (setup-test-abbrev-table)))
149 (with-current-buffer (prepare-abbrev-list-buffer)
150 ;; Check for a couple of abbrev-table names in buffer.
151 (should (and (progn
152 (goto-char (point-min))
153 (search-forward (symbol-name (abbrev-table-name ert-test-abbrevs))))
154 (progn
155 (goto-char (point-min))
156 (search-forward "global-abbrev-table"))))
157 (should (equal 'edit-abbrevs-mode major-mode))
158 (kill-buffer "*Abbrevs*")))
160 ;; mode-specific abbrevs (temp buffer uses fundamental-mode)
161 (with-temp-buffer
162 (prepare-abbrev-list-buffer t)
163 (with-current-buffer "*Abbrevs*"
164 (should (progn
165 (goto-char (point-min))
166 (search-forward "fundamental-mode-abbrev-table")))
167 (should-error (progn
168 (goto-char (point-min))
169 (search-forward "global-abbrev-table")))
170 (should-not (equal 'edit-abbrevs-mode major-mode))
171 (kill-buffer "*Abbrevs*"))))
173 (ert-deftest insert-abbrevs-test ()
174 "Test inserting abbrev definitions into buffer"
175 (with-temp-buffer
176 (insert-abbrevs)
177 (should (progn
178 (goto-char (point-min))
179 (search-forward "global-abbrev-table")))))
181 (ert-deftest edit-abbrevs-test ()
182 "Test editing abbrevs from buffer"
183 (defvar ert-edit-abbrevs-test-table nil)
184 (let ((ert-test-abbrevs (setup-test-abbrev-table)))
185 (with-temp-buffer
186 ;; insert test table and new abbrev, redefine, check definition
187 (goto-char (point-min))
188 (insert "(ert-edit-abbrevs-test-table)\n")
189 (insert "\n" "\"e-a-t\"\t" "0\t" "\"edit-abbrevs-test\"\n")
190 ;; check test table before redefine
191 (should (equal "abbrev-ert-test"
192 (abbrev-expansion "a-e-t" ert-test-abbrevs)))
193 (edit-abbrevs-redefine)
194 (should-not (abbrev-expansion "a-e-t" ert-test-abbrevs))
195 (should (equal "edit-abbrevs-test"
196 (abbrev-expansion "e-a-t" ert-edit-abbrevs-test-table))))))
198 (ert-deftest define-abbrevs-test ()
199 "Test defining abbrevs from buffer"
200 (defvar ert-bad-abbrev-table nil)
201 (defvar ert-good-abbrev-table nil)
202 (defvar ert-redefine-abbrev-table nil)
203 (with-temp-buffer
204 ;; insert bad abbrev data and attempt define
205 (goto-char (point-min))
206 (insert "ert-bad-abbrev-table\n")
207 (insert "\n" "\"b-a-t\"\t" "0\t" "\n")
208 (should-not (define-abbrevs))
209 (should (equal nil (abbrev-expansion "b-a-t" ert-bad-abbrev-table)))
210 (delete-region (point-min) (point-max))
211 ;; try with valid abbrev data
212 (goto-char (point-min))
213 (insert "(ert-good-abbrev-table)\n")
214 (insert "\n" "\"g-a-t\"\t" "0\t" "\"good-abbrev-table\"\n")
215 (define-abbrevs)
216 (should (equal "good-abbrev-table"
217 (abbrev-expansion "g-a-t" ert-good-abbrev-table)))
218 ;; redefine from buffer
219 (delete-region (point-min) (point-max))
220 (insert "(ert-redefine-abbrev-table)\n")
221 (insert "\n" "\"r-a-t\"\t" "0\t" "\"redefine-abbrev-table\"\n")
222 ;; arg = kill-all-abbrevs
223 (define-abbrevs t)
224 (should (equal "redefine-abbrev-table"
225 (abbrev-expansion "r-a-t" ert-redefine-abbrev-table)))
226 (should (equal nil (abbrev-expansion "g-a-t" ert-good-abbrev-table)))))
228 (ert-deftest read-write-abbrev-file-test ()
229 "Test reading and writing abbrevs from file"
230 (let ((temp-test-file (make-temp-file "ert-abbrev-test"))
231 (ert-test-abbrevs (setup-test-abbrev-table)))
232 (write-abbrev-file temp-test-file)
233 (clear-abbrev-table ert-test-abbrevs)
234 (should (abbrev-table-empty-p ert-test-abbrevs))
235 (read-abbrev-file temp-test-file)
236 (should (equal "abbrev-ert-test" (abbrev-expansion "a-e-t" ert-test-abbrevs)))
237 (delete-file temp-test-file)))
239 (ert-deftest read-write-abbrev-file-test-with-props ()
240 "Test reading and writing abbrevs from file"
241 (let ((temp-test-file (make-temp-file "ert-abbrev-test"))
242 (ert-test-abbrevs (setup-test-abbrev-table-with-props)))
243 (write-abbrev-file temp-test-file)
244 (clear-abbrev-table ert-test-abbrevs)
245 (should (abbrev-table-empty-p ert-test-abbrevs))
246 (read-abbrev-file temp-test-file)
247 (should (equal "fooBar" (abbrev-expansion "fb" ert-test-abbrevs)))
248 (delete-file temp-test-file)))
250 (ert-deftest abbrev-edit-save-to-file-test ()
251 "Test saving abbrev definitions in buffer to file"
252 (defvar ert-save-test-table nil)
253 (let ((temp-test-file (make-temp-file "ert-abbrev-test"))
254 (ert-test-abbrevs (setup-test-abbrev-table)))
255 (with-temp-buffer
256 (goto-char (point-min))
257 (insert "(ert-save-test-table)\n")
258 (insert "\n" "\"s-a-t\"\t" "0\t" "\"save-abbrevs-test\"\n")
259 (should (equal "abbrev-ert-test"
260 (abbrev-expansion "a-e-t" ert-test-abbrevs)))
261 ;; clears abbrev tables
262 (abbrev-edit-save-to-file temp-test-file)
263 (should-not (abbrev-expansion "a-e-t" ert-test-abbrevs))
264 (read-abbrev-file temp-test-file)
265 (should (equal "save-abbrevs-test"
266 (abbrev-expansion "s-a-t" ert-save-test-table)))
267 (delete-file temp-test-file))))
269 (provide 'abbrev-tests)
271 ;;; abbrev-tests.el ends here