--- /dev/null
+;;; yasnippet-tests.el --- some yasnippet tests\r
+\r
+;; Copyright (C) 2012 João Távora\r
+\r
+;; Author: João Távora <joaot@siscog.pt>\r
+;; Keywords: emulations, convenience\r
+\r
+;; This program is free software; you can redistribute it and/or modify\r
+;; it under the terms of the GNU General Public License as published by\r
+;; the Free Software Foundation, either version 3 of the License, or\r
+;; (at your option) any later version.\r
+\r
+;; This program is distributed in the hope that it will be useful,\r
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+;; GNU General Public License for more details.\r
+\r
+;; You should have received a copy of the GNU General Public License\r
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.\r
+\r
+;;; Commentary:\r
+\r
+;; Attempt to test basic snippet mechanics and the loading system \r
+\r
+;;; Code:\r
+\r
+(require 'yasnippet)\r
+(require 'ert)\r
+(require 'ert-x)\r
+\r
+\r
+;;; Snippet mechanics\r
+\r
+(ert-deftest field-navigation ()\r
+ (with-temp-buffer\r
+ (yas/minor-mode 1)\r
+ (yas/expand-snippet "${1:brother} from another ${2:mother}")\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from another mother"))\r
+ \r
+ (should (looking-at "brother"))\r
+ (ert-simulate-command '(yas/next-field-or-maybe-expand))\r
+ (should (looking-at "mother"))\r
+ (ert-simulate-command '(yas/prev-field))\r
+ (should (looking-at "brother"))))\r
+\r
+(ert-deftest simple-mirror ()\r
+ (with-temp-buffer\r
+ (yas/minor-mode 1)\r
+ (yas/expand-snippet "${1:brother} from another $1")\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from another brother"))\r
+ (ert-simulate-command `(yas/mock-insert "bla"))\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "bla from another bla"))))\r
+\r
+(ert-deftest mirror-with-transformation ()\r
+ (with-temp-buffer\r
+ (yas/minor-mode 1)\r
+ (yas/expand-snippet "${1:brother} from another ${1:$(upcase yas/text)}")\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from another BROTHER"))\r
+ (ert-simulate-command `(yas/mock-insert "bla"))\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "bla from another BLA"))))\r
+\r
+(ert-deftest nested-placeholders-kill-superfield ()\r
+ (with-temp-buffer\r
+ (yas/minor-mode 1)\r
+ (yas/expand-snippet "brother from ${2:another ${3:mother}}!")\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from another mother!"))\r
+ (ert-simulate-command `(yas/mock-insert "bla"))\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from bla!"))))\r
+\r
+(ert-deftest nested-placeholders-use-subfield ()\r
+ (with-temp-buffer\r
+ (yas/minor-mode 1)\r
+ (yas/expand-snippet "brother from ${2:another ${3:mother}}!")\r
+ (ert-simulate-command '(yas/next-field-or-maybe-expand))\r
+ (ert-simulate-command `(yas/mock-insert "bla"))\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from another bla!"))))\r
+\r
+;; (ert-deftest in-snippet-undo ()\r
+;; (with-temp-buffer\r
+;; (yas/minor-mode 1)\r
+;; (yas/expand-snippet "brother from ${2:another ${3:mother}}!")\r
+;; (ert-simulate-command '(yas/next-field-or-maybe-expand))\r
+;; (ert-simulate-command `(yas/mock-insert "bla"))\r
+;; (ert-simulate-command '(undo))\r
+;; (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+;; "brother from another mother!"))))\r
+\r
+\r
+;;; Misc tests\r
+;;; \r
+\r
+(ert-deftest protection-overlay-no-cheating ()\r
+ "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!\r
+\r
+TODO: correct this bug!"\r
+ :expected-result :failed\r
+ (with-temp-buffer\r
+ (yas/minor-mode 1)\r
+ (yas/expand-snippet "${2:brother} from another ${1:mother}")\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from another mother") ;; no newline should be here!\r
+ )))\r
+\r
+;;; Loading\r
+;;;\r
+(ert-deftest basic-loading ()\r
+ "Test basic loading and expansion of snippets"\r
+ (yas/saving-variables\r
+ (with-snippet-dirs\r
+ '((".emacs.d/snippets"\r
+ ("c-mode"\r
+ (".yas-parents" . "cc-mode")\r
+ ("printf" . "printf($1);"))\r
+ ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)"))\r
+ ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode")))\r
+ ("library/snippets"\r
+ ("c-mode" (".yas-parents" . "c++-mode"))\r
+ ("cc-mode" ("def" . "# define"))\r
+ ("emacs-lisp-mode" ("dolist" . "(dolist)"))\r
+ ("lisp-interaction-mode" ("sc" . "brother from another mother"))))\r
+ (yas/reload-all)\r
+ (with-temp-buffer\r
+ (lisp-interaction-mode)\r
+ (yas/minor-mode 1)\r
+ (insert "sc")\r
+ (ert-simulate-command '(yas/expand))\r
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))\r
+ "brother from another mother"))))))\r
+\r
+\r
+\r
+;;; Helpers\r
+;;; \r
+\r
+(defun yas/mock-insert (string)\r
+ (interactive)\r
+ (do ((i 0 (1+ i)))\r
+ ((= i (length string)))\r
+ (insert (aref string i))))\r
+\r
+(defun yas/make-file-or-dirs (ass)\r
+ (let ((file-or-dir-name (car ass))\r
+ (content (cdr ass)))\r
+ (cond ((listp content)\r
+ (make-directory file-or-dir-name 'parents)\r
+ (let ((default-directory (concat default-directory "/" file-or-dir-name)))\r
+ (mapc #'yas/make-file-or-dirs content)))\r
+ ((stringp content)\r
+ (with-current-buffer (find-file file-or-dir-name)\r
+ (insert content)\r
+ (save-buffer)\r
+ (kill-buffer)))\r
+ (t\r
+ (message "[yas] oops don't know this content")))))\r
+\r
+\r
+(defun yas/variables ()\r
+ (let ((syms))\r
+ (mapatoms #'(lambda (sym)\r
+ (if (and (string-match "^yas/[^/]" (symbol-name sym))\r
+ (boundp sym))\r
+ (push sym syms))))\r
+ syms))\r
+\r
+\r
+(defmacro yas/saving-variables (&rest body)\r
+ `(let ,(mapcar #'(lambda (sym)\r
+ `(,sym ,sym))\r
+ (yas/variables))\r
+ ,@body))\r
+\r
+(defmacro with-snippet-dirs (dirs &rest body)\r
+ `(let ((default-directory (make-temp-file "yasnippet-fixture" t)))\r
+ (setq yas/snippet-dirs ',(mapcar #'car (cadr dirs)))\r
+ (mapc #'yas/make-file-or-dirs ,dirs)\r
+ ,@body))\r
+\r
+\r
+(provide 'yasnippet-tests)\r
+;;; yasnippet-tests.el ends here\r
(defstruct (yas/template (:constructor yas/make-blank-template))
"A template for a snippet."
- table
key
content
name
menu-binding-pair
group ;; as dictated by the #group: directive or .yas-make-groups
perm-group ;; as dictated by `yas/define-menu'
+ table
)
(defun yas/populate-template (template &rest args)
(setq binding (match-string-no-properties 2)))))
(setq template
(buffer-substring-no-properties (point-min) (point-max))))
+ (unless (or key binding)
+ (setq key (and file (file-name-nondirectory file))))
(when (eq type 'command)
(setq template (yas/read-lisp (concat "(progn" template ")"))))
(when group
(defun yas/load-snippet-dirs ()
"Reload the directories listed in `yas/snippet-dirs' or
prompt the user to select one."
- (if yas/snippet-dirs
- (dolist (directory (reverse (yas/snippet-dirs)))
- (yas/load-directory directory))
- (call-interactively 'yas/load-directory)))
+ (let (errors)
+ (if yas/snippet-dirs
+ (dolist (directory (reverse (yas/snippet-dirs)))
+ (condition-case oops
+ (progn
+ (yas/load-directory directory)
+ (message "[yas] Loaded %s" directory))
+ (error (push oops errors)
+ (message "[yas] Check your `yas/snippet-dirs': %s" (second oops)))))
+ (call-interactively 'yas/load-directory))
+ errors))
(defun yas/reload-all (&optional with-jit)
"Reload all snippets and rebuild the YASnippet menu. "
;; Reload the directories listed in `yas/snippet-dirs' or prompt
;; the user to select one.
;;
- (condition-case oops
- (yas/load-snippet-dirs)
- (error (push oops errors)
- (message "[yas] Check your `yas/snippet-dirs': %s" (second oops))))
+ (setq errors (yas/load-snippet-dirs))
;; Reload the direct keybindings
;;
(yas/direct-keymaps-reload)