test-org-export.el 46 KB

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