-
Notifications
You must be signed in to change notification settings - Fork 24
/
lsp-dart-test-output.el
210 lines (173 loc) · 8.3 KB
/
lsp-dart-test-output.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
;;; lsp-dart-test-output.el --- Test output features and decorations -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Eric Dallo
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; Test output features and decorations
;;
;;; Code:
(require 'rx)
(require 'lsp-dart-protocol)
(require 'lsp-dart-utils)
(defcustom lsp-dart-test-pop-to-buffer-on-run 'display-only
"Controls whether to pop to the tests buffer on run.
When set to nil the buffer will only be created, and not displayed.
When set to `display-only' the buffer will be displayed, but it will
not become focused, otherwise the buffer is displayed and focused."
:group 'lsp-dart
:type '(choice (const :tag "Create the buffer, but don't display it" nil)
(const :tag "Create and display the buffer, but don't focus it" display-only)
(const :tag "Create, display, and focus the buffer" t)))
;;; Internal
(defconst lsp-dart-test-output--passed-icon "★")
(defconst lsp-dart-test-output--success-icon "✔")
(defconst lsp-dart-test-output--skipped-icon "•")
(defconst lsp-dart-test-output--hidden-icon "○")
(defconst lsp-dart-test-output--error-icon "✖")
(defvar lsp-dart-test-output--tests-count 0)
(defvar lsp-dart-test-output--tests-passed 0)
(defvar lsp-dart-test-output--show-loading-tests-message t)
(defconst lsp-dart-test-output--buffer-name "*LSP Dart tests*")
(defconst lsp-dart-test-output--exception-re
(rx (or (and (zero-or-more any)
(or "exception" "EXCEPTION")
(zero-or-more any))
"<asynchronous suspension>"
(and "#"
(one-or-more
any)))))
(defconst lsp-dart-test-output--expected-actual-re
(rx (or (and (zero-or-more blank)
"Expected:"
(zero-or-more any))
(and (zero-or-more blank)
"Actual:"
(zero-or-more any)))))
(defconst lsp-dart-test--font-lock
`((,lsp-dart-test-output--exception-re . 'error)
(,lsp-dart-test-output--expected-actual-re . 'warning)))
(defvar lsp-dart-test--output-font-lock
'((lsp-dart-test--font-lock)))
(lsp-defun lsp-dart-test-output--get-icon ((&TestDoneNotification :result :skipped :hidden))
"Return the icon for test done notification."
(cond
(hidden
lsp-dart-test-output--hidden-icon)
((and (string= result "success")
skipped)
lsp-dart-test-output--skipped-icon)
((and (string= result "success")
(not skipped))
lsp-dart-test-output--success-icon)
(t lsp-dart-test-output--error-icon)))
(lsp-defun lsp-dart-test-output--get-face ((&TestDoneNotification :result :skipped :hidden))
"Return the icon for test done notification."
(cond
(hidden
'font-lock-comment-face)
((and (string= result "success")
skipped)
'homoglyph)
((and (string= result "success")
(not skipped))
'success)
(t 'error)))
(defun lsp-dart-test-output--send (message &rest args)
"Send MESSAGE with ARGS to test buffer."
(let* ((inhibit-read-only t))
(with-current-buffer (lsp-dart-test-output--get-buffer-create)
(save-excursion
(goto-char (point-max))
(insert (apply #'format (concat message "\n") args))))))
(defun lsp-dart-test-output--get-buffer-create ()
"Create a buffer for test display."
(let ((buffer (get-buffer-create lsp-dart-test-output--buffer-name)))
(with-current-buffer buffer
(setq-local default-directory (or (lsp-dart-get-project-root) default-directory))
(unless (derived-mode-p 'lsp-dart-test-output-content-mode)
(lsp-dart-test-output-content-mode))
(current-buffer))))
(defun lsp-dart-test-output--show-buffer ()
"Show test buffer."
(let ((test-buffer (lsp-dart-test-output--get-buffer-create))
(inhibit-read-only t))
(with-current-buffer test-buffer
(let ((inhibit-read-only t))
(erase-buffer)))
(pcase lsp-dart-test-pop-to-buffer-on-run
(`display-only
(let ((orig-buffer (current-buffer)))
(display-buffer test-buffer)
(set-buffer orig-buffer)))
((pred identity) (pop-to-buffer test-buffer)))))
(defun lsp-dart-test-output--handle-run-started ()
"Handle test run started."
(setq lsp-dart-test-output--show-loading-tests-message t)
(lsp-dart-test-output--show-buffer)
(lsp-dart-test-output--send "Spawning test process..."))
(defun lsp-dart-test-output--handle-all-start (_notification)
"Handle all start notification."
(lsp-dart-test-output--send "Running tests...")
(setq lsp-dart-test-output--tests-count 0)
(setq lsp-dart-test-output--tests-passed 0))
(lsp-defun lsp-dart-test-output--handle-start ((&TestStartNotification :test (&Test :group-i-ds)))
(unless (seq-empty-p group-i-ds)
(setq lsp-dart-test-output--tests-count (1+ lsp-dart-test-output--tests-count))))
(lsp-defun lsp-dart-test-output--handle-done ((notification &as &TestDoneNotification :result :time :hidden) test-name test-start-time)
"Handle test done notification."
(when lsp-dart-test-output--show-loading-tests-message
(with-current-buffer (lsp-dart-test-output--get-buffer-create)
(let ((inhibit-read-only t))
(erase-buffer)))
(setq lsp-dart-test-output--show-loading-tests-message nil))
(let ((text (propertize (concat (lsp-dart-test-output--get-icon notification)
" "
test-name)
'font-lock-face (lsp-dart-test-output--get-face notification))))
(if hidden
(lsp-dart-test-output--send "%s" text)
(progn
(when (string= result "success")
(setq lsp-dart-test-output--tests-passed (1+ lsp-dart-test-output--tests-passed)))
(let ((formatted-time (propertize (format "(%s ms)"
(- time test-start-time))
'font-lock-face 'font-lock-comment-face)))
(lsp-dart-test-output--send "%s %s" text formatted-time))))))
(lsp-defun lsp-dart-test-output--handle-all-done ((&DoneNotification :success))
"Handle all tests done notification."
(if success
(lsp-dart-test-output--send (propertize (format "\n%s All ran tests passed %s" lsp-dart-test-output--passed-icon lsp-dart-test-output--passed-icon)
'font-lock-face 'success))
(lsp-dart-test-output--send (propertize (format "\n● %s/%s tests passed" lsp-dart-test-output--tests-passed lsp-dart-test-output--tests-count)
'font-lock-face font-lock-warning-face))))
(lsp-defun lsp-dart-test-output--handle-print ((&PrintNotification :message))
"Handle test print notification."
(lsp-dart-test-output--send "%s" message))
(lsp-defun lsp-dart-test-output--handle-error ((&ErrorNotification :error :stack-trace))
"Handle test error notification."
(lsp-dart-test-output--send "%s" error)
(lsp-dart-test-output--send "%s" stack-trace))
(define-derived-mode lsp-dart-test-output-content-mode special-mode lsp-dart-test-output--buffer-name
"Major mode for buffer running tests."
(setq font-lock-defaults lsp-dart-test--output-font-lock))
(add-hook 'lsp-dart-test-run-started-hook #'lsp-dart-test-output--handle-run-started)
(add-hook 'lsp-dart-test-all-start-notification-hook #'lsp-dart-test-output--handle-all-start)
(add-hook 'lsp-dart-test-start-notification-hook #'lsp-dart-test-output--handle-start)
(add-hook 'lsp-dart-test-done-notification-hook #'lsp-dart-test-output--handle-done)
(add-hook 'lsp-dart-test-all-done-notification-hook #'lsp-dart-test-output--handle-all-done)
(add-hook 'lsp-dart-test-print-notification-hook #'lsp-dart-test-output--handle-print)
(add-hook 'lsp-dart-test-error-notification-hook #'lsp-dart-test-output--handle-error)
(provide 'lsp-dart-test-output)
;;; lsp-dart-test-output.el ends here