Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 303 lines (261 sloc) 11.499 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
;;; yasnippet-tests.el --- some yasnippet tests

;; Copyright (C) 2012 João Távora

;; Author: João Távora <joaot@siscog.pt>
;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; 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
;;;
(defmacro yas/with-some-interesting-snippet-dirs (&rest body)
  `(yas/saving-variables
    (yas/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"))))
     ,@body)))

(ert-deftest basic-jit-loading ()
  "Test basic loading and expansion of snippets"
  (yas/with-some-interesting-snippet-dirs
   (yas/reload-all)
   (yas/basic-jit-loading-1)))

(ert-deftest basic-jit-loading-with-compiled-snippets ()
  "Test basic loading and expansion of snippets"
  (yas/with-some-interesting-snippet-dirs
   (yas/reload-all)
   (yas/recompile-all)
   (flet ((yas/load-directory-2
           (&rest dummies)
           (ert-fail "yas/load-directory-2 shouldn't be called when snippets have been compiled")))
     (yas/reload-all)
     (yas/basic-jit-loading-1))))

(defun yas/basic-jit-loading-1 (&optional compile)
  (with-temp-buffer
    (should (= 4 (hash-table-count yas/scheduled-jit-loads)))
    (should (= 0 (hash-table-count yas/tables)))
    (lisp-interaction-mode)
    (yas/minor-mode 1)
    (should (= 2 (hash-table-count yas/scheduled-jit-loads)))
    (should (= 2 (hash-table-count yas/tables)))
    (should (= 1 (hash-table-count (yas/table-uuidhash (gethash 'lisp-interaction-mode yas/tables)))))
    (should (= 2 (hash-table-count (yas/table-uuidhash (gethash 'emacs-lisp-mode yas/tables)))))
    (yas/should-expand '(("sc" . "brother from another mother")
                         ("dolist" . "(dolist)")
                         ("ert-deftest" . "(ert-deftest name () )")))
    (c-mode)
    (yas/minor-mode 1)
    (should (= 0 (hash-table-count yas/scheduled-jit-loads)))
    (should (= 4 (hash-table-count yas/tables)))
    (should (= 1 (hash-table-count (yas/table-uuidhash (gethash 'c-mode yas/tables)))))
    (should (= 1 (hash-table-count (yas/table-uuidhash (gethash 'cc-mode yas/tables)))))
    (yas/should-expand '(("printf" . "printf();")
                         ("def" . "# define")))
    (yas/should-not-expand '("sc" "dolist" "ert-deftest"))))

;;; Smart recompilation
;;;
(ert-deftest smart-recompilation ()
  "Test if the mtime-based recompilation logic works"
  (yas/with-snippet-dirs
   '((".emacs.d/snippets"
      ("c-mode"
       ("printf" . "printf($1);")
       ("fopen" . "fopen($1);")
       ("stupidsnippet" . "stupidbla($1);"))))
   (yas/recompile-all)
   (let ((compiled ".emacs.d/snippets/c-mode/.yas-compiled-snippets.el"))
     (should (file-readable-p compiled))
     (let ((yas/auto-compile-snippets t)
           (yas/verbosity 4)
           (old-compiled-mtime (nth 5 (file-attributes compiled))))
       (flet ((test-compiled-snippets-mtime ()
                                            (sit-for 1)
                                            (yas/reload-all t)
                                            (should (file-readable-p compiled))
                                            (let ((new-compiled-mtime (nth 5 (file-attributes compiled))))
                                              (should (time-less-p old-compiled-mtime
                                                                   new-compiled-mtime))
                                              (setq old-compiled-mtime new-compiled-mtime))))
         ;; modify a snippet file
         ;;
         (sit-for 1)
         (with-current-buffer
             (find-file-noselect ".emacs.d/snippets/c-mode/stupidsnippet")
           (insert "something")
           (save-buffer)
           (kill-buffer (current-buffer)))
         (test-compiled-snippets-mtime)
         ;; delete a snippet file
         ;;
         (delete-file ".emacs.d/snippets/c-mode/stupidsnippet")
         (test-compiled-snippets-mtime)
         ;; add a snippet file
         ;;
         (sit-for 1)
         (with-current-buffer (find-file-noselect ".emacs.d/snippets/c-mode/anotherstupidsnippet")
           (insert "blablabla")
           (save-buffer)
           (kill-buffer (current-buffer)))
         (test-compiled-snippets-mtime))))))


;;; Helpers
;;;

(defun yas/should-expand (keys-and-expansions)
  (dolist (key-and-expansion keys-and-expansions)
    (yas/exit-all-snippets)
    (erase-buffer)
    (insert (car key-and-expansion))
    (let ((yas/fallback-behavior nil))
      (ert-simulate-command '(yas/expand)))
    (should (string= (buffer-substring-no-properties (point-min) (point-max))
                       (cdr key-and-expansion))))
  (yas/exit-all-snippets))

(defun yas/should-not-expand (keys)
  (dolist (key keys)
    (yas/exit-all-snippets)
    (erase-buffer)
    (insert key)
    (let ((yas/fallback-behavior nil))
      (ert-simulate-command '(yas/expand)))
    (should (string= (buffer-substring-no-properties (point-min) (point-max)) key))))

(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-temp-buffer
             (insert content)
             (write-region nil nil file-or-dir-name nil 'nomessage)))
          (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))

(defun yas/call-with-snippet-dirs (dirs fn)
  (let* ((default-directory (make-temp-file "yasnippet-fixture" t))
         (yas/snippet-dirs (mapcar #'car dirs)))
    (with-temp-message ""
      (unwind-protect
          (progn
            (mapc #'yas/make-file-or-dirs dirs)
            (funcall fn))
        (when (>= emacs-major-version 23)
          (delete-directory default-directory 'recursive))))))

(defmacro yas/with-snippet-dirs (dirs &rest body)
  `(yas/call-with-snippet-dirs ,dirs
                               #'(lambda ()
                                   ,@body)))

;;; Older emacsen
;;;
(unless (fboundp 'special-mode)
  (define-minor-mode special-mode "Just a placeholder for something isn't in emacs 22"))

;;; btw to test this in emacs22 mac osx:
;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert.el
;;; curl -L -O https://github.com/mirrors/emacs/raw/master/lisp/emacs-lisp/ert-x.el
;;; /usr/bin/emacs -nw -Q -L . -l yasnippet-tests.el --batch -e ert

(provide 'yasnippet-tests)
;;; yasnippet-tests.el ends here
Something went wrong with that request. Please try again.