Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo: improve and unify function regexp #28

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rustic-cargo.el
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ If ARG is not nil, use value as argument and store it in
(defconst rustic-cargo-mod-regexp
"^\s*mod\s+\\([[:word:][:multibyte:]_][[:word:][:multibyte:]_[:digit:]]*\\)\s*{")
(defconst rustic-cargo-fn-regexp
"^\s*\\(?:async\s+\\)?\s*fn\s+\\([^(]+\\)\s*(")
(concat rustic-func-item-beg-re "\\([^(<]+\\)\\s-*\\(?:<\\s-*.+\\s-*>\\s-*\\)?("))

(defun rustic-cargo--get-test-target()
"Return either a full fn name or a mod name, whatever is closer to the point."
Expand Down
11 changes: 9 additions & 2 deletions rustic-interaction.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ visiting a project."
;; that we added an extra regexp for funcs.

(defvar rustic-func-item-beg-re
(concat "\\s-*\\(?:priv\\|pub\\)?\\s-*\\(?:async\\)?\\s-*"
(regexp-opt '("fn")))
(concat "^"
"\\(?:\\s-*"
(regexp-opt '("priv" "pub")) "\\s-*"
"\\(?:(\\s-*\\(?:in\\s-+\\)?" (regexp-opt '("crate" "self" "super")) "\\s-*)\\)?"
"\\)?"
"\\(?:\\s-*" (regexp-opt '("async" "const")) "\\s-+\\)?"
"\\(?:\\s-*unsafe\\s-+\\)?"
"\\(?:\\s-*extern\\(?:\\s-+\"C\"\\)?\\s-+\\)?"
"\\s-*fn\\s-+")
"Start of a rust function.")

(defun rustic-beginning-of-function ()
Expand Down
206 changes: 206 additions & 0 deletions test/rustic-cargo-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,212 @@ fn test1() {
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-pub ()
(let* ((string "#[test]
fn test1() {
}
#[test]
pub fn test2() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 4)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-pub-in-crate ()
(let* ((string "#[test]
fn test1() {
}
#[test]
pub(in crate) fn test2() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 4)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-pub-self ()
(let* ((string "#[test]
fn test1() {
}
#[test]
pub( self) fn test2() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 4)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-pub-super ()
(let* ((string "#[test]
pub (super ) fn test1() {
}
#[test]
fn test2() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 1)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-const ()
(let* ((string "#[test]
const fn test1() {
}
#[test]
fn test2() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 1)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-unsafe ()
(let* ((string "#[test]
unsafe fn test1() {
}
#[test]
fn test2() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 1)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-extern ()
(let* ((string "#[test]
extern \"C\" fn test1() {
}
#[test]
fn test2() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 1)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should-not (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-diamond ()
(let* ((string "#[test]
fn test1() {
}
#[test]
fn test2<'a>() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 4)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-combined ()
(let* ((string "#[test]
fn test1() {
}
pub(in self) const unsafe extern fn test2 <T >() {
}")
(default-directory (rustic-test-count-error-helper string))
(buf (get-buffer-create "test-current-test")))
(with-current-buffer buf
(insert string)
(goto-char (point-min))
(forward-line 4)
(let* ((proc (rustic-cargo-current-test))
(proc-buf (process-buffer proc)))
(rustic-test--wait-till-finished rustic-test-buffer-name)
(with-current-buffer proc-buf
(should-not (string-match "test1" (buffer-substring-no-properties (point-min) (point-max))))
(should (string-match "test2" (buffer-substring-no-properties (point-min) (point-max)))))
(kill-buffer proc-buf)))
(setq rustic-test-arguments "")
(kill-buffer buf)))

(ert-deftest rustic-test-cargo-current-test-no-test-found ()
;; test with use #46
(let* ((string "mod tests {
Expand Down
Loading