diff options
| author | Federico Tedin | 2019-05-14 09:16:00 -0300 |
|---|---|---|
| committer | Basil L. Contovounesios | 2019-05-21 15:23:23 +0100 |
| commit | eb2e9a2ca29f7d5e3b97709e9eca14fa5556ac63 (patch) | |
| tree | 1cf8ff22d7dae507eb4ea9bb8fcb2165e2375791 /test/lisp | |
| parent | 5856512911488e40609be5685e64f9fb8cd395ad (diff) | |
| download | emacs-eb2e9a2ca29f7d5e3b97709e9eca14fa5556ac63.tar.gz emacs-eb2e9a2ca29f7d5e3b97709e9eca14fa5556ac63.zip | |
Use lexical-binding in tempo.el and add tests
For discussion, see the following thread:
https://lists.gnu.org/archive/html/emacs-devel/2019-05/msg00395.html
* lisp/tempo.el: Use lexical-binding.
(tempo-define-template): Expand documentation to mention
`tempo-user-elements'.
(tempo-local-tags, tempo-collection, tempo-dirty-collection)
(tempo-marks, tempo-match-finder): Define with defvar-local.
(tempo-named-insertions, tempo-region-start, tempo-region-stop): Make
them automatically buffer-local.
* test/lisp/tempo-tests.el: Add tests for tempo.el.
Diffstat (limited to 'test/lisp')
| -rw-r--r-- | test/lisp/tempo-tests.el | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/test/lisp/tempo-tests.el b/test/lisp/tempo-tests.el new file mode 100644 index 00000000000..6e610ffa6ea --- /dev/null +++ b/test/lisp/tempo-tests.el | |||
| @@ -0,0 +1,228 @@ | |||
| 1 | ;;; tempo-tests.el --- Test suite for tempo.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2019 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Federico Tedin <federicotedin@gmail.com> | ||
| 6 | ;; Keywords: abbrev | ||
| 7 | |||
| 8 | ;; This file is part of GNU Emacs. | ||
| 9 | |||
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 11 | ;; it under the terms of the GNU General Public License as published by | ||
| 12 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 13 | ;; (at your option) any later version. | ||
| 14 | |||
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | ;; GNU General Public License for more details. | ||
| 19 | |||
| 20 | ;; You should have received a copy of the GNU General Public License | ||
| 21 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 22 | |||
| 23 | ;;; Code: | ||
| 24 | |||
| 25 | (require 'tempo) | ||
| 26 | (eval-when-compile (require 'cl-lib)) | ||
| 27 | |||
| 28 | (ert-deftest tempo-string-element-test () | ||
| 29 | "Test a template containing a string element." | ||
| 30 | (with-temp-buffer | ||
| 31 | (tempo-define-template "test" '("GNU Emacs Tempo test")) | ||
| 32 | (tempo-insert-template 'tempo-template-test nil) | ||
| 33 | (should (equal (buffer-string) "GNU Emacs Tempo test")))) | ||
| 34 | |||
| 35 | (ert-deftest tempo-p-bare-element-test () | ||
| 36 | "Test a template containing a bare `p' element." | ||
| 37 | (with-temp-buffer | ||
| 38 | (tempo-define-template "test" '("abcde" p)) | ||
| 39 | (tempo-insert-template 'tempo-template-test nil) | ||
| 40 | (tempo-forward-mark) | ||
| 41 | (should (equal (point) 6)))) | ||
| 42 | |||
| 43 | (ert-deftest tempo-r-bare-element-test () | ||
| 44 | "Test a template containing a bare `r' element." | ||
| 45 | (with-temp-buffer | ||
| 46 | (tempo-define-template "test" '("abcde" r "ghijk")) | ||
| 47 | (insert "F") | ||
| 48 | (set-mark (point)) | ||
| 49 | (goto-char (point-min)) | ||
| 50 | (tempo-insert-template 'tempo-template-test t) | ||
| 51 | (should (equal (buffer-string) "abcdeFghijk")))) | ||
| 52 | |||
| 53 | (ert-deftest tempo-p-element-test () | ||
| 54 | "Testing template containing a `p' (prompt) element." | ||
| 55 | (with-temp-buffer | ||
| 56 | (tempo-define-template "test" '("hello " (p ">"))) | ||
| 57 | (let ((tempo-interactive t)) | ||
| 58 | (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world"))) | ||
| 59 | (tempo-insert-template 'tempo-template-test nil)) | ||
| 60 | (should (equal (buffer-string) "hello world"))))) | ||
| 61 | |||
| 62 | (ert-deftest tempo-P-element-test () | ||
| 63 | "Testing template containing a `P' (prompt) element." | ||
| 64 | (with-temp-buffer | ||
| 65 | (tempo-define-template "test" '("hello " (P ">"))) | ||
| 66 | ;; By default, `tempo-interactive' is nil, `P' should ignore this. | ||
| 67 | (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world"))) | ||
| 68 | (tempo-insert-template 'tempo-template-test nil)) | ||
| 69 | (should (equal (buffer-string) "hello world")))) | ||
| 70 | |||
| 71 | (ert-deftest tempo-r-element-test () | ||
| 72 | "Testing template containing an `r' (with prompt) element." | ||
| 73 | (with-temp-buffer | ||
| 74 | (tempo-define-template "test" '("abcde" (r ">") "ghijk")) | ||
| 75 | (let ((tempo-interactive t)) | ||
| 76 | (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "F"))) | ||
| 77 | (tempo-insert-template 'tempo-template-test nil)) | ||
| 78 | (should (equal (buffer-string) "abcdeFghijk"))))) | ||
| 79 | |||
| 80 | (ert-deftest tempo-s-element-test () | ||
| 81 | "Testing template containing an `s' element." | ||
| 82 | (with-temp-buffer | ||
| 83 | (tempo-define-template "test" '("hello " (p ">" P1) " " (s P1))) | ||
| 84 | (let ((tempo-interactive t)) | ||
| 85 | (cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world!"))) | ||
| 86 | (tempo-insert-template 'tempo-template-test nil)) | ||
| 87 | (should (equal (buffer-string) "hello world! world!"))))) | ||
| 88 | |||
| 89 | (ert-deftest tempo-&-element-test () | ||
| 90 | "Testing template containing an `&' element." | ||
| 91 | (tempo-define-template "test" '(& "test")) | ||
| 92 | (with-temp-buffer | ||
| 93 | (insert " ") | ||
| 94 | (tempo-insert-template 'tempo-template-test nil) | ||
| 95 | (should (equal (buffer-string) " test"))) | ||
| 96 | (with-temp-buffer | ||
| 97 | (insert "hello") | ||
| 98 | (tempo-insert-template 'tempo-template-test nil) | ||
| 99 | (should (equal (buffer-string) "hello\ntest")))) | ||
| 100 | |||
| 101 | (ert-deftest tempo-%-element-test () | ||
| 102 | "Testing template containing an `%' element." | ||
| 103 | (tempo-define-template "test" '("test" %)) | ||
| 104 | (with-temp-buffer | ||
| 105 | (tempo-insert-template 'tempo-template-test nil) | ||
| 106 | (should (equal (buffer-string) "test"))) | ||
| 107 | (with-temp-buffer | ||
| 108 | (insert "hello") | ||
| 109 | (goto-char (point-min)) | ||
| 110 | (tempo-insert-template 'tempo-template-test nil) | ||
| 111 | (should (equal (buffer-string) "test\nhello")))) | ||
| 112 | |||
| 113 | (ert-deftest tempo-n-element-test () | ||
| 114 | "Testing template containing an `n' element." | ||
| 115 | (tempo-define-template "test" '("test" n "test")) | ||
| 116 | (with-temp-buffer | ||
| 117 | (tempo-insert-template 'tempo-template-test nil) | ||
| 118 | (should (equal (buffer-string) "test\ntest")))) | ||
| 119 | |||
| 120 | (ert-deftest tempo-n>-element-test () | ||
| 121 | "Testing template containing an `n>' element." | ||
| 122 | (tempo-define-template "test" '("(progn" n> "(list 1 2 3))")) | ||
| 123 | (with-temp-buffer | ||
| 124 | (emacs-lisp-mode) | ||
| 125 | (tempo-insert-template 'tempo-template-test nil) | ||
| 126 | ;; Tempo should have inserted two spaces before (list 1 2 3) | ||
| 127 | (should (equal (buffer-string) "(progn\n (list 1 2 3))")))) | ||
| 128 | |||
| 129 | (ert-deftest tempo->-element-test () | ||
| 130 | "Testing template containing a `>' element." | ||
| 131 | (with-temp-buffer | ||
| 132 | (emacs-lisp-mode) | ||
| 133 | (insert "(progn\n)") | ||
| 134 | (backward-char) | ||
| 135 | (tempo-define-template "test" '("(list 1 2 3)" >)) | ||
| 136 | (tempo-insert-template 'tempo-template-test nil) | ||
| 137 | ;; Tempo should have inserted two spaces before (list 1 2 3) | ||
| 138 | (should (equal (buffer-string) "(progn\n (list 1 2 3))")))) | ||
| 139 | |||
| 140 | (ert-deftest tempo-r>-bare-element-test () | ||
| 141 | "Testing template containing a bare `r>' element." | ||
| 142 | (with-temp-buffer | ||
| 143 | (tempo-define-template "test" '("(progn" n r> ")")) | ||
| 144 | (emacs-lisp-mode) | ||
| 145 | (insert "(list 1 2 3)") | ||
| 146 | (set-mark (point)) | ||
| 147 | (goto-char (point-min)) | ||
| 148 | (tempo-insert-template 'tempo-template-test t) | ||
| 149 | ;; Tempo should have inserted two spaces before (list 1 2 3) | ||
| 150 | (should (equal (buffer-string) "(progn\n (list 1 2 3))")))) | ||
| 151 | |||
| 152 | (ert-deftest tempo-r>-element-test () | ||
| 153 | "Testing template containing an `r>' (with prompt) element." | ||
| 154 | (tempo-define-template "test" '("(progn" n (r> ":") ")")) | ||
| 155 | (with-temp-buffer | ||
| 156 | ;; Test on-region use | ||
| 157 | (emacs-lisp-mode) | ||
| 158 | (insert "(list 1 2 3)") | ||
| 159 | (set-mark (point)) | ||
| 160 | (goto-char (point-min)) | ||
| 161 | (tempo-insert-template 'tempo-template-test t) | ||
| 162 | (should (equal (buffer-string) "(progn\n (list 1 2 3))"))) | ||
| 163 | (with-temp-buffer | ||
| 164 | ;; Test interactive use | ||
| 165 | (emacs-lisp-mode) | ||
| 166 | (let ((tempo-interactive t)) | ||
| 167 | (cl-letf (((symbol-function 'read-string) (lambda (&rest _) " (list 1 2 3)"))) | ||
| 168 | (tempo-insert-template 'tempo-template-test nil)) | ||
| 169 | (should (equal (buffer-string) "(progn\n (list 1 2 3))"))))) | ||
| 170 | |||
| 171 | (ert-deftest tempo-o-element-test () | ||
| 172 | "Testing template containing an `o' element." | ||
| 173 | (with-temp-buffer | ||
| 174 | (tempo-define-template "test" '("test" o)) | ||
| 175 | (insert "hello") | ||
| 176 | (goto-char (point-min)) | ||
| 177 | (tempo-insert-template 'tempo-template-test nil) | ||
| 178 | (should (equal (buffer-string) "test\nhello")) | ||
| 179 | (should (equal (point) 5)))) | ||
| 180 | |||
| 181 | (ert-deftest tempo-nil-element-test () | ||
| 182 | "Testing template with nil elements." | ||
| 183 | (with-temp-buffer | ||
| 184 | (tempo-define-template "test" '("Hello," nil " World!")) | ||
| 185 | (tempo-insert-template 'tempo-template-test nil) | ||
| 186 | (should (equal (buffer-string) "Hello, World!")))) | ||
| 187 | |||
| 188 | (ert-deftest tempo-eval-element-test () | ||
| 189 | "Testing template with Emacs Lisp expressions." | ||
| 190 | (with-temp-buffer | ||
| 191 | (tempo-define-template "test" '((int-to-string (+ 1 1)) "=" (concat "1" "+1"))) | ||
| 192 | (tempo-insert-template 'tempo-template-test nil) | ||
| 193 | (should (equal (buffer-string) "2=1+1")))) | ||
| 194 | |||
| 195 | (ert-deftest tempo-l-element-test () | ||
| 196 | "Testing template containing an `l' element." | ||
| 197 | (with-temp-buffer | ||
| 198 | (tempo-define-template "test" '("list: " (l "1, " "2, " (int-to-string (+ 1 2))))) | ||
| 199 | (tempo-insert-template 'tempo-template-test nil) | ||
| 200 | (should (equal (buffer-string) "list: 1, 2, 3")))) | ||
| 201 | |||
| 202 | (ert-deftest tempo-tempo-user-elements-test () | ||
| 203 | "Testing a template with elements for `tempo-user-elements'." | ||
| 204 | (with-temp-buffer | ||
| 205 | (make-local-variable 'tempo-user-elements) | ||
| 206 | (add-to-list 'tempo-user-elements (lambda (x) (int-to-string (* x x)))) | ||
| 207 | (tempo-define-template "test" '(1 " " 2 " " 3 " " 4)) | ||
| 208 | (tempo-insert-template 'tempo-template-test nil) | ||
| 209 | (should (equal (buffer-string) "1 4 9 16")))) | ||
| 210 | |||
| 211 | (ert-deftest tempo-expand-tag-test () | ||
| 212 | "Testing expansion of a template with a tag." | ||
| 213 | (with-temp-buffer | ||
| 214 | (tempo-define-template "test" '("Hello, World!") "hello") | ||
| 215 | (insert "hello") | ||
| 216 | (tempo-complete-tag) | ||
| 217 | (should (equal (buffer-string) "Hello, World!")))) | ||
| 218 | |||
| 219 | (ert-deftest tempo-expand-partial-tag-test () | ||
| 220 | "Testing expansion of a template with a tag, with a partial match." | ||
| 221 | (with-temp-buffer | ||
| 222 | (tempo-define-template "test" '("Hello, World!") "hello") | ||
| 223 | (insert "hel") | ||
| 224 | (tempo-complete-tag) | ||
| 225 | (should (equal (buffer-string) "Hello, World!")))) | ||
| 226 | |||
| 227 | (provide 'tempo-tests) | ||
| 228 | ;;; tempo-tests.el ends here | ||