Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 262 additions & 0 deletions tempel-tempo-tests.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
;;; tempel-tempo-tests.el --- Test suite for tempo.el compatibility -*- lexical-binding: t; -*-

;; Copyright (C) 2026 Free Software Foundation, Inc.

;; Author: Elias Gabriel Pérez <eg642616@gmail.com>
;; Keywords: abbrev, languages, tools, text

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.

;;; Code:

(require 'tempel-tempo)
(eval-when-compile (require 'cl-lib))

(ert-deftest tempel-tempo-string-element-test ()
"Test a template containing a string element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("GNU Emacs Tempo test"))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "GNU Emacs Tempo test"))))

(ert-deftest tempel-tempo-p-bare-element-test ()
"Test a template containing a bare `p' element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("abcde" p))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(tempel-next)
(should (equal (point) 6))))

(ert-deftest tempel-tempo-r-bare-element-test ()
"Test a template containing a bare `r' element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("abcde" r "ghijk"))
(insert "F")
(set-mark (point))
(goto-char (point-min))
(tempel-tempo-insert-template 'tempel-tempo-template-test t)
(should (equal (buffer-string) "abcdeFghijk"))))

(ert-deftest tempel-tempo-p-element-test ()
"Testing template containing a `p' (prompt) element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("hello " (p ">")))
(let ((tempel-tempo-interactive t))
(cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world")))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil))
(should (equal (buffer-string) "hello world")))))

(ert-deftest tempel-tempo-P-element-test ()
"Testing template containing a `P' (prompt) element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("hello " (P ">")))
;; By default, `tempo-interactive' is nil, `P' should ignore this.
(cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world")))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil))
(should (equal (buffer-string) "hello world"))))

(ert-deftest tempel-tempo-r-element-test ()
"Testing template containing an `r' (with prompt) element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("abcde" (r ">") "ghijk"))
(let ((tempel-tempo-interactive t))
(cl-letf (((symbol-function 'read-string) (lambda (&rest _) "F")))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil))
(should (equal (buffer-string) "abcdeFghijk")))))

(ert-deftest tempel-tempo-s-element-test ()
"Testing template containing an `s' element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("hello " (p ">" P1) " " (s P1)))
(let ((tempel-tempo-interactive t))
(cl-letf (((symbol-function 'read-string) (lambda (&rest _) "world!")))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil))
(should (equal (buffer-string) "hello world! world!")))))

(ert-deftest tempel-tempo-&-element-test ()
"Testing template containing an `&' element."
(tempel-tempo-define-template "test" '(& "test"))
(with-temp-buffer
(insert " ")
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) " test")))
(with-temp-buffer
(insert "hello")
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "hello\ntest"))))

(ert-deftest tempel-tempo-%-element-test ()
"Testing template containing an `%' element."
(tempel-tempo-define-template "test" '("test" %))
(with-temp-buffer
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "test")))
(with-temp-buffer
(insert "hello")
(goto-char (point-min))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "test\nhello"))))

(ert-deftest tempel-tempo-n-element-test ()
"Testing template containing an `n' element."
(tempel-tempo-define-template "test" '("test" n "test"))
(with-temp-buffer
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "test\ntest"))))

(ert-deftest tempel-tempo-n>-element-test ()
"Testing template containing an `n>' element."
(tempel-tempo-define-template "test" '("(progn" n> "(list 1 2 3))"))
(with-temp-buffer
(emacs-lisp-mode)
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
;; Tempo should have inserted two spaces before (list 1 2 3)
(should (equal (buffer-string) "(progn\n (list 1 2 3))"))))

(ert-deftest tempel-tempo->-element-test ()
"Testing template containing a `>' element."
(with-temp-buffer
(emacs-lisp-mode)
(insert "(progn\n)")
(backward-char)
(tempel-tempo-define-template "test" '("(list 1 2 3)" >))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
;; Tempo should have inserted two spaces before (list 1 2 3)
(should (equal (buffer-string) "(progn\n (list 1 2 3))"))))

(ert-deftest tempel-tempo-r>-bare-element-test ()
"Testing template containing a bare `r>' element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("(progn" n r> ")"))
(emacs-lisp-mode)
(insert "(list 1 2 3)")
(set-mark (point))
(goto-char (point-min))
(tempel-tempo-insert-template 'tempel-tempo-template-test t)
;; Tempo should have inserted two spaces before (list 1 2 3)
(should (equal (buffer-string) "(progn\n (list 1 2 3))"))))

(ert-deftest tempel-tempo-r>-element-test ()
"Testing template containing an `r>' (with prompt) element."
(tempel-tempo-define-template "test" '("(progn" n (r> ":") ")"))
(with-temp-buffer
;; Test on-region use
(emacs-lisp-mode)
(insert "(list 1 2 3)")
(set-mark (point))
(goto-char (point-min))
(tempel-tempo-insert-template 'tempel-tempo-template-test t)
(should (equal (buffer-string) "(progn\n (list 1 2 3))")))
(with-temp-buffer
;; Test interactive use
(emacs-lisp-mode)
(let ((tempel-tempo-interactive t))
(cl-letf (((symbol-function 'read-string) (lambda (&rest _) " (list 1 2 3)")))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil))
(should (equal (buffer-string) "(progn\n (list 1 2 3))")))))

(ert-deftest tempel-tempo-o-element-test ()
"Testing template containing an `o' element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("test" o))
(insert "hello")
(goto-char (point-min))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "test\nhello"))
(should (equal (point) 5))))

(ert-deftest tempel-tempo-nil-element-test ()
"Testing template with nil elements."
(with-temp-buffer
(tempel-tempo-define-template "test" '("Hello," nil " World!"))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "Hello, World!"))))

(ert-deftest tempel-tempo-eval-element-test ()
"Testing template with Emacs Lisp expressions."
(with-temp-buffer
(tempel-tempo-define-template "test" '((int-to-string (+ 1 1)) "=" (concat "1" "+1")))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "2=1+1"))))

(ert-deftest tempel-tempo-l-element-test ()
"Testing template containing an `l' element."
(with-temp-buffer
(tempel-tempo-define-template "test" '("list: " (l "1, " "2, " (int-to-string (+ 1 2)))))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "list: 1, 2, 3"))))

(ert-deftest tempel-tempo-tempo-user-elements-test ()
"Testing a template with elements for `tempo-user-elements'."
(with-temp-buffer
(add-hook 'tempel-user-elements
(lambda (x) (int-to-string (* x x))) nil :local)
(tempel-tempo-define-template "test" '(1 " " 2 " " 3 " " 4))
(tempel-tempo-insert-template 'tempel-tempo-template-test nil)
(should (equal (buffer-string) "1 4 9 16"))))

(ert-deftest tempel-tempo-expand-tag-test ()
"Testing expansion of a template with a tag."
(with-temp-buffer
(tempel-tempo-define-template "test" '("Hello, World!") "hello")
(insert "hello")
(tempel-expand t)
(should (equal (buffer-string) "Hello, World!"))))

(ert-deftest tempel-tempo-define-tag-globally-test ()
"Testing usage of a template tag defined from another buffer."
(tempel-tempo-define-template "test" '("Hello, World!") "hello")

(with-temp-buffer
;; Use a tag in buffer 1
(insert "hello")
(tempel-expand t)
(should (equal (buffer-string) "Hello, World!"))
(erase-buffer)

;; Define a tag on buffer 2
(with-temp-buffer
(tempel-tempo-define-template "test2" '("Now expanded.") "mytag"))

;; I should be able to use this template back in buffer 1
(insert "mytag")
(tempel-expand t)
(should (equal (buffer-string) "Now expanded."))))

(ert-deftest tempel-tempo-overwrite-tag-test ()
"Testing ability to reassign templates to tags."
(with-temp-buffer
;; Define a tag and use it
(tempel-tempo-define-template "test-tag-1" '("abc") "footag")
(insert "footag")
(tempel-expand t)
(should (equal (buffer-string) "abc"))
(erase-buffer)

;; Define a new template with the same tag
(tempel-tempo-define-template "test-tag-2" '("xyz") "footag")
(insert "footag")
(tempel-expand t)
(should (equal (buffer-string) "xyz"))))

(ert-deftest tempel-tempo-expand-partial-tag-test ()
"Testing expansion of a template with a tag, with a partial match."
(with-temp-buffer
(tempel-tempo-define-template "test" '("Hello, World!") "hello")
(insert "hel")
(tempel-complete t)
(should (equal (buffer-string) "Hello, World!"))))

(provide 'tempel-tempo-tests)
;;; tempel-tempo-tests.el ends here
Loading