summaryrefslogtreecommitdiff
path: root/test/lisp/hi-lock-tests.el
blob: 199512fe7de8faaf72da5e509a5844329503fc98 (plain)
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
;;; hi-lock-tests.el --- Tests for hi-lock.el  -*- lexical-binding: t; -*-

;; Copyright (C) 2017-2021 Free Software Foundation, Inc.

;; Author: Tino Calancha <tino.calancha@gmail.com>
;; Keywords:

;; 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/>.

;;; Code:

(require 'ert)
(require 'hi-lock)

(ert-deftest hi-lock-bug26666 ()
  "Test for https://debbugs.gnu.org/26666 ."
  (let ((faces hi-lock-face-defaults))
    (with-temp-buffer
      (insert "a A b B\n")
      (cl-letf (((symbol-function 'completing-read)
                   (lambda (_prompt _coll _x _y _z _hist defaults)
                     (car defaults))))
        (dotimes (_ 2)
          (let ((face (hi-lock-read-face-name)))
            (hi-lock-set-pattern "a" face))))
      (should (equal hi-lock--unused-faces (cdr faces))))))

(ert-deftest hi-lock-test-set-pattern ()
  (let ((faces hi-lock-face-defaults))
    (with-temp-buffer
      (insert "foo bar")
      (cl-letf (((symbol-function 'completing-read)
                 (lambda (_prompt _coll _x _y _z _hist defaults)
                   (car defaults))))
        (hi-lock-set-pattern "9999" (hi-lock-read-face-name)) ; No match
        (hi-lock-set-pattern "foo" (hi-lock-read-face-name)))
      ;; Only one match, then we have used just 1 face
      (should (equal hi-lock--unused-faces (cdr faces))))))

(ert-deftest hi-lock-case-fold ()
  "Test for case-sensitivity."
  (let ((hi-lock-auto-select-face t))
    (with-temp-buffer
      (insert "a A b B\n")

      (dotimes (_ 2) (highlight-regexp "[a]"))
      (should (= (length (overlays-in (point-min) (point-max))) 2))
      (unhighlight-regexp "[a]")
      (should (= (length (overlays-in (point-min) (point-max))) 0))

      (dotimes (_ 2) (highlight-regexp "[a]" nil nil "a"))
      (should (= (length (overlays-in (point-min) (point-max))) 2))
      (unhighlight-regexp "a")
      (should (= (length (overlays-in (point-min) (point-max))) 0))

      (dotimes (_ 2) (highlight-regexp "[A]" ))
      (should (= (length (overlays-in (point-min) (point-max))) 1))
      (unhighlight-regexp "[A]")
      (should (= (length (overlays-in (point-min) (point-max))) 0))

      (dotimes (_ 2) (highlight-regexp "[A]" nil nil "A"))
      (should (= (length (overlays-in (point-min) (point-max))) 1))
      (unhighlight-regexp "A")
      (should (= (length (overlays-in (point-min) (point-max))) 0))

      (let ((case-fold-search nil)) (dotimes (_ 2) (highlight-regexp "[a]")))
      (should (= (length (overlays-in (point-min) (point-max))) 1))
      (unhighlight-regexp "[a]")
      (should (= (length (overlays-in (point-min) (point-max))) 0))

      (dotimes (_ 2) (highlight-phrase "a   a"))
      (should (= (length (overlays-in (point-min) (point-max))) 1))
      (unhighlight-regexp "a   a")
      (should (= (length (overlays-in (point-min) (point-max))) 0))

      (let ((search-spaces-regexp search-whitespace-regexp)) (highlight-regexp "a   a"))
      (should (= (length (overlays-in (point-min) (point-max))) 1))
      (cl-letf (((symbol-function 'completing-read)
                 (lambda (_prompt _coll _x _y _z _hist defaults)
                   (car defaults))))
        (call-interactively 'unhighlight-regexp))
      (should (= (length (overlays-in (point-min) (point-max))) 0))

      (emacs-lisp-mode)
      (setq font-lock-mode t)

      (dotimes (_ 2) (highlight-regexp "[a]"))
      (font-lock-ensure)
      (should (memq 'hi-yellow (get-text-property 1 'face)))
      (should (memq 'hi-yellow (get-text-property 3 'face)))
      (let ((font-lock-fontified t)) (unhighlight-regexp "[a]"))
      (should (null (get-text-property 3 'face)))

      (dotimes (_ 2) (highlight-regexp "[a]" nil nil "a"))
      (font-lock-ensure)
      (should (memq 'hi-yellow (get-text-property 1 'face)))
      (should (memq 'hi-yellow (get-text-property 3 'face)))
      (let ((font-lock-fontified t)) (unhighlight-regexp "a"))
      (should (null (get-text-property 3 'face)))

      (dotimes (_ 2) (highlight-regexp "[A]" ))
      (font-lock-ensure)
      (should (null (get-text-property 1 'face)))
      (should (memq 'hi-yellow (get-text-property 3 'face)))
      (let ((font-lock-fontified t)) (unhighlight-regexp "[A]"))
      (should (null (get-text-property 3 'face)))

      (dotimes (_ 2) (highlight-regexp "[A]" nil nil "A"))
      (font-lock-ensure)
      (should (null (get-text-property 1 'face)))
      (should (memq 'hi-yellow (get-text-property 3 'face)))
      (let ((font-lock-fontified t)) (unhighlight-regexp "A"))
      (should (null (get-text-property 3 'face)))

      (let ((case-fold-search nil)) (dotimes (_ 2) (highlight-regexp "[a]")))
      (font-lock-ensure)
      (should (memq 'hi-yellow (get-text-property 1 'face)))
      (should (null (get-text-property 3 'face)))
      (let ((font-lock-fontified t)) (unhighlight-regexp "[a]"))
      (should (null (get-text-property 1 'face)))

      (dotimes (_ 2) (highlight-phrase "a   a"))
      (font-lock-ensure)
      (should (memq 'hi-yellow (get-text-property 1 'face)))
      (let ((font-lock-fontified t)) (unhighlight-regexp "a   a"))
      (should (null (get-text-property 1 'face)))

      (let ((search-spaces-regexp search-whitespace-regexp)) (highlight-regexp "a   a"))
      (font-lock-ensure)
      (should (memq 'hi-yellow (get-text-property 1 'face)))
      (cl-letf (((symbol-function 'completing-read)
                 (lambda (_prompt _coll _x _y _z _hist defaults)
                   (car defaults)))
                (font-lock-fontified t))
        (call-interactively 'unhighlight-regexp))
      (should (null (get-text-property 1 'face))))))

(ert-deftest hi-lock-unhighlight ()
  "Test for unhighlighting and `hi-lock--regexps-at-point'."
  (let ((hi-lock-auto-select-face t))
    (with-temp-buffer
      (insert "aAbB\n")

      (cl-letf (((symbol-function 'completing-read)
                 (lambda (_prompt _coll _x _y _z _hist defaults)
                   (car defaults))))

        (highlight-regexp "a")
        (highlight-regexp "b")
        (should (= (length (overlays-in (point-min) (point-max))) 4))
        ;; `hi-lock--regexps-at-point' should take regexp "a" at point 1,
        ;; not the last regexp "b"
        (goto-char 1)
        (call-interactively 'unhighlight-regexp)
        (should (= (length (overlays-in 1 3)) 0))
        (should (= (length (overlays-in 3 5)) 2))
        ;; Next call should unhighlight remaining regepxs
        (call-interactively 'unhighlight-regexp)
        (should (= (length (overlays-in 3 5)) 0))

        ;; Test unhighlight all
        (highlight-regexp "a")
        (highlight-regexp "b")
        (should (= (length (overlays-in (point-min) (point-max))) 4))
        (unhighlight-regexp t)
        (should (= (length (overlays-in (point-min) (point-max))) 0))

        (emacs-lisp-mode)
        (setq font-lock-mode t)

        (highlight-regexp "a")
        (highlight-regexp "b")
        (font-lock-ensure)
        (should (memq 'hi-yellow (get-text-property 1 'face)))
        (should (memq 'hi-yellow (get-text-property 3 'face)))
        ;; `hi-lock--regexps-at-point' should take regexp "a" at point 1,
        ;; not the last regexp "b"
        (goto-char 1)
        (let ((font-lock-fontified t)) (call-interactively 'unhighlight-regexp))
        (should (null (get-text-property 1 'face)))
        (should (memq 'hi-yellow (get-text-property 3 'face)))
        ;; Next call should unhighlight remaining regepxs
        (let ((font-lock-fontified t)) (call-interactively 'unhighlight-regexp))
        (should (null (get-text-property 3 'face)))

        ;; Test unhighlight all
        (highlight-regexp "a")
        (highlight-regexp "b")
        (font-lock-ensure)
        (should (memq 'hi-yellow (get-text-property 1 'face)))
        (should (memq 'hi-yellow (get-text-property 3 'face)))
        (let ((font-lock-fontified t)) (unhighlight-regexp t))
        (should (null (get-text-property 1 'face)))
        (should (null (get-text-property 3 'face)))))))

(provide 'hi-lock-tests)
;;; hi-lock-tests.el ends here