test-org-export.el 39 KB

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