unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob f7b0da4102956e239f4bd59c3fe830b27e399a41 13868 bytes (raw)
name: test/src/casefiddle-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
 
;;; casefiddle-tests.el --- tests for casefiddle.c functions -*- lexical-binding: t -*-

;; Copyright (C) 2015-2016 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'case-table)
(require 'ert)

(ert-deftest casefiddle-tests-char-properties ()
  "Sanity check of character Unicode properties."
  (should-not
   (let (errors)
     ;;            character  uppercase  lowercase  titlecase
     (dolist (test '((?A nil ?a nil)
                     (?a ?A nil ?A)
                     (?Ł nil ?ł nil)
                     (?ł ?Ł nil ?Ł)

                     (?DŽ nil ?dž ?Dž)
                     (?Dž ?DŽ ?dž ?Dž)
                     (?dž ?DŽ nil ?Dž)

                     (?Σ nil ?σ nil)
                     (?σ ?Σ nil ?Σ)
                     (?ς ?Σ nil ?Σ)

                     (?ⅷ ?Ⅷ nil ?Ⅷ)
                     (?Ⅷ nil ?ⅷ nil)))
       (let ((ch (car test))
             (expected (cdr test))
             (props '(uppercase lowercase titlecase)))
         (while props
           (let ((got (get-char-code-property ch (car props))))
             (unless (equal (car expected) got)
               (push (format "\n%c %s; expected: %s but got: %s"
                             ch (car props) (car expected) got)
                     errors)))
           (setq props (cdr props) expected (cdr expected)))))
     (when errors
       (mapconcat (lambda (line) line) (nreverse errors) "")))))


(defconst casefiddle-tests--characters
  ;; character  uppercase  lowercase  titlecase
  '((?A ?A ?a ?A)
    (?a ?A ?a ?A)
    (?Ł ?Ł ?ł ?Ł)
    (?ł ?Ł ?ł ?Ł)

    (?DŽ ?DŽ ?dž ?Dž)
    (?Dž ?DŽ ?dž ?Dž)
    (?dž ?DŽ ?dž ?Dž)

    (?Σ ?Σ ?σ ?Σ)
    (?σ ?Σ ?σ ?Σ)
    (?ς ?Σ ?ς ?Σ)

    (?Ⅷ ?Ⅷ ?ⅷ ?Ⅷ)
    (?ⅷ ?Ⅷ ?ⅷ ?Ⅷ)))


(ert-deftest casefiddle-tests-case-table ()
  "Sanity check of down and up case tables."
  (should-not
   (let (errors
         (up (case-table-get-table (current-case-table) 'up))
         (down (case-table-get-table (current-case-table) 'down)))
     (dolist (test casefiddle-tests--characters)
       (let ((ch (car test))
             (expected (cdr test))
             (props '(uppercase lowercase))
             (tabs (list up down)))
         (while props
           (let ((got (aref (car tabs) ch)))
             (unless (equal (car expected) got)
               (push (format "\n%c %s; expected: %s but got: %s"
                             ch (car props) (car expected) got)
                     errors)))
           (setq props (cdr props) tabs (cdr tabs) expected (cdr expected)))))
     (when errors
       (mapconcat (lambda (line) line) (nreverse errors) "")))))


(ert-deftest casefiddle-tests-casing-character ()
  (should-not
   (let (errors)
     (dolist (test casefiddle-tests--characters)
       (let ((ch (car test))
             (expected (cdr test))
             (funcs '(upcase downcase capitalize)))
         (while funcs
           (let ((got (funcall (car funcs) ch)))
             (unless (equal (car expected) got)
               (push (format "\n%c %s; expected: %s but got: %s"
                             ch (car funcs) (car expected) got)
                     errors)))
           (setq funcs (cdr funcs) expected (cdr expected)))))
     (when errors
       (mapconcat (lambda (line) line) (nreverse errors) "")))))


(ert-deftest casefiddle-tests-casing-word ()
  (with-temp-buffer
    (dolist (test '((upcase-word     . "FOO Bar")
                    (downcase-word   . "foo Bar")
                    (capitalize-word . "Foo Bar")))
      (dolist (back '(nil t))
        (delete-region (point-min) (point-max))
        (insert "foO Bar")
        (goto-char (+ (if back 4 0) (point-min)))
        (funcall (car test) (if back -1 1))
        (should (string-equal (cdr test) (buffer-string)))
        (should (equal (+ (if back 4 3) (point-min)) (point)))))))


(defun casefiddle-tests--test-casing (tests)
  (nreverse
   (cl-reduce
    (lambda (errors test)
      (let* ((input (car test))
             (expected (cdr test))
             (buffer-language (or (nth 5 test) "en_GB"))
             (func-pairs '((upcase upcase-region)
                           (downcase downcase-region)
                           (capitalize capitalize-region)
                           (upcase-initials upcase-initials-region)))
             (get-string (lambda (func) (funcall func input)))
             (get-region (lambda (func)
                           (delete-region (point-min) (point-max))
                           (unwind-protect
                               (progn
                                 (unless (multibyte-string-p input)
                                   (toggle-enable-multibyte-characters))
                                 (insert input)
                                 (funcall func (point-min) (point-max))
                                 (buffer-string))
                             (unless (multibyte-string-p input)
                               (toggle-enable-multibyte-characters)))))
             (fmt-str (lambda (str)
                        (format "%s  (%sbyte; %d chars; %d bytes)"
                                str
                                (if (multibyte-string-p str) "multi" "uni")
                                (length str) (string-bytes str))))
             funcs getters)
        (while (and func-pairs expected)
          (setq funcs (car func-pairs)
                getters (list get-string get-region))
          (while (and funcs getters)
            (let ((got (funcall (car getters) (car funcs))))
              (unless (string-equal got (car expected))
                (let ((fmt (length (symbol-name (car funcs)))))
                  (setq fmt (format "\n%%%ds: %%s" (max fmt 8)))
                  (push (format (concat fmt fmt fmt)
                                (car funcs) (funcall fmt-str input)
                                "expected" (funcall fmt-str (car expected))
                                "but got" (funcall fmt-str got))
                        errors))))
            (setq funcs (cdr funcs) getters (cdr getters)))
          (setq func-pairs (cdr func-pairs) expected (cdr expected))))
      errors)
    (cons () tests))))

(ert-deftest casefiddle-tests-casing ()
  (should-not
   (with-temp-buffer
     (casefiddle-tests--test-casing
      ;; input     upper     lower    capitalize up-initials  [locale]
      `(("Foo baR" "FOO BAR" "foo bar" "Foo Bar" "Foo BaR")
        ("Ⅷ ⅷ" "Ⅷ Ⅷ" "ⅷ ⅷ" "Ⅷ Ⅷ" "Ⅷ Ⅷ")
        ;; "DžUNGLA" is an unfortunate result but it’s really best we can
        ;; do while still being consistent.  Hopefully, users only ever
        ;; use upcase-initials on camelCase identifiers not real words.
        ("DŽUNGLA" "DŽUNGLA" "džungla" "Džungla" "DžUNGLA")
        ("Džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla")
        ("džungla" "DŽUNGLA" "džungla" "Džungla" "Džungla")
        ("define" "DEFINE" "define" "Define" "Define")
        ("fish" "FISH" "fish" "Fish" "Fish")
        ("Straße" "STRASSE" "straße" "Straße" "Straße")

        ;; The word repeated twice to test behaviour at the end of a word
        ;; inside of an input string as well as at the end of the string.
        ("ΌΣΟΣ ΌΣΟΣ" "ΌΣΟΣ ΌΣΟΣ" "όσος όσος" "Όσος Όσος" "ΌΣΟΣ ΌΣΟΣ")
        ;; What should be done with sole sigma?  It is ‘final’ but on the
        ;; other hand it does not form a word.  We’re using regular sigma.
        ("Σ Σ" "Σ Σ" "σ σ" "Σ Σ" "Σ Σ")
        ("όσος" "ΌΣΟΣ" "όσος" "Όσος" "Όσος")
        ;; If sigma is already lower case, we don’t want to change it.
        ("όσοσ" "ΌΣΟΣ" "όσοσ" "Όσοσ" "Όσοσ")

        ;; Dutch 'ij' is capitalised as single digraph.
        ("ijsland" "IJSLAND" "ijsland" "Ijsland" "Ijsland")
        ("ijsland" "IJSLAND" "ijsland" "IJsland" "IJsland" "nl")

        ;; There is a language-independent special casing rule which
        ;; converts İ into i followed by combining dot above that’s why we
        ;; get the weird \u0307.  Conceptually, it converts i with
        ;; a soft-dot into an i with a hard-dot so it makes some doze of
        ;; sense.
        ("İstanbul" "İSTANBUL" "i\u0307stanbul" "İstanbul" "İstanbul")
        ("İstanbul" "İSTANBUL" "istanbul" "İstanbul" "İstanbul" "tr")
        ("İstanbul" "İSTANBUL" "istanbul" "İstanbul" "İstanbul" "az")
        (,(decode-coding-string "istanbul" 'no-conversion-multibyte) ; make it multibyte
         "ISTANBUL" "istanbul" "Istanbul" "Istanbul")
        (,(decode-coding-string "istanbul" 'no-conversion-multibyte)
         "İSTANBUL" "istanbul" "İstanbul" "İstanbul" "tr")
        (,(decode-coding-string "istanbul" 'no-conversion-multibyte)
         "İSTANBUL" "istanbul" "İstanbul" "İstanbul" "az")
        (,(decode-coding-string "Irmak" 'no-conversion-multibyte)
         "IRMAK" "irmak" "Irmak" "Irmak")
        (,(decode-coding-string "Irmak" 'no-conversion-multibyte)
         "IRMAK" "ırmak" "Irmak" "Irmak" "tr")
        (,(decode-coding-string "Irmak" 'no-conversion-multibyte)
         "IRMAK" "ırmak" "Irmak" "Irmak" "az")
        ;; FIXME: We explicitly exclude ı→I mapping from the case tables
        ;; in characters.el which is why instead of:
        ;;("ırmak" "IRMAK" "ırmak" "Irmak" "Irmak")
        ;; we actually get:
        ("ırmak" "ıRMAK" "ırmak" "Irmak" "Irmak")
        ;; ‘But wait,’ you ask, ‘why capitalisation works’?  This is
        ;; because those bypass case-table and use character’s Unicode
        ;; titlecase property.
        ("ırmak" "IRMAK" "ırmak" "Irmak" "Irmak" "tr")
        ("ırmak" "IRMAK" "ırmak" "Irmak" "Irmak" "az")
        ;; And for some combining dot above removal.
        ("I\u0307si\u0307s" "I\u0307\u0307S" "isi\u0307s"
         "I\u0307si\u0307s" "I\u0307si\u0307s" "tr")
        ("I\u0307sI\u0307s" "I\u0307SI\u0307S" "isis"
         "I\u0307sis" "I\u0307sI\u0307s" "tr")

        ;; Test combining dot above in inserted when needed when lower
        ;; casing I or J.
        ("I\u0328\u0300"          ; I + ogonek + grave
         "I\u0328\u0300" "i\u0328\u0307\u0300"
         "I\u0328\u0300" "I\u0328\u0300" "lt")

        ("J\u0328\u0300"          ; J + ogonek + grave
         "J\u0328\u0300" "j\u0328\u0307\u0300"
         "J\u0328\u0300" "J\u0328\u0300" "lt")

        (\u0300"          ; I-ogonek + grave
         \u0300" \u0307\u0300" \u0300" \u0300" "lt")

        ("Ì Í Ĩ"
         "Ì Í Ĩ" "i\u0307\u0300 i\u0307\u0301 i\u0307\u0303"
         "Ì Í Ĩ" "Ì Í Ĩ" "lt")

        ;; Test combining dot above in removed when upper casing i or j.
        ("i\u0328\u0307"          ; i + ogonek + dot above
         "I\u0328" "i\u0328\u0307" "I\u0328" "I\u0328" "lt")
        ("j\u0328\u0307"          ; j + ogonek + dot above
         "J\u0328" "j\u0328\u0307" "J\u0328" "J\u0328" "lt")
        (\u0307"                ; i-ogonek + dot above
         "Į" \u0307" "Į" "Į" "lt"))))))

(ert-deftest casefiddle-tests-casing-byte8 ()
  (should-not
   (with-temp-buffer
     (casefiddle-tests--test-casing
      '(("\xff Foo baR \xff"
         "\xff FOO BAR \xff"
         "\xff foo bar \xff"
         "\xff Foo Bar \xff"
         "\xff Foo BaR \xff")
        ("\xff Zażółć gĘŚlą \xff"
         "\xff ZAŻÓŁĆ GĘŚLĄ \xff"
         "\xff zażółć gęślą \xff"
         "\xff Zażółć Gęślą \xff"
         "\xff Zażółć GĘŚlą \xff"))))))

(ert-deftest casefiddle-tests-casing-byte8-with-changes ()
  (let ((tab (copy-case-table (standard-case-table)))
        (test '("\xff\xff\xef Foo baR \xcf\xcf"
                "\xef\xef\xef FOO BAR \xcf\xcf"
                "\xff\xff\xff foo bar \xcf\xcf"
                "\xef\xff\xff Foo Bar \xcf\xcf"
                "\xef\xff\xef Foo BaR \xcf\xcf"))
        (byte8 #x3FFF00))
    (should-not
     (with-temp-buffer
       (set-case-table tab)
       (set-case-syntax-pair (+ byte8 #xef) (+ byte8 #xff) tab)
       (casefiddle-tests--test-casing
        (list test
              (mapcar (lambda (str) (decode-coding-string str 'binary)) test)
              '("\xff\xff\xef Zażółć gĘŚlą \xcf\xcf"
                "\xef\xef\xef ZAŻÓŁĆ GĘŚLĄ \xcf\xcf"
                "\xff\xff\xff zażółć gęślą \xcf\xcf"
                "\xef\xff\xff Zażółć Gęślą \xcf\xcf"
                "\xef\xff\xef Zażółć GĘŚlą \xcf\xcf")))))))


(ert-deftest casefiddle-tests-char-casing ()
  ;;             input upcase downcase [titlecase]
  (dolist (test '((?a ?A ?a) (?A ?A ?a)
                  (?ł ?Ł ?ł) (?Ł ?Ł ?ł)
                  (?ß ?ß ?ß) (?ẞ ?ẞ ?ß)
                  (?ⅷ ?Ⅷ ?ⅷ) (?Ⅷ ?Ⅷ ?ⅷ)
                  (?DŽ ?DŽ ?dž ?Dž) (?Dž ?DŽ ?dž ?Dž) (?dž ?DŽ ?dž ?Dž)))
    (let ((ch (car test))
          (up (nth 1 test))
          (lo (nth 2 test))
          (tc (or (nth 3 test) (nth 1 test))))
      (should (eq up (upcase ch)))
      (should (eq lo (downcase ch)))
      (should (eq tc (capitalize ch)))
      (should (eq tc (upcase-initials ch))))))


;;; casefiddle-tests.el ends here

debug log:

solving f7b0da41029 ...
found f7b0da41029 in https://yhetil.org/emacs-bugs/20170309215150.9562-11-mina86@mina86.com/
found ce1bb18dd40 in https://yhetil.org/emacs-bugs/20170309215150.9562-10-mina86@mina86.com/
found 5e38a97d256 in https://yhetil.org/emacs-bugs/20170309215150.9562-9-mina86@mina86.com/
found 10450360eab in https://yhetil.org/emacs-bugs/20170309215150.9562-7-mina86@mina86.com/
found 8a88292bd78 in https://yhetil.org/emacs-bugs/20170309215150.9562-6-mina86@mina86.com/
found e83cb00059b in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 e83cb00059beeba553e9c5abc335751d86f8d852	test/src/casefiddle-tests.el

applying [1/5] https://yhetil.org/emacs-bugs/20170309215150.9562-6-mina86@mina86.com/
diff --git a/test/src/casefiddle-tests.el b/test/src/casefiddle-tests.el
index e83cb00059b..8a88292bd78 100644


applying [2/5] https://yhetil.org/emacs-bugs/20170309215150.9562-7-mina86@mina86.com/
diff --git a/test/src/casefiddle-tests.el b/test/src/casefiddle-tests.el
index 8a88292bd78..10450360eab 100644


applying [3/5] https://yhetil.org/emacs-bugs/20170309215150.9562-9-mina86@mina86.com/
diff --git a/test/src/casefiddle-tests.el b/test/src/casefiddle-tests.el
index 10450360eab..5e38a97d256 100644


applying [4/5] https://yhetil.org/emacs-bugs/20170309215150.9562-10-mina86@mina86.com/
diff --git a/test/src/casefiddle-tests.el b/test/src/casefiddle-tests.el
index 5e38a97d256..ce1bb18dd40 100644


applying [5/5] https://yhetil.org/emacs-bugs/20170309215150.9562-11-mina86@mina86.com/
diff --git a/test/src/casefiddle-tests.el b/test/src/casefiddle-tests.el
index ce1bb18dd40..f7b0da41029 100644

Checking patch test/src/casefiddle-tests.el...
Applied patch test/src/casefiddle-tests.el cleanly.
Checking patch test/src/casefiddle-tests.el...
Applied patch test/src/casefiddle-tests.el cleanly.
Checking patch test/src/casefiddle-tests.el...
Applied patch test/src/casefiddle-tests.el cleanly.
Checking patch test/src/casefiddle-tests.el...
Applied patch test/src/casefiddle-tests.el cleanly.
Checking patch test/src/casefiddle-tests.el...
Applied patch test/src/casefiddle-tests.el cleanly.

index at:
100644 f7b0da4102956e239f4bd59c3fe830b27e399a41	test/src/casefiddle-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).