Add the base of tests for lem-vi-mode#965
Conversation
061d146 to
f3b1a74
Compare
cxxxr
left a comment
There was a problem hiding this comment.
The time spent reading this PR was very meaningful and educational.
I made some comments, but it is not MUST.
extensions/vi-mode/tests/utils.lisp
Outdated
| (defmacro with-test-buffer ((var buffer-string | ||
| &rest buffer-args | ||
| &key name (temporary t temporary-specified-p) | ||
| &allow-other-keys) | ||
| &body body) | ||
| (declare (ignore temporary)) | ||
| (unless temporary-specified-p | ||
| (setf (getf buffer-args :temporary) t)) | ||
| (remove-from-plistf buffer-args :name) | ||
| (with-gensyms (point buffer-content position visual-regions) | ||
| `(with-fake-interface () | ||
| (let* ((,var (make-buffer ,name ,@buffer-args)) | ||
| (,point (buffer-point ,var))) | ||
| (multiple-value-bind (,buffer-content ,position ,visual-regions) | ||
| (parse-buffer-string ,buffer-string) | ||
| (insert-string ,point ,buffer-content) | ||
| (move-to-position ,point ,position) | ||
| (dolist (region ,visual-regions) | ||
| (destructuring-bind (from . to) region | ||
| (with-point ((start ,point) | ||
| (end ,point)) | ||
| (move-to-position start from) | ||
| (move-to-position end to) | ||
| (push (lem:make-overlay start end 'lem:region) | ||
| lem-vi-mode/visual::*visual-overlays*))))) | ||
| ,@body)))) | ||
|
|
||
| (defmacro with-current-buffer ((buffer) &body body) | ||
| (with-gensyms (window) | ||
| (once-only (buffer) | ||
| `(lem-base::with-current-buffers () | ||
| (let ((lem-base::*current-buffer* ,buffer) | ||
| (,window (current-window))) | ||
| (lem-core::set-window-buffer ,buffer ,window) | ||
| (lem-core::set-window-view-point (copy-point (lem:buffer-point ,buffer)) | ||
| ,window) | ||
| ,@body))))) | ||
|
|
||
| (defmacro with-vi-state ((state) &body body) | ||
| `(let* ((lem-vi-mode/core::*current-state* (ensure-state (keyword-to-state ,state)))) | ||
| (change-global-mode-keymap | ||
| 'vi-mode | ||
| (lem-vi-mode/core::state-keymap lem-vi-mode/core::*current-state*)) | ||
| ,@body)) | ||
|
|
||
| (defmacro with-vi-tests ((buffer &key (state :normal)) &body body) | ||
| `(with-current-buffer (,buffer) | ||
| (lem-core:change-buffer-mode ,buffer 'vi-mode) | ||
| (with-vi-state (,state) | ||
| (rove:testing (format nil "[buf] \"~A\"" | ||
| (text-backslashed | ||
| (make-buffer-string (current-buffer)))) | ||
| (locally | ||
| (declare #+sbcl (sb-ext:muffle-conditions sb-ext:code-deletion-note)) | ||
| (labels ((cmd (keys) | ||
| (check-type keys string) | ||
| (rove:diag (format nil "[cmd] ~A" keys)) | ||
| (execute-key-sequence | ||
| (parse-command-keys keys))) | ||
| (pos= (expected-point) | ||
| (point= (current-point) expected-point)) | ||
| (text= (expected-buffer-text) | ||
| (string= (buffer-text (current-buffer)) | ||
| expected-buffer-text)) | ||
| (state= (expected-state) | ||
| (eq (keyword-to-state expected-state) | ||
| (current-state))) | ||
| (buf= (expected-buffer-string) | ||
| (check-type expected-buffer-string string) | ||
| (multiple-value-bind (expected-buffer-text expected-position) | ||
| (parse-buffer-string expected-buffer-string) | ||
| (with-point ((p (current-point))) | ||
| (move-to-position p expected-position) | ||
| (and (text= expected-buffer-text) | ||
| (pos= p)))))) | ||
| ,@body)))))) |
There was a problem hiding this comment.
https://google.github.io/styleguide/lispguide.xml?showone=Macros#Macros
Since huge macros are not maintainable, how about using call-with-style?
| (deftest vi-forward-char | ||
| (with-test-buffer (b #?"[a]bcdef\n") | ||
| (with-vi-tests (b) | ||
| (cmd "l") | ||
| (ok (buf= #?"a[b]cdef\n")) | ||
| (cmd "3l") | ||
| (ok (buf= #?"abcd[e]f\n")) | ||
| (cmd "10l") | ||
| (ok (buf= #?"abcde[f]\n"))))) |
extensions/vi-mode/tests/utils.lisp
Outdated
| (:import-from :rove | ||
| :form-description | ||
| :diag | ||
| :testing) | ||
| (:import-from :lem-base | ||
| :*current-buffer*) | ||
| (:import-from :lem-fake-interface | ||
| :with-fake-interface) | ||
| (:import-from :lem-vi-mode | ||
| :vi-mode) | ||
| (:import-from :lem-vi-mode/core | ||
| :*current-state* | ||
| :state-keymap | ||
| :normal | ||
| :insert | ||
| :current-state | ||
| :ensure-state) | ||
| (:import-from :lem-vi-mode/visual | ||
| :visual-line | ||
| :visual-block | ||
| :apply-visual-range) | ||
| (:import-from :cl-ppcre | ||
| :scan | ||
| :regex-replace) | ||
| (:import-from :alexandria | ||
| :remove-from-plistf | ||
| :with-gensyms | ||
| :once-only | ||
| :appendf | ||
| :if-let) |
There was a problem hiding this comment.
It seems that there are symbols that have been imported but not used.
Can you check with M-x lisp-organize-import?
There was a problem hiding this comment.
Why haven't I known this feature?
This must be one of the good reasons to recommend people to use Lem.
extensions/vi-mode/tests/utils.lisp
Outdated
| (setf (getf buffer-args :temporary) t)) | ||
| (remove-from-plistf buffer-args :name) | ||
| (with-gensyms (point buffer-content position visual-regions) | ||
| `(with-fake-interface () |
There was a problem hiding this comment.
I am a little concerned about the with-fake-interface being hidden inside this macro.
The person reading the test needs to dig into the macro to understand that this is using a fake-interface.
How about moving the use of with-fake-interface to the top level of testing?
b0512e4 to
6dfa792
Compare
|
@cxxxr |
6bbbbd2 to
83d1a79
Compare
|
Thank you so much! |
Usage
with-test-buffercreates a buffer object for testingwith-vi-bufferto set the initial vi state and bind the given buffer (or string) to be the current buffercmd: Executes vi commands by key types in a stringh,G,<Esc>,3h,5j^D,<C-x>o,<H-Shift-x>,<M-Space>.buf=: Check if the current buffer text equals the given buffer's text, and the cursor position is righttext=: Check if the current buffer text equals the given buffer's textpos=: Check if the current point is at the same position as the given pointstate=: Check if the vi state is the given one.Examples
Test definitions
[x]means a cursor that is on the characterx.<and>means a region of vi's visual mode.(deftest vi-forward-char (with-fake-interface () (with-vi-buffer (#?"[a]bcdef\n") ;; `l`: vi-forward-char (cmd "l") (ok (buf= #?"a[b]cdef\n")) ;; `l` accepts a universal argument (cmd "3l") (ok (buf= #?"abcd[e]f\n")) ;; `l` doesn't exceed the end of the line (cmd "10l") (ok (buf= #?"abcde[f]\n")) (cmd "hv^") (ok (buf= #?"<[a]bcde>f\n")) (cmd "vlV") (ok (buf= #?"<a[b]cdef\n>")))))))Test outputs