Skip to content

Commit 212eb89

Browse files
committed
Merge pull request #1033 from gracjan/pr-with-temp-switch-to-buffer
Cleanup tests with with-temp-switch-to-buffer
2 parents d98356c + 79d2982 commit 212eb89

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

tests/haskell-indentation-tests.el

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -850,32 +850,46 @@ fact n =
850850
(4 0 2 4 6)
851851
(5 0 2 4 6))
852852

853+
(defmacro with-temp-switch-to-buffer (&rest body)
854+
"Create a temporary buffer, use `switch-to-buffer' and evaluate BODY there like `progn'.
855+
856+
Seems that `execute-kbd-macro' is not able to correctly execute keybindings without this."
857+
(declare (indent 0) (debug t))
858+
(let ((temp-buffer (make-symbol "temp-buffer")))
859+
`(let ((,temp-buffer (generate-new-buffer " *temp*")))
860+
;; FIXME: kill-buffer can change current-buffer in some odd cases.
861+
(unwind-protect
862+
(progn
863+
(switch-to-buffer ,temp-buffer)
864+
,@body)
865+
(and (buffer-name ,temp-buffer)
866+
(kill-buffer ,temp-buffer))))))
867+
853868
(ert-deftest haskell-indentation-ret-indents ()
854-
(switch-to-buffer (get-buffer-create "source.hs"))
855-
(haskell-mode)
856-
(insert "main = do")
857-
(execute-kbd-macro (kbd "<RET>"))
858-
(should (equal 2 (count-lines (point-min) (point))))
859-
(should (equal 2 (- (point) (line-beginning-position)))))
869+
(with-temp-switch-to-buffer
870+
(haskell-mode)
871+
(insert "main = do")
872+
873+
(execute-kbd-macro (kbd "<RET>"))
874+
(should (equal 2 (- (point) (line-beginning-position))))))
860875

861876
(ert-deftest haskell-indentation-tab-and-backtab ()
862-
(switch-to-buffer (get-buffer-create "source.hs"))
863-
(haskell-mode)
864-
(insert "main = do\n lala\n ")
865-
(execute-kbd-macro (kbd "TAB"))
866-
(should (equal 4 (count-lines (point-min) (point))))
867-
(should (equal 4 (- (point) (line-beginning-position))))
868-
(execute-kbd-macro (kbd "<backtab>"))
869-
(should (equal 4 (count-lines (point-min) (point))))
870-
(should (equal 2 (- (point) (line-beginning-position)))))
877+
(with-temp-switch-to-buffer
878+
(haskell-mode)
879+
(insert "main = do\n lala\n ")
880+
881+
(execute-kbd-macro (kbd "TAB"))
882+
(should (equal 4 (- (point) (line-beginning-position))))
883+
884+
(execute-kbd-macro (kbd "<backtab>"))
885+
(should (equal 2 (- (point) (line-beginning-position))))))
871886

872887
(ert-deftest haskell-indentation-altj-comment ()
873888
:expected-result :failed
874-
(switch-to-buffer (get-buffer-create "another.hs"))
875-
(haskell-mode)
876-
(insert "main = do\n return ()\n\n-- comment")
877-
(execute-kbd-macro (kbd "M-j"))
878-
(should (equal 2 (count-lines (point-min) (point))))
879-
(should (equal 3 (- (point) (line-beginning-position)))))
889+
(with-temp-switch-to-buffer
890+
(haskell-mode)
891+
(insert "main = do\n return ()\n\n-- comment")
892+
(execute-kbd-macro (kbd "M-j"))
893+
(should (equal 3 (- (point) (line-beginning-position))))))
880894

881895
;;; haskell-indentation-tests.el ends here

0 commit comments

Comments
 (0)