simple-tests.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. ;;; simple-test.el --- Tests for simple.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015-2017 Free Software Foundation, Inc.
  3. ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (require 'ert)
  16. (defmacro simple-test--dummy-buffer (&rest body)
  17. (declare (indent 0)
  18. (debug t))
  19. `(with-temp-buffer
  20. (emacs-lisp-mode)
  21. (setq indent-tabs-mode nil)
  22. (insert "(a b")
  23. (save-excursion (insert " c d)"))
  24. ,@body
  25. (cons (buffer-substring (point-min) (point))
  26. (buffer-substring (point) (point-max)))))
  27. (defmacro simple-test--transpositions (&rest body)
  28. (declare (indent 0)
  29. (debug t))
  30. `(with-temp-buffer
  31. (emacs-lisp-mode)
  32. (insert "(s1) (s2) (s3) (s4) (s5)")
  33. (backward-sexp 1)
  34. ,@body
  35. (cons (buffer-substring (point-min) (point))
  36. (buffer-substring (point) (point-max)))))
  37. ;;; `newline'
  38. (ert-deftest newline ()
  39. (should-error (newline -1))
  40. (should (equal (simple-test--dummy-buffer (newline 1))
  41. '("(a b\n" . " c d)")))
  42. (should (equal (simple-test--dummy-buffer
  43. (electric-indent-mode -1)
  44. (call-interactively #'newline))
  45. '("(a b\n" . " c d)")))
  46. (should (equal (simple-test--dummy-buffer
  47. (let ((current-prefix-arg 5))
  48. (call-interactively #'newline)))
  49. '("(a b\n\n\n\n\n" . " c d)")))
  50. (should (equal (simple-test--dummy-buffer (newline 5))
  51. '("(a b\n\n\n\n\n" . " c d)")))
  52. (should (equal (simple-test--dummy-buffer
  53. (forward-char 1)
  54. (newline 1))
  55. '("(a b \n" . "c d)"))))
  56. (ert-deftest newline-indent ()
  57. (should (equal (simple-test--dummy-buffer
  58. (electric-indent-local-mode 1)
  59. (newline 1))
  60. '("(a b\n" . " c d)")))
  61. (should (equal (simple-test--dummy-buffer
  62. (electric-indent-local-mode 1)
  63. (newline 1 'interactive))
  64. '("(a b\n " . "c d)")))
  65. (should (equal (simple-test--dummy-buffer
  66. (electric-indent-local-mode 1)
  67. (let ((current-prefix-arg nil))
  68. (call-interactively #'newline)
  69. (call-interactively #'newline)))
  70. '("(a b\n\n " . "c d)")))
  71. (should (equal (simple-test--dummy-buffer
  72. (electric-indent-local-mode 1)
  73. (newline 5 'interactive))
  74. '("(a b\n\n\n\n\n " . "c d)")))
  75. (should (equal (simple-test--dummy-buffer
  76. (electric-indent-local-mode 1)
  77. (let ((current-prefix-arg 5))
  78. (call-interactively #'newline)))
  79. '("(a b\n\n\n\n\n " . "c d)")))
  80. (should (equal (simple-test--dummy-buffer
  81. (forward-char 1)
  82. (electric-indent-local-mode 1)
  83. (newline 1 'interactive))
  84. '("(a b\n " . "c d)"))))
  85. ;;; `open-line'
  86. (ert-deftest open-line ()
  87. (should-error (open-line -1))
  88. (should-error (open-line))
  89. (should (equal (simple-test--dummy-buffer (open-line 1))
  90. '("(a b" . "\n c d)")))
  91. (should (equal (simple-test--dummy-buffer
  92. (electric-indent-mode -1)
  93. (call-interactively #'open-line))
  94. '("(a b" . "\n c d)")))
  95. (should (equal (simple-test--dummy-buffer
  96. (let ((current-prefix-arg 5))
  97. (call-interactively #'open-line)))
  98. '("(a b" . "\n\n\n\n\n c d)")))
  99. (should (equal (simple-test--dummy-buffer (open-line 5))
  100. '("(a b" . "\n\n\n\n\n c d)")))
  101. (should (equal (simple-test--dummy-buffer
  102. (forward-char 1)
  103. (open-line 1))
  104. '("(a b " . "\nc d)"))))
  105. (ert-deftest open-line-margin-and-prefix ()
  106. (should (equal (simple-test--dummy-buffer
  107. (let ((left-margin 10))
  108. (open-line 3)))
  109. '("(a b" . "\n\n\n c d)")))
  110. (should (equal (simple-test--dummy-buffer
  111. (forward-line 0)
  112. (let ((left-margin 2))
  113. (open-line 1)))
  114. '(" " . "\n (a b c d)")))
  115. (should (equal (simple-test--dummy-buffer
  116. (let ((fill-prefix "- - "))
  117. (open-line 1)))
  118. '("(a b" . "\n c d)")))
  119. (should (equal (simple-test--dummy-buffer
  120. (forward-line 0)
  121. (let ((fill-prefix "- - "))
  122. (open-line 1)))
  123. '("- - " . "\n(a b c d)"))))
  124. ;; For a while, from 24 Oct - 21 Nov 2015, `open-line' in the Emacs
  125. ;; development tree became sensitive to `electric-indent-mode', which
  126. ;; it had not been before. This sensitivity was reverted for the
  127. ;; Emacs 25 release, so it could be discussed further (see thread
  128. ;; "Questioning the new behavior of `open-line'." on the Emacs Devel
  129. ;; mailing list, and bug #21884).
  130. (ert-deftest open-line-indent ()
  131. (should (equal (simple-test--dummy-buffer
  132. (electric-indent-local-mode 1)
  133. (open-line 1))
  134. '("(a b" . "\n c d)")))
  135. (should (equal (simple-test--dummy-buffer
  136. (electric-indent-local-mode 1)
  137. (open-line 1))
  138. '("(a b" . "\n c d)")))
  139. (should (equal (simple-test--dummy-buffer
  140. (electric-indent-local-mode 1)
  141. (let ((current-prefix-arg nil))
  142. (call-interactively #'open-line)
  143. (call-interactively #'open-line)))
  144. '("(a b" . "\n\n c d)")))
  145. (should (equal (simple-test--dummy-buffer
  146. (electric-indent-local-mode 1)
  147. (open-line 5))
  148. '("(a b" . "\n\n\n\n\n c d)")))
  149. (should (equal (simple-test--dummy-buffer
  150. (electric-indent-local-mode 1)
  151. (let ((current-prefix-arg 5))
  152. (call-interactively #'open-line)))
  153. '("(a b" . "\n\n\n\n\n c d)")))
  154. (should (equal (simple-test--dummy-buffer
  155. (forward-char 1)
  156. (electric-indent-local-mode 1)
  157. (open-line 1))
  158. '("(a b " . "\nc d)"))))
  159. ;; From 24 Oct - 21 Nov 2015, `open-line' took a second argument
  160. ;; INTERACTIVE and ran `post-self-insert-hook' if the argument was
  161. ;; true. This test tested that. Currently, however, `open-line'
  162. ;; does not run run `post-self-insert-hook' at all, so for now
  163. ;; this test just makes sure that it doesn't.
  164. (ert-deftest open-line-hook ()
  165. (let* ((x 0)
  166. (inc (lambda () (setq x (1+ x)))))
  167. (simple-test--dummy-buffer
  168. (add-hook 'post-self-insert-hook inc nil 'local)
  169. (open-line 1))
  170. (should (= x 0))
  171. (simple-test--dummy-buffer
  172. (add-hook 'post-self-insert-hook inc nil 'local)
  173. (open-line 1))
  174. (should (= x 0))
  175. (unwind-protect
  176. (progn
  177. (add-hook 'post-self-insert-hook inc)
  178. (simple-test--dummy-buffer
  179. (open-line 1))
  180. (should (= x 0))
  181. (simple-test--dummy-buffer
  182. (open-line 10))
  183. (should (= x 0)))
  184. (remove-hook 'post-self-insert-hook inc))))
  185. ;;; `delete-trailing-whitespace'
  186. (ert-deftest simple-delete-trailing-whitespace--bug-21766 ()
  187. "Test bug#21766: delete-whitespace sometimes deletes non-whitespace."
  188. (defvar python-indent-guess-indent-offset) ; to avoid a warning
  189. (let ((python (featurep 'python))
  190. (python-indent-guess-indent-offset nil)
  191. (delete-trailing-lines t))
  192. (unwind-protect
  193. (with-temp-buffer
  194. (python-mode)
  195. (insert (concat "query = \"\"\"WITH filtered AS \n"
  196. "WHERE \n"
  197. "\"\"\".format(fv_)\n"
  198. "\n"
  199. "\n"))
  200. (delete-trailing-whitespace)
  201. (should (string-equal (buffer-string)
  202. (concat "query = \"\"\"WITH filtered AS\n"
  203. "WHERE\n"
  204. "\"\"\".format(fv_)\n"))))
  205. ;; Let's clean up if running interactive
  206. (unless (or noninteractive python)
  207. (unload-feature 'python)))))
  208. (ert-deftest simple-delete-trailing-whitespace--formfeeds ()
  209. "Test formfeeds are not deleted but whitespace past them is."
  210. (with-temp-buffer
  211. (with-syntax-table (make-syntax-table)
  212. (modify-syntax-entry ?\f " ") ; Make sure \f is whitespace
  213. (insert " \f \n \f \f \n\nlast\n")
  214. (delete-trailing-whitespace)
  215. (should (string-equal (buffer-string) " \f\n \f \f\n\nlast\n"))
  216. (should (equal ?\s (char-syntax ?\f)))
  217. (should (equal ?\s (char-syntax ?\n))))))
  218. ;;; auto-boundary tests
  219. (ert-deftest undo-auto-boundary-timer ()
  220. (should
  221. undo-auto-current-boundary-timer))
  222. (ert-deftest undo-auto--boundaries-added ()
  223. ;; The change in the buffer should have caused addition
  224. ;; to undo-auto--undoably-changed-buffers.
  225. (should
  226. (with-temp-buffer
  227. (setq buffer-undo-list nil)
  228. (insert "hello")
  229. (member (current-buffer) undo-auto--undoably-changed-buffers)))
  230. ;; The head of buffer-undo-list should be the insertion event, and
  231. ;; therefore not nil
  232. (should
  233. (with-temp-buffer
  234. (setq buffer-undo-list nil)
  235. (insert "hello")
  236. (car buffer-undo-list)))
  237. ;; Now the head of the buffer-undo-list should be a boundary and so
  238. ;; nil. We have to call auto-boundary explicitly because we are out
  239. ;; of the command loop
  240. (should-not
  241. (with-temp-buffer
  242. (setq buffer-undo-list nil)
  243. (insert "hello")
  244. (car buffer-undo-list)
  245. (undo-auto--boundaries 'test))))
  246. ;;; Transposition with negative args (bug#20698, bug#21885)
  247. (ert-deftest simple-transpose-subr ()
  248. (should (equal (simple-test--transpositions (transpose-sexps -1))
  249. '("(s1) (s2) (s4)" . " (s3) (s5)")))
  250. (should (equal (simple-test--transpositions (transpose-sexps -2))
  251. '("(s1) (s4)" . " (s2) (s3) (s5)"))))
  252. ;; Test for a regression introduced by undo-auto--boundaries changes.
  253. ;; https://lists.gnu.org/archive/html/emacs-devel/2015-11/msg01652.html
  254. (defun undo-test-kill-c-a-then-undo ()
  255. (with-temp-buffer
  256. (switch-to-buffer (current-buffer))
  257. (setq buffer-undo-list nil)
  258. (insert "a\nb\n\c\n")
  259. (goto-char (point-max))
  260. ;; We use a keyboard macro because it adds undo events in the same
  261. ;; way as if a user were involved.
  262. (kmacro-call-macro nil nil nil
  263. [left
  264. ;; Delete "c"
  265. backspace
  266. left left left
  267. ;; Delete "a"
  268. backspace
  269. ;; C-/ or undo
  270. 67108911
  271. ])
  272. (point)))
  273. (defun undo-test-point-after-forward-kill ()
  274. (with-temp-buffer
  275. (switch-to-buffer (current-buffer))
  276. (setq buffer-undo-list nil)
  277. (insert "kill word forward")
  278. ;; Move to word "word".
  279. (goto-char 6)
  280. (kmacro-call-macro nil nil nil
  281. [
  282. ;; kill-word
  283. C-delete
  284. ;; undo
  285. 67108911
  286. ])
  287. (point)))
  288. (ert-deftest undo-point-in-wrong-place ()
  289. (should
  290. ;; returns 5 with the bug
  291. (= 2
  292. (undo-test-kill-c-a-then-undo)))
  293. (should
  294. (= 6
  295. (undo-test-point-after-forward-kill))))
  296. (defmacro simple-test-undo-with-switched-buffer (buffer &rest body)
  297. (declare (indent 1) (debug t))
  298. (let ((before-buffer (make-symbol "before-buffer")))
  299. `(let ((,before-buffer (current-buffer)))
  300. (unwind-protect
  301. (progn
  302. (switch-to-buffer ,buffer)
  303. ,@body)
  304. (switch-to-buffer ,before-buffer)))))
  305. ;; This tests for a regression in emacs 25.0 see bug #23632
  306. (ert-deftest simple-test-undo-extra-boundary-in-tex ()
  307. (should
  308. (string=
  309. ""
  310. (simple-test-undo-with-switched-buffer
  311. "temp.tex"
  312. (latex-mode)
  313. ;; This macro calls `latex-insert-block'
  314. (execute-kbd-macro
  315. (read-kbd-macro
  316. "
  317. C-c C-o ;; latex-insert-block
  318. RET ;; newline
  319. C-/ ;; undo
  320. "
  321. ))
  322. (buffer-substring-no-properties
  323. (point-min)
  324. (point-max))))))
  325. (ert-deftest missing-record-point-in-undo ()
  326. "Check point is being restored correctly.
  327. See Bug#21722."
  328. (should
  329. (= 5
  330. (with-temp-buffer
  331. (generate-new-buffer " *temp*")
  332. (emacs-lisp-mode)
  333. (setq buffer-undo-list nil)
  334. (insert "(progn (end-of-line) (insert \"hello\"))")
  335. (beginning-of-line)
  336. (forward-char 4)
  337. (undo-boundary)
  338. (eval-defun nil)
  339. (undo-boundary)
  340. (undo)
  341. (point)))))
  342. (provide 'simple-test)
  343. ;;; simple-test.el ends here