Skip to content

Commit 02a1726

Browse files
danielmartintaku0
authored andcommitted
Introduce some font-lock tests
For now, they are only focused on swift-mode:negation-char-face.
1 parent 9757df9 commit 02a1726

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Negation characters.
3+
//
4+
5+
//
6+
// These "!" should be identified as negation chars.
7+
//
8+
9+
if !foo {} // #("if !foo {}" 0 2 (face swift-mode:keyword-face) 3 4 (face swift-mode:negation-char-face))
10+
11+
if !!!!!foo {} // #("if !!!!!foo {}" 0 2 (face swift-mode:keyword-face) 3 8 (face swift-mode:negation-char-face))
12+
13+
if !!!!!(foo) {} // #("if !!!!!(foo) {}" 0 2 (face swift-mode:keyword-face) 3 8 (face swift-mode:negation-char-face))
14+
15+
if (!foo) {} // #("if (!foo) {}" 0 2 (face swift-mode:keyword-face) 4 5 (face swift-mode:negation-char-face))
16+
17+
let x = [!a,!b] // #("let x = [!a,!b]" 0 3 (face swift-mode:keyword-face) 9 10 (face swift-mode:negation-char-face) 12 13 (face swift-mode:negation-char-face))
18+
19+
let x = [a:!b] // #("let x = [a:!b]" 0 3 (face swift-mode:keyword-face) 11 12 (face swift-mode:negation-char-face))
20+
21+
//
22+
// These "!" should NOT be identified as negation chars.
23+
//
24+
25+
try! foo // #("try! foo" 0 3 (face swift-mode:keyword-face))
26+
27+
foo as! Foo // #("foo as! Foo" 4 6 (face swift-mode:keyword-face))
28+
29+
foo != bar // "foo != bar"
30+
31+
foo !== bar // "foo !== bar"
32+
33+
a.b.c! // #("a.b.c!" 4 5 (face swift-mode:property-access-face))
34+
35+
a.b.c_! // #("a.b.c_!" 4 5 (face swift-mode:property-access-face))
36+
37+
a.b.aあ! // #("a.b.a\343\201\202!" 4 8 (face swift-mode:property-access-face))
38+
39+
init! {} // #("init! {}" 0 4 (face swift-mode:keyword-face))
40+
41+
let foo: Foo! = bar // #("let foo: Foo! = bar" 0 3 (face swift-mode:keyword-face))
42+
43+
let x = foo()! // #("let x = foo()!" 0 3 (face swift-mode:keyword-face) 8 11 (face swift-mode:function-call-face))
44+
45+
let x = foo[0]! // #("let x = foo[0]!" 0 3 (face swift-mode:keyword-face))
46+
47+
// Identifiers can be quoted.
48+
a.b.`c`! // #("a.b.`c`!" 4 7 (face font-lock-string-face))
49+
50+
// Custom operators.
51+
foo +!+!+!+!+ bbb // "foo +!+!+!+!+ bbb"

test/swift-mode-test-font-lock.el

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

test/swift-mode-test.el

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
(require 'swift-mode-test-indent)
3030
(require 'swift-mode-test-beginning-of-defun)
3131
(require 'swift-mode-test-imenu)
32+
(require 'swift-mode-test-font-lock)
3233

3334
(defvar swift-mode:test:basedir
3435
(file-name-directory (or load-file-name buffer-file-name)))
@@ -48,7 +49,8 @@ Return the error-buffer"
4849
(defvar swift-mode:tests
4950
'(swift-mode:run-test:indent
5051
swift-mode:run-test:beginning-of-defun
51-
swift-mode:run-test:imenu))
52+
swift-mode:run-test:imenu
53+
swift-mode:run-test:font-lock))
5254

5355
(defun swift-mode:run-test (&optional tests)
5456
"Run TESTS for `swift-mode'."

0 commit comments

Comments
 (0)