+(defmacro yas-with-even-more-interesting-snippet-dirs (&rest body)
+ `(yas-saving-variables
+ (yas-with-snippet-dirs
+ `((".emacs.d/snippets"
+ ("c-mode"
+ (".yas-make-groups" . "")
+ ("printf" . "printf($1);")
+ ("foo-group-a"
+ ("fnprintf" . "fprintf($1);")
+ ("snprintf" . "snprintf($1);"))
+ ("foo-group-b"
+ ("strcmp" . "strecmp($1);")
+ ("strcasecmp" . "strcasecmp($1);")))
+ ("lisp-interaction-mode"
+ ("ert-deftest" . "# group: barbar\n# --\n(ert-deftest ${1:name} () $0)"))
+ ("fancy-mode"
+ ("a-guy" . "# uuid: 999\n# --\nyo!")
+ ("a-sir" . "# uuid: 12345\n# --\nindeed!")
+ ("a-lady" . "# uuid: 54321\n# --\noh-la-la!")
+ ("a-beggar" . "# uuid: 0101\n# --\narrrgh!")
+ ("an-outcast" . "# uuid: 666\n# --\narrrgh!")
+ (".yas-setup.el" . , (pp-to-string
+ '(yas-define-menu 'fancy-mode
+ '((yas-ignore-item "0101")
+ (yas-item "999")
+ (yas-submenu "sirs"
+ ((yas-item "12345")))
+ (yas-submenu "ladies"
+ ((yas-item "54321"))))
+ '("666")))))))
+ ,@body)))
+
+(ert-deftest test-yas-define-menu ()
+ (let ((yas-use-menu t))
+ (yas-with-even-more-interesting-snippet-dirs
+ (yas-reload-all 'no-jit)
+ (let ((menu (cdr (gethash 'fancy-mode yas--menu-table))))
+ (should (eql 4 (length menu)))
+ (dolist (item '("a-guy" "a-beggar"))
+ (should (find item menu :key #'third :test #'string=)))
+ (should-not (find "an-outcast" menu :key #'third :test #'string=))
+ (dolist (submenu '("sirs" "ladies"))
+ (should (keymapp
+ (fourth
+ (find submenu menu :key #'third :test #'string=)))))
+ ))))
+
+(ert-deftest test-group-menus ()
+ "Test group-based menus using .yas-make-groups and the group directive"
+ (let ((yas-use-menu t))
+ (yas-with-even-more-interesting-snippet-dirs
+ (yas-reload-all 'no-jit)
+ ;; first the subdir-based groups
+ ;;
+ (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
+ (should (eql 3 (length menu)))
+ (dolist (item '("printf" "foo-group-a" "foo-group-b"))
+ (should (find item menu :key #'third :test #'string=)))
+ (dolist (submenu '("foo-group-a" "foo-group-b"))
+ (should (keymapp
+ (fourth
+ (find submenu menu :key #'third :test #'string=))))))
+ ;; now group directives
+ ;;
+ (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
+ (should (eql 1 (length menu)))
+ (should (find "barbar" menu :key #'third :test #'string=))
+ (should (keymapp
+ (fourth
+ (find "barbar" menu :key #'third :test #'string=))))))))
+
+(ert-deftest test-group-menus-twisted ()
+ "Same as similarly named test, but be mean.
+
+TODO: be meaner"
+ (let ((yas-use-menu t))
+ (yas-with-even-more-interesting-snippet-dirs
+ ;; add a group directive conflicting with the subdir and watch
+ ;; behaviour
+ (with-temp-buffer
+ (insert "# group: foo-group-c\n# --\nstrecmp($1)")
+ (write-region nil nil (concat (first (yas-snippet-dirs))
+ "/c-mode/foo-group-b/strcmp")))
+ (yas-reload-all 'no-jit)
+ (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
+ (should (eql 4 (length menu)))
+ (dolist (item '("printf" "foo-group-a" "foo-group-b" "foo-group-c"))
+ (should (find item menu :key #'third :test #'string=)))
+ (dolist (submenu '("foo-group-a" "foo-group-b" "foo-group-c"))
+ (should (keymapp
+ (fourth
+ (find submenu menu :key #'third :test #'string=))))))
+ ;; delete the .yas-make-groups file and watch behaviour
+ ;;
+ (delete-file (concat (first (yas-snippet-dirs))
+ "/c-mode/.yas-make-groups"))
+ (yas-reload-all 'no-jit)
+ (let ((menu (cdr (gethash 'c-mode yas--menu-table))))
+ (should (eql 5 (length menu))))
+ ;; Change a group directive and reload
+ ;;
+ (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
+ (should (find "barbar" menu :key #'third :test #'string=)))
+
+ (with-temp-buffer
+ (insert "# group: foofoo\n# --\n(ert-deftest ${1:name} () $0)")
+ (write-region nil nil (concat (first (yas-snippet-dirs))
+ "/lisp-interaction-mode/ert-deftest")))
+ (yas-reload-all 'no-jit)
+ (let ((menu (cdr (gethash 'lisp-interaction-mode yas--menu-table))))
+ (should (eql 1 (length menu)))
+ (should (find "foofoo" menu :key #'third :test #'string=))
+ (should (keymapp
+ (fourth
+ (find "foofoo" menu :key #'third :test #'string=))))))))
+
+\f
+;;; The infamous and problematic tab keybinding
+;;;
+(ert-deftest test-yas-tab-binding ()
+ (with-temp-buffer
+ (yas-minor-mode -1)
+ (should (not (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand)))
+ (yas-minor-mode 1)
+ (should (eq (key-binding (yas--read-keybinding "<tab>")) 'yas-expand))
+ (yas-expand-snippet "$1 $2 $3")
+ (should (eq (key-binding [(tab)]) 'yas-next-field-or-maybe-expand))
+ (should (eq (key-binding (kbd "TAB")) 'yas-next-field-or-maybe-expand))
+ (should (eq (key-binding [(shift tab)]) 'yas-prev-field))
+ (should (eq (key-binding [backtab]) 'yas-prev-field))))
+
+(ert-deftest test-rebindings ()
+ (unwind-protect
+ (progn
+ (define-key yas-minor-mode-map [tab] nil)
+ (define-key yas-minor-mode-map (kbd "TAB") nil)
+ (define-key yas-minor-mode-map (kbd "SPC") 'yas-expand)
+ (with-temp-buffer
+ (yas-minor-mode 1)
+ (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
+ (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))
+ (yas-reload-all)
+ (should (not (eq (key-binding (yas--read-keybinding "TAB")) 'yas-expand)))
+ (should (eq (key-binding (yas--read-keybinding "SPC")) 'yas-expand))))
+ ;; 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
+ (org-mode)
+ (yas-minor-mode 1)
+ (should (eq (key-binding [(tab)]) 'yas-expand))
+ (should (eq (key-binding (kbd "TAB")) 'yas-expand))))