PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lisp/tempo-tests.el

https://gitlab.com/RobertCochran/emacs
Emacs Lisp | 228 lines | 175 code | 30 blank | 23 comment | 1 complexity | d3d22ae4c0bd680daa3f98fa40084b68 MD5 | raw file
  1. ;;; tempo-tests.el --- Test suite for tempo.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2019 Free Software Foundation, Inc.
  3. ;; Author: Federico Tedin <federicotedin@gmail.com>
  4. ;; Keywords: abbrev
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  16. ;;; Code:
  17. (require 'tempo)
  18. (eval-when-compile (require 'cl-lib))
  19. (ert-deftest tempo-string-element-test ()
  20. "Test a template containing a string element."
  21. (with-temp-buffer
  22. (tempo-define-template "test" '("GNU Emacs Tempo test"))
  23. (tempo-insert-template 'tempo-template-test nil)
  24. (should (equal (buffer-string) "GNU Emacs Tempo test"))))
  25. (ert-deftest tempo-p-bare-element-test ()
  26. "Test a template containing a bare `p' element."
  27. (with-temp-buffer
  28. (tempo-define-template "test" '("abcde" p))
  29. (tempo-insert-template 'tempo-template-test nil)
  30. (tempo-forward-mark)
  31. (should (equal (point) 6))))
  32. (ert-deftest tempo-r-bare-element-test ()
  33. "Test a template containing a bare `r' element."
  34. (with-temp-buffer
  35. (tempo-define-template "test" '("abcde" r "ghijk"))
  36. (insert "F")
  37. (set-mark (point))
  38. (goto-char (point-min))
  39. (tempo-insert-template 'tempo-template-test t)
  40. (should (equal (buffer-string) "abcdeFghijk"))))
  41. (ert-deftest tempo-p-element-test ()
  42. "Testing template containing a `p' (prompt) element."
  43. (with-temp-buffer
  44. (tempo-define-template "test" '("hello " (p ">")))
  45. (let ((tempo-interactive t))
  46. (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world")))
  47. (tempo-insert-template 'tempo-template-test nil))
  48. (should (equal (buffer-string) "hello world")))))
  49. (ert-deftest tempo-P-element-test ()
  50. "Testing template containing a `P' (prompt) element."
  51. (with-temp-buffer
  52. (tempo-define-template "test" '("hello " (P ">")))
  53. ;; By default, `tempo-interactive' is nil, `P' should ignore this.
  54. (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world")))
  55. (tempo-insert-template 'tempo-template-test nil))
  56. (should (equal (buffer-string) "hello world"))))
  57. (ert-deftest tempo-r-element-test ()
  58. "Testing template containing an `r' (with prompt) element."
  59. (with-temp-buffer
  60. (tempo-define-template "test" '("abcde" (r ">") "ghijk"))
  61. (let ((tempo-interactive t))
  62. (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "F")))
  63. (tempo-insert-template 'tempo-template-test nil))
  64. (should (equal (buffer-string) "abcdeFghijk")))))
  65. (ert-deftest tempo-s-element-test ()
  66. "Testing template containing an `s' element."
  67. (with-temp-buffer
  68. (tempo-define-template "test" '("hello " (p ">" P1) " " (s P1)))
  69. (let ((tempo-interactive t))
  70. (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world!")))
  71. (tempo-insert-template 'tempo-template-test nil))
  72. (should (equal (buffer-string) "hello world! world!")))))
  73. (ert-deftest tempo-&-element-test ()
  74. "Testing template containing an `&' element."
  75. (tempo-define-template "test" '(& "test"))
  76. (with-temp-buffer
  77. (insert " ")
  78. (tempo-insert-template 'tempo-template-test nil)
  79. (should (equal (buffer-string) " test")))
  80. (with-temp-buffer
  81. (insert "hello")
  82. (tempo-insert-template 'tempo-template-test nil)
  83. (should (equal (buffer-string) "hello\ntest"))))
  84. (ert-deftest tempo-%-element-test ()
  85. "Testing template containing an `%' element."
  86. (tempo-define-template "test" '("test" %))
  87. (with-temp-buffer
  88. (tempo-insert-template 'tempo-template-test nil)
  89. (should (equal (buffer-string) "test")))
  90. (with-temp-buffer
  91. (insert "hello")
  92. (goto-char (point-min))
  93. (tempo-insert-template 'tempo-template-test nil)
  94. (should (equal (buffer-string) "test\nhello"))))
  95. (ert-deftest tempo-n-element-test ()
  96. "Testing template containing an `n' element."
  97. (tempo-define-template "test" '("test" n "test"))
  98. (with-temp-buffer
  99. (tempo-insert-template 'tempo-template-test nil)
  100. (should (equal (buffer-string) "test\ntest"))))
  101. (ert-deftest tempo-n>-element-test ()
  102. "Testing template containing an `n>' element."
  103. (tempo-define-template "test" '("(progn" n> "(list 1 2 3))"))
  104. (with-temp-buffer
  105. (emacs-lisp-mode)
  106. (tempo-insert-template 'tempo-template-test nil)
  107. ;; Tempo should have inserted two spaces before (list 1 2 3)
  108. (should (equal (buffer-string) "(progn\n (list 1 2 3))"))))
  109. (ert-deftest tempo->-element-test ()
  110. "Testing template containing a `>' element."
  111. (with-temp-buffer
  112. (emacs-lisp-mode)
  113. (insert "(progn\n)")
  114. (backward-char)
  115. (tempo-define-template "test" '("(list 1 2 3)" >))
  116. (tempo-insert-template 'tempo-template-test nil)
  117. ;; Tempo should have inserted two spaces before (list 1 2 3)
  118. (should (equal (buffer-string) "(progn\n (list 1 2 3))"))))
  119. (ert-deftest tempo-r>-bare-element-test ()
  120. "Testing template containing a bare `r>' element."
  121. (with-temp-buffer
  122. (tempo-define-template "test" '("(progn" n r> ")"))
  123. (emacs-lisp-mode)
  124. (insert "(list 1 2 3)")
  125. (set-mark (point))
  126. (goto-char (point-min))
  127. (tempo-insert-template 'tempo-template-test t)
  128. ;; Tempo should have inserted two spaces before (list 1 2 3)
  129. (should (equal (buffer-string) "(progn\n (list 1 2 3))"))))
  130. (ert-deftest tempo-r>-element-test ()
  131. "Testing template containing an `r>' (with prompt) element."
  132. (tempo-define-template "test" '("(progn" n (r> ":") ")"))
  133. (with-temp-buffer
  134. ;; Test on-region use
  135. (emacs-lisp-mode)
  136. (insert "(list 1 2 3)")
  137. (set-mark (point))
  138. (goto-char (point-min))
  139. (tempo-insert-template 'tempo-template-test t)
  140. (should (equal (buffer-string) "(progn\n (list 1 2 3))")))
  141. (with-temp-buffer
  142. ;; Test interactive use
  143. (emacs-lisp-mode)
  144. (let ((tempo-interactive t))
  145. (cl-letf (((symbol-function 'read-string) (lambda (&rest _) " (list 1 2 3)")))
  146. (tempo-insert-template 'tempo-template-test nil))
  147. (should (equal (buffer-string) "(progn\n (list 1 2 3))")))))
  148. (ert-deftest tempo-o-element-test ()
  149. "Testing template containing an `o' element."
  150. (with-temp-buffer
  151. (tempo-define-template "test" '("test" o))
  152. (insert "hello")
  153. (goto-char (point-min))
  154. (tempo-insert-template 'tempo-template-test nil)
  155. (should (equal (buffer-string) "test\nhello"))
  156. (should (equal (point) 5))))
  157. (ert-deftest tempo-nil-element-test ()
  158. "Testing template with nil elements."
  159. (with-temp-buffer
  160. (tempo-define-template "test" '("Hello," nil " World!"))
  161. (tempo-insert-template 'tempo-template-test nil)
  162. (should (equal (buffer-string) "Hello, World!"))))
  163. (ert-deftest tempo-eval-element-test ()
  164. "Testing template with Emacs Lisp expressions."
  165. (with-temp-buffer
  166. (tempo-define-template "test" '((int-to-string (+ 1 1)) "=" (concat "1" "+1")))
  167. (tempo-insert-template 'tempo-template-test nil)
  168. (should (equal (buffer-string) "2=1+1"))))
  169. (ert-deftest tempo-l-element-test ()
  170. "Testing template containing an `l' element."
  171. (with-temp-buffer
  172. (tempo-define-template "test" '("list: " (l "1, " "2, " (int-to-string (+ 1 2)))))
  173. (tempo-insert-template 'tempo-template-test nil)
  174. (should (equal (buffer-string) "list: 1, 2, 3"))))
  175. (ert-deftest tempo-tempo-user-elements-test ()
  176. "Testing a template with elements for `tempo-user-elements'."
  177. (with-temp-buffer
  178. (make-local-variable 'tempo-user-elements)
  179. (add-to-list 'tempo-user-elements (lambda (x) (int-to-string (* x x))))
  180. (tempo-define-template "test" '(1 " " 2 " " 3 " " 4))
  181. (tempo-insert-template 'tempo-template-test nil)
  182. (should (equal (buffer-string) "1 4 9 16"))))
  183. (ert-deftest tempo-expand-tag-test ()
  184. "Testing expansion of a template with a tag."
  185. (with-temp-buffer
  186. (tempo-define-template "test" '("Hello, World!") "hello")
  187. (insert "hello")
  188. (tempo-complete-tag)
  189. (should (equal (buffer-string) "Hello, World!"))))
  190. (ert-deftest tempo-expand-partial-tag-test ()
  191. "Testing expansion of a template with a tag, with a partial match."
  192. (with-temp-buffer
  193. (tempo-define-template "test" '("Hello, World!") "hello")
  194. (insert "hel")
  195. (tempo-complete-tag)
  196. (should (equal (buffer-string) "Hello, World!"))))
  197. (provide 'tempo-tests)
  198. ;;; tempo-tests.el ends here