test-org-export.el 43 KB

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