+(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 <foo>\n")
+ (let ((yas-good-grace nil)) (yas-expand-snippet "`\"TODO: \"`"))
+ (should (string= (yas--buffer-contents) "#include <foo>\nTODO: "))))
+
+(ert-deftest example-for-issue-404 ()
+ (yas--with-font-locked-temp-buffer
+ (c++-mode)
+ (yas-minor-mode 1)
+ (insert "#include <foo>\n")
+ (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
+ (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
+
+(ert-deftest example-for-issue-404-c-mode ()
+ (yas--with-font-locked-temp-buffer
+ (c-mode)
+ (yas-minor-mode 1)
+ (insert "#include <foo>\n")
+ (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
+ (should (string= (yas--buffer-contents) "#include <foo>\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"))))
+