test-org-export.el 39 KB

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