test-org-export.el 52 KB

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