Skip to content

Commit 49ba0d3

Browse files
committed
Use "magic" comments in PHP files to simplify indentation testing
If a line in a PHP test file contains the magic comment `###php-mode-test### (SPEC ...)`, then each `SPEC` is executed. Each `SPEC` is of the form `(NAME [ARG ...])` and is a test which applies to the current line and returns non-nil if it passes. Each `ARG` is `eval`ed before being passed to `NAME`. A magic comment looks like this header('Location: http://github.com'); // ###php-mode-test### ((indent c-basic-offset)) In this case, it indicates that the current line should be indented by one `c-basic-offset'. Currently, the only available test is for indentation.
1 parent 25a3684 commit 49ba0d3

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

php-mode-test.el

+35-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,35 @@
3737
(defvar php-mode-test-dir (expand-file-name "tests" (file-name-directory load-file-name))
3838
"Directory containing the `php-mode' test files.")
3939

40-
(defmacro* with-php-mode-test ((file &optional &key style) &rest body)
40+
(defvar php-mode-test-valid-magics '(indent)
41+
"List of allowed \"magic\" directives which can appear in test cases.")
42+
43+
(defvar php-mode-test-magic-regexp "###php-mode-test### \\((.+)\\)"
44+
"Regexp which identifies a magic comment.")
45+
46+
(defun php-mode-test-process-magics ()
47+
"Process the test directives in the current buffer.
48+
These are the ###php-mode-test### comments. Valid magics are
49+
listed in `php-mode-test-valid-magics'; no other directives will
50+
be processed."
51+
(flet ((indent (offset) (equal (current-indentation) offset)))
52+
(let (directives answers)
53+
(save-excursion
54+
(goto-char (point-min))
55+
(while (re-search-forward php-mode-test-magic-regexp nil t)
56+
(setq directives (read (buffer-substring (match-beginning 1)
57+
(match-end 1))))
58+
(setq answers
59+
(append (mapcar (lambda (curr)
60+
(let ((fn (car curr))
61+
(args (mapcar 'eval (cdr-safe curr))))
62+
(if (memq fn php-mode-test-valid-magics)
63+
(apply fn args))))
64+
directives)
65+
answers))))
66+
answers)))
67+
68+
(defmacro* with-php-mode-test ((file &optional &key style indent magic) &rest body)
4169
"Set up environment for testing `php-mode'.
4270
Execute BODY in a temporary buffer containing the contents of
4371
FILE, in `php-mode'. Optional keyword `:style' can be used to set
@@ -51,6 +79,11 @@ the coding style to one of `pear', `drupal', or `wordpress'."
5179
(wordpress '(php-enable-wordpress-coding-style)))
5280
(php-mode)
5381
(font-lock-fontify-buffer)
82+
,(if indent
83+
'(indent-region (point-min) (point-max)))
84+
,(if magic
85+
'(should (reduce (lambda (l r) (and l r))
86+
(php-mode-test-process-magics))))
5487
(goto-char (point-min))
5588
,@body))
5689

@@ -98,15 +131,7 @@ Gets the face of the text after the comma."
98131

99132
(ert-deftest php-mode-test-issue-18 ()
100133
"Indentation of strings which include \"//\"."
101-
(with-php-mode-test ("issue-18.php")
102-
(let ((expected (concat "if ($a === 'github') {\n"
103-
" header('Location: http://github.com');\n"
104-
"}")))
105-
(indent-region (point-min) (point-max))
106-
(goto-char (point-min))
107-
(re-search-forward "^if ")
108-
(should (string= (buffer-substring-no-properties (match-beginning 0) (point-max))
109-
expected)))))
134+
(with-php-mode-test ("issue-18.php" :indent t :magic t)))
110135

111136
(ert-deftest php-mode-test-issue-19 ()
112137
"Alignment of arrow operators."

tests/issue-18.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
$a = 'github';
2020

2121
if ($a === 'github') {
22-
header('Location: http://github.com');
22+
header('Location: http://github.com'); // ###php-mode-test### ((indent c-basic-offset))
2323
}

0 commit comments

Comments
 (0)