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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
|
;;; ert-font-lock-tests.el --- ERT Font Lock tests -*- lexical-binding: t -*-
;; Copyright (C) 2023-2025 Free Software Foundation, Inc.
;; Author: Vladimir Kazanov
;; 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:
;; This file is part of ERT Font Lock, an extension to the Emacs Lisp
;; Regression Test library (ERT) providing a convenient way to check
;; syntax highlighting provided by font-lock.
;;
;; See ert-font-lock.el for details, and below for example usage of
;; ert-font-lock facilities.
(require 'ert)
(require 'ert-x)
(require 'ert-font-lock)
;;; Helpers
;;
(defmacro with-temp-buffer-str-mode (mode str &rest body)
"Create a buffer with STR contents and MODE. "
(declare (indent 1) (debug t))
`(with-temp-buffer
(insert ,str)
(,mode)
(goto-char (point-min))
,@body))
(defun ert-font-lock--wrap-begin-end (re)
(concat "^" re "$"))
;;; Regexp tests
;;;
(ert-deftest test-regexp--face-symbol-re ()
(let ((re (ert-font-lock--wrap-begin-end
ert-font-lock--face-symbol-re)))
(should (string-match-p re "font-lock-keyword-face"))
(should (string-match-p re "-face"))
(should (string-match-p re "weird-package/-face"))
(should (string-match-p re "-"))
(should (string-match-p re "font-lock.face"))
(should-not (string-match-p re "face suffix-with"))
(should-not (string-match-p re "("))))
(ert-deftest test-regexp--face-symbol-list-re ()
(let ((re (ert-font-lock--wrap-begin-end
ert-font-lock--face-symbol-list-re)))
(should (string-match-p re "(face1 face2)"))
(should (string-match-p re "(face1)"))
(should (string-match-p re "()"))
(should-not (string-match-p re ")"))
(should-not (string-match-p re "("))))
(ert-deftest test-regexp--assertion-line-re ()
(let ((re (ert-font-lock--wrap-begin-end
ert-font-lock--assertion-line-re)))
(should (string-match-p re "^ something-face"))
(should (string-match-p re "^ !something-face"))
(should (string-match-p re "^ (face1 face2)"))
(should (string-match-p re "^ !(face1 face2)"))
(should (string-match-p re "^ ()"))
(should (string-match-p re "^ !()"))
(should (string-match-p re "^ nil"))
(should (string-match-p re "^ !nil"))
(should (string-match-p re "<- something-face"))
(should (string-match-p re "<- ^ something-face"))
(should (string-match-p re "^^ ^ something-face"))
(should (string-match-p re "^ ^something-face"))
(should-not (string-match-p re "^ <- ^something-face"))))
;;; Comment parsing tests
;;
(ert-deftest test-line-comment-p--fundamental ()
(with-temp-buffer-str-mode fundamental-mode
"// comment\n"
(should-not (ert-font-lock--line-comment-p))))
(ert-deftest test-line-comment-p--emacs-lisp ()
(with-temp-buffer-str-mode emacs-lisp-mode
"not comment
;; comment
"
(should-not (ert-font-lock--line-comment-p))
(forward-line)
(should (ert-font-lock--line-comment-p))
(forward-line)
(should-not (ert-font-lock--line-comment-p))))
(ert-deftest test-line-comment-p--shell-script ()
(with-temp-buffer-str-mode shell-script-mode
"echo Not a comment
# comment
"
(should-not (ert-font-lock--line-comment-p))
(forward-line)
(should (ert-font-lock--line-comment-p))))
(declare-function php-mode "php-mode")
(ert-deftest test-line-comment-p--php ()
(skip-unless (featurep 'php-mode))
(with-temp-buffer-str-mode php-mode
"echo 'Not a comment'
// comment
/* comment */
"
(should-not (ert-font-lock--line-comment-p))
(forward-line)
(should (ert-font-lock--line-comment-p))
(forward-line)
(should (ert-font-lock--line-comment-p))))
(ert-deftest test-line-comment-p--javascript ()
(with-temp-buffer-str-mode javascript-mode
"// comment
// comment, after a blank line
var abc = function(d) {};
"
(should (ert-font-lock--line-comment-p))
(forward-line)
(should-not (ert-font-lock--line-comment-p))
(forward-line)
(should (ert-font-lock--line-comment-p))
(forward-line)
(should-not (ert-font-lock--line-comment-p))
(forward-line)
(should-not (ert-font-lock--line-comment-p))))
(ert-deftest test-line-comment-p--python ()
(with-temp-buffer-str-mode python-mode
"# comment
# comment
print(\"Hello, world!\")"
(should (ert-font-lock--line-comment-p))
(forward-line)
(should-not (ert-font-lock--line-comment-p))
(forward-line)
(should (ert-font-lock--line-comment-p))
(forward-line)
(should-not (ert-font-lock--line-comment-p))))
(ert-deftest test-line-comment-p--c ()
(with-temp-buffer-str-mode c-mode
"// comment
/* also comment */"
(should (ert-font-lock--line-comment-p))
(forward-line)
(should (ert-font-lock--line-comment-p))))
(ert-deftest test-parse-comments--no-assertion-error ()
(let* ((str "
not_an_assertion
random_symbol
"))
(with-temp-buffer
(insert str)
(javascript-mode)
(should-error (ert-font-lock--parse-comments) :type 'user-error))))
(ert-deftest test-parse-comments--single-line-error ()
(let* ((str "// ^ face.face1"))
(with-temp-buffer
(insert str)
(javascript-mode)
(should-error (ert-font-lock--parse-comments) :type 'user-error))))
(ert-deftest test-parse-comments--single-line-single-caret ()
(let* ((str "
first
// ^ face.face1
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 1))
(should (equal (car asserts)
'(:line-checked 2 :line-assert 3 :column-checked 3 :face face.face1 :negation nil))))))
(ert-deftest test-parse-comments--single-line-many-carets ()
(let* ((str "
multiplecarets
//^^^ ^^ ^ face.face1
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 6))
(should (equal asserts
'((:line-checked 2 :line-assert 3 :column-checked 2 :face face.face1 :negation nil)
(:line-checked 2 :line-assert 3 :column-checked 3 :face face.face1 :negation nil)
(:line-checked 2 :line-assert 3 :column-checked 4 :face face.face1 :negation nil)
(:line-checked 2 :line-assert 3 :column-checked 6 :face face.face1 :negation nil)
(:line-checked 2 :line-assert 3 :column-checked 7 :face face.face1 :negation nil)
(:line-checked 2 :line-assert 3 :column-checked 9 :face face.face1 :negation nil)))))))
(ert-deftest test-parse-comments--face-list ()
(let* ((str "
facelist
// ^ (face1 face2)
// ^ !(face3 face4)
// ^ (face5)
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 3))
(should (equal asserts
'((:line-checked 2 :line-assert 3 :column-checked 3 :face (face1 face2) :negation nil)
(:line-checked 2 :line-assert 4 :column-checked 3 :face (face3 face4) :negation t)
(:line-checked 2 :line-assert 5 :column-checked 3 :face (face5) :negation nil)))))))
(ert-deftest test-parse-comments--caret-negation ()
(let* ((str "
first
// ^ !face
// ^ face
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 2))
(should (equal asserts
'((:line-checked 2 :line-assert 3 :column-checked 3 :face face :negation t)
(:line-checked 2 :line-assert 4 :column-checked 3 :face face :negation nil)))))))
(ert-deftest test-parse-comments--single-line-multiple-assert-lines ()
(let* ((str "
first
// ^ face1
// ^ face.face2
// ^ face-face.face3
// ^ face_face.face4
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 4))
(should (equal asserts
'((:line-checked 2 :line-assert 3 :column-checked 3 :face face1 :negation nil)
(:line-checked 2 :line-assert 4 :column-checked 7 :face face.face2 :negation nil)
(:line-checked 2 :line-assert 5 :column-checked 7 :face face-face.face3 :negation nil)
(:line-checked 2 :line-assert 6 :column-checked 7 :face face_face.face4 :negation nil)))))))
(ert-deftest test-parse-comments--multiple-line-multiple-assert-lines ()
(let* ((str "
first
// ^ face1
second
// ^ face2
// ^ face3
third
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 3))
(should (equal asserts
'((:line-checked 2 :line-assert 3 :column-checked 3 :face face1 :negation nil)
(:line-checked 4 :line-assert 5 :column-checked 3 :face face2 :negation nil)
(:line-checked 4 :line-assert 6 :column-checked 5 :face face3 :negation nil)))))))
(ert-deftest test-parse-comments--arrow-single-line-single ()
(let* ((str "
first
// <- face1
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 1))
(should (equal (car asserts)
'(:line-checked 2 :line-assert 3 :column-checked 0 :face face1 :negation nil))))))
(ert-deftest test-parse-comments-arrow-multiple-line-single ()
(let* ((str "
first
// <- face1
// <- face2
// <- face3
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 3))
(should (equal asserts
'((:line-checked 2 :line-assert 3 :column-checked 0 :face face1 :negation nil)
(:line-checked 2 :line-assert 4 :column-checked 2 :face face2 :negation nil)
(:line-checked 2 :line-assert 5 :column-checked 4 :face face3 :negation nil)))))))
(ert-deftest test-parse-comments--non-assert-comment-single ()
(let* ((str "
// first
// ^ comment-face
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 1))
(should (equal (car asserts)
'(:line-checked 2 :line-assert 3 :column-checked 4 :face comment-face :negation nil))))))
(ert-deftest test-parse-comments--non-assert-comment-multiple ()
(let* ((str "
// first second third
// ^ comment-face
// ^ comment-face
// ^ comment-face
")
asserts)
(with-temp-buffer
(insert str)
(javascript-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 3))
(should (equal asserts
'((:line-checked 2 :line-assert 3 :column-checked 4 :face comment-face :negation nil)
(:line-checked 2 :line-assert 4 :column-checked 10 :face comment-face :negation nil)
(:line-checked 2 :line-assert 5 :column-checked 18 :face comment-face :negation nil)))))))
(ert-deftest test-parse-comments--multiline-comment-single ()
(let* ((str "
/*
this is a comment
^ comment-face
*/
")
asserts)
(with-temp-buffer
(insert str)
(c-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 1))
(should (equal (car asserts)
'(:line-checked 3 :line-assert 4 :column-checked 3 :face comment-face :negation nil))))))
(ert-deftest test-parse-comments--multiline-comment-multiple ()
(let* ((str "
/*
this is a comment
^ comment-face
another comment
^ comment-face
*/
")
asserts)
(with-temp-buffer
(insert str)
(c-mode)
(setq asserts (ert-font-lock--parse-comments))
(should (eql (length asserts) 2))
(should (equal asserts
'((:line-checked 3 :line-assert 4 :column-checked 3 :face comment-face :negation nil)
(:line-checked 5 :line-assert 6 :column-checked 4 :face comment-face :negation nil)))))))
;;; Syntax highlighting assertion tests
;;
(ert-deftest test-syntax-highlight-inline--nil-list ()
(let ((str "
var abc = function(d) {
// ^ nil
// ^ !nil
};
"))
(with-temp-buffer
(insert str)
(javascript-mode)
(font-lock-ensure)
(ert-font-lock--check-faces
(ert-font-lock--parse-comments)))))
(ert-deftest test-syntax-highlight-inline--face-list ()
(let ((str "
var abc = function(d) {
// ^ (test-face-2 test-face-1 font-lock-variable-name-face)
};
"))
(with-temp-buffer
(insert str)
(javascript-mode)
(font-lock-ensure)
(add-face-text-property (point-min) (point-max) 'test-face-1)
(add-face-text-property (point-min) (point-max) 'test-face-2)
(ert-font-lock--check-faces
(ert-font-lock--parse-comments)))))
(ert-deftest test-syntax-highlight-inline--caret-multiple-assertions ()
(let ((str "
var abc = function(d) {
// ^ font-lock-variable-name-face
// ^ font-lock-keyword-face
// ^ font-lock-variable-name-face
};
"))
(with-temp-buffer
(insert str)
(javascript-mode)
(font-lock-ensure)
(ert-font-lock--check-faces
(ert-font-lock--parse-comments)))))
(ert-deftest test-syntax-highlight-inline--caret-wrong-face ()
(let* ((str "
var abc = function(d) {
// ^ not-a-face
};
"))
(with-temp-buffer
(insert str)
(javascript-mode)
(font-lock-ensure)
(should-error (ert-font-lock--check-faces
(ert-font-lock--parse-comments))))))
(ert-deftest test-syntax-highlight-inline--caret-negated-wrong-face ()
(let* ((str "
var abc = function(d) {
// ^ !not-a-face
};
"))
(with-temp-buffer
(insert str)
(javascript-mode)
(font-lock-ensure)
(ert-font-lock--check-faces
(ert-font-lock--parse-comments)))))
(ert-deftest test-syntax-highlight-inline--comment-face ()
(let* ((str "
// this is a comment
// ^ font-lock-comment-face
// ^ font-lock-comment-face
// ^ font-lock-comment-face
"))
(with-temp-buffer
(insert str)
(javascript-mode)
(font-lock-ensure)
(ert-font-lock--check-faces
(ert-font-lock--parse-comments)))))
(ert-deftest test-syntax-highlight-inline--multiline-comment-face ()
(let* ((str "
/*
this is a comment
^ font-lock-comment-face
another comment
more comments
^ font-lock-comment-face
*/
"))
(with-temp-buffer
(insert str)
(c-mode)
(font-lock-ensure)
(ert-font-lock--check-faces
(ert-font-lock--parse-comments)))))
(ert-deftest test-font-lock-test-string--correct ()
(ert-font-lock-test-string
"
var abc = function(d) {
// <- font-lock-keyword-face
// ^ font-lock-variable-name-face
// ^ font-lock-keyword-face
// ^ font-lock-variable-name-face
};
"
'javascript-mode))
(ert-deftest test-font-lock-test-file--correct ()
(ert-font-lock-test-file
(ert-resource-file "correct.js")
'javascript-mode))
(ert-deftest test-font-lock-test-file--wrong ()
:expected-result :failed
(ert-font-lock-test-file
(ert-resource-file "broken.js")
'javascript-mode))
;;; Macro tests
;;
(ert-font-lock-deftest test-macro-test--correct-highlighting
emacs-lisp-mode
"
(defun fun ())
;; ^ font-lock-keyword-face
;; ^ font-lock-function-name-face")
(ert-font-lock-deftest test-macro-test--docstring
"A test with a docstring."
emacs-lisp-mode
"
(defun fun ())
;; ^ font-lock-keyword-face"
)
(ert-font-lock-deftest test-macro-test--failing
"A failing test."
:expected-result :failed
emacs-lisp-mode
"
(defun fun ())
;; ^ wrong-face")
(ert-font-lock-deftest-file test-macro-test--file
"Test reading correct assertions from a file"
javascript-mode
"correct.js")
(ert-font-lock-deftest-file test-macro-test--file-no-asserts
"Check failing on files without assertions"
:expected-result :failed
javascript-mode
"no-asserts.js")
(ert-font-lock-deftest-file test-macro-test--file-failing
"Test reading wrong assertions from a file"
:expected-result :failed
javascript-mode
"broken.js")
;;; ert-font-lock-tests.el ends here
|