X-Git-Url: https://code.delx.au/gnu-emacs-elpa/blobdiff_plain/0c2da6600ad7913bfc0a329a3e13016dcd7360fa..23a624ca1d40fa9cefd7229ac6152b79278a6517:/packages/yasnippet/yasnippet-tests.el diff --git a/packages/yasnippet/yasnippet-tests.el b/packages/yasnippet/yasnippet-tests.el index 9bf592f88..2d7a0bc84 100644 --- a/packages/yasnippet/yasnippet-tests.el +++ b/packages/yasnippet/yasnippet-tests.el @@ -1,6 +1,6 @@ ;;; yasnippet-tests.el --- some yasnippet tests -*- lexical-binding: t -*- -;; Copyright (C) 2012, 2013, 2014 Free Software Foundation, Inc. +;; Copyright (C) 2012, 2013, 2014, 2015 Free Software Foundation, Inc. ;; Author: João Távora ;; Keywords: emulations, convenience @@ -71,7 +71,7 @@ (ert-deftest primary-field-transformation () (with-temp-buffer (yas-minor-mode 1) - (let ((snippet "${1:$$(upcase yas/text)}${1:$(concat \"bar\" yas/text)}")) + (let ((snippet "${1:$$(upcase yas-text)}${1:$(concat \"bar\" yas-text)}")) (yas-expand-snippet snippet) (should (string= (yas--buffer-contents) "bar")) (ert-simulate-command `(yas-mock-insert "foo")) @@ -113,6 +113,17 @@ (ert-simulate-command `(yas-mock-insert "abc")) (should (string= (yas--buffer-contents) "abcabcabcabc")))) +(ert-deftest delete-numberless-inner-snippet-issue-562 () + (with-temp-buffer + (yas-minor-mode 1) + (yas-expand-snippet "${3:${test}bla}$0${2:ble}") + (ert-simulate-command '(yas-next-field-or-maybe-expand)) + (should (looking-at "testblable")) + (ert-simulate-command '(yas-next-field-or-maybe-expand)) + (ert-simulate-command '(yas-skip-and-clear-or-delete-char)) + (should (looking-at "ble")) + (should (null (yas--snippets-at-point))))) + ;; (ert-deftest in-snippet-undo () ;; (with-temp-buffer ;; (yas-minor-mode 1) @@ -173,38 +184,100 @@ (ert-deftest be-careful-when-escaping-in-yas-selected-text () (with-temp-buffer (yas-minor-mode 1) - (let ((yas/selected-text "He\\\\o world!")) - (yas-expand-snippet "Look ma! `(yas/selected-text)`") + (let ((yas-selected-text "He\\\\o world!")) + (yas-expand-snippet "Look ma! `(yas-selected-text)`") (should (string= (yas--buffer-contents) "Look ma! He\\\\o world!"))) (yas-exit-all-snippets) (erase-buffer) - (let ((yas/selected-text "He\"o world!")) - (yas-expand-snippet "Look ma! `(yas/selected-text)`") + (let ((yas-selected-text "He\"o world!")) + (yas-expand-snippet "Look ma! `(yas-selected-text)`") (should (string= (yas--buffer-contents) "Look ma! He\"o world!"))) (yas-exit-all-snippets) (erase-buffer) - (let ((yas/selected-text "He\"\)\\o world!")) - (yas-expand-snippet "Look ma! `(yas/selected-text)`") + (let ((yas-selected-text "He\"\)\\o world!")) + (yas-expand-snippet "Look ma! `(yas-selected-text)`") (should (string= (yas--buffer-contents) "Look ma! He\"\)\\o world!"))) (yas-exit-all-snippets) (erase-buffer))) (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 () (with-temp-buffer - (let ((yas/selected-text "He)}o world!")) - (yas-expand-snippet "Look ma! ${1:`(yas/selected-text)`} OK?") + (yas-minor-mode 1) + (let ((yas-selected-text "He)}o world!")) + (yas-expand-snippet "Look ma! ${1:`(yas-selected-text)`} OK?") (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?"))))) (ert-deftest example-for-issue-271 () (with-temp-buffer (yas-minor-mode 1) (let ((yas-selected-text "aaa") - (snippet "if ${1:condition}\n`yas/selected-text`\nelse\n$3\nend")) + (snippet "if ${1:condition}\n`yas-selected-text`\nelse\n$3\nend")) (yas-expand-snippet snippet) (yas-next-field) (ert-simulate-command `(yas-mock-insert "bbb")) (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend"))))) +(defmacro yas--with-font-locked-temp-buffer (&rest body) + "Like `with-temp-buffer', but ensure `font-lock-mode'." + (declare (indent 0) (debug t)) + (let ((temp-buffer (make-symbol "temp-buffer"))) + ;; NOTE: buffer name must not start with a space, otherwise + ;; `font-lock-mode' doesn't turn on. + `(let ((,temp-buffer (generate-new-buffer "*yas-temp*"))) + (with-current-buffer ,temp-buffer + ;; pretend we're interactive so `font-lock-mode' turns on + (let ((noninteractive nil) + ;; turn on font locking after major mode change + (change-major-mode-after-body-hook #'font-lock-mode)) + (unwind-protect + (progn (require 'font-lock) + ;; turn on font locking before major mode change + (font-lock-mode +1) + ,@body) + (and (buffer-name ,temp-buffer) + (kill-buffer ,temp-buffer)))))))) + +(defmacro yas-saving-variables (&rest body) + `(yas-call-with-saving-variables #'(lambda () ,@body))) + +(defmacro yas-with-snippet-dirs (dirs &rest body) + (declare (indent defun)) + `(yas-call-with-snippet-dirs ,dirs + #'(lambda () + ,@body))) + +(ert-deftest example-for-issue-474 () + (yas--with-font-locked-temp-buffer + (c-mode) + (yas-minor-mode 1) + (insert "#include \n") + (let ((yas-good-grace nil)) (yas-expand-snippet "`\"TODO: \"`")) + (should (string= (yas--buffer-contents) "#include \nTODO: ")))) + +(ert-deftest example-for-issue-404 () + (yas--with-font-locked-temp-buffer + (c++-mode) + (yas-minor-mode 1) + (insert "#include \n") + (let ((yas-good-grace nil)) (yas-expand-snippet "main")) + (should (string= (yas--buffer-contents) "#include \nmain")))) + +(ert-deftest example-for-issue-404-c-mode () + (yas--with-font-locked-temp-buffer + (c-mode) + (yas-minor-mode 1) + (insert "#include \n") + (let ((yas-good-grace nil)) (yas-expand-snippet "main")) + (should (string= (yas--buffer-contents) "#include \nmain")))) + +(ert-deftest middle-of-buffer-snippet-insertion () + (with-temp-buffer + (yas-minor-mode 1) + (insert "beginning") + (save-excursion (insert "end")) + (yas-expand-snippet "-middle-") + (should (string= (yas--buffer-contents) "beginning-middle-end")))) + (ert-deftest another-example-for-issue-271 () ;; expect this to fail in batch mode since `region-active-p' doesn't ;; used by `yas-expand-snippet' doesn't make sense in that context. @@ -214,7 +287,7 @@ :passed) (with-temp-buffer (yas-minor-mode 1) - (let ((snippet "\\${${1:1}:`yas/selected-text`}")) + (let ((snippet "\\${${1:1}:`yas-selected-text`}")) (insert "aaabbbccc") (set-mark 4) (goto-char 7) @@ -237,7 +310,7 @@ (yas-minor-mode 1) ;; the rule here is: To use regexps in embedded `(elisp)` expressions, ;; escape backslashes once, i.e. to use \\( \\) constructs, write \\\\( \\\\). - (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas/text) + (let ((snippet "$1${1:$(if (string-match \"foo\\\\\\\\(ba+r\\\\\\\\)baz\" yas-text) \"ok\" \"fail\")}")) (yas-expand-snippet snippet) @@ -262,6 +335,50 @@ TODO: correct this bug!" (should (string= (yas--buffer-contents) "brother from another mother") ;; no newline should be here! ))) + +(defvar yas--barbaz) +(defvar yas--foobarbaz) + +;; See issue #497. To understand this test, follow the example of the +;; `yas-key-syntaxes' docstring. +;; +(ert-deftest complicated-yas-key-syntaxes () + (with-temp-buffer + (yas-saving-variables + (yas-with-snippet-dirs + '((".emacs.d/snippets" + ("emacs-lisp-mode" + ("foo-barbaz" . "# condition: yas--foobarbaz\n# --\nOKfoo-barbazOK") + ("barbaz" . "# condition: yas--barbaz\n# --\nOKbarbazOK") + ("baz" . "OKbazOK") + ("'quote" . "OKquoteOK")))) + (yas-reload-all) + (emacs-lisp-mode) + (yas-minor-mode-on) + (let ((yas-key-syntaxes '("w" "w_"))) + (let ((yas--barbaz t)) + (yas-should-expand '(("foo-barbaz" . "foo-OKbarbazOK") + ("barbaz" . "OKbarbazOK")))) + (let ((yas--foobarbaz t)) + (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK")))) + (let ((yas-key-syntaxes + (cons #'(lambda (_start-point) + (unless (looking-back "-") + (backward-char) + 'again)) + yas-key-syntaxes)) + (yas--foobarbaz t)) + (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK"))))) + (let ((yas-key-syntaxes '(yas-try-key-from-whitespace))) + (yas-should-expand '(("xxx\n'quote" . "xxx\nOKquoteOK") + ("xxx 'quote" . "xxx OKquoteOK")))) + (let ((yas-key-syntaxes '(yas-shortest-key-until-whitespace)) + (yas--foobarbaz t) (yas--barbaz t)) + (yas-should-expand '(("foo-barbaz" . "foo-barOKbazOK"))) + (setq yas-key-syntaxes '(yas-longest-key-from-whitespace)) + (yas-should-expand '(("foo-barbaz" . "OKfoo-barbazOK") + ("foo " . "foo ")))))))) + ;;; Loading ;;; @@ -276,10 +393,10 @@ TODO: correct this bug!" ;; saving all definitions before overriding anything ensures FDEFINITION ;; errors don't cause accidental permanent redefinitions. ;; - (labels ((set-fdefinitions (names functions) - (loop for name in names - for fn in functions - do (fset name fn)))) + (cl-labels ((set-fdefinitions (names functions) + (loop for name in names + for fn in functions + do (fset name fn)))) (set-fdefinitions definition-names overriding-functions) (unwind-protect (funcall function) (set-fdefinitions definition-names saved-functions))))) @@ -298,9 +415,6 @@ TODO: correct this bug!" (lambda ,@(rest thingy)))) fdefinitions))) -(put 'yas--with-temporary-redefinitions 'lisp-indent-function 1) -(put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body)) - (defmacro yas-with-overriden-buffer-list (&rest body) (let ((saved-sym (make-symbol "yas--buffer-list"))) `(let ((,saved-sym (symbol-function 'buffer-list))) @@ -312,6 +426,7 @@ TODO: correct this bug!" (funcall ,saved-sym)))) ,@body)))) + (defmacro yas-with-some-interesting-snippet-dirs (&rest body) `(yas-saving-variables (yas-with-overriden-buffer-list @@ -331,6 +446,7 @@ TODO: correct this bug!" ("lisp-interaction-mode" ("sc" . "brother from another mother")))) ,@body)))) + (ert-deftest basic-jit-loading () "Test basic loading and expansion of snippets" (yas-with-some-interesting-snippet-dirs @@ -355,14 +471,63 @@ TODO: correct this bug!" ("c-mode" (".yas-parents" . "cc-mode")) ("cc-mode" - (".yas-parents" . "yet-another-c-mode")) + (".yas-parents" . "yet-another-c-mode and-that-one")) ("yet-another-c-mode" - (".yas-parents" . "c-mode")))) + (".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode")))) (yas-reload-all) - (condition-case nil - (yas--all-parents 'c-mode) - (error - (ert-fail "cyclic parenthood test failed")))))) + (with-temp-buffer + (let* ((major-mode 'c-mode) + (expected `(c-mode + cc-mode + yet-another-c-mode + and-also-this-one + and-that-one + ;; prog-mode doesn't exit in emacs 24.3 + ,@(if (fboundp 'prog-mode) + '(prog-mode)) + emacs-lisp-mode + lisp-interaction-mode)) + (observed (yas--modes-to-activate))) + (should (null (cl-set-exclusive-or expected observed))) + (should (= (length expected) + (length observed)))))))) + +(defalias 'yas--phony-c-mode 'c-mode) + +(ert-deftest issue-492-and-494 () + (define-derived-mode yas--test-mode yas--phony-c-mode "Just a test mode") + (yas-with-snippet-dirs '((".emacs.d/snippets" + ("yas--test-mode"))) + (yas-reload-all) + (with-temp-buffer + (let* ((major-mode 'yas--test-mode) + (expected `(c-mode + ,@(if (fboundp 'prog-mode) + '(prog-mode)) + yas--phony-c-mode + yas--test-mode)) + (observed (yas--modes-to-activate))) + (should (null (cl-set-exclusive-or expected observed))) + (should (= (length expected) + (length observed))))))) + +(define-derived-mode yas--test-mode c-mode "Just a test mode") +(define-derived-mode yas--another-test-mode c-mode "Another test mode") + +(ert-deftest issue-504-tricky-jit () + (yas-with-snippet-dirs + '((".emacs.d/snippets" + ("yas--another-test-mode" + (".yas-parents" . "yas--test-mode")) + ("yas--test-mode"))) + (let ((b (with-current-buffer (generate-new-buffer "*yas-test*") + (yas--another-test-mode) + (current-buffer)))) + (unwind-protect + (progn + (yas-reload-all) + (should (= 0 (hash-table-count yas--scheduled-jit-loads)))) + (kill-buffer b))))) (defun yas--basic-jit-loading-1 () (with-temp-buffer @@ -534,7 +699,11 @@ TODO: be meaner" (yas-reload-all) (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand))) (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand)))) - (setcdr yas-minor-mode-map (cdr (yas--init-minor-keymap))))) + ;; FIXME: actually should restore to whatever saved values where there. + ;; + (define-key yas-minor-mode-map [tab] 'yas-expand) + (define-key yas-minor-mode-map (kbd "TAB") 'yas-expand) + (define-key yas-minor-mode-map (kbd "SPC") nil))) (ert-deftest test-yas-in-org () (with-temp-buffer @@ -543,36 +712,57 @@ TODO: be meaner" (should (eq (key-binding [(tab)]) 'yas-expand)) (should (eq (key-binding (kbd "TAB")) 'yas-expand)))) +(ert-deftest test-yas-activate-extra-modes () + "Given a symbol, `yas-activate-extra-mode' should be able to +add the snippets associated with the given mode." + (with-temp-buffer + (yas-saving-variables + (yas-with-snippet-dirs + '((".emacs.d/snippets" + ("markdown-mode" + ("_" . "_Text_ ")) + ("emacs-lisp-mode" + ("car" . "(car )")))) + (yas-reload-all) + (emacs-lisp-mode) + (yas-minor-mode-on) + (yas-activate-extra-mode 'markdown-mode) + (should (eq 'markdown-mode (car yas--extra-modes))) + (yas-should-expand '(("_" . "_Text_ "))) + (yas-should-expand '(("car" . "(car )"))) + (yas-deactivate-extra-mode 'markdown-mode) + (should-not (eq 'markdown-mode (car yas--extra-modes))) + (yas-should-not-expand '("_")) + (yas-should-expand '(("car" . "(car )"))))))) + ;;; Helpers ;;; -(defun yas/ert () - (interactive) - (with-temp-buffer - (yas--with-temporary-redefinitions - ((message (&rest _args) nil)) - (ert t (buffer-name (current-buffer))) - (princ (buffer-string))))) - - (defun yas-should-expand (keys-and-expansions) (dolist (key-and-expansion keys-and-expansions) (yas-exit-all-snippets) - (erase-buffer) + (narrow-to-region (point) (point)) (insert (car key-and-expansion)) (let ((yas-fallback-behavior nil)) (ert-simulate-command '(yas-expand))) - (should (string= (yas--buffer-contents) (cdr key-and-expansion)))) + (unless (string= (yas--buffer-contents) (cdr key-and-expansion)) + (ert-fail (format "\"%s\" should have expanded to \"%s\" but got \"%s\"" + (car key-and-expansion) + (cdr key-and-expansion) + (yas--buffer-contents))))) (yas-exit-all-snippets)) (defun yas-should-not-expand (keys) (dolist (key keys) (yas-exit-all-snippets) - (erase-buffer) + (narrow-to-region (point) (point)) (insert key) (let ((yas-fallback-behavior nil)) (ert-simulate-command '(yas-expand))) - (should (string= (yas--buffer-contents) key)))) + (unless (string= (yas--buffer-contents) key) + (ert-fail (format "\"%s\" should have stayed put, but instead expanded to \"%s\"" + key + (yas--buffer-contents)))))) (defun yas-mock-insert (string) (interactive) @@ -612,10 +802,6 @@ TODO: be meaner" for saved in saved-values do (set var saved))))) -(defmacro yas-saving-variables (&rest body) - `(yas-call-with-saving-variables #'(lambda () ,@body))) - - (defun yas-call-with-snippet-dirs (dirs fn) (let* ((default-directory (make-temp-file "yasnippet-fixture" t)) (yas-snippet-dirs (mapcar #'car dirs))) @@ -627,12 +813,6 @@ TODO: be meaner" (when (>= emacs-major-version 24) (delete-directory default-directory 'recursive)))))) -(defmacro yas-with-snippet-dirs (dirs &rest body) - (declare (indent defun)) - `(yas-call-with-snippet-dirs ,dirs - #'(lambda () - ,@body))) - ;;; Older emacsen ;;; (unless (fboundp 'special-mode) @@ -645,5 +825,21 @@ TODO: be meaner" ;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert +(put 'yas-saving-variables 'edebug-form-spec t) +(put 'yas-with-snippet-dirs 'edebug-form-spec t) +(put 'yas-with-overriden-buffer-list 'edebug-form-spec t) +(put 'yas-with-some-interesting-snippet-dirs 'edebug-form-spec t) + + +(put 'yas--with-temporary-redefinitions 'lisp-indent-function 1) +(put 'yas--with-temporary-redefinitions 'edebug-form-spec '((&rest (defun*)) cl-declarations body)) + + + + (provide 'yasnippet-tests) +;; Local Variables: +;; indent-tabs-mode: nil +;; byte-compile-warnings: (not cl-functions) +;; End: ;;; yasnippet-tests.el ends here