all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 1d97e1f05451ca09dc8fffbf96b24b2052f2058d 4951 bytes (raw)
name: test/lisp/hi-lock-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
 
;;; 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))

(ert-deftest hi-lock-bug26666 ()
  "Test for http://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))))))

(defun hi-lock--count (face)
  (let ((count 0))
    (save-excursion
      (goto-char (point-min))
      (cond ((and font-lock-mode (font-lock-specified-p major-mode))
             (when (and (consp (get-text-property (point) 'face))
                        (memq 'hi-yellow (get-text-property (point) 'face)))
               (cl-incf count))
             (while (next-property-change (point))
               (goto-char (next-property-change (point)))
               (when (and (consp (get-text-property (point) 'face))
                          (memq 'hi-yellow (get-text-property (point) 'face)))
                 (cl-incf count))))
            (t
             (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))))

(ert-deftest hi-lock-bug22520 ()
  "Test for http://debbugs.gnu.org/22520 ."
  (with-temp-buffer
    (erase-buffer)
    (insert "foo and Foo")
    (dolist (ucase '(nil t))
      (dolist (cfold '(nil t))
        (let ((res (cond ((null ucase)
                          (if cfold 2 1))
                         (t 2))))
        (hi-lock--interactive-test "f" 'hi-yellow res ucase cfold)
        (hi-lock-unface-buffer "f")
        (should (= 0 (hi-lock--count 'hi-yellow))))))))

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

debug log:

solving 1d97e1f054 ...
found 1d97e1f054 in https://yhetil.org/emacs/87bmrilylu.fsf@calancha-pc/
found 2cb662cfac in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 2cb662cfaca06be0480c950ed420175a391c17c1	test/lisp/hi-lock-tests.el

applying [1/1] https://yhetil.org/emacs/87bmrilylu.fsf@calancha-pc/
diff --git a/test/lisp/hi-lock-tests.el b/test/lisp/hi-lock-tests.el
index 2cb662cfac..1d97e1f054 100644

Checking patch test/lisp/hi-lock-tests.el...
Applied patch test/lisp/hi-lock-tests.el cleanly.

index at:
100644 1d97e1f05451ca09dc8fffbf96b24b2052f2058d	test/lisp/hi-lock-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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.