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
| | ;;; hi-lock-tests.el --- Tests for hi-lock.el -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Free Software Foundation, Inc.
;; Author: Tino Calancha <tino.calancha@gmail.com>
;; Keywords:
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Code:
(require 'ert)
(require 'hi-lock)
(eval-when-compile (require 'cl-lib))
(defun hi-lock--count (face)
(let ((count 0))
(save-excursion
(goto-char (point-min))
(dolist (ov (car (overlay-lists)))
(let ((props (memq 'face (overlay-properties ov))))
(when (eq (cadr props) face)
(cl-incf count)))))
count))
(defun hi-lock--highlight-and-count (regexp face case-fold)
"Highlight REGEXP with FACE with case fold CASE-FOLD.
Return number of matches."
(hi-lock-unface-buffer t)
(should (eq 0 (hi-lock--count face)))
(hi-lock-face-buffer regexp face case-fold)
(hi-lock--count face))
(defun hi-lock--interactive-test-1 (regexp face res ucase cfold)
(hi-lock-unface-buffer t)
(should (eq 0 (hi-lock--count face)))
(cl-letf (((symbol-function 'read-regexp)
(lambda (x y) (ignore x y) regexp))
((symbol-function 'hi-lock-read-face-name)
(lambda () face)))
(setq search-upper-case ucase
case-fold-search cfold)
(call-interactively 'hi-lock-face-buffer)
(should (= res (hi-lock--count face)))))
;; Interactive test should not depend on the major mode.
(defun hi-lock--interactive-test (regexp face res ucase cfold)
(lisp-interaction-mode)
(hi-lock--interactive-test-1 regexp face res ucase cfold)
(fundamental-mode)
(hi-lock--interactive-test-1 regexp face res ucase cfold))
;; In batch calls to `hi-lock-face-buffer', case is given by
;; its third argument. In interactive calls, case depends
;; on `search-upper-case' and `case-fold-search'.
(ert-deftest hi-lock-face-buffer-test ()
"Test for http://debbugs.gnu.org/22541 ."
(let ((face 'hi-yellow)
(regexp "a")
case-fold-search search-upper-case)
(with-temp-buffer
(insert "a A\n")
(should (= 1 (hi-lock--highlight-and-count regexp face nil)))
(should (= 2 (hi-lock--highlight-and-count regexp face t)))
;; Case depends on the regexp.
(hi-lock--interactive-test regexp face 2 t nil)
(hi-lock--interactive-test "A" face 1 t nil)
(hi-lock--interactive-test "\\A" face 2 t nil)
;; Case depends on `case-fold-search'.
(hi-lock--interactive-test "a" face 1 nil nil)
(hi-lock--interactive-test "A" face 1 nil nil)
(hi-lock--interactive-test "\\A" face 1 nil nil)
;;
(hi-lock--interactive-test "a" face 2 nil t)
(hi-lock--interactive-test "A" face 2 nil t)
(hi-lock--interactive-test "\\A" face 2 nil t))))
(provide 'hi-lock-tests)
;;; hi-lock-tests.el ends here
|