From 6be1de4a09cc24c60d93a256e582b9ef618820e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20T=C3=A1vora?= Date: Tue, 31 Jul 2012 08:41:52 +0100 Subject: [PATCH] incomplete: preparing tests to fix #271 --- yasnippet-tests.el | 56 +++++++++++++++++++++++++++++++++++----------- yasnippet.el | 40 +++++++++++++++++---------------- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/yasnippet-tests.el b/yasnippet-tests.el index 536fea2..3aa1ecd 100644 --- a/yasnippet-tests.el +++ b/yasnippet-tests.el @@ -31,11 +31,14 @@ ;;; Snippet mechanics +(defun yas--buffer-contents () + (buffer-substring-no-properties (point-min) (point-max))) + (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)) + (should (string= (yas--buffer-contents) "brother from another mother")) (should (looking-at "brother")) @@ -48,30 +51,30 @@ (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)) + (should (string= (yas--buffer-contents) "brother from another brother")) (ert-simulate-command `(yas-mock-insert "bla")) - (should (string= (buffer-substring-no-properties (point-min) (point-max)) + (should (string= (yas--buffer-contents) "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)) + (should (string= (yas--buffer-contents) "brother from another BROTHER")) (ert-simulate-command `(yas-mock-insert "bla")) - (should (string= (buffer-substring-no-properties (point-min) (point-max)) + (should (string= (yas--buffer-contents) "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)) + (should (string= (yas--buffer-contents) "brother from another mother!")) (ert-simulate-command `(yas-mock-insert "bla")) - (should (string= (buffer-substring-no-properties (point-min) (point-max)) + (should (string= (yas--buffer-contents) "brother from bla!")))) (ert-deftest nested-placeholders-use-subfield () @@ -80,7 +83,7 @@ (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)) + (should (string= (yas--buffer-contents) "brother from another bla!")))) ;; (ert-deftest in-snippet-undo () @@ -90,9 +93,37 @@ ;; (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)) +;; (should (string= (yas--buffer-contents) ;; "brother from another mother!")))) + +;;; Snippet expansion +;;; +(ert-deftest escape-dollar () + (with-temp-buffer + (yas-minor-mode 1) + (yas-expand-snippet "bla\\${1:bla}ble") + (should (string= (yas--buffer-contents) "bla${1:bla}ble")))) + +(ert-deftest escape-closing-brace () + (with-temp-buffer + (yas-minor-mode 1) + (yas-expand-snippet "bla${1:bla\\}}ble") + (should (string= (yas--buffer-contents) "blabla}ble")) + (should (string= (yas-field-value 1) "bla}")))) + +(ert-deftest escape-some-elisp-with-strings () + (with-temp-buffer + (yas-minor-mode 1) + ;; The rules here is: to output a literal `"' you need to escape + ;; it with one backslash. You don't need to escape them in + ;; embedded elisp. + (yas-expand-snippet "soon \\\"`(concat (upcase \"(my arms)\")\"\\\" were all around her\")`") + (should (string= (yas--buffer-contents) "soon \"MY ARMS\" were all around her")))) + + + + ;;; Misc tests ;;; @@ -105,7 +136,7 @@ TODO: correct this bug!" (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)) + (should (string= (yas--buffer-contents) "brother from another mother") ;; no newline should be here! ))) @@ -297,8 +328,7 @@ TODO: be meaner" (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)))) + (should (string= (yas--buffer-contents) (cdr key-and-expansion)))) (yas-exit-all-snippets)) (defun yas-should-not-expand (keys) @@ -308,7 +338,7 @@ TODO: be meaner" (insert key) (let ((yas-fallback-behavior nil)) (ert-simulate-command '(yas-expand))) - (should (string= (buffer-substring-no-properties (point-min) (point-max)) key)))) + (should (string= (yas--buffer-contents) key)))) (defun yas-mock-insert (string) (interactive) diff --git a/yasnippet.el b/yasnippet.el index f9c0b00..28bd7e8 100644 --- a/yasnippet.el +++ b/yasnippet.el @@ -2799,11 +2799,11 @@ Use this in primary and mirror transformations to tget." (not (string= "" yas-text))) yas-text)) -;; (defun yas-selected-text () -;; "Return `yas-selected-text' if that exists and is non-empty, else nil." -;; (if (and yas-selected-text -;; (not (string= "" yas-selected-text))) -;; yas-selected-text)) +(defun yas-selected-text () + "Return `yas-selected-text' if that exists and is non-empty, else nil." + (if (and yas-selected-text + (not (string= "" yas-selected-text))) + yas-selected-text)) (defun yas--get-field-once (number &optional transform-fn) (unless yas-modified-p @@ -3447,22 +3447,24 @@ template. EXPAND-ENV is are let-style variable to value bindings considered when expanding the snippet." (run-hooks 'yas-before-expand-snippet-hook) - ;; If a region is active, set `yas-selected-text' - (setq yas-selected-text - (when (region-active-p) - (prog1 (buffer-substring-no-properties (region-beginning) - (region-end)) - (unless start (setq start (region-beginning)) - (unless end (setq end (region-end))))))) - - (when start - (goto-char start)) - ;; - (let ((to-delete (and start end (buffer-substring-no-properties start end))) - (start (or start (point))) - (end (or end (point))) + (let ((yas-selected-text (or yas-selected-text + (and (region-active-p) + (buffer-substring-no-properties (region-beginning) + (region-end))))) + (to-delete (and start + end + (buffer-substring-no-properties start end))) + (start (or start + (and (region-active-p) + (region-beginning)) + (point))) + (end (or end + (and (region-active-p) + (region-end)) + (point))) snippet) + (goto-char start) (setq yas--indent-original-column (current-column)) ;; Delete the region to delete, this *does* get undo-recorded. ;;