diff options
| author | Michal Nazarewicz | 2016-09-07 22:17:21 +0200 |
|---|---|---|
| committer | Michal Nazarewicz | 2017-02-15 16:54:06 +0100 |
| commit | 0d4290650d9ec635a657ed8537cfc960b41381b9 (patch) | |
| tree | ca1c0e71c0f6faeae2a23a6562d155b3d064f5e7 /test/src | |
| parent | aeeb86c99d8f25793393324c4e826a23b38b6c3c (diff) | |
| download | emacs-0d4290650d9ec635a657ed8537cfc960b41381b9.tar.gz emacs-0d4290650d9ec635a657ed8537cfc960b41381b9.zip | |
Add tests for casefiddle.c (bug#24603)
Fixes cases marked FIXME upcoming in followup commits.
* test/src/casefiddle-tests.el (casefiddle-tests-char-properties,
casefiddle-tests-case-table, casefiddle-tests-casing-character,
casefiddle-tests-casing, casefiddle-tests-casing-byte8,
casefiddle-tests-casing-byte8-with-changes): New tests.
(casefiddle-tests--test-casing): New helper function for runnig
some of the tests.
Diffstat (limited to 'test/src')
| -rw-r--r-- | test/src/casefiddle-tests.el | 247 |
1 files changed, 247 insertions, 0 deletions
diff --git a/test/src/casefiddle-tests.el b/test/src/casefiddle-tests.el new file mode 100644 index 00000000000..8d9cf34ee50 --- /dev/null +++ b/test/src/casefiddle-tests.el | |||
| @@ -0,0 +1,247 @@ | |||
| 1 | ;;; casefiddle-tests.el --- tests for casefiddle.c functions -*- lexical-binding: t -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2015-2016 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; This file is part of GNU Emacs. | ||
| 6 | |||
| 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 | |||
| 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. | ||
| 16 | |||
| 17 | ;; You should have received a copy of the GNU General Public License | ||
| 18 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | |||
| 20 | ;;; Code: | ||
| 21 | |||
| 22 | (require 'case-table) | ||
| 23 | (require 'ert) | ||
| 24 | |||
| 25 | (ert-deftest casefiddle-tests-char-properties () | ||
| 26 | "Sanity check of character Unicode properties." | ||
| 27 | (should-not | ||
| 28 | (let (errors) | ||
| 29 | ;; character uppercase lowercase titlecase | ||
| 30 | (dolist (test '((?A nil ?a nil) | ||
| 31 | (?a ?A nil ?A) | ||
| 32 | (?Ł nil ?ł nil) | ||
| 33 | (?ł ?Ł nil ?Ł) | ||
| 34 | |||
| 35 | (?DŽ nil ?dž ?Dž) | ||
| 36 | (?Dž ?DŽ ?dž ?Dž) | ||
| 37 | (?dž ?DŽ nil ?Dž) | ||
| 38 | |||
| 39 | (?Σ nil ?σ nil) | ||
| 40 | (?σ ?Σ nil ?Σ) | ||
| 41 | (?ς ?Σ nil ?Σ) | ||
| 42 | |||
| 43 | (?ⅷ ?Ⅷ nil ?Ⅷ) | ||
| 44 | (?Ⅷ nil ?ⅷ nil))) | ||
| 45 | (let ((ch (car test)) | ||
| 46 | (expected (cdr test)) | ||
| 47 | (props '(uppercase lowercase titlecase))) | ||
| 48 | (while props | ||
| 49 | (let ((got (get-char-code-property ch (car props)))) | ||
| 50 | (unless (equal (car expected) got) | ||
| 51 | (push (format "\n%c %s; expected: %s but got: %s" | ||
| 52 | ch (car props) (car expected) got) | ||
| 53 | errors))) | ||
| 54 | (setq props (cdr props) expected (cdr expected))))) | ||
| 55 | (when errors | ||
| 56 | (mapconcat (lambda (line) line) (nreverse errors) ""))))) | ||
| 57 | |||
| 58 | |||
| 59 | (defconst casefiddle-tests--characters | ||
| 60 | ;; character uppercase lowercase titlecase | ||
| 61 | '((?A ?A ?a ?A) | ||
| 62 | (?a ?A ?a ?A) | ||
| 63 | (?Ł ?Ł ?ł ?Ł) | ||
| 64 | (?ł ?Ł ?ł ?Ł) | ||
| 65 | |||
| 66 | ;; FIXME(bug#24603): We should have: | ||
| 67 | ;;(?DŽ ?DŽ ?dž ?Dž) | ||
| 68 | ;; but instead we have: | ||
| 69 | (?DŽ ?DŽ ?dž ?DŽ) | ||
| 70 | ;; FIXME(bug#24603): Those two are broken at the moment: | ||
| 71 | ;;(?Dž ?DŽ ?dž ?Dž) | ||
| 72 | ;;(?dž ?DŽ ?dž ?Dž) | ||
| 73 | |||
| 74 | (?Σ ?Σ ?σ ?Σ) | ||
| 75 | (?σ ?Σ ?σ ?Σ) | ||
| 76 | ;; FIXME(bug#24603): Another broken one: | ||
| 77 | ;;(?ς ?Σ ?ς ?Σ) | ||
| 78 | |||
| 79 | (?Ⅷ ?Ⅷ ?ⅷ ?Ⅷ) | ||
| 80 | (?ⅷ ?Ⅷ ?ⅷ ?Ⅷ))) | ||
| 81 | |||
| 82 | |||
| 83 | (ert-deftest casefiddle-tests-case-table () | ||
| 84 | "Sanity check of down and up case tables." | ||
| 85 | (should-not | ||
| 86 | (let (errors | ||
| 87 | (up (case-table-get-table (current-case-table) 'up)) | ||
| 88 | (down (case-table-get-table (current-case-table) 'down))) | ||
| 89 | (dolist (test casefiddle-tests--characters) | ||
| 90 | (let ((ch (car test)) | ||
| 91 | (expected (cdr test)) | ||
| 92 | (props '(uppercase lowercase)) | ||
| 93 | (tabs (list up down))) | ||
| 94 | (while props | ||
| 95 | (let ((got (aref (car tabs) ch))) | ||
| 96 | (unless (equal (car expected) got) | ||
| 97 | (push (format "\n%c %s; expected: %s but got: %s" | ||
| 98 | ch (car props) (car expected) got) | ||
| 99 | errors))) | ||
| 100 | (setq props (cdr props) tabs (cdr tabs) expected (cdr expected))))) | ||
| 101 | (when errors | ||
| 102 | (mapconcat (lambda (line) line) (nreverse errors) ""))))) | ||
| 103 | |||
| 104 | |||
| 105 | (ert-deftest casefiddle-tests-casing-character () | ||
| 106 | (should-not | ||
| 107 | (let (errors) | ||
| 108 | (dolist (test casefiddle-tests--characters) | ||
| 109 | (let ((ch (car test)) | ||
| 110 | (expected (cdr test)) | ||
| 111 | (funcs '(upcase downcase capitalize))) | ||
| 112 | (while funcs | ||
| 113 | (let ((got (funcall (car funcs) ch))) | ||
| 114 | (unless (equal (car expected) got) | ||
| 115 | (push (format "\n%c %s; expected: %s but got: %s" | ||
| 116 | ch (car funcs) (car expected) got) | ||
| 117 | errors))) | ||
| 118 | (setq funcs (cdr funcs) expected (cdr expected))))) | ||
| 119 | (when errors | ||
| 120 | (mapconcat (lambda (line) line) (nreverse errors) ""))))) | ||
| 121 | |||
| 122 | |||
| 123 | (ert-deftest casefiddle-tests-casing-word () | ||
| 124 | (with-temp-buffer | ||
| 125 | (dolist (test '((upcase-word . "FOO Bar") | ||
| 126 | (downcase-word . "foo Bar") | ||
| 127 | (capitalize-word . "Foo Bar"))) | ||
| 128 | (dolist (back '(nil t)) | ||
| 129 | (delete-region (point-min) (point-max)) | ||
| 130 | (insert "foO Bar") | ||
| 131 | (goto-char (+ (if back 4 0) (point-min))) | ||
| 132 | (funcall (car test) (if back -1 1)) | ||
| 133 | (should (string-equal (cdr test) (buffer-string))) | ||
| 134 | (should (equal (+ (if back 4 3) (point-min)) (point))))))) | ||
| 135 | |||
| 136 | |||
| 137 | (defun casefiddle-tests--test-casing (tests) | ||
| 138 | (nreverse | ||
| 139 | (cl-reduce | ||
| 140 | (lambda (errors test) | ||
| 141 | (let* ((input (car test)) | ||
| 142 | (expected (cdr test)) | ||
| 143 | (func-pairs '((upcase upcase-region) | ||
| 144 | (downcase downcase-region) | ||
| 145 | (capitalize capitalize-region) | ||
| 146 | (upcase-initials upcase-initials-region))) | ||
| 147 | (get-string (lambda (func) (funcall func input))) | ||
| 148 | (get-region (lambda (func) | ||
| 149 | (delete-region (point-min) (point-max)) | ||
| 150 | (unwind-protect | ||
| 151 | (progn | ||
| 152 | (unless (multibyte-string-p input) | ||
| 153 | (toggle-enable-multibyte-characters)) | ||
| 154 | (insert input) | ||
| 155 | (funcall func (point-min) (point-max)) | ||
| 156 | (buffer-string)) | ||
| 157 | (unless (multibyte-string-p input) | ||
| 158 | (toggle-enable-multibyte-characters))))) | ||
| 159 | (fmt-str (lambda (str) | ||
| 160 | (format "%s (%sbyte; %d chars; %d bytes)" | ||
| 161 | str | ||
| 162 | (if (multibyte-string-p str) "multi" "uni") | ||
| 163 | (length str) (string-bytes str)))) | ||
| 164 | funcs getters) | ||
| 165 | (while (and func-pairs expected) | ||
| 166 | (setq funcs (car func-pairs) | ||
| 167 | getters (list get-string get-region)) | ||
| 168 | (while (and funcs getters) | ||
| 169 | (let ((got (funcall (car getters) (car funcs)))) | ||
| 170 | (unless (string-equal got (car expected)) | ||
| 171 | (let ((fmt (length (symbol-name (car funcs))))) | ||
| 172 | (setq fmt (format "\n%%%ds: %%s" (max fmt 8))) | ||
| 173 | (push (format (concat fmt fmt fmt) | ||
| 174 | (car funcs) (funcall fmt-str input) | ||
| 175 | "expected" (funcall fmt-str (car expected)) | ||
| 176 | "but got" (funcall fmt-str got)) | ||
| 177 | errors)))) | ||
| 178 | (setq funcs (cdr funcs) getters (cdr getters))) | ||
| 179 | (setq func-pairs (cdr func-pairs) expected (cdr expected)))) | ||
| 180 | errors) | ||
| 181 | (cons () tests)))) | ||
| 182 | |||
| 183 | (ert-deftest casefiddle-tests-casing () | ||
| 184 | (should-not | ||
| 185 | (with-temp-buffer | ||
| 186 | (casefiddle-tests--test-casing | ||
| 187 | ;; input upper lower capitalize up-initials | ||
| 188 | '(("Foo baR" "FOO BAR" "foo bar" "Foo Bar" "Foo BaR") | ||
| 189 | ("Ⅷ ⅷ" "Ⅷ Ⅷ" "ⅷ ⅷ" "Ⅷ Ⅷ" "Ⅷ Ⅷ") | ||
| 190 | ;; FIXME(bug#24603): Everything below is broken at the moment. | ||
| 191 | ;; Here’s what should happen: | ||
| 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") | ||
| 198 | ;;("ΌΣΟΣ" "ΌΣΟΣ" "όσος" "Όσος" "Όσος") | ||
| 199 | ;;("όσος" "ΌΣΟΣ" "όσος" "Όσος" "Όσος") | ||
| 200 | ;; And here’s what is actually happening: | ||
| 201 | ("DŽUNGLA" "DŽUNGLA" "džungla" "DŽungla" "DŽUNGLA") | ||
| 202 | ("Džungla" "DžUNGLA" "džungla" "Džungla" "Džungla") | ||
| 203 | ("džungla" "DŽUNGLA" "džungla" "DŽungla" "DŽungla") | ||
| 204 | ("define" "DEfiNE" "define" "Define" "Define") | ||
| 205 | ("fish" "fiSH" "fish" "fish" "fish") | ||
| 206 | ("Straße" "STRAßE" "straße" "Straße" "Straße") | ||
| 207 | ("ΌΣΟΣ" "ΌΣΟΣ" "όσοσ" "Όσοσ" "ΌΣΟΣ") | ||
| 208 | ("όσος" "ΌΣΟς" "όσος" "Όσος" "Όσος")))))) | ||
| 209 | |||
| 210 | (ert-deftest casefiddle-tests-casing-byte8 () | ||
| 211 | (should-not | ||
| 212 | (with-temp-buffer | ||
| 213 | (casefiddle-tests--test-casing | ||
| 214 | '(("\xff Foo baR \xff" | ||
| 215 | "\xff FOO BAR \xff" | ||
| 216 | "\xff foo bar \xff" | ||
| 217 | "\xff Foo Bar \xff" | ||
| 218 | "\xff Foo BaR \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" | ||
| 223 | "\xff Zażółć GĘŚlą \xff")))))) | ||
| 224 | |||
| 225 | (ert-deftest casefiddle-tests-casing-byte8-with-changes () | ||
| 226 | (let ((tab (copy-case-table (standard-case-table))) | ||
| 227 | (test '("\xff\xff\xef Foo baR \xcf\xcf" | ||
| 228 | "\xef\xef\xef FOO BAR \xcf\xcf" | ||
| 229 | "\xff\xff\xff foo bar \xcf\xcf" | ||
| 230 | "\xef\xff\xff Foo Bar \xcf\xcf" | ||
| 231 | "\xef\xff\xef Foo BaR \xcf\xcf")) | ||
| 232 | (byte8 #x3FFF00)) | ||
| 233 | (should-not | ||
| 234 | (with-temp-buffer | ||
| 235 | (set-case-table tab) | ||
| 236 | (set-case-syntax-pair (+ byte8 #xef) (+ byte8 #xff) tab) | ||
| 237 | (casefiddle-tests--test-casing | ||
| 238 | (list test | ||
| 239 | (mapcar (lambda (str) (decode-coding-string str 'binary)) test) | ||
| 240 | '("\xff\xff\xef Zażółć gĘŚlą \xcf\xcf" | ||
| 241 | "\xef\xef\xef ZAŻÓŁĆ GĘŚLĄ \xcf\xcf" | ||
| 242 | "\xff\xff\xff zażółć gęślą \xcf\xcf" | ||
| 243 | "\xef\xff\xff Zażółć Gęślą \xcf\xcf" | ||
| 244 | "\xef\xff\xef Zażółć GĘŚlą \xcf\xcf"))))))) | ||
| 245 | |||
| 246 | |||
| 247 | ;;; casefiddle-tests.el ends here | ||