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/>.
25 (ert-deftest test-org
/macro-replace-all
()
26 "Test `org-macro-replace-all' specifications."
31 (org-test-with-temp-text "#+MACRO: A B\n1 {{{A}}} 3"
32 (org-macro-initialize-templates)
33 (org-macro-replace-all org-macro-templates
)
35 ;; Macro with arguments.
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
)
47 (org-test-with-temp-text
48 "#+MACRO: add (eval (+ (string-to-number $1) (string-to-number $2)))
49 <point>{{{add(1,2)}}}"
50 (org-macro-initialize-templates)
51 (org-macro-replace-all org-macro-templates
)
52 (buffer-substring-no-properties (point) (line-end-position)))))
56 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\ninner outer"
57 (org-test-with-temp-text
58 "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
59 (org-macro-initialize-templates)
60 (org-macro-replace-all org-macro-templates
)
62 ;; Error out when macro expansion is circular.
64 (org-test-with-temp-text
65 "#+MACRO: mac1 {{{mac2}}}\n#+MACRO: mac2 {{{mac1}}}\n{{{mac1}}}"
66 (org-macro-initialize-templates)
67 (org-macro-replace-all org-macro-templates
)))
68 ;; Macros in setup file.
72 (org-test-with-temp-text
73 (format "#+MACRO: other-macro success
74 #+SETUPFILE: \"%sexamples/macro-templates.org\"
75 {{{included-macro}}} {{{other-macro}}}"
77 (org-macro-initialize-templates)
78 (org-macro-replace-all org-macro-templates
)
80 ;; Macro expansion ignores narrowing.
84 (org-test-with-temp-text
85 "#+MACRO: macro expansion\n{{{macro}}}\n<point>Contents"
86 (narrow-to-region (point) (point-max))
87 (org-macro-initialize-templates)
88 (org-macro-replace-all org-macro-templates
)
89 (org-with-wide-buffer (buffer-string)))))
90 ;; Macros in a commented tree are not expanded.
94 (org-test-with-temp-text
95 "#+MACRO: macro expansion\n* COMMENT H\n<point>{{{macro}}}"
96 (org-macro-initialize-templates)
97 (org-macro-replace-all org-macro-templates
)
98 (org-with-wide-buffer (buffer-string)))))
102 (org-test-with-temp-text
103 "#+MACRO: macro expansion\n* COMMENT H1\n** H2\n<point>{{{macro}}}"
104 (org-macro-initialize-templates)
105 (org-macro-replace-all org-macro-templates
)
106 (org-with-wide-buffer (buffer-string)))))
107 ;; User-defined macros take precedence over built-in macros.
111 (org-test-with-temp-text
112 "#+MACRO: title foo\n#+TITLE: bar\n<point>{{{title}}}"
113 (org-macro-initialize-templates)
114 (org-macro-replace-all org-macro-templates
)
115 (goto-char (point-max))
116 (buffer-substring-no-properties (line-beginning-position)
117 (line-end-position))))))
119 (ert-deftest test-org-macro
/property
()
120 "Test {{{property}}} macro."
121 ;; With only one argument, retrieve property from current headline.
122 ;; Otherwise, the second argument is a search option to get the
123 ;; property from another headline.
126 (org-test-with-temp-text
127 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
128 (org-macro-initialize-templates)
129 (org-macro-replace-all org-macro-templates
)
130 (buffer-substring-no-properties
131 (line-beginning-position) (line-end-position)))))
134 (org-test-with-temp-text
135 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
136 (org-macro-initialize-templates)
137 (org-macro-replace-all org-macro-templates
)
138 (buffer-substring-no-properties
139 (line-beginning-position) (line-end-position)))))
143 (org-test-with-temp-text
144 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
145 (org-macro-initialize-templates)
146 (org-macro-replace-all org-macro-templates
)
147 (buffer-substring-no-properties
148 (line-beginning-position) (line-end-position)))))
150 (org-test-with-temp-text
151 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
152 (org-macro-initialize-templates)
153 (org-macro-replace-all org-macro-templates
))))
155 (ert-deftest test-org-macro
/n
()
156 "Test {{{n}}} macro."
157 ;; Standard test with default counter.
160 (org-test-with-temp-text "{{{n}}} {{{n}}}"
161 (org-macro-initialize-templates)
162 (org-macro-replace-all org-macro-templates
)
163 (buffer-substring-no-properties
164 (line-beginning-position) (line-end-position)))))
167 (org-test-with-temp-text "{{{n()}}} {{{n}}}"
168 (org-macro-initialize-templates)
169 (org-macro-replace-all org-macro-templates
)
170 (buffer-substring-no-properties
171 (line-beginning-position) (line-end-position)))))
172 ;; Test alternative counters.
175 (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
176 (org-macro-initialize-templates)
177 (org-macro-replace-all org-macro-templates
)
178 (buffer-substring-no-properties
179 (line-beginning-position) (line-end-position)))))
180 ;; Second argument set a counter to a given value. A non-numeric
181 ;; value resets the counter to 1.
184 (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
185 (org-macro-initialize-templates)
186 (org-macro-replace-all org-macro-templates
)
187 (buffer-substring-no-properties
188 (line-beginning-position) (line-end-position)))))
191 (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
192 (org-macro-initialize-templates)
193 (org-macro-replace-all org-macro-templates
)
194 (buffer-substring-no-properties
195 (line-beginning-position) (line-end-position)))))
196 ;; Check that reset happens when the second argument is neither "-"
200 (org-test-with-temp-text
201 (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
202 " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
203 (org-macro-initialize-templates)
204 (org-macro-replace-all org-macro-templates
)
205 (buffer-substring-no-properties
206 (line-beginning-position) (line-end-position)))))
207 ;; Tolerate spaces in first argument.
210 (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
211 (org-macro-initialize-templates)
212 (org-macro-replace-all org-macro-templates
)
213 (buffer-substring-no-properties
214 (line-beginning-position) (line-end-position)))))
215 ;; Tolerate spaces when second argument is an integer.
218 (org-test-with-temp-text
219 (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
220 " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
221 (org-macro-initialize-templates)
222 (org-macro-replace-all org-macro-templates
)
223 (buffer-substring-no-properties
224 (line-beginning-position) (line-end-position)))))
225 ;; Tolerate spaces when second argument is the hold argument.
228 (org-test-with-temp-text
229 (concat "{{{n(,7)}}} {{{n(, -)}}}"
230 " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
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 used to reset the counter.
238 (org-test-with-temp-text
239 (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
240 " {{{n(c,reset )}}} {{{n(c, reset )}}}")
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 ;; Second argument also applies to default counter.
248 (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
249 (org-macro-initialize-templates)
250 (org-macro-replace-all org-macro-templates
)
251 (buffer-substring-no-properties
252 (line-beginning-position) (line-end-position)))))
253 ;; An empty second argument is equivalent to no argument.
256 (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
257 (org-macro-initialize-templates)
258 (org-macro-replace-all org-macro-templates
)
259 (buffer-substring-no-properties
260 (line-beginning-position) (line-end-position)))))
261 ;; Hold value at reset value of 1 if the counter hasn't yet started.
264 (org-test-with-temp-text "{{{n(,-)}}}"
265 (org-macro-initialize-templates)
266 (org-macro-replace-all org-macro-templates
)
267 (buffer-substring-no-properties
268 (line-beginning-position) (line-end-position)))))
269 ;; Increment counter following a hold.
272 (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
273 (org-macro-initialize-templates)
274 (org-macro-replace-all org-macro-templates
)
275 (buffer-substring-no-properties
276 (line-beginning-position) (line-end-position)))))
277 ;; Hold counter value following a counter value set.
280 (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
281 (org-macro-initialize-templates)
282 (org-macro-replace-all org-macro-templates
)
283 (buffer-substring-no-properties
284 (line-beginning-position) (line-end-position)))))
285 ;; Hold counter value in a multiple-counter situation.
288 (org-test-with-temp-text
289 "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
290 (org-macro-initialize-templates)
291 (org-macro-replace-all org-macro-templates
)
292 (buffer-substring-no-properties
293 (line-beginning-position) (line-end-position)))))
294 ;; Hold counter values on one or multiple counters at the same time.
296 (equal "1.1 1.2 2.2 2.2"
297 (org-test-with-temp-text
298 (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
299 " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
300 (org-macro-initialize-templates)
301 (org-macro-replace-all org-macro-templates
)
302 (buffer-substring-no-properties
303 (line-beginning-position) (line-end-position))))))
305 (ert-deftest test-org-macro
/keyword
()
306 "Test {{{keyword}}} macro."
307 ;; Replace macro with keyword's value.
311 (org-test-with-temp-text
312 "#+keyword: value\n<point>{{{keyword(KEYWORD)}}}"
313 (org-macro-initialize-templates)
314 (org-macro-replace-all org-macro-templates
)
315 (buffer-substring-no-properties
316 (line-beginning-position) (point-max)))))
317 ;; Replace macro with keyword's value.
321 (org-test-with-temp-text
322 "#+keyword: value\n#+keyword: value2\n<point>{{{keyword(KEYWORD)}}}"
323 (org-macro-initialize-templates)
324 (org-macro-replace-all org-macro-templates
)
325 (buffer-substring-no-properties
326 (line-beginning-position) (point-max))))))
328 (ert-deftest test-org-macro
/escape-arguments
()
329 "Test `org-macro-escape-arguments' specifications."
331 (should (equal "a" (org-macro-escape-arguments "a")))
332 (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
333 ;; Handle empty arguments.
334 (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
335 ;; Properly escape commas and backslashes preceding them.
336 (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
337 (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
338 (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
340 (ert-deftest test-org-macro
/extract-arguments
()
341 "Test `org-macro-extract-arguments' specifications."
343 (should (equal '("a") (org-macro-extract-arguments "a")))
344 (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
345 ;; Handle empty arguments.
346 (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
347 ;; Handle escaped commas and backslashes.
348 (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
349 (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
350 (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
353 (provide 'test-org-macro
)
354 ;;; test-org-macro.el ends here