Skip to content

Commit 062ca04

Browse files
authored
Merge pull request #63 from emacs-lsp/refactor-tests-structure
Refactor tests structure
2 parents 1889ac7 + f185ae1 commit 062ca04

File tree

6 files changed

+344
-236
lines changed

6 files changed

+344
-236
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- 26.1
1818
- 26.2
1919
- 26.3
20+
- 27.1
2021
- snapshot
2122

2223
steps:

lsp-dart-code-lens.el

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,20 @@ If TEST-FILE? debug tests otherwise debug application."
131131
test-file?)
132132
"\n"))))
133133

134-
(lsp-defun lsp-dart-code-lens--build-test-overlay (buffer names kind test-range (range &as &Range :start
135-
(&Position :character beg-position)))
134+
(lsp-defun lsp-dart-code-lens--build-test-overlay (test)
136135
"Build an overlay in BUFFER for a test NAMES of KIND.
137136
TEST-RANGE is the test method range.
138137
RANGE is the overlay range to build."
139-
(-let* (((beg . end) (lsp--range-to-region range))
138+
(-let* (((beg . end) (plist-get test :element-range))
139+
(beg-position (-> (lsp--region-to-range beg end)
140+
(plist-get :start)
141+
(plist-get :character)))
140142
(beg-line (progn (goto-char beg)
141143
(line-beginning-position)))
142144
(spaces (make-string beg-position ?\s))
143-
(overlay (make-overlay beg-line end buffer))
144-
(test (make-lsp-dart-test-len :file-name (buffer-file-name buffer)
145-
:names names
146-
:position beg
147-
:kind kind))
145+
(overlay (make-overlay beg-line end (current-buffer)))
148146
(separator (propertize " " 'font-lock-face 'lsp-dart-code-lens-separator)))
149147
(overlay-put overlay 'lsp-dart-test-code-lens t)
150-
(overlay-put overlay 'lsp-dart-test test)
151-
(overlay-put overlay 'lsp-dart-code-lens-overlay-test-range (lsp--range-to-region test-range))
152148
(overlay-put overlay 'before-string
153149
(concat spaces
154150
(lsp-dart-code-lens--build-action "Run"
@@ -164,21 +160,6 @@ RANGE is the overlay range to build."
164160
test)
165161
"\n"))))
166162

167-
(defun lsp-dart-code-lens--add-test (buffer items &optional names)
168-
"Add test code lens to BUFFER for ITEMS.
169-
NAMES arg is optional and are the group of tests representing a test name."
170-
(seq-doseq (item items)
171-
(-let* (((&Outline :children :code-range test-range :element
172-
(&Element :kind :name :range)) item)
173-
(test-kind? (lsp-dart-test--test-kind-p kind))
174-
(concatened-names (if test-kind?
175-
(append names (list name))
176-
names)))
177-
(when test-kind?
178-
(lsp-dart-code-lens--build-test-overlay buffer (append names (list name)) kind test-range range))
179-
(unless (seq-empty-p children)
180-
(lsp-dart-code-lens--add-test buffer children concatened-names)))))
181-
182163
(lsp-defun lsp-dart--main-code-lens-check ((&OutlineNotification :uri :outline (&Outline :children)))
183164
"Check URI and outline for main method adding lens to it."
184165
(-let* ((buffer (find-buffer-visiting (lsp--uri-to-path uri)))
@@ -190,14 +171,12 @@ NAMES arg is optional and are the group of tests representing a test name."
190171
(save-excursion
191172
(lsp-dart-code-lens--build-main-overlay buffer main-outline)))))))
192173

193-
(lsp-defun lsp-dart--test-code-lens-check ((&OutlineNotification :uri :outline (&Outline :children)))
194-
"Check URI and outline for test adding lens to it."
195-
(when (lsp-dart-test-file-p uri)
196-
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
197-
(with-current-buffer buffer
198-
(remove-overlays (point-min) (point-max) 'lsp-dart-test-code-lens t)
199-
(save-excursion
200-
(lsp-dart-code-lens--add-test buffer children))))))
174+
(lsp-defun lsp-dart--set-test-lens (tests)
175+
"Add lens to the given TESTS."
176+
(save-excursion
177+
(remove-overlays (point-min) (point-max) 'lsp-dart-test-code-lens t)
178+
(seq-doseq (test tests)
179+
(lsp-dart-code-lens--build-test-overlay test))))
201180

202181

203182
;; Public
@@ -225,7 +204,7 @@ NAMES arg is optional and are the group of tests representing a test name."
225204
(lsp-dart-define-key "t L" #'lsp-dart-debug-last-test)
226205
(lsp-dart-define-key "t a" #'lsp-dart-run-all-tests)
227206
(lsp-dart-define-key "t v" #'lsp-dart-visit-last-test)
228-
(add-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--test-code-lens-check nil t))
207+
(add-hook 'lsp-dart-tests-added-hook #'lsp-dart--set-test-lens nil t))
229208
(t
230209
(lsp-dart-define-key "t t" 'ignore)
231210
(lsp-dart-define-key "t T" 'ignore)
@@ -235,7 +214,7 @@ NAMES arg is optional and are the group of tests representing a test name."
235214
(lsp-dart-define-key "t a" 'ignore)
236215
(lsp-dart-define-key "t v" 'ignore)
237216
(remove-overlays (point-min) (point-max) 'lsp-dart-test-code-lens t)
238-
(remove-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--test-code-lens-check t))))
217+
(remove-hook 'lsp-dart-tests-added-hook #'lsp-dart--set-test-lens t))))
239218

240219
(provide 'lsp-dart-code-lens)
241220
;;; lsp-dart-code-lens.el ends here

lsp-dart-test-output.el

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

0 commit comments

Comments
 (0)