]> code.delx.au - gnu-emacs-elpa/blob - yasnippet-tests.el
Added a few ert unit tests, mostly for very basic snippet mechanics
[gnu-emacs-elpa] / yasnippet-tests.el
1 ;;; yasnippet-tests.el --- some yasnippet tests
2
3 ;; Copyright (C) 2012 João Távora
4
5 ;; Author: João Távora <joaot@siscog.pt>
6 ;; Keywords: emulations, convenience
7
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;; Attempt to test basic snippet mechanics and the loading system
24
25 ;;; Code:
26
27 (require 'yasnippet)
28 (require 'ert)
29 (require 'ert-x)
30
31
32 ;;; Snippet mechanics
33
34 (ert-deftest field-navigation ()
35 (with-temp-buffer
36 (yas/minor-mode 1)
37 (yas/expand-snippet "${1:brother} from another ${2:mother}")
38 (should (string= (buffer-substring-no-properties (point-min) (point-max))
39 "brother from another mother"))
40
41 (should (looking-at "brother"))
42 (ert-simulate-command '(yas/next-field-or-maybe-expand))
43 (should (looking-at "mother"))
44 (ert-simulate-command '(yas/prev-field))
45 (should (looking-at "brother"))))
46
47 (ert-deftest simple-mirror ()
48 (with-temp-buffer
49 (yas/minor-mode 1)
50 (yas/expand-snippet "${1:brother} from another $1")
51 (should (string= (buffer-substring-no-properties (point-min) (point-max))
52 "brother from another brother"))
53 (ert-simulate-command `(yas/mock-insert "bla"))
54 (should (string= (buffer-substring-no-properties (point-min) (point-max))
55 "bla from another bla"))))
56
57 (ert-deftest mirror-with-transformation ()
58 (with-temp-buffer
59 (yas/minor-mode 1)
60 (yas/expand-snippet "${1:brother} from another ${1:$(upcase yas/text)}")
61 (should (string= (buffer-substring-no-properties (point-min) (point-max))
62 "brother from another BROTHER"))
63 (ert-simulate-command `(yas/mock-insert "bla"))
64 (should (string= (buffer-substring-no-properties (point-min) (point-max))
65 "bla from another BLA"))))
66
67 (ert-deftest nested-placeholders-kill-superfield ()
68 (with-temp-buffer
69 (yas/minor-mode 1)
70 (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
71 (should (string= (buffer-substring-no-properties (point-min) (point-max))
72 "brother from another mother!"))
73 (ert-simulate-command `(yas/mock-insert "bla"))
74 (should (string= (buffer-substring-no-properties (point-min) (point-max))
75 "brother from bla!"))))
76
77 (ert-deftest nested-placeholders-use-subfield ()
78 (with-temp-buffer
79 (yas/minor-mode 1)
80 (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
81 (ert-simulate-command '(yas/next-field-or-maybe-expand))
82 (ert-simulate-command `(yas/mock-insert "bla"))
83 (should (string= (buffer-substring-no-properties (point-min) (point-max))
84 "brother from another bla!"))))
85
86 ;; (ert-deftest in-snippet-undo ()
87 ;; (with-temp-buffer
88 ;; (yas/minor-mode 1)
89 ;; (yas/expand-snippet "brother from ${2:another ${3:mother}}!")
90 ;; (ert-simulate-command '(yas/next-field-or-maybe-expand))
91 ;; (ert-simulate-command `(yas/mock-insert "bla"))
92 ;; (ert-simulate-command '(undo))
93 ;; (should (string= (buffer-substring-no-properties (point-min) (point-max))
94 ;; "brother from another mother!"))))
95
96
97 ;;; Misc tests
98 ;;;
99
100 (ert-deftest protection-overlay-no-cheating ()
101 "Protection overlays at the very end of the buffer, are dealt by cheatingly inserting a newline!
102
103 TODO: correct this bug!"
104 :expected-result :failed
105 (with-temp-buffer
106 (yas/minor-mode 1)
107 (yas/expand-snippet "${2:brother} from another ${1:mother}")
108 (should (string= (buffer-substring-no-properties (point-min) (point-max))
109 "brother from another mother") ;; no newline should be here!
110 )))
111
112 ;;; Helpers
113 ;;;
114
115 (defun yas/mock-insert (string)
116 (interactive)
117 (do ((i 0 (1+ i)))
118 ((= i (length string)))
119 (insert (aref string i))))
120
121
122 (provide 'yasnippet-tests)
123 ;;; yasnippet-tests.el ends here