test-org-export.el 53 KB

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