;;; hi-lock-tests.el --- Tests for hi-lock.el -*- lexical-binding: t; -*- ;; Copyright (C) 2017 Free Software Foundation, Inc. ;; Author: Tino Calancha ;; 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 . ;;; 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