PageRenderTime 46ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/testing/lisp/test-org-export.el

https://bitbucket.org/jxonas/org-mode
Lisp | 2204 lines | 1826 code | 104 blank | 274 comment | 4 complexity | 1aa58d059f5077e6c1f5bcf7fda3c61d MD5 | raw file
Possible License(s): GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. ;;; test-org-export.el --- Tests for org-export.el
  2. ;; Copyright (C) 2012, 2013 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (unless (featurep 'org-export)
  17. (signal 'missing-test-dependency "org-export"))
  18. (defmacro org-test-with-backend (backend &rest body)
  19. "Execute body with an export back-end defined.
  20. BACKEND is the name of the back-end. BODY is the body to
  21. execute. The defined back-end simply returns parsed data as Org
  22. syntax."
  23. (declare (debug (form body)) (indent 1))
  24. `(let ((org-export-registered-backends
  25. ',(list
  26. (list backend
  27. :translate-alist
  28. (let (transcode-table)
  29. (dolist (type (append org-element-all-elements
  30. org-element-all-objects)
  31. transcode-table)
  32. (push
  33. (cons type
  34. (lambda (obj contents info)
  35. (funcall
  36. (intern (format "org-element-%s-interpreter"
  37. type))
  38. obj contents)))
  39. transcode-table)))))))
  40. (progn ,@body)))
  41. (defmacro org-test-with-parsed-data (data &rest body)
  42. "Execute body with parsed data available.
  43. DATA is a string containing the data to be parsed. BODY is the
  44. body to execute. Parse tree is available under the `tree'
  45. variable, and communication channel under `info'.
  46. This function calls `org-export-collect-tree-properties'. As
  47. such, `:ignore-list' (for `org-element-map') and
  48. `:parse-tree' (for `org-export-get-genealogy') properties are
  49. already filled in `info'."
  50. (declare (debug (form body)) (indent 1))
  51. `(org-test-with-temp-text ,data
  52. (let* ((tree (org-element-parse-buffer))
  53. (info (org-export-collect-tree-properties
  54. tree (org-export-get-environment))))
  55. ,@body)))
  56. ;;; Internal Tests
  57. (ert-deftest test-org-export/bind-keyword ()
  58. "Test reading #+BIND: keywords."
  59. ;; Test with `org-export-all-BIND' set to t.
  60. (should
  61. (org-test-with-temp-text "#+BIND: variable value"
  62. (let ((org-export-allow-BIND t))
  63. (org-export--install-letbind-maybe)
  64. (eq variable 'value))))
  65. ;; Test with `org-export-all-BIND' set to nil.
  66. (should-not
  67. (org-test-with-temp-text "#+BIND: variable value"
  68. (let ((org-export-allow-BIND nil))
  69. (org-export--install-letbind-maybe)
  70. (boundp 'variable))))
  71. ;; Test with `org-export-all-BIND' set to 'confirm and
  72. ;; `org-export--allow-BIND-local' to t .
  73. (should
  74. (org-test-with-temp-text "#+BIND: variable value"
  75. (let ((org-export-allow-BIND 'confirm))
  76. (org-set-local 'org-export--allow-BIND-local t)
  77. (org-export--install-letbind-maybe)
  78. (eq variable 'value))))
  79. ;; Test with `org-export-all-BIND' set to 'confirm and
  80. ;; `org-export--allow-BIND-local' to nil.
  81. (should-not
  82. (org-test-with-temp-text "#+BIND: variable value"
  83. (let ((org-export-allow-BIND 'confirm))
  84. (org-set-local 'org-export--allow-BIND-local nil)
  85. (org-export--install-letbind-maybe)
  86. (boundp 'variable))))
  87. ;; BIND keywords are case-insensitive.
  88. (should
  89. (org-test-with-temp-text "#+bind: variable value"
  90. (let ((org-export-allow-BIND t))
  91. (org-export--install-letbind-maybe)
  92. (eq variable 'value)))))
  93. (ert-deftest test-org-export/parse-option-keyword ()
  94. "Test reading all standard #+OPTIONS: items."
  95. (should
  96. (equal
  97. (org-export--parse-option-keyword
  98. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  99. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  100. stat:t")
  101. '(:headline-levels
  102. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  103. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  104. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  105. :with-footnotes t :with-inlinetasks nil :with-priority t
  106. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  107. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  108. :with-todo-keywords t)))
  109. ;; Test some special values.
  110. (should
  111. (equal
  112. (org-export--parse-option-keyword
  113. "arch:headline creator:comment d:(\"TEST\")
  114. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  115. '( :section-numbers
  116. 2
  117. :with-archived-trees headline :with-creator comment
  118. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  119. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  120. (ert-deftest test-org-export/get-inbuffer-options ()
  121. "Test reading all standard export keywords."
  122. (should
  123. (equal
  124. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  125. #+CREATOR: Idem
  126. #+DATE: Today
  127. #+DESCRIPTION: Testing
  128. #+DESCRIPTION: with two lines
  129. #+EMAIL: some@email.org
  130. #+EXCLUDE_TAGS: noexport invisible
  131. #+KEYWORDS: test
  132. #+LANGUAGE: en
  133. #+SELECT_TAGS: export
  134. #+TITLE: Some title
  135. #+TITLE: with spaces"
  136. (org-export--get-inbuffer-options))
  137. '(:author
  138. ("Me, Myself and I") :creator "Idem" :date ("Today")
  139. :description "Testing\nwith two lines" :email "some@email.org"
  140. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  141. :select-tags ("export") :title ("Some title with spaces")))))
  142. (ert-deftest test-org-export/get-subtree-options ()
  143. "Test setting options from headline's properties."
  144. ;; EXPORT_TITLE.
  145. (org-test-with-temp-text "#+TITLE: Title
  146. * Headline
  147. :PROPERTIES:
  148. :EXPORT_TITLE: Subtree Title
  149. :END:
  150. Paragraph"
  151. (forward-line)
  152. (should (equal (plist-get (org-export-get-environment nil t) :title)
  153. '("Subtree Title"))))
  154. :title
  155. '("subtree-title")
  156. ;; EXPORT_OPTIONS.
  157. (org-test-with-temp-text "#+OPTIONS: H:1
  158. * Headline
  159. :PROPERTIES:
  160. :EXPORT_OPTIONS: H:2
  161. :END:
  162. Paragraph"
  163. (forward-line)
  164. (should
  165. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  166. ;; EXPORT_DATE.
  167. (org-test-with-temp-text "#+DATE: today
  168. * Headline
  169. :PROPERTIES:
  170. :EXPORT_DATE: 29-03-2012
  171. :END:
  172. Paragraph"
  173. (forward-line)
  174. (should (equal (plist-get (org-export-get-environment nil t) :date)
  175. '("29-03-2012"))))
  176. ;; Properties with `split' behaviour are stored as a list of
  177. ;; strings.
  178. (should
  179. (equal '("a" "b")
  180. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  181. * Headline
  182. :PROPERTIES:
  183. :EXPORT_EXCLUDE_TAGS: a b
  184. :END:
  185. Paragraph"
  186. (progn
  187. (forward-line)
  188. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  189. ;; Handle :PROPERTY+: syntax.
  190. (should
  191. (equal '("a" "b")
  192. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  193. * Headline
  194. :PROPERTIES:
  195. :EXPORT_EXCLUDE_TAGS: a
  196. :EXPORT_EXCLUDE_TAGS+: b
  197. :END:
  198. Paragraph"
  199. (progn
  200. (forward-line)
  201. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  202. ;; Export properties are case-insensitive.
  203. (org-test-with-temp-text "* Headline
  204. :PROPERTIES:
  205. :EXPORT_Date: 29-03-2012
  206. :END:
  207. Paragraph"
  208. (should (equal (plist-get (org-export-get-environment nil t) :date)
  209. '("29-03-2012")))))
  210. (ert-deftest test-org-export/handle-options ()
  211. "Test if export options have an impact on output."
  212. ;; Test exclude tags.
  213. (org-test-with-temp-text "* Head1 :noexport:"
  214. (org-test-with-backend test
  215. (should
  216. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  217. ""))))
  218. ;; Test include tags.
  219. (org-test-with-temp-text "
  220. * Head1
  221. * Head2
  222. ** Sub-Head2.1 :export:
  223. *** Sub-Head2.1.1
  224. * Head2"
  225. (org-test-with-backend test
  226. (should
  227. (equal
  228. "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
  229. (let ((org-tags-column 0))
  230. (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
  231. ;; Test mixing include tags and exclude tags.
  232. (org-test-with-temp-text "
  233. * Head1 :export:
  234. ** Sub-Head1 :noexport:
  235. ** Sub-Head2
  236. * Head2 :noexport:
  237. ** Sub-Head1 :export:"
  238. (org-test-with-backend test
  239. (should
  240. (string-match
  241. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  242. (org-export-as
  243. 'test nil nil nil
  244. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  245. ;; Ignore tasks.
  246. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  247. (org-test-with-temp-text "* TODO Head1"
  248. (org-test-with-backend test
  249. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  250. "")))))
  251. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  252. (org-test-with-temp-text "* TODO Head1"
  253. (org-test-with-backend test
  254. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  255. "* TODO Head1\n")))))
  256. ;; Archived tree.
  257. (org-test-with-temp-text "* Head1 :archive:"
  258. (let ((org-archive-tag "archive"))
  259. (org-test-with-backend test
  260. (should
  261. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  262. "")))))
  263. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  264. (let ((org-archive-tag "archive"))
  265. (org-test-with-backend test
  266. (should
  267. (string-match
  268. "\\* Head1[ \t]+:archive:"
  269. (org-export-as 'test nil nil nil
  270. '(:with-archived-trees headline)))))))
  271. (org-test-with-temp-text "* Head1 :archive:"
  272. (let ((org-archive-tag "archive"))
  273. (org-test-with-backend test
  274. (should
  275. (string-match
  276. "\\`\\* Head1[ \t]+:archive:\n\\'"
  277. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  278. ;; Drawers.
  279. (let ((org-drawers '("TEST")))
  280. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  281. (org-test-with-backend test
  282. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  283. ""))
  284. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  285. ":TEST:\ncontents\n:END:\n")))))
  286. (let ((org-drawers '("FOO" "BAR")))
  287. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  288. (org-test-with-backend test
  289. (should
  290. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  291. ":FOO:\nkeep\n:END:\n")))))
  292. ;; Timestamps.
  293. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  294. (org-test-with-backend test
  295. (should
  296. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  297. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  298. (should
  299. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  300. (should
  301. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  302. "<2012-04-29 sun. 10:45>\n"))
  303. (should
  304. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  305. "[2012-04-29 sun. 10:45]\n"))))
  306. ;; Clocks.
  307. (let ((org-clock-string "CLOCK:"))
  308. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  309. (org-test-with-backend test
  310. (should
  311. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  312. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  313. (should
  314. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  315. ;; Plannings.
  316. (let ((org-closed-string "CLOSED:"))
  317. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  318. (org-test-with-backend test
  319. (should
  320. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  321. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  322. (should
  323. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  324. "")))))
  325. ;; Inlinetasks.
  326. (when (featurep 'org-inlinetask)
  327. (should
  328. (equal
  329. (let ((org-inlinetask-min-level 15))
  330. (org-test-with-temp-text "*************** Task"
  331. (org-test-with-backend test
  332. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  333. ""))
  334. (should
  335. (equal
  336. (let ((org-inlinetask-min-level 15))
  337. (org-test-with-temp-text
  338. "*************** Task\nContents\n*************** END"
  339. (org-test-with-backend test
  340. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  341. "")))
  342. ;; Statistics cookies.
  343. (should
  344. (equal ""
  345. (org-test-with-temp-text "[0/0]"
  346. (org-test-with-backend test
  347. (org-export-as
  348. 'test nil nil nil '(:with-statistics-cookies nil)))))))
  349. (ert-deftest test-org-export/comment-tree ()
  350. "Test if export process ignores commented trees."
  351. (let ((org-comment-string "COMMENT"))
  352. (org-test-with-temp-text "* COMMENT Head1"
  353. (org-test-with-backend test
  354. (should (equal (org-export-as 'test) ""))))))
  355. (ert-deftest test-org-export/export-scope ()
  356. "Test all export scopes."
  357. (org-test-with-temp-text "
  358. * Head1
  359. ** Head2
  360. text
  361. *** Head3"
  362. (org-test-with-backend test
  363. ;; Subtree.
  364. (forward-line 3)
  365. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  366. ;; Visible.
  367. (goto-char (point-min))
  368. (forward-line)
  369. (org-cycle)
  370. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  371. ;; Region.
  372. (goto-char (point-min))
  373. (forward-line 3)
  374. (transient-mark-mode 1)
  375. (push-mark (point) t t)
  376. (goto-char (point-at-eol))
  377. (should (equal (org-export-as 'test) "text\n"))))
  378. ;; Subtree with a code block calling another block outside.
  379. (should
  380. (equal ": 3\n"
  381. (org-test-with-temp-text "
  382. * Head1
  383. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  384. <<test>>
  385. #+END_SRC
  386. * Head2
  387. #+NAME: test
  388. #+BEGIN_SRC emacs-lisp
  389. \(+ 1 2)
  390. #+END_SRC"
  391. (org-test-with-backend test
  392. (forward-line 1)
  393. (org-export-as 'test 'subtree)))))
  394. ;; Body only.
  395. (org-test-with-temp-text "Text"
  396. (org-test-with-backend test
  397. (plist-put
  398. (cdr (assq 'test org-export-registered-backends))
  399. :translate-alist
  400. (cons (cons 'template (lambda (body info) (format "BEGIN\n%sEND" body)))
  401. (org-export-backend-translate-table 'test)))
  402. (should (equal (org-export-as 'test nil nil 'body-only) "Text\n"))
  403. (should (equal (org-export-as 'test) "BEGIN\nText\nEND")))))
  404. (ert-deftest test-org-export/expand-include ()
  405. "Test file inclusion in an Org buffer."
  406. ;; Error when file isn't specified.
  407. (should-error
  408. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  409. (org-export-expand-include-keyword)))
  410. ;; Full insertion with recursive inclusion.
  411. (org-test-with-temp-text
  412. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  413. (org-export-expand-include-keyword)
  414. (should (equal (buffer-string)
  415. "Small Org file with an include keyword.
  416. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  417. Success!
  418. * Heading
  419. body\n")))
  420. ;; Localized insertion.
  421. (org-test-with-temp-text
  422. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  423. org-test-dir)
  424. (org-export-expand-include-keyword)
  425. (should (equal (buffer-string)
  426. "Small Org file with an include keyword.\n")))
  427. ;; Insertion with constraints on headlines level.
  428. (org-test-with-temp-text
  429. (format
  430. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  431. org-test-dir)
  432. (org-export-expand-include-keyword)
  433. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  434. ;; Inclusion within an example block.
  435. (org-test-with-temp-text
  436. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  437. org-test-dir)
  438. (org-export-expand-include-keyword)
  439. (should
  440. (equal
  441. (buffer-string)
  442. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  443. ;; Inclusion within a src-block.
  444. (org-test-with-temp-text
  445. (format
  446. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  447. org-test-dir)
  448. (org-export-expand-include-keyword)
  449. (should (equal (buffer-string)
  450. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  451. (ert-deftest test-org-export/expand-macro ()
  452. "Test macro expansion in an Org buffer."
  453. ;; Standard macro expansion.
  454. (should
  455. (equal "#+MACRO: macro1 value\nvalue\n"
  456. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  457. (org-test-with-backend test (org-export-as 'test)))))
  458. ;; Expand specific macros.
  459. (should
  460. (equal "me 2012-03-29 me@here Title\n"
  461. (org-test-with-temp-text
  462. "
  463. #+TITLE: Title
  464. #+DATE: 2012-03-29
  465. #+AUTHOR: me
  466. #+EMAIL: me@here
  467. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  468. (let ((output (org-test-with-backend test (org-export-as 'test))))
  469. (substring output (string-match ".*\n\\'" output))))))
  470. ;; Expand specific macros when property contained a regular macro
  471. ;; already.
  472. (should
  473. (equal "value\n"
  474. (org-test-with-temp-text "
  475. #+MACRO: macro1 value
  476. #+TITLE: {{{macro1}}}
  477. {{{title}}}"
  478. (let ((output (org-test-with-backend test (org-export-as 'test))))
  479. (substring output (string-match ".*\n\\'" output))))))
  480. ;; Expand macros with templates in included files.
  481. (should
  482. (equal "success\n"
  483. (org-test-with-temp-text
  484. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  485. {{{included-macro}}}" org-test-dir)
  486. (let ((output (org-test-with-backend test (org-export-as 'test))))
  487. (substring output (string-match ".*\n\\'" output)))))))
  488. (ert-deftest test-org-export/user-ignore-list ()
  489. "Test if `:ignore-list' accepts user input."
  490. (org-test-with-backend test
  491. (flet ((skip-note-head
  492. (data backend info)
  493. ;; Ignore headlines with the word "note" in their title.
  494. (org-element-map
  495. data 'headline
  496. (lambda (headline)
  497. (when (string-match "\\<note\\>"
  498. (org-element-property :raw-value headline))
  499. (org-export-ignore-element headline info)))
  500. info)
  501. data))
  502. ;; Install function in parse tree filters.
  503. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  504. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  505. (should (equal (org-export-as 'test) "* Head1\n")))))))
  506. (ert-deftest test-org-export/before-processing-hook ()
  507. "Test `org-export-before-processing-hook'."
  508. (should
  509. (equal
  510. "#+MACRO: mac val\nTest\n"
  511. (org-test-with-backend test
  512. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  513. (let ((org-export-before-processing-hook
  514. '((lambda (backend)
  515. (while (re-search-forward "{{{" nil t)
  516. (let ((object (org-element-context)))
  517. (when (eq (org-element-type object) 'macro)
  518. (delete-region
  519. (org-element-property :begin object)
  520. (org-element-property :end object)))))))))
  521. (org-export-as 'test)))))))
  522. (ert-deftest test-org-export/before-parsing-hook ()
  523. "Test `org-export-before-parsing-hook'."
  524. (should
  525. (equal "Body 1\nBody 2\n"
  526. (org-test-with-backend test
  527. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  528. (let ((org-export-before-parsing-hook
  529. '((lambda (backend)
  530. (org-map-entries
  531. (lambda ()
  532. (delete-region (point) (progn (forward-line) (point)))))))))
  533. (org-export-as 'test)))))))
  534. ;;; Affiliated Keywords
  535. (ert-deftest test-org-export/read-attribute ()
  536. "Test `org-export-read-attribute' specifications."
  537. ;; Standard test.
  538. (should
  539. (equal
  540. (org-export-read-attribute
  541. :attr_html
  542. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  543. (org-element-at-point)))
  544. '(:a 1 :b 2)))
  545. ;; Return nil on empty attribute.
  546. (should-not
  547. (org-export-read-attribute
  548. :attr_html
  549. (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
  550. (ert-deftest test-org-export/get-caption ()
  551. "Test `org-export-get-caption' specifications."
  552. ;; Without optional argument, return long caption
  553. (should
  554. (equal
  555. '("l")
  556. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  557. (org-export-get-caption (org-element-at-point)))))
  558. ;; With optional argument, return short caption.
  559. (should
  560. (equal
  561. '("s")
  562. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  563. (org-export-get-caption (org-element-at-point) t))))
  564. ;; Multiple lines are separated by white spaces.
  565. (should
  566. (equal
  567. '("a" " " "b")
  568. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  569. (org-export-get-caption (org-element-at-point))))))
  570. ;;; Back-End Tools
  571. (ert-deftest test-org-export/define-backend ()
  572. "Test back-end definition and accessors."
  573. ;; Translate table.
  574. (should
  575. (equal '((headline . my-headline-test))
  576. (let (org-export-registered-backends)
  577. (org-export-define-backend test ((headline . my-headline-test)))
  578. (org-export-backend-translate-table 'test))))
  579. ;; Filters.
  580. (should
  581. (equal '((:filter-headline . my-filter))
  582. (let (org-export-registered-backends)
  583. (org-export-define-backend test
  584. ((headline . my-headline-test))
  585. :filters-alist ((:filter-headline . my-filter)))
  586. (org-export-backend-filters 'test))))
  587. ;; Options.
  588. (should
  589. (equal '((:prop value))
  590. (let (org-export-registered-backends)
  591. (org-export-define-backend test
  592. ((headline . my-headline-test))
  593. :options-alist ((:prop value)))
  594. (org-export-backend-options 'test))))
  595. ;; Menu.
  596. (should
  597. (equal '(?k "Test Export" test)
  598. (let (org-export-registered-backends)
  599. (org-export-define-backend test
  600. ((headline . my-headline-test))
  601. :menu-entry (?k "Test Export" test))
  602. (org-export-backend-menu 'test))))
  603. ;; Export Blocks.
  604. (should
  605. (equal '(("TEST" . org-element-export-block-parser))
  606. (let (org-export-registered-backends org-element-block-name-alist)
  607. (org-export-define-backend test
  608. ((headline . my-headline-test))
  609. :export-block ("test"))
  610. org-element-block-name-alist))))
  611. (ert-deftest test-org-export/define-derived-backend ()
  612. "Test `org-export-define-derived-backend' specifications."
  613. ;; Error when parent back-end is not defined.
  614. (should-error
  615. (let (org-export-registered-backends)
  616. (org-export-define-derived-backend test parent)))
  617. ;; Append translation table to parent's.
  618. (should
  619. (equal '((:headline . test) (:headline . parent))
  620. (let (org-export-registered-backends)
  621. (org-export-define-backend parent ((:headline . parent)))
  622. (org-export-define-derived-backend test parent
  623. :translate-alist ((:headline . test)))
  624. (org-export-backend-translate-table 'test)))))
  625. (ert-deftest test-org-export/derived-backend-p ()
  626. "Test `org-export-derived-backend-p' specifications."
  627. ;; Non-nil with direct match.
  628. (should
  629. (let (org-export-registered-backends)
  630. (org-export-define-backend test ((headline . test)))
  631. (org-export-derived-backend-p 'test 'test)))
  632. (should
  633. (let (org-export-registered-backends)
  634. (org-export-define-backend test ((headline . test)))
  635. (org-export-define-derived-backend test2 test)
  636. (org-export-derived-backend-p 'test2 'test2)))
  637. ;; Non-nil with a direct parent.
  638. (should
  639. (let (org-export-registered-backends)
  640. (org-export-define-backend test ((headline . test)))
  641. (org-export-define-derived-backend test2 test)
  642. (org-export-derived-backend-p 'test2 'test)))
  643. ;; Non-nil with an indirect parent.
  644. (should
  645. (let (org-export-registered-backends)
  646. (org-export-define-backend test ((headline . test)))
  647. (org-export-define-derived-backend test2 test)
  648. (org-export-define-derived-backend test3 test2)
  649. (org-export-derived-backend-p 'test3 'test)))
  650. ;; Nil otherwise.
  651. (should-not
  652. (let (org-export-registered-backends)
  653. (org-export-define-backend test ((headline . test)))
  654. (org-export-define-backend test2 ((headline . test2)))
  655. (org-export-derived-backend-p 'test2 'test)))
  656. (should-not
  657. (let (org-export-registered-backends)
  658. (org-export-define-backend test ((headline . test)))
  659. (org-export-define-backend test2 ((headline . test2)))
  660. (org-export-define-derived-backend test3 test2)
  661. (org-export-derived-backend-p 'test3 'test))))
  662. (ert-deftest test-org-export/with-backend ()
  663. "Test `org-export-with-backend' definition."
  664. ;; Error when calling an undefined back-end
  665. (should-error
  666. (let (org-export-registered-backends)
  667. (org-export-with-backend 'test "Test")))
  668. ;; Error when called back-end doesn't have an appropriate
  669. ;; transcoder.
  670. (should-error
  671. (let (org-export-registered-backends)
  672. (org-export-define-backend test ((headline . ignore)))
  673. (org-export-with-backend 'test "Test")))
  674. ;; Otherwise, export using correct transcoder
  675. (should
  676. (equal "Success"
  677. (let (org-export-registered-backends)
  678. (org-export-define-backend test
  679. ((plain-text . (lambda (text contents info) "Failure"))))
  680. (org-export-define-backend test2
  681. ((plain-text . (lambda (text contents info) "Success"))))
  682. (org-export-with-backend 'test2 "Test")))))
  683. (ert-deftest test-org-export/data-with-translations ()
  684. "Test `org-export-data-with-translations' specifications."
  685. (should
  686. (equal
  687. "Success!"
  688. (org-export-data-with-translations
  689. '(bold nil "Test")
  690. '((plain-text . (lambda (text info) "Success"))
  691. (bold . (lambda (bold contents info) (concat contents "!"))))
  692. '(:with-emphasize t)))))
  693. (ert-deftest test-org-export/data-with-backend ()
  694. "Test `org-export-data-with-backend' specifications."
  695. ;; Error when calling an undefined back-end.
  696. (should-error
  697. (let (org-export-registered-backends)
  698. (org-export-data-with-backend 'test "Test" nil)))
  699. ;; Otherwise, export data recursively, using correct back-end.
  700. (should
  701. (equal
  702. "Success!"
  703. (let (org-export-registered-backends)
  704. (org-export-define-backend test
  705. ((plain-text . (lambda (text info) "Success"))
  706. (bold . (lambda (bold contents info) (concat contents "!")))))
  707. (org-export-data-with-backend
  708. '(bold nil "Test") 'test '(:with-emphasize t))))))
  709. ;;; Export Snippets
  710. (ert-deftest test-org-export/export-snippet ()
  711. "Test export snippets transcoding."
  712. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  713. (org-test-with-backend test
  714. (plist-put
  715. (cdr (assq 'test org-export-registered-backends))
  716. :translate-alist
  717. (cons (cons 'export-snippet
  718. (lambda (snippet contents info)
  719. (when (eq (org-export-snippet-backend snippet) 'test)
  720. (org-element-property :value snippet))))
  721. (org-export-backend-translate-table 'test)))
  722. (let ((org-export-snippet-translation-alist nil))
  723. (should (equal (org-export-as 'test) "A\n")))
  724. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  725. (should (equal (org-export-as 'test) "AB\n"))))))
  726. ;;; Footnotes
  727. (ert-deftest test-org-export/footnotes ()
  728. "Test footnotes specifications."
  729. (let ((org-footnote-section nil)
  730. (org-export-with-footnotes t))
  731. ;; 1. Read every type of footnote.
  732. (should
  733. (equal
  734. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  735. (org-test-with-parsed-data
  736. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  737. (org-element-map
  738. tree 'footnote-reference
  739. (lambda (ref)
  740. (let ((def (org-export-get-footnote-definition ref info)))
  741. (cons (org-export-get-footnote-number ref info)
  742. (if (eq (org-element-property :type ref) 'inline) (car def)
  743. (car (org-element-contents
  744. (car (org-element-contents def))))))))
  745. info))))
  746. ;; 2. Test nested footnotes order.
  747. (org-test-with-parsed-data
  748. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  749. (should
  750. (equal
  751. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  752. (org-element-map
  753. tree 'footnote-reference
  754. (lambda (ref)
  755. (when (org-export-footnote-first-reference-p ref info)
  756. (cons (org-export-get-footnote-number ref info)
  757. (org-element-property :label ref))))
  758. info))))
  759. ;; 3. Test nested footnote in invisible definitions.
  760. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  761. ;; Hide definitions.
  762. (narrow-to-region (point) (point-at-eol))
  763. (let* ((tree (org-element-parse-buffer))
  764. (info (org-combine-plists
  765. `(:parse-tree ,tree)
  766. (org-export-collect-tree-properties
  767. tree (org-export-get-environment)))))
  768. ;; Both footnotes should be seen.
  769. (should
  770. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  771. ;; 4. Test footnotes definitions collection.
  772. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  773. \[fn:2] B [fn:3] [fn::D].
  774. \[fn:3] C."
  775. (should (= (length (org-export-collect-footnote-definitions tree info))
  776. 4)))
  777. ;; 5. Test export of footnotes defined outside parsing scope.
  778. (org-test-with-temp-text "[fn:1] Out of scope
  779. * Title
  780. Paragraph[fn:1]"
  781. (org-test-with-backend test
  782. (plist-put
  783. (cdr (assq 'test org-export-registered-backends))
  784. :translate-alist
  785. (cons (cons 'footnote-reference
  786. (lambda (fn contents info)
  787. (org-element-interpret-data
  788. (org-export-get-footnote-definition fn info))))
  789. (org-export-backend-translate-table 'test)))
  790. (forward-line)
  791. (should (equal "ParagraphOut of scope\n"
  792. (org-export-as 'test 'subtree)))))))
  793. ;;; Headlines and Inlinetasks
  794. (ert-deftest test-org-export/get-relative-level ()
  795. "Test `org-export-get-relative-level' specifications."
  796. ;; Standard test.
  797. (should
  798. (equal '(1 2)
  799. (let ((org-odd-levels-only nil))
  800. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  801. (org-element-map
  802. tree 'headline
  803. (lambda (h) (org-export-get-relative-level h info))
  804. info)))))
  805. ;; Missing levels
  806. (should
  807. (equal '(1 3)
  808. (let ((org-odd-levels-only nil))
  809. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  810. (org-element-map
  811. tree 'headline
  812. (lambda (h) (org-export-get-relative-level h info))
  813. info))))))
  814. (ert-deftest test-org-export/low-level-p ()
  815. "Test `org-export-low-level-p' specifications."
  816. (should
  817. (equal
  818. '(no yes)
  819. (let ((org-odd-levels-only nil))
  820. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  821. (org-element-map
  822. tree 'headline
  823. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  824. (plist-put info :headline-levels 1)))))))
  825. (ert-deftest test-org-export/get-headline-number ()
  826. "Test `org-export-get-headline-number' specifications."
  827. ;; Standard test.
  828. (should
  829. (equal
  830. '((1) (1 1))
  831. (let ((org-odd-levels-only nil))
  832. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  833. (org-element-map
  834. tree 'headline
  835. (lambda (h) (org-export-get-headline-number h info))
  836. info)))))
  837. ;; Missing levels are replaced with 0.
  838. (should
  839. (equal
  840. '((1) (1 0 1))
  841. (let ((org-odd-levels-only nil))
  842. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  843. (org-element-map
  844. tree 'headline
  845. (lambda (h) (org-export-get-headline-number h info))
  846. info))))))
  847. (ert-deftest test-org-export/numbered-headline-p ()
  848. "Test `org-export-numbered-headline-p' specifications."
  849. ;; If `:section-numbers' is nil, never number headlines.
  850. (should-not
  851. (org-test-with-parsed-data "* Headline"
  852. (org-element-map
  853. tree 'headline
  854. (lambda (h) (org-export-numbered-headline-p h info))
  855. (plist-put info :section-numbers nil))))
  856. ;; If `:section-numbers' is a number, only number headlines with
  857. ;; a level greater that it.
  858. (should
  859. (equal
  860. '(yes no)
  861. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  862. (org-element-map
  863. tree 'headline
  864. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  865. (plist-put info :section-numbers 1)))))
  866. ;; Otherwise, headlines are always numbered.
  867. (should
  868. (org-test-with-parsed-data "* Headline"
  869. (org-element-map
  870. tree 'headline
  871. (lambda (h) (org-export-numbered-headline-p h info))
  872. (plist-put info :section-numbers t)))))
  873. (ert-deftest test-org-export/number-to-roman ()
  874. "Test `org-export-number-to-roman' specifications."
  875. ;; If number is negative, return it as a string.
  876. (should (equal (org-export-number-to-roman -1) "-1"))
  877. ;; Otherwise, return it as a roman number.
  878. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  879. (ert-deftest test-org-export/get-tags ()
  880. "Test `org-export-get-tags' specifications."
  881. (let ((org-export-exclude-tags '("noexport"))
  882. (org-export-select-tags '("export")))
  883. ;; Standard test: tags which are not a select tag, an exclude tag,
  884. ;; or specified as optional argument shouldn't be ignored.
  885. (should
  886. (org-test-with-parsed-data "* Headline :tag:"
  887. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  888. info)))
  889. ;; Exclude tags are removed.
  890. (should-not
  891. (org-test-with-parsed-data "* Headline :noexport:"
  892. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  893. info)))
  894. ;; Select tags are removed.
  895. (should-not
  896. (org-test-with-parsed-data "* Headline :export:"
  897. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  898. info)))
  899. (should
  900. (equal
  901. '("tag")
  902. (org-test-with-parsed-data "* Headline :tag:export:"
  903. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  904. info))))
  905. ;; Tags provided in the optional argument are also ignored.
  906. (should-not
  907. (org-test-with-parsed-data "* Headline :ignore:"
  908. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  909. info '("ignore"))))
  910. ;; Allow tag inheritance.
  911. (should
  912. (equal
  913. '(("tag") ("tag"))
  914. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  915. (org-element-map
  916. tree 'headline
  917. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  918. ;; Tag inheritance checks FILETAGS keywords.
  919. (should
  920. (equal
  921. '(("a" "b" "tag"))
  922. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  923. (org-element-map
  924. tree 'headline
  925. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  926. (ert-deftest test-org-export/get-node-property ()
  927. "Test`org-export-get-node-property' specifications."
  928. ;; Standard test.
  929. (should
  930. (equal "value"
  931. (org-test-with-parsed-data "* Headline
  932. :PROPERTIES:
  933. :prop: value
  934. :END:"
  935. (org-export-get-node-property
  936. :prop (org-element-map tree 'headline 'identity nil t)))))
  937. ;; Test inheritance.
  938. (should
  939. (equal "value"
  940. (org-test-with-parsed-data "* Parent
  941. :PROPERTIES:
  942. :prop: value
  943. :END:
  944. ** Headline
  945. Paragraph"
  946. (org-export-get-node-property
  947. :prop (org-element-map tree 'paragraph 'identity nil t) t))))
  948. ;; Cannot return a value before the first headline.
  949. (should-not
  950. (org-test-with-parsed-data "Paragraph
  951. * Headline
  952. :PROPERTIES:
  953. :prop: value
  954. :END:"
  955. (org-export-get-node-property
  956. :prop (org-element-map tree 'paragraph 'identity nil t)))))
  957. (ert-deftest test-org-export/get-category ()
  958. "Test `org-export-get-category' specifications."
  959. ;; Standard test.
  960. (should
  961. (equal "value"
  962. (org-test-with-parsed-data "* Headline
  963. :PROPERTIES:
  964. :CATEGORY: value
  965. :END:"
  966. (org-export-get-category
  967. (org-element-map tree 'headline 'identity nil t) info))))
  968. ;; Test inheritance from a parent headline.
  969. (should
  970. (equal '("value" "value")
  971. (org-test-with-parsed-data "* Headline1
  972. :PROPERTIES:
  973. :CATEGORY: value
  974. :END:
  975. ** Headline2"
  976. (org-element-map
  977. tree 'headline
  978. (lambda (hl) (org-export-get-category hl info)) info))))
  979. ;; Test inheritance from #+CATEGORY keyword
  980. (should
  981. (equal "value"
  982. (org-test-with-parsed-data "#+CATEGORY: value
  983. * Headline"
  984. (org-export-get-category
  985. (org-element-map tree 'headline 'identity nil t) info))))
  986. ;; Test inheritance from file name.
  987. (should
  988. (equal "test"
  989. (org-test-with-parsed-data "* Headline"
  990. (let ((info (plist-put info :input-file "~/test.org")))
  991. (org-export-get-category
  992. (org-element-map tree 'headline 'identity nil t) info)))))
  993. ;; Fall-back value.
  994. (should
  995. (equal "???"
  996. (org-test-with-parsed-data "* Headline"
  997. (org-export-get-category
  998. (org-element-map tree 'headline 'identity nil t) info)))))
  999. (ert-deftest test-org-export/first-sibling-p ()
  1000. "Test `org-export-first-sibling-p' specifications."
  1001. ;; Standard test.
  1002. (should
  1003. (equal
  1004. '(yes yes no)
  1005. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1006. (org-element-map
  1007. tree 'headline
  1008. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1009. info))))
  1010. ;; Ignore headlines not exported.
  1011. (should
  1012. (equal
  1013. '(yes)
  1014. (let ((org-export-exclude-tags '("ignore")))
  1015. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1016. (org-element-map
  1017. tree 'headline
  1018. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1019. info))))))
  1020. (ert-deftest test-org-export/last-sibling-p ()
  1021. "Test `org-export-last-sibling-p' specifications."
  1022. ;; Standard test.
  1023. (should
  1024. (equal
  1025. '(yes no yes)
  1026. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1027. (org-element-map
  1028. tree 'headline
  1029. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1030. info))))
  1031. ;; Ignore headlines not exported.
  1032. (should
  1033. (equal
  1034. '(yes)
  1035. (let ((org-export-exclude-tags '("ignore")))
  1036. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1037. (org-element-map
  1038. tree 'headline
  1039. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1040. info))))))
  1041. ;;; Links
  1042. (ert-deftest test-org-export/get-coderef-format ()
  1043. "Test `org-export-get-coderef-format' specifications."
  1044. ;; A link without description returns "%s"
  1045. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1046. "%s"))
  1047. ;; Return "%s" when path is matched within description.
  1048. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1049. "desc %s"))
  1050. ;; Otherwise return description.
  1051. (should (equal (org-export-get-coderef-format "path" "desc")
  1052. "desc")))
  1053. (ert-deftest test-org-export/inline-image-p ()
  1054. "Test `org-export-inline-image-p' specifications."
  1055. (should
  1056. (org-export-inline-image-p
  1057. (org-test-with-temp-text "[[#id]]"
  1058. (org-element-map
  1059. (org-element-parse-buffer) 'link 'identity nil t))
  1060. '(("custom-id" . "id")))))
  1061. (ert-deftest test-org-export/fuzzy-link ()
  1062. "Test fuzzy links specifications."
  1063. ;; 1. Links to invisible (keyword) targets should be ignored.
  1064. (org-test-with-parsed-data
  1065. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  1066. (should-not
  1067. (org-element-map
  1068. tree 'link
  1069. (lambda (link)
  1070. (org-export-get-ordinal
  1071. (org-export-resolve-fuzzy-link link info) info)) info)))
  1072. ;; 2. Link to an headline should return headline's number.
  1073. (org-test-with-parsed-data
  1074. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1075. (should
  1076. ;; Note: Headline's number is in fact a list of numbers.
  1077. (equal '(2)
  1078. (org-element-map
  1079. tree 'link
  1080. (lambda (link)
  1081. (org-export-get-ordinal
  1082. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1083. ;; 3. Link to a target in an item should return item's number.
  1084. (org-test-with-parsed-data
  1085. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1086. (should
  1087. ;; Note: Item's number is in fact a list of numbers.
  1088. (equal '(1 2)
  1089. (org-element-map
  1090. tree 'link
  1091. (lambda (link)
  1092. (org-export-get-ordinal
  1093. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1094. ;; 4. Link to a target in a footnote should return footnote's
  1095. ;; number.
  1096. (org-test-with-parsed-data "
  1097. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1098. (should
  1099. (equal '(2 3)
  1100. (org-element-map
  1101. tree 'link
  1102. (lambda (link)
  1103. (org-export-get-ordinal
  1104. (org-export-resolve-fuzzy-link link info) info)) info))))
  1105. ;; 5. Link to a named element should return sequence number of that
  1106. ;; element.
  1107. (org-test-with-parsed-data
  1108. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1109. (should
  1110. (= 2
  1111. (org-element-map
  1112. tree 'link
  1113. (lambda (link)
  1114. (org-export-get-ordinal
  1115. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1116. ;; 6. Link to a target not within an item, a table, a footnote
  1117. ;; reference or definition should return section number.
  1118. (org-test-with-parsed-data
  1119. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1120. (should
  1121. (equal '(2)
  1122. (org-element-map
  1123. tree 'link
  1124. (lambda (link)
  1125. (org-export-get-ordinal
  1126. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  1127. (ert-deftest test-org-export/resolve-coderef ()
  1128. "Test `org-export-resolve-coderef' specifications."
  1129. (let ((org-coderef-label-format "(ref:%s)"))
  1130. ;; 1. A link to a "-n -k -r" block returns line number.
  1131. (org-test-with-parsed-data
  1132. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1133. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1134. (org-test-with-parsed-data
  1135. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1136. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1137. ;; 2. A link to a "-n -r" block returns line number.
  1138. (org-test-with-parsed-data
  1139. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1140. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1141. (org-test-with-parsed-data
  1142. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1143. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1144. ;; 3. A link to a "-n" block returns coderef.
  1145. (org-test-with-parsed-data
  1146. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1147. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1148. (org-test-with-parsed-data
  1149. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1150. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1151. ;; 4. A link to a "-r" block returns line number.
  1152. (org-test-with-parsed-data
  1153. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1154. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1155. (org-test-with-parsed-data
  1156. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1157. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1158. ;; 5. A link to a block without a switch returns coderef.
  1159. (org-test-with-parsed-data
  1160. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1161. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1162. (org-test-with-parsed-data
  1163. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1164. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1165. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1166. ;; should resume numbering from previous block with numbered
  1167. ;; lines, ignoring blocks not numbering lines in the process.
  1168. ;; A "-n" switch resets count.
  1169. (org-test-with-parsed-data "
  1170. #+BEGIN_EXAMPLE -n
  1171. Text.
  1172. #+END_EXAMPLE
  1173. #+BEGIN_SRC emacs-lisp
  1174. \(- 1 1)
  1175. #+END_SRC
  1176. #+BEGIN_SRC emacs-lisp +n -r
  1177. \(+ 1 1) (ref:addition)
  1178. #+END_SRC
  1179. #+BEGIN_EXAMPLE -n -r
  1180. Another text. (ref:text)
  1181. #+END_EXAMPLE"
  1182. (should (= (org-export-resolve-coderef "addition" info) 2))
  1183. (should (= (org-export-resolve-coderef "text" info) 1)))
  1184. ;; 7. Recognize coderef with user-specified syntax.
  1185. (org-test-with-parsed-data
  1186. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1187. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1188. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1189. "Test `org-export-resolve-fuzzy-link' specifications."
  1190. ;; 1. Match target objects.
  1191. (org-test-with-parsed-data "<<target>> [[target]]"
  1192. (should
  1193. (org-export-resolve-fuzzy-link
  1194. (org-element-map tree 'link 'identity info t) info)))
  1195. ;; 2. Match target elements.
  1196. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  1197. (should
  1198. (org-export-resolve-fuzzy-link
  1199. (org-element-map tree 'link 'identity info t) info)))
  1200. ;; 3. Match named elements.
  1201. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1202. (should
  1203. (org-export-resolve-fuzzy-link
  1204. (org-element-map tree 'link 'identity info t) info)))
  1205. ;; 4. Match exact headline's name.
  1206. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1207. (should
  1208. (org-export-resolve-fuzzy-link
  1209. (org-element-map tree 'link 'identity info t) info)))
  1210. ;; 5. Targets objects have priority over named elements and headline
  1211. ;; titles.
  1212. (org-test-with-parsed-data
  1213. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1214. (should
  1215. (eq 'target
  1216. (org-element-type
  1217. (org-export-resolve-fuzzy-link
  1218. (org-element-map tree 'link 'identity info t) info)))))
  1219. ;; 6. Named elements have priority over headline titles.
  1220. (org-test-with-parsed-data
  1221. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1222. (should
  1223. (eq 'paragraph
  1224. (org-element-type
  1225. (org-export-resolve-fuzzy-link
  1226. (org-element-map tree 'link 'identity info t) info)))))
  1227. ;; 7. If link's path starts with a "*", only match headline titles,
  1228. ;; though.
  1229. (org-test-with-parsed-data
  1230. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1231. (should
  1232. (eq 'headline
  1233. (org-element-type
  1234. (org-export-resolve-fuzzy-link
  1235. (org-element-map tree 'link 'identity info t) info)))))
  1236. ;; 8. Return nil if no match.
  1237. (org-test-with-parsed-data "[[target]]"
  1238. (should-not
  1239. (org-export-resolve-fuzzy-link
  1240. (org-element-map tree 'link 'identity info t) info))))
  1241. (ert-deftest test-org-export/resolve-id-link ()
  1242. "Test `org-export-resolve-id-link' specifications."
  1243. ;; 1. Regular test for custom-id link.
  1244. (org-test-with-parsed-data "* Headline1
  1245. :PROPERTIES:
  1246. :CUSTOM-ID: test
  1247. :END:
  1248. * Headline 2
  1249. \[[#test]]"
  1250. (should
  1251. (org-export-resolve-id-link
  1252. (org-element-map tree 'link 'identity info t) info)))
  1253. ;; 2. Failing test for custom-id link.
  1254. (org-test-with-parsed-data "* Headline1
  1255. :PROPERTIES:
  1256. :CUSTOM-ID: test
  1257. :END:
  1258. * Headline 2
  1259. \[[#no-match]]"
  1260. (should-not
  1261. (org-export-resolve-id-link
  1262. (org-element-map tree 'link 'identity info t) info)))
  1263. ;; 3. Test for internal id target.
  1264. (org-test-with-parsed-data "* Headline1
  1265. :PROPERTIES:
  1266. :ID: aaaa
  1267. :END:
  1268. * Headline 2
  1269. \[[id:aaaa]]"
  1270. (should
  1271. (org-export-resolve-id-link
  1272. (org-element-map tree 'link 'identity info t) info)))
  1273. ;; 4. Test for external id target.
  1274. (org-test-with-parsed-data "[[id:aaaa]]"
  1275. (should
  1276. (org-export-resolve-id-link
  1277. (org-element-map tree 'link 'identity info t)
  1278. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1279. (ert-deftest test-org-export/resolve-radio-link ()
  1280. "Test `org-export-resolve-radio-link' specifications."
  1281. ;; Standard test.
  1282. (org-test-with-temp-text "<<<radio>>> radio"
  1283. (org-update-radio-target-regexp)
  1284. (should
  1285. (let* ((tree (org-element-parse-buffer))
  1286. (info `(:parse-tree ,tree)))
  1287. (org-export-resolve-radio-link
  1288. (org-element-map tree 'link 'identity info t)
  1289. info))))
  1290. ;; Radio target with objects.
  1291. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1292. (org-update-radio-target-regexp)
  1293. (should
  1294. (let* ((tree (org-element-parse-buffer))
  1295. (info `(:parse-tree ,tree)))
  1296. (org-export-resolve-radio-link
  1297. (org-element-map tree 'link 'identity info t)
  1298. info)))))
  1299. ;;; Src-block and example-block
  1300. (ert-deftest test-org-export/unravel-code ()
  1301. "Test `org-export-unravel-code' function."
  1302. (let ((org-coderef-label-format "(ref:%s)"))
  1303. ;; 1. Code without reference.
  1304. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1305. (should (equal (org-export-unravel-code (org-element-at-point))
  1306. '("(+ 1 1)\n"))))
  1307. ;; 2. Code with reference.
  1308. (org-test-with-temp-text
  1309. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1310. (should (equal (org-export-unravel-code (org-element-at-point))
  1311. '("(+ 1 1)\n" (1 . "test")))))
  1312. ;; 3. Code with user-defined reference.
  1313. (org-test-with-temp-text
  1314. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1315. (should (equal (org-export-unravel-code (org-element-at-point))
  1316. '("(+ 1 1)\n" (1 . "test")))))
  1317. ;; 4. Code references keys are relative to the current block.
  1318. (org-test-with-temp-text "
  1319. #+BEGIN_EXAMPLE -n
  1320. \(+ 1 1)
  1321. #+END_EXAMPLE
  1322. #+BEGIN_EXAMPLE +n
  1323. \(+ 2 2)
  1324. \(+ 3 3) (ref:one)
  1325. #+END_EXAMPLE"
  1326. (goto-line 5)
  1327. (should (equal (org-export-unravel-code (org-element-at-point))
  1328. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1329. ;;; Smart Quotes
  1330. (ert-deftest test-org-export/activate-smart-quotes ()
  1331. "Test `org-export-activate-smart-quotes' specifications."
  1332. ;; Opening double quotes: standard test.
  1333. (should
  1334. (equal
  1335. '("some &ldquo;paragraph")
  1336. (let ((org-export-default-language "en"))
  1337. (org-test-with-parsed-data "some \"paragraph"
  1338. (org-element-map
  1339. tree 'plain-text
  1340. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1341. info)))))
  1342. ;; Opening quotes: at the beginning of a paragraph.
  1343. (should
  1344. (equal
  1345. '("&ldquo;begin")
  1346. (let ((org-export-default-language "en"))
  1347. (org-test-with-parsed-data "\"begin"
  1348. (org-element-map
  1349. tree 'plain-text
  1350. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1351. info)))))
  1352. ;; Opening quotes: after an object.
  1353. (should
  1354. (equal
  1355. '("&ldquo;begin")
  1356. (let ((org-export-default-language "en"))
  1357. (org-test-with-parsed-data "=verb= \"begin"
  1358. (org-element-map
  1359. tree 'plain-text
  1360. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1361. info)))))
  1362. ;; Closing quotes: standard test.
  1363. (should
  1364. (equal
  1365. '("some&rdquo; paragraph")
  1366. (let ((org-export-default-language "en"))
  1367. (org-test-with-parsed-data "some\" paragraph"
  1368. (org-element-map
  1369. tree 'plain-text
  1370. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1371. info)))))
  1372. ;; Closing quotes: at the end of a paragraph.
  1373. (should
  1374. (equal
  1375. '("end&rdquo;")
  1376. (let ((org-export-default-language "en"))
  1377. (org-test-with-parsed-data "end\""
  1378. (org-element-map
  1379. tree 'plain-text
  1380. (lambda (s) (org-export-activate-smart-quotes s :h

Large files files are truncated, but you can click here to view the full file