test-org-export.el 40 KB

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