test-org-export.el 52 KB

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