-;;; yasnippet-tests.el --- some yasnippet tests
+;;; yasnippet-tests.el --- some yasnippet tests -*- lexical-binding: t -*-
-;; Copyright (C) 2012 João Távora
+;; Copyright (C) 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
;; Author: João Távora <joaot@siscog.pt>
;; Keywords: emulations, convenience
(require 'yasnippet)
(require 'ert)
(require 'ert-x)
+(require 'cl)
\f
;;; Snippet mechanics
(yas-expand-snippet "${1:brother} from another $1")
(should (string= (yas--buffer-contents)
"brother from another brother"))
- (ert-simulate-command `(yas-mock-insert "bla"))
+ (yas-mock-insert "bla")
(should (string= (yas--buffer-contents)
"bla from another bla"))))
(yas-expand-snippet "${1:brother} from another ${1:$(upcase yas-text)}")
(should (string= (yas--buffer-contents)
"brother from another BROTHER"))
- (ert-simulate-command `(yas-mock-insert "bla"))
+ (yas-mock-insert "bla")
(should (string= (yas--buffer-contents)
"bla from another BLA"))))
(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"))
+ (yas-mock-insert "foo")
(should (string= (yas--buffer-contents) "FOObarFOO")))))
(ert-deftest nested-placeholders-kill-superfield ()
(yas-expand-snippet "brother from ${2:another ${3:mother}}!")
(should (string= (yas--buffer-contents)
"brother from another mother!"))
- (ert-simulate-command `(yas-mock-insert "bla"))
+ (yas-mock-insert "bla")
(should (string= (yas--buffer-contents)
"brother from bla!"))))
(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"))
+ (yas-mock-insert "bla")
(should (string= (yas--buffer-contents)
"brother from another bla!"))))
(yas-expand-snippet "<%= f.submit \"${1:Submit}\"${2:$(and (yas-text) \", :disable_with => '\")}${2:$1ing...}${2:$(and (yas-text) \"'\")} %>")
(should (string= (yas--buffer-contents)
"<%= f.submit \"Submit\", :disable_with => 'Submiting...' %>"))
- (ert-simulate-command `(yas-mock-insert "Send"))
+ (yas-mock-insert "Send")
(should (string= (yas--buffer-contents)
"<%= f.submit \"Send\", :disable_with => 'Sending...' %>"))))
(with-temp-buffer
(yas-minor-mode 1)
(yas-expand-snippet "${1:FOOOOOOO}${2:$1}${3:$2}${4:$3}")
- (ert-simulate-command `(yas-mock-insert "abc"))
+ (yas-mock-insert "abc")
(should (string= (yas--buffer-contents) "abcabcabcabc"))))
(ert-deftest delete-numberless-inner-snippet-issue-562 ()
(should (looking-at "ble"))
(should (null (yas--snippets-at-point)))))
+(ert-deftest ignore-trailing-whitespace ()
+ (should (equal
+ (with-temp-buffer
+ (insert "# key: foo\n# --\nfoo")
+ (yas--parse-template))
+ (with-temp-buffer
+ (insert "# key: foo \n# --\nfoo")
+ (yas--parse-template)))))
+
;; (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"))
+;; (yas-mock-insert "bla")
;; (ert-simulate-command '(undo))
;; (should (string= (yas--buffer-contents)
;; "brother from another mother!"))))
+(ert-deftest dont-clear-on-partial-deletion-issue-515 ()
+ "Ensure fields are not cleared when user doesn't really mean to."
+ (with-temp-buffer
+ (yas-minor-mode 1)
+ (yas-expand-snippet "my ${1:kid brother} from another ${2:mother}")
+
+ (ert-simulate-command '(kill-word 1))
+ (ert-simulate-command '(delete-char 1))
+
+ (should (string= (yas--buffer-contents)
+ "my brother from another mother"))
+ (should (looking-at "brother"))
+
+ (ert-simulate-command '(yas-next-field))
+ (should (looking-at "mother"))
+ (ert-simulate-command '(yas-prev-field))
+ (should (looking-at "brother"))))
+
+(ert-deftest do-clear-on-yank-issue-515 ()
+ "A yank should clear an unmodified field."
+ (with-temp-buffer
+ (yas-minor-mode 1)
+ (yas-expand-snippet "my ${1:kid brother} from another ${2:mother}")
+ (yas-mock-yank "little sibling")
+ (should (string= (yas--buffer-contents)
+ "my little sibling from another mother"))
+ (ert-simulate-command '(yas-next-field))
+ (ert-simulate-command '(yas-prev-field))
+ (should (looking-at "little sibling"))))
+
\f
;;; Snippet expansion and character escaping
;;; Thanks to @zw963 (Billy) for the testing
(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"))
+ (yas-mock-insert "bbb")
(should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
(defmacro yas--with-font-locked-temp-buffer (&rest 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)
\"fail\")}"))
(yas-expand-snippet snippet)
(should (string= (yas--buffer-contents) "fail"))
- (ert-simulate-command `(yas-mock-insert "foobaaar"))
+ (yas-mock-insert "foobaaar")
(should (string= (yas--buffer-contents) "foobaaarfail"))
- (ert-simulate-command `(yas-mock-insert "baz"))
+ (yas-mock-insert "baz")
(should (string= (yas--buffer-contents) "foobaaarbazok")))))
\f
"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.
;;
(should (equal (sort expected-rest #'string<)
(sort (cl-subseq observed (length expected-first)) #'string<))))))))
+(defalias 'yas--phony-c-mode 'c-mode)
+
(ert-deftest issue-492-and-494 ()
- (defalias 'yas--phony-c-mode 'c-mode)
(define-derived-mode yas--test-mode yas--phony-c-mode "Just a test mode")
(yas-with-snippet-dirs '((".emacs.d/snippets"
("yas--test-mode")))
(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 ()
- (define-derived-mode yas--test-mode c-mode "Just a test mode")
- (define-derived-mode yas--another-test-mode c-mode "Another test mode")
(yas-with-snippet-dirs
'((".emacs.d/snippets"
("yas--another-test-mode"
(yas--buffer-contents))))))
(defun yas-mock-insert (string)
- (interactive)
- (do ((i 0 (1+ i)))
- ((= i (length string)))
- (insert (aref string i))))
+ (dotimes (i (length string))
+ (let ((last-command-event (aref string i)))
+ (ert-simulate-command '(self-insert-command 1)))))
+
+(defun yas-mock-yank (string)
+ (let ((interprogram-paste-function (lambda () string)))
+ (ert-simulate-command '(yank nil))))
(defun yas-make-file-or-dirs (ass)
(let ((file-or-dir-name (car ass))
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)))
(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)
(provide 'yasnippet-tests)
;; Local Variables:
;; indent-tabs-mode: nil
-;; lexical-binding: t
;; byte-compile-warnings: (not cl-functions)
;; End:
;;; yasnippet-tests.el ends here