Require one or more spaces (+) between keyword and headline
[org-mode.git] / testing / lisp / test-org-export.el
blobf34cc7b772984bff44b8d74e3bca22e04950b597
1 ;;; test-org-export.el --- Tests for org-export.el
3 ;; Copyright (C) 2012 Nicolas Goaziou
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
7 ;; Released under the GNU General Public License version 3
8 ;; see: http://www.gnu.org/licenses/gpl-3.0.html
10 ;;;; Comments
14 ;;; Code:
16 (unless (featurep 'org-export)
17 (signal 'missing-test-dependency "org-export"))
21 ;;; Tests
23 (defmacro org-test-with-backend (backend &rest body)
24 "Execute body with an export back-end defined.
26 BACKEND is the name, as a string, of the back-end. BODY is the
27 body to execute. The defined back-end simply returns parsed data
28 as Org syntax."
29 (declare (debug (form body)) (indent 1))
30 `(flet ,(let (transcoders)
31 (dolist (type (append org-element-all-elements
32 org-element-all-objects)
33 transcoders)
34 (push `(,(intern (format "org-%s-%s" backend type))
35 (obj contents info)
36 (,(intern (format "org-element-%s-interpreter" type))
37 obj contents))
38 transcoders)))
39 ,@body))
41 (ert-deftest test-org-export/parse-option-keyword ()
42 "Test reading all standard #+OPTIONS: items."
43 (should
44 (equal
45 (org-export-parse-option-keyword
46 "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
47 *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
48 '(:headline-levels
49 1 :preserve-breaks t :section-numbers t :time-stamp-file t
50 :with-archived-trees t :with-author t :with-creator t :with-drawers t
51 :with-email t :with-emphasize t :with-entities t :with-fixed-width t
52 :with-footnotes t :with-priority t :with-special-strings t
53 :with-sub-superscript t :with-toc t :with-tables t :with-tags t
54 :with-tasks t :with-timestamps t :with-todo-keywords t)))
55 ;; Test some special values.
56 (should
57 (equal
58 (org-export-parse-option-keyword
59 "arch:headline creator:comment d:(\"TEST\")
60 ^:{} toc:1 tags:not-in-toc tasks:todo num:2")
61 '( :section-numbers
63 :with-archived-trees headline :with-creator comment
64 :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
65 :with-tags not-in-toc :with-tasks todo))))
67 (ert-deftest test-org-export/get-inbuffer-options ()
68 "Test reading all standard export keywords."
69 (should
70 (equal
71 (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
72 #+CREATOR: Idem
73 #+DATE: Today
74 #+DESCRIPTION: Testing
75 #+DESCRIPTION: with two lines
76 #+EMAIL: some@email.org
77 #+EXPORT_EXCLUDE_TAGS: noexport invisible
78 #+KEYWORDS: test
79 #+LANGUAGE: en
80 #+EXPORT_SELECT_TAGS: export
81 #+TITLE: Some title
82 #+TITLE: with spaces"
83 (org-export-get-inbuffer-options))
84 '(:author
85 "Me, Myself and I" :creator "Idem" :date "Today"
86 :description "Testing\nwith two lines" :email "some@email.org"
87 :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
88 :select-tags ("export") :title "Some title with spaces"))))
90 (ert-deftest test-org-export/define-macro ()
91 "Try defining various Org macro using in-buffer #+MACRO: keyword."
92 ;; Parsed macro.
93 (should (equal (org-test-with-temp-text "#+MACRO: one 1"
94 (org-export-get-inbuffer-options))
95 '(:macro-one ("1"))))
96 ;; Evaled macro.
97 (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
98 (org-export-get-inbuffer-options))
99 '(:macro-two "(eval (+ 1 1))")))
100 ;; Incomplete macro.
101 (should-not (org-test-with-temp-text "#+MACRO: three"
102 (org-export-get-inbuffer-options)))
103 ;; Macro with newline character.
104 (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
105 (org-export-get-inbuffer-options))
106 '(:macro-four ("a\nb"))))
107 ;; Macro with protected newline character.
108 (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
109 (org-export-get-inbuffer-options))
110 '(:macro-five ("a\\nb"))))
111 ;; Recursive macro.
112 (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
113 (should
114 (equal
115 (org-export-get-inbuffer-options)
116 '(:macro-six
117 ("6")
118 :macro-seven
119 ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
120 :post-blank 0))))))))
122 (ert-deftest test-org-export/handle-options ()
123 "Test if export options have an impact on output."
124 ;; Test exclude tags.
125 (org-test-with-temp-text "* Head1 :noexport:"
126 (org-test-with-backend "test"
127 (should
128 (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
129 ""))))
130 ;; Test include tags.
131 (org-test-with-temp-text "
132 * Head1
133 ** Sub-Head1.1 :export:
134 *** Sub-Head1.1.1
135 * Head2"
136 (org-test-with-backend "test"
137 (should
138 (string-match
139 "\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
140 (org-export-as 'test nil nil nil '(:select-tags ("export")))))))
141 ;; Test mixing include tags and exclude tags.
142 (org-test-with-temp-text "
143 * Head1 :export:
144 ** Sub-Head1 :noexport:
145 ** Sub-Head2
146 * Head2 :noexport:
147 ** Sub-Head1 :export:"
148 (org-test-with-backend "test"
149 (should
150 (string-match
151 "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
152 (org-export-as
153 'test nil nil nil
154 '(:select-tags ("export") :exclude-tags ("noexport")))))))
155 ;; Ignore tasks.
156 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
157 (org-test-with-temp-text "* TODO Head1"
158 (org-test-with-backend "test"
159 (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
160 "")))))
161 (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
162 (org-test-with-temp-text "* TODO Head1"
163 (org-test-with-backend "test"
164 (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
165 "* TODO Head1\n")))))
166 ;; Archived tree.
167 (org-test-with-temp-text "* Head1 :archive:"
168 (let ((org-archive-tag "archive"))
169 (org-test-with-backend "test"
170 (should
171 (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
172 "")))))
173 (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
174 (let ((org-archive-tag "archive"))
175 (org-test-with-backend "test"
176 (should
177 (string-match
178 "\\* Head1[ \t]+:archive:"
179 (org-export-as 'test nil nil nil
180 '(:with-archived-trees headline)))))))
181 (org-test-with-temp-text "* Head1 :archive:"
182 (let ((org-archive-tag "archive"))
183 (org-test-with-backend "test"
184 (should
185 (string-match
186 "\\`\\* Head1[ \t]+:archive:\n\\'"
187 (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
188 ;; Drawers.
189 (let ((org-drawers '("TEST")))
190 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
191 (org-test-with-backend "test"
192 (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
193 "")))))
194 (let ((org-drawers '("TEST")))
195 (org-test-with-temp-text ":TEST:\ncontents\n:END:"
196 (org-test-with-backend "test"
197 (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
198 ":TEST:\ncontents\n:END:\n"))))))
200 (ert-deftest test-org-export/comment-tree ()
201 "Test if export process ignores commented trees."
202 (let ((org-comment-string "COMMENT"))
203 (org-test-with-temp-text "* COMMENT Head1"
204 (org-test-with-backend "test"
205 (should (equal (org-export-as 'test) ""))))))
207 (ert-deftest test-org-export/export-scope ()
208 "Test all export scopes."
209 (org-test-with-temp-text "
210 * Head1
211 ** Head2
212 text
213 *** Head3"
214 (org-test-with-backend "test"
215 ;; Subtree.
216 (forward-line 3)
217 (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
218 ;; Visible.
219 (goto-char (point-min))
220 (forward-line)
221 (org-cycle)
222 (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
223 ;; Body only.
224 (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
225 (should (equal (org-export-as 'test nil nil 'body-only)
226 "* Head1\n** Head2\ntext\n*** Head3\n"))
227 (should (equal (org-export-as 'test)
228 "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
229 ;; Region.
230 (goto-char (point-min))
231 (forward-line 3)
232 (transient-mark-mode 1)
233 (push-mark (point) t t)
234 (goto-char (point-at-eol))
235 (should (equal (org-export-as 'test) "text\n")))))
237 (ert-deftest test-org-export/export-snippet ()
238 "Test export snippets transcoding."
239 (org-test-with-temp-text "@test{A}@t{B}"
240 (org-test-with-backend "test"
241 (flet ((org-test-export-snippet
242 (snippet contents info)
243 (when (eq (org-export-snippet-backend snippet) 'test)
244 (org-element-property :value snippet))))
245 (let ((org-export-snippet-translation-alist nil))
246 (should (equal (org-export-as 'test) "A\n")))
247 (let ((org-export-snippet-translation-alist '(("t" . "test"))))
248 (should (equal (org-export-as 'test) "AB\n")))))))
250 (ert-deftest test-org-export/expand-include ()
251 "Test file inclusion in an Org buffer."
252 ;; Full insertion with recursive inclusion.
253 (org-test-with-temp-text
254 (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
255 (org-export-expand-include-keyword)
256 (should (equal (buffer-string)
257 "Small Org file with an include keyword.
259 #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
261 Success!
263 * Heading
264 body\n")))
265 ;; Localized insertion.
266 (org-test-with-temp-text
267 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
268 org-test-dir)
269 (org-export-expand-include-keyword)
270 (should (equal (buffer-string)
271 "Small Org file with an include keyword.\n")))
272 ;; Insertion with constraints on headlines level.
273 (org-test-with-temp-text
274 (format
275 "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
276 org-test-dir)
277 (org-export-expand-include-keyword)
278 (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
279 ;; Inclusion within an example block.
280 (org-test-with-temp-text
281 (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
282 org-test-dir)
283 (org-export-expand-include-keyword)
284 (should
285 (equal
286 (buffer-string)
287 "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
288 ;; Inclusion within a src-block.
289 (org-test-with-temp-text
290 (format
291 "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
292 org-test-dir)
293 (org-export-expand-include-keyword)
294 (should (equal (buffer-string)
295 "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
297 (ert-deftest test-org-export/user-ignore-list ()
298 "Test if `:ignore-list' accepts user input."
299 (org-test-with-backend "test"
300 (flet ((skip-note-head
301 (data backend info)
302 ;; Ignore headlines with the word "note" in their title.
303 (org-element-map
304 data 'headline
305 (lambda (headline)
306 (when (string-match "\\<note\\>"
307 (org-element-property :raw-value headline))
308 (org-export-ignore-element headline info)))
309 info)
310 data))
311 ;; Install function in parse tree filters.
312 (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
313 (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
314 (should (equal (org-export-as 'test) "* Head1\n")))))))
318 ;; Footnotes
320 (ert-deftest test-org-export/footnotes ()
321 "Test footnotes specifications."
322 (let ((org-footnote-section nil))
323 ;; 1. Read every type of footnote.
324 (org-test-with-temp-text
325 "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
326 (let* ((tree (org-element-parse-buffer))
327 (info (org-combine-plists
328 (org-export-initial-options) '(:with-footnotes t))))
329 (setq info (org-combine-plists
330 info (org-export-collect-tree-properties tree info 'test)))
331 (should
332 (equal
333 '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
334 (org-element-map
335 tree 'footnote-reference
336 (lambda (ref)
337 (let ((def (org-export-get-footnote-definition ref info)))
338 (cons (org-export-get-footnote-number ref info)
339 (if (eq (org-element-property :type ref) 'inline) (car def)
340 (car (org-element-contents
341 (car (org-element-contents def))))))))
342 info)))))
343 ;; 2. Test nested footnotes order.
344 (org-test-with-temp-text
345 "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
346 (let* ((tree (org-element-parse-buffer))
347 (info (org-combine-plists
348 (org-export-initial-options) '(:with-footnotes t))))
349 (setq info (org-combine-plists
350 info (org-export-collect-tree-properties tree info 'test)))
351 (should
352 (equal
353 '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
354 (org-element-map
355 tree 'footnote-reference
356 (lambda (ref)
357 (when (org-export-footnote-first-reference-p ref info)
358 (cons (org-export-get-footnote-number ref info)
359 (org-element-property :label ref))))
360 info)))))
361 ;; 3. Test nested footnote in invisible definitions.
362 (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
363 ;; Hide definitions.
364 (narrow-to-region (point) (point-at-eol))
365 (let* ((tree (org-element-parse-buffer))
366 (info (org-combine-plists
367 (org-export-initial-options) '(:with-footnotes t))))
368 (setq info (org-combine-plists
369 info (org-export-collect-tree-properties tree info 'test)))
370 ;; Both footnotes should be seen.
371 (should
372 (= (length (org-export-collect-footnote-definitions tree info)) 2))))
373 ;; 4. Test footnotes definitions collection.
374 (org-test-with-temp-text "Text[fn:1:A[fn:2]] [fn:3].
376 \[fn:2] B [fn:3] [fn::D].
378 \[fn:3] C."
379 (let ((tree (org-element-parse-buffer))
380 (info (org-combine-plists
381 (org-export-initial-options) '(:with-footnotes t))))
382 (setq info (org-combine-plists
383 info (org-export-collect-tree-properties tree info 'test)))
384 (should (= (length (org-export-collect-footnote-definitions tree info))
385 4))))))
389 ;;; Links
391 (ert-deftest test-org-export/fuzzy-links ()
392 "Test fuzz link export specifications."
393 ;; 1. Links to invisible (keyword) targets should be ignored.
394 (org-test-with-temp-text
395 "Paragraph.\n#+TARGET: Test\n[[Test]]"
396 (let* ((tree (org-element-parse-buffer))
397 (info (org-combine-plists (org-export-initial-options))))
398 (setq info (org-combine-plists
399 info (org-export-collect-tree-properties tree info 'test)))
400 (should-not
401 (org-element-map
402 tree 'link
403 (lambda (link)
404 (org-export-get-ordinal
405 (org-export-resolve-fuzzy-link link info) info)) info))))
406 ;; 2. Link to an headline should return headline's number.
407 (org-test-with-temp-text
408 "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
409 (let* ((tree (org-element-parse-buffer))
410 (info (org-combine-plists (org-export-initial-options))))
411 (setq info (org-combine-plists
412 info (org-export-collect-tree-properties tree info 'test)))
413 (should
414 ;; Note: Headline's number is in fact a list of numbers.
415 (equal '(2)
416 (org-element-map
417 tree 'link
418 (lambda (link)
419 (org-export-get-ordinal
420 (org-export-resolve-fuzzy-link link info) info)) info t)))))
421 ;; 3. Link to a target in an item should return item's number.
422 (org-test-with-temp-text
423 "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
424 (let* ((tree (org-element-parse-buffer))
425 (info (org-combine-plists (org-export-initial-options))))
426 (setq info (org-combine-plists
427 info (org-export-collect-tree-properties tree info 'test)))
428 (should
429 ;; Note: Item's number is in fact a list of numbers.
430 (equal '(1 2)
431 (org-element-map
432 tree 'link
433 (lambda (link)
434 (org-export-get-ordinal
435 (org-export-resolve-fuzzy-link link info) info)) info t)))))
436 ;; 4. Link to a target in a footnote should return footnote's
437 ;; number.
438 (org-test-with-temp-text
439 "Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
440 (let* ((tree (org-element-parse-buffer))
441 (info (org-combine-plists (org-export-initial-options))))
442 (setq info (org-combine-plists
443 info (org-export-collect-tree-properties tree info 'test)))
444 (should
445 (equal '(2 3)
446 (org-element-map
447 tree 'link
448 (lambda (link)
449 (org-export-get-ordinal
450 (org-export-resolve-fuzzy-link link info) info)) info)))))
451 ;; 5. Link to a named element should return sequence number of that
452 ;; element.
453 (org-test-with-temp-text
454 "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
455 (let* ((tree (org-element-parse-buffer))
456 (info (org-combine-plists (org-export-initial-options))))
457 (setq info (org-combine-plists
458 info (org-export-collect-tree-properties tree info 'test)))
459 (should
460 (= 2
461 (org-element-map
462 tree 'link
463 (lambda (link)
464 (org-export-get-ordinal
465 (org-export-resolve-fuzzy-link link info) info)) info t)))))
466 ;; 6. Link to a target not within an item, a table, a footnote
467 ;; reference or definition should return section number.
468 (org-test-with-temp-text
469 "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
470 (let* ((tree (org-element-parse-buffer))
471 (info (org-combine-plists (org-export-initial-options))))
472 (setq info (org-combine-plists
473 info (org-export-collect-tree-properties tree info 'test)))
474 (should
475 (equal '(2)
476 (org-element-map
477 tree 'link
478 (lambda (link)
479 (org-export-get-ordinal
480 (org-export-resolve-fuzzy-link link info) info)) info t))))))
482 (defun test-org-export/resolve-coderef ()
483 "Test `org-export-resolve-coderef' specifications."
484 (let ((org-coderef-label-format "(ref:%s)"))
485 ;; 1. A link to a "-n -k -r" block returns line number.
486 (org-test-with-temp-text
487 "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
488 (let ((tree (org-element-parse-buffer)))
489 (should
490 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
491 (org-test-with-temp-text
492 "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
493 (let ((tree (org-element-parse-buffer)))
494 (should
495 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
496 ;; 2. A link to a "-n -r" block returns line number.
497 (org-test-with-temp-text
498 "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
499 (let ((tree (org-element-parse-buffer)))
500 (should
501 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
502 (org-test-with-temp-text
503 "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
504 (let ((tree (org-element-parse-buffer)))
505 (should
506 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
507 ;; 3. A link to a "-n" block returns coderef.
508 (org-test-with-temp-text
509 "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
510 (let ((tree (org-element-parse-buffer)))
511 (should
512 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
513 "coderef"))))
514 (org-test-with-temp-text
515 "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
516 (let ((tree (org-element-parse-buffer)))
517 (should
518 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
519 "coderef"))))
520 ;; 4. A link to a "-r" block returns line number.
521 (org-test-with-temp-text
522 "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
523 (let ((tree (org-element-parse-buffer)))
524 (should
525 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
526 (org-test-with-temp-text
527 "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
528 (let ((tree (org-element-parse-buffer)))
529 (should
530 (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
531 ;; 5. A link to a block without a switch returns coderef.
532 (org-test-with-temp-text
533 "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
534 (let ((tree (org-element-parse-buffer)))
535 (should
536 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
537 "coderef"))))
538 (org-test-with-temp-text
539 "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
540 (let ((tree (org-element-parse-buffer)))
541 (should
542 (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
543 "coderef"))))
544 ;; 6. Correctly handle continued line numbers. A "+n" switch
545 ;; should resume numbering from previous block with numbered
546 ;; lines, ignoring blocks not numbering lines in the process.
547 ;; A "-n" switch resets count.
548 (org-test-with-temp-text "
549 #+BEGIN_EXAMPLE -n
550 Text.
551 #+END_EXAMPLE
553 #+BEGIN_SRC emacs-lisp
554 \(- 1 1)
555 #+END_SRC
557 #+BEGIN_SRC emacs-lisp +n -r
558 \(+ 1 1) (ref:addition)
559 #+END_SRC
561 #+BEGIN_EXAMPLE -n -r
562 Another text. (ref:text)
563 #+END_EXAMPLE"
564 (let* ((tree (org-element-parse-buffer))
565 (info `(:parse-tree ,tree)))
566 (should (= (org-export-resolve-coderef "addition" info) 2))
567 (should (= (org-export-resolve-coderef "text" info) 1))))
568 ;; 7. Recognize coderef with user-specified syntax.
569 (org-test-with-temp-text
570 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
571 (let ((tree (org-element-parse-buffer)))
572 (should (equal (org-export-resolve-coderef "text" `(:parse-tree ,tree))
573 "text"))))))
577 ;;; Src-block and example-block
579 (ert-deftest test-org-export/unravel-code ()
580 "Test `org-export-unravel-code' function."
581 (let ((org-coderef-label-format "(ref:%s)"))
582 ;; 1. Code without reference.
583 (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
584 (should (equal (org-export-unravel-code (org-element-current-element))
585 '("(+ 1 1)\n"))))
586 ;; 2. Code with reference.
587 (org-test-with-temp-text
588 "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
589 (should (equal (org-export-unravel-code (org-element-current-element))
590 '("(+ 1 1)\n" (1 . "test")))))
591 ;; 3. Code with user-defined reference.
592 (org-test-with-temp-text
593 "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
594 (should (equal (org-export-unravel-code (org-element-current-element))
595 '("(+ 1 1)\n" (1 . "test")))))
596 ;; 4. Code references keys are relative to the current block.
597 (org-test-with-temp-text "
598 #+BEGIN_EXAMPLE -n
599 \(+ 1 1)
600 #+END_EXAMPLE
601 #+BEGIN_EXAMPLE +n
602 \(+ 2 2)
603 \(+ 3 3) (ref:one)
604 #+END_EXAMPLE"
605 (goto-line 5)
606 (should (equal (org-export-unravel-code (org-element-current-element))
607 '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
608 ;; 5. Free up comma-protected lines.
610 ;; 5.1. In an Org source block, every line is protected.
611 (org-test-with-temp-text
612 "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
613 (should (equal (org-export-unravel-code (org-element-current-element))
614 '("* Test\n# comment\nText\n"))))
615 ;; 5.2. In other blocks, only headlines, comments and keywords are
616 ;; protected.
617 (org-test-with-temp-text
618 "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
619 (should (equal (org-export-unravel-code (org-element-current-element))
620 '("* Headline\n, * Not headline\n,Keep\n"))))))
624 (provide 'test-org-export)
625 ;;; test-org-export.el end here