aboutsummaryrefslogtreecommitdiffstats
path: root/test/lisp/misc-tests.el
diff options
context:
space:
mode:
Diffstat (limited to 'test/lisp/misc-tests.el')
-rw-r--r--test/lisp/misc-tests.el90
1 files changed, 90 insertions, 0 deletions
diff --git a/test/lisp/misc-tests.el b/test/lisp/misc-tests.el
index b6f5f01ad2a..81ebe1a5869 100644
--- a/test/lisp/misc-tests.el
+++ b/test/lisp/misc-tests.el
@@ -25,6 +25,7 @@
25 25
26(require 'ert) 26(require 'ert)
27(require 'misc) 27(require 'misc)
28(require 'mule-util)
28 29
29(defmacro with-misc-test (original result &rest body) 30(defmacro with-misc-test (original result &rest body)
30 (declare (indent 2)) 31 (declare (indent 2))
@@ -243,5 +244,94 @@
243 (setq-default display-line-numbers dln)) 244 (setq-default display-line-numbers dln))
244 (should (= w0 w1)))) 245 (should (= w0 w1))))
245 246
247;; Exercise `truncate-string-pixelwise' with strings of the same
248;; characters of differing widths, with and without ellipses, in varying
249;; faces, and varying face heights and compare results to each
250;; character's measured width.
251(ert-deftest misc-test-truncate-string-pixelwise ()
252 ;; Test empty string without an explicit buffer.
253 (should (equal (truncate-string-pixelwise "" 123) ""))
254 ;; Test fast path without an explicit buffer.
255 (should (equal (truncate-string-pixelwise "123" 123) "123"))
256 (with-temp-buffer
257 ;; Test empty string with an explicit buffer.
258 (should (equal (truncate-string-pixelwise "" 123 (current-buffer)) ""))
259 ;; Test fast path with an explicit buffer.
260 (should (equal (truncate-string-pixelwise "123" 123 (current-buffer)) "123")))
261
262 (dolist (c '(?W ?X ?y ?1))
263 (dolist (ellipsis `(nil "..." ,(truncate-string-ellipsis)))
264 (dolist (face '(fixed-pitch variable-pitch))
265 (dolist (height '(1.0 0.5 1.5))
266 (with-temp-buffer
267 (setq-local face-remapping-alist `((,face . default)))
268 (face-remap-add-relative 'default :height height)
269 (let ((char-pixels (string-pixel-width
270 (make-string 1 c) (current-buffer))))
271 (dotimes (i 20)
272 (setq i (1+ i))
273 (should (eq i (length
274 (truncate-string-pixelwise
275 (make-string (* i 2) c)
276 (* i char-pixels)
277 (current-buffer)
278 ellipsis))))))))))))
279
280;; Exercise `truncate-string-pixelwise' with varying unicode strings, in
281;; varying faces, and varying face heights and compare results to a
282;; naive `string-pixel-width' based string truncate function.
283(ert-deftest misc-test-truncate-string-pixelwise-unicode ()
284 :tags '(:expensive-test)
285 (skip-when noninteractive)
286 (let ((max-pixels 500)
287 (truncate-string-naive (lambda (string pixels buffer)
288 (while (and (length> string 0)
289 (> (string-pixel-width string buffer) pixels))
290 (setq string (substring string 0 (1- (length string)))))
291 string))
292 (strings (list
293 "foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz"
294 (concat "話說天下大勢,分久必合,合久必分:周末七國分爭,并入於秦。"
295 "及秦滅之後,楚、漢分爭,又并入於漢。漢朝自高祖斬白蛇而起義,"
296 "一統天下。後來光武中興,傳至獻帝,遂分為三國。推其致亂之由,"
297 "殆始於桓、靈二帝。桓帝禁錮善類,崇信宦官。及桓帝崩,靈帝即位,"
298 "大將軍竇武、太傅陳蕃,共相輔佐。時有宦官曹節等弄權,竇武、陳蕃謀誅之,"
299 "作事不密,反為所害。中涓自此愈橫")
300 (concat "короче теперь если по русски написать все четко или все равно"
301 " короче теперь если по русски написать все четко или все равно"
302 " короче теперь если по русски написать все четко или все равно"
303 " короче теперь если по русски написать все четко или все равно")
304 "будет разрыв строки непонятно где🏁🚩🎌🏴🏳️ 🏳️ <200d>🌈🏳️ <200d>⚧️🏴<200d>☠️"
305 (apply #'concat (make-list 200 "\u0065\u0301 ")) ; composed é \u00E9
306 (let ((woman-loves-man ; 👩‍❤️‍👨
307 (concat "\N{WOMAN}"
308 "\N{ZERO WIDTH JOINER}"
309 "\N{HEAVY BLACK HEART}"
310 "\N{VARIATION SELECTOR-16}"
311 "\N{ZERO WIDTH JOINER}"
312 "\N{MAN}"
313 " ")))
314 (apply #'concat (make-list 200 woman-loves-man)))
315 (propertize (let ((varying-height-string
316 (mapconcat
317 #'identity
318 (list "AWi!"
319 (propertize "foo" 'face '(:height 2.5))
320 (propertize "bar" 'face '(:height 0.5))
321 (propertize "baz" 'face '(:height 1.0)))
322 " ")))
323 (apply #'concat (make-list 100 varying-height-string)))
324 'face 'variable-pitch))))
325 (dolist (face '(fixed-pitch variable-pitch))
326 (dolist (height '(1.0 0.5 1.5))
327 (with-temp-buffer
328 (setq-local face-remapping-alist `((,face . default)))
329 (face-remap-add-relative 'default :height height)
330 (dolist (string strings)
331 (should (eq (length (funcall truncate-string-naive
332 string max-pixels (current-buffer)))
333 (length (truncate-string-pixelwise
334 string max-pixels (current-buffer)))))))))))
335
246(provide 'misc-tests) 336(provide 'misc-tests)
247;;; misc-tests.el ends here 337;;; misc-tests.el ends here