branch: elpa/idris-mode commit c3e11a8117d0dc4e965b8d438e979ba9cd363331 Author: Marek L <nospam.ke...@gmail.com> Commit: Marek L <nospam.ke...@gmail.com>
Fix small bug in idris-xref jump to column and improve it's tests by addressing warnings from emacs lisp compiler and removing dependency on *Messages* buffer. --- idris-xref.el | 4 +- test/idris-xref-test.el | 262 ++++++++++++++++++++++-------------------------- 2 files changed, 121 insertions(+), 145 deletions(-) diff --git a/idris-xref.el b/idris-xref.el index bbc6cb88da..f8914116e7 100644 --- a/idris-xref.el +++ b/idris-xref.el @@ -93,8 +93,8 @@ update coordinates to be indexed from 1 as expected by Emacs." candidate)) (let ((new-fn (idris-xref-filepath term fn))) `(,term (:filename ,new-fn) - (:start ,(1+ start-line) ,(1+ start-col)) - (:end ,(1+ end-line) ,(1+ end-col)))))) + (:start ,(1+ start-line) ,start-col) + (:end ,(1+ end-line) ,end-col))))) (defun idris-xref-make-xref (location) "Return a new Xref object from LOCATION." diff --git a/test/idris-xref-test.el b/test/idris-xref-test.el index 57d62cb63e..7b8bcc4a88 100644 --- a/test/idris-xref-test.el +++ b/test/idris-xref-test.el @@ -21,24 +21,20 @@ (require 'ert) (require 'idris-xref) +(eval-when-compile (require 'cl-lib)) (ert-deftest idris-xref-backend-definitions--error-when-no-connection () "Test that the file is loaded before making search for definition." (let ((buffer (find-file-noselect "test-data/AddClause.idr")) - (inhibit-read-only t)) - (with-current-buffer "*Messages*" (erase-buffer)) + error-msg) (with-current-buffer buffer (goto-char (point-min)) (search-forward ": Test") (condition-case err (funcall-interactively 'xref-find-definitions "Test") - (error (message "Error: %s" (error-message-string err))) - (user-error (message "User Error: %s" (error-message-string err))))) + (error (setq error-msg (error-message-string err))))) - (with-current-buffer "*Messages*" - (should - (string-match-p "Buffer AddClause.idr has no process" - (buffer-substring-no-properties (point-min) (point-max))))) + (should (string-match-p "Buffer AddClause.idr has no process" error-msg)) ;; Cleanup (kill-buffer buffer))) @@ -46,12 +42,12 @@ (ert-deftest idris-xref-backend-definitions--not-supported-on-Idris-1 () "Test that user error raised when invoking `xref-find-definitions' used on Idris1." (let ((buffer (find-file-noselect "test-data/AddClause.idr")) - (err-msg "did not understand (synchronous Idris evaluation failed)") - (inhibit-read-only t)) - (with-current-buffer "*Messages*" (erase-buffer)) + (stub-err-msg "did not understand (synchronous Idris evaluation failed)") + error-msg) + (cl-flet ((idris-load-file-sync-stub () nil) (idris-eval-stub (&optional &rest _args) - (user-error err-msg))) + (user-error stub-err-msg))) (advice-add 'idris-load-file-sync :override #'idris-load-file-sync-stub) (advice-add 'idris-eval :override #'idris-eval-stub) @@ -62,14 +58,12 @@ (condition-case err (funcall-interactively 'xref-find-definitions "Test") - (error (message "%s" (error-message-string err))))) + (error (setq error-msg (error-message-string err))))) (advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub) (advice-remove 'idris-eval #'idris-eval-stub))) - (with-current-buffer "*Messages*" - (should - (string-match-p err-msg (buffer-substring-no-properties (point-min) (point-max))))) + (should (string-match-p stub-err-msg error-msg)) ;; Cleanup (kill-buffer buffer))) @@ -79,8 +73,8 @@ ;; Arrange (let ((buffer (find-file-noselect "test-data/AddClause.idr")) (idris-protocol-version 3) - (inhibit-read-only t)) - (with-current-buffer "*Messages*" (erase-buffer)) + error-msg) + (cl-flet ((idris-load-file-sync-stub () nil) (idris-eval-stub (&optional &rest _args) '())) (advice-add 'idris-load-file-sync :override #'idris-load-file-sync-stub) @@ -90,21 +84,16 @@ (with-current-buffer buffer (goto-char (point-min)) (search-forward ": Test") - ;; (with-demoted-errors "Error: %s" - ;; ;; Act - ;; (funcall-interactively 'xref-find-definitions "Test")) + (condition-case err (funcall-interactively 'xref-find-definitions "Test") - (user-error (message "%s" (error-message-string err))))) + (user-error (setq error-msg (error-message-string err))))) (advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub) (advice-remove 'idris-eval #'idris-eval-stub))) - (with-current-buffer "*Messages*" - ;; Assert - (should - (string-match-p "No definitions found for: Test" - (buffer-substring-no-properties (point-min) (point-max))))) + (should (string-match-p "No definitions found for: Test" error-msg)) + ;; Cleanup (kill-buffer buffer))) @@ -115,9 +104,8 @@ (eval-result `((("AddClause.Test" (:filename ,(buffer-file-name buffer)) (:start 2 0) - (:end 2 17))))) - (inhibit-read-only t)) - (with-current-buffer "*Messages*" (erase-buffer)) + (:end 2 17)))))) + (cl-flet ((idris-load-file-sync-stub () nil) (idris-eval-stub (&optional &rest _args) eval-result)) (advice-add 'idris-load-file-sync :override #'idris-load-file-sync-stub) @@ -129,9 +117,7 @@ (search-forward ": Test") (should (eq 6 (line-number-at-pos (point)))) - (condition-case err - (funcall-interactively 'xref-find-definitions "Test") - (user-error (message "%s" (error-message-string err)))) + (funcall-interactively 'xref-find-definitions "Test") (should (eq 3 (line-number-at-pos (point))))) @@ -149,8 +135,8 @@ (:filename "(Interactive)") (:start 0 0) (:end 0 0))))) - (inhibit-read-only t)) - (with-current-buffer "*Messages*" (erase-buffer)) + error-msg) + (cl-flet ((idris-load-file-sync-stub () nil) (idris-eval-stub (&optional &rest _args) eval-result)) (advice-add 'idris-load-file-sync :override #'idris-load-file-sync-stub) @@ -160,21 +146,16 @@ (with-current-buffer buffer (goto-char (point-min)) (search-forward ": Test") - ;; (with-demoted-errors "Error: %s" - ;; ;; Act - ;; (funcall-interactively 'xref-find-definitions "Test")) + (condition-case err (funcall-interactively 'xref-find-definitions "Test") - (user-error (message "%s" (error-message-string err))))) + (user-error (setq error-msg (error-message-string err))))) (advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub) (advice-remove 'idris-eval #'idris-eval-stub))) - (with-current-buffer "*Messages*" - ;; Assert - (should - (string-match-p "prim__lte_Bits64 : (Interactive)" - (buffer-substring-no-properties (point-min) (point-max))))) + (should (string-match-p "prim__lte_Bits64 : (Interactive)" error-msg)) + ;; Cleanup (kill-buffer buffer))) @@ -203,12 +184,8 @@ (with-current-buffer buffer (goto-char (point-min)) (search-forward ": Test") - ;; (with-demoted-errors "Error: %s" - ;; ;; Act - ;; (funcall-interactively 'xref-find-definitions "Test")) - (condition-case err - (funcall-interactively 'xref-find-definitions "Test") - (user-error (message "%s" (error-message-string err))))) + + (funcall-interactively 'xref-find-definitions "Test")) (advice-remove 'idris-load-file-sync #'idris-load-file-sync-stub) (advice-remove 'idris-eval #'idris-eval-stub))) @@ -230,99 +207,98 @@ The updated candidate should have absolute path to file when possible and coordinates indexed as expected by Emacs." (let* ((buffer (find-file-noselect "test-data/AddClause.idr")) - (candidate `("AddClause.Test" - (:filename ,(buffer-file-name buffer)) - (:start 2 0) - (:end 2 17))) - (result (idris-xref-normalise candidate))) - - (pcase-let ((`(,_term (:filename ,fn) - (:start ,start-line ,start-col) - (:end ,_end-line ,_end-col)) - result)) - (should (string= fn (buffer-file-name buffer))) - (should (eq start-line 3)) - (should (eq start-col 1)))) - - ;; Test that the filepath is reconstructed from term - ;; and Idris process current working directory - (let* ((buffer (find-file-noselect "test-data/AddClause.idr")) - (candidate `("AddClause.Test" - (:filename "(File-Not-Found)") - (:start 2 0) - (:end 2 17))) - (idris-process-current-working-directory (file-name-directory (buffer-file-name buffer))) - (result (idris-xref-normalise candidate))) - - (pcase-let ((`(,_term (:filename ,fn) - (:start ,start-line ,start-col) - (:end ,_end-line ,_end-col)) - result)) - (should (string= fn (buffer-file-name buffer))) - (should (eq start-line 3)) - (should (eq start-col 1)))) - - ;; Test that the original filename returned if no success to - ;; reconstruct real absolute file path - (let* ((buffer (find-file-noselect "test-data/AddClause.idr")) - (candidate `("AddClause.Test" - (:filename "(File-Not-Found)") - (:start 2 0) - (:end 2 17))) - (idris-process-current-working-directory nil) - (result (idris-xref-normalise candidate))) - - (pcase-let ((`(,_term (:filename ,fn) - (:start ,start-line ,start-col) - (:end ,_end-line ,_end-col)) - result)) - (should (string= fn "(File-Not-Found)")) - (should (eq start-line 3)) - (should (eq start-col 1)))) - - ;; Test that the filepath is reconstructed from term - ;; and (idris-xref-idris-source-directories) - (let* ((buffer (find-file-noselect "test-data/AddClause.idr")) - (candidate `("AddClause.Test" - (:filename "(File-Not-Found)") - (:start 2 0) - (:end 2 17))) - (idris-process-current-working-directory nil)) - (cl-flet ((idris-xref-idris-source-directories-stub - () - (cons (file-name-directory (buffer-file-name buffer)) '()))) - (advice-add 'idris-xref-idris-source-directories - :override #'idris-xref-idris-source-directories-stub) - - (let ((result (idris-xref-normalise candidate))) - (pcase-let ((`(,_term (:filename ,fn) - (:start ,start-line ,start-col) - (:end ,_end-line ,_end-col)) - result)) - - (should (string= fn (buffer-file-name buffer))) - (should (eq start-line 3)) - (should (eq start-col 1)))) - (advice-remove 'idris-xref-idris-source-directories - #'idris-xref-idris-source-directories-stub))) - - ;; Test that the filepath is reconstructed from term - ;; and path from idris-xref-idris-source-locations - (let* ((buffer (find-file-noselect "test-data/AddClause.idr")) - (candidate `("AddClause.Test" - (:filename "(File-Not-Found)") - (:start 2 0) - (:end 2 17))) - (idris-xref-idris-source-locations (cons (file-name-directory (buffer-file-name buffer)) '())) - (result (idris-xref-normalise candidate))) - - (pcase-let ((`(,_term (:filename ,fn) - (:start ,start-line ,start-col) - (:end ,_end-line ,_end-col)) - result)) - (should (string= fn (buffer-file-name buffer))) - (should (eq start-line 3)) - (should (eq start-col 1))))) + (abs-buffer-file-path (buffer-file-name buffer))) + (kill-buffer buffer) + + (let* ((candidate `("AddClause.Test" + (:filename ,abs-buffer-file-path) + (:start 2 0) + (:end 2 17))) + (result (idris-xref-normalise candidate))) + + (pcase-let ((`(,_term (:filename ,fn) + (:start ,start-line ,start-col) + (:end ,_end-line ,_end-col)) + result)) + (should (string= fn abs-buffer-file-path)) + (should (eq start-line 3)) + (should (eq start-col 0)))) + + ;; Test that the filepath is reconstructed from term + ;; and Idris process current working directory + (let* ((candidate `("AddClause.Test" + (:filename "(File-Not-Found)") + (:start 2 0) + (:end 2 17))) + (idris-process-current-working-directory (file-name-directory abs-buffer-file-path)) + (result (idris-xref-normalise candidate))) + + (pcase-let ((`(,_term (:filename ,fn) + (:start ,start-line ,start-col) + (:end ,_end-line ,_end-col)) + result)) + (should (string= fn abs-buffer-file-path)) + (should (eq start-line 3)) + (should (eq start-col 0)))) + + ;; Test that the original filename returned if no success to + ;; reconstruct real absolute file path + (let* ((candidate `("AddClause.Test" + (:filename "(File-Not-Found)") + (:start 2 0) + (:end 2 17))) + (idris-process-current-working-directory nil) + (result (idris-xref-normalise candidate))) + + (pcase-let ((`(,_term (:filename ,fn) + (:start ,start-line ,start-col) + (:end ,_end-line ,_end-col)) + result)) + (should (string= fn "(File-Not-Found)")) + (should (eq start-line 3)) + (should (eq start-col 0)))) + + ;; Test that the filepath is reconstructed from term + ;; and (idris-xref-idris-source-directories) + (let* ((candidate `("AddClause.Test" + (:filename "(File-Not-Found)") + (:start 2 0) + (:end 2 17))) + (idris-process-current-working-directory nil)) + (cl-flet ((idris-xref-idris-source-directories-stub + () + (cons (file-name-directory abs-buffer-file-path) '()))) + (advice-add 'idris-xref-idris-source-directories + :override #'idris-xref-idris-source-directories-stub) + + (let ((result (idris-xref-normalise candidate))) + (pcase-let ((`(,_term (:filename ,fn) + (:start ,start-line ,start-col) + (:end ,_end-line ,_end-col)) + result)) + + (should (string= fn abs-buffer-file-path)) + (should (eq start-line 3)) + (should (eq start-col 0)))) + (advice-remove 'idris-xref-idris-source-directories + #'idris-xref-idris-source-directories-stub))) + + ;; Test that the filepath is reconstructed from term + ;; and path from idris-xref-idris-source-locations + (let* ((candidate `("AddClause.Test" + (:filename "(File-Not-Found)") + (:start 2 0) + (:end 2 17))) + (idris-xref-idris-source-locations (cons (file-name-directory abs-buffer-file-path) '())) + (result (idris-xref-normalise candidate))) + + (pcase-let ((`(,_term (:filename ,fn) + (:start ,start-line ,start-col) + (:end ,_end-line ,_end-col)) + result)) + (should (string= fn abs-buffer-file-path)) + (should (eq start-line 3)) + (should (eq start-col 0)))))) (provide 'idris-xref-test) ;;; idris-xref-test.el ends here