all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob f3d42437216e881d29d3ab8a37f892df825387f4 13868 bytes (raw)
name: test/lisp/textmodes/css-mode-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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
 
;;; css-mode-tests.el --- Test suite for CSS mode  -*- lexical-binding: t; -*-

;; Copyright (C) 2016-2017 Free Software Foundation, Inc.

;; Author: Simen Heggestøyl <simenheg@gmail.com>
;; Keywords: internal

;; This file is part of GNU Emacs.

;; 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 'css-mode)
(require 'ert)
(require 'seq)

(ert-deftest css-test-property-values ()
  ;; The `float' property has a flat value list.
  (should
   (equal (seq-sort #'string-lessp (css--property-values "float"))
          '("left" "none" "right")))

  ;; The `list-style' property refers to several other properties.
  (should
   (equal (seq-sort #'string-lessp (css--property-values "list-style"))
          (seq-sort
           #'string-lessp
           (seq-uniq
            (append (css--property-values "list-style-type")
                    (css--property-values "list-style-position")
                    (css--property-values "list-style-image"))))))

  ;; The `position' property is tricky because it's also the name of a
  ;; value class.
  (should
   (equal (seq-sort #'string-lessp (css--property-values "position"))
          '("absolute" "fixed" "relative" "static")))

  ;; The `background-position' property should refer to the `position'
  ;; value class, not the property of the same name.
  (should
   (equal (css--property-values "background-position")
          (css--value-class-lookup 'position)))

  ;; Check that the `color' property doesn't cause infinite recursion
  ;; because it refers to the value class of the same name.
  (should (= (length (css--property-values "color")) 152)))

(ert-deftest css-test-property-value-cache ()
  "Test that `css--property-value-cache' is in use."
  (should-not (gethash "word-wrap" css--property-value-cache))
  (let ((word-wrap-values (css--property-values "word-wrap")))
    (should (equal (gethash "word-wrap" css--property-value-cache)
                   word-wrap-values))))

(ert-deftest css-test-property-values-no-duplicates ()
  "Test that `css--property-values' returns no duplicates."
  ;; The `flex' property is prone to duplicate values; if they aren't
  ;; removed, it'll contain at least two instances of `auto'.
  (should
   (equal (seq-sort #'string-lessp (css--property-values "flex"))
          '("auto" "calc()" "content" "none"))))

(ert-deftest css-test-value-class-lookup ()
  (should
   (equal (seq-sort #'string-lessp (css--value-class-lookup 'position))
          '("bottom" "calc()" "center" "left" "right" "top"))))

(ert-deftest css-test-current-defun-name ()
  (with-temp-buffer
    (insert "body { top: 0; }")
    (goto-char 7)
    (should (equal (css-current-defun-name) "body"))
    (goto-char 18)
    (should (equal (css-current-defun-name) "body"))))

(ert-deftest css-test-current-defun-name-nested ()
  (with-temp-buffer
    (insert "body > .main a { top: 0; }")
    (goto-char 20)
    (should (equal (css-current-defun-name) "body > .main a"))))

(ert-deftest css-test-current-defun-name-complex ()
  (with-temp-buffer
    (insert "input[type=submit]:hover { color: red; }")
    (goto-char 30)
    (should (equal (css-current-defun-name)
                   "input[type=submit]:hover"))))

;;; Completion

(defun css-mode-tests--completions ()
  (let ((data (css-completion-at-point)))
    (all-completions (buffer-substring (nth 0 data) (nth 1 data))
                     (nth 2 data))))

(ert-deftest css-test-complete-bang-rule ()
  (with-temp-buffer
    (css-mode)
    (insert "body { left: 0 !")
    (let ((completions (css-mode-tests--completions)))
      (should (member "important" completions))
      ;; Don't include SCSS bang-rules
      (should-not (member "default" completions)))))

(ert-deftest scss-test-complete-bang-rule ()
  (with-temp-buffer
    (scss-mode)
    (insert "body { left: 0 !")
    (let ((completions (css-mode-tests--completions)))
      (should (member "important" completions))
      (should (member "default" completions)))))

(ert-deftest css-test-complete-property-value ()
  (with-temp-buffer
    (css-mode)
    (insert "body { position: ")
    (let ((completions (css-mode-tests--completions)))
      (should
       (equal (seq-sort #'string-lessp completions)
              '("absolute" "fixed" "inherit" "initial" "relative"
                "static" "unset"))))))

(ert-deftest css-test-complete-pseudo-class ()
  (with-temp-buffer
    (css-mode)
    (insert "body:a")
    (let ((completions (css-mode-tests--completions)))
      (should (member "active" completions))
      (should-not (member "disabled" completions))
      ;; Don't include pseudo-elements
      (should-not (member "after" completions)))))

(ert-deftest css-test-complete-pseudo-element ()
  (with-temp-buffer
    (css-mode)
    (insert "body::a")
    (let ((completions (css-mode-tests--completions)))
      (should (member "after" completions))
      (should-not (member "disabled" completions))
      ;; Don't include pseudo-classes
      (should-not (member "active" completions)))))

(ert-deftest css-test-complete-at-rule ()
  (with-temp-buffer
    (css-mode)
    (insert "@m")
    (let ((completions (css-mode-tests--completions)))
      (should (member "media" completions))
      (should-not (member "keyframes" completions))
      ;; Don't include SCSS at-rules
      (should-not (member "mixin" completions)))))

(ert-deftest scss-test-complete-at-rule ()
  (with-temp-buffer
    (scss-mode)
    (insert "@m")
    (let ((completions (css-mode-tests--completions)))
      (should (member "media" completions))
      (should-not (member "keyframes" completions))
      (should (member "mixin" completions)))))

(ert-deftest css-test-complete-property ()
  (with-temp-buffer
    (css-mode)
    (insert "body { f")
    (let ((completions (css-mode-tests--completions)))
      (should (member "filter" completions))
      (should-not (member "position" completions))))
  ;; Bug#27392
  (with-temp-buffer
    (css-mode)
    (insert "html { grid")
    (should (> (length (css-mode-tests--completions)) 0))))

(ert-deftest css-test-foreign-completions ()
  (let ((other-buffer-1 (generate-new-buffer "1"))
        (other-buffer-2 (generate-new-buffer "2")))
    (with-current-buffer other-buffer-1
      (setq-local css-class-list-function (lambda () '("foo" "bar"))))
    (with-current-buffer other-buffer-2
      (setq-local css-class-list-function (lambda () '("bar" "baz"))))
    (let ((completions
           (css--foreign-completions 'css-class-list-function)))
      ;; Completions from `other-buffer-1' and `other-buffer-2' should
      ;; be merged.
      (should (equal (seq-sort #'string-lessp completions)
                     '("bar" "baz" "foo"))))
    (kill-buffer other-buffer-1)
    (kill-buffer other-buffer-2)))

(ert-deftest css-test-complete-selector-tag ()
  (with-temp-buffer
    (css-mode)
    (insert "b")
    (let ((completions (css-mode-tests--completions)))
      (should (member "body" completions))
      (should-not (member "article" completions)))))

(ert-deftest css-test-complete-selector-class ()
  (with-temp-buffer
    (setq-local css-class-list-function (lambda () '("foo" "bar")))
    (with-temp-buffer
      (css-mode)
      (insert ".f")
      (let ((completions (css-mode-tests--completions)))
        (should (equal completions '("foo")))))))

(ert-deftest css-test-complete-selector-id ()
  (with-temp-buffer
    (setq-local css-id-list-function (lambda () '("foo" "bar")))
    (with-temp-buffer
      (css-mode)
      (insert "#b")
      (let ((completions (css-mode-tests--completions)))
        (should (equal completions '("bar")))))))

(ert-deftest css-test-complete-nested-selector ()
  (with-temp-buffer
    (css-mode)
    (insert "body {")
    (let ((completions (css-mode-tests--completions)))
      (should-not (member "body" completions)))))

(ert-deftest scss-test-complete-nested-selector ()
  (with-temp-buffer
    (scss-mode)
    (insert "body { b")
    (let ((completions (css-mode-tests--completions)))
      (should (member "body" completions))
      (should-not (member "article" completions)))))

(ert-deftest css-test-color-to-4-dpc ()
  (should (equal (css--color-to-4-dpc "#ffffff")
                 (css--color-to-4-dpc "#fff")))
  (should (equal (css--color-to-4-dpc "#aabbcc")
                 (css--color-to-4-dpc "#abc")))
  (should (equal (css--color-to-4-dpc "#fab")
                 "#ffffaaaabbbb"))
  (should (equal (css--color-to-4-dpc "#fafbfc")
                 "#fafafbfbfcfc")))

(ert-deftest css-test-named-color-to-hex ()
  (dolist (item '(("black" "#000000")
                  ("white" "#ffffff")
                  ("salmon" "#fa8072")))
    (with-temp-buffer
      (css-mode)
      (insert (nth 0 item))
      (css--named-color-to-hex)
      (should (equal (buffer-string) (nth 1 item))))))

(ert-deftest css-test-format-rgba-alpha ()
  (should (equal (css--format-rgba-alpha 0) "0"))
  (should (equal (css--format-rgba-alpha 0.0) "0"))
  (should (equal (css--format-rgba-alpha 1) "1"))
  (should (equal (css--format-rgba-alpha 1.0) "1"))
  (should (equal (css--format-rgba-alpha 0.10000) "0.1"))
  (should (equal (css--format-rgba-alpha 0.100001) "0.1"))
  (should (equal (css--format-rgba-alpha 0.2524334) "0.25")))

(ert-deftest css-test-hex-to-rgb ()
  (dolist (item '(("#000" "rgb(0, 0, 0)")
                  ("#000000" "rgb(0, 0, 0)")
                  ("#fff" "rgb(255, 255, 255)")
                  ("#ffffff" "rgb(255, 255, 255)")
                  ("#ffffff80" "rgba(255, 255, 255, 0.5)")
                  ("#fff8" "rgba(255, 255, 255, 0.5)")))
    (with-temp-buffer
      (css-mode)
      (insert (nth 0 item))
      (css--hex-to-rgb)
      (should (equal (buffer-string) (nth 1 item))))))

(ert-deftest css-test-rgb-to-named-color-or-hex ()
  (dolist (item '(("rgb(0, 0, 0)" "black")
                  ("rgb(255, 255, 255)" "white")
                  ("rgb(255, 255, 240)" "ivory")
                  ("rgb(18, 52, 86)" "#123456")
                  ("rgba(18, 52, 86, 0.5)" "#12345680")))
    (with-temp-buffer
      (css-mode)
      (insert (nth 0 item))
      (css--rgb-to-named-color-or-hex)
      (should (equal (buffer-string) (nth 1 item))))))

(ert-deftest css-test-cycle-color-format ()
  (with-temp-buffer
    (css-mode)
    (insert "black")
    (css-cycle-color-format)
    (should (equal (buffer-string) "#000000"))
    (css-cycle-color-format)
    (should (equal (buffer-string) "rgb(0, 0, 0)"))
    (css-cycle-color-format)
    (should (equal (buffer-string) "black"))))

(ert-deftest css-mdn-symbol-guessing ()
  (dolist (item '(("@med" "ia" "@media")
                  ("@keyframes " "{" "@keyframes")
                  ("p::after" "" "::after")
                  ("p:before" "" ":before")
                  ("a:v" "isited" ":visited")
                  ("border-" "color: red" "border-color")
                  ("border-color: red" ";" "border-color")
                  ("border-color: red; color: green" ";" "color")
                  ("  border-collapse " ": collapse;" "border-collapse")))
    (with-temp-buffer
      (css-mode)
      (insert (nth 0 item))
      (save-excursion (insert (nth 1 item)))
      (should (equal (nth 2 item) (css--mdn-find-symbol))))))

(ert-deftest css-test-rgb-parser ()
  (with-temp-buffer
    (css-mode)
    (dolist (input '("255, 0, 127"
                     "255, /* comment */ 0, 127"
                     "255 0 127"
                     "255, 0, 127, 0.75"
                     "255 0 127 / 0.75"
                     "100%, 0%, 50%"
                     "100%, 0%, 50%, 0.115"
                     "100% 0% 50%"
                     "100% 0% 50% / 0.115"))
      (erase-buffer)
      (save-excursion
        (insert input ")"))
      (should (equal (css--rgb-color) "#ff007f")))))

(ert-deftest css-test-hsl-parser ()
  (with-temp-buffer
    (css-mode)
    (dolist (input '("0, 100%, 50%"
                     "0 100% 50%"
                     "0 /* two */ /* comments */100% 50%"
                     "0, 100%, 50%, 0.75"
                     "0 100% 50% / 0.75"
                     "0deg 100% 50%"
                     "360deg 100% 50%"
                     "0rad, 100%, 50%, 0.115"
                     "0grad, 100%, 50%, 0.115"
                     "1turn 100% 50% / 0.115"))
      (erase-buffer)
      (save-excursion
        (insert input ")"))
      (should (equal (css--hsl-color) "#ff0000")))))

(ert-deftest css-test-hex-color ()
  (should (equal (css--hex-color "#abc") "#abc"))
  (should (equal (css--hex-color "#abcd") "#abc"))
  (should (equal (css--hex-color "#aabbcc") "#aabbcc"))
  (should (equal (css--hex-color "#aabbccdd") "#aabbcc")))

(ert-deftest css-test-hex-alpha ()
  (should (equal (css--hex-alpha "#abcd") "d"))
  (should-not (css--hex-alpha "#abc"))
  (should (equal (css--hex-alpha "#aabbccdd") "dd"))
  (should-not (css--hex-alpha "#aabbcc")))

(ert-deftest css-test-named-color ()
  (dolist (text '("@mixin black" "@include black"))
    (with-temp-buffer
      (insert text)
      (should-not (css--named-color (save-excursion
                                      (backward-word)
                                      (point))
                                    "black")))))

(provide 'css-mode-tests)
;;; css-mode-tests.el ends here

debug log:

solving f3d4243721 ...
found f3d4243721 in https://yhetil.org/emacs/1512909974.15208.0@smtp.gmail.com/
found 47cf5f9244 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 47cf5f9244b49477fe6ca3ce9a379a3850bfdc46	test/lisp/textmodes/css-mode-tests.el

applying [1/1] https://yhetil.org/emacs/1512909974.15208.0@smtp.gmail.com/
diff --git a/test/lisp/textmodes/css-mode-tests.el b/test/lisp/textmodes/css-mode-tests.el
index 47cf5f9244..f3d4243721 100644

Checking patch test/lisp/textmodes/css-mode-tests.el...
Applied patch test/lisp/textmodes/css-mode-tests.el cleanly.

index at:
100644 f3d42437216e881d29d3ab8a37f892df825387f4	test/lisp/textmodes/css-mode-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.