37
37
(defvar php-mode-test-dir (expand-file-name " tests" (file-name-directory load-file-name))
38
38
" Directory containing the `php-mode' test files." )
39
39
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)
41
69
" Set up environment for testing `php-mode' .
42
70
Execute BODY in a temporary buffer containing the contents of
43
71
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'."
51
79
(wordpress '(php-enable-wordpress-coding-style)))
52
80
(php-mode)
53
81
(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))))
54
87
(goto-char (point-min ))
55
88
,@body ))
56
89
@@ -75,17 +108,7 @@ have a string face."
75
108
(ert-deftest php-mode-test-issue-14 ()
76
109
" Array indentation."
77
110
: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 )))
89
112
90
113
(ert-deftest php-mode-test-issue-16 ()
91
114
" Comma separated \" use\" (namespaces).
@@ -98,22 +121,12 @@ Gets the face of the text after the comma."
98
121
99
122
(ert-deftest php-mode-test-issue-18 ()
100
123
" 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 )))
110
125
111
126
(ert-deftest php-mode-test-issue-19 ()
112
127
" Alignment of arrow operators."
113
128
: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 )
117
130
(while (re-search-forward " ^\\ s-*\\ $object->" )
118
131
; ; Point is just after `->'
119
132
(let ((col (current-column )))
@@ -141,11 +154,7 @@ This applies for both single and double quotes."
141
154
142
155
(ert-deftest php-mode-test-issue-27 ()
143
156
" 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 )))
149
158
150
159
(ert-deftest php-mode-test-issue-28 ()
151
160
" Slowdown when scrolling.
@@ -161,16 +170,10 @@ This doesn't test anything, for now."
161
170
(ert-deftest php-mode-test-issue-29 ()
162
171
" Indentation of anonymous functions as arguments.
163
172
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 )))
170
174
171
175
(ert-deftest php-mode-test-issue-42 ()
172
176
" Error while indenting closures.
173
177
If the bug has been fixed, indenting the buffer should not cause
174
178
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 )))
0 commit comments