test-org-export.el 47 KB

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