Skip to content

Commit bdc54bc

Browse files
committed
Add useful test functions
1 parent 16d7568 commit bdc54bc

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ Besides the `lsp-mode` features, `lsp-dart` implements the [custom methods featu
7171

7272
`lsp-dart-run-test-at-point` - Run single test at point. [:warning:*](#features-only-available-for-dart-sdk-version-280-currently-the-dev-branch-or-above)
7373

74+
`lsp-dart-visit-last-test` - Go to last ran test.
75+
76+
`lsp-dart-run-last-test` - Run last ran test.
77+
7478
Running a test interactively:
7579

7680
![test](images/run-test.gif)

lsp-dart-test-support.el

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
;;; Code:
2626

27+
(require 'cl-lib)
2728
(require 'lsp-mode)
2829

2930
(require 'lsp-dart-project)
@@ -33,6 +34,12 @@
3334

3435
;;; Internal
3536

37+
(cl-defstruct lsp-dart-test
38+
(file-name nil)
39+
(names nil)
40+
(position nil)
41+
(kind nil))
42+
3643
(defun lsp-dart-test-support--test-kind-p (kind)
3744
"Return non-nil if KIND is a test type."
3845
(or (string= kind "UNIT_TEST_TEST")
@@ -92,20 +99,25 @@ otherwise the dart command."
9299
escaped-str nil t)))
93100
escaped-str))
94101

95-
(defun lsp-dart-test-support-run (buffer &optional names kind)
102+
(defun lsp-dart-test-support-run (test)
96103
"Run Dart/Flutter test command in a compilation buffer for BUFFER file.
97-
If NAMES is non nil, it will run only for KIND the test joining the name
98-
from NAMES."
104+
If TEST is non nil, it will run only this test."
99105
(interactive)
100106
(lsp-dart-test-support--from-project-root
101-
(let* ((test-file (file-relative-name (buffer-file-name buffer)
107+
(let* ((file-name (lsp-dart-test-file-name test))
108+
(buffer (get-file-buffer file-name))
109+
(names (lsp-dart-test-names test))
110+
(kind (lsp-dart-test-kind test))
111+
(test-file (file-relative-name file-name
102112
(lsp-dart-project-get-root)))
103113
(test-name (lsp-dart-test-support--build-test-name names))
104114
(group-kind? (string= kind "UNIT_TEST_GROUP"))
105115
(test-arg (when test-name
106116
(concat "--name '^"
107117
(lsp-dart-test-support--escape-test-name test-name)
108118
(if group-kind? "'" "$'")))))
119+
(when names
120+
(lsp-workspace-set-metadata "last-ran-test" test))
109121
(compilation-start (format "%s test %s %s"
110122
(lsp-dart-test-support--build-command buffer)
111123
(or test-arg "")
@@ -122,10 +134,13 @@ TEST-RANGE is the test method range."
122134
(beg-line (progn (goto-char beg)
123135
(line-beginning-position)))
124136
(spaces (make-string beg-position ?\s))
125-
(overlay (make-overlay beg-line end buffer)))
137+
(overlay (make-overlay beg-line end buffer))
138+
(test (make-lsp-dart-test :file-name (buffer-file-name buffer)
139+
:names names
140+
:position beg
141+
:kind kind)))
126142
(overlay-put overlay 'lsp-dart-test-code-lens t)
127-
(overlay-put overlay 'lsp-dart-test-names names)
128-
(overlay-put overlay 'lsp-dart-test-kind kind)
143+
(overlay-put overlay 'lsp-dart-test test)
129144
(overlay-put overlay 'lsp-dart-test-overlay-test-range (lsp--range-to-region test-range))
130145
(overlay-put overlay 'before-string
131146
(concat spaces
@@ -135,7 +150,7 @@ TEST-RANGE is the test method range."
135150
'local-map (-doto (make-sparse-keymap)
136151
(define-key [mouse-1] (lambda ()
137152
(interactive)
138-
(lsp-dart-test-support-run buffer names kind))))
153+
(lsp-dart-test-support-run test))))
139154
'font-lock-face 'lsp-lens-face)))))
140155

141156
(defun lsp-dart-test-support--add-code-lens (buffer items &optional names)
@@ -168,6 +183,28 @@ PARAMS is the notification data from outline."
168183
"Return non-nil if FILE-NAME is a dart test files."
169184
(string-match "_test.dart" file-name))
170185

186+
(defun lsp-dart-test-support-run-last-test ()
187+
"Visit the last ran test going to the test definition."
188+
(if-let ((test (lsp-workspace-get-metadata "last-ran-test")))
189+
(lsp-dart-test-support-run test)
190+
(lsp-dart-project-log "No last test found.")))
191+
192+
(defun lsp-dart-test-support-visit-last-test ()
193+
"Visit the last ran test going to the test definition."
194+
(-if-let* ((test (lsp-workspace-get-metadata "last-ran-test"))
195+
(file-name (lsp-dart-test-file-name test))
196+
(buffer (or (get-file-buffer file-name)
197+
(find-file file-name)))
198+
(position (lsp-dart-test-position test)))
199+
(if-let ((window (get-buffer-window buffer 'visible)))
200+
(progn
201+
(select-window window)
202+
(goto-char position))
203+
(with-current-buffer buffer
204+
(switch-to-buffer buffer nil t)
205+
(goto-char position)))
206+
(lsp-dart-project-log "No last test found.")))
207+
171208

172209
(provide 'lsp-dart-test-support)
173210
;;; lsp-dart-test-support.el ends here

lsp-dart.el

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,18 +421,28 @@ all test overlays in the current buffer."
421421
((beg2 . end2) (overlay-get other 'lsp-dart-test-overlay-test-range)))
422422
(and (< beg1 beg2)
423423
(> end1 end2))) it)
424-
(lsp-dart-test-support-run (current-buffer)
425-
(overlay-get it 'lsp-dart-test-names)
426-
(overlay-get it 'lsp-dart-test-kind))))
424+
(lsp-dart-test-support-run (overlay-get it 'lsp-dart-test))))
427425

428426
;;;###autoload
429427
(defun lsp-dart-run-test-file ()
430428
"Run dart/Flutter test command only for current buffer."
431429
(interactive)
432430
(if (lsp-dart-test-support-test-file-p (buffer-file-name))
433-
(lsp-dart-test-support-run (current-buffer))
431+
(lsp-dart-test-support-run (->> (current-buffer) buffer-name file-truename (make-lsp-dart-test :file-name)))
434432
(user-error "Current buffer is not a Dart/Flutter test file")))
435433

434+
;;;###autoload
435+
(defun lsp-dart-visit-last-test ()
436+
"Visit the last ran test going to test definition."
437+
(interactive)
438+
(lsp-dart-test-support-visit-last-test))
439+
440+
;;;###autoload
441+
(defun lsp-dart-run-last-test ()
442+
"Visit the last ran test going to test definition."
443+
(interactive)
444+
(lsp-dart-test-support-run-last-test))
445+
436446

437447
;;;###autoload(with-eval-after-load 'lsp-mode (require 'lsp-dart))
438448

0 commit comments

Comments
 (0)