@@ -41,64 +41,116 @@ PROGRESS-REPORTER is the progress-reporter."
4141 (interactive )
4242 (if (not swift-mode:test:running)
4343 (swift-mode:run-test '(swift-mode:run-test:font-lock))
44- (let ((current-line 0 ))
45- (setq default-directory
46- (concat (file-name-as-directory swift-mode:test:basedir)
47- (file-name-as-directory " swift-files" )
48- " font-lock" ))
49-
50- (dolist (swift-file (file-expand-wildcards " *.swift" ))
51- (redisplay )
52- (with-temp-buffer
53- (switch-to-buffer (current-buffer ))
54- (insert-file-contents-literally swift-file)
55- (swift-mode)
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+ (dolist (swift-file (file-expand-wildcards " *.swift" ))
49+ (redisplay )
50+ (with-temp-buffer
51+ (switch-to-buffer (current-buffer ))
52+ (insert-file-contents-literally swift-file)
53+ (swift-mode)
54+ (let ((tests (swift-mode:parse-font-lock-test))
55+ status
56+ count-assoc)
5657 (funcall (if (fboundp 'font-lock-ensure )
5758 #'font-lock-ensure
5859 #'font-lock-fontify-buffer ))
59- (setq current-line 0 )
60- (while (not (eobp ))
60+ (dolist (test tests)
6161 (when (not noninteractive)
6262 (progress-reporter-update progress-reporter))
63- (setq current-line (1+ current-line))
64- (cond
65- ((= (line-beginning-position ) (line-end-position ))
66- ; ; Empty line
67- nil )
68- ((looking-at-p " //.*" )
69- ; ; Ignore comments
70- nil )
71- (t
72- (let*
73- ((status (swift-mode:test-current-line-font-lock
74- swift-file current-line error-buffer))
75- (count-assoc (assq status error-counts)))
76- (setcdr count-assoc (1+ (cdr count-assoc))))))
77- (forward-line )))))))
78-
79- (defun swift-mode:test-current-line-font-lock
80- (swift-file current-line error-buffer)
81- " Compute the font-lock properties applied by swift-mode on current line.
63+ (setq status (swift-mode:test-font-lock-1
64+ swift-file
65+ (nth 0 test)
66+ (nth 1 test)
67+ (nth 2 test)
68+ error-buffer))
69+ (setq count-assoc (assq status error-counts))
70+ (setcdr count-assoc (1+ (cdr count-assoc)))))))))
71+
72+ (defun swift-mode:parse-font-lock-test ()
73+ (save-excursion
74+ (goto-char (point-min ))
75+ (let ((tests nil )
76+ (current-line 0 ))
77+ (while (progn
78+ (setq current-line (1+ current-line))
79+ (when (looking-at
80+ " \\ (.*\\ )[\s\t ]+//[\s\t ]+EXPECTED:[\s\t ]+\\ (.*\\ )$" )
81+ (push (list (point )
82+ current-line
83+ (read
84+ (concat
85+ " ("
86+ (match-string-no-properties 2 )
87+ " )" )))
88+ tests)
89+ (delete-region (match-end 1 ) (line-end-position )))
90+ (eq 0 (forward-line )))
91+ t )
92+ (reverse tests))))
93+
94+ (defun swift-mode:test-font-lock-1
95+ (swift-file pos current-line expected error-buffer)
96+ " Compute the font-lock properties applied by Swift mode on the line at POS.
8297
8398SWIFT-FILE is the filename of the current test case.
8499CURRENT-LINE is the current line number.
100+ EXPECTED is expected faces computed by `swift-mode:get-faces-of-current-line' .
85101ERROR-BUFFER is the buffer to output errors."
86- (let ((status 'ok ))
87- (when (looking-at " \\ (.*\\ )[ /t]+//[ /t]+\\ (.*\\ )" )
88- (let ((actual-props (format " %S " (buffer-substring (match-beginning 1 ) (match-end 1 ))))
89- (expected-props (buffer-substring-no-properties (match-beginning 2 )
90- (match-end 2 ))))
91- (when (not (string-equal expected-props actual-props))
92- (setq status 'error )
93- (swift-mode:show-error
94- error-buffer swift-file current-line
95- " error"
96- (concat
97- " font-lock: expected "
98- (prin1-to-string expected-props)
99- " but "
100- (prin1-to-string actual-props))))))
101- status))
102+ (save-excursion
103+ (goto-char pos)
104+ (let ((actual nil ))
105+ (dolist (cons (swift-mode:get-faces-of-current-line))
106+ (push (car cons ) actual)
107+ (push (cdr cons ) actual))
108+ (setq actual (reverse actual))
109+ (if (equal expected actual)
110+ 'ok
111+ (swift-mode:show-error
112+ error-buffer swift-file current-line
113+ " error"
114+ (format " font-lock: expected %S but %S " expected actual))
115+ 'error ))))
116+
117+ (defun swift-mode:get-faces-of-current-line ()
118+ (save-excursion
119+ (let ((faces nil ))
120+ (beginning-of-line )
121+ (while (not (eolp ))
122+ (let ((face (get-text-property (point ) 'face ))
123+ (start (point ))
124+ (end (progn
125+ (goto-char (next-single-property-change
126+ (point )
127+ 'face
128+ nil
129+ (line-end-position )))
130+ (point ))))
131+ (when face
132+ (push (cons (buffer-substring-no-properties start end)
133+ face)
134+ faces))))
135+ (reverse faces))))
136+
137+ (defun swift-mode:add-expected ()
138+ (when (fboundp 'string-replace )
139+ (save-excursion
140+ (let ((faces (swift-mode:get-faces-of-current-line)))
141+ (end-of-line )
142+ (insert " // EXPECTED:" )
143+ (dolist (tuple faces)
144+ (insert
145+ (format " %s %s "
146+ (string-replace " *" " \\ *" (prin1-to-string (car tuple)))
147+ (cdr tuple))))))))
148+
149+ ; ; (progn
150+ ; ; (goto-char (point-max))
151+ ; ; (while (search-backward " // ★" nil t)
152+ ; ; (replace-match "")
153+ ; ; (swift-mode:add-expected)))
102154
103155(provide 'swift-mode-test-font-lock )
104156
0 commit comments