summaryrefslogtreecommitdiff
path: root/test/lisp/emacs-lisp/faceup-tests/faceup-test-basics.el
blob: fa3372fa24098f283c671eb09bc9841ed7bfc530 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
;;; faceup-test-basics.el --- Tests for the `faceup' package.

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

;; Author: Anders Lindgren
;; Keywords: languages, faces

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

;;; Commentary:

;; Basic tests for the `faceup' package.

;;; Code:

(eval-when-compile (require 'cl-lib))
(require 'faceup)

(ert-deftest faceup-functions ()
  "Test primitive functions."
  (should (equal (faceup-normalize-face-property '()) '()))
  (should (equal (faceup-normalize-face-property 'a) '(a)))
  (should (equal (faceup-normalize-face-property '(a)) '(a)))
  (should (equal (faceup-normalize-face-property '(:x t)) '((:x t))))
  (should (equal (faceup-normalize-face-property '(:x t a)) '((:x t))))
  (should (equal (faceup-normalize-face-property '(:x t a b)) '((:x t))))
  (should (equal (faceup-normalize-face-property '(a :x t)) '(a (:x t))))
  (should (equal (faceup-normalize-face-property '(a b :x t))
                 '(a b (:x t))))

  (should (equal (faceup-normalize-face-property '(:x t :y nil))
                 '((:y nil) (:x t))))
  (should (equal (faceup-normalize-face-property '(:x t :y nil a))
                 '((:y nil) (:x t))))
  (should (equal (faceup-normalize-face-property '(:x t  :y nil a b))
                 '((:y nil) (:x t))))
  (should (equal (faceup-normalize-face-property '(a :x t :y nil))
                 '(a (:y nil) (:x t))))
  (should (equal (faceup-normalize-face-property '(a b :x t :y nil))
                 '(a b (:y nil) (:x t)))))


(ert-deftest faceup-markup-basics ()
  (should (equal (faceup-markup-string "")     ""))
  (should (equal (faceup-markup-string "test") "test")))

(ert-deftest faceup-markup-escaping ()
  (should (equal (faceup-markup-string "«") "««"))
  (should (equal (faceup-markup-string "«A«B«C«") "««A««B««C««"))
  (should (equal (faceup-markup-string "»") "«»"))
  (should (equal (faceup-markup-string "»A»B»C»") "«»A«»B«»C«»")))

(ert-deftest faceup-markup-plain ()
  ;;   UU
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF" 2 4 (face underline)))
                 "AB«U:CD»EF")))

(ert-deftest faceup-markup-plain-full-text ()
  ;; UUUUUU
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF" 0 6 (face underline)))
                 "«U:ABCDEF»")))

(ert-deftest faceup-markup-anonymous-face ()
  ;;   AA
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF" 2 4 (face (:underline t))))
                 "AB«:(:underline t):CD»EF")))

(ert-deftest faceup-markup-anonymous-face-2keys ()
  ;;   AA
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF" 2 4 (face (:foo t :bar nil))))
                 "AB«:(:foo t):«:(:bar nil):CD»»EF"))
  ;; Plist in list.
  (should (equal (faceup-markup-string
                  #("ABCDEF" 2 4 (face ((:foo t :bar nil)))))
                 "AB«:(:foo t):«:(:bar nil):CD»»EF"))
  ;; Two plists.
  (should (equal (faceup-markup-string
                  #("ABCDEF" 2 4 (face ((:foo t) (:bar nil)))))
                 "AB«:(:bar nil):«:(:foo t):CD»»EF")))

(ert-deftest faceup-markup-anonymous-nested ()
  ;;   AA
  ;;  IIII
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF"
                    1 2 (face ((:foo t)))
                    2 4 (face ((:bar t) (:foo t)))
                    4 5 (face ((:foo t)))))
                 "A«:(:foo t):B«:(:bar t):CD»E»F")))

(ert-deftest faceup-markup-nested ()
  ;;   UU
  ;;  IIII
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF"
                    1 2 (face italic)
                    2 4 (face (underline italic))
                    4 5 (face italic)))
                 "A«I:B«U:CD»E»F")))

(ert-deftest faceup-markup-overlapping ()
  ;;   UUU
  ;;  III
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF"
                    1 2 (face italic)
                    2 4 (face (underline italic))
                    4 5 (face underline)))
                 "A«I:B«U:CD»»«U:E»F"))
  ;;  III
  ;;   UUU
  ;; ABCDEF
  (should (equal (faceup-markup-string
                  #("ABCDEF"
                    1 2 (face italic)
                    2 4 (face (italic underline))
                    4 5 (face underline)))
                 "A«I:B»«U:«I:CD»E»F")))

(ert-deftest faceup-markup-multi-face ()
  ;; More than one face at the same location.
  ;;
  ;; The property to the front takes precedence, it is rendered as the
  ;; innermost parenthesis pair.
  (should (equal (faceup-markup-string
                  #("ABCDEF" 2 4 (face (underline italic))))
                 "AB«I:«U:CD»»EF"))
  (should (equal (faceup-markup-string
                  #("ABCDEF" 2 4 (face (italic underline))))
                 "AB«U:«I:CD»»EF"))
  ;; Equal ranges, full text.
  (should (equal (faceup-markup-string
                  #("ABCDEF" 0 6 (face (underline italic))))
                 "«I:«U:ABCDEF»»"))
  ;; Ditto, with stray markup characters.
  (should (equal (faceup-markup-string
                  #("AB«CD»EF" 0 8 (face (underline italic))))
                 "«I:«U:AB««CD«»EF»»")))

(ert-deftest faceup-markup-multi-property ()
  (let ((faceup-properties '(alpha beta gamma)))
    ;; One property.
    (should (equal (faceup-markup-string
                    #("ABCDEF" 2 4 (alpha (a l p h a))))
                   "AB«(alpha):(a l p h a):CD»EF"))

    ;; Two properties, inner enclosed.
    (should (equal (faceup-markup-string
                    (let ((s (copy-sequence "ABCDEFGHIJ")))
                      (set-text-properties 2 8 '(alpha (a l p h a)) s)
                      (font-lock-append-text-property 4 6 'beta '(b e t a) s)
                      s))
                   "AB«(alpha):(a l p h a):CD«(beta):(b e t a):EF»GH»IJ"))

    ;; Two properties, same end
    (should (equal (faceup-markup-string
                    (let ((s (copy-sequence "ABCDEFGH")))
                      (set-text-properties 2 6 '(alpha (a)) s)
                      (add-text-properties 4 6 '(beta (b)) s)
                      s))
                   "AB«(alpha):(a):CD«(beta):(b):EF»»GH"))

    ;; Two properties, overlap.
    (should (equal (faceup-markup-string
                    (let ((s (copy-sequence "ABCDEFGHIJ")))
                      (set-text-properties 2 6 '(alpha (a)) s)
                      (add-text-properties 4 8 '(beta (b)) s)
                      s))
                   "AB«(alpha):(a):CD«(beta):(b):EF»»«(beta):(b):GH»IJ"))))


(ert-deftest faceup-clean ()
  "Test the clean features of `faceup'."
  (should (equal (faceup-clean-string "")     ""))
  (should (equal (faceup-clean-string "test") "test"))
  (should (equal (faceup-clean-string "AB«U:CD»EF")         "ABCDEF"))
  (should (equal (faceup-clean-string "«U:ABCDEF»")         "ABCDEF"))
  (should (equal (faceup-clean-string "A«I:B«U:CD»E»F")     "ABCDEF"))
  (should (equal (faceup-clean-string "A«I:B«U:CD»»«U:E»F") "ABCDEF"))
  (should (equal (faceup-clean-string "AB«I:«U:CD»»EF")     "ABCDEF"))
  (should (equal (faceup-clean-string "«I:«U:ABCDEF»»")     "ABCDEF"))
  (should (equal (faceup-clean-string "«(foo)I:ABC»DEF")    "ABCDEF"))
  (should (equal (faceup-clean-string "«:(:foo t):ABC»DEF") "ABCDEF"))
  ;; Escaped markup characters.
  (should (equal (faceup-clean-string "««") "«"))
  (should (equal (faceup-clean-string "«»") "»"))
  (should (equal (faceup-clean-string "A«I:B«U:CD»«»»«U:E»F") "ABCD»EF")))


(ert-deftest faceup-render ()
  "Test the render features of `faceup'."
  (should (equal (faceup-render-string "")     ""))
  (should (equal (faceup-render-string "««") "«"))
  (should (equal (faceup-render-string "«»") "»"))
  (should (equal (faceup-render-string "A«I:B«U:CD»«»»«U:E»F") "ABCD»EF")))


(defvar faceup-test-resources-directory
  (concat (file-name-directory
           (substring (faceup-this-file-directory) 0 -1))
          "faceup-resources/")
  "The `faceup-resources' directory.")


(defvar faceup-test-this-file-directory nil
  "The result of `faceup-this-file-directory' in various contexts.

This is set by the file test support file
`faceup-test-this-file-directory.el'.")


(ert-deftest faceup-directory ()
  "Test `faceup-this-file-directory'."
  (let ((file (concat faceup-test-resources-directory
                      "faceup-test-this-file-directory.el"))
        (load-file-name nil))
    ;; Test normal load.
    (makunbound 'faceup-test-this-file-directory)
    (load file nil :nomessage)
    (should (equal faceup-test-this-file-directory
                   faceup-test-resources-directory))
    ;; Test `eval-buffer'.
    (makunbound 'faceup-test-this-file-directory)
    (save-excursion
      (find-file file)
      (eval-buffer))
    (should (equal faceup-test-this-file-directory
                   faceup-test-resources-directory))
    ;; Test `eval-defun'.
    (makunbound 'faceup-test-this-file-directory)
    (save-excursion
      (find-file file)
      (save-excursion
        (goto-char (point-min))
        (while (not (eobp))
          ;; Note: In batch mode, this prints the result of the
          ;; evaluation.  Unfortunately, this is hard to fix.
          (eval-defun nil)
          (forward-sexp))))
    (should (equal faceup-test-this-file-directory
                   faceup-test-resources-directory))))

(provide 'faceup-test-basics)

;;; faceup-test-basics.el ends here