test-org-export.el 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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/fuzzy-link ()
  440. "Test fuzzy links specifications."
  441. ;; 1. Links to invisible (keyword) targets should be ignored.
  442. (org-test-with-parsed-data
  443. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  444. (should-not
  445. (org-element-map
  446. tree 'link
  447. (lambda (link)
  448. (org-export-get-ordinal
  449. (org-export-resolve-fuzzy-link link info) info)) info)))
  450. ;; 2. Link to an headline should return headline's number.
  451. (org-test-with-parsed-data
  452. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  453. (should
  454. ;; Note: Headline's number is in fact a list of numbers.
  455. (equal '(2)
  456. (org-element-map
  457. tree 'link
  458. (lambda (link)
  459. (org-export-get-ordinal
  460. (org-export-resolve-fuzzy-link link info) info)) info t))))
  461. ;; 3. Link to a target in an item should return item's number.
  462. (org-test-with-parsed-data
  463. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  464. (should
  465. ;; Note: Item's number is in fact a list of numbers.
  466. (equal '(1 2)
  467. (org-element-map
  468. tree 'link
  469. (lambda (link)
  470. (org-export-get-ordinal
  471. (org-export-resolve-fuzzy-link link info) info)) info t))))
  472. ;; 4. Link to a target in a footnote should return footnote's
  473. ;; number.
  474. (org-test-with-parsed-data "
  475. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  476. (should
  477. (equal '(2 3)
  478. (org-element-map
  479. tree 'link
  480. (lambda (link)
  481. (org-export-get-ordinal
  482. (org-export-resolve-fuzzy-link link info) info)) info))))
  483. ;; 5. Link to a named element should return sequence number of that
  484. ;; element.
  485. (org-test-with-parsed-data
  486. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  487. (should
  488. (= 2
  489. (org-element-map
  490. tree 'link
  491. (lambda (link)
  492. (org-export-get-ordinal
  493. (org-export-resolve-fuzzy-link link info) info)) info t))))
  494. ;; 6. Link to a target not within an item, a table, a footnote
  495. ;; reference or definition should return section number.
  496. (org-test-with-parsed-data
  497. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  498. (should
  499. (equal '(2)
  500. (org-element-map
  501. tree 'link
  502. (lambda (link)
  503. (org-export-get-ordinal
  504. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  505. (ert-deftest test-org-export/resolve-coderef ()
  506. "Test `org-export-resolve-coderef' specifications."
  507. (let ((org-coderef-label-format "(ref:%s)"))
  508. ;; 1. A link to a "-n -k -r" block returns line number.
  509. (org-test-with-parsed-data
  510. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  511. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  512. (org-test-with-parsed-data
  513. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  514. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  515. ;; 2. A link to a "-n -r" block returns line number.
  516. (org-test-with-parsed-data
  517. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  518. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  519. (org-test-with-parsed-data
  520. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  521. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  522. ;; 3. A link to a "-n" block returns coderef.
  523. (org-test-with-parsed-data
  524. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  525. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  526. (org-test-with-parsed-data
  527. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  528. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  529. ;; 4. A link to a "-r" block returns line number.
  530. (org-test-with-parsed-data
  531. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  532. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  533. (org-test-with-parsed-data
  534. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  535. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  536. ;; 5. A link to a block without a switch returns coderef.
  537. (org-test-with-parsed-data
  538. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  539. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  540. (org-test-with-parsed-data
  541. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  542. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  543. ;; 6. Correctly handle continued line numbers. A "+n" switch
  544. ;; should resume numbering from previous block with numbered
  545. ;; lines, ignoring blocks not numbering lines in the process.
  546. ;; A "-n" switch resets count.
  547. (org-test-with-parsed-data "
  548. #+BEGIN_EXAMPLE -n
  549. Text.
  550. #+END_EXAMPLE
  551. #+BEGIN_SRC emacs-lisp
  552. \(- 1 1)
  553. #+END_SRC
  554. #+BEGIN_SRC emacs-lisp +n -r
  555. \(+ 1 1) (ref:addition)
  556. #+END_SRC
  557. #+BEGIN_EXAMPLE -n -r
  558. Another text. (ref:text)
  559. #+END_EXAMPLE"
  560. (should (= (org-export-resolve-coderef "addition" info) 2))
  561. (should (= (org-export-resolve-coderef "text" info) 1)))
  562. ;; 7. Recognize coderef with user-specified syntax.
  563. (org-test-with-parsed-data
  564. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  565. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  566. (ert-deftest test-org-exprot/resolve-fuzzy-link ()
  567. "Test `org-export-resolve-fuzzy-link' specifications."
  568. ;; 1. Match target objects.
  569. (org-test-with-parsed-data "<<target>> [[target]]"
  570. (should
  571. (org-export-resolve-fuzzy-link
  572. (org-element-map tree 'link 'identity info t) info)))
  573. ;; 2. Match target elements.
  574. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  575. (should
  576. (org-export-resolve-fuzzy-link
  577. (org-element-map tree 'link 'identity info t) info)))
  578. ;; 3. Match named elements.
  579. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  580. (should
  581. (org-export-resolve-fuzzy-link
  582. (org-element-map tree 'link 'identity info t) info)))
  583. ;; 4. Match exact headline's name.
  584. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  585. (should
  586. (org-export-resolve-fuzzy-link
  587. (org-element-map tree 'link 'identity info t) info)))
  588. ;; 5. Targets objects have priority over named elements and headline
  589. ;; titles.
  590. (org-test-with-parsed-data
  591. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  592. (should
  593. (eq 'target
  594. (org-element-type
  595. (org-export-resolve-fuzzy-link
  596. (org-element-map tree 'link 'identity info t) info)))))
  597. ;; 6. Named elements have priority over headline titles.
  598. (org-test-with-parsed-data
  599. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  600. (should
  601. (eq 'paragraph
  602. (org-element-type
  603. (org-export-resolve-fuzzy-link
  604. (org-element-map tree 'link 'identity info t) info)))))
  605. ;; 7. If link's path starts with a "*", only match headline titles,
  606. ;; though.
  607. (org-test-with-parsed-data
  608. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  609. (should
  610. (eq 'headline
  611. (org-element-type
  612. (org-export-resolve-fuzzy-link
  613. (org-element-map tree 'link 'identity info t) info)))))
  614. ;; 8. Return nil if no match.
  615. (org-test-with-parsed-data "[[target]]"
  616. (should-not
  617. (org-export-resolve-fuzzy-link
  618. (org-element-map tree 'link 'identity info t) info))))
  619. (ert-deftest test-org-export/resolve-id-link ()
  620. "Test `org-export-resolve-id-link' specifications."
  621. ;; 1. Regular test for custom-id link.
  622. (org-test-with-parsed-data "* Headline1
  623. :PROPERTIES:
  624. :CUSTOM-ID: test
  625. :END:
  626. * Headline 2
  627. \[[#test]]"
  628. (should
  629. (org-export-resolve-id-link
  630. (org-element-map tree 'link 'identity info t) info)))
  631. ;; 2. Failing test for custom-id link.
  632. (org-test-with-parsed-data "* Headline1
  633. :PROPERTIES:
  634. :CUSTOM-ID: test
  635. :END:
  636. * Headline 2
  637. \[[#no-match]]"
  638. (should-not
  639. (org-export-resolve-id-link
  640. (org-element-map tree 'link 'identity info t) info)))
  641. ;; 3. Test for internal id target.
  642. (org-test-with-parsed-data "* Headline1
  643. :PROPERTIES:
  644. :ID: aaaa
  645. :END:
  646. * Headline 2
  647. \[[id:aaaa]]"
  648. (should
  649. (org-export-resolve-id-link
  650. (org-element-map tree 'link 'identity info t) info)))
  651. ;; 4. Test for external id target.
  652. (org-test-with-parsed-data "[[id:aaaa]]"
  653. (should
  654. (org-export-resolve-id-link
  655. (org-element-map tree 'link 'identity info t)
  656. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  657. (ert-deftest test-org-export/resolve-radio-link ()
  658. "Test `org-export-resolve-radio-link' specifications."
  659. ;; Standard test.
  660. (org-test-with-temp-text "<<<radio>>> radio"
  661. (org-update-radio-target-regexp)
  662. (should
  663. (let* ((tree (org-element-parse-buffer))
  664. (info `(:parse-tree ,tree)))
  665. (org-export-resolve-radio-link
  666. (org-element-map tree 'link 'identity info t)
  667. info))))
  668. ;; Radio target with objects.
  669. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  670. (org-update-radio-target-regexp)
  671. (should
  672. (let* ((tree (org-element-parse-buffer))
  673. (info `(:parse-tree ,tree)))
  674. (org-export-resolve-radio-link
  675. (org-element-map tree 'link 'identity info t)
  676. info)))))
  677. ;;; Macro
  678. (ert-deftest test-org-export/define-macro ()
  679. "Try defining various Org macro using in-buffer #+MACRO: keyword."
  680. ;; Parsed macro.
  681. (should (equal (org-test-with-temp-text "#+MACRO: one 1"
  682. (org-export-get-inbuffer-options))
  683. '(:macro-one ("1"))))
  684. ;; Evaled macro.
  685. (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
  686. (org-export-get-inbuffer-options))
  687. '(:macro-two ("(eval (+ 1 1))"))))
  688. ;; Incomplete macro.
  689. (should-not (org-test-with-temp-text "#+MACRO: three"
  690. (org-export-get-inbuffer-options)))
  691. ;; Macro with newline character.
  692. (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
  693. (org-export-get-inbuffer-options))
  694. '(:macro-four ("a\nb"))))
  695. ;; Macro with protected newline character.
  696. (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
  697. (org-export-get-inbuffer-options))
  698. '(:macro-five ("a\\nb"))))
  699. ;; Recursive macro.
  700. (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
  701. (should
  702. (equal
  703. (org-export-get-inbuffer-options)
  704. '(:macro-six
  705. ("6")
  706. :macro-seven
  707. ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
  708. :post-blank 0))))))))
  709. (ert-deftest test-org-export/expand-macro ()
  710. "Test `org-export-expand-macro' specifications."
  711. ;; Standard test.
  712. (should
  713. (equal
  714. "some text"
  715. (org-test-with-parsed-data "#+MACRO: macro some text\n{{{macro}}}"
  716. (org-export-expand-macro
  717. (org-element-map tree 'macro 'identity info t) info))))
  718. ;; Macro with arguments.
  719. (should
  720. (equal
  721. "some text"
  722. (org-test-with-parsed-data "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
  723. (org-export-expand-macro
  724. (org-element-map tree 'macro 'identity info t) info))))
  725. ;; Macro with "eval"
  726. (should
  727. (equal
  728. "3"
  729. (org-test-with-parsed-data "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
  730. (org-export-expand-macro
  731. (org-element-map tree 'macro 'identity info t) info))))
  732. ;; Nested macros.
  733. (should
  734. (equal
  735. "inner outer"
  736. (org-test-with-parsed-data
  737. "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
  738. (flet ((translate-macro (macro contents info)
  739. (org-export-expand-macro macro info)))
  740. (org-export-expand-macro
  741. (org-element-map tree 'macro 'identity info t)
  742. (org-combine-plists
  743. info `(:translate-alist ((macro . translate-macro))))))))))
  744. ;;; Src-block and example-block
  745. (ert-deftest test-org-export/unravel-code ()
  746. "Test `org-export-unravel-code' function."
  747. (let ((org-coderef-label-format "(ref:%s)"))
  748. ;; 1. Code without reference.
  749. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  750. (should (equal (org-export-unravel-code (org-element-current-element))
  751. '("(+ 1 1)\n"))))
  752. ;; 2. Code with reference.
  753. (org-test-with-temp-text
  754. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  755. (should (equal (org-export-unravel-code (org-element-current-element))
  756. '("(+ 1 1)\n" (1 . "test")))))
  757. ;; 3. Code with user-defined reference.
  758. (org-test-with-temp-text
  759. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  760. (should (equal (org-export-unravel-code (org-element-current-element))
  761. '("(+ 1 1)\n" (1 . "test")))))
  762. ;; 4. Code references keys are relative to the current block.
  763. (org-test-with-temp-text "
  764. #+BEGIN_EXAMPLE -n
  765. \(+ 1 1)
  766. #+END_EXAMPLE
  767. #+BEGIN_EXAMPLE +n
  768. \(+ 2 2)
  769. \(+ 3 3) (ref:one)
  770. #+END_EXAMPLE"
  771. (goto-line 5)
  772. (should (equal (org-export-unravel-code (org-element-current-element))
  773. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
  774. ;; 5. Free up comma-protected lines.
  775. ;;
  776. ;; 5.1. In an Org source block, every line is protected.
  777. (org-test-with-temp-text
  778. "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
  779. (should (equal (org-export-unravel-code (org-element-current-element))
  780. '("* Test\n# comment\nText\n"))))
  781. ;; 5.2. In other blocks, only headlines, comments and keywords are
  782. ;; protected.
  783. (org-test-with-temp-text
  784. "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
  785. (should (equal (org-export-unravel-code (org-element-current-element))
  786. '("* Headline\n, * Not headline\n,Keep\n"))))))
  787. ;;; Tables
  788. (ert-deftest test-org-export/special-column ()
  789. "Test if the table's special column is properly recognized."
  790. ;; 1. First column is special if it contains only a special marking
  791. ;; characters or empty cells.
  792. (org-test-with-temp-text "
  793. | ! | 1 |
  794. | | 2 |"
  795. (should
  796. (org-export-table-has-special-column-p
  797. (org-element-map
  798. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  799. ;; 2. If the column contains anything else, it isn't special.
  800. (org-test-with-temp-text "
  801. | ! | 1 |
  802. | b | 2 |"
  803. (should-not
  804. (org-export-table-has-special-column-p
  805. (org-element-map
  806. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  807. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  808. ;; and "!".
  809. (org-test-with-temp-text "
  810. | # | 1 |
  811. | ^ | 2 |
  812. | * | 3 |
  813. | _ | 4 |
  814. | / | 5 |
  815. | $ | 6 |
  816. | ! | 7 |"
  817. (should
  818. (org-export-table-has-special-column-p
  819. (org-element-map
  820. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  821. ;; 4. A first column with only empty cells isn't considered as
  822. ;; special.
  823. (org-test-with-temp-text "
  824. | | 1 |
  825. | | 2 |"
  826. (should-not
  827. (org-export-table-has-special-column-p
  828. (org-element-map
  829. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  830. (ert-deftest test-org-export/table-row-is-special-p ()
  831. "Test `org-export-table-row-is-special-p' specifications."
  832. ;; 1. A row is special if it has a special marking character in the
  833. ;; special column.
  834. (org-test-with-parsed-data "| ! | 1 |"
  835. (should
  836. (org-export-table-row-is-special-p
  837. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  838. ;; 2. A row is special when its first field is "/"
  839. (org-test-with-parsed-data "
  840. | / | 1 |
  841. | a | b |"
  842. (should
  843. (org-export-table-row-is-special-p
  844. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  845. ;; 3. A row only containing alignment cookies is also considered as
  846. ;; special.
  847. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  848. (should
  849. (org-export-table-row-is-special-p
  850. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  851. ;; 4. Everything else isn't considered as special.
  852. (org-test-with-parsed-data "| \alpha | | c |"
  853. (should-not
  854. (org-export-table-row-is-special-p
  855. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  856. ;; 5. Table's rules are never considered as special rows.
  857. (org-test-with-parsed-data "|---+---|"
  858. (should-not
  859. (org-export-table-row-is-special-p
  860. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  861. (ert-deftest test-org-export/has-header-p ()
  862. "Test `org-export-table-has-header-p' specifications."
  863. ;; 1. With an header.
  864. (org-test-with-parsed-data "
  865. | a | b |
  866. |---+---|
  867. | c | d |"
  868. (should
  869. (org-export-table-has-header-p
  870. (org-element-map tree 'table 'identity info 'first-match)
  871. info)))
  872. ;; 2. Without an header.
  873. (org-test-with-parsed-data "
  874. | a | b |
  875. | c | d |"
  876. (should-not
  877. (org-export-table-has-header-p
  878. (org-element-map tree 'table 'identity info 'first-match)
  879. info)))
  880. ;; 3. Don't get fooled with starting and ending rules.
  881. (org-test-with-parsed-data "
  882. |---+---|
  883. | a | b |
  884. | c | d |
  885. |---+---|"
  886. (should-not
  887. (org-export-table-has-header-p
  888. (org-element-map tree 'table 'identity info 'first-match)
  889. info))))
  890. (ert-deftest test-org-export/table-row-group ()
  891. "Test `org-export-table-row-group' specifications."
  892. ;; 1. A rule creates a new group.
  893. (org-test-with-parsed-data "
  894. | a | b |
  895. |---+---|
  896. | 1 | 2 |"
  897. (should
  898. (equal
  899. '(1 nil 2)
  900. (mapcar (lambda (row) (org-export-table-row-group row info))
  901. (org-element-map tree 'table-row 'identity)))))
  902. ;; 2. Special rows are ignored in count.
  903. (org-test-with-parsed-data "
  904. | / | < | > |
  905. |---|---+---|
  906. | | 1 | 2 |"
  907. (should
  908. (equal
  909. '(nil nil 1)
  910. (mapcar (lambda (row) (org-export-table-row-group row info))
  911. (org-element-map tree 'table-row 'identity)))))
  912. ;; 3. Double rules also are ignored in count.
  913. (org-test-with-parsed-data "
  914. | a | b |
  915. |---+---|
  916. |---+---|
  917. | 1 | 2 |"
  918. (should
  919. (equal
  920. '(1 nil nil 2)
  921. (mapcar (lambda (row) (org-export-table-row-group row info))
  922. (org-element-map tree 'table-row 'identity))))))
  923. (ert-deftest test-org-export/table-cell-width ()
  924. "Test `org-export-table-cell-width' specifications."
  925. ;; 1. Width is primarily determined by width cookies. If no cookie
  926. ;; is found, cell's width is nil.
  927. (org-test-with-parsed-data "
  928. | / | <l> | <6> | <l7> |
  929. | | a | b | c |"
  930. (should
  931. (equal
  932. '(nil 6 7)
  933. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  934. (org-element-map tree 'table-cell 'identity info)))))
  935. ;; 2. The last width cookie has precedence.
  936. (org-test-with-parsed-data "
  937. | <6> |
  938. | <7> |
  939. | a |"
  940. (should
  941. (equal
  942. '(7)
  943. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  944. (org-element-map tree 'table-cell 'identity info)))))
  945. ;; 3. Valid width cookies must have a specific row.
  946. (org-test-with-parsed-data "| <6> | cell |"
  947. (should
  948. (equal
  949. '(nil nil)
  950. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  951. (org-element-map tree 'table-cell 'identity))))))
  952. (ert-deftest test-org-export/table-cell-alignment ()
  953. "Test `org-export-table-cell-alignment' specifications."
  954. (let ((org-table-number-fraction 0.5)
  955. (org-table-number-regexp "^[0-9]+$"))
  956. ;; 1. Alignment is primarily determined by alignment cookies.
  957. (org-test-with-temp-text "| <l> | <c> | <r> |"
  958. (let* ((tree (org-element-parse-buffer))
  959. (info `(:parse-tree ,tree)))
  960. (should
  961. (equal
  962. '(left center right)
  963. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  964. (org-element-map tree 'table-cell 'identity))))))
  965. ;; 2. The last alignment cookie has precedence.
  966. (org-test-with-parsed-data "
  967. | <l8> |
  968. | cell |
  969. | <r9> |"
  970. (should
  971. (equal
  972. '(right right right)
  973. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  974. (org-element-map tree 'table-cell 'identity)))))
  975. ;; 3. If there's no cookie, cell's contents determine alignment.
  976. ;; A column mostly made of cells containing numbers will align
  977. ;; its cells to the right.
  978. (org-test-with-parsed-data "
  979. | 123 |
  980. | some text |
  981. | 12345 |"
  982. (should
  983. (equal
  984. '(right right right)
  985. (mapcar (lambda (cell)
  986. (org-export-table-cell-alignment cell info))
  987. (org-element-map tree 'table-cell 'identity)))))
  988. ;; 4. Otherwise, they will be aligned to the left.
  989. (org-test-with-parsed-data "
  990. | text |
  991. | some text |
  992. | \alpha |"
  993. (should
  994. (equal
  995. '(left left left)
  996. (mapcar (lambda (cell)
  997. (org-export-table-cell-alignment cell info))
  998. (org-element-map tree 'table-cell 'identity)))))))
  999. (ert-deftest test-org-export/table-cell-borders ()
  1000. "Test `org-export-table-cell-borders' specifications."
  1001. ;; 1. Recognize various column groups indicators.
  1002. (org-test-with-parsed-data "| / | < | > | <> |"
  1003. (should
  1004. (equal
  1005. '((right bottom top) (left bottom top) (right bottom top)
  1006. (right left bottom top))
  1007. (mapcar (lambda (cell)
  1008. (org-export-table-cell-borders cell info))
  1009. (org-element-map tree 'table-cell 'identity)))))
  1010. ;; 2. Accept shortcuts to define column groups.
  1011. (org-test-with-parsed-data "| / | < | < |"
  1012. (should
  1013. (equal
  1014. '((right bottom top) (right left bottom top) (left bottom top))
  1015. (mapcar (lambda (cell)
  1016. (org-export-table-cell-borders cell info))
  1017. (org-element-map tree 'table-cell 'identity)))))
  1018. ;; 3. A valid column groups row must start with a "/".
  1019. (org-test-with-parsed-data "
  1020. | | < |
  1021. | a | b |"
  1022. (should
  1023. (equal '((top) (top) (bottom) (bottom))
  1024. (mapcar (lambda (cell)
  1025. (org-export-table-cell-borders cell info))
  1026. (org-element-map tree 'table-cell 'identity)))))
  1027. ;; 4. Take table rules into consideration.
  1028. (org-test-with-parsed-data "
  1029. | 1 |
  1030. |---|
  1031. | 2 |"
  1032. (should
  1033. (equal '((below top) (bottom above))
  1034. (mapcar (lambda (cell)
  1035. (org-export-table-cell-borders cell info))
  1036. (org-element-map tree 'table-cell 'identity)))))
  1037. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1038. ;; (resp. `bottom' and `below') borders. Any special row is
  1039. ;; ignored.
  1040. (org-test-with-parsed-data "
  1041. |---+----|
  1042. | / | |
  1043. | | 1 |
  1044. |---+----|"
  1045. (should
  1046. (equal '((bottom below top above))
  1047. (last
  1048. (mapcar (lambda (cell)
  1049. (org-export-table-cell-borders cell info))
  1050. (org-element-map tree 'table-cell 'identity)))))))
  1051. (ert-deftest test-org-export/table-dimensions ()
  1052. "Test `org-export-table-dimensions' specifications."
  1053. ;; 1. Standard test.
  1054. (org-test-with-parsed-data "
  1055. | 1 | 2 | 3 |
  1056. | 4 | 5 | 6 |"
  1057. (should
  1058. (equal '(2 . 3)
  1059. (org-export-table-dimensions
  1060. (org-element-map tree 'table 'identity info 'first-match) info))))
  1061. ;; 2. Ignore horizontal rules and special columns.
  1062. (org-test-with-parsed-data "
  1063. | / | < | > |
  1064. | 1 | 2 | 3 |
  1065. |---+---+---|
  1066. | 4 | 5 | 6 |"
  1067. (should
  1068. (equal '(2 . 3)
  1069. (org-export-table-dimensions
  1070. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1071. (ert-deftest test-org-export/table-cell-address ()
  1072. "Test `org-export-table-cell-address' specifications."
  1073. ;; 1. Standard test: index is 0-based.
  1074. (org-test-with-parsed-data "| a | b |"
  1075. (should
  1076. (equal '((0 . 0) (0 . 1))
  1077. (org-element-map
  1078. tree 'table-cell
  1079. (lambda (cell) (org-export-table-cell-address cell info))
  1080. info))))
  1081. ;; 2. Special column isn't counted, nor are special rows.
  1082. (org-test-with-parsed-data "
  1083. | / | <> |
  1084. | | c |"
  1085. (should
  1086. (equal '(0 . 0)
  1087. (org-export-table-cell-address
  1088. (car (last (org-element-map tree 'table-cell 'identity info)))
  1089. info))))
  1090. ;; 3. Tables rules do not count either.
  1091. (org-test-with-parsed-data "
  1092. | a |
  1093. |---|
  1094. | b |
  1095. |---|
  1096. | c |"
  1097. (should
  1098. (equal '(2 . 0)
  1099. (org-export-table-cell-address
  1100. (car (last (org-element-map tree 'table-cell 'identity info)))
  1101. info))))
  1102. ;; 4. Return nil for special cells.
  1103. (org-test-with-parsed-data "| / | a |"
  1104. (should-not
  1105. (org-export-table-cell-address
  1106. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1107. info))))
  1108. (ert-deftest test-org-export/get-table-cell-at ()
  1109. "Test `org-export-get-table-cell-at' specifications."
  1110. ;; 1. Address ignores special columns, special rows and rules.
  1111. (org-test-with-parsed-data "
  1112. | / | <> |
  1113. | | a |
  1114. |---+----|
  1115. | | b |"
  1116. (should
  1117. (equal '("b")
  1118. (org-element-contents
  1119. (org-export-get-table-cell-at
  1120. '(1 . 0)
  1121. (org-element-map tree 'table 'identity info 'first-match)
  1122. info)))))
  1123. ;; 2. Return value for a non-existent address is nil.
  1124. (org-test-with-parsed-data "| a |"
  1125. (should-not
  1126. (org-export-get-table-cell-at
  1127. '(2 . 2)
  1128. (org-element-map tree 'table 'identity info 'first-match)
  1129. info)))
  1130. (org-test-with-parsed-data "| / |"
  1131. (should-not
  1132. (org-export-get-table-cell-at
  1133. '(0 . 0)
  1134. (org-element-map tree 'table 'identity info 'first-match)
  1135. info))))
  1136. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1137. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1138. ;; 1. A cell at a beginning of a row always starts a column group.
  1139. (org-test-with-parsed-data "| a |"
  1140. (should
  1141. (org-export-table-cell-starts-colgroup-p
  1142. (org-element-map tree 'table-cell 'identity info 'first-match)
  1143. info)))
  1144. ;; 2. Special column should be ignored when determining the
  1145. ;; beginning of the row.
  1146. (org-test-with-parsed-data "
  1147. | / | |
  1148. | | a |"
  1149. (should
  1150. (org-export-table-cell-starts-colgroup-p
  1151. (org-element-map tree 'table-cell 'identity info 'first-match)
  1152. info)))
  1153. ;; 2. Explicit column groups.
  1154. (org-test-with-parsed-data "
  1155. | / | | < |
  1156. | a | b | c |"
  1157. (should
  1158. (equal
  1159. '(yes no yes)
  1160. (org-element-map
  1161. tree 'table-cell
  1162. (lambda (cell)
  1163. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1164. info)))))
  1165. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1166. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1167. ;; 1. A cell at the end of a row always ends a column group.
  1168. (org-test-with-parsed-data "| a |"
  1169. (should
  1170. (org-export-table-cell-ends-colgroup-p
  1171. (org-element-map tree 'table-cell 'identity info 'first-match)
  1172. info)))
  1173. ;; 2. Special column should be ignored when determining the
  1174. ;; beginning of the row.
  1175. (org-test-with-parsed-data "
  1176. | / | |
  1177. | | a |"
  1178. (should
  1179. (org-export-table-cell-ends-colgroup-p
  1180. (org-element-map tree 'table-cell 'identity info 'first-match)
  1181. info)))
  1182. ;; 3. Explicit column groups.
  1183. (org-test-with-parsed-data "
  1184. | / | < | |
  1185. | a | b | c |"
  1186. (should
  1187. (equal
  1188. '(yes no yes)
  1189. (org-element-map
  1190. tree 'table-cell
  1191. (lambda (cell)
  1192. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1193. info)))))
  1194. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1195. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1196. ;; 1. A row at the beginning of a table always starts a row group.
  1197. ;; So does a row following a table rule.
  1198. (org-test-with-parsed-data "
  1199. | a |
  1200. |---|
  1201. | b |"
  1202. (should
  1203. (equal
  1204. '(yes no yes)
  1205. (org-element-map
  1206. tree 'table-row
  1207. (lambda (row)
  1208. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1209. info))))
  1210. ;; 2. Special rows should be ignored when determining the beginning
  1211. ;; of the row.
  1212. (org-test-with-parsed-data "
  1213. | / | < |
  1214. | | a |
  1215. |---+---|
  1216. | / | < |
  1217. | | b |"
  1218. (should
  1219. (equal
  1220. '(yes no yes)
  1221. (org-element-map
  1222. tree 'table-row
  1223. (lambda (row)
  1224. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1225. info)))))
  1226. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1227. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1228. ;; 1. A row at the end of a table always ends a row group. So does
  1229. ;; a row preceding a table rule.
  1230. (org-test-with-parsed-data "
  1231. | a |
  1232. |---|
  1233. | b |"
  1234. (should
  1235. (equal
  1236. '(yes no yes)
  1237. (org-element-map
  1238. tree 'table-row
  1239. (lambda (row)
  1240. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1241. info))))
  1242. ;; 2. Special rows should be ignored when determining the beginning
  1243. ;; of the row.
  1244. (org-test-with-parsed-data "
  1245. | | a |
  1246. | / | < |
  1247. |---+---|
  1248. | | b |
  1249. | / | < |"
  1250. (should
  1251. (equal
  1252. '(yes no yes)
  1253. (org-element-map
  1254. tree 'table-row
  1255. (lambda (row)
  1256. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1257. info)))))
  1258. (ert-deftest test-org-export/table-row-starts-header-p ()
  1259. "Test `org-export-table-row-starts-header-p' specifications."
  1260. ;; 1. Only the row starting the first row group starts the table
  1261. ;; header.
  1262. (org-test-with-parsed-data "
  1263. | a |
  1264. | b |
  1265. |---|
  1266. | c |"
  1267. (should
  1268. (equal
  1269. '(yes no no no)
  1270. (org-element-map
  1271. tree 'table-row
  1272. (lambda (row)
  1273. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1274. info))))
  1275. ;; 2. A row cannot start an header if there's no header in the
  1276. ;; table.
  1277. (org-test-with-parsed-data "
  1278. | a |
  1279. |---|"
  1280. (should-not
  1281. (org-export-table-row-starts-header-p
  1282. (org-element-map tree 'table-row 'identity info 'first-match)
  1283. info))))
  1284. (ert-deftest test-org-export/table-row-ends-header-p ()
  1285. "Test `org-export-table-row-ends-header-p' specifications."
  1286. ;; 1. Only the row starting the first row group starts the table
  1287. ;; header.
  1288. (org-test-with-parsed-data "
  1289. | a |
  1290. | b |
  1291. |---|
  1292. | c |"
  1293. (should
  1294. (equal
  1295. '(no yes no no)
  1296. (org-element-map
  1297. tree 'table-row
  1298. (lambda (row)
  1299. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1300. info))))
  1301. ;; 2. A row cannot start an header if there's no header in the
  1302. ;; table.
  1303. (org-test-with-parsed-data "
  1304. | a |
  1305. |---|"
  1306. (should-not
  1307. (org-export-table-row-ends-header-p
  1308. (org-element-map tree 'table-row 'identity info 'first-match)
  1309. info))))
  1310. (provide 'test-org-export)
  1311. ;;; test-org-export.el end here