|
| 1 | +;;; php-mode-test.el --- Tests for php-mode |
| 2 | + |
| 3 | +;; Copyright (C) 2013 Daniel Hackney |
| 4 | + |
| 5 | +;; Author: Daniel Hackney <[email protected]> |
| 6 | +;; URL: https://github.com/ejmr/php-mode |
| 7 | + |
| 8 | +;;; License |
| 9 | + |
| 10 | +;; This file is free software; you can redistribute it and/or |
| 11 | +;; modify it under the terms of the GNU General Public License |
| 12 | +;; as published by the Free Software Foundation; either version 3 |
| 13 | +;; of the License, or (at your option) any later version. |
| 14 | + |
| 15 | +;; This file is distributed in the hope that it will be useful, |
| 16 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +;; GNU General Public License for more details. |
| 19 | + |
| 20 | +;; You should have received a copy of the GNU General Public License |
| 21 | +;; along with this file; if not, write to the Free Software |
| 22 | +;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 23 | +;; 02110-1301, USA. |
| 24 | + |
| 25 | +;;; Commentary: |
| 26 | + |
| 27 | +;; Automate tests from the "tests" directory using `ert', which comes bundled |
| 28 | +;; with Emacs >= 24.1. |
| 29 | + |
| 30 | +;;; Code: |
| 31 | + |
| 32 | +(require 'php-mode) |
| 33 | +(require 'ert) |
| 34 | +(eval-when-compile |
| 35 | + (require 'cl)) |
| 36 | + |
| 37 | +(defvar php-mode-test-dir (expand-file-name "tests" (file-name-directory load-file-name)) |
| 38 | + "Directory containing the `php-mode' test files.") |
| 39 | + |
| 40 | +(defmacro* with-php-mode-test ((file &optional &key style) &rest body) |
| 41 | + "Set up environment for testing `php-mode'. |
| 42 | +Execute BODY in a temporary buffer containing the contents of |
| 43 | +FILE, in `php-mode'. Optional keyword `:style' can be used to set |
| 44 | +the coding style to one of `pear', `drupal', or `wordpress'." |
| 45 | + (declare (indent 1)) |
| 46 | + `(with-temp-buffer |
| 47 | + (insert-file-contents (expand-file-name ,file php-mode-test-dir)) |
| 48 | + ,(case style |
| 49 | + (pear '(php-enable-pear-coding-style)) |
| 50 | + (drupal '(php-enable-drupal-coding-style)) |
| 51 | + (wordpress '(php-enable-wordpress-coding-style))) |
| 52 | + (php-mode) |
| 53 | + (font-lock-fontify-buffer) |
| 54 | + (goto-char (point-min)) |
| 55 | + ,@body)) |
| 56 | + |
| 57 | +(ert-deftest php-mode-test-issue-8 () |
| 58 | + "Annotation highlighting." |
| 59 | + (with-php-mode-test ("issue-8.php") |
| 60 | + (search-forward "@ORM") |
| 61 | + (should (eq |
| 62 | + (get-text-property (match-beginning 0) 'face) |
| 63 | + 'php-annotations-annotation-face)))) |
| 64 | + |
| 65 | +(ert-deftest php-mode-test-issue-9 () |
| 66 | + "Single quote in text in HTML misinterpreted. |
| 67 | +The next character after \">We\" is a single quote. It should not |
| 68 | +have a string face." |
| 69 | + :expected-result :failed |
| 70 | + (with-php-mode-test ("issue-9.php") |
| 71 | + (should-not (eq |
| 72 | + (get-text-property (search-forward ">We") 'face) |
| 73 | + 'font-lock-string-face)))) |
| 74 | + |
| 75 | +(ert-deftest php-mode-test-issue-14 () |
| 76 | + "Array indentation." |
| 77 | + :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))))) |
| 89 | + |
| 90 | +(ert-deftest php-mode-test-issue-16 () |
| 91 | + "Comma separated \"use\" (namespaces). |
| 92 | +Gets the face of the text after the comma." |
| 93 | + (with-php-mode-test ("issue-16.php") |
| 94 | + (re-search-forward "^use " nil nil 3) |
| 95 | + (should (eq |
| 96 | + (get-text-property (search-forward ", ") 'face) |
| 97 | + 'font-lock-type-face)))) |
| 98 | + |
| 99 | +(ert-deftest php-mode-test-issue-18 () |
| 100 | + "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))))) |
| 110 | + |
| 111 | +(ert-deftest php-mode-test-issue-19 () |
| 112 | + "Alignment of arrow operators." |
| 113 | + :expected-result :failed |
| 114 | + (with-php-mode-test ("issue-19.php") |
| 115 | + (indent-region (point-min) (point-max)) |
| 116 | + (goto-char (point-min)) |
| 117 | + (while (re-search-forward "^\\s-*\\$object->") |
| 118 | + ;; Point is just after `->' |
| 119 | + (let ((col (current-column))) |
| 120 | + (search-forward "->") |
| 121 | + (should (eq (current-column) col)))))) |
| 122 | + |
| 123 | +(ert-deftest php-mode-test-issue-21 () |
| 124 | + "Font locking multi-line string." |
| 125 | + (with-php-mode-test ("issue-21.php") |
| 126 | + (search-forward "\"") |
| 127 | + (while (not (looking-at "\"")) |
| 128 | + (should (eq (get-text-property (point) 'face) |
| 129 | + 'font-lock-string-face)) |
| 130 | + (forward-char)))) |
| 131 | + |
| 132 | +(ert-deftest php-mode-test-issue-22 () |
| 133 | + "Font lock quotes within comments as regular comments. |
| 134 | +This applies for both single and double quotes." |
| 135 | + (with-php-mode-test ("issue-21.php") |
| 136 | + (while (search-forward "#" nil t) |
| 137 | + (while (not (looking-at "\n")) |
| 138 | + (should (eq (get-text-property (point) 'face) |
| 139 | + 'font-lock-comment-face)) |
| 140 | + (forward-char))))) |
| 141 | + |
| 142 | +(ert-deftest php-mode-test-issue-27 () |
| 143 | + "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)))) |
| 149 | + |
| 150 | +(ert-deftest php-mode-test-issue-28 () |
| 151 | + "Slowdown when scrolling. |
| 152 | +No obvious way to test this. One possibility is to record time it |
| 153 | +takes to scroll down the whole buffer using `next-line'. This may |
| 154 | +not cause the desired fontification, and it could take different |
| 155 | +amounts of time on different machines, so an absolute time would |
| 156 | +not be very useful. |
| 157 | +
|
| 158 | +This doesn't test anything, for now." |
| 159 | + (should t)) |
| 160 | + |
| 161 | +(ert-deftest php-mode-test-issue-29 () |
| 162 | + "Indentation of anonymous functions as arguments. |
| 163 | +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)))) |
| 170 | + |
| 171 | +(ert-deftest php-mode-test-issue-42 () |
| 172 | + "Error while indenting closures. |
| 173 | +If the bug has been fixed, indenting the buffer should not cause |
| 174 | +an error." |
| 175 | + (with-php-mode-test ("issue-42.php") |
| 176 | + (indent-region (point-min) (point-max)))) |
0 commit comments