test-org-export.el 42 KB

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