Refactor undo tests

* yasnippet-tests.el (yas-test-expand-and-undo): New helper function.
(undo-indentation-1, undo-indentation-2, undo-indentation-multiline-1)
(undo-indentation-multiline-2): Use it.
This commit is contained in:
Noam Postavsky 2017-11-25 18:16:58 -05:00
parent 7ea1305e67
commit 1292cd263a

View File

@ -297,77 +297,51 @@ attention to case differences."
;; (should (string= (yas--buffer-contents)
;; "brother from another mother!"))))
(ert-deftest undo-indentation-1 ()
"Check undoing works when only line of snippet is indented."
(let ((yas-also-auto-indent-first-line t))
(defun yas-test-expand-and-undo (mode snippet-entry initial-contents)
(yas-with-snippet-dirs
'((".emacs.d/snippets" ("emacs-lisp-mode" ("s" . "(setq $0)"))))
`((".emacs.d/snippets" (,(symbol-name mode) ,snippet-entry)))
(with-temp-buffer
(emacs-lisp-mode)
(funcall mode)
(yas-reload-all)
(yas-minor-mode 1)
(insert "(let\n(while s")
(yas-expand-snippet initial-contents)
(let ((pre-expand-string (buffer-string)))
(setq buffer-undo-list ())
(ert-simulate-command '(yas-expand))
;; Need undo barrier, I think command loop puts it normally.
(push nil buffer-undo-list)
(should (string= (buffer-string) "(let\n (while (setq )"))
(ert-simulate-command '(undo))
(should (string= (buffer-string) "(let\n(while s"))))))
(should (string= (buffer-string) pre-expand-string))))))
(ert-deftest undo-indentation-1 ()
"Check undoing works when only line of snippet is indented."
(let ((yas-also-auto-indent-first-line t))
(yas-test-expand-and-undo
'emacs-lisp-mode '("s" . "(setq $0)") "(let\n(while s$0")))
(ert-deftest undo-indentation-2 ()
"Check undoing works when only line of snippet is indented."
(let ((yas-also-auto-indent-first-line t)
(indent-tabs-mode nil))
(yas-with-snippet-dirs
'((".emacs.d/snippets" ("emacs-lisp-mode" ("t" . "; TODO"))))
(with-temp-buffer
(emacs-lisp-mode)
(yas-reload-all)
(yas-minor-mode 1)
(insert "t")
(setq buffer-undo-list ())
(ert-simulate-command '(yas-expand))
;; Need undo barrier, I think command loop puts it normally.
(push nil buffer-undo-list)
(should (string= (buffer-string) (concat (make-string comment-column ?\s) "; TODO")))
(ert-simulate-command '(undo))
(should (string= (buffer-string) "t"))))))
(yas-test-expand-and-undo
'emacs-lisp-mode '("t" . "; TODO") "t$0")))
(ert-deftest undo-indentation-multiline-1 ()
"Check undoing works when 1st line of multi-line snippet is indented."
(yas-with-snippet-dirs
'((".emacs.d/snippets" ("js-mode" ("if" . "if ($1) {\n\n}\n"))))
(with-temp-buffer
(js-mode)
(yas-reload-all)
(yas-minor-mode 1)
(insert "if\nabc = 123456789 + abcdef;")
(setq buffer-undo-list ())
(goto-char (point-min))
(search-forward "if")
(ert-simulate-command '(yas-expand))
(push nil buffer-undo-list) ; See test above.
(ert-simulate-command '(undo))
(should (string= (buffer-string) "if\nabc = 123456789 + abcdef;")))))
(let ((yas-also-auto-indent-first-line t)
(indent-tabs-mode nil))
(yas-test-expand-and-undo
'js-mode '("if" . "if ($1) {\n\n}\n")
"if$0\nabc = 123456789 + abcdef;")))
(ert-deftest undo-indentation-multiline-2 ()
"Check undoing works when 2nd line of multi-line snippet is indented."
(yas-with-snippet-dirs
'((".emacs.d/snippets" ("js-mode" ("if" . "if (true) {\n${1:foo};\n}\n"))))
(with-temp-buffer
(js-mode)
(yas-reload-all)
(yas-minor-mode 1)
(insert "if\nabc = 123456789 + abcdef;")
(setq buffer-undo-list ())
(goto-char (point-min))
(search-forward "if")
(ert-simulate-command '(yas-expand))
(push nil buffer-undo-list) ; See test above.
(ert-simulate-command '(undo))
(should (string= (buffer-string) "if\nabc = 123456789 + abcdef;")))))
(let ((yas-also-auto-indent-first-line t)
(indent-tabs-mode nil))
(yas-test-expand-and-undo
'js-mode '("if" . "if (true) {\n${1:foo};\n}\n")
"if$0\nabc = 123456789 + abcdef;")))
(ert-deftest dont-clear-on-partial-deletion-issue-515 ()
"Ensure fields are not cleared when user doesn't really mean to."