]> code.delx.au - gnu-emacs-elpa/blobdiff - yasnippet-tests.el
Merge pull request #621 from npostavs/dfs-extra-modes2
[gnu-emacs-elpa] / yasnippet-tests.el
index ac95258bc61604deb30ec8ab86167e564f0bf0f2..47565718a9a12afa1d0714af3d713e11a3634554 100644 (file)
     (ert-simulate-command `(yas-mock-insert "abc"))
     (should (string= (yas--buffer-contents) "abcabcabcabc"))))
 
+(ert-deftest delete-numberless-inner-snippet-issue-562 ()
+  (with-temp-buffer
+    (yas-minor-mode 1)
+    (yas-expand-snippet "${3:${test}bla}$0${2:ble}")
+    (ert-simulate-command '(yas-next-field-or-maybe-expand))
+    (should (looking-at "testblable"))
+    (ert-simulate-command '(yas-next-field-or-maybe-expand))
+    (ert-simulate-command '(yas-skip-and-clear-or-delete-char))
+    (should (looking-at "ble"))
+    (should (null (yas--snippets-at-point)))))
+
 ;; (ert-deftest in-snippet-undo ()
 ;;   (with-temp-buffer
 ;;     (yas-minor-mode 1)
 
 (ert-deftest be-careful-when-escaping-in-yas-selected-text-2 ()
   (with-temp-buffer
+    (yas-minor-mode 1)
     (let ((yas-selected-text "He)}o world!"))
       (yas-expand-snippet "Look ma! ${1:`(yas-selected-text)`} OK?")
       (should (string= (yas--buffer-contents) "Look ma! He)}o world! OK?")))))
@@ -421,6 +433,14 @@ TODO: correct this bug!"
           ("lisp-interaction-mode" ("sc" . "brother from another mother"))))
        ,@body))))
 
+(ert-deftest snippet-lookup ()
+  "Test `yas-lookup-snippet'."
+  (yas-with-some-interesting-snippet-dirs
+   (yas-reload-all 'no-jit)
+   (should (equal (yas-lookup-snippet "printf" 'c-mode) "printf($1);"))
+   (should (equal (yas-lookup-snippet "def" 'c-mode) "# define"))
+   (should-not (yas-lookup-snippet "no such snippet" nil 'noerror))
+   (should-not (yas-lookup-snippet "printf" 'emacs-lisp-mode 'noerror))))
 
 (ert-deftest basic-jit-loading ()
   "Test basic loading and expansion of snippets"
@@ -429,7 +449,7 @@ TODO: correct this bug!"
    (yas--basic-jit-loading-1)))
 
 (ert-deftest basic-jit-loading-with-compiled-snippets ()
-  "Test basic loading and expansion of snippets"
+  "Test basic loading and expansion of compiled snippets"
   (yas-with-some-interesting-snippet-dirs
    (yas-reload-all)
    (yas-recompile-all)
@@ -439,6 +459,20 @@ TODO: correct this bug!"
      (yas-reload-all)
      (yas--basic-jit-loading-1))))
 
+(ert-deftest visiting-compiled-snippets ()
+  "Test snippet visiting for compiled snippets."
+  (yas-with-some-interesting-snippet-dirs
+   (yas-recompile-all)
+   (yas-reload-all 'no-jit) ; must be loaded for `yas-lookup-snippet' to work.
+   (yas--with-temporary-redefinitions ((find-file-noselect
+                                        (filename &rest _)
+                                        (throw 'yas-snippet-file filename)))
+     (should (string-suffix-p
+              "cc-mode/def"
+              (catch 'yas-snippet-file
+                (yas--visit-snippet-file-1
+                 (yas--lookup-snippet-1 "def" 'cc-mode))))))))
+
 (ert-deftest loading-with-cyclic-parenthood ()
   "Test loading when cyclic parenthood is setup."
   (yas-saving-variables
@@ -457,15 +491,40 @@ TODO: correct this bug!"
                           yet-another-c-mode
                           and-also-this-one
                           and-that-one
-                          ;; prog-mode doesn't exit in emacs 24.3
+                          ;; prog-mode doesn't exist in emacs 24.3
+                          ,@(if (fboundp 'prog-mode)
+                                '(prog-mode))
+                          emacs-lisp-mode
+                          lisp-interaction-mode))
+              (observed (yas--modes-to-activate)))
+         (should (equal (sort expected #'string<) (sort observed #'string<))))))))
+
+(ert-deftest extra-modes-parenthood ()
+  "Test activation of parents of `yas--extra-modes'."
+  (yas-saving-variables
+   (yas-with-snippet-dirs '((".emacs.d/snippets"
+                             ("c-mode"
+                              (".yas-parents" . "cc-mode"))
+                             ("cc-mode"
+                              (".yas-parents" . "yet-another-c-mode and-that-one"))
+                             ("yet-another-c-mode"
+                              (".yas-parents" . "c-mode and-also-this-one lisp-interaction-mode"))))
+     (yas-reload-all)
+     (with-temp-buffer
+       (let* ((_ (yas-activate-extra-mode 'c-mode))
+              (expected `(,major-mode
+                          c-mode
+                          cc-mode
+                          yet-another-c-mode
+                          and-also-this-one
+                          and-that-one
+                          ;; prog-mode doesn't exist in emacs 24.3
                           ,@(if (fboundp 'prog-mode)
                                 '(prog-mode))
                           emacs-lisp-mode
                           lisp-interaction-mode))
               (observed (yas--modes-to-activate)))
-         (should (null (cl-set-exclusive-or expected observed)))
-         (should (= (length expected)
-                    (length observed))))))))
+         (should (equal (sort expected #'string<) (sort observed #'string<))))))))
 
 (ert-deftest issue-492-and-494 ()
   (defalias 'yas--phony-c-mode 'c-mode)
@@ -802,6 +861,17 @@ add the snippets associated with the given mode."
   ;; FIXME: Why provide this default definition here?!?
   (defalias 'special-mode 'fundamental))
 
+(unless (fboundp 'string-suffix-p)
+  ;; introduced in Emacs 24.4
+  (defun string-suffix-p (suffix string &optional ignore-case)
+    "Return non-nil if SUFFIX is a suffix of STRING.
+If IGNORE-CASE is non-nil, the comparison is done without paying
+attention to case differences."
+    (let ((start-pos (- (length string) (length suffix))))
+      (and (>= start-pos 0)
+           (eq t (compare-strings suffix nil nil
+                                  string start-pos nil ignore-case))))))
+
 ;;; 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
@@ -822,6 +892,7 @@ add the snippets associated with the given mode."
 
 (provide 'yasnippet-tests)
 ;; Local Variables:
+;; indent-tabs-mode: nil
 ;; lexical-binding: t
 ;; byte-compile-warnings: (not cl-functions)
 ;; End: