test-org-export.el 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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")
  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))))
  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]"
  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]\n"))
  205. (should
  206. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))))
  207. ;; Clocks.
  208. (let ((org-clock-string "CLOCK:"))
  209. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  210. (org-test-with-backend "test"
  211. (should
  212. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  213. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  214. (should
  215. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  216. ;; Plannings.
  217. (let ((org-closed-string "CLOSED:"))
  218. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  219. (org-test-with-backend "test"
  220. (should
  221. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  222. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  223. (should
  224. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  225. ""))))))
  226. (ert-deftest test-org-export/comment-tree ()
  227. "Test if export process ignores commented trees."
  228. (let ((org-comment-string "COMMENT"))
  229. (org-test-with-temp-text "* COMMENT Head1"
  230. (org-test-with-backend "test"
  231. (should (equal (org-export-as 'test) ""))))))
  232. (ert-deftest test-org-export/export-scope ()
  233. "Test all export scopes."
  234. (org-test-with-temp-text "
  235. * Head1
  236. ** Head2
  237. text
  238. *** Head3"
  239. (org-test-with-backend "test"
  240. ;; Subtree.
  241. (forward-line 3)
  242. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  243. ;; Visible.
  244. (goto-char (point-min))
  245. (forward-line)
  246. (org-cycle)
  247. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  248. ;; Body only.
  249. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  250. (should (equal (org-export-as 'test nil nil 'body-only)
  251. "* Head1\n** Head2\ntext\n*** Head3\n"))
  252. (should (equal (org-export-as 'test)
  253. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  254. ;; Region.
  255. (goto-char (point-min))
  256. (forward-line 3)
  257. (transient-mark-mode 1)
  258. (push-mark (point) t t)
  259. (goto-char (point-at-eol))
  260. (should (equal (org-export-as 'test) "text\n"))))
  261. ;; Subtree with a code block calling another block outside.
  262. (org-test-with-temp-text "
  263. * Head1
  264. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  265. <<test>>
  266. #+END_SRC
  267. * Head2
  268. #+NAME: test
  269. #+BEGIN_SRC emacs-lisp
  270. \(+ 1 2)
  271. #+END_SRC"
  272. (org-test-with-backend "test"
  273. (forward-line 1)
  274. (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
  275. (ert-deftest test-org-export/export-snippet ()
  276. "Test export snippets transcoding."
  277. (org-test-with-temp-text "@test{A}@t{B}"
  278. (org-test-with-backend "test"
  279. (flet ((org-test-export-snippet
  280. (snippet contents info)
  281. (when (eq (org-export-snippet-backend snippet) 'test)
  282. (org-element-property :value snippet))))
  283. (let ((org-export-snippet-translation-alist nil))
  284. (should (equal (org-export-as 'test) "A\n")))
  285. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  286. (should (equal (org-export-as 'test) "AB\n")))))))
  287. (ert-deftest test-org-export/expand-include ()
  288. "Test file inclusion in an Org buffer."
  289. ;; Full insertion with recursive inclusion.
  290. (org-test-with-temp-text
  291. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  292. (org-export-expand-include-keyword)
  293. (should (equal (buffer-string)
  294. "Small Org file with an include keyword.
  295. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  296. Success!
  297. * Heading
  298. body\n")))
  299. ;; Localized insertion.
  300. (org-test-with-temp-text
  301. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  302. org-test-dir)
  303. (org-export-expand-include-keyword)
  304. (should (equal (buffer-string)
  305. "Small Org file with an include keyword.\n")))
  306. ;; Insertion with constraints on headlines level.
  307. (org-test-with-temp-text
  308. (format
  309. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  310. org-test-dir)
  311. (org-export-expand-include-keyword)
  312. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  313. ;; Inclusion within an example block.
  314. (org-test-with-temp-text
  315. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  316. org-test-dir)
  317. (org-export-expand-include-keyword)
  318. (should
  319. (equal
  320. (buffer-string)
  321. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  322. ;; Inclusion within a src-block.
  323. (org-test-with-temp-text
  324. (format
  325. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  326. org-test-dir)
  327. (org-export-expand-include-keyword)
  328. (should (equal (buffer-string)
  329. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  330. (ert-deftest test-org-export/user-ignore-list ()
  331. "Test if `:ignore-list' accepts user input."
  332. (org-test-with-backend "test"
  333. (flet ((skip-note-head
  334. (data backend info)
  335. ;; Ignore headlines with the word "note" in their title.
  336. (org-element-map
  337. data 'headline
  338. (lambda (headline)
  339. (when (string-match "\\<note\\>"
  340. (org-element-property :raw-value headline))
  341. (org-export-ignore-element headline info)))
  342. info)
  343. data))
  344. ;; Install function in parse tree filters.
  345. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  346. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  347. (should (equal (org-export-as 'test) "* Head1\n")))))))
  348. (ert-deftest test-org-export/before-parsing-hook ()
  349. "Test `org-export-before-parsing-hook'."
  350. (org-test-with-backend "test"
  351. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  352. (let ((org-export-before-parsing-hook
  353. '((lambda ()
  354. (org-map-entries
  355. (lambda ()
  356. (delete-region (point) (progn (forward-line) (point)))))))))
  357. (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
  358. ;;; Footnotes
  359. (ert-deftest test-org-export/footnotes ()
  360. "Test footnotes specifications."
  361. (let ((org-footnote-section nil))
  362. ;; 1. Read every type of footnote.
  363. (org-test-with-temp-text
  364. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  365. (let* ((tree (org-element-parse-buffer))
  366. (info (org-export-store-footnote-definitions
  367. `(:parse-tree ,tree :with-footnotes t))))
  368. (should
  369. (equal
  370. '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
  371. (org-element-map
  372. tree 'footnote-reference
  373. (lambda (ref)
  374. (let ((def (org-export-get-footnote-definition ref info)))
  375. (cons (org-export-get-footnote-number ref info)
  376. (if (eq (org-element-property :type ref) 'inline) (car def)
  377. (car (org-element-contents
  378. (car (org-element-contents def))))))))
  379. info)))))
  380. ;; 2. Test nested footnotes order.
  381. (org-test-with-temp-text
  382. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  383. (let* ((tree (org-element-parse-buffer))
  384. (info (org-export-store-footnote-definitions
  385. `(:parse-tree ,tree :with-footnotes t))))
  386. (should
  387. (equal
  388. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  389. (org-element-map
  390. tree 'footnote-reference
  391. (lambda (ref)
  392. (when (org-export-footnote-first-reference-p ref info)
  393. (cons (org-export-get-footnote-number ref info)
  394. (org-element-property :label ref))))
  395. info)))))
  396. ;; 3. Test nested footnote in invisible definitions.
  397. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  398. ;; Hide definitions.
  399. (narrow-to-region (point) (point-at-eol))
  400. (let* ((tree (org-element-parse-buffer))
  401. (info (org-export-store-footnote-definitions
  402. `(:parse-tree ,tree :with-footnotes t))))
  403. ;; Both footnotes should be seen.
  404. (should
  405. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  406. ;; 4. Test footnotes definitions collection.
  407. (org-test-with-temp-text "Text[fn:1:A[fn:2]] [fn:3].
  408. \[fn:2] B [fn:3] [fn::D].
  409. \[fn:3] C."
  410. (let* ((tree (org-element-parse-buffer))
  411. (info (org-export-store-footnote-definitions
  412. `(:parse-tree ,tree :with-footnotes t))))
  413. (should (= (length (org-export-collect-footnote-definitions tree info))
  414. 4))))
  415. ;; 5. Test export of footnotes defined outside parsing scope.
  416. (org-test-with-temp-text "[fn:1] Out of scope
  417. * Title
  418. Paragraph[fn:1]"
  419. (org-test-with-backend "test"
  420. (flet ((org-test-footnote-reference
  421. (fn-ref contents info)
  422. (org-element-interpret-data
  423. (org-export-get-footnote-definition fn-ref info))))
  424. (forward-line)
  425. (should (equal "ParagraphOut of scope\n"
  426. (org-export-as 'test 'subtree))))))))
  427. ;;; Links
  428. (ert-deftest test-org-export/fuzzy-links ()
  429. "Test fuzzy link export specifications."
  430. ;; 1. Links to invisible (keyword) targets should be ignored.
  431. (org-test-with-parsed-data
  432. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  433. (should-not
  434. (org-element-map
  435. tree 'link
  436. (lambda (link)
  437. (org-export-get-ordinal
  438. (org-export-resolve-fuzzy-link link info) info)) info)))
  439. ;; 2. Link to an headline should return headline's number.
  440. (org-test-with-parsed-data
  441. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  442. (should
  443. ;; Note: Headline's number is in fact a list of numbers.
  444. (equal '(2)
  445. (org-element-map
  446. tree 'link
  447. (lambda (link)
  448. (org-export-get-ordinal
  449. (org-export-resolve-fuzzy-link link info) info)) info t))))
  450. ;; 3. Link to a target in an item should return item's number.
  451. (org-test-with-parsed-data
  452. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  453. (should
  454. ;; Note: Item's number is in fact a list of numbers.
  455. (equal '(1 2)
  456. (org-element-map
  457. tree 'link
  458. (lambda (link)
  459. (org-export-get-ordinal
  460. (org-export-resolve-fuzzy-link link info) info)) info t))))
  461. ;; 4. Link to a target in a footnote should return footnote's
  462. ;; number.
  463. (org-test-with-parsed-data "
  464. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  465. (should
  466. (equal '(2 3)
  467. (org-element-map
  468. tree 'link
  469. (lambda (link)
  470. (org-export-get-ordinal
  471. (org-export-resolve-fuzzy-link link info) info)) info))))
  472. ;; 5. Link to a named element should return sequence number of that
  473. ;; element.
  474. (org-test-with-parsed-data
  475. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  476. (should
  477. (= 2
  478. (org-element-map
  479. tree 'link
  480. (lambda (link)
  481. (org-export-get-ordinal
  482. (org-export-resolve-fuzzy-link link info) info)) info t))))
  483. ;; 6. Link to a target not within an item, a table, a footnote
  484. ;; reference or definition should return section number.
  485. (org-test-with-parsed-data
  486. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  487. (should
  488. (equal '(2)
  489. (org-element-map
  490. tree 'link
  491. (lambda (link)
  492. (org-export-get-ordinal
  493. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  494. (defun test-org-export/resolve-coderef ()
  495. "Test `org-export-resolve-coderef' specifications."
  496. (let ((org-coderef-label-format "(ref:%s)"))
  497. ;; 1. A link to a "-n -k -r" block returns line number.
  498. (org-test-with-temp-text
  499. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  500. (let ((tree (org-element-parse-buffer)))
  501. (should
  502. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  503. (org-test-with-temp-text
  504. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  505. (let ((tree (org-element-parse-buffer)))
  506. (should
  507. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  508. ;; 2. A link to a "-n -r" block returns line number.
  509. (org-test-with-temp-text
  510. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  511. (let ((tree (org-element-parse-buffer)))
  512. (should
  513. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  514. (org-test-with-temp-text
  515. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  516. (let ((tree (org-element-parse-buffer)))
  517. (should
  518. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  519. ;; 3. A link to a "-n" block returns coderef.
  520. (org-test-with-temp-text
  521. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  522. (let ((tree (org-element-parse-buffer)))
  523. (should
  524. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  525. "coderef"))))
  526. (org-test-with-temp-text
  527. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  528. (let ((tree (org-element-parse-buffer)))
  529. (should
  530. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  531. "coderef"))))
  532. ;; 4. A link to a "-r" block returns line number.
  533. (org-test-with-temp-text
  534. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  535. (let ((tree (org-element-parse-buffer)))
  536. (should
  537. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  538. (org-test-with-temp-text
  539. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  540. (let ((tree (org-element-parse-buffer)))
  541. (should
  542. (= (org-export-resolve-coderef "coderef" `(:parse-tree ,tree)) 1))))
  543. ;; 5. A link to a block without a switch returns coderef.
  544. (org-test-with-temp-text
  545. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  546. (let ((tree (org-element-parse-buffer)))
  547. (should
  548. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  549. "coderef"))))
  550. (org-test-with-temp-text
  551. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  552. (let ((tree (org-element-parse-buffer)))
  553. (should
  554. (equal (org-export-resolve-coderef "coderef" `(:parse-tree ,tree))
  555. "coderef"))))
  556. ;; 6. Correctly handle continued line numbers. A "+n" switch
  557. ;; should resume numbering from previous block with numbered
  558. ;; lines, ignoring blocks not numbering lines in the process.
  559. ;; A "-n" switch resets count.
  560. (org-test-with-temp-text "
  561. #+BEGIN_EXAMPLE -n
  562. Text.
  563. #+END_EXAMPLE
  564. #+BEGIN_SRC emacs-lisp
  565. \(- 1 1)
  566. #+END_SRC
  567. #+BEGIN_SRC emacs-lisp +n -r
  568. \(+ 1 1) (ref:addition)
  569. #+END_SRC
  570. #+BEGIN_EXAMPLE -n -r
  571. Another text. (ref:text)
  572. #+END_EXAMPLE"
  573. (let* ((tree (org-element-parse-buffer))
  574. (info `(:parse-tree ,tree)))
  575. (should (= (org-export-resolve-coderef "addition" info) 2))
  576. (should (= (org-export-resolve-coderef "text" info) 1))))
  577. ;; 7. Recognize coderef with user-specified syntax.
  578. (org-test-with-temp-text
  579. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  580. (let ((tree (org-element-parse-buffer)))
  581. (should (equal (org-export-resolve-coderef "text" `(:parse-tree ,tree))
  582. "text"))))))
  583. ;;; Src-block and example-block
  584. (ert-deftest test-org-export/unravel-code ()
  585. "Test `org-export-unravel-code' function."
  586. (let ((org-coderef-label-format "(ref:%s)"))
  587. ;; 1. Code without reference.
  588. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  589. (should (equal (org-export-unravel-code (org-element-current-element))
  590. '("(+ 1 1)\n"))))
  591. ;; 2. Code with reference.
  592. (org-test-with-temp-text
  593. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  594. (should (equal (org-export-unravel-code (org-element-current-element))
  595. '("(+ 1 1)\n" (1 . "test")))))
  596. ;; 3. Code with user-defined reference.
  597. (org-test-with-temp-text
  598. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  599. (should (equal (org-export-unravel-code (org-element-current-element))
  600. '("(+ 1 1)\n" (1 . "test")))))
  601. ;; 4. Code references keys are relative to the current block.
  602. (org-test-with-temp-text "
  603. #+BEGIN_EXAMPLE -n
  604. \(+ 1 1)
  605. #+END_EXAMPLE
  606. #+BEGIN_EXAMPLE +n
  607. \(+ 2 2)
  608. \(+ 3 3) (ref:one)
  609. #+END_EXAMPLE"
  610. (goto-line 5)
  611. (should (equal (org-export-unravel-code (org-element-current-element))
  612. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
  613. ;; 5. Free up comma-protected lines.
  614. ;;
  615. ;; 5.1. In an Org source block, every line is protected.
  616. (org-test-with-temp-text
  617. "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
  618. (should (equal (org-export-unravel-code (org-element-current-element))
  619. '("* Test\n# comment\nText\n"))))
  620. ;; 5.2. In other blocks, only headlines, comments and keywords are
  621. ;; protected.
  622. (org-test-with-temp-text
  623. "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
  624. (should (equal (org-export-unravel-code (org-element-current-element))
  625. '("* Headline\n, * Not headline\n,Keep\n"))))))
  626. ;;; Tables
  627. (ert-deftest test-org-export/special-column ()
  628. "Test if the table's special column is properly recognized."
  629. ;; 1. First column is special if it contains only a special marking
  630. ;; characters or empty cells.
  631. (org-test-with-temp-text "
  632. | ! | 1 |
  633. | | 2 |"
  634. (should
  635. (org-export-table-has-special-column-p
  636. (org-element-map
  637. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  638. ;; 2. If the column contains anything else, it isn't special.
  639. (org-test-with-temp-text "
  640. | ! | 1 |
  641. | b | 2 |"
  642. (should-not
  643. (org-export-table-has-special-column-p
  644. (org-element-map
  645. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  646. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  647. ;; and "!".
  648. (org-test-with-temp-text "
  649. | # | 1 |
  650. | ^ | 2 |
  651. | * | 3 |
  652. | _ | 4 |
  653. | / | 5 |
  654. | $ | 6 |
  655. | ! | 7 |"
  656. (should
  657. (org-export-table-has-special-column-p
  658. (org-element-map
  659. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  660. ;; 4. A first column with only empty cells isn't considered as
  661. ;; special.
  662. (org-test-with-temp-text "
  663. | | 1 |
  664. | | 2 |"
  665. (should-not
  666. (org-export-table-has-special-column-p
  667. (org-element-map
  668. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  669. (ert-deftest test-org-export/special-row ()
  670. "Test if special rows in a table are properly recognized."
  671. ;; 1. A row is special if it has a special marking character in the
  672. ;; special column.
  673. (org-test-with-parsed-data "| ! | 1 |"
  674. (should
  675. (org-export-table-row-is-special-p
  676. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  677. ;; 2. A row is special when its first field is "/"
  678. (org-test-with-parsed-data "
  679. | / | 1 |
  680. | a | b |"
  681. (should
  682. (org-export-table-row-is-special-p
  683. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  684. ;; 3. A row only containing alignment cookies is also considered as
  685. ;; special.
  686. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  687. (should
  688. (org-export-table-row-is-special-p
  689. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  690. ;; 4. Everything else isn't considered as special.
  691. (org-test-with-parsed-data "| a | | c |"
  692. (should-not
  693. (org-export-table-row-is-special-p
  694. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  695. ;; 5. Table's rules are never considered as special rows.
  696. (org-test-with-parsed-data "|---+---|"
  697. (should-not
  698. (org-export-table-row-is-special-p
  699. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  700. (ert-deftest test-org-export/has-header-p ()
  701. "Test `org-export-table-has-header-p' specifications."
  702. ;; 1. With an header.
  703. (org-test-with-parsed-data "
  704. | a | b |
  705. |---+---|
  706. | c | d |"
  707. (should
  708. (org-export-table-has-header-p
  709. (org-element-map tree 'table 'identity info 'first-match)
  710. info)))
  711. ;; 2. Without an header.
  712. (org-test-with-parsed-data "
  713. | a | b |
  714. | c | d |"
  715. (should-not
  716. (org-export-table-has-header-p
  717. (org-element-map tree 'table 'identity info 'first-match)
  718. info)))
  719. ;; 3. Don't get fooled with starting and ending rules.
  720. (org-test-with-parsed-data "
  721. |---+---|
  722. | a | b |
  723. | c | d |
  724. |---+---|"
  725. (should-not
  726. (org-export-table-has-header-p
  727. (org-element-map tree 'table 'identity info 'first-match)
  728. info))))
  729. (ert-deftest test-org-export/table-row-group ()
  730. "Test `org-export-table-row-group' specifications."
  731. ;; 1. A rule creates a new group.
  732. (org-test-with-parsed-data "
  733. | a | b |
  734. |---+---|
  735. | 1 | 2 |"
  736. (should
  737. (equal
  738. '(1 nil 2)
  739. (mapcar (lambda (row) (org-export-table-row-group row info))
  740. (org-element-map tree 'table-row 'identity)))))
  741. ;; 2. Special rows are ignored in count.
  742. (org-test-with-parsed-data "
  743. | / | < | > |
  744. |---|---+---|
  745. | | 1 | 2 |"
  746. (should
  747. (equal
  748. '(nil nil 1)
  749. (mapcar (lambda (row) (org-export-table-row-group row info))
  750. (org-element-map tree 'table-row 'identity)))))
  751. ;; 3. Double rules also are ignored in count.
  752. (org-test-with-parsed-data "
  753. | a | b |
  754. |---+---|
  755. |---+---|
  756. | 1 | 2 |"
  757. (should
  758. (equal
  759. '(1 nil nil 2)
  760. (mapcar (lambda (row) (org-export-table-row-group row info))
  761. (org-element-map tree 'table-row 'identity))))))
  762. (ert-deftest test-org-export/table-cell-width ()
  763. "Test `org-export-table-cell-width' specifications."
  764. ;; 1. Width is primarily determined by width cookies. If no cookie
  765. ;; is found, cell's width is nil.
  766. (org-test-with-parsed-data "
  767. | / | <l> | <6> | <l7> |
  768. | | a | b | c |"
  769. (should
  770. (equal
  771. '(nil 6 7)
  772. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  773. (org-element-map tree 'table-cell 'identity info)))))
  774. ;; 2. The last width cookie has precedence.
  775. (org-test-with-parsed-data "
  776. | <6> |
  777. | <7> |
  778. | a |"
  779. (should
  780. (equal
  781. '(7)
  782. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  783. (org-element-map tree 'table-cell 'identity info)))))
  784. ;; 3. Valid width cookies must have a specific row.
  785. (org-test-with-parsed-data "| <6> | cell |"
  786. (should
  787. (equal
  788. '(nil nil)
  789. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  790. (org-element-map tree 'table-cell 'identity))))))
  791. (ert-deftest test-org-export/table-cell-alignment ()
  792. "Test `org-export-table-cell-alignment' specifications."
  793. (let ((org-table-number-fraction 0.5)
  794. (org-table-number-regexp "^[0-9]+$"))
  795. ;; 1. Alignment is primarily determined by alignment cookies.
  796. (org-test-with-temp-text "| <l> | <c> | <r> |"
  797. (let* ((tree (org-element-parse-buffer))
  798. (info `(:parse-tree ,tree)))
  799. (should
  800. (equal
  801. '(left center right)
  802. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  803. (org-element-map tree 'table-cell 'identity))))))
  804. ;; 2. The last alignment cookie has precedence.
  805. (org-test-with-temp-text "
  806. | <l8> |
  807. | cell |
  808. | <r9> |"
  809. (let* ((tree (org-element-parse-buffer))
  810. (info `(:parse-tree ,tree)))
  811. (should
  812. (equal
  813. '(right right right)
  814. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  815. (org-element-map tree 'table-cell 'identity))))))
  816. ;; 3. If there's no cookie, cell's contents determine alignment.
  817. ;; A column mostly made of cells containing numbers will align
  818. ;; its cells to the right.
  819. (org-test-with-temp-text "
  820. | 123 |
  821. | some text |
  822. | 12345 |"
  823. (let* ((tree (org-element-parse-buffer))
  824. (info `(:parse-tree ,tree)))
  825. (should
  826. (equal
  827. '(right right right)
  828. (mapcar (lambda (cell)
  829. (org-export-table-cell-alignment cell info))
  830. (org-element-map tree 'table-cell 'identity))))))
  831. ;; 5. Otherwise, they will be aligned to the left.
  832. (org-test-with-temp-text "
  833. | text |
  834. | some text |
  835. | 12345 |"
  836. (let* ((tree (org-element-parse-buffer))
  837. (info `(:parse-tree ,tree)))
  838. (should
  839. (equal
  840. '(left left left)
  841. (mapcar (lambda (cell)
  842. (org-export-table-cell-alignment cell info))
  843. (org-element-map tree 'table-cell 'identity))))))))
  844. (ert-deftest test-org-export/table-cell-borders ()
  845. "Test `org-export-table-cell-borders' specifications."
  846. ;; 1. Recognize various column groups indicators.
  847. (org-test-with-parsed-data "| / | < | > | <> |"
  848. (should
  849. (equal
  850. '((right bottom top) (left bottom top) (right bottom top)
  851. (right left bottom top))
  852. (mapcar (lambda (cell)
  853. (org-export-table-cell-borders cell info))
  854. (org-element-map tree 'table-cell 'identity)))))
  855. ;; 2. Accept shortcuts to define column groups.
  856. (org-test-with-parsed-data "| / | < | < |"
  857. (should
  858. (equal
  859. '((right bottom top) (right left bottom top) (left bottom top))
  860. (mapcar (lambda (cell)
  861. (org-export-table-cell-borders cell info))
  862. (org-element-map tree 'table-cell 'identity)))))
  863. ;; 3. A valid column groups row must start with a "/".
  864. (org-test-with-parsed-data "
  865. | | < |
  866. | a | b |"
  867. (should
  868. (equal '((top) (top) (bottom) (bottom))
  869. (mapcar (lambda (cell)
  870. (org-export-table-cell-borders cell info))
  871. (org-element-map tree 'table-cell 'identity)))))
  872. ;; 4. Take table rules into consideration.
  873. (org-test-with-parsed-data "
  874. | 1 |
  875. |---|
  876. | 2 |"
  877. (should
  878. (equal '((below top) (bottom above))
  879. (mapcar (lambda (cell)
  880. (org-export-table-cell-borders cell info))
  881. (org-element-map tree 'table-cell 'identity)))))
  882. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  883. ;; (resp. `bottom' and `below') borders. Any special row is
  884. ;; ignored.
  885. (org-test-with-parsed-data "
  886. |---+----|
  887. | / | |
  888. | | 1 |
  889. |---+----|"
  890. (should
  891. (equal '((bottom below top above))
  892. (last
  893. (mapcar (lambda (cell)
  894. (org-export-table-cell-borders cell info))
  895. (org-element-map tree 'table-cell 'identity)))))))
  896. (ert-deftest test-org-export/table-dimensions ()
  897. "Test `org-export-table-dimensions' specifications."
  898. ;; 1. Standard test.
  899. (org-test-with-parsed-data "
  900. | 1 | 2 | 3 |
  901. | 4 | 5 | 6 |"
  902. (should
  903. (equal '(2 . 3)
  904. (org-export-table-dimensions
  905. (org-element-map tree 'table 'identity info 'first-match) info))))
  906. ;; 2. Ignore horizontal rules and special columns.
  907. (org-test-with-parsed-data "
  908. | / | < | > |
  909. | 1 | 2 | 3 |
  910. |---+---+---|
  911. | 4 | 5 | 6 |"
  912. (should
  913. (equal '(2 . 3)
  914. (org-export-table-dimensions
  915. (org-element-map tree 'table 'identity info 'first-match) info)))))
  916. (ert-deftest test-org-export/table-cell-address ()
  917. "Test `org-export-table-cell-address' specifications."
  918. ;; 1. Standard test: index is 0-based.
  919. (org-test-with-parsed-data "| a | b |"
  920. (should
  921. (equal '((0 . 0) (0 . 1))
  922. (org-element-map
  923. tree 'table-cell
  924. (lambda (cell) (org-export-table-cell-address cell info))
  925. info))))
  926. ;; 2. Special column isn't counted, nor are special rows.
  927. (org-test-with-parsed-data "
  928. | / | <> |
  929. | | c |"
  930. (should
  931. (equal '(0 . 0)
  932. (org-export-table-cell-address
  933. (car (last (org-element-map tree 'table-cell 'identity info)))
  934. info))))
  935. ;; 3. Tables rules do not count either.
  936. (org-test-with-parsed-data "
  937. | a |
  938. |---|
  939. | b |
  940. |---|
  941. | c |"
  942. (should
  943. (equal '(2 . 0)
  944. (org-export-table-cell-address
  945. (car (last (org-element-map tree 'table-cell 'identity info)))
  946. info))))
  947. ;; 4. Return nil for special cells.
  948. (org-test-with-parsed-data "| / | a |"
  949. (should-not
  950. (org-export-table-cell-address
  951. (org-element-map tree 'table-cell 'identity nil 'first-match)
  952. info))))
  953. (ert-deftest test-org-export/get-table-cell-at ()
  954. "Test `org-export-get-table-cell-at' specifications."
  955. ;; 1. Address ignores special columns, special rows and rules.
  956. (org-test-with-parsed-data "
  957. | / | <> |
  958. | | a |
  959. |---+----|
  960. | | b |"
  961. (should
  962. (equal '("b")
  963. (org-element-contents
  964. (org-export-get-table-cell-at
  965. '(1 . 0)
  966. (org-element-map tree 'table 'identity info 'first-match)
  967. info)))))
  968. ;; 2. Return value for a non-existent address is nil.
  969. (org-test-with-parsed-data "| a |"
  970. (should-not
  971. (org-export-get-table-cell-at
  972. '(2 . 2)
  973. (org-element-map tree 'table 'identity info 'first-match)
  974. info)))
  975. (org-test-with-parsed-data "| / |"
  976. (should-not
  977. (org-export-get-table-cell-at
  978. '(0 . 0)
  979. (org-element-map tree 'table 'identity info 'first-match)
  980. info))))
  981. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  982. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  983. ;; 1. A cell at a beginning of a row always starts a column group.
  984. (org-test-with-parsed-data "| a |"
  985. (should
  986. (org-export-table-cell-starts-colgroup-p
  987. (org-element-map tree 'table-cell 'identity info 'first-match)
  988. info)))
  989. ;; 2. Special column should be ignored when determining the
  990. ;; beginning of the row.
  991. (org-test-with-parsed-data "
  992. | / | |
  993. | | a |"
  994. (should
  995. (org-export-table-cell-starts-colgroup-p
  996. (org-element-map tree 'table-cell 'identity info 'first-match)
  997. info)))
  998. ;; 2. Explicit column groups.
  999. (org-test-with-parsed-data "
  1000. | / | | < |
  1001. | a | b | c |"
  1002. (should
  1003. (equal
  1004. '(yes no yes)
  1005. (org-element-map
  1006. tree 'table-cell
  1007. (lambda (cell)
  1008. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1009. info)))))
  1010. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1011. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1012. ;; 1. A cell at the end of a row always ends a column group.
  1013. (org-test-with-parsed-data "| a |"
  1014. (should
  1015. (org-export-table-cell-ends-colgroup-p
  1016. (org-element-map tree 'table-cell 'identity info 'first-match)
  1017. info)))
  1018. ;; 2. Special column should be ignored when determining the
  1019. ;; beginning of the row.
  1020. (org-test-with-parsed-data "
  1021. | / | |
  1022. | | a |"
  1023. (should
  1024. (org-export-table-cell-ends-colgroup-p
  1025. (org-element-map tree 'table-cell 'identity info 'first-match)
  1026. info)))
  1027. ;; 3. Explicit column groups.
  1028. (org-test-with-parsed-data "
  1029. | / | < | |
  1030. | a | b | c |"
  1031. (should
  1032. (equal
  1033. '(yes no yes)
  1034. (org-element-map
  1035. tree 'table-cell
  1036. (lambda (cell)
  1037. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1038. info)))))
  1039. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1040. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1041. ;; 1. A row at the beginning of a table always starts a row group.
  1042. ;; So does a row following a table rule.
  1043. (org-test-with-parsed-data "
  1044. | a |
  1045. |---|
  1046. | b |"
  1047. (should
  1048. (equal
  1049. '(yes no yes)
  1050. (org-element-map
  1051. tree 'table-row
  1052. (lambda (row)
  1053. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1054. info))))
  1055. ;; 2. Special rows should be ignored when determining the beginning
  1056. ;; of the row.
  1057. (org-test-with-parsed-data "
  1058. | / | < |
  1059. | | a |
  1060. |---+---|
  1061. | / | < |
  1062. | | b |"
  1063. (should
  1064. (equal
  1065. '(yes no yes)
  1066. (org-element-map
  1067. tree 'table-row
  1068. (lambda (row)
  1069. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1070. info)))))
  1071. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1072. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1073. ;; 1. A row at the end of a table always ends a row group. So does
  1074. ;; a row preceding a table rule.
  1075. (org-test-with-parsed-data "
  1076. | a |
  1077. |---|
  1078. | b |"
  1079. (should
  1080. (equal
  1081. '(yes no yes)
  1082. (org-element-map
  1083. tree 'table-row
  1084. (lambda (row)
  1085. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1086. info))))
  1087. ;; 2. Special rows should be ignored when determining the beginning
  1088. ;; of the row.
  1089. (org-test-with-parsed-data "
  1090. | | a |
  1091. | / | < |
  1092. |---+---|
  1093. | | b |
  1094. | / | < |"
  1095. (should
  1096. (equal
  1097. '(yes no yes)
  1098. (org-element-map
  1099. tree 'table-row
  1100. (lambda (row)
  1101. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1102. info)))))
  1103. (ert-deftest test-org-export/table-row-starts-header-p ()
  1104. "Test `org-export-table-row-starts-header-p' specifications."
  1105. ;; 1. Only the row starting the first row group starts the table
  1106. ;; header.
  1107. (org-test-with-parsed-data "
  1108. | a |
  1109. | b |
  1110. |---|
  1111. | c |"
  1112. (should
  1113. (equal
  1114. '(yes no no no)
  1115. (org-element-map
  1116. tree 'table-row
  1117. (lambda (row)
  1118. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1119. info))))
  1120. ;; 2. A row cannot start an header if there's no header in the
  1121. ;; table.
  1122. (org-test-with-parsed-data "
  1123. | a |
  1124. |---|"
  1125. (should-not
  1126. (org-export-table-row-starts-header-p
  1127. (org-element-map tree 'table-row 'identity info 'first-match)
  1128. info))))
  1129. (ert-deftest test-org-export/table-row-ends-header-p ()
  1130. "Test `org-export-table-row-ends-header-p' specifications."
  1131. ;; 1. Only the row starting the first row group starts the table
  1132. ;; header.
  1133. (org-test-with-parsed-data "
  1134. | a |
  1135. | b |
  1136. |---|
  1137. | c |"
  1138. (should
  1139. (equal
  1140. '(no yes no no)
  1141. (org-element-map
  1142. tree 'table-row
  1143. (lambda (row)
  1144. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1145. info))))
  1146. ;; 2. A row cannot start an header if there's no header in the
  1147. ;; table.
  1148. (org-test-with-parsed-data "
  1149. | a |
  1150. |---|"
  1151. (should-not
  1152. (org-export-table-row-ends-header-p
  1153. (org-element-map tree 'table-row 'identity info 'first-match)
  1154. info))))
  1155. (provide 'test-org-export)
  1156. ;;; test-org-export.el end here