lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / testing / lisp / test-org-macro.el
blob28bc712e3cfc04019e5ca115e8519439ac5f12a4
1 ;;; test-org-macro.el --- Tests for org-macro.el
3 ;; Copyright (C) 2013, 2014, 2019 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 (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 "3"
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)))))
53 ;; Nested macros.
54 (should
55 (equal
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)
61 (buffer-string))))
62 ;; Error out when macro expansion is circular.
63 (should-error
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.
69 (should
70 (string-match
71 "success success\\'"
72 (org-test-with-temp-text
73 (format "#+MACRO: other-macro success
74 #+SETUPFILE: \"%sexamples/macro-templates.org\"
75 {{{included-macro}}} {{{other-macro}}}"
76 org-test-dir)
77 (org-macro-initialize-templates)
78 (org-macro-replace-all org-macro-templates)
79 (buffer-string))))
80 ;; Macro expansion ignores narrowing.
81 (should
82 (string-match
83 "expansion"
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.
91 (should
92 (string-match-p
93 "{{{macro}}}"
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)))))
99 (should
100 (string-match-p
101 "{{{macro}}}"
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))))))
108 (ert-deftest test-org-macro/property ()
109 "Test {{{property}}} macro."
110 ;; With only one argument, retrieve property from current headline.
111 ;; Otherwise, the second argument is a search option to get the
112 ;; property from another headline.
113 (should
114 (equal "1"
115 (org-test-with-temp-text
116 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A)}}}<point>"
117 (org-macro-initialize-templates)
118 (org-macro-replace-all org-macro-templates)
119 (buffer-substring-no-properties
120 (line-beginning-position) (line-end-position)))))
121 (should
122 (equal "1"
123 (org-test-with-temp-text
124 "* H\n:PROPERTIES:\n:A: 1\n:END:\n{{{property(A,)}}}<point>"
125 (org-macro-initialize-templates)
126 (org-macro-replace-all org-macro-templates)
127 (buffer-substring-no-properties
128 (line-beginning-position) (line-end-position)))))
129 (should
130 (equal
132 (org-test-with-temp-text
133 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*H1)}}}<point>"
134 (org-macro-initialize-templates)
135 (org-macro-replace-all org-macro-templates)
136 (buffer-substring-no-properties
137 (line-beginning-position) (line-end-position)))))
138 (should-error
139 (org-test-with-temp-text
140 "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n{{{property(A,*???)}}}<point>"
141 (org-macro-initialize-templates)
142 (org-macro-replace-all org-macro-templates))))
144 (ert-deftest test-org-macro/n ()
145 "Test {{{n}}} macro."
146 ;; Standard test with default counter.
147 (should
148 (equal "1 2"
149 (org-test-with-temp-text "{{{n}}} {{{n}}}"
150 (org-macro-initialize-templates)
151 (org-macro-replace-all org-macro-templates)
152 (buffer-substring-no-properties
153 (line-beginning-position) (line-end-position)))))
154 (should
155 (equal "1 2"
156 (org-test-with-temp-text "{{{n()}}} {{{n}}}"
157 (org-macro-initialize-templates)
158 (org-macro-replace-all org-macro-templates)
159 (buffer-substring-no-properties
160 (line-beginning-position) (line-end-position)))))
161 ;; Test alternative counters.
162 (should
163 (equal "1 1 1 2"
164 (org-test-with-temp-text "{{{n}}} {{{n(c1)}}} {{{n(c2)}}} {{{n(c1)}}}"
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 ;; Second argument set a counter to a given value. A non-numeric
170 ;; value resets the counter to 1.
171 (should
172 (equal "9 10"
173 (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c)}}}"
174 (org-macro-initialize-templates)
175 (org-macro-replace-all org-macro-templates)
176 (buffer-substring-no-properties
177 (line-beginning-position) (line-end-position)))))
178 (should
179 (equal "9 1"
180 (org-test-with-temp-text "{{{n(c,9)}}} {{{n(c,reset)}}}"
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 ;; Check that reset happens when the second argument is neither "-"
186 ;; nor a number.
187 (should
188 (equal "9 1 1 1"
189 (org-test-with-temp-text
190 (concat "{{{n(c,9)}}} {{{n(c,reiniciar)}}}"
191 " {{{n(c,réinitialiser)}}} {{{n(c,zurückstellen)}}}")
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 ;; Tolerate spaces in first argument.
197 (should
198 (equal "1 2 3 4"
199 (org-test-with-temp-text "{{{n(c)}}} {{{n(c )}}} {{{n( c)}}} {{{n( c )}}}"
200 (org-macro-initialize-templates)
201 (org-macro-replace-all org-macro-templates)
202 (buffer-substring-no-properties
203 (line-beginning-position) (line-end-position)))))
204 ;; Tolerate spaces when second argument is an integer.
205 (should
206 (equal "2 3 5 7"
207 (org-test-with-temp-text
208 (concat "{{{n(c,2)}}} {{{n(c, 3)}}}"
209 " {{{n(c,5 )}}} {{{n(c, 7 )}}}")
210 (org-macro-initialize-templates)
211 (org-macro-replace-all org-macro-templates)
212 (buffer-substring-no-properties
213 (line-beginning-position) (line-end-position)))))
214 ;; Tolerate spaces when second argument is the hold argument.
215 (should
216 (equal "7 7 8 8 9 9"
217 (org-test-with-temp-text
218 (concat "{{{n(,7)}}} {{{n(, -)}}}"
219 " {{{n}}} {{{n(,- )}}} {{{n}}} {{{n(, - )}}}")
220 (org-macro-initialize-templates)
221 (org-macro-replace-all org-macro-templates)
222 (buffer-substring-no-properties
223 (line-beginning-position) (line-end-position)))))
224 ;; Tolerate spaces when second argument is used to reset the counter.
225 (should
226 (equal "9 1 1 1 1"
227 (org-test-with-temp-text
228 (concat "{{{n(c,9)}}} {{{n(c,reset)}}} {{{n(c, reset)}}}"
229 " {{{n(c,reset )}}} {{{n(c, reset )}}}")
230 (org-macro-initialize-templates)
231 (org-macro-replace-all org-macro-templates)
232 (buffer-substring-no-properties
233 (line-beginning-position) (line-end-position)))))
234 ;; Second argument also applies to default counter.
235 (should
236 (equal "9 10 1"
237 (org-test-with-temp-text "{{{n(,9)}}} {{{n}}} {{{n(,reset)}}}"
238 (org-macro-initialize-templates)
239 (org-macro-replace-all org-macro-templates)
240 (buffer-substring-no-properties
241 (line-beginning-position) (line-end-position)))))
242 ;; An empty second argument is equivalent to no argument.
243 (should
244 (equal "2 3"
245 (org-test-with-temp-text "{{{n(c,2)}}} {{{n(c,)}}}"
246 (org-macro-initialize-templates)
247 (org-macro-replace-all org-macro-templates)
248 (buffer-substring-no-properties
249 (line-beginning-position) (line-end-position)))))
250 ;; Hold value at reset value of 1 if the counter hasn't yet started.
251 (should
252 (equal "1"
253 (org-test-with-temp-text "{{{n(,-)}}}"
254 (org-macro-initialize-templates)
255 (org-macro-replace-all org-macro-templates)
256 (buffer-substring-no-properties
257 (line-beginning-position) (line-end-position)))))
258 ;; Increment counter following a hold.
259 (should
260 (equal "1 1 2"
261 (org-test-with-temp-text "{{{n}}} {{{n(,-)}}} {{{n}}}"
262 (org-macro-initialize-templates)
263 (org-macro-replace-all org-macro-templates)
264 (buffer-substring-no-properties
265 (line-beginning-position) (line-end-position)))))
266 ;; Hold counter value following a counter value set.
267 (should
268 (equal "1 10 10"
269 (org-test-with-temp-text "{{{n}}} {{{n(,10)}}} {{{n(,-)}}}"
270 (org-macro-initialize-templates)
271 (org-macro-replace-all org-macro-templates)
272 (buffer-substring-no-properties
273 (line-beginning-position) (line-end-position)))))
274 ;; Hold counter value in a multiple-counter situation.
275 (should
276 (equal "1.1 1.2 1.3"
277 (org-test-with-temp-text
278 "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
279 (org-macro-initialize-templates)
280 (org-macro-replace-all org-macro-templates)
281 (buffer-substring-no-properties
282 (line-beginning-position) (line-end-position)))))
283 ;; Hold counter values on one or multiple counters at the same time.
284 (should
285 (equal "1.1 1.2 2.2 2.2"
286 (org-test-with-temp-text
287 (concat "{{{n}}}.{{{n(c)}}} {{{n(,-)}}}.{{{n(c)}}}"
288 " {{{n}}}.{{{n(c,-)}}} {{{n(,-)}}}.{{{n(c,-)}}}")
289 (org-macro-initialize-templates)
290 (org-macro-replace-all org-macro-templates)
291 (buffer-substring-no-properties
292 (line-beginning-position) (line-end-position))))))
294 (ert-deftest test-org-macro/keyword ()
295 "Test {{{keyword}}} macro."
296 ;; Replace macro with keyword's value.
297 (should
298 (equal
299 "value"
300 (org-test-with-temp-text
301 "#+keyword: value\n<point>{{{keyword(KEYWORD)}}}"
302 (org-macro-initialize-templates)
303 (org-macro-replace-all org-macro-templates)
304 (buffer-substring-no-properties
305 (line-beginning-position) (point-max))))))
307 (ert-deftest test-org-macro/escape-arguments ()
308 "Test `org-macro-escape-arguments' specifications."
309 ;; Regular tests.
310 (should (equal "a" (org-macro-escape-arguments "a")))
311 (should (equal "a,b" (org-macro-escape-arguments "a" "b")))
312 ;; Handle empty arguments.
313 (should (equal "a,,b" (org-macro-escape-arguments "a" "" "b")))
314 ;; Properly escape commas and backslashes preceding them.
315 (should (equal "a\\,b" (org-macro-escape-arguments "a,b")))
316 (should (equal "a\\\\,b" (org-macro-escape-arguments "a\\" "b")))
317 (should (equal "a\\\\\\,b" (org-macro-escape-arguments "a\\,b"))))
319 (ert-deftest test-org-macro/extract-arguments ()
320 "Test `org-macro-extract-arguments' specifications."
321 ;; Regular tests.
322 (should (equal '("a") (org-macro-extract-arguments "a")))
323 (should (equal '("a" "b") (org-macro-extract-arguments "a,b")))
324 ;; Handle empty arguments.
325 (should (equal '("a" "" "b") (org-macro-extract-arguments "a,,b")))
326 ;; Handle escaped commas and backslashes.
327 (should (equal '("a,b") (org-macro-extract-arguments "a\\,b")))
328 (should (equal '("a\\" "b") (org-macro-extract-arguments "a\\\\,b")))
329 (should (equal '("a\\,b") (org-macro-extract-arguments "a\\\\\\,b"))))
332 (provide 'test-org-macro)
333 ;;; test-org-macro.el ends here