test-org-export.el 46 KB

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