test-org-export.el 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. ;;; test-org-export.el --- Tests for org-export.el
  2. ;; Copyright (C) 2012 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  6. ;;;; Comments
  7. ;;; Code:
  8. (unless (featurep 'org-export)
  9. (signal 'missing-test-dependency "org-export"))
  10. ;;; Tests
  11. (defmacro org-test-with-backend (backend &rest body)
  12. "Execute body with an export back-end defined.
  13. BACKEND is the name of the back-end. BODY is the body to
  14. execute. The defined back-end simply returns parsed data as Org
  15. syntax."
  16. (declare (debug (form body)) (indent 1))
  17. `(let ((,(intern (format "org-%s-translate-alist" backend))
  18. ',(let (transcode-table)
  19. (dolist (type (append org-element-all-elements
  20. org-element-all-objects)
  21. transcode-table)
  22. (push (cons type (intern (format "org-%s-%s" backend type)))
  23. transcode-table)))))
  24. (flet ,(let (transcoders)
  25. (dolist (type (append org-element-all-elements
  26. org-element-all-objects)
  27. transcoders)
  28. (push `(,(intern (format "org-%s-%s" backend type))
  29. (obj contents info)
  30. (,(intern (format "org-element-%s-interpreter" type))
  31. obj contents))
  32. transcoders)))
  33. ,@body)))
  34. (defmacro org-test-with-parsed-data (data &rest body)
  35. "Execute body with parsed data available.
  36. DATA is a string containing the data to be parsed. BODY is the
  37. body to execute. Parse tree is available under the `tree'
  38. variable, and communication channel under `info'.
  39. This function calls `org-export-collect-tree-properties'. As
  40. such, `:ignore-list' (for `org-element-map') and
  41. `:parse-tree' (for `org-export-get-genealogy') properties are
  42. already filled in `info'."
  43. (declare (debug (form body)) (indent 1))
  44. `(org-test-with-temp-text ,data
  45. (let* ((tree (org-element-parse-buffer))
  46. (info (org-export-collect-tree-properties tree nil)))
  47. ,@body)))
  48. (ert-deftest test-org-export/parse-option-keyword ()
  49. "Test reading all standard #+OPTIONS: items."
  50. (should
  51. (equal
  52. (org-export-parse-option-keyword
  53. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  54. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
  55. '(:headline-levels
  56. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  57. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  58. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  59. :with-footnotes t :with-priority t :with-special-strings t
  60. :with-sub-superscript t :with-toc t :with-tables t :with-tags t
  61. :with-tasks t :with-timestamps t :with-todo-keywords t)))
  62. ;; Test some special values.
  63. (should
  64. (equal
  65. (org-export-parse-option-keyword
  66. "arch:headline creator:comment d:(\"TEST\")
  67. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  68. '( :section-numbers
  69. 2
  70. :with-archived-trees headline :with-creator comment
  71. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  72. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  73. (ert-deftest test-org-export/get-inbuffer-options ()
  74. "Test reading all standard export keywords."
  75. (should
  76. (equal
  77. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  78. #+CREATOR: Idem
  79. #+DATE: Today
  80. #+DESCRIPTION: Testing
  81. #+DESCRIPTION: with two lines
  82. #+EMAIL: some@email.org
  83. #+EXPORT_EXCLUDE_TAGS: noexport invisible
  84. #+KEYWORDS: test
  85. #+LANGUAGE: en
  86. #+EXPORT_SELECT_TAGS: export
  87. #+TITLE: Some title
  88. #+TITLE: with spaces"
  89. (org-export-get-inbuffer-options))
  90. '(:author
  91. ("Me, Myself and I") :creator "Idem" :date "Today"
  92. :description "Testing\nwith two lines" :email "some@email.org"
  93. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  94. :select-tags ("export") :title ("Some title with spaces")))))
  95. (ert-deftest test-org-export/define-macro ()
  96. "Try defining various Org macro using in-buffer #+MACRO: keyword."
  97. ;; Parsed macro.
  98. (should (equal (org-test-with-temp-text "#+MACRO: one 1"
  99. (org-export-get-inbuffer-options))
  100. '(:macro-one ("1"))))
  101. ;; Evaled macro.
  102. (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
  103. (org-export-get-inbuffer-options))
  104. '(:macro-two "(eval (+ 1 1))")))
  105. ;; Incomplete macro.
  106. (should-not (org-test-with-temp-text "#+MACRO: three"
  107. (org-export-get-inbuffer-options)))
  108. ;; Macro with newline character.
  109. (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
  110. (org-export-get-inbuffer-options))
  111. '(:macro-four ("a\nb"))))
  112. ;; Macro with protected newline character.
  113. (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
  114. (org-export-get-inbuffer-options))
  115. '(:macro-five ("a\\nb"))))
  116. ;; Recursive macro.
  117. (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
  118. (should
  119. (equal
  120. (org-export-get-inbuffer-options)
  121. '(:macro-six
  122. ("6")
  123. :macro-seven
  124. ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
  125. :post-blank 0))))))))
  126. (ert-deftest test-org-export/handle-options ()
  127. "Test if export options have an impact on output."
  128. ;; Test exclude tags.
  129. (org-test-with-temp-text "* Head1 :noexport:"
  130. (org-test-with-backend test
  131. (should
  132. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  133. ""))))
  134. ;; Test include tags.
  135. (org-test-with-temp-text "
  136. * Head1
  137. ** Sub-Head1.1 :export:
  138. *** Sub-Head1.1.1
  139. * Head2"
  140. (org-test-with-backend test
  141. (should
  142. (string-match
  143. "\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
  144. (org-export-as 'test nil nil nil '(:select-tags ("export")))))))
  145. ;; Test mixing include tags and exclude tags.
  146. (org-test-with-temp-text "
  147. * Head1 :export:
  148. ** Sub-Head1 :noexport:
  149. ** Sub-Head2
  150. * Head2 :noexport:
  151. ** Sub-Head1 :export:"
  152. (org-test-with-backend test
  153. (should
  154. (string-match
  155. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  156. (org-export-as
  157. 'test nil nil nil
  158. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  159. ;; Ignore tasks.
  160. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  161. (org-test-with-temp-text "* TODO Head1"
  162. (org-test-with-backend test
  163. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  164. "")))))
  165. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  166. (org-test-with-temp-text "* TODO Head1"
  167. (org-test-with-backend test
  168. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  169. "* TODO Head1\n")))))
  170. ;; Archived tree.
  171. (org-test-with-temp-text "* Head1 :archive:"
  172. (let ((org-archive-tag "archive"))
  173. (org-test-with-backend test
  174. (should
  175. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  176. "")))))
  177. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  178. (let ((org-archive-tag "archive"))
  179. (org-test-with-backend test
  180. (should
  181. (string-match
  182. "\\* Head1[ \t]+:archive:"
  183. (org-export-as 'test nil nil nil
  184. '(:with-archived-trees headline)))))))
  185. (org-test-with-temp-text "* Head1 :archive:"
  186. (let ((org-archive-tag "archive"))
  187. (org-test-with-backend test
  188. (should
  189. (string-match
  190. "\\`\\* Head1[ \t]+:archive:\n\\'"
  191. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  192. ;; Drawers.
  193. (let ((org-drawers '("TEST")))
  194. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  195. (org-test-with-backend test
  196. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  197. ""))
  198. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  199. ":TEST:\ncontents\n:END:\n")))))
  200. (let ((org-drawers '("FOO" "BAR")))
  201. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  202. (org-test-with-backend test
  203. (should
  204. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  205. ":FOO:\nkeep\n:END:\n")))))
  206. ;; Timestamps.
  207. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  208. (org-test-with-backend test
  209. (should
  210. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  211. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  212. (should
  213. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  214. (should
  215. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  216. "<2012-04-29 sun. 10:45>\n"))
  217. (should
  218. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  219. "[2012-04-29 sun. 10:45]\n"))))
  220. ;; Clocks.
  221. (let ((org-clock-string "CLOCK:"))
  222. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  223. (org-test-with-backend test
  224. (should
  225. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  226. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  227. (should
  228. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  229. ;; Plannings.
  230. (let ((org-closed-string "CLOSED:"))
  231. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  232. (org-test-with-backend test
  233. (should
  234. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  235. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  236. (should
  237. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  238. ""))))))
  239. (ert-deftest test-org-export/comment-tree ()
  240. "Test if export process ignores commented trees."
  241. (let ((org-comment-string "COMMENT"))
  242. (org-test-with-temp-text "* COMMENT Head1"
  243. (org-test-with-backend test
  244. (should (equal (org-export-as 'test) ""))))))
  245. (ert-deftest test-org-export/export-scope ()
  246. "Test all export scopes."
  247. (org-test-with-temp-text "
  248. * Head1
  249. ** Head2
  250. text
  251. *** Head3"
  252. (org-test-with-backend test
  253. ;; Subtree.
  254. (forward-line 3)
  255. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  256. ;; Visible.
  257. (goto-char (point-min))
  258. (forward-line)
  259. (org-cycle)
  260. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  261. ;; Body only.
  262. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  263. (should (equal (org-export-as 'test nil nil 'body-only)
  264. "* Head1\n** Head2\ntext\n*** Head3\n"))
  265. (should (equal (org-export-as 'test)
  266. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  267. ;; Region.
  268. (goto-char (point-min))
  269. (forward-line 3)
  270. (transient-mark-mode 1)
  271. (push-mark (point) t t)
  272. (goto-char (point-at-eol))
  273. (should (equal (org-export-as 'test) "text\n"))))
  274. ;; Subtree with a code block calling another block outside.
  275. (org-test-with-temp-text "
  276. * Head1
  277. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  278. <<test>>
  279. #+END_SRC
  280. * Head2
  281. #+NAME: test
  282. #+BEGIN_SRC emacs-lisp
  283. \(+ 1 2)
  284. #+END_SRC"
  285. (org-test-with-backend test
  286. (forward-line 1)
  287. (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
  288. (ert-deftest test-org-export/export-snippet ()
  289. "Test export snippets transcoding."
  290. (org-test-with-temp-text "<test@A><t@B>"
  291. (org-test-with-backend test
  292. (flet ((org-test-export-snippet
  293. (snippet contents info)
  294. (when (eq (org-export-snippet-backend snippet) 'test)
  295. (org-element-property :value snippet))))
  296. (let ((org-export-snippet-translation-alist nil))
  297. (should (equal (org-export-as 'test) "A\n")))
  298. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  299. (should (equal (org-export-as 'test) "AB\n")))))))
  300. (ert-deftest test-org-export/expand-include ()
  301. "Test file inclusion in an Org buffer."
  302. ;; Full insertion with recursive inclusion.
  303. (org-test-with-temp-text
  304. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  305. (org-export-expand-include-keyword)
  306. (should (equal (buffer-string)
  307. "Small Org file with an include keyword.
  308. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  309. Success!
  310. * Heading
  311. body\n")))
  312. ;; Localized insertion.
  313. (org-test-with-temp-text
  314. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  315. org-test-dir)
  316. (org-export-expand-include-keyword)
  317. (should (equal (buffer-string)
  318. "Small Org file with an include keyword.\n")))
  319. ;; Insertion with constraints on headlines level.
  320. (org-test-with-temp-text
  321. (format
  322. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  323. org-test-dir)
  324. (org-export-expand-include-keyword)
  325. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  326. ;; Inclusion within an example block.
  327. (org-test-with-temp-text
  328. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  329. org-test-dir)
  330. (org-export-expand-include-keyword)
  331. (should
  332. (equal
  333. (buffer-string)
  334. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  335. ;; Inclusion within a src-block.
  336. (org-test-with-temp-text
  337. (format
  338. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  339. org-test-dir)
  340. (org-export-expand-include-keyword)
  341. (should (equal (buffer-string)
  342. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  343. (ert-deftest test-org-export/user-ignore-list ()
  344. "Test if `:ignore-list' accepts user input."
  345. (org-test-with-backend test
  346. (flet ((skip-note-head
  347. (data backend info)
  348. ;; Ignore headlines with the word "note" in their title.
  349. (org-element-map
  350. data 'headline
  351. (lambda (headline)
  352. (when (string-match "\\<note\\>"
  353. (org-element-property :raw-value headline))
  354. (org-export-ignore-element headline info)))
  355. info)
  356. data))
  357. ;; Install function in parse tree filters.
  358. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  359. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  360. (should (equal (org-export-as 'test) "* Head1\n")))))))
  361. (ert-deftest test-org-export/before-parsing-hook ()
  362. "Test `org-export-before-parsing-hook'."
  363. (org-test-with-backend test
  364. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  365. (let ((org-export-before-parsing-hook
  366. '((lambda ()
  367. (org-map-entries
  368. (lambda ()
  369. (delete-region (point) (progn (forward-line) (point)))))))))
  370. (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
  371. ;;; Footnotes
  372. (ert-deftest test-org-export/footnotes ()
  373. "Test footnotes specifications."
  374. (let ((org-footnote-section nil))
  375. ;; 1. Read every type of footnote.
  376. (org-test-with-temp-text
  377. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  378. (let* ((tree (org-element-parse-buffer))
  379. (info (org-export-store-footnote-definitions
  380. `(:parse-tree ,tree :with-footnotes t))))
  381. (should
  382. (equal
  383. '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
  384. (org-element-map
  385. tree 'footnote-reference
  386. (lambda (ref)
  387. (let ((def (org-export-get-footnote-definition ref info)))
  388. (cons (org-export-get-footnote-number ref info)
  389. (if (eq (org-element-property :type ref) 'inline) (car def)
  390. (car (org-element-contents
  391. (car (org-element-contents def))))))))
  392. info)))))
  393. ;; 2. Test nested footnotes order.
  394. (org-test-with-temp-text
  395. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  396. (let* ((tree (org-element-parse-buffer))
  397. (info (org-export-store-footnote-definitions
  398. `(:parse-tree ,tree :with-footnotes t))))
  399. (should
  400. (equal
  401. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  402. (org-element-map
  403. tree 'footnote-reference
  404. (lambda (ref)
  405. (when (org-export-footnote-first-reference-p ref info)
  406. (cons (org-export-get-footnote-number ref info)
  407. (org-element-property :label ref))))
  408. info)))))
  409. ;; 3. Test nested footnote in invisible definitions.
  410. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  411. ;; Hide definitions.
  412. (narrow-to-region (point) (point-at-eol))
  413. (let* ((tree (org-element-parse-buffer))
  414. (info (org-export-store-footnote-definitions
  415. `(:parse-tree ,tree :with-footnotes t))))
  416. ;; Both footnotes should be seen.
  417. (should
  418. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  419. ;; 4. Test footnotes definitions collection.
  420. (org-test-with-temp-text "Text[fn:1:A[fn:2]] [fn:3].
  421. \[fn:2] B [fn:3] [fn::D].
  422. \[fn:3] C."
  423. (let* ((tree (org-element-parse-buffer))
  424. (info (org-export-store-footnote-definitions
  425. `(:parse-tree ,tree :with-footnotes t))))
  426. (should (= (length (org-export-collect-footnote-definitions tree info))
  427. 4))))
  428. ;; 5. Test export of footnotes defined outside parsing scope.
  429. (org-test-with-temp-text "[fn:1] Out of scope
  430. * Title
  431. Paragraph[fn:1]"
  432. (org-test-with-backend test
  433. (flet ((org-test-footnote-reference
  434. (fn-ref contents info)
  435. (org-element-interpret-data
  436. (org-export-get-footnote-definition fn-ref info))))
  437. (forward-line)
  438. (should (equal "ParagraphOut of scope\n"
  439. (org-export-as 'test 'subtree))))))))
  440. ;;; Links
  441. (ert-deftest test-org-export/fuzzy-links ()
  442. "Test fuzzy link export specifications."
  443. ;; 1. Links to invisible (keyword) targets should be ignored.
  444. (org-test-with-parsed-data
  445. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  446. (should-not
  447. (org-element-map
  448. tree 'link
  449. (lambda (link)
  450. (org-export-get-ordinal
  451. (org-export-resolve-fuzzy-link link info) info)) info)))
  452. ;; 2. Link to an headline should return headline's number.
  453. (org-test-with-parsed-data
  454. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  455. (should
  456. ;; Note: Headline's number is in fact a list of numbers.
  457. (equal '(2)
  458. (org-element-map
  459. tree 'link
  460. (lambda (link)
  461. (org-export-get-ordinal
  462. (org-export-resolve-fuzzy-link link info) info)) info t))))
  463. ;; 3. Link to a target in an item should return item's number.
  464. (org-test-with-parsed-data
  465. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  466. (should
  467. ;; Note: Item's number is in fact a list of numbers.
  468. (equal '(1 2)
  469. (org-element-map
  470. tree 'link
  471. (lambda (link)
  472. (org-export-get-ordinal
  473. (org-export-resolve-fuzzy-link link info) info)) info t))))
  474. ;; 4. Link to a target in a footnote should return footnote's
  475. ;; number.
  476. (org-test-with-parsed-data "
  477. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  478. (should
  479. (equal '(2 3)
  480. (org-element-map
  481. tree 'link
  482. (lambda (link)
  483. (org-export-get-ordinal
  484. (org-export-resolve-fuzzy-link link info) info)) info))))
  485. ;; 5. Link to a named element should return sequence number of that
  486. ;; element.
  487. (org-test-with-parsed-data
  488. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  489. (should
  490. (= 2
  491. (org-element-map
  492. tree 'link
  493. (lambda (link)
  494. (org-export-get-ordinal
  495. (org-export-resolve-fuzzy-link link info) info)) info t))))
  496. ;; 6. Link to a target not within an item, a table, a footnote
  497. ;; reference or definition should return section number.
  498. (org-test-with-parsed-data
  499. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  500. (should
  501. (equal '(2)
  502. (org-element-map
  503. tree 'link
  504. (lambda (link)
  505. (org-export-get-ordinal
  506. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  507. (defun test-org-export/resolve-coderef ()
  508. "Test `org-export-resolve-coderef' specifications."
  509. (let ((org-coderef-label-format "(ref:%s)"))
  510. ;; 1. A link to a "-n -k -r" block returns line number.
  511. (org-test-with-temp-text
  512. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  513. (let ((tree (org-element-parse-buffer)))
  514. (should
  515. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  516. (org-test-with-temp-text
  517. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  518. (let ((tree (org-element-parse-buffer)))
  519. (should
  520. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  521. ;; 2. A link to a "-n -r" block returns line number.
  522. (org-test-with-temp-text
  523. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  524. (let ((tree (org-element-parse-buffer)))
  525. (should
  526. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  527. (org-test-with-temp-text
  528. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  529. (let ((tree (org-element-parse-buffer)))
  530. (should
  531. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  532. ;; 3. A link to a "-n" block returns coderef.
  533. (org-test-with-temp-text
  534. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  535. (let ((tree (org-element-parse-buffer)))
  536. (should
  537. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  538. "coderef"))))
  539. (org-test-with-temp-text
  540. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  541. (let ((tree (org-element-parse-buffer)))
  542. (should
  543. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  544. "coderef"))))
  545. ;; 4. A link to a "-r" block returns line number.
  546. (org-test-with-temp-text
  547. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  548. (let ((tree (org-element-parse-buffer)))
  549. (should
  550. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  551. (org-test-with-temp-text
  552. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  553. (let ((tree (org-element-parse-buffer)))
  554. (should
  555. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  556. ;; 5. A link to a block without a switch returns coderef.
  557. (org-test-with-temp-text
  558. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  559. (let ((tree (org-element-parse-buffer)))
  560. (should
  561. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  562. "coderef"))))
  563. (org-test-with-temp-text
  564. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  565. (let ((tree (org-element-parse-buffer)))
  566. (should
  567. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  568. "coderef"))))
  569. ;; 6. Correctly handle continued line numbers. A "+n" switch
  570. ;; should resume numbering from previous block with numbered
  571. ;; lines, ignoring blocks not numbering lines in the process.
  572. ;; A "-n" switch resets count.
  573. (org-test-with-temp-text "
  574. #+BEGIN_EXAMPLE -n
  575. Text.
  576. #+END_EXAMPLE
  577. #+BEGIN_SRC emacs-lisp
  578. \(- 1 1)
  579. #+END_SRC
  580. #+BEGIN_SRC emacs-lisp +n -r
  581. \(+ 1 1) (ref:addition)
  582. #+END_SRC
  583. #+BEGIN_EXAMPLE -n -r
  584. Another text. (ref:text)
  585. #+END_EXAMPLE"
  586. (let* ((tree (org-element-parse-buffer))
  587. (info `(:parse-tree ,tree)))
  588. (should (= (org-export-resolve-coderef "addition" info) 2))
  589. (should (= (org-export-resolve-coderef "text" info) 1))))
  590. ;; 7. Recognize coderef with user-specified syntax.
  591. (org-test-with-temp-text
  592. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  593. (let ((tree (org-element-parse-buffer)))
  594. (should (equal (org-export-resolve-coderef "text" `(:parse-tree ,tree))
  595. "text"))))))
  596. (ert-deftest test-org-export/resolve-radio-link ()
  597. "Test `org-export-resolve-radio-link' specifications."
  598. ;; Standard test.
  599. (org-test-with-temp-text "<<<radio>>> radio"
  600. (org-update-radio-target-regexp)
  601. (should
  602. (let* ((tree (org-element-parse-buffer))
  603. (info `(:parse-tree ,tree)))
  604. (org-export-resolve-radio-link
  605. (org-element-map tree 'link 'identity info t)
  606. info))))
  607. ;; Radio target with objects.
  608. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  609. (org-update-radio-target-regexp)
  610. (should
  611. (let* ((tree (org-element-parse-buffer))
  612. (info `(:parse-tree ,tree)))
  613. (org-export-resolve-radio-link
  614. (org-element-map tree 'link 'identity info t)
  615. info)))))
  616. ;;; Src-block and example-block
  617. (ert-deftest test-org-export/unravel-code ()
  618. "Test `org-export-unravel-code' function."
  619. (let ((org-coderef-label-format "(ref:%s)"))
  620. ;; 1. Code without reference.
  621. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  622. (should (equal (org-export-unravel-code (org-element-current-element))
  623. '("(+ 1 1)\n"))))
  624. ;; 2. Code with reference.
  625. (org-test-with-temp-text
  626. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  627. (should (equal (org-export-unravel-code (org-element-current-element))
  628. '("(+ 1 1)\n" (1 . "test")))))
  629. ;; 3. Code with user-defined reference.
  630. (org-test-with-temp-text
  631. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  632. (should (equal (org-export-unravel-code (org-element-current-element))
  633. '("(+ 1 1)\n" (1 . "test")))))
  634. ;; 4. Code references keys are relative to the current block.
  635. (org-test-with-temp-text "
  636. #+BEGIN_EXAMPLE -n
  637. \(+ 1 1)
  638. #+END_EXAMPLE
  639. #+BEGIN_EXAMPLE +n
  640. \(+ 2 2)
  641. \(+ 3 3) (ref:one)
  642. #+END_EXAMPLE"
  643. (goto-line 5)
  644. (should (equal (org-export-unravel-code (org-element-current-element))
  645. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
  646. ;; 5. Free up comma-protected lines.
  647. ;;
  648. ;; 5.1. In an Org source block, every line is protected.
  649. (org-test-with-temp-text
  650. "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
  651. (should (equal (org-export-unravel-code (org-element-current-element))
  652. '("* Test\n# comment\nText\n"))))
  653. ;; 5.2. In other blocks, only headlines, comments and keywords are
  654. ;; protected.
  655. (org-test-with-temp-text
  656. "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
  657. (should (equal (org-export-unravel-code (org-element-current-element))
  658. '("* Headline\n, * Not headline\n,Keep\n"))))))
  659. ;;; Tables
  660. (ert-deftest test-org-export/special-column ()
  661. "Test if the table's special column is properly recognized."
  662. ;; 1. First column is special if it contains only a special marking
  663. ;; characters or empty cells.
  664. (org-test-with-temp-text "
  665. | ! | 1 |
  666. | | 2 |"
  667. (should
  668. (org-export-table-has-special-column-p
  669. (org-element-map
  670. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  671. ;; 2. If the column contains anything else, it isn't special.
  672. (org-test-with-temp-text "
  673. | ! | 1 |
  674. | b | 2 |"
  675. (should-not
  676. (org-export-table-has-special-column-p
  677. (org-element-map
  678. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  679. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  680. ;; and "!".
  681. (org-test-with-temp-text "
  682. | # | 1 |
  683. | ^ | 2 |
  684. | * | 3 |
  685. | _ | 4 |
  686. | / | 5 |
  687. | $ | 6 |
  688. | ! | 7 |"
  689. (should
  690. (org-export-table-has-special-column-p
  691. (org-element-map
  692. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  693. ;; 4. A first column with only empty cells isn't considered as
  694. ;; special.
  695. (org-test-with-temp-text "
  696. | | 1 |
  697. | | 2 |"
  698. (should-not
  699. (org-export-table-has-special-column-p
  700. (org-element-map
  701. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  702. (ert-deftest test-org-export/table-row-is-special-p ()
  703. "Test `org-export-table-row-is-special-p' specifications."
  704. ;; 1. A row is special if it has a special marking character in the
  705. ;; special column.
  706. (org-test-with-parsed-data "| ! | 1 |"
  707. (should
  708. (org-export-table-row-is-special-p
  709. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  710. ;; 2. A row is special when its first field is "/"
  711. (org-test-with-parsed-data "
  712. | / | 1 |
  713. | a | b |"
  714. (should
  715. (org-export-table-row-is-special-p
  716. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  717. ;; 3. A row only containing alignment cookies is also considered as
  718. ;; special.
  719. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  720. (should
  721. (org-export-table-row-is-special-p
  722. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  723. ;; 4. Everything else isn't considered as special.
  724. (org-test-with-parsed-data "| \alpha | | c |"
  725. (should-not
  726. (org-export-table-row-is-special-p
  727. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  728. ;; 5. Table's rules are never considered as special rows.
  729. (org-test-with-parsed-data "|---+---|"
  730. (should-not
  731. (org-export-table-row-is-special-p
  732. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  733. (ert-deftest test-org-export/has-header-p ()
  734. "Test `org-export-table-has-header-p' specifications."
  735. ;; 1. With an header.
  736. (org-test-with-parsed-data "
  737. | a | b |
  738. |---+---|
  739. | c | d |"
  740. (should
  741. (org-export-table-has-header-p
  742. (org-element-map tree 'table 'identity info 'first-match)
  743. info)))
  744. ;; 2. Without an header.
  745. (org-test-with-parsed-data "
  746. | a | b |
  747. | c | d |"
  748. (should-not
  749. (org-export-table-has-header-p
  750. (org-element-map tree 'table 'identity info 'first-match)
  751. info)))
  752. ;; 3. Don't get fooled with starting and ending rules.
  753. (org-test-with-parsed-data "
  754. |---+---|
  755. | a | b |
  756. | c | d |
  757. |---+---|"
  758. (should-not
  759. (org-export-table-has-header-p
  760. (org-element-map tree 'table 'identity info 'first-match)
  761. info))))
  762. (ert-deftest test-org-export/table-row-group ()
  763. "Test `org-export-table-row-group' specifications."
  764. ;; 1. A rule creates a new group.
  765. (org-test-with-parsed-data "
  766. | a | b |
  767. |---+---|
  768. | 1 | 2 |"
  769. (should
  770. (equal
  771. '(1 nil 2)
  772. (mapcar (lambda (row) (org-export-table-row-group row info))
  773. (org-element-map tree 'table-row 'identity)))))
  774. ;; 2. Special rows are ignored in count.
  775. (org-test-with-parsed-data "
  776. | / | < | > |
  777. |---|---+---|
  778. | | 1 | 2 |"
  779. (should
  780. (equal
  781. '(nil nil 1)
  782. (mapcar (lambda (row) (org-export-table-row-group row info))
  783. (org-element-map tree 'table-row 'identity)))))
  784. ;; 3. Double rules also are ignored in count.
  785. (org-test-with-parsed-data "
  786. | a | b |
  787. |---+---|
  788. |---+---|
  789. | 1 | 2 |"
  790. (should
  791. (equal
  792. '(1 nil nil 2)
  793. (mapcar (lambda (row) (org-export-table-row-group row info))
  794. (org-element-map tree 'table-row 'identity))))))
  795. (ert-deftest test-org-export/table-cell-width ()
  796. "Test `org-export-table-cell-width' specifications."
  797. ;; 1. Width is primarily determined by width cookies. If no cookie
  798. ;; is found, cell's width is nil.
  799. (org-test-with-parsed-data "
  800. | / | <l> | <6> | <l7> |
  801. | | a | b | c |"
  802. (should
  803. (equal
  804. '(nil 6 7)
  805. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  806. (org-element-map tree 'table-cell 'identity info)))))
  807. ;; 2. The last width cookie has precedence.
  808. (org-test-with-parsed-data "
  809. | <6> |
  810. | <7> |
  811. | a |"
  812. (should
  813. (equal
  814. '(7)
  815. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  816. (org-element-map tree 'table-cell 'identity info)))))
  817. ;; 3. Valid width cookies must have a specific row.
  818. (org-test-with-parsed-data "| <6> | cell |"
  819. (should
  820. (equal
  821. '(nil nil)
  822. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  823. (org-element-map tree 'table-cell 'identity))))))
  824. (ert-deftest test-org-export/table-cell-alignment ()
  825. "Test `org-export-table-cell-alignment' specifications."
  826. (let ((org-table-number-fraction 0.5)
  827. (org-table-number-regexp "^[0-9]+$"))
  828. ;; 1. Alignment is primarily determined by alignment cookies.
  829. (org-test-with-temp-text "| <l> | <c> | <r> |"
  830. (let* ((tree (org-element-parse-buffer))
  831. (info `(:parse-tree ,tree)))
  832. (should
  833. (equal
  834. '(left center right)
  835. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  836. (org-element-map tree 'table-cell 'identity))))))
  837. ;; 2. The last alignment cookie has precedence.
  838. (org-test-with-parsed-data "
  839. | <l8> |
  840. | cell |
  841. | <r9> |"
  842. (should
  843. (equal
  844. '(right right right)
  845. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  846. (org-element-map tree 'table-cell 'identity)))))
  847. ;; 3. If there's no cookie, cell's contents determine alignment.
  848. ;; A column mostly made of cells containing numbers will align
  849. ;; its cells to the right.
  850. (org-test-with-parsed-data "
  851. | 123 |
  852. | some text |
  853. | 12345 |"
  854. (should
  855. (equal
  856. '(right right right)
  857. (mapcar (lambda (cell)
  858. (org-export-table-cell-alignment cell info))
  859. (org-element-map tree 'table-cell 'identity)))))
  860. ;; 4. Otherwise, they will be aligned to the left.
  861. (org-test-with-parsed-data "
  862. | text |
  863. | some text |
  864. | \alpha |"
  865. (should
  866. (equal
  867. '(left left left)
  868. (mapcar (lambda (cell)
  869. (org-export-table-cell-alignment cell info))
  870. (org-element-map tree 'table-cell 'identity)))))))
  871. (ert-deftest test-org-export/table-cell-borders ()
  872. "Test `org-export-table-cell-borders' specifications."
  873. ;; 1. Recognize various column groups indicators.
  874. (org-test-with-parsed-data "| / | < | > | <> |"
  875. (should
  876. (equal
  877. '((right bottom top) (left bottom top) (right bottom top)
  878. (right left bottom top))
  879. (mapcar (lambda (cell)
  880. (org-export-table-cell-borders cell info))
  881. (org-element-map tree 'table-cell 'identity)))))
  882. ;; 2. Accept shortcuts to define column groups.
  883. (org-test-with-parsed-data "| / | < | < |"
  884. (should
  885. (equal
  886. '((right bottom top) (right left bottom top) (left bottom top))
  887. (mapcar (lambda (cell)
  888. (org-export-table-cell-borders cell info))
  889. (org-element-map tree 'table-cell 'identity)))))
  890. ;; 3. A valid column groups row must start with a "/".
  891. (org-test-with-parsed-data "
  892. | | < |
  893. | a | b |"
  894. (should
  895. (equal '((top) (top) (bottom) (bottom))
  896. (mapcar (lambda (cell)
  897. (org-export-table-cell-borders cell info))
  898. (org-element-map tree 'table-cell 'identity)))))
  899. ;; 4. Take table rules into consideration.
  900. (org-test-with-parsed-data "
  901. | 1 |
  902. |---|
  903. | 2 |"
  904. (should
  905. (equal '((below top) (bottom above))
  906. (mapcar (lambda (cell)
  907. (org-export-table-cell-borders cell info))
  908. (org-element-map tree 'table-cell 'identity)))))
  909. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  910. ;; (resp. `bottom' and `below') borders. Any special row is
  911. ;; ignored.
  912. (org-test-with-parsed-data "
  913. |---+----|
  914. | / | |
  915. | | 1 |
  916. |---+----|"
  917. (should
  918. (equal '((bottom below top above))
  919. (last
  920. (mapcar (lambda (cell)
  921. (org-export-table-cell-borders cell info))
  922. (org-element-map tree 'table-cell 'identity)))))))
  923. (ert-deftest test-org-export/table-dimensions ()
  924. "Test `org-export-table-dimensions' specifications."
  925. ;; 1. Standard test.
  926. (org-test-with-parsed-data "
  927. | 1 | 2 | 3 |
  928. | 4 | 5 | 6 |"
  929. (should
  930. (equal '(2 . 3)
  931. (org-export-table-dimensions
  932. (org-element-map tree 'table 'identity info 'first-match) info))))
  933. ;; 2. Ignore horizontal rules and special columns.
  934. (org-test-with-parsed-data "
  935. | / | < | > |
  936. | 1 | 2 | 3 |
  937. |---+---+---|
  938. | 4 | 5 | 6 |"
  939. (should
  940. (equal '(2 . 3)
  941. (org-export-table-dimensions
  942. (org-element-map tree 'table 'identity info 'first-match) info)))))
  943. (ert-deftest test-org-export/table-cell-address ()
  944. "Test `org-export-table-cell-address' specifications."
  945. ;; 1. Standard test: index is 0-based.
  946. (org-test-with-parsed-data "| a | b |"
  947. (should
  948. (equal '((0 . 0) (0 . 1))
  949. (org-element-map
  950. tree 'table-cell
  951. (lambda (cell) (org-export-table-cell-address cell info))
  952. info))))
  953. ;; 2. Special column isn't counted, nor are special rows.
  954. (org-test-with-parsed-data "
  955. | / | <> |
  956. | | c |"
  957. (should
  958. (equal '(0 . 0)
  959. (org-export-table-cell-address
  960. (car (last (org-element-map tree 'table-cell 'identity info)))
  961. info))))
  962. ;; 3. Tables rules do not count either.
  963. (org-test-with-parsed-data "
  964. | a |
  965. |---|
  966. | b |
  967. |---|
  968. | c |"
  969. (should
  970. (equal '(2 . 0)
  971. (org-export-table-cell-address
  972. (car (last (org-element-map tree 'table-cell 'identity info)))
  973. info))))
  974. ;; 4. Return nil for special cells.
  975. (org-test-with-parsed-data "| / | a |"
  976. (should-not
  977. (org-export-table-cell-address
  978. (org-element-map tree 'table-cell 'identity nil 'first-match)
  979. info))))
  980. (ert-deftest test-org-export/get-table-cell-at ()
  981. "Test `org-export-get-table-cell-at' specifications."
  982. ;; 1. Address ignores special columns, special rows and rules.
  983. (org-test-with-parsed-data "
  984. | / | <> |
  985. | | a |
  986. |---+----|
  987. | | b |"
  988. (should
  989. (equal '("b")
  990. (org-element-contents
  991. (org-export-get-table-cell-at
  992. '(1 . 0)
  993. (org-element-map tree 'table 'identity info 'first-match)
  994. info)))))
  995. ;; 2. Return value for a non-existent address is nil.
  996. (org-test-with-parsed-data "| a |"
  997. (should-not
  998. (org-export-get-table-cell-at
  999. '(2 . 2)
  1000. (org-element-map tree 'table 'identity info 'first-match)
  1001. info)))
  1002. (org-test-with-parsed-data "| / |"
  1003. (should-not
  1004. (org-export-get-table-cell-at
  1005. '(0 . 0)
  1006. (org-element-map tree 'table 'identity info 'first-match)
  1007. info))))
  1008. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1009. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1010. ;; 1. A cell at a beginning of a row always starts a column group.
  1011. (org-test-with-parsed-data "| a |"
  1012. (should
  1013. (org-export-table-cell-starts-colgroup-p
  1014. (org-element-map tree 'table-cell 'identity info 'first-match)
  1015. info)))
  1016. ;; 2. Special column should be ignored when determining the
  1017. ;; beginning of the row.
  1018. (org-test-with-parsed-data "
  1019. | / | |
  1020. | | a |"
  1021. (should
  1022. (org-export-table-cell-starts-colgroup-p
  1023. (org-element-map tree 'table-cell 'identity info 'first-match)
  1024. info)))
  1025. ;; 2. Explicit column groups.
  1026. (org-test-with-parsed-data "
  1027. | / | | < |
  1028. | a | b | c |"
  1029. (should
  1030. (equal
  1031. '(yes no yes)
  1032. (org-element-map
  1033. tree 'table-cell
  1034. (lambda (cell)
  1035. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1036. info)))))
  1037. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1038. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1039. ;; 1. A cell at the end of a row always ends a column group.
  1040. (org-test-with-parsed-data "| a |"
  1041. (should
  1042. (org-export-table-cell-ends-colgroup-p
  1043. (org-element-map tree 'table-cell 'identity info 'first-match)
  1044. info)))
  1045. ;; 2. Special column should be ignored when determining the
  1046. ;; beginning of the row.
  1047. (org-test-with-parsed-data "
  1048. | / | |
  1049. | | a |"
  1050. (should
  1051. (org-export-table-cell-ends-colgroup-p
  1052. (org-element-map tree 'table-cell 'identity info 'first-match)
  1053. info)))
  1054. ;; 3. Explicit column groups.
  1055. (org-test-with-parsed-data "
  1056. | / | < | |
  1057. | a | b | c |"
  1058. (should
  1059. (equal
  1060. '(yes no yes)
  1061. (org-element-map
  1062. tree 'table-cell
  1063. (lambda (cell)
  1064. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1065. info)))))
  1066. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1067. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1068. ;; 1. A row at the beginning of a table always starts a row group.
  1069. ;; So does a row following a table rule.
  1070. (org-test-with-parsed-data "
  1071. | a |
  1072. |---|
  1073. | b |"
  1074. (should
  1075. (equal
  1076. '(yes no yes)
  1077. (org-element-map
  1078. tree 'table-row
  1079. (lambda (row)
  1080. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1081. info))))
  1082. ;; 2. Special rows should be ignored when determining the beginning
  1083. ;; of the row.
  1084. (org-test-with-parsed-data "
  1085. | / | < |
  1086. | | a |
  1087. |---+---|
  1088. | / | < |
  1089. | | b |"
  1090. (should
  1091. (equal
  1092. '(yes no yes)
  1093. (org-element-map
  1094. tree 'table-row
  1095. (lambda (row)
  1096. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1097. info)))))
  1098. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1099. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1100. ;; 1. A row at the end of a table always ends a row group. So does
  1101. ;; a row preceding a table rule.
  1102. (org-test-with-parsed-data "
  1103. | a |
  1104. |---|
  1105. | b |"
  1106. (should
  1107. (equal
  1108. '(yes no yes)
  1109. (org-element-map
  1110. tree 'table-row
  1111. (lambda (row)
  1112. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1113. info))))
  1114. ;; 2. Special rows should be ignored when determining the beginning
  1115. ;; of the row.
  1116. (org-test-with-parsed-data "
  1117. | | a |
  1118. | / | < |
  1119. |---+---|
  1120. | | b |
  1121. | / | < |"
  1122. (should
  1123. (equal
  1124. '(yes no yes)
  1125. (org-element-map
  1126. tree 'table-row
  1127. (lambda (row)
  1128. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1129. info)))))
  1130. (ert-deftest test-org-export/table-row-starts-header-p ()
  1131. "Test `org-export-table-row-starts-header-p' specifications."
  1132. ;; 1. Only the row starting the first row group starts the table
  1133. ;; header.
  1134. (org-test-with-parsed-data "
  1135. | a |
  1136. | b |
  1137. |---|
  1138. | c |"
  1139. (should
  1140. (equal
  1141. '(yes no no no)
  1142. (org-element-map
  1143. tree 'table-row
  1144. (lambda (row)
  1145. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1146. info))))
  1147. ;; 2. A row cannot start an header if there's no header in the
  1148. ;; table.
  1149. (org-test-with-parsed-data "
  1150. | a |
  1151. |---|"
  1152. (should-not
  1153. (org-export-table-row-starts-header-p
  1154. (org-element-map tree 'table-row 'identity info 'first-match)
  1155. info))))
  1156. (ert-deftest test-org-export/table-row-ends-header-p ()
  1157. "Test `org-export-table-row-ends-header-p' specifications."
  1158. ;; 1. Only the row starting the first row group starts the table
  1159. ;; header.
  1160. (org-test-with-parsed-data "
  1161. | a |
  1162. | b |
  1163. |---|
  1164. | c |"
  1165. (should
  1166. (equal
  1167. '(no yes no no)
  1168. (org-element-map
  1169. tree 'table-row
  1170. (lambda (row)
  1171. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1172. info))))
  1173. ;; 2. A row cannot start an header if there's no header in the
  1174. ;; table.
  1175. (org-test-with-parsed-data "
  1176. | a |
  1177. |---|"
  1178. (should-not
  1179. (org-export-table-row-ends-header-p
  1180. (org-element-map tree 'table-row 'identity info 'first-match)
  1181. info))))
  1182. (provide 'test-org-export)
  1183. ;;; test-org-export.el end here