Merge branch 'maint'
[org-mode.git] / testing / lisp / test-org-macro.el
blobff542f887393e560f9c2f40c701a4c673519a6b7
1 ;;; test-org-macro.el --- Tests for org-macro.el
3 ;; Copyright (C) 2013, 2014 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
7 ;; This program is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Code:
23 ;;; Macros
25 (ert-deftest test-org/macro-replace-all ()
26 "Test `org-macro-replace-all' specifications."
27 ;; Standard test.
28 (should
29 (equal
30 "#+MACRO: A B\n1 B 3"
31 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
32 (progn (org-macro-initialize-templates)
33 (org-macro-replace-all org-macro-templates)
34 (buffer-string)))))
35 ;; Macro with arguments.
36 (should
37 (equal
38 "#+MACRO: macro $1 $2\nsome text"
39 (org-test-with-temp-text "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
40 (progn (org-macro-initialize-templates)
41 (org-macro-replace-all org-macro-templates)
42 (buffer-string)))))
43 ;; Macro with "eval".
44 (should
45 (equal
46 "#+MACRO: add (eval (+ $1 $2))\n3"
47 (org-test-with-temp-text "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
48 (progn (org-macro-initialize-templates)
49 (org-macro-replace-all org-macro-templates)
50 (buffer-string)))))
51 ;; Nested macros.
52 (should
53 (equal
54 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
55 (org-test-with-temp-text
56 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
57 (progn (org-macro-initialize-templates)
58 (org-macro-replace-all org-macro-templates)
59 (buffer-string)))))
60 ;; Error out when macro expansion is circular.
61 (should-error
62 (org-test-with-temp-text
63 "#+MACRO: mac1 {{{mac2}}}\n#+MACRO: mac2 {{{mac1}}}\n{{{mac1}}}"
64 (org-macro-initialize-templates)
65 (org-macro-replace-all org-macro-templates)))
66 ;; Macros in setup file.
67 (should
68 (string-match
69 "success success\\'"
70 (org-test-with-temp-text
71 (format "#+MACRO: other-macro success
72 #+SETUPFILE: \"%sexamples/macro-templates.org\"
73 {{{included-macro}}} {{{other-macro}}}"
74 org-test-dir)
75 (org-macro-initialize-templates)
76 (org-macro-replace-all org-macro-templates)
77 (buffer-string))))
78 ;; Test special "property" macro. With only one argument, retrieve
79 ;; property from current headline. Otherwise, the second argument
80 ;; is a search option to get the property from another headline.
81 (should
82 (equal "1"
83 (org-test-with-temp-text
84 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
85 (org-macro-initialize-templates)
86 (org-macro-replace-all org-macro-templates)
87 (buffer-substring-no-properties
88 (line-beginning-position) (line-end-position)))))
89 (should
90 (equal "1"
91 (org-test-with-temp-text
92 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
93 (org-macro-initialize-templates)
94 (org-macro-replace-all org-macro-templates)
95 (buffer-substring-no-properties
96 (line-beginning-position) (line-end-position)))))
97 (should
98 (equal
99 "1"
100 (org-test-with-temp-text
101 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
102 (org-macro-initialize-templates)
103 (org-macro-replace-all org-macro-templates)
104 (buffer-substring-no-properties
105 (line-beginning-position) (line-end-position)))))
106 (should-error
107 (org-test-with-temp-text
108 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
109 (org-macro-initialize-templates)
110 (org-macro-replace-all org-macro-templates)))
111 ;; Macro expansion ignores narrowing.
112 (should
113 (string-match
114 "expansion"
115 (org-test-with-temp-text
116 "#+MACRO: macro expansion\n{{{macro}}}\n<point>Contents"
117 (narrow-to-region (point) (point-max))
118 (org-macro-initialize-templates)
119 (org-macro-replace-all org-macro-templates)
120 (org-with-wide-buffer (buffer-string)))))
121 ;; Macros in a commented tree are not expanded.
122 (should
123 (string-match-p
124 "{{{macro}}}"
125 (org-test-with-temp-text
126 "#+MACRO: macro expansion\n* COMMENT H\n<point>{{{macro}}}"
127 (org-macro-initialize-templates)
128 (org-macro-replace-all org-macro-templates)
129 (org-with-wide-buffer (buffer-string)))))
130 (should
131 (string-match-p
132 "{{{macro}}}"
133 (org-test-with-temp-text
134 "#+MACRO: macro expansion\n* COMMENT H1\n** H2\n<point>{{{macro}}}"
135 (org-macro-initialize-templates)
136 (org-macro-replace-all org-macro-templates)
137 (org-with-wide-buffer (buffer-string))))))
139 (ert-deftest test-org-macro/property ()
140 "Test {{{property}}} macro."
141 ;; With only one argument, retrieve property from current headline.
142 ;; Otherwise, the second argument is a search option to get the
143 ;; property from another headline.
144 (should
145 (equal "1"
146 (org-test-with-temp-text
147 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
148 (org-macro-initialize-templates)
149 (org-macro-replace-all org-macro-templates)
150 (buffer-substring-no-properties
151 (line-beginning-position) (line-end-position)))))
152 (should
153 (equal "1"
154 (org-test-with-temp-text
155 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
156 (org-macro-initialize-templates)
157 (org-macro-replace-all org-macro-templates)
158 (buffer-substring-no-properties
159 (line-beginning-position) (line-end-position)))))
160 (should
161 (equal
163 (org-test-with-temp-text
164 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
165 (org-macro-initialize-templates)
166 (org-macro-replace-all org-macro-templates)
167 (buffer-substring-no-properties
168 (line-beginning-position) (line-end-position)))))
169 (should-error
170 (org-test-with-temp-text
171 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
172 (org-macro-initialize-templates)
173 (org-macro-replace-all org-macro-templates))))
175 (ert-deftest test-org-macro/n ()
176 "Test {{{n}}} macro."
177 ;; Standard test with default counter.
178 (should
179 (equal "1 2"
180 (org-test-with-temp-text "{{{n}}} {{{n}}}"
181 (org-macro-initialize-templates)
182 (org-macro-replace-all org-macro-templates)
183 (buffer-substring-no-properties
184 (line-beginning-position) (line-end-position)))))
185 (should
186 (equal "1 2"
187 (org-test-with-temp-text "{{{n()}}} {{{n}}}"
188 (org-macro-initialize-templates)
189 (org-macro-replace-all org-macro-templates)
190 (buffer-substring-no-properties
191 (line-beginning-position) (line-end-position)))))
192 ;; Test alternative counters.
193 (should
194 (equal "1 1 1 2"
195 (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
196 (org-macro-initialize-templates)
197 (org-macro-replace-all org-macro-templates)
198 (buffer-substring-no-properties
199 (line-beginning-position) (line-end-position)))))
200 ;; Second argument set a counter to a given value. A non-numeric
201 ;; value resets the counter to 1.
202 (should
203 (equal "9 10"
204 (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
205 (org-macro-initialize-templates)
206 (org-macro-replace-all org-macro-templates)
207 (buffer-substring-no-properties
208 (line-beginning-position) (line-end-position)))))
209 (should
210 (equal "9 1"
211 (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
212 (org-macro-initialize-templates)
213 (org-macro-replace-all org-macro-templates)
214 (buffer-substring-no-properties
215 (line-beginning-position) (line-end-position)))))
216 ;; Check that reset happens when the second argument is neither "-"
217 ;; nor a number.
218 (should
219 (equal "9 1 1 1"
220 (org-test-with-temp-text
221 (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
222 " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
223 (org-macro-initialize-templates)
224 (org-macro-replace-all org-macro-templates)
225 (buffer-substring-no-properties
226 (line-beginning-position) (line-end-position)))))
227 ;; Tolerate spaces in first argument.
228 (should
229 (equal "1 2 3 4"
230 (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
231 (org-macro-initialize-templates)
232 (org-macro-replace-all org-macro-templates)
233 (buffer-substring-no-properties
234 (line-beginning-position) (line-end-position)))))
235 ;; Tolerate spaces when second argument is an integer.
236 (should
237 (equal "2 3 5 7"
238 (org-test-with-temp-text
239 (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
240 " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
241 (org-macro-initialize-templates)
242 (org-macro-replace-all org-macro-templates)
243 (buffer-substring-no-properties
244 (line-beginning-position) (line-end-position)))))
245 ;; Tolerate spaces when second argument is the hold argument.
246 (should
247 (equal "7 7 8 8 9 9"
248 (org-test-with-temp-text
249 (concat "{{{n(,7)}}} {{{n(, -)}}}"
250 " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
251 (org-macro-initialize-templates)
252 (org-macro-replace-all org-macro-templates)
253 (buffer-substring-no-properties
254 (line-beginning-position) (line-end-position)))))
255 ;; Tolerate spaces when second argument is used to reset the counter.
256 (should
257 (equal "9 1 1 1 1"
258 (org-test-with-temp-text
259 (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
260 " {{{n(c,reset )}}} {{{n(c, reset )}}}")
261 (org-macro-initialize-templates)
262 (org-macro-replace-all org-macro-templates)
263 (buffer-substring-no-properties
264 (line-beginning-position) (line-end-position)))))
265 ;; Second argument also applies to default counter.
266 (should
267 (equal "9 10 1"
268 (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
269 (org-macro-initialize-templates)
270 (org-macro-replace-all org-macro-templates)
271 (buffer-substring-no-properties
272 (line-beginning-position) (line-end-position)))))
273 ;; An empty second argument is equivalent to no argument.
274 (should
275 (equal "2 3"
276 (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
277 (org-macro-initialize-templates)
278 (org-macro-replace-all org-macro-templates)
279 (buffer-substring-no-properties
280 (line-beginning-position) (line-end-position)))))
281 ;; Hold value at reset value of 1 if the counter hasn't yet started.
282 (should
283 (equal "1"
284 (org-test-with-temp-text "{{{n(,-)}}}"
285 (org-macro-initialize-templates)
286 (org-macro-replace-all org-macro-templates)
287 (buffer-substring-no-properties
288 (line-beginning-position) (line-end-position)))))
289 ;; Increment counter following a hold.
290 (should
291 (equal "1 1 2"
292 (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
293 (org-macro-initialize-templates)
294 (org-macro-replace-all org-macro-templates)
295 (buffer-substring-no-properties
296 (line-beginning-position) (line-end-position)))))
297 ;; Hold counter value following a counter value set.
298 (should
299 (equal "1 10 10"
300 (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
301 (org-macro-initialize-templates)
302 (org-macro-replace-all org-macro-templates)
303 (buffer-substring-no-properties
304 (line-beginning-position) (line-end-position)))))
305 ;; Hold counter value in a multiple-counter situation.
306 (should
307 (equal "1.1 1.2 1.3"
308 (org-test-with-temp-text
309 "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
310 (org-macro-initialize-templates)
311 (org-macro-replace-all org-macro-templates)
312 (buffer-substring-no-properties
313 (line-beginning-position) (line-end-position)))))
314 ;; Hold counter values on one or multiple counters at the same time.
315 (should
316 (equal "1.1 1.2 2.2 2.2"
317 (org-test-with-temp-text
318 (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
319 " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
320 (org-macro-initialize-templates)
321 (org-macro-replace-all org-macro-templates)
322 (buffer-substring-no-properties
323 (line-beginning-position) (line-end-position))))))
325 (ert-deftest test-org-macro/keyword ()
326 "Test {{{keyword}}} macro."
327 ;; Replace macro with keyword's value.
328 (should
329 (equal
330 "value"
331 (org-test-with-temp-text
332 "#+keyword: value\n<point>{{{keyword(KEYWORD)}}}"
333 (org-macro-initialize-templates)
334 (org-macro-replace-all org-macro-templates)
335 (buffer-substring-no-properties
336 (line-beginning-position) (point-max)))))
337 ;; Replace macro with keyword's value.
338 (should
339 (equal
340 "value value2"
341 (org-test-with-temp-text
342 "#+keyword: value\n#+keyword: value2\n<point>{{{keyword(KEYWORD)}}}"
343 (org-macro-initialize-templates)
344 (org-macro-replace-all org-macro-templates)
345 (buffer-substring-no-properties
346 (line-beginning-position) (point-max))))))
348 (ert-deftest test-org-macro/escape-arguments ()
349 "Test `org-macro-escape-arguments' specifications."
350 ;; Regular tests.
351 (should (equal "a" (org-macro-escape-arguments "a")))
352 (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
353 ;; Handle empty arguments.
354 (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
355 ;; Properly escape commas and backslashes preceding them.
356 (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
357 (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
358 (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
360 (ert-deftest test-org-macro/extract-arguments ()
361 "Test `org-macro-extract-arguments' specifications."
362 ;; Regular tests.
363 (should (equal '("a") (org-macro-extract-arguments "a")))
364 (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
365 ;; Handle empty arguments.
366 (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
367 ;; Handle escaped commas and backslashes.
368 (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
369 (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
370 (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
373 (provide 'test-org-macro)
374 ;;; test-org-macro.el ends here