forked from emacs-php/php-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-mode-test.el
276 lines (237 loc) · 10.2 KB
/
php-mode-test.el
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
;;; php-mode-test.el --- Tests for php-mode
;; Copyright (C) 2013 Daniel Hackney
;; 2014 Eric James Michael Ritz
;; Author: Daniel Hackney <dan@haxney.org>
;; URL: https://github.com/ejmr/php-mode
;;; License
;; This file 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 file 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 file; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Commentary:
;; Automate tests from the "tests" directory using `ert', which comes bundled
;; with Emacs >= 24.1.
;;; Code:
(require 'php-mode)
(require 'ert)
(eval-when-compile
(require 'cl))
;; Work around bug #14325
;; <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=14325>.
(c-after-font-lock-init)
(defvar php-mode-test-dir (expand-file-name "tests" (file-name-directory load-file-name))
"Directory containing the `php-mode' test files.")
(defvar php-mode-test-valid-magics '(indent)
"List of allowed \"magic\" directives which can appear in test cases.")
(defvar php-mode-test-magic-regexp "###php-mode-test### \\((.+)\\)"
"Regexp which identifies a magic comment.")
(defun php-mode-test-process-magics ()
"Process the test directives in the current buffer.
These are the ###php-mode-test### comments. Valid magics are
listed in `php-mode-test-valid-magics'; no other directives will
be processed."
(flet ((indent (offset) (equal (current-indentation) offset)))
(let (directives answers)
(save-excursion
(goto-char (point-min))
(while (re-search-forward php-mode-test-magic-regexp nil t)
(setq directives (read (buffer-substring (match-beginning 1)
(match-end 1))))
(setq answers
(append (mapcar (lambda (curr)
(let ((fn (car curr))
(args (mapcar 'eval (cdr-safe curr))))
(if (memq fn php-mode-test-valid-magics)
(apply fn args))))
directives)
answers))))
answers)))
(defmacro* with-php-mode-test ((file &key style indent magic) &rest body)
"Set up environment for testing `php-mode'.
Execute BODY in a temporary buffer containing the contents of
FILE, in `php-mode'. Optional keyword `:style' can be used to set
the coding style to one of the following:
1. `pear'
2. `drupal'
3. `wordpress'
4. `symfony2'
Using any other symbol for STYLE results in undefined behavior.
The test will use the PEAR style by default."
(declare (indent 1))
`(with-temp-buffer
(insert-file-contents (expand-file-name ,file php-mode-test-dir))
(php-mode)
(font-lock-fontify-buffer)
,(case style
(pear '(php-enable-pear-coding-style))
(drupal '(php-enable-drupal-coding-style))
(wordpress '(php-enable-wordpress-coding-style))
(symfony2 '(php-enable-symfony2-coding-style))
(t '(php-enable-pear-coding-style)))
,(if indent
'(indent-region (point-min) (point-max)))
,(if magic
'(should (reduce (lambda (l r) (and l r))
(php-mode-test-process-magics))))
(goto-char (point-min))
,@body))
(ert-deftest php-mode-test-issue-8 ()
"Annotation highlighting."
(with-php-mode-test ("issue-8.php")
(search-forward "@ORM")
(should (eq
(get-text-property (match-beginning 0) 'face)
'php-annotations-annotation-face))))
(ert-deftest php-mode-test-issue-9 ()
"Single quote in text in HTML misinterpreted.
The next character after \">We\" is a single quote. It should not
have a string face."
:expected-result :failed
(with-php-mode-test ("issue-9.php")
(should-not (eq
(get-text-property (search-forward ">We") 'face)
'font-lock-string-face))))
(ert-deftest php-mode-test-issue-14 ()
"Array indentation."
(with-php-mode-test ("issue-14.php" :indent t :magic t)))
(ert-deftest php-mode-test-issue-16 ()
"Comma separated \"use\" (namespaces).
Gets the face of the text after the comma."
(with-php-mode-test ("issue-16.php")
(re-search-forward "^use " nil nil 3)
(should (eq
(get-text-property (search-forward ", ") 'face)
'font-lock-type-face))))
(ert-deftest php-mode-test-issue-18 ()
"Indentation of strings which include \"//\"."
(with-php-mode-test ("issue-18.php" :indent t :magic t)))
(ert-deftest php-mode-test-issue-19 ()
"Alignment of arrow operators."
(with-php-mode-test ("issue-19.php" :indent t)
(while (search-forward "$object->" (point-max) t)
;; Point is just after `->'
(let ((col (current-column)))
(search-forward "->")
(should (= (current-column) col))))))
(ert-deftest php-mode-test-issue-21 ()
"Font locking multi-line string."
(with-php-mode-test ("issue-21.php")
(search-forward "\"")
(while (not (looking-at "\""))
(should (eq (get-text-property (point) 'face)
'font-lock-string-face))
(forward-char))))
(ert-deftest php-mode-test-issue-22 ()
"Font lock quotes within comments as regular comments.
This applies for both single and double quotes."
(with-php-mode-test ("issue-21.php")
(while (search-forward "#" nil t)
(while (not (looking-at "\n"))
(should (eq (get-text-property (point) 'face)
'font-lock-comment-face))
(forward-char)))))
(ert-deftest php-mode-test-issue-27 ()
"Indentation in a file with a shebang."
(with-php-mode-test ("issue-27.php" :indent t :magic t)))
(ert-deftest php-mode-test-issue-28 ()
"Slowdown when scrolling.
No obvious way to test this. One possibility is to record time it
takes to scroll down the whole buffer using `next-line'. This may
not cause the desired fontification, and it could take different
amounts of time on different machines, so an absolute time would
not be very useful.
This doesn't test anything, for now."
(should t))
(ert-deftest php-mode-test-issue-29 ()
"Indentation of anonymous functions as arguments.
The closing brace and parenthesis should be at column 0."
(with-php-mode-test ("issue-29.php" :indent t :magic t)))
(ert-deftest php-mode-test-issue-42 ()
"Error while indenting closures.
If the bug has been fixed, indenting the buffer should not cause
an error."
(with-php-mode-test ("issue-42.php" :indent t)))
(ert-deftest php-mode-test-issue-53 ()
"Check if whitespace effects are undone when changing coding
style from Drupal."
(with-php-mode-test
("issue-53.php")
(search-forward "return $this->bar;")
;; the file written to has no significance, only the buffer
(let ((tmp-filename (make-temp-name temporary-file-directory)))
(dolist (mode '(pear wordpress symfony2))
(php-mode-custom-coding-style-set 'php-mode-coding-style 'drupal)
(php-mode-custom-coding-style-set 'php-mode-coding-style mode)
(should-not show-trailing-whitespace)
(write-file tmp-filename)
(should (char-equal (char-after) ?\s))))))
(ert-deftest php-mode-test-issue-73 ()
"The `delete-indentation' function should work properly for PHP.
This means modifying the logic of `fixup-whitespace' so that it
eliminates spaces before ',', ';', '->' amd '::' and after '->' and
'::'."
(with-php-mode-test ("issue-73.php")
(when (search-forward "# Correct" nil t)
(forward-line 1)
(let ((correct-line (thing-at-point 'line)))
(while (search-forward "# Test" nil t)
(forward-line 1)
(let ((current-line (line-number-at-pos)))
(catch 'eob
(while (not (looking-at-p "$"))
(unless (zerop (forward-line 1))
(throw 'eob t))))
(forward-line -1)
(while (not (eq (line-number-at-pos) current-line))
(delete-indentation))
(beginning-of-line)
(should (string= (thing-at-point 'line) correct-line))))))))
(ert-deftest php-mode-test-issue-99 ()
"Proper indentation for 'foreach' statements without braces."
(with-php-mode-test ("issue-99.php" :indent t :magic t)))
(ert-deftest php-mode-test-issue-115 ()
"Proper alignment for chained method calls inside arrays."
:expected-result :failed
(with-php-mode-test ("issue-115.php" :indent t :magic t)))
(ert-deftest php-mode-test-issue-124 ()
"Proper syntax propertizing when a quote appears in a heredoc."
(with-php-mode-test ("issue-124.php" :indent t)
(search-forward "Heredoc")
;; The heredoc should be recognized as a string.
(dolist (syntax (c-guess-basic-syntax))
(should (eq (car syntax) 'string)))
(search-forward "function bar")
;; After the heredoc should *not* be recognized as a string.
(dolist (syntax (c-guess-basic-syntax))
(should (not (eq (car syntax) 'string))))))
(ert-deftest php-mode-test-issue-136 ()
"Proper highlighting for variable interpolation."
(with-php-mode-test ("issue-136.php")
(let ((variables '("$name"
"${name}"
"{$name}"
"{$user->name}"
"{$user->getName()}"
"{$users[0]->name}"
"{$users[$index]->name}"
"{$users[$user->id]->name}"
"{$users[$user->getID()]->name}")))
;; All of the strings we want to test come after the call to
;; ob_start(), so we jump to there first.
(search-forward "ob_start()")
(dolist (variable variables)
(search-forward variable)
(should (eq 'font-lock-variable-name-face
(get-text-property (point) 'face)))))))
(ert-deftest php-mode-test-issue-145 ()
"Closure indentation."
(with-php-mode-test ("issue-145.php" :indent t)))
;;; php-mode-test.el ends here