test-org-export.el 42 KB

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