diff --git a/yasnippet-tests.el b/yasnippet-tests.el new file mode 100755 index 0000000..f1219fa --- /dev/null +++ b/yasnippet-tests.el @@ -0,0 +1,188 @@ +;;; yasnippet-tests.el --- some yasnippet tests + +;; Copyright (C) 2012 João Távora + +;; Author: João Távora +;; Keywords: emulations, convenience + +;; 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 . + +;;; Commentary: + +;; Attempt to test basic snippet mechanics and the loading system + +;;; Code: + +(require 'yasnippet) +(require 'ert) +(require 'ert-x) + + +;;; Snippet mechanics + +(ert-deftest field-navigation () + (with-temp-buffer + (yas/minor-mode 1) + (yas/expand-snippet "${1:brother} from another ${2:mother}") + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from another mother")) + + (should (looking-at "brother")) + (ert-simulate-command '(yas/next-field-or-maybe-expand)) + (should (looking-at "mother")) + (ert-simulate-command '(yas/prev-field)) + (should (looking-at "brother")))) + +(ert-deftest simple-mirror () + (with-temp-buffer + (yas/minor-mode 1) + (yas/expand-snippet "${1:brother} from another $1") + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from another brother")) + (ert-simulate-command `(yas/mock-insert "bla")) + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "bla from another bla")))) + +(ert-deftest mirror-with-transformation () + (with-temp-buffer + (yas/minor-mode 1) + (yas/expand-snippet "${1:brother} from another ${1:$(upcase yas/text)}") + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from another BROTHER")) + (ert-simulate-command `(yas/mock-insert "bla")) + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "bla from another BLA")))) + +(ert-deftest nested-placeholders-kill-superfield () + (with-temp-buffer + (yas/minor-mode 1) + (yas/expand-snippet "brother from ${2:another ${3:mother}}!") + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from another mother!")) + (ert-simulate-command `(yas/mock-insert "bla")) + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from bla!")))) + +(ert-deftest nested-placeholders-use-subfield () + (with-temp-buffer + (yas/minor-mode 1) + (yas/expand-snippet "brother from ${2:another ${3:mother}}!") + (ert-simulate-command '(yas/next-field-or-maybe-expand)) + (ert-simulate-command `(yas/mock-insert "bla")) + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from another bla!")))) + +;; (ert-deftest in-snippet-undo () +;; (with-temp-buffer +;; (yas/minor-mode 1) +;; (yas/expand-snippet "brother from ${2:another ${3:mother}}!") +;; (ert-simulate-command '(yas/next-field-or-maybe-expand)) +;; (ert-simulate-command `(yas/mock-insert "bla")) +;; (ert-simulate-command '(undo)) +;; (should (string= (buffer-substring-no-properties (point-min) (point-max)) +;; "brother from another mother!")))) + + +;;; Misc tests +;;; + +(ert-deftest protection-overlay-no-cheating () + "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline! + +TODO: correct this bug!" + :expected-result :failed + (with-temp-buffer + (yas/minor-mode 1) + (yas/expand-snippet "${2:brother} from another ${1:mother}") + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from another mother") ;; no newline should be here! + ))) + +;;; Loading +;;; +(ert-deftest basic-loading () + "Test basic loading and expansion of snippets" + (yas/saving-variables + (with-snippet-dirs + '((".emacs.d/snippets" + ("c-mode" + (".yas-parents" . "cc-mode") + ("printf" . "printf($1);")) + ("emacs-lisp-mode" ("ert-deftest" . "(ert-deftest ${1:name} () $0)")) + ("lisp-interaction-mode" (".yas-parents" . "emacs-lisp-mode"))) + ("library/snippets" + ("c-mode" (".yas-parents" . "c++-mode")) + ("cc-mode" ("def" . "# define")) + ("emacs-lisp-mode" ("dolist" . "(dolist)")) + ("lisp-interaction-mode" ("sc" . "brother from another mother")))) + (yas/reload-all) + (with-temp-buffer + (lisp-interaction-mode) + (yas/minor-mode 1) + (insert "sc") + (ert-simulate-command '(yas/expand)) + (should (string= (buffer-substring-no-properties (point-min) (point-max)) + "brother from another mother")))))) + + + +;;; Helpers +;;; + +(defun yas/mock-insert (string) + (interactive) + (do ((i 0 (1+ i))) + ((= i (length string))) + (insert (aref string i)))) + +(defun yas/make-file-or-dirs (ass) + (let ((file-or-dir-name (car ass)) + (content (cdr ass))) + (cond ((listp content) + (make-directory file-or-dir-name 'parents) + (let ((default-directory (concat default-directory "/" file-or-dir-name))) + (mapc #'yas/make-file-or-dirs content))) + ((stringp content) + (with-current-buffer (find-file file-or-dir-name) + (insert content) + (save-buffer) + (kill-buffer))) + (t + (message "[yas] oops don't know this content"))))) + + +(defun yas/variables () + (let ((syms)) + (mapatoms #'(lambda (sym) + (if (and (string-match "^yas/[^/]" (symbol-name sym)) + (boundp sym)) + (push sym syms)))) + syms)) + + +(defmacro yas/saving-variables (&rest body) + `(let ,(mapcar #'(lambda (sym) + `(,sym ,sym)) + (yas/variables)) + ,@body)) + +(defmacro with-snippet-dirs (dirs &rest body) + `(let ((default-directory (make-temp-file "yasnippet-fixture" t))) + (setq yas/snippet-dirs ',(mapcar #'car (cadr dirs))) + (mapc #'yas/make-file-or-dirs ,dirs) + ,@body)) + + +(provide 'yasnippet-tests) +;;; yasnippet-tests.el ends here diff --git a/yasnippet.el b/yasnippet.el index b01f75b..6966ea1 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -917,7 +917,6 @@ Do this unless `yas/dont-activate' is truish " (defstruct (yas/template (:constructor yas/make-blank-template)) "A template for a snippet." - table key content name @@ -929,6 +928,7 @@ Do this unless `yas/dont-activate' is truish " 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) @@ -1398,6 +1398,8 @@ Here's a list of currently recognized directives: (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 @@ -1660,10 +1662,17 @@ Below TOP-LEVEL-DIR., each directory is a mode name." (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. " @@ -1688,10 +1697,7 @@ Below TOP-LEVEL-DIR., each directory is a mode name." ;; 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)