|
| 1 | +;;; swift-mode-test-font-lock.el --- Test for swift-mode: font-lock -*- lexical-binding: t -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2020 Daniel Martín |
| 4 | + |
| 5 | +;; Authors: Daniel Martín (http://github.com/taku0) |
| 6 | + |
| 7 | +;; This file is not part of GNU Emacs. |
| 8 | + |
| 9 | +;; This program is free software: you can redistribute it and/or modify |
| 10 | +;; it under the terms of the GNU General Public License as published by |
| 11 | +;; the Free Software Foundation, either version 3 of the License, or |
| 12 | +;; (at your option) any later version. |
| 13 | + |
| 14 | +;; This program is distributed in the hope that it will be useful, |
| 15 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +;; GNU General Public License for more details. |
| 18 | + |
| 19 | +;; You should have received a copy of the GNU General Public License |
| 20 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +;;; Commentary: |
| 23 | + |
| 24 | +;; Test for swift-mode: font-lock. |
| 25 | +;; Execute swift-mode:run-test:font-lock interactively or in batch mode. |
| 26 | + |
| 27 | +;;; Code: |
| 28 | + |
| 29 | +(require 'swift-mode) |
| 30 | +(require 'swift-mode-font-lock) |
| 31 | + |
| 32 | +(defun swift-mode:run-test:font-lock |
| 33 | + (&optional error-buffer error-counts progress-reporter) |
| 34 | + "Run font-lock test for `swift-mode'. |
| 35 | +
|
| 36 | +ERROR-BUFFER is the buffer to output errors. |
| 37 | +ERROR-COUNTS is a association list holding counts of errors. Updated |
| 38 | +destructively. |
| 39 | +PROGRESS-REPORTER is the progress-reporter." |
| 40 | + (interactive) |
| 41 | + (if (not swift-mode:test:running) |
| 42 | + (swift-mode:run-test '(swift-mode:run-test:font-lock)) |
| 43 | + (let ((current-line 0)) |
| 44 | + (setq default-directory |
| 45 | + (concat (file-name-as-directory swift-mode:test:basedir) |
| 46 | + (file-name-as-directory "swift-files") |
| 47 | + "font-lock")) |
| 48 | + |
| 49 | + (dolist (swift-file (file-expand-wildcards "*.swift")) |
| 50 | + (redisplay) |
| 51 | + (with-temp-buffer |
| 52 | + (switch-to-buffer (current-buffer)) |
| 53 | + (insert-file-contents-literally swift-file) |
| 54 | + (swift-mode) |
| 55 | + (font-lock-fontify-buffer) |
| 56 | + (setq current-line 0) |
| 57 | + (while (not (eobp)) |
| 58 | + (when (not noninteractive) |
| 59 | + (progress-reporter-update progress-reporter)) |
| 60 | + (setq current-line (1+ current-line)) |
| 61 | + (cond |
| 62 | + ((= (line-beginning-position) (line-end-position)) |
| 63 | + ;; Empty line |
| 64 | + nil) |
| 65 | + ((looking-at-p "//.*") |
| 66 | + ;; Ignore comments |
| 67 | + nil) |
| 68 | + (t |
| 69 | + (let* |
| 70 | + ((status (swift-mode:test-current-line-font-lock |
| 71 | + swift-file current-line error-buffer)) |
| 72 | + (count-assoc (assq status error-counts))) |
| 73 | + (setcdr count-assoc (1+ (cdr count-assoc)))))) |
| 74 | + (forward-line))))))) |
| 75 | + |
| 76 | +(defun swift-mode:test-current-line-font-lock |
| 77 | + (swift-file current-line error-buffer) |
| 78 | + "Compute the font-lock properties applied by swift-mode on current line. |
| 79 | +
|
| 80 | +SWIFT-FILE is the filename of the current test case. |
| 81 | +CURRENT-LINE is the current line number. |
| 82 | +ERROR-BUFFER is the buffer to output errors." |
| 83 | + (let ((status 'ok)) |
| 84 | + (when (looking-at "\\(.*\\)[ /t]+//[ /t]+\\(.*\\)") |
| 85 | + (let ((actual-props (format "%S" (buffer-substring (match-beginning 1) (match-end 1)))) |
| 86 | + (expected-props (buffer-substring-no-properties (match-beginning 2) |
| 87 | + (match-end 2)))) |
| 88 | + (when (not (string-equal expected-props actual-props)) |
| 89 | + (setq status 'error) |
| 90 | + (swift-mode:show-error |
| 91 | + error-buffer swift-file current-line |
| 92 | + "error" |
| 93 | + (concat |
| 94 | + "font-lock: expected " |
| 95 | + (prin1-to-string expected-props) |
| 96 | + " but " |
| 97 | + (prin1-to-string actual-props)))))) |
| 98 | + status)) |
| 99 | + |
| 100 | +(provide 'swift-mode-test-font-lock) |
| 101 | + |
| 102 | +;;; swift-mode-test-font-lock.el ends here |
0 commit comments