Skip to content

Commit 811603f

Browse files
authored
Merge pull request #417 from zonuexe/refactor/test-font-lock
Refactor face test
2 parents 0d7f970 + 6187085 commit 811603f

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

php-mode-test.el

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,35 @@ be processed."
7878
answers))))
7979
answers)))
8080

81-
(cl-defmacro with-php-mode-test ((file &key style indent magic custom) &rest body)
81+
(defun php-mode-test--buffer-face-list (buffer)
82+
"Return list of (STRING . FACE) from `BUFFER'."
83+
(with-current-buffer buffer
84+
(save-excursion
85+
(goto-char (point-min))
86+
(let (retval begin-pos last-face current-face str)
87+
(setq last-face (get-text-property (point) 'face))
88+
(setq begin-pos (point))
89+
(forward-char 1)
90+
91+
(while (< (point) (point-max))
92+
(setq current-face (get-text-property (point) 'face))
93+
(unless (equal current-face last-face)
94+
(setq str (buffer-substring-no-properties begin-pos (point)))
95+
(setq retval (nconc retval (list (cons str last-face))))
96+
(setq begin-pos (point))
97+
(setq last-face current-face))
98+
(forward-char 1))
99+
(setq str (buffer-substring-no-properties begin-pos (point)))
100+
(nconc retval (list (cons str last-face)))))))
101+
102+
(defun php-mode-test--parse-list-file (file-path)
103+
"Return list from `FILE-PATH'."
104+
(with-temp-buffer
105+
(insert-file-contents file-path)
106+
(let ((read-circle t))
107+
(read (current-buffer)))))
108+
109+
(cl-defmacro with-php-mode-test ((file &key style indent magic custom faces) &rest body)
82110
"Set up environment for testing `php-mode'.
83111
Execute BODY in a temporary buffer containing the contents of
84112
FILE, in `php-mode'. Optional keyword `:style' can be used to set
@@ -95,7 +123,10 @@ The test will use the PHP style by default.
95123
96124
If the `:custom' keyword is set, customized variables are not reset to
97125
their default state prior to starting the test. Use this if the test should
98-
run with specific customizations set."
126+
run with specific customizations set.
127+
128+
If the `:faces' keyword is set, read the file with `.faces' added to that
129+
file name and check that the faces of the fonts in the buffer match."
99130
(declare (indent 1))
100131
`(with-temp-buffer
101132
(insert-file-contents (expand-file-name ,file php-mode-test-dir))
@@ -119,6 +150,11 @@ run with specific customizations set."
119150
,(if magic
120151
'(should (cl-reduce (lambda (l r) (and l r))
121152
(php-mode-test-process-magics))))
153+
,(if faces
154+
`(should (equal
155+
(php-mode-test--parse-list-file
156+
(concat (expand-file-name ,file php-mode-test-dir) ".faces"))
157+
(php-mode-test--buffer-face-list (current-buffer)))))
122158
(goto-char (point-min))
123159
(let ((case-fold-search nil))
124160
,@body)))
@@ -728,9 +764,9 @@ style from Drupal."
728764
(should (eq 'php-constant
729765
(get-text-property (match-beginning 1) 'face)))))
730766

731-
(ert-deftest php-mode-test-variables()
767+
(ert-deftest php-mode-test-variables ()
732768
"Proper highlighting for variables."
733-
(with-php-mode-test ("variables.php")
769+
(with-php-mode-test ("variables.php" :faces t)
734770
(let ((variables '("regularVariable"
735771
"variableVariable"
736772
"staticVariable")))

tests/variables.php.faces

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
;; -*- mode: emacs-lisp -*-
2+
(("<?php" . font-lock-preprocessor-face)
3+
("\n\n" . nil)
4+
("// " . font-lock-comment-delimiter-face)
5+
("Test highlighting of variables with the variable-name-face\n" . font-lock-comment-face)
6+
("$" . php-variable-sigil)
7+
("regularVariable" . php-variable-name)
8+
(";\n" . nil)
9+
("$$" . php-variable-sigil)
10+
("variableVariable" . php-variable-name)
11+
(";\n" . nil)
12+
("MyClass" . php-constant)
13+
("::" . php-paamayim-nekudotayim)
14+
("$" . php-variable-sigil)
15+
("staticVariable" . php-variable-name)
16+
(";\n" . nil)
17+
("$" . php-variable-sigil)
18+
("object" . php-variable-name)
19+
("->" . php-object-op)
20+
("memberVariable" . php-property-name)
21+
(";\n" . nil)
22+
("$" . php-variable-sigil)
23+
("object" . php-variable-name)
24+
("->" . php-object-op)
25+
("funCall" . php-method-call)
26+
("();\n" . nil))

0 commit comments

Comments
 (0)