test-org-export.el 43 KB

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