Allow inserting non-BMP characters
[emacs.git] / test / src / casefiddle-tests.el
blob5d8798c984f93bcc2d58fb58c517f434af59e6b5
1 ;;; casefiddle-tests.el --- tests for casefiddle.c functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 2015-2016, 2018 Free Software Foundation, Inc.
5 ;; 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.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
20 ;;; Code:
22 (require 'case-table)
23 (require 'ert)
25 (ert-deftest casefiddle-tests-char-properties ()
26 "Sanity check of character Unicode properties."
27 (let ((props '(uppercase lowercase titlecase
28 special-uppercase special-lowercase special-titlecase))
29 (tests '((?A nil ?a nil nil nil nil)
30 (?a ?A nil ?A nil nil nil)
31 (?Ł nil ?ł nil nil nil nil)
32 (?ł ?Ł nil ?Ł nil nil nil)
34 (?DŽ nil ?dž ?Dž nil nil nil)
35 (?Dž ?DŽ ?dž ?Dž nil nil nil)
36 (?dž ?DŽ nil ?Dž nil nil nil)
38 (?Σ nil ?σ nil nil nil nil)
39 (?σ ?Σ nil ?Σ nil nil nil)
40 (?ς ?Σ nil ?Σ nil nil nil)
42 (?ⅷ ?Ⅷ nil ?Ⅷ nil nil nil)
43 (?Ⅷ nil ?ⅷ nil nil nil nil)
45 (?fi nil nil nil "FI" nil "Fi")
46 (?ß nil nil nil "SS" nil "Ss")
47 (?İ nil ?i nil nil "i\u0307" nil)))
48 errors)
49 (dolist (test tests)
50 (let ((ch (car test))
51 (expected (cdr test)))
52 (dolist (prop props)
53 (let ((got (get-char-code-property ch prop)))
54 (unless (equal (car expected) got)
55 (push (format "\n%c %s; expected: %s but got: %s"
56 ch prop (car expected) got)
57 errors)))
58 (setq expected (cdr expected)))))
59 (when errors
60 (ert-fail (mapconcat (lambda (line) line) (nreverse errors) "")))))
63 (defconst casefiddle-tests--characters
64 ;; character uppercase lowercase titlecase
65 '((?A ?A ?a ?A)
66 (?a ?A ?a ?A)
67 (?Ł ?Ł ?ł ?Ł)
68 (?ł ?Ł ?ł ?Ł)
70 (?DŽ ?DŽ ?dž ?Dž)
71 (?Dž ?DŽ ?dž ?Dž)
72 (?dž ?DŽ ?dž ?Dž)
74 (?Σ ?Σ ?σ ?Σ)
75 (?σ ?Σ ?σ ?Σ)
76 (?ς ?Σ ?ς ?Σ)
78 (?Ⅷ ?Ⅷ ?ⅷ ?Ⅷ)
79 (?ⅷ ?Ⅷ ?ⅷ ?Ⅷ)))
82 (ert-deftest casefiddle-tests-case-table ()
83 "Sanity check of down and up case tables."
84 (should-not
85 (let (errors
86 (up (case-table-get-table (current-case-table) 'up))
87 (down (case-table-get-table (current-case-table) 'down)))
88 (dolist (test casefiddle-tests--characters)
89 (let ((ch (car test))
90 (expected (cdr test))
91 (props '(uppercase lowercase))
92 (tabs (list up down)))
93 (while props
94 (let ((got (aref (car tabs) ch)))
95 (unless (equal (car expected) got)
96 (push (format "\n%c %s; expected: %s but got: %s"
97 ch (car props) (car expected) got)
98 errors)))
99 (setq props (cdr props) tabs (cdr tabs) expected (cdr expected)))))
100 (when errors
101 (mapconcat (lambda (line) line) (nreverse errors) "")))))
104 (ert-deftest casefiddle-tests-casing-character ()
105 (should-not
106 (let (errors)
107 (dolist (test casefiddle-tests--characters)
108 (let ((ch (car test))
109 (expected (cdr test))
110 (funcs '(upcase downcase capitalize)))
111 (while funcs
112 (let ((got (funcall (car funcs) ch)))
113 (unless (equal (car expected) got)
114 (push (format "\n%c %s; expected: %s but got: %s"
115 ch (car funcs) (car expected) got)
116 errors)))
117 (setq funcs (cdr funcs) expected (cdr expected)))))
118 (when errors
119 (mapconcat (lambda (line) line) (nreverse errors) "")))))
122 (ert-deftest casefiddle-tests-casing-word ()
123 (with-temp-buffer
124 (dolist (test '((upcase-word . "FOO Bar")
125 (downcase-word . "foo Bar")
126 (capitalize-word . "Foo Bar")))
127 (dolist (back '(nil t))
128 (delete-region (point-min) (point-max))
129 (insert "foO Bar")
130 (goto-char (+ (if back 4 0) (point-min)))
131 (funcall (car test) (if back -1 1))
132 (should (string-equal (cdr test) (buffer-string)))
133 (should (equal (+ (if back 4 3) (point-min)) (point)))))))
136 (defun casefiddle-tests--test-casing (tests)
137 (nreverse
138 (cl-reduce
139 (lambda (errors test)
140 (let* ((input (car test))
141 (expected (cdr test))
142 (func-pairs '((upcase upcase-region)
143 (downcase downcase-region)
144 (capitalize capitalize-region)
145 (upcase-initials upcase-initials-region)))
146 (get-string (lambda (func) (funcall func input)))
147 (get-region (lambda (func)
148 (delete-region (point-min) (point-max))
149 (unwind-protect
150 (progn
151 (unless (multibyte-string-p input)
152 (toggle-enable-multibyte-characters))
153 (insert input)
154 (funcall func (point-min) (point-max))
155 (buffer-string))
156 (unless (multibyte-string-p input)
157 (toggle-enable-multibyte-characters)))))
158 (fmt-str (lambda (str)
159 (format "%s (%sbyte; %d chars; %d bytes)"
161 (if (multibyte-string-p str) "multi" "uni")
162 (length str) (string-bytes str))))
163 funcs getters)
164 (while (and func-pairs expected)
165 (setq funcs (car func-pairs)
166 getters (list get-string get-region))
167 (while (and funcs getters)
168 (let ((got (funcall (car getters) (car funcs))))
169 (unless (string-equal got (car expected))
170 (let ((fmt (length (symbol-name (car funcs)))))
171 (setq fmt (format "\n%%%ds: %%s" (max fmt 8)))
172 (push (format (concat fmt fmt fmt)
173 (car funcs) (funcall fmt-str input)
174 "expected" (funcall fmt-str (car expected))
175 "but got" (funcall fmt-str got))
176 errors))))
177 (setq funcs (cdr funcs) getters (cdr getters)))
178 (setq func-pairs (cdr func-pairs) expected (cdr expected))))
179 errors)
180 (cons () tests))))
182 (ert-deftest casefiddle-tests-casing ()
183 (should-not
184 (with-temp-buffer
185 (casefiddle-tests--test-casing
186 ;; input upper lower capitalize up-initials
187 '(("Foo baR" "FOO BAR" "foo bar" "Foo Bar" "Foo BaR")
188 ("Ⅷ ⅷ" "Ⅷ Ⅷ" "ⅷ ⅷ" "Ⅷ Ⅷ" "Ⅷ Ⅷ")
189 ;; "DžUNGLA" is an unfortunate result but it’s really best we can
190 ;; do while still being consistent. Hopefully, users only ever
191 ;; use upcase-initials on camelCase identifiers not real words.
192 ("DŽUNGLA" "DŽUNGLA" "džungla" "Džungla" "DžUNGLA")
193 ("Džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla")
194 ("džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla")
195 ("define" "DEFINE" "define" "Define" "Define")
196 ("fish" "FISH" "fish" "Fish" "Fish")
197 ("Straße" "STRASSE" "straße" "Straße" "Straße")
199 ;; The word repeated twice to test behaviour at the end of a word
200 ;; inside of an input string as well as at the end of the string.
201 ("ΌΣΟΣ ΌΣΟΣ" "ΌΣΟΣ ΌΣΟΣ" "όσος όσος" "Όσος Όσος" "ΌΣΟΣ ΌΣΟΣ")
202 ;; What should be done with sole sigma? It is ‘final’ but on the
203 ;; other hand it does not form a word. We’re using regular sigma.
204 ("Σ Σ" "Σ Σ" "σ σ" "Σ Σ" "Σ Σ")
205 ("όσος" "ΌΣΟΣ" "όσος" "Όσος" "Όσος")
206 ;; If sigma is already lower case, we don’t want to change it.
207 ("όσοσ" "ΌΣΟΣ" "όσοσ" "Όσοσ" "Όσοσ"))))))
209 (ert-deftest casefiddle-tests-casing-byte8 ()
210 (should-not
211 (with-temp-buffer
212 (casefiddle-tests--test-casing
213 '(("\xff Foo baR \xff"
214 "\xff FOO BAR \xff"
215 "\xff foo bar \xff"
216 "\xff Foo Bar \xff"
217 "\xff Foo BaR \xff")
218 ("\xff Zażółć gĘŚlą \xff"
219 "\xff ZAŻÓŁĆ GĘŚLĄ \xff"
220 "\xff zażółć gęślą \xff"
221 "\xff Zażółć Gęślą \xff"
222 "\xff Zażółć GĘŚlą \xff"))))))
224 (ert-deftest casefiddle-tests-casing-byte8-with-changes ()
225 (let ((tab (copy-case-table (standard-case-table)))
226 (test '("\xff\xff\xef Foo baR \xcf\xcf"
227 "\xef\xef\xef FOO BAR \xcf\xcf"
228 "\xff\xff\xff foo bar \xcf\xcf"
229 "\xef\xff\xff Foo Bar \xcf\xcf"
230 "\xef\xff\xef Foo BaR \xcf\xcf"))
231 (byte8 #x3FFF00))
232 (should-not
233 (with-temp-buffer
234 (set-case-table tab)
235 (set-case-syntax-pair (+ byte8 #xef) (+ byte8 #xff) tab)
236 (casefiddle-tests--test-casing
237 (list test
238 (mapcar (lambda (str) (decode-coding-string str 'binary)) test)
239 '("\xff\xff\xef Zażółć gĘŚlą \xcf\xcf"
240 "\xef\xef\xef ZAŻÓŁĆ GĘŚLĄ \xcf\xcf"
241 "\xff\xff\xff zażółć gęślą \xcf\xcf"
242 "\xef\xff\xff Zażółć Gęślą \xcf\xcf"
243 "\xef\xff\xef Zażółć GĘŚlą \xcf\xcf")))))))
246 (ert-deftest casefiddle-tests-char-casing ()
247 ;; input upcase downcase [titlecase]
248 (dolist (test '((?a ?A ?a) (?A ?A ?a)
249 (?ł ?Ł ?ł) (?Ł ?Ł ?ł)
250 (?ß ?ß ?ß) (?ẞ ?ẞ ?ß)
251 (?ⅷ ?Ⅷ ?ⅷ) (?Ⅷ ?Ⅷ ?ⅷ)
252 (?DŽ ?DŽ ?dž ?Dž) (?Dž ?DŽ ?dž ?Dž) (?dž ?DŽ ?dž ?Dž)))
253 (let ((ch (car test))
254 (up (nth 1 test))
255 (lo (nth 2 test))
256 (tc (or (nth 3 test) (nth 1 test))))
257 (should (eq up (upcase ch)))
258 (should (eq lo (downcase ch)))
259 (should (eq tc (capitalize ch)))
260 (should (eq tc (upcase-initials ch))))))
263 ;;; casefiddle-tests.el ends here