New test for cc-mode's fontification functions

This issue requires font locking to trigger. This can be be done by
let-binding noninteractive to nil and using a buffer name that doesn't
start with a space, instead of the external emacs hack which is now
redundant.

* yasnippet-tests.el (yas--with-font-locked-temp-buffer): new macro
similar to with-temp-buffer but enables font-lock-mode even in -batch
mode.
(example-for-issue-474): new test
(example-for-issue-404, example-for-issue-404-c-mode): use new macro
(example-for-issue-404-external-emacs): removed
This commit is contained in:
Noam Postavsky 2014-04-26 19:09:57 -04:00
parent 4d9eee93ba
commit 4473b4ede5

View File

@ -204,60 +204,56 @@
(ert-simulate-command `(yas-mock-insert "bbb")) (ert-simulate-command `(yas-mock-insert "bbb"))
(should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend"))))) (should (string= (yas--buffer-contents) "if condition\naaa\nelse\nbbb\nend")))))
(ert-deftest example-for-issue-404 () (defmacro yas--with-font-locked-temp-buffer (&rest body)
(with-temp-buffer "Like `with-temp-buffer', but ensure `font-lock-mode'."
(c++-mode) (declare (indent 0) (debug t))
(yas-minor-mode 1) (let ((temp-buffer (make-symbol "temp-buffer")))
(insert "#include <foo>\n") ;; NOTE: buffer name must not start with a space, otherwise
(let ((snippet "main")) ;; `font-lock-mode' doesn't turn on.
(let ((yas-good-grace nil)) (yas-expand-snippet snippet)) `(let ((,temp-buffer (generate-new-buffer "*yas-temp*")))
(should (string= (yas--buffer-contents) "#include <foo>\nmain"))))) (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))))))))
(ert-deftest example-for-issue-404-c-mode () (ert-deftest example-for-issue-474 ()
(with-temp-buffer (yas--with-font-locked-temp-buffer
(c-mode) (c-mode)
(yas-minor-mode 1) (yas-minor-mode 1)
(insert "#include <foo>\n") (insert "#include <foo>\n")
(let ((snippet "main")) (let ((yas-good-grace nil)) (yas-expand-snippet "`(insert \"TODO: \")`"))
(let ((yas-good-grace nil)) (yas-expand-snippet snippet)) (should (string= (yas--buffer-contents) "#include <foo>\nTODO: "))))
(should (string= (yas--buffer-contents) "#include <foo>\nmain")))))
(ert-deftest example-for-issue-404-external-emacs () (ert-deftest example-for-issue-404 ()
:tags '(:external) (yas--with-font-locked-temp-buffer
(let ((fixture-el-file (make-temp-file "yas-404-fixture" nil ".el"))) (c++-mode)
(with-temp-buffer (yas-minor-mode 1)
(insert (pp-to-string (insert "#include <foo>\n")
`(condition-case _ (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
(progn (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
(require 'yasnippet-tests)
(yas-with-snippet-dirs (ert-deftest example-for-issue-404-c-mode ()
'((".emacs.d/snippets" (yas--with-font-locked-temp-buffer
("c-mode" (c-mode)
("main" . "int main ()")))) (yas-minor-mode 1)
(yas-global-mode) (insert "#include <foo>\n")
(switch-to-buffer "foo.c") (let ((yas-good-grace nil)) (yas-expand-snippet "main"))
(c-mode) (should (string= (yas--buffer-contents) "#include <foo>\nmain"))))
(insert "#include <iostream>\nmain")
(setq yas-good-grace nil)
(yas-expand)
(should (string= (buffer-string)
"#include <iostream>\nint main ()"))
(kill-emacs 0)))
(error (kill-emacs -1)))))
(write-file fixture-el-file))
(should (= 0
(call-process (concat invocation-directory invocation-name)
nil nil nil
"-Q" "--batch"
"-L" "." "-l" fixture-el-file)))))
(ert-deftest middle-of-buffer-snippet-insertion () (ert-deftest middle-of-buffer-snippet-insertion ()
(with-temp-buffer (with-temp-buffer
(yas-minor-mode 1) (yas-minor-mode 1)
(insert "beginning") (insert "beginning")
(save-excursion (insert "end")) (save-excursion (insert "end"))
(let ((snippet "-middle-")) (yas-expand-snippet "-middle-")
(yas-expand-snippet snippet))
(should (string= (yas--buffer-contents) "beginning-middle-end")))) (should (string= (yas--buffer-contents) "beginning-middle-end"))))
(ert-deftest another-example-for-issue-271 () (ert-deftest another-example-for-issue-271 ()