PageRenderTime 67ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/test/automated/ert-x-tests.el

https://github.com/T-force/emacs
Emacs Lisp | 273 lines | 212 code | 25 blank | 36 comment | 4 complexity | b67c31a4caf4f6530c6626d5e756f20b MD5 | raw file
  1. ;;; ert-x-tests.el --- Tests for ert-x.el
  2. ;; Copyright (C) 2008, 2010-2011 Free Software Foundation, Inc.
  3. ;; Author: Phil Hagelberg
  4. ;; Christian Ohler <ohler@gnu.org>
  5. ;; This file is part of GNU Emacs.
  6. ;; This program is free software: you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation, either version 3 of the
  9. ;; License, or (at your option) any later version.
  10. ;;
  11. ;; This program is distributed in the hope that it will be useful, but
  12. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;; General Public License for more details.
  15. ;;
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with this program. If not, see `http://www.gnu.org/licenses/'.
  18. ;;; Commentary:
  19. ;; This file is part of ERT, the Emacs Lisp Regression Testing tool.
  20. ;; See ert.el or the texinfo manual for more details.
  21. ;;; Code:
  22. (eval-when-compile
  23. (require 'cl))
  24. (require 'ert)
  25. (require 'ert-x)
  26. ;;; Utilities
  27. (ert-deftest ert-test-buffer-string-reindented ()
  28. (ert-with-test-buffer (:name "well-indented")
  29. (insert (concat "(hello (world\n"
  30. " 'elisp)\n"))
  31. (emacs-lisp-mode)
  32. (should (equal (ert-buffer-string-reindented) (buffer-string))))
  33. (ert-with-test-buffer (:name "badly-indented")
  34. (insert (concat "(hello\n"
  35. " world)"))
  36. (emacs-lisp-mode)
  37. (should-not (equal (ert-buffer-string-reindented) (buffer-string)))))
  38. (defun ert--hash-table-to-alist (table)
  39. (let ((accu nil))
  40. (maphash (lambda (key value)
  41. (push (cons key value) accu))
  42. table)
  43. (nreverse accu)))
  44. (ert-deftest ert-test-test-buffers ()
  45. (let (buffer-1
  46. buffer-2)
  47. (let ((test-1
  48. (make-ert-test
  49. :name 'test-1
  50. :body (lambda ()
  51. (ert-with-test-buffer (:name "foo")
  52. (should (string-match
  53. "[*]Test buffer (ert-test-test-buffers): foo[*]"
  54. (buffer-name)))
  55. (setq buffer-1 (current-buffer))))))
  56. (test-2
  57. (make-ert-test
  58. :name 'test-2
  59. :body (lambda ()
  60. (ert-with-test-buffer (:name "bar")
  61. (should (string-match
  62. "[*]Test buffer (ert-test-test-buffers): bar[*]"
  63. (buffer-name)))
  64. (setq buffer-2 (current-buffer))
  65. (ert-fail "fail for test"))))))
  66. (let ((ert--test-buffers (make-hash-table :weakness t)))
  67. (ert-run-tests `(member ,test-1 ,test-2) #'ignore)
  68. (should (equal (ert--hash-table-to-alist ert--test-buffers)
  69. `((,buffer-2 . t))))
  70. (should-not (buffer-live-p buffer-1))
  71. (should (buffer-live-p buffer-2))))))
  72. (ert-deftest ert-filter-string ()
  73. (should (equal (ert-filter-string "foo bar baz" "quux")
  74. "foo bar baz"))
  75. (should (equal (ert-filter-string "foo bar baz" "bar")
  76. "foo baz")))
  77. (ert-deftest ert-propertized-string ()
  78. (should (ert-equal-including-properties
  79. (ert-propertized-string "a" '(a b) "b" '(c t) "cd")
  80. #("abcd" 1 2 (a b) 2 4 (c t))))
  81. (should (ert-equal-including-properties
  82. (ert-propertized-string "foo " '(face italic) "bar" " baz" nil
  83. " quux")
  84. #("foo bar baz quux" 4 11 (face italic)))))
  85. ;;; Tests for ERT itself that require test features from ert-x.el.
  86. (ert-deftest ert-test-run-tests-interactively-2 ()
  87. :tags '(:causes-redisplay)
  88. (let ((passing-test (make-ert-test :name 'passing-test
  89. :body (lambda () (ert-pass))))
  90. (failing-test (make-ert-test :name 'failing-test
  91. :body (lambda ()
  92. (ert-info ((propertize "foo\nbar"
  93. 'a 'b))
  94. (ert-fail
  95. "failure message"))))))
  96. (let ((ert-debug-on-error nil))
  97. (let* ((buffer-name (generate-new-buffer-name "*ert-test-run-tests*"))
  98. (messages nil)
  99. (mock-message-fn
  100. (lambda (format-string &rest args)
  101. (push (apply #'format format-string args) messages))))
  102. (flet ((expected-string (with-font-lock-p)
  103. (ert-propertized-string
  104. "Selector: (member <passing-test> <failing-test>)\n"
  105. "Passed: 1\n"
  106. "Failed: 1 (1 unexpected)\n"
  107. "Total: 2/2\n\n"
  108. "Started at:\n"
  109. "Finished.\n"
  110. "Finished at:\n\n"
  111. `(category ,(button-category-symbol
  112. 'ert--results-progress-bar-button)
  113. button (t)
  114. face ,(if with-font-lock-p
  115. 'ert-test-result-unexpected
  116. 'button))
  117. ".F" nil "\n\n"
  118. `(category ,(button-category-symbol
  119. 'ert--results-expand-collapse-button)
  120. button (t)
  121. face ,(if with-font-lock-p
  122. 'ert-test-result-unexpected
  123. 'button))
  124. "F" nil " "
  125. `(category ,(button-category-symbol
  126. 'ert--test-name-button)
  127. button (t)
  128. ert-test-name failing-test)
  129. "failing-test"
  130. nil "\n Info: " '(a b) "foo\n"
  131. nil " " '(a b) "bar"
  132. nil "\n (ert-test-failed \"failure message\")\n\n\n"
  133. )))
  134. (save-window-excursion
  135. (unwind-protect
  136. (let ((case-fold-search nil))
  137. (ert-run-tests-interactively
  138. `(member ,passing-test ,failing-test) buffer-name
  139. mock-message-fn)
  140. (should (equal messages `(,(concat
  141. "Ran 2 tests, 1 results were "
  142. "as expected, 1 unexpected"))))
  143. (with-current-buffer buffer-name
  144. (font-lock-mode 0)
  145. (should (ert-equal-including-properties
  146. (ert-filter-string (buffer-string)
  147. '("Started at:\\(.*\\)$" 1)
  148. '("Finished at:\\(.*\\)$" 1))
  149. (expected-string nil)))
  150. ;; `font-lock-mode' only works if interactive, so
  151. ;; pretend we are.
  152. (let ((noninteractive nil))
  153. (font-lock-mode 1))
  154. (should (ert-equal-including-properties
  155. (ert-filter-string (buffer-string)
  156. '("Started at:\\(.*\\)$" 1)
  157. '("Finished at:\\(.*\\)$" 1))
  158. (expected-string t)))))
  159. (when (get-buffer buffer-name)
  160. (kill-buffer buffer-name)))))))))
  161. (ert-deftest ert-test-describe-test ()
  162. "Tests `ert-describe-test'."
  163. (save-window-excursion
  164. (ert-with-buffer-renamed ("*Help*")
  165. (if (< emacs-major-version 24)
  166. (should (equal (should-error (ert-describe-test 'ert-describe-test))
  167. '(error "Requires Emacs 24")))
  168. (ert-describe-test 'ert-test-describe-test)
  169. (with-current-buffer "*Help*"
  170. (let ((case-fold-search nil))
  171. (should (string-match (concat
  172. "\\`ert-test-describe-test is a test"
  173. " defined in `ert-x-tests.elc?'\\.\n\n"
  174. "Tests `ert-describe-test'\\.\n\\'")
  175. (buffer-string)))))))))
  176. (ert-deftest ert-test-message-log-truncation ()
  177. :tags '(:causes-redisplay)
  178. (let ((test (make-ert-test
  179. :body (lambda ()
  180. ;; Emacs would combine messages if we
  181. ;; generate the same message multiple
  182. ;; times.
  183. (message "a")
  184. (message "b")
  185. (message "c")
  186. (message "d")))))
  187. (let (result)
  188. (ert-with-buffer-renamed ("*Messages*")
  189. (let ((message-log-max 2))
  190. (setq result (ert-run-test test)))
  191. (should (equal (with-current-buffer "*Messages*"
  192. (buffer-string))
  193. "c\nd\n")))
  194. (should (equal (ert-test-result-messages result) "a\nb\nc\nd\n")))))
  195. (ert-deftest ert-test-builtin-message-log-flushing ()
  196. "This test attempts to demonstrate that there is no way to
  197. force immediate truncation of the *Messages* buffer from Lisp
  198. \(and hence justifies the existence of
  199. `ert--force-message-log-buffer-truncation'\): The only way that
  200. came to my mind was \(message \"\"\), which doesn't have the
  201. desired effect."
  202. :tags '(:causes-redisplay)
  203. (ert-with-buffer-renamed ("*Messages*")
  204. (with-current-buffer "*Messages*"
  205. (should (equal (buffer-string) ""))
  206. ;; We used to get sporadic failures in this test that involved
  207. ;; a spurious newline at the beginning of the buffer, before
  208. ;; the first message. Below, we print a message and erase the
  209. ;; buffer since this seems to eliminate the sporadic failures.
  210. (message "foo")
  211. (erase-buffer)
  212. (should (equal (buffer-string) ""))
  213. (let ((message-log-max 2))
  214. (let ((message-log-max t))
  215. (loop for i below 4 do
  216. (message "%s" i))
  217. (should (equal (buffer-string) "0\n1\n2\n3\n")))
  218. (should (equal (buffer-string) "0\n1\n2\n3\n"))
  219. (message "")
  220. (should (equal (buffer-string) "0\n1\n2\n3\n"))
  221. (message "Test message")
  222. (should (equal (buffer-string) "3\nTest message\n"))))))
  223. (ert-deftest ert-test-force-message-log-buffer-truncation ()
  224. :tags '(:causes-redisplay)
  225. (labels ((body ()
  226. (loop for i below 3 do
  227. (message "%s" i)))
  228. ;; Uses the implicit messages buffer truncation implemented
  229. ;; in Emacs' C core.
  230. (c (x)
  231. (ert-with-buffer-renamed ("*Messages*")
  232. (let ((message-log-max x))
  233. (body))
  234. (with-current-buffer "*Messages*"
  235. (buffer-string))))
  236. ;; Uses our lisp reimplementation.
  237. (lisp (x)
  238. (ert-with-buffer-renamed ("*Messages*")
  239. (let ((message-log-max t))
  240. (body))
  241. (let ((message-log-max x))
  242. (ert--force-message-log-buffer-truncation))
  243. (with-current-buffer "*Messages*"
  244. (buffer-string)))))
  245. (loop for x in '(0 1 2 3 4 t) do
  246. (should (equal (c x) (lisp x))))))
  247. (provide 'ert-x-tests)
  248. ;;; ert-x-tests.el ends here