summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp/text-property-search-tests.el
blob: 47db54a0512fecfaced47f1de1bc9f8d0cba0963 (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
;;; text-property-search-tests.el --- Testing text-property-search

;; Copyright (C) 2018-2019 Free Software Foundation, Inc.

;; Author: Lars Ingebrigtsen <larsi@gnus.org>
;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'ert)
(require 'text-property-search)
(require 'cl-lib)

(defun text-property-setup ()
  (insert "This is "
          (propertize "bold1" 'face 'bold)
          " and this is "
          (propertize "italic1" 'face 'italic)
          (propertize "bold2" 'face 'bold)
          (propertize "italic2" 'face 'italic)
          " at the end")
  (goto-char (point-min)))

(defmacro with-test (form result &optional point)
  `(with-temp-buffer
     (text-property-setup)
     (when ,point
       (goto-char ,point))
     (should
      (equal
       (cl-loop for match = ,form
                while match
                collect (buffer-substring (prop-match-beginning match)
                                          (prop-match-end match)))
       ,result))))

(ert-deftest text-property-search-forward-bold-t ()
  (with-test (text-property-search-forward 'face 'bold t)
             '("bold1" "bold2")))

(ert-deftest text-property-search-forward-bold-nil ()
  (with-test (text-property-search-forward 'face 'bold nil)
             '("This is " " and this is italic1" "italic2 at the end")))

(ert-deftest text-property-search-forward-nil-t ()
  (with-test (text-property-search-forward 'face nil t)
             '("This is " " and this is " " at the end")))

(ert-deftest text-property-search-forward-nil-nil ()
  (with-test (text-property-search-forward 'face nil nil)
             '("bold1" "italic1" "bold2" "italic2")))

(ert-deftest text-property-search-forward-partial-bold-t ()
  (with-test (text-property-search-forward 'face 'bold t)
             '("old1" "bold2")
             10))

(ert-deftest text-property-search-forward-partial-non-current-bold-t ()
  (with-test (text-property-search-forward 'face 'bold t t)
             '("bold2")
             10))


(ert-deftest text-property-search-backward-bold-t ()
  (with-test (text-property-search-backward 'face 'bold t)
             '("bold2" "bold1")
             (point-max)))

(ert-deftest text-property-search-backward-bold-nil ()
  (with-test (text-property-search-backward 'face 'bold nil)
             '( "italic2 at the end" " and this is italic1" "This is ")
             (point-max)))

(ert-deftest text-property-search-backward-nil-t ()
  (with-test (text-property-search-backward 'face nil t)
             '(" at the end" " and this is " "This is ")
             (point-max)))

(ert-deftest text-property-search-backward-nil-nil ()
  (with-test (text-property-search-backward 'face nil nil)
             '("italic2" "bold2" "italic1" "bold1")
             (point-max)))

(ert-deftest text-property-search-backward-partial-bold-t ()
  (with-test (text-property-search-backward 'face 'bold t)
             '("b" "bold1")
             35))

(ert-deftest text-property-search-backward-partial-non-current-bold-t ()
  (with-test (text-property-search-backward 'face 'bold t t)
             '("bold1")
             35))

(provide 'text-property-search-tests)

;;; text-property-search-tests.el ends here