unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 0d640ad59c3c1b21c84ff075c434cc982ab020c1 15358 bytes (raw)
name: test/lisp/erc/erc-nicks-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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
 
;;; erc-nicks-tests.el --- Tests for erc-nicks  -*- lexical-binding:t -*-

;; Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Unlike most of ERC's tests, the ones in this file can be run
;; interactively in the same session.

;; TODO:
;;
;; * Add mock session (or scenario) with buffer snapshots, like those
;;   in erc-fill-tests.el.  (Should probably move helpers to a common
;;   library under ./resources.)

;;; Code:

(require 'ert)
(require 'erc-nicks)

;; This function replicates the behavior of older "invert" strategy
;; implementations from EmacsWiki, etc.  The values for the lower and
;; upper bounds (0.33 and 0.66) are likewise inherited.  See
;; `erc-nicks--invert-classic--dark' below for one reason its results
;; may not be plainly obvious.
(defun erc-nicks-tests--invert-classic (color)
  (if (pcase (erc-nicks--bg-mode)
        ('dark (< (erc-nicks--get-luminance color) (/ 1 3.0)))
        ('light (> (erc-nicks--get-luminance color) (/ 2 3.0))))
      (list (- 1.0 (nth 0 color)) (- 1.0 (nth 1 color)) (- 1.0 (nth 2 color)))
    color))


(ert-deftest erc-nicks--get-luminance ()
  (should (eql 0.0 (erc-nicks--get-luminance "black")))
  (should (eql 1.0 (erc-nicks--get-luminance "white")))
  (should (eql 21.0 (/ (+ 0.05 1.0) (+ 0.05 0.0))))

  ;; RGB floats from a `display-graphic-p' session.
  (let ((a (erc-nicks--get-luminance ; #9439ad
            '(0.5803921568627451 0.2235294117647059 0.6784313725490196)))
        (b (erc-nicks--get-luminance ; #ae54c7
            '(0.6823529411764706 0.32941176470588235 0.7803921568627451)))
        (c (erc-nicks--get-luminance ; #d19ddf
            '(0.8196078431372549 0.615686274509804 0.8745098039215686)))
        (d (erc-nicks--get-luminance ; #f5e8f8
            '(0.9607843137254902 0.9098039215686274 0.9725490196078431))))
    ;; Low, med, high contrast comparisons against known values from
    ;; an external source.
    (should (eql 1.42 (/ (round (* 100 (/ (+ 0.05 b) (+ 0.05 a)))) 100.0)))
    (should (eql 2.78 (/ (round (* 100 (/ (+ 0.05 c) (+ 0.05 a)))) 100.0)))
    (should (eql 5.16 (/ (round (* 100 (/ (+ 0.05 d) (+ 0.05 a)))) 100.0)))))

(ert-deftest erc-nicks-invert--classic ()
  (let ((convert (lambda (n) (apply #'color-rgb-to-hex
                                    (erc-nicks-tests--invert-classic
                                     (color-name-to-rgb n))))))
    (let ((erc-nicks--bg-mode-value 'dark))
      (should (equal (funcall convert "white") "#ffffffffffff"))
      (should (equal (funcall convert "black") "#ffffffffffff"))
      (should (equal (funcall convert "green") "#0000ffff0000")))
    (let ((erc-nicks--bg-mode-value 'light))
      (should (equal (funcall convert "white") "#000000000000"))
      (should (equal (funcall convert "black") "#000000000000"))
      (should (equal (funcall convert "green") "#ffff0000ffff")))))

(ert-deftest erc-nicks--get-contrast ()
  (should (= 21.0 (erc-nicks--get-contrast "white" "black")))
  (should (= 21.0 (erc-nicks--get-contrast "black" "white")))
  (should (= 1.0 (erc-nicks--get-contrast "black" "black")))
  (should (= 1.0 (erc-nicks--get-contrast "white" "white"))))

(defun erc-nicks-tests--print-contrast (fn color)
  (let* ((erc-nicks-color-adjustments (list fn))
         (result (erc-nicks--reduce color))
         (start (point)))
    (insert (format "%16s%-16s%16s%-16s\n"
                    (concat color "-")
                    (concat ">" result)
                    (concat color " ")
                    (concat " " result)))
    (put-text-property (+ start 32) (+ start 48) 'face
                       (list :background color :foreground result))
    (put-text-property (+ start 48) (+ start 64) 'face
                       (list :background result :foreground color))
    result))

(ert-deftest erc-nicks--invert-classic--light ()
  (let ((erc-nicks--bg-luminance 1.0)
        (erc-nicks--bg-mode-value 'light)
        (show (lambda (c) (erc-nicks-tests--print-contrast
                           #'erc-nicks-tests--invert-classic c))))

    (with-current-buffer (get-buffer-create
                          "*erc-nicks--invert-classic--light*")
      (should (equal "#000000000000" (funcall show "white")))
      (should (equal "#000000000000" (funcall show "black")))
      (should (equal "#ffff00000000" (funcall show "red")))
      (should (equal "#ffff0000ffff" (funcall show "green"))) ; magenta
      (should (equal "#00000000ffff" (funcall show "blue")))

      (unless noninteractive
        (should (equal "#bbbbbbbbbbbb" (funcall show "#bbbbbbbbbbbb")))
        (should (equal "#cccccccccccc" (funcall show "#cccccccccccc")))
        (should (equal "#222122212221" (funcall show "#dddddddddddd")))
        (should (equal "#111011101110" (funcall show "#eeeeeeeeeeee"))))

      (when noninteractive
        (kill-buffer)))))

;; This shows that the output can be darker (have less contrast) than
;; the input.
(ert-deftest erc-nicks--invert-classic--dark ()
  (let ((erc-nicks--bg-luminance 0.0)
        (erc-nicks--bg-mode-value 'dark)
        (show (lambda (c) (erc-nicks-tests--print-contrast
                           #'erc-nicks-tests--invert-classic c))))

    (with-current-buffer (get-buffer-create
                          "*erc-nicks--invert-classic--dark*")
      (should (equal "#ffffffffffff" (funcall show "white")))
      (should (equal "#ffffffffffff" (funcall show "black")))
      (should (equal "#0000ffffffff" (funcall show "red"))) ; cyan
      (should (equal "#0000ffff0000" (funcall show "green")))
      (should (equal "#ffffffff0000" (funcall show "blue"))) ; yellow

      (unless noninteractive
        (should (equal "#aaaaaaaaaaaa" (funcall show "#555555555555")))
        (should (equal "#999999999999" (funcall show "#666666666666")))
        (should (equal "#888888888888" (funcall show "#777777777777")))
        (should (equal "#777777777777" (funcall show "#888888888888")))
        (should (equal "#666666666666" (funcall show "#999999999999")))
        (should (equal "#aaaaaaaaaaaa" (funcall show "#aaaaaaaaaaaa"))))

      (when noninteractive
        (kill-buffer)))))

;; These are the same as the legacy version but work in terms of
;; contrast ratios.  Converting the original bounds to contrast ratios
;; (assuming pure white and black backgrounds) gives:
;;
;;   min-lum of 0.33 ~~> 1.465
;;   max-lum of 0.66 ~~> 7.666
;;
(ert-deftest erc-nicks-invert--light ()
  (let ((erc-nicks--bg-luminance 1.0)
        (erc-nicks--bg-mode-value 'light)
        (erc-nicks-contrast-range '(1.465))
        (show (lambda (c) (erc-nicks-tests--print-contrast
                           #'erc-nicks-invert c))))

    (with-current-buffer (get-buffer-create
                          "*erc-nicks--invert-classic--light*")
      (should (equal "#000000000000" (funcall show "white")))
      (should (equal "#000000000000" (funcall show "black")))
      (should (equal "#ffff00000000" (funcall show "red")))
      (should (equal "#ffff0000ffff" (funcall show "green"))) ; magenta
      (should (equal "#00000000ffff" (funcall show "blue")))

      (unless noninteractive
        (should (equal "#bbbbbbbbbbbb" (funcall show "#bbbbbbbbbbbb")))
        (should (equal "#cccccccccccc" (funcall show "#cccccccccccc")))
        (should (equal "#222122212221" (funcall show "#dddddddddddd")))
        (should (equal "#111011101110" (funcall show "#eeeeeeeeeeee"))))

      (when noninteractive
        (kill-buffer)))))

(ert-deftest erc-nicks-invert--dark ()
  (let ((erc-nicks--bg-luminance 0.0)
        (erc-nicks--bg-mode-value 'dark)
        (erc-nicks-contrast-range '(7.666))
        (show (lambda (c) (erc-nicks-tests--print-contrast
                           #'erc-nicks-invert c))))

    (with-current-buffer (get-buffer-create "*erc-nicks-invert--dark*")
      (should (equal "#ffffffffffff" (funcall show "white")))
      (should (equal "#ffffffffffff" (funcall show "black")))
      (should (equal "#0000ffffffff" (funcall show "red"))) ; cyan
      (should (equal "#0000ffff0000" (funcall show "green")))
      (should (equal "#ffffffff0000" (funcall show "blue"))) ; yellow

      (unless noninteractive
        (should (equal "#aaaaaaaaaaaa" (funcall show "#555555555555")))
        (should (equal "#999999999999" (funcall show "#666666666666")))
        (should (equal "#888888888888" (funcall show "#777777777777")))
        (should (equal "#888888888888" (funcall show "#888888888888")))
        (should (equal "#999999999999" (funcall show "#999999999999"))))

      (when noninteractive
        (kill-buffer)))))

(ert-deftest erc-nicks-add-contrast ()
  (let ((erc-nicks--bg-luminance 1.0)
        (erc-nicks--bg-mode-value 'light)
        (erc-nicks-contrast-range '(3.5))
        (show (lambda (c) (erc-nicks-tests--print-contrast
                           #'erc-nicks-add-contrast c))))

    (with-current-buffer (get-buffer-create "*erc-nicks-add-contrast*")
      (should (equal "#893a893a893a" (funcall show "white")))
      (should (equal "#893a893a893a" (funcall show "#893a893a893a")))
      (should (equal "#000000000000" (funcall show "black")))
      (should (equal "#ffff00000000" (funcall show "red")))
      (should (equal "#0000a12e0000" (funcall show "green")))
      (should (equal "#00000000ffff" (funcall show "blue")))

      ;; When the input is already near the desired ratio, the result
      ;; may not be in bounds, only close.  But the difference is
      ;; usually imperceptible.
      (unless noninteractive
        ;; Well inside (light slate gray)
        (should (equal "#777788889999" (funcall show "#777788889999")))
        ;; Slightly outside -> just outside
        (should (equal "#7c498bd39b5c" (funcall show "#88889999aaaa")))
        ;; Just outside -> just inside
        (should (equal "#7bcc8b479ac0" (funcall show "#7c498bd39b5c")))
        ;; Just inside
        (should (equal "#7bcc8b479ac0" (funcall show "#7bcc8b479ac0"))))

      (when noninteractive
        (kill-buffer)))))

(ert-deftest erc-nicks-cap-contrast ()
  (should (= 12.5 (cdr erc-nicks-contrast-range)))
  (let ((erc-nicks--bg-luminance 1.0)
        (erc-nicks--bg-mode-value 'light)
        (show (lambda (c) (erc-nicks-tests--print-contrast
                           #'erc-nicks-cap-contrast c))))

    (with-current-buffer (get-buffer-create "*erc-nicks-remove-contrast*")
      (should (equal (funcall show "black") "#34e534e534e5" )) ; 21.0 -> 12.14
      (should ; 12.32 -> 12.32 (same)
       (equal (funcall show "#34e534e534e5") "#34e534e534e5"))
      (should (equal (funcall show "white") "#ffffffffffff"))

      (unless noninteractive
        (should (equal (funcall show "DarkRed") "#8b8b00000000"))
        (should (equal (funcall show "DarkGreen") "#000064640000"))
        ;; 15.29 -> 12.38
        (should (equal (funcall show "DarkBlue") "#1cf11cf198b5"))

        ;; 12.50 -> 12.22
        (should (equal (funcall show "#33e033e033e0") "#34ab34ab34ab"))
        ;; 12.57 -> 12.28
        (should (equal (funcall show "#338033803380") "#344c344c344c"))
        ;; 12.67 -> 12.37
        (should (equal (funcall show "#330033003300") "#33cc33cc33cc")))

      (when noninteractive
        (kill-buffer)))))

;; Here is an example of how filters can steer us wrong (don't always
;; DTRT).  Two keys with similar names hash to very different values:
;;
;;   1) "awbLibera.Chat" -> #x1e3b5ca4edbc ; deep blue
;;   2) "twbLibera.Chat" -> #xdeb4c26934af ; yellow/orange
;;
;; But on a dark bg, (1) falls below `erc-nicks-invert's min threshold
;; and thus gets treated, becoming #xe1c4a35b1243, which is quite
;; close to and thus easily confused with (2).

(ert-deftest erc-nicks--hash ()
  (with-current-buffer (get-buffer-create "*erc-nicks--hash*")
    ;; Here, we're just using `erc-nicks-tests--show-contrast' for show.
    (let ((show (lambda (c) (erc-nicks-tests--print-contrast #'identity c))))

      ;; Similar nicks yielding similar colors is likely undesirable.
      (should (= (erc-nicks--hash "00000000") #xe4deaa6df385))
      (should (= (erc-nicks--hash "00000001") #xe4deaa6df386))
      (funcall show "#e4deaa6df385")
      (funcall show "#e4deaa6df386")

      ;; So we currently pad from the right to avoid this.
      (should (= (erc-nicks--hash "0Libera.Chat") #x32fdc0d63a92))
      (should (= (erc-nicks--hash "1Libera.Chat") #xc2c4f1c997f3))
      (funcall show "#32fdc0d63a92")
      (funcall show "#c2c4f1c997f3")

      (should (= (erc-nicks--hash "0       OFTC") #x6805b7521261))
      (should (= (erc-nicks--hash "1       OFTC") #xf7cce8456fc2))
      (funcall show "#6805b7521261")
      (funcall show "#f7cce8456fc2"))

    (when noninteractive
      (kill-buffer))))

(ert-deftest erc-nicks--skip-p ()
  ;; Baseline
  (should-not (erc-nicks--skip-p 'bold nil 10000000))
  (should-not (erc-nicks--skip-p '(bold) nil 10000000))
  (should-not (erc-nicks--skip-p nil '(bold) 10000000))
  (should-not (erc-nicks--skip-p 'bold '(bold) 0))
  (should-not (erc-nicks--skip-p '(bold) '(bold) 0))
  (should-not (erc-nicks--skip-p 'bold '(foo bold) 0))
  (should-not (erc-nicks--skip-p '((:inherit bold)) '(bold) 1))
  (should (erc-nicks--skip-p 'bold '(bold) 1))
  (should (erc-nicks--skip-p 'bold '(fake bold) 1))
  (should (erc-nicks--skip-p 'bold '(foo bar bold) 1))
  (should (erc-nicks--skip-p '(bold) '(bold) 1))
  (should (erc-nicks--skip-p '((bold)) '(bold) 1))
  (should (erc-nicks--skip-p '((((bold)))) '(bold) 1))
  (should (erc-nicks--skip-p '(bold) '(foo bold) 1))
  (should (erc-nicks--skip-p '(:inherit bold) '((:inherit bold)) 1))
  (should (erc-nicks--skip-p '((:inherit bold)) '((:inherit bold)) 1))
  (should (erc-nicks--skip-p '(((:inherit bold))) '((:inherit bold)) 1))

  ;; Composed
  (should-not (erc-nicks--skip-p '(italic bold) '(bold) 1))
  (should-not (erc-nicks--skip-p '((italic) bold) '(bold) 1))
  (should-not (erc-nicks--skip-p '(italic (bold)) '(bold) 1))
  (should (erc-nicks--skip-p '(italic bold) '(bold) 2))
  (should (erc-nicks--skip-p '((italic) bold) '(bold) 2))
  (should (erc-nicks--skip-p '(italic (bold)) '(bold) 2))

  (should-not (erc-nicks--skip-p '(italic default bold) '(bold) 2))
  (should-not (erc-nicks--skip-p '((default italic) bold) '(bold) 2))
  (should-not (erc-nicks--skip-p '(italic (default bold)) '(bold) 2))
  (should-not (erc-nicks--skip-p '((default italic) (bold shadow)) '(bold) 2))
  (should (erc-nicks--skip-p '((default italic) bold) '(bold) 3))
  (should (erc-nicks--skip-p '(italic (default bold)) '(bold) 3))
  (should (erc-nicks--skip-p '((default italic) (bold shadow)) '(bold) 3))
  (should (erc-nicks--skip-p '(italic (default (bold shadow))) '(bold) 3)))

;;; erc-nicks-tests.el ends here

debug log:

solving 0d640ad59c3 ...
found 0d640ad59c3 in https://yhetil.org/emacs-bugs/87r0qgknt1.fsf__15764.4733366155$1686629306$gmane$org@neverwas.me/
found e84a2fea6ce in https://yhetil.org/emacs-bugs/87wn0p6gnb.fsf__8976.16626914299$1685456794$gmane$org@neverwas.me/
found e0a5691b073 in https://yhetil.org/emacs-bugs/87v8gjrwwo.fsf__25148.7589253642$1684849113$gmane$org@neverwas.me/
found 756260d718d in https://yhetil.org/emacs-bugs/87ilcp1za1.fsf__10272.1162418433$1684420707$gmane$org@neverwas.me/

applying [1/4] https://yhetil.org/emacs-bugs/87ilcp1za1.fsf__10272.1162418433$1684420707$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-nicks-tests.el b/test/lisp/erc/erc-nicks-tests.el
new file mode 100644
index 00000000000..756260d718d


applying [2/4] https://yhetil.org/emacs-bugs/87v8gjrwwo.fsf__25148.7589253642$1684849113$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-nicks-tests.el b/test/lisp/erc/erc-nicks-tests.el
index 756260d718d..e0a5691b073 100644

Checking patch test/lisp/erc/erc-nicks-tests.el...
Applied patch test/lisp/erc/erc-nicks-tests.el cleanly.
Checking patch test/lisp/erc/erc-nicks-tests.el...
Applied patch test/lisp/erc/erc-nicks-tests.el cleanly.

skipping https://yhetil.org/emacs-bugs/87v8gjrwwo.fsf__25148.7589253642$1684849113$gmane$org@neverwas.me/ for e0a5691b073
index at:
100644 e0a5691b0735a513fcbbb707d2d69aeaff6c0b60	test/lisp/erc/erc-nicks-tests.el

applying [3/4] https://yhetil.org/emacs-bugs/87wn0p6gnb.fsf__8976.16626914299$1685456794$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-nicks-tests.el b/test/lisp/erc/erc-nicks-tests.el
index e0a5691b073..e84a2fea6ce 100644

Checking patch test/lisp/erc/erc-nicks-tests.el...
Applied patch test/lisp/erc/erc-nicks-tests.el cleanly.

skipping https://yhetil.org/emacs-bugs/87wn0p6gnb.fsf__8976.16626914299$1685456794$gmane$org@neverwas.me/ for e84a2fea6ce
index at:
100644 e84a2fea6cece5e2a5d13a97e8d94b688905ac77	test/lisp/erc/erc-nicks-tests.el

applying [4/4] https://yhetil.org/emacs-bugs/87r0qgknt1.fsf__15764.4733366155$1686629306$gmane$org@neverwas.me/
diff --git a/test/lisp/erc/erc-nicks-tests.el b/test/lisp/erc/erc-nicks-tests.el
index e84a2fea6ce..0d640ad59c3 100644

Checking patch test/lisp/erc/erc-nicks-tests.el...
Applied patch test/lisp/erc/erc-nicks-tests.el cleanly.

skipping https://yhetil.org/emacs-bugs/87r0qgknt1.fsf__15764.4733366155$1686629306$gmane$org@neverwas.me/ for 0d640ad59c3
index at:
100644 0d640ad59c3c1b21c84ff075c434cc982ab020c1	test/lisp/erc/erc-nicks-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).