Skip to content

Commit 05cb4fa

Browse files
author
Eric James Michael Ritz
committed
Merge branch 'haxney/tests-with-magic-comments'
* haxney/tests-with-magic-comments: Use magic for tests of issues #14, #19, #27, #29, #42 Use "magic" comments in PHP files to simplify indentation testing
2 parents 25a3684 + de2775a commit 05cb4fa

File tree

5 files changed

+48
-45
lines changed

5 files changed

+48
-45
lines changed

php-mode-test.el

+40-37
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

@@ -75,17 +108,7 @@ have a string face."
75108
(ert-deftest php-mode-test-issue-14 ()
76109
"Array indentation."
77110
:expected-result :failed
78-
(with-php-mode-test ("issue-14.php")
79-
(let ((expected (concat "$post = Post::model()->find(array(\n"
80-
" 'select' => 'title',\n"
81-
" 'condition' => 'postID=:postID',\n"
82-
" 'params' => array(':postID'=>10),\n"
83-
"));")))
84-
(indent-region (point-min) (point-max))
85-
(goto-char (point-min))
86-
(re-search-forward "^\\$post")
87-
(should (string= (buffer-substring-no-properties (match-beginning 0) (point-max))
88-
expected)))))
111+
(with-php-mode-test ("issue-14.php" :indent t :magic t)))
89112

90113
(ert-deftest php-mode-test-issue-16 ()
91114
"Comma separated \"use\" (namespaces).
@@ -98,22 +121,12 @@ Gets the face of the text after the comma."
98121

99122
(ert-deftest php-mode-test-issue-18 ()
100123
"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)))))
124+
(with-php-mode-test ("issue-18.php" :indent t :magic t)))
110125

111126
(ert-deftest php-mode-test-issue-19 ()
112127
"Alignment of arrow operators."
113128
:expected-result :failed
114-
(with-php-mode-test ("issue-19.php")
115-
(indent-region (point-min) (point-max))
116-
(goto-char (point-min))
129+
(with-php-mode-test ("issue-19.php" :indent t)
117130
(while (re-search-forward "^\\s-*\\$object->")
118131
;; Point is just after `->'
119132
(let ((col (current-column)))
@@ -141,11 +154,7 @@ This applies for both single and double quotes."
141154

142155
(ert-deftest php-mode-test-issue-27 ()
143156
"Indentation in a file with a shebang."
144-
(with-php-mode-test ("issue-27.php")
145-
(re-search-forward "^\\s-*// Tab")
146-
(indent-for-tab-command)
147-
(back-to-indentation)
148-
(should (eq (current-column) tab-width))))
157+
(with-php-mode-test ("issue-27.php" :indent t :magic t)))
149158

150159
(ert-deftest php-mode-test-issue-28 ()
151160
"Slowdown when scrolling.
@@ -161,16 +170,10 @@ This doesn't test anything, for now."
161170
(ert-deftest php-mode-test-issue-29 ()
162171
"Indentation of anonymous functions as arguments.
163172
The closing brace and parenthesis should be at column 0."
164-
(with-php-mode-test ("issue-29.php")
165-
(indent-region (point-min) (point-max))
166-
(goto-char (point-min))
167-
(re-search-forward "^\\s-*});")
168-
(back-to-indentation)
169-
(should (eq (current-column) 0))))
173+
(with-php-mode-test ("issue-29.php" :indent t :magic t)))
170174

171175
(ert-deftest php-mode-test-issue-42 ()
172176
"Error while indenting closures.
173177
If the bug has been fixed, indenting the buffer should not cause
174178
an error."
175-
(with-php-mode-test ("issue-42.php")
176-
(indent-region (point-min) (point-max))))
179+
(with-php-mode-test ("issue-42.php" :indent t)))

tests/issue-14.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
$post = Post::model()->find(array(
18-
'select' => 'title',
19-
'condition' => 'postID=:postID',
20-
'params' => array(':postID'=>10),
21-
));
18+
'select' => 'title', // ###php-mode-test### ((indent c-basic-offset))
19+
'condition' => 'postID=:postID', // ###php-mode-test### ((indent c-basic-offset))
20+
'params' => array(':postID'=>10), // ###php-mode-test### ((indent c-basic-offset))
21+
)); // ###php-mode-test### ((indent 0))

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
}

tests/issue-27.php

100644100755
+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
*/
1313

1414
if (1 == 1) {
15-
// Tab
15+
// Tab ###php-mode-test### ((indent c-basic-offset))
1616
}

tests/issue-29.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
*/
1313

1414
$app->get("/index", function() {
15-
$app->redirect("foo");
16-
});
15+
$app->redirect("foo"); // ###php-mode-test### ((indent c-basic-offset))
16+
}); // ###php-mode-test### ((indent 0))

0 commit comments

Comments
 (0)