test-org-export.el 20 KB

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