unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 756260d718d2b1f29ac8a11f4dc7f31e8a3e315c 6315 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
 
;;; 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:

;; 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)

(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 ()
  (let ((erc-nicks--bg-mode-value 'dark))
    (should (equal (erc-nicks-invert "white") "white"))
    (should (equal (erc-nicks-invert "black") "#ffffffffffff"))
    (should (equal (erc-nicks-invert "green") "green")))
  (let ((erc-nicks--bg-mode-value 'light))
    (should (equal (erc-nicks-invert "white") "#000000000000"))
    (should (equal (erc-nicks-invert "black") "black"))
    (should (equal (erc-nicks-invert "green") "#ffff0000ffff"))))

(defun erc-nicks-tests--show-contrast (color)
  (let ((result (erc-nicks-add-contrast color))
        (fg (if (eq 'dark erc-nicks--bg-mode-value) "white" "black"))
        (start (point)))
    (insert (format "%16s%-16s%16s%-16s\n"
                    (concat color "-")
                    (concat ">" result)
                    (concat color " ")
                    (concat " " result)))
    (put-text-property start (+ start 32) 'face
                       (list :foreground fg))
    (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-add-contrast ()
  (let ((erc-nicks--bg-luminance 1.0)
        (erc-nicks--bg-mode-value 'light))

    (with-current-buffer (get-buffer-create "*erc-nicks-add-contrast*")
      (should (equal "#893a893a893a" (erc-nicks-tests--show-contrast "white")))
      (should (equal "#893a893a893a"
                     (erc-nicks-tests--show-contrast "#893a893a893a")))
      (should (equal "#000000000000" (erc-nicks-tests--show-contrast "black")))
      (should (equal "#ffff00000000" (erc-nicks-tests--show-contrast "red")))
      (should (equal "#0000a12e0000" (erc-nicks-tests--show-contrast "green")))
      (should (equal "#00000000ffff" (erc-nicks-tests--show-contrast "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
        (should (equal "#777788889999" ; well inside (light slate gray)
                       (erc-nicks-tests--show-contrast "#777788889999")))
        (should (equal "#7c498bd39b5c" ; slightly outside -> just outside
                       (erc-nicks-tests--show-contrast "#88889999aaaa")))
        (should (equal "#7bcc8b479ac0" ; just outside -> just inside
                       (erc-nicks-tests--show-contrast "#7c498bd39b5c")))
        (should (equal "#7bcc8b479ac0" ; just inside
                       (erc-nicks-tests--show-contrast "#7bcc8b479ac0"))))

      (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*")
    ;; Similar nicks yielding similar colors is likely undesirable.
    (should (= (erc-nicks--hash "00000000") #xe4deaa6df385))
    (should (= (erc-nicks--hash "00000001") #xe4deaa6df386))
    (erc-nicks-tests--show-contrast "#e4deaa6df385")
    (erc-nicks-tests--show-contrast "#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))
    (erc-nicks-tests--show-contrast "#32fdc0d63a92")
    (erc-nicks-tests--show-contrast "#c2c4f1c997f3")

    (should (= (erc-nicks--hash "0       OFTC") #x6805b7521261))
    (should (= (erc-nicks--hash "1       OFTC") #xf7cce8456fc2))
    (erc-nicks-tests--show-contrast "#6805b7521261")
    (erc-nicks-tests--show-contrast "#f7cce8456fc2")
    (when noninteractive
      (kill-buffer))))

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

debug log:

solving 756260d718d ...
found 756260d718d in https://yhetil.org/emacs-bugs/87ilcp1za1.fsf__10272.1162418433$1684420707$gmane$org@neverwas.me/

applying [1/1] 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

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

index at:
100644 756260d718d2b1f29ac8a11f4dc7f31e8a3e315c	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).