From e55e64373c0b2ac0462df9e90417f854dcb16d11 Mon Sep 17 00:00:00 2001 From: jao Date: Mon, 18 May 2026 13:29:37 +0100 Subject: rust: improvements locating validation tests --- lib/prog/jao-rust.el | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'lib/prog') diff --git a/lib/prog/jao-rust.el b/lib/prog/jao-rust.el index 5e20744..2281875 100644 --- a/lib/prog/jao-rust.el +++ b/lib/prog/jao-rust.el @@ -34,17 +34,23 @@ (let* ((root (expand-file-name (jao-rust--root))) (current (file-name-sans-extension (buffer-file-name))) (rel (string-replace root "" current))) - (thread-last - rel - (replace-regexp-in-string "^src\\(/bin\\)?/" "") - (string-replace "-" "_") - (replace-regexp-in-string "/\\(mod\\|lib\\|main\\)$" "") - (string-replace "/" "::")))) + (and (string-prefix-p "src/" rel) + (thread-last + rel + (replace-regexp-in-string "^src\\(/bin\\)?/" "") + (string-replace "-" "_") + (replace-regexp-in-string "/\\(mod\\|lib\\|main\\)$" "") + (string-replace "/" "::"))))) + +(defun jao-rust--tests-prefix () + (if-let* ((m (jao-rust-current-module-name))) + (concat m "::tests::") + "")) (defconst jao-rust--test-header-rx (concat "#\\[" (regexp-opt '("test" "quickcheck" "tokio::test") t))) -(defconst jao-rust--test-name-rx "^ *\\(pub\\)?\\( async\\)? fn \\([^(]+\\)(") +(defconst jao-rust--test-name-rx "^ *\\(pub \\)?\\( async \\)?fn \\([^(]+\\)(") (defun jao-rust-test-at-point () (when (looking-at jao-rust--test-name-rx) (match-string-no-properties 3))) @@ -62,6 +68,9 @@ (interactive) (or (re-search-forward jao-rust--tests-mod-rx nil t) (re-search-backward jao-rust--tests-mod-rx nil t) + (progn + (goto-char (point-min)) + (re-search-forward jao-rust--test-header-rx nil t)) (when (y-or-n-p "No tests module. Add it?") (goto-char (point-max)) (insert "\n#[cfg(test)]\nmod tests {\n\n}") @@ -69,9 +78,9 @@ (defun jao-rust-test-current () (interactive) - (if-let* ((m (jao-rust-current-module-name)) + (if-let* ((m (jao-rust--tests-prefix)) (n (jao-rust-test-before-point))) - (let ((rustic-test-arguments (format "-- --exact %s::tests::%s" m n)) + (let ((rustic-test-arguments (format "-- --exact %s%s" m n)) (rustic-cargo-use-last-stored-arguments t)) (rustic-cargo-test)) (message "No test before point"))) @@ -89,9 +98,9 @@ (interactive) (let ((tests (jao-rust-buffer-tests))) (if (seq-empty-p tests) - (messages "No tests in this module") - (let* ((mod (jao-rust-current-module-name)) - (tests (mapconcat (lambda (x) (format "%s::tests::%s" mod x)) tests " ")) + (message "No tests in this module") + (let* ((prefix (jao-rust--tests-prefix)) + (tests (mapconcat (lambda (x) (format "%s%s" prefix x)) tests " ")) (rustic-test-arguments (format "-- --exact %s" tests)) (rustic-cargo-use-last-stored-arguments t)) (rustic-cargo-test))))) -- cgit v1.2.3