test-ox.el 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  1. ;;; test-ox.el --- Tests for ox.el
  2. ;; Copyright (C) 2012, 2013 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (unless (featurep 'ox)
  17. (signal 'missing-test-dependency "org-export"))
  18. (defmacro org-test-with-backend (backend &rest body)
  19. "Execute body with an export back-end defined.
  20. BACKEND is the name of the back-end. BODY is the body to
  21. execute. The defined back-end simply returns parsed data as Org
  22. syntax."
  23. (declare (debug (form body)) (indent 1))
  24. `(let ((org-export-registered-backends
  25. ',(list
  26. (list backend
  27. :translate-alist
  28. (let (transcode-table)
  29. (dolist (type (append org-element-all-elements
  30. org-element-all-objects)
  31. transcode-table)
  32. (push
  33. (cons type
  34. (lambda (obj contents info)
  35. (funcall
  36. (intern (format "org-element-%s-interpreter"
  37. type))
  38. obj contents)))
  39. transcode-table)))))))
  40. (progn ,@body)))
  41. (defmacro org-test-with-parsed-data (data &rest body)
  42. "Execute body with parsed data available.
  43. DATA is a string containing the data to be parsed. BODY is the
  44. body to execute. Parse tree is available under the `tree'
  45. variable, and communication channel under `info'.
  46. This function calls `org-export-collect-tree-properties'. As
  47. such, `:ignore-list' (for `org-element-map') and
  48. `:parse-tree' (for `org-export-get-genealogy') properties are
  49. already filled in `info'."
  50. (declare (debug (form body)) (indent 1))
  51. `(org-test-with-temp-text ,data
  52. (let* ((tree (org-element-parse-buffer))
  53. (info (org-export-collect-tree-properties
  54. tree (org-export-get-environment))))
  55. ,@body)))
  56. ;;; Internal Tests
  57. (ert-deftest test-org-export/bind-keyword ()
  58. "Test reading #+BIND: keywords."
  59. ;; Test with `org-export-allow-bind-keywords' set to t.
  60. (should
  61. (org-test-with-temp-text "#+BIND: variable value"
  62. (let ((org-export-allow-bind-keywords t))
  63. (org-export--install-letbind-maybe)
  64. (eq variable 'value))))
  65. ;; Test with `org-export-allow-bind-keywords' set to nil.
  66. (should-not
  67. (org-test-with-temp-text "#+BIND: variable value"
  68. (let ((org-export-allow-bind-keywords nil))
  69. (org-export--install-letbind-maybe)
  70. (boundp 'variable))))
  71. ;; BIND keywords are case-insensitive.
  72. (should
  73. (org-test-with-temp-text "#+bind: variable value"
  74. (let ((org-export-allow-bind-keywords t))
  75. (org-export--install-letbind-maybe)
  76. (eq variable 'value)))))
  77. (ert-deftest test-org-export/parse-option-keyword ()
  78. "Test reading all standard #+OPTIONS: items."
  79. (should
  80. (equal
  81. (org-export--parse-option-keyword
  82. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  83. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  84. stat:t")
  85. '(:headline-levels
  86. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  87. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  88. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  89. :with-footnotes t :with-inlinetasks nil :with-priority t
  90. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  91. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  92. :with-todo-keywords t)))
  93. ;; Test some special values.
  94. (should
  95. (equal
  96. (org-export--parse-option-keyword
  97. "arch:headline creator:comment d:(\"TEST\")
  98. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  99. '( :section-numbers
  100. 2
  101. :with-archived-trees headline :with-creator comment
  102. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  103. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  104. (ert-deftest test-org-export/get-inbuffer-options ()
  105. "Test reading all standard export keywords."
  106. ;; Properties should follow buffer order.
  107. (should
  108. (equal
  109. (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
  110. (org-export--get-inbuffer-options))
  111. '(:language "fr" :creator "Me" :email "email")))
  112. ;; Parse document keywords.
  113. (should
  114. (equal
  115. (org-test-with-temp-text "#+AUTHOR: Me"
  116. (org-export--get-inbuffer-options))
  117. '(:author ("Me"))))
  118. ;; Test `space' behaviour.
  119. (should
  120. (equal
  121. (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
  122. (org-export--get-inbuffer-options))
  123. '(:title ("Some title with spaces"))))
  124. ;; Test `newline' behaviour.
  125. (should
  126. (equal
  127. (org-test-with-temp-text "#+DESCRIPTION: With\n#+DESCRIPTION: two lines"
  128. (org-export--get-inbuffer-options))
  129. '(:description "With\ntwo lines")))
  130. ;; Test `split' behaviour.
  131. (should
  132. (equal
  133. (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
  134. (org-export--get-inbuffer-options))
  135. '(:select-tags ("a" "b"))))
  136. ;; Options set through SETUPFILE.
  137. (should
  138. (equal
  139. (org-test-with-temp-text
  140. (format "#+DESCRIPTION: l1
  141. #+LANGUAGE: es
  142. #+SELECT_TAGS: a
  143. #+TITLE: a
  144. #+SETUPFILE: \"%s/examples/setupfile.org\"
  145. #+DESCRIPTION: l3
  146. #+LANGUAGE: fr
  147. #+SELECT_TAGS: c
  148. #+TITLE: c"
  149. org-test-dir)
  150. (org-export--get-inbuffer-options))
  151. '(:description "l1\nl2\nl3":language "fr" :select-tags ("a" "b" "c")
  152. :title ("a b c")))))
  153. (ert-deftest test-org-export/get-subtree-options ()
  154. "Test setting options from headline's properties."
  155. ;; EXPORT_TITLE.
  156. (org-test-with-temp-text "#+TITLE: Title
  157. * Headline
  158. :PROPERTIES:
  159. :EXPORT_TITLE: Subtree Title
  160. :END:
  161. Paragraph"
  162. (forward-line)
  163. (should (equal (plist-get (org-export-get-environment nil t) :title)
  164. '("Subtree Title"))))
  165. :title
  166. '("subtree-title")
  167. ;; EXPORT_OPTIONS.
  168. (org-test-with-temp-text "#+OPTIONS: H:1
  169. * Headline
  170. :PROPERTIES:
  171. :EXPORT_OPTIONS: H:2
  172. :END:
  173. Paragraph"
  174. (forward-line)
  175. (should
  176. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  177. ;; EXPORT_DATE.
  178. (org-test-with-temp-text "#+DATE: today
  179. * Headline
  180. :PROPERTIES:
  181. :EXPORT_DATE: 29-03-2012
  182. :END:
  183. Paragraph"
  184. (forward-line)
  185. (should (equal (plist-get (org-export-get-environment nil t) :date)
  186. '("29-03-2012"))))
  187. ;; Properties with `split' behaviour are stored as a list of
  188. ;; strings.
  189. (should
  190. (equal '("a" "b")
  191. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  192. * Headline
  193. :PROPERTIES:
  194. :EXPORT_EXCLUDE_TAGS: a b
  195. :END:
  196. Paragraph"
  197. (progn
  198. (forward-line)
  199. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  200. ;; Handle :PROPERTY+: syntax.
  201. (should
  202. (equal '("a" "b")
  203. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  204. * Headline
  205. :PROPERTIES:
  206. :EXPORT_EXCLUDE_TAGS: a
  207. :EXPORT_EXCLUDE_TAGS+: b
  208. :END:
  209. Paragraph"
  210. (progn
  211. (forward-line)
  212. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  213. ;; Export properties are case-insensitive.
  214. (org-test-with-temp-text "* Headline
  215. :PROPERTIES:
  216. :EXPORT_Date: 29-03-2012
  217. :END:
  218. Paragraph"
  219. (should (equal (plist-get (org-export-get-environment nil t) :date)
  220. '("29-03-2012"))))
  221. ;; Still grab correct options when section above is empty.
  222. (should
  223. (equal '("H1")
  224. (org-test-with-temp-text "* H1\n** H11\n** H12"
  225. (progn (forward-line 2)
  226. (plist-get (org-export-get-environment nil t) :title))))))
  227. (ert-deftest test-org-export/handle-options ()
  228. "Test if export options have an impact on output."
  229. ;; Test exclude tags for headlines and inlinetasks.
  230. (should
  231. (equal ""
  232. (org-test-with-temp-text "* Head1 :noexp:"
  233. (org-test-with-backend test
  234. (org-export-as 'test nil nil nil '(:exclude-tags ("noexp")))))))
  235. ;; Test include tags for headlines and inlinetasks.
  236. (should
  237. (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
  238. (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
  239. (let ((org-tags-column 0))
  240. (org-test-with-backend test
  241. (org-export-as 'test nil nil nil '(:select-tags ("exp"))))))))
  242. ;; Test mixing include tags and exclude tags.
  243. (org-test-with-temp-text "
  244. * Head1 :export:
  245. ** Sub-Head1 :noexport:
  246. ** Sub-Head2
  247. * Head2 :noexport:
  248. ** Sub-Head1 :export:"
  249. (org-test-with-backend test
  250. (should
  251. (string-match
  252. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  253. (org-export-as
  254. 'test nil nil nil
  255. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  256. ;; Ignore tasks.
  257. (should
  258. (equal ""
  259. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  260. (org-test-with-temp-text "* TODO Head1"
  261. (org-test-with-backend test
  262. (org-export-as 'test nil nil nil '(:with-tasks nil)))))))
  263. (should
  264. (equal "* TODO Head1\n"
  265. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  266. (org-test-with-temp-text "* TODO Head1"
  267. (org-test-with-backend test
  268. (org-export-as 'test nil nil nil '(:with-tasks t)))))))
  269. ;; Archived tree.
  270. (org-test-with-temp-text "* Head1 :archive:"
  271. (let ((org-archive-tag "archive"))
  272. (org-test-with-backend test
  273. (should
  274. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  275. "")))))
  276. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  277. (let ((org-archive-tag "archive"))
  278. (org-test-with-backend test
  279. (should
  280. (string-match
  281. "\\* Head1[ \t]+:archive:"
  282. (org-export-as 'test nil nil nil
  283. '(:with-archived-trees headline)))))))
  284. (org-test-with-temp-text "* Head1 :archive:"
  285. (let ((org-archive-tag "archive"))
  286. (org-test-with-backend test
  287. (should
  288. (string-match
  289. "\\`\\* Head1[ \t]+:archive:\n\\'"
  290. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  291. ;; Clocks.
  292. (let ((org-clock-string "CLOCK:"))
  293. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  294. (org-test-with-backend test
  295. (should
  296. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  297. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  298. (should
  299. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  300. ;; Drawers.
  301. (let ((org-drawers '("TEST")))
  302. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  303. (org-test-with-backend test
  304. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  305. ""))
  306. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  307. ":TEST:\ncontents\n:END:\n")))))
  308. (let ((org-drawers '("FOO" "BAR")))
  309. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  310. (org-test-with-backend test
  311. (should
  312. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  313. ":FOO:\nkeep\n:END:\n")))))
  314. (let ((org-drawers '("FOO" "BAR")))
  315. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  316. (org-test-with-backend test
  317. (should
  318. (equal (org-export-as 'test nil nil nil '(:with-drawers (not "BAR")))
  319. ":FOO:\nkeep\n:END:\n")))))
  320. ;; Inlinetasks.
  321. (when (featurep 'org-inlinetask)
  322. (should
  323. (equal
  324. (let ((org-inlinetask-min-level 15))
  325. (org-test-with-temp-text "*************** Task"
  326. (org-test-with-backend test
  327. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  328. ""))
  329. (should
  330. (equal
  331. (let ((org-inlinetask-min-level 15))
  332. (org-test-with-temp-text
  333. "*************** Task\nContents\n*************** END"
  334. (org-test-with-backend test
  335. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  336. "")))
  337. ;; Plannings.
  338. (let ((org-closed-string "CLOSED:"))
  339. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  340. (org-test-with-backend test
  341. (should
  342. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  343. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  344. (should
  345. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  346. "")))))
  347. ;; Statistics cookies.
  348. (should
  349. (equal ""
  350. (org-test-with-temp-text "[0/0]"
  351. (org-test-with-backend test
  352. (org-export-as
  353. 'test nil nil nil '(:with-statistics-cookies nil))))))
  354. ;; Timestamps.
  355. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  356. (org-test-with-backend test
  357. (should
  358. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  359. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  360. (should
  361. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  362. (should
  363. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  364. "<2012-04-29 sun. 10:45>\n"))
  365. (should
  366. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  367. "[2012-04-29 sun. 10:45]\n")))))
  368. (ert-deftest test-org-export/comment-tree ()
  369. "Test if export process ignores commented trees."
  370. (let ((org-comment-string "COMMENT"))
  371. (org-test-with-temp-text "* COMMENT Head1"
  372. (org-test-with-backend test
  373. (should (equal (org-export-as 'test) ""))))))
  374. (ert-deftest test-org-export/export-scope ()
  375. "Test all export scopes."
  376. (org-test-with-temp-text "
  377. * Head1
  378. ** Head2
  379. text
  380. *** Head3"
  381. (org-test-with-backend test
  382. ;; Subtree.
  383. (forward-line 3)
  384. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  385. ;; Visible.
  386. (goto-char (point-min))
  387. (forward-line)
  388. (org-cycle)
  389. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  390. ;; Region.
  391. (goto-char (point-min))
  392. (forward-line 3)
  393. (transient-mark-mode 1)
  394. (push-mark (point) t t)
  395. (goto-char (point-at-eol))
  396. (should (equal (org-export-as 'test) "text\n"))))
  397. ;; Subtree with a code block calling another block outside.
  398. (should
  399. (equal ": 3\n"
  400. (org-test-with-temp-text "
  401. * Head1
  402. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  403. <<test>>
  404. #+END_SRC
  405. * Head2
  406. #+NAME: test
  407. #+BEGIN_SRC emacs-lisp
  408. \(+ 1 2)
  409. #+END_SRC"
  410. (org-test-with-backend test
  411. (forward-line 1)
  412. (org-export-as 'test 'subtree)))))
  413. ;; Body only.
  414. (org-test-with-temp-text "Text"
  415. (org-test-with-backend test
  416. (plist-put
  417. (cdr (assq 'test org-export-registered-backends))
  418. :translate-alist
  419. (cons (cons 'template (lambda (body info) (format "BEGIN\n%sEND" body)))
  420. (org-export-backend-translate-table 'test)))
  421. (should (equal (org-export-as 'test nil nil 'body-only) "Text\n"))
  422. (should (equal (org-export-as 'test) "BEGIN\nText\nEND")))))
  423. (ert-deftest test-org-export/output-file-name ()
  424. "Test `org-export-output-file-name' specifications."
  425. ;; Export from a file: name is built from original file name.
  426. (should
  427. (org-test-with-temp-text-in-file "Test"
  428. (equal (concat (file-name-as-directory ".")
  429. (file-name-nondirectory
  430. (file-name-sans-extension (buffer-file-name))))
  431. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  432. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  433. (should
  434. (org-test-with-temp-text-in-file
  435. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  436. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  437. ;; From a buffer not associated to a file, too.
  438. (should
  439. (org-test-with-temp-text
  440. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  441. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  442. ;; When provided name is absolute, preserve it.
  443. (should
  444. (org-test-with-temp-text
  445. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  446. (expand-file-name "test"))
  447. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  448. ;; When PUB-DIR argument is provided, use it.
  449. (should
  450. (org-test-with-temp-text-in-file "Test"
  451. (equal (file-name-directory
  452. (org-export-output-file-name ".ext" nil "dir/"))
  453. "dir/")))
  454. ;; When returned name would overwrite original file, add EXTENSION
  455. ;; another time.
  456. (should
  457. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  458. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  459. (ert-deftest test-org-export/expand-include ()
  460. "Test file inclusion in an Org buffer."
  461. ;; Error when file isn't specified.
  462. (should-error
  463. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  464. (org-export-expand-include-keyword)))
  465. ;; Full insertion with recursive inclusion.
  466. (org-test-with-temp-text
  467. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  468. (org-export-expand-include-keyword)
  469. (should (equal (buffer-string)
  470. "Small Org file with an include keyword.
  471. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  472. Success!
  473. * Heading
  474. body\n")))
  475. ;; Localized insertion.
  476. (org-test-with-temp-text
  477. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  478. org-test-dir)
  479. (org-export-expand-include-keyword)
  480. (should (equal (buffer-string)
  481. "Small Org file with an include keyword.\n")))
  482. ;; Insertion with constraints on headlines level.
  483. (should
  484. (equal
  485. "* Top heading\n** Heading\nbody\n"
  486. (org-test-with-temp-text
  487. (format
  488. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  489. org-test-dir)
  490. (org-export-expand-include-keyword)
  491. (buffer-string))))
  492. (should
  493. (equal
  494. "* Top heading\n* Heading\nbody\n"
  495. (org-test-with-temp-text
  496. (format
  497. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
  498. org-test-dir)
  499. (org-export-expand-include-keyword)
  500. (buffer-string))))
  501. ;; Inclusion within an example block.
  502. (org-test-with-temp-text
  503. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  504. org-test-dir)
  505. (org-export-expand-include-keyword)
  506. (should
  507. (equal
  508. (buffer-string)
  509. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  510. ;; Inclusion within a src-block.
  511. (org-test-with-temp-text
  512. (format
  513. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  514. org-test-dir)
  515. (org-export-expand-include-keyword)
  516. (should (equal (buffer-string)
  517. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  518. (ert-deftest test-org-export/expand-macro ()
  519. "Test macro expansion in an Org buffer."
  520. ;; Standard macro expansion.
  521. (should
  522. (equal "#+MACRO: macro1 value\nvalue\n"
  523. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  524. (org-test-with-backend test (org-export-as 'test)))))
  525. ;; Expand specific macros.
  526. (should
  527. (equal "me 2012-03-29 me@here Title\n"
  528. (org-test-with-temp-text
  529. "
  530. #+TITLE: Title
  531. #+DATE: 2012-03-29
  532. #+AUTHOR: me
  533. #+EMAIL: me@here
  534. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  535. (let ((output (org-test-with-backend test (org-export-as 'test))))
  536. (substring output (string-match ".*\n\\'" output))))))
  537. ;; Expand specific macros when property contained a regular macro
  538. ;; already.
  539. (should
  540. (equal "value\n"
  541. (org-test-with-temp-text "
  542. #+MACRO: macro1 value
  543. #+TITLE: {{{macro1}}}
  544. {{{title}}}"
  545. (let ((output (org-test-with-backend test (org-export-as 'test))))
  546. (substring output (string-match ".*\n\\'" output))))))
  547. ;; Expand macros with templates in included files.
  548. (should
  549. (equal "success\n"
  550. (org-test-with-temp-text
  551. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  552. {{{included-macro}}}" org-test-dir)
  553. (let ((output (org-test-with-backend test (org-export-as 'test))))
  554. (substring output (string-match ".*\n\\'" output)))))))
  555. (ert-deftest test-org-export/user-ignore-list ()
  556. "Test if `:ignore-list' accepts user input."
  557. (org-test-with-backend test
  558. (flet ((skip-note-head
  559. (data backend info)
  560. ;; Ignore headlines with the word "note" in their title.
  561. (org-element-map data 'headline
  562. (lambda (headline)
  563. (when (string-match "\\<note\\>"
  564. (org-element-property :raw-value headline))
  565. (org-export-ignore-element headline info)))
  566. info)
  567. data))
  568. ;; Install function in parse tree filters.
  569. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  570. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  571. (should (equal (org-export-as 'test) "* Head1\n")))))))
  572. (ert-deftest test-org-export/before-processing-hook ()
  573. "Test `org-export-before-processing-hook'."
  574. (should
  575. (equal
  576. "#+MACRO: mac val\nTest\n"
  577. (org-test-with-backend test
  578. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  579. (let ((org-export-before-processing-hook
  580. '((lambda (backend)
  581. (while (re-search-forward "{{{" nil t)
  582. (let ((object (org-element-context)))
  583. (when (eq (org-element-type object) 'macro)
  584. (delete-region
  585. (org-element-property :begin object)
  586. (org-element-property :end object)))))))))
  587. (org-export-as 'test)))))))
  588. (ert-deftest test-org-export/before-parsing-hook ()
  589. "Test `org-export-before-parsing-hook'."
  590. (should
  591. (equal "Body 1\nBody 2\n"
  592. (org-test-with-backend test
  593. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  594. (let ((org-export-before-parsing-hook
  595. '((lambda (backend)
  596. (goto-char (point-min))
  597. (while (re-search-forward org-outline-regexp-bol nil t)
  598. (delete-region
  599. (point-at-bol) (progn (forward-line) (point))))))))
  600. (org-export-as 'test)))))))
  601. ;;; Affiliated Keywords
  602. (ert-deftest test-org-export/read-attribute ()
  603. "Test `org-export-read-attribute' specifications."
  604. ;; Standard test.
  605. (should
  606. (equal
  607. (org-export-read-attribute
  608. :attr_html
  609. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  610. (org-element-at-point)))
  611. '(:a "1" :b "2")))
  612. ;; Return nil on empty attribute.
  613. (should-not
  614. (org-export-read-attribute
  615. :attr_html
  616. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  617. ;; Return nil on "nil" string.
  618. (should
  619. (equal '(:a nil :b nil)
  620. (org-export-read-attribute
  621. :attr_html
  622. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  623. (org-element-at-point)))))
  624. ;; Return nil on empty string.
  625. (should
  626. (equal '(:a nil :b nil)
  627. (org-export-read-attribute
  628. :attr_html
  629. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  630. (org-element-at-point)))))
  631. ;; Ignore text before first property.
  632. (should-not
  633. (member "ignore"
  634. (org-export-read-attribute
  635. :attr_html
  636. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  637. (org-element-at-point))))))
  638. (ert-deftest test-org-export/get-caption ()
  639. "Test `org-export-get-caption' specifications."
  640. ;; Without optional argument, return long caption
  641. (should
  642. (equal
  643. '("l")
  644. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  645. (org-export-get-caption (org-element-at-point)))))
  646. ;; With optional argument, return short caption.
  647. (should
  648. (equal
  649. '("s")
  650. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  651. (org-export-get-caption (org-element-at-point) t))))
  652. ;; Multiple lines are separated by white spaces.
  653. (should
  654. (equal
  655. '("a" " " "b")
  656. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  657. (org-export-get-caption (org-element-at-point))))))
  658. ;;; Back-End Tools
  659. (ert-deftest test-org-export/define-backend ()
  660. "Test back-end definition and accessors."
  661. ;; Translate table.
  662. (should
  663. (equal '((headline . my-headline-test))
  664. (let (org-export-registered-backends)
  665. (org-export-define-backend 'test '((headline . my-headline-test)))
  666. (org-export-backend-translate-table 'test))))
  667. ;; Filters.
  668. (should
  669. (equal '((:filter-headline . my-filter))
  670. (let (org-export-registered-backends)
  671. (org-export-define-backend 'test
  672. '((headline . my-headline-test))
  673. :filters-alist '((:filter-headline . my-filter)))
  674. (org-export-backend-filters 'test))))
  675. ;; Options.
  676. (should
  677. (equal '((:prop value))
  678. (let (org-export-registered-backends)
  679. (org-export-define-backend 'test
  680. '((headline . my-headline-test))
  681. :options-alist '((:prop value)))
  682. (org-export-backend-options 'test))))
  683. ;; Menu.
  684. (should
  685. (equal '(?k "Test Export" test)
  686. (let (org-export-registered-backends)
  687. (org-export-define-backend 'test
  688. '((headline . my-headline-test))
  689. :menu-entry '(?k "Test Export" test))
  690. (org-export-backend-menu 'test))))
  691. ;; Export Blocks.
  692. (should
  693. (equal '(("TEST" . org-element-export-block-parser))
  694. (let (org-export-registered-backends org-element-block-name-alist)
  695. (org-export-define-backend 'test
  696. '((headline . my-headline-test))
  697. :export-block '("test"))
  698. org-element-block-name-alist))))
  699. (ert-deftest test-org-export/define-derived-backend ()
  700. "Test `org-export-define-derived-backend' specifications."
  701. ;; Error when parent back-end is not defined.
  702. (should-error
  703. (let (org-export-registered-backends)
  704. (org-export-define-derived-backend 'test 'parent)))
  705. ;; Append translation table to parent's.
  706. (should
  707. (equal '((:headline . test) (:headline . parent))
  708. (let (org-export-registered-backends)
  709. (org-export-define-backend 'parent '((:headline . parent)))
  710. (org-export-define-derived-backend 'test 'parent
  711. :translate-alist '((:headline . test)))
  712. (org-export-backend-translate-table 'test))))
  713. ;; Options defined in the new back have priority over those defined
  714. ;; in parent.
  715. (should
  716. (eq 'test
  717. (let (org-export-registered-backends)
  718. (org-export-define-backend 'parent
  719. '((:headline . parent))
  720. :options-alist '((:a nil nil 'parent)))
  721. (org-export-define-derived-backend 'test 'parent
  722. :options-alist '((:a nil nil 'test)))
  723. (plist-get (org-export--get-global-options 'test) :a)))))
  724. (ert-deftest test-org-export/derived-backend-p ()
  725. "Test `org-export-derived-backend-p' specifications."
  726. ;; Non-nil with direct match.
  727. (should
  728. (let (org-export-registered-backends)
  729. (org-export-define-backend 'test '((headline . test)))
  730. (org-export-derived-backend-p 'test 'test)))
  731. (should
  732. (let (org-export-registered-backends)
  733. (org-export-define-backend 'test '((headline . test)))
  734. (org-export-define-derived-backend 'test2 'test)
  735. (org-export-derived-backend-p 'test2 'test2)))
  736. ;; Non-nil with a direct parent.
  737. (should
  738. (let (org-export-registered-backends)
  739. (org-export-define-backend 'test '((headline . test)))
  740. (org-export-define-derived-backend 'test2 'test)
  741. (org-export-derived-backend-p 'test2 'test)))
  742. ;; Non-nil with an indirect parent.
  743. (should
  744. (let (org-export-registered-backends)
  745. (org-export-define-backend 'test '((headline . test)))
  746. (org-export-define-derived-backend 'test2 'test)
  747. (org-export-define-derived-backend 'test3 'test2)
  748. (org-export-derived-backend-p 'test3 'test)))
  749. ;; Nil otherwise.
  750. (should-not
  751. (let (org-export-registered-backends)
  752. (org-export-define-backend 'test '((headline . test)))
  753. (org-export-define-backend 'test2 '((headline . test2)))
  754. (org-export-derived-backend-p 'test2 'test)))
  755. (should-not
  756. (let (org-export-registered-backends)
  757. (org-export-define-backend 'test '((headline . test)))
  758. (org-export-define-backend 'test2 '((headline . test2)))
  759. (org-export-define-derived-backend 'test3 'test2)
  760. (org-export-derived-backend-p 'test3 'test))))
  761. (ert-deftest test-org-export/with-backend ()
  762. "Test `org-export-with-backend' definition."
  763. ;; Error when calling an undefined back-end
  764. (should-error
  765. (let (org-export-registered-backends)
  766. (org-export-with-backend 'test "Test")))
  767. ;; Error when called back-end doesn't have an appropriate
  768. ;; transcoder.
  769. (should-error
  770. (let (org-export-registered-backends)
  771. (org-export-define-backend 'test ((headline . ignore)))
  772. (org-export-with-backend 'test "Test")))
  773. ;; Otherwise, export using correct transcoder
  774. (should
  775. (equal "Success"
  776. (let (org-export-registered-backends)
  777. (org-export-define-backend 'test
  778. '((plain-text . (lambda (text contents info) "Failure"))))
  779. (org-export-define-backend 'test2
  780. '((plain-text . (lambda (text contents info) "Success"))))
  781. (org-export-with-backend 'test2 "Test")))))
  782. (ert-deftest test-org-export/data-with-translations ()
  783. "Test `org-export-data-with-translations' specifications."
  784. (should
  785. (equal
  786. "Success!"
  787. (org-export-data-with-translations
  788. '(bold nil "Test")
  789. '((plain-text . (lambda (text info) "Success"))
  790. (bold . (lambda (bold contents info) (concat contents "!"))))
  791. '(:with-emphasize t)))))
  792. (ert-deftest test-org-export/data-with-backend ()
  793. "Test `org-export-data-with-backend' specifications."
  794. ;; Error when calling an undefined back-end.
  795. (should-error
  796. (let (org-export-registered-backends)
  797. (org-export-data-with-backend 'test "Test" nil)))
  798. ;; Otherwise, export data recursively, using correct back-end.
  799. (should
  800. (equal
  801. "Success!"
  802. (let (org-export-registered-backends)
  803. (org-export-define-backend 'test
  804. '((plain-text . (lambda (text info) "Success"))
  805. (bold . (lambda (bold contents info) (concat contents "!")))))
  806. (org-export-data-with-backend
  807. '(bold nil "Test") 'test '(:with-emphasize t))))))
  808. ;;; Export Snippets
  809. (ert-deftest test-org-export/export-snippet ()
  810. "Test export snippets transcoding."
  811. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  812. (org-test-with-backend test
  813. (plist-put
  814. (cdr (assq 'test org-export-registered-backends))
  815. :translate-alist
  816. (cons (cons 'export-snippet
  817. (lambda (snippet contents info)
  818. (when (eq (org-export-snippet-backend snippet) 'test)
  819. (org-element-property :value snippet))))
  820. (org-export-backend-translate-table 'test)))
  821. (let ((org-export-snippet-translation-alist nil))
  822. (should (equal (org-export-as 'test) "A\n")))
  823. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  824. (should (equal (org-export-as 'test) "AB\n")))))
  825. ;; Ignored export snippets do not remove any blank.
  826. (should
  827. (equal "begin end\n"
  828. (org-test-with-parsed-data "begin @@test:A@@ end"
  829. (org-export-data-with-translations
  830. tree
  831. '((paragraph . (lambda (paragraph contents info) contents))
  832. (section . (lambda (section contents info) contents)))
  833. info)))))
  834. ;;; Footnotes
  835. (ert-deftest test-org-export/footnotes ()
  836. "Test footnotes specifications."
  837. (let ((org-footnote-section nil)
  838. (org-export-with-footnotes t))
  839. ;; 1. Read every type of footnote.
  840. (should
  841. (equal
  842. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  843. (org-test-with-parsed-data
  844. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  845. (org-element-map tree 'footnote-reference
  846. (lambda (ref)
  847. (let ((def (org-export-get-footnote-definition ref info)))
  848. (cons (org-export-get-footnote-number ref info)
  849. (if (eq (org-element-property :type ref) 'inline) (car def)
  850. (car (org-element-contents
  851. (car (org-element-contents def))))))))
  852. info))))
  853. ;; 2. Test nested footnotes order.
  854. (org-test-with-parsed-data
  855. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  856. (should
  857. (equal
  858. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  859. (org-element-map tree 'footnote-reference
  860. (lambda (ref)
  861. (when (org-export-footnote-first-reference-p ref info)
  862. (cons (org-export-get-footnote-number ref info)
  863. (org-element-property :label ref))))
  864. info))))
  865. ;; 3. Test nested footnote in invisible definitions.
  866. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  867. ;; Hide definitions.
  868. (narrow-to-region (point) (point-at-eol))
  869. (let* ((tree (org-element-parse-buffer))
  870. (info (org-combine-plists
  871. `(:parse-tree ,tree)
  872. (org-export-collect-tree-properties
  873. tree (org-export-get-environment)))))
  874. ;; Both footnotes should be seen.
  875. (should
  876. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  877. ;; 4. Test footnotes definitions collection.
  878. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  879. \[fn:2] B [fn:3] [fn::D].
  880. \[fn:3] C."
  881. (should (= (length (org-export-collect-footnote-definitions tree info))
  882. 4)))
  883. ;; 5. Test export of footnotes defined outside parsing scope.
  884. (org-test-with-temp-text "[fn:1] Out of scope
  885. * Title
  886. Paragraph[fn:1]"
  887. (org-test-with-backend test
  888. (plist-put
  889. (cdr (assq 'test org-export-registered-backends))
  890. :translate-alist
  891. (cons (cons 'footnote-reference
  892. (lambda (fn contents info)
  893. (org-element-interpret-data
  894. (org-export-get-footnote-definition fn info))))
  895. (org-export-backend-translate-table 'test)))
  896. (forward-line)
  897. (should (equal "ParagraphOut of scope\n"
  898. (org-export-as 'test 'subtree)))))
  899. ;; 6. Footnotes without a definition should be provided a fallback
  900. ;; definition.
  901. (should
  902. (org-test-with-parsed-data "[fn:1]"
  903. (org-export-get-footnote-definition
  904. (org-element-map tree 'footnote-reference 'identity info t) info)))
  905. ;; 7. Footnote section should be ignored in TOC and in headlines
  906. ;; numbering.
  907. (should
  908. (= 1 (let ((org-footnote-section "Footnotes"))
  909. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  910. (org-export-collect-headlines info))))))
  911. (should
  912. (equal '(2)
  913. (let ((org-footnote-section "Footnotes"))
  914. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  915. (org-element-map tree 'headline
  916. (lambda (hl)
  917. (when (equal (org-element-property :raw-value hl) "H2")
  918. (org-export-get-headline-number hl info)))
  919. info t)))))))
  920. ;;; Headlines and Inlinetasks
  921. (ert-deftest test-org-export/get-relative-level ()
  922. "Test `org-export-get-relative-level' specifications."
  923. ;; Standard test.
  924. (should
  925. (equal '(1 2)
  926. (let ((org-odd-levels-only nil))
  927. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  928. (org-element-map tree 'headline
  929. (lambda (h) (org-export-get-relative-level h info))
  930. info)))))
  931. ;; Missing levels
  932. (should
  933. (equal '(1 3)
  934. (let ((org-odd-levels-only nil))
  935. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  936. (org-element-map tree 'headline
  937. (lambda (h) (org-export-get-relative-level h info))
  938. info))))))
  939. (ert-deftest test-org-export/low-level-p ()
  940. "Test `org-export-low-level-p' specifications."
  941. (should
  942. (equal
  943. '(no yes)
  944. (let ((org-odd-levels-only nil))
  945. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  946. (org-element-map tree 'headline
  947. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  948. (plist-put info :headline-levels 1)))))))
  949. (ert-deftest test-org-export/get-headline-number ()
  950. "Test `org-export-get-headline-number' specifications."
  951. ;; Standard test.
  952. (should
  953. (equal
  954. '((1) (1 1))
  955. (let ((org-odd-levels-only nil))
  956. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  957. (org-element-map tree 'headline
  958. (lambda (h) (org-export-get-headline-number h info))
  959. info)))))
  960. ;; Missing levels are replaced with 0.
  961. (should
  962. (equal
  963. '((1) (1 0 1))
  964. (let ((org-odd-levels-only nil))
  965. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  966. (org-element-map tree 'headline
  967. (lambda (h) (org-export-get-headline-number h info))
  968. info))))))
  969. (ert-deftest test-org-export/numbered-headline-p ()
  970. "Test `org-export-numbered-headline-p' specifications."
  971. ;; If `:section-numbers' is nil, never number headlines.
  972. (should-not
  973. (org-test-with-parsed-data "* Headline"
  974. (org-element-map tree 'headline
  975. (lambda (h) (org-export-numbered-headline-p h info))
  976. (plist-put info :section-numbers nil))))
  977. ;; If `:section-numbers' is a number, only number headlines with
  978. ;; a level greater that it.
  979. (should
  980. (equal
  981. '(yes no)
  982. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  983. (org-element-map tree 'headline
  984. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  985. (plist-put info :section-numbers 1)))))
  986. ;; Otherwise, headlines are always numbered.
  987. (should
  988. (org-test-with-parsed-data "* Headline"
  989. (org-element-map tree 'headline
  990. (lambda (h) (org-export-numbered-headline-p h info))
  991. (plist-put info :section-numbers t)))))
  992. (ert-deftest test-org-export/number-to-roman ()
  993. "Test `org-export-number-to-roman' specifications."
  994. ;; If number is negative, return it as a string.
  995. (should (equal (org-export-number-to-roman -1) "-1"))
  996. ;; Otherwise, return it as a roman number.
  997. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  998. (ert-deftest test-org-export/get-optional-title ()
  999. "Test `org-export-get-alt-title' specifications."
  1000. ;; If ALT_TITLE property is defined, use it.
  1001. (should
  1002. (equal '("opt")
  1003. (org-test-with-parsed-data
  1004. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1005. (org-export-get-alt-title
  1006. (org-element-map tree 'headline 'identity info t)
  1007. info))))
  1008. ;; Otherwise, fall-back to regular title.
  1009. (should
  1010. (equal '("Headline")
  1011. (org-test-with-parsed-data "* Headline"
  1012. (org-export-get-alt-title
  1013. (org-element-map tree 'headline 'identity info t)
  1014. info)))))
  1015. (ert-deftest test-org-export/get-tags ()
  1016. "Test `org-export-get-tags' specifications."
  1017. (let ((org-export-exclude-tags '("noexport"))
  1018. (org-export-select-tags '("export")))
  1019. ;; Standard test: tags which are not a select tag, an exclude tag,
  1020. ;; or specified as optional argument shouldn't be ignored.
  1021. (should
  1022. (org-test-with-parsed-data "* Headline :tag:"
  1023. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1024. info)))
  1025. ;; Exclude tags are removed.
  1026. (should-not
  1027. (org-test-with-parsed-data "* Headline :noexport:"
  1028. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1029. info)))
  1030. ;; Select tags are removed.
  1031. (should-not
  1032. (org-test-with-parsed-data "* Headline :export:"
  1033. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1034. info)))
  1035. (should
  1036. (equal
  1037. '("tag")
  1038. (org-test-with-parsed-data "* Headline :tag:export:"
  1039. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1040. info))))
  1041. ;; Tags provided in the optional argument are also ignored.
  1042. (should-not
  1043. (org-test-with-parsed-data "* Headline :ignore:"
  1044. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1045. info '("ignore"))))
  1046. ;; Allow tag inheritance.
  1047. (should
  1048. (equal
  1049. '(("tag") ("tag"))
  1050. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1051. (org-element-map tree 'headline
  1052. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1053. ;; Tag inheritance checks FILETAGS keywords.
  1054. (should
  1055. (equal
  1056. '(("a" "b" "tag"))
  1057. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1058. (org-element-map tree 'headline
  1059. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1060. (ert-deftest test-org-export/get-node-property ()
  1061. "Test`org-export-get-node-property' specifications."
  1062. ;; Standard test.
  1063. (should
  1064. (equal "value"
  1065. (org-test-with-parsed-data "* Headline
  1066. :PROPERTIES:
  1067. :prop: value
  1068. :END:"
  1069. (org-export-get-node-property
  1070. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1071. ;; Test inheritance.
  1072. (should
  1073. (equal "value"
  1074. (org-test-with-parsed-data "* Parent
  1075. :PROPERTIES:
  1076. :prop: value
  1077. :END:
  1078. ** Headline
  1079. Paragraph"
  1080. (org-export-get-node-property
  1081. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1082. ;; Cannot return a value before the first headline.
  1083. (should-not
  1084. (org-test-with-parsed-data "Paragraph
  1085. * Headline
  1086. :PROPERTIES:
  1087. :prop: value
  1088. :END:"
  1089. (org-export-get-node-property
  1090. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1091. (ert-deftest test-org-export/get-category ()
  1092. "Test `org-export-get-category' specifications."
  1093. ;; Standard test.
  1094. (should
  1095. (equal "value"
  1096. (org-test-with-parsed-data "* Headline
  1097. :PROPERTIES:
  1098. :CATEGORY: value
  1099. :END:"
  1100. (org-export-get-category
  1101. (org-element-map tree 'headline 'identity nil t) info))))
  1102. ;; Test inheritance from a parent headline.
  1103. (should
  1104. (equal '("value" "value")
  1105. (org-test-with-parsed-data "* Headline1
  1106. :PROPERTIES:
  1107. :CATEGORY: value
  1108. :END:
  1109. ** Headline2"
  1110. (org-element-map tree 'headline
  1111. (lambda (hl) (org-export-get-category hl info)) info))))
  1112. ;; Test inheritance from #+CATEGORY keyword
  1113. (should
  1114. (equal "value"
  1115. (org-test-with-parsed-data "#+CATEGORY: value
  1116. * Headline"
  1117. (org-export-get-category
  1118. (org-element-map tree 'headline 'identity nil t) info))))
  1119. ;; Test inheritance from file name.
  1120. (should
  1121. (equal "test"
  1122. (org-test-with-parsed-data "* Headline"
  1123. (let ((info (plist-put info :input-file "~/test.org")))
  1124. (org-export-get-category
  1125. (org-element-map tree 'headline 'identity nil t) info)))))
  1126. ;; Fall-back value.
  1127. (should
  1128. (equal "???"
  1129. (org-test-with-parsed-data "* Headline"
  1130. (org-export-get-category
  1131. (org-element-map tree 'headline 'identity nil t) info)))))
  1132. (ert-deftest test-org-export/first-sibling-p ()
  1133. "Test `org-export-first-sibling-p' specifications."
  1134. ;; Standard test.
  1135. (should
  1136. (equal
  1137. '(yes yes no)
  1138. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1139. (org-element-map tree 'headline
  1140. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1141. info))))
  1142. ;; Ignore headlines not exported.
  1143. (should
  1144. (equal
  1145. '(yes)
  1146. (let ((org-export-exclude-tags '("ignore")))
  1147. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1148. (org-element-map tree 'headline
  1149. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1150. info))))))
  1151. (ert-deftest test-org-export/last-sibling-p ()
  1152. "Test `org-export-last-sibling-p' specifications."
  1153. ;; Standard test.
  1154. (should
  1155. (equal
  1156. '(yes no yes)
  1157. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1158. (org-element-map tree 'headline
  1159. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1160. info))))
  1161. ;; Ignore headlines not exported.
  1162. (should
  1163. (equal
  1164. '(yes)
  1165. (let ((org-export-exclude-tags '("ignore")))
  1166. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1167. (org-element-map tree 'headline
  1168. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1169. info))))))
  1170. (ert-deftest test-org-export/handle-inlinetasks ()
  1171. "Test inlinetask export."
  1172. ;; Inlinetask with an exclude tag.
  1173. (when (featurep 'org-inlinetask)
  1174. (should
  1175. (equal
  1176. ""
  1177. (let ((org-inlinetask-min-level 3))
  1178. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  1179. (org-test-with-backend test
  1180. (org-export-as 'test nil nil nil '(:exclude-tags ("noexp"))))))))
  1181. ;; Inlinetask with an include tag.
  1182. (should
  1183. (equal
  1184. "* H2\n*** Inline :exp:\n"
  1185. (let ((org-inlinetask-min-level 3)
  1186. (org-tags-column 0))
  1187. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  1188. (org-test-with-backend test
  1189. (org-export-as 'test nil nil nil '(:select-tags ("exp"))))))))
  1190. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  1191. (should
  1192. (equal ""
  1193. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  1194. (org-inlinetask-min-level 3))
  1195. (org-test-with-temp-text "*** TODO Inline"
  1196. (org-test-with-backend test
  1197. (org-export-as 'test nil nil nil '(:with-tasks nil)))))))))
  1198. ;;; Links
  1199. (ert-deftest test-org-export/get-coderef-format ()
  1200. "Test `org-export-get-coderef-format' specifications."
  1201. ;; A link without description returns "%s"
  1202. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1203. "%s"))
  1204. ;; Return "%s" when path is matched within description.
  1205. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1206. "desc %s"))
  1207. ;; Otherwise return description.
  1208. (should (equal (org-export-get-coderef-format "path" "desc")
  1209. "desc")))
  1210. (ert-deftest test-org-export/inline-image-p ()
  1211. "Test `org-export-inline-image-p' specifications."
  1212. (should
  1213. (org-export-inline-image-p
  1214. (org-test-with-temp-text "[[#id]]"
  1215. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  1216. '(("custom-id" . "id")))))
  1217. (ert-deftest test-org-export/fuzzy-link ()
  1218. "Test fuzzy links specifications."
  1219. ;; 1. Links to invisible (keyword) targets should be ignored.
  1220. (org-test-with-parsed-data
  1221. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  1222. (should-not
  1223. (org-element-map tree 'link
  1224. (lambda (link)
  1225. (org-export-get-ordinal
  1226. (org-export-resolve-fuzzy-link link info) info)) info)))
  1227. ;; 2. Link to an headline should return headline's number.
  1228. (org-test-with-parsed-data
  1229. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1230. (should
  1231. ;; Note: Headline's number is in fact a list of numbers.
  1232. (equal '(2)
  1233. (org-element-map tree 'link
  1234. (lambda (link)
  1235. (org-export-get-ordinal
  1236. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1237. ;; 3. Link to a target in an item should return item's number.
  1238. (org-test-with-parsed-data
  1239. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1240. (should
  1241. ;; Note: Item's number is in fact a list of numbers.
  1242. (equal '(1 2)
  1243. (org-element-map tree 'link
  1244. (lambda (link)
  1245. (org-export-get-ordinal
  1246. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1247. ;; 4. Link to a target in a footnote should return footnote's
  1248. ;; number.
  1249. (org-test-with-parsed-data "
  1250. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1251. (should
  1252. (equal '(2 3)
  1253. (org-element-map tree 'link
  1254. (lambda (link)
  1255. (org-export-get-ordinal
  1256. (org-export-resolve-fuzzy-link link info) info)) info))))
  1257. ;; 5. Link to a named element should return sequence number of that
  1258. ;; element.
  1259. (org-test-with-parsed-data
  1260. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1261. (should
  1262. (= 2
  1263. (org-element-map tree 'link
  1264. (lambda (link)
  1265. (org-export-get-ordinal
  1266. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1267. ;; 6. Link to a target not within an item, a table, a footnote
  1268. ;; reference or definition should return section number.
  1269. (org-test-with-parsed-data
  1270. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1271. (should
  1272. (equal '(2)
  1273. (org-element-map tree 'link
  1274. (lambda (link)
  1275. (org-export-get-ordinal
  1276. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1277. ;; 7. Space are not significant when matching a fuzzy link.
  1278. (should
  1279. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  1280. (org-element-map tree 'link
  1281. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1282. info t)))
  1283. ;; 8. Statistics cookies are ignored for headline match.
  1284. (should
  1285. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  1286. (org-element-map tree 'link
  1287. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1288. info t)))
  1289. (should
  1290. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  1291. (org-element-map tree 'link
  1292. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1293. info t))))
  1294. (ert-deftest test-org-export/resolve-coderef ()
  1295. "Test `org-export-resolve-coderef' specifications."
  1296. (let ((org-coderef-label-format "(ref:%s)"))
  1297. ;; 1. A link to a "-n -k -r" block returns line number.
  1298. (org-test-with-parsed-data
  1299. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1300. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1301. (org-test-with-parsed-data
  1302. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1303. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1304. ;; 2. A link to a "-n -r" block returns line number.
  1305. (org-test-with-parsed-data
  1306. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1307. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1308. (org-test-with-parsed-data
  1309. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1310. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1311. ;; 3. A link to a "-n" block returns coderef.
  1312. (org-test-with-parsed-data
  1313. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1314. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1315. (org-test-with-parsed-data
  1316. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1317. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1318. ;; 4. A link to a "-r" block returns line number.
  1319. (org-test-with-parsed-data
  1320. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1321. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1322. (org-test-with-parsed-data
  1323. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1324. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1325. ;; 5. A link to a block without a switch returns coderef.
  1326. (org-test-with-parsed-data
  1327. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1328. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1329. (org-test-with-parsed-data
  1330. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1331. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1332. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1333. ;; should resume numbering from previous block with numbered
  1334. ;; lines, ignoring blocks not numbering lines in the process.
  1335. ;; A "-n" switch resets count.
  1336. (org-test-with-parsed-data "
  1337. #+BEGIN_EXAMPLE -n
  1338. Text.
  1339. #+END_EXAMPLE
  1340. #+BEGIN_SRC emacs-lisp
  1341. \(- 1 1)
  1342. #+END_SRC
  1343. #+BEGIN_SRC emacs-lisp +n -r
  1344. \(+ 1 1) (ref:addition)
  1345. #+END_SRC
  1346. #+BEGIN_EXAMPLE -n -r
  1347. Another text. (ref:text)
  1348. #+END_EXAMPLE"
  1349. (should (= (org-export-resolve-coderef "addition" info) 2))
  1350. (should (= (org-export-resolve-coderef "text" info) 1)))
  1351. ;; 7. Recognize coderef with user-specified syntax.
  1352. (org-test-with-parsed-data
  1353. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1354. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1355. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1356. "Test `org-export-resolve-fuzzy-link' specifications."
  1357. ;; 1. Match target objects.
  1358. (org-test-with-parsed-data "<<target>> [[target]]"
  1359. (should
  1360. (org-export-resolve-fuzzy-link
  1361. (org-element-map tree 'link 'identity info t) info)))
  1362. ;; 2. Match target elements.
  1363. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  1364. (should
  1365. (org-export-resolve-fuzzy-link
  1366. (org-element-map tree 'link 'identity info t) info)))
  1367. ;; 3. Match named elements.
  1368. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1369. (should
  1370. (org-export-resolve-fuzzy-link
  1371. (org-element-map tree 'link 'identity info t) info)))
  1372. ;; 4. Match exact headline's name.
  1373. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1374. (should
  1375. (org-export-resolve-fuzzy-link
  1376. (org-element-map tree 'link 'identity info t) info)))
  1377. ;; 5. Targets objects have priority over named elements and headline
  1378. ;; titles.
  1379. (org-test-with-parsed-data
  1380. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1381. (should
  1382. (eq 'target
  1383. (org-element-type
  1384. (org-export-resolve-fuzzy-link
  1385. (org-element-map tree 'link 'identity info t) info)))))
  1386. ;; 6. Named elements have priority over headline titles.
  1387. (org-test-with-parsed-data
  1388. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1389. (should
  1390. (eq 'paragraph
  1391. (org-element-type
  1392. (org-export-resolve-fuzzy-link
  1393. (org-element-map tree 'link 'identity info t) info)))))
  1394. ;; 7. If link's path starts with a "*", only match headline titles,
  1395. ;; though.
  1396. (org-test-with-parsed-data
  1397. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1398. (should
  1399. (eq 'headline
  1400. (org-element-type
  1401. (org-export-resolve-fuzzy-link
  1402. (org-element-map tree 'link 'identity info t) info)))))
  1403. ;; 8. Return nil if no match.
  1404. (org-test-with-parsed-data "[[target]]"
  1405. (should-not
  1406. (org-export-resolve-fuzzy-link
  1407. (org-element-map tree 'link 'identity info t) info))))
  1408. (ert-deftest test-org-export/resolve-id-link ()
  1409. "Test `org-export-resolve-id-link' specifications."
  1410. ;; 1. Regular test for custom-id link.
  1411. (org-test-with-parsed-data "* Headline1
  1412. :PROPERTIES:
  1413. :CUSTOM_ID: test
  1414. :END:
  1415. * Headline 2
  1416. \[[#test]]"
  1417. (should
  1418. (org-export-resolve-id-link
  1419. (org-element-map tree 'link 'identity info t) info)))
  1420. ;; 2. Failing test for custom-id link.
  1421. (org-test-with-parsed-data "* Headline1
  1422. :PROPERTIES:
  1423. :CUSTOM_ID: test
  1424. :END:
  1425. * Headline 2
  1426. \[[#no-match]]"
  1427. (should-not
  1428. (org-export-resolve-id-link
  1429. (org-element-map tree 'link 'identity info t) info)))
  1430. ;; 3. Test for internal id target.
  1431. (org-test-with-parsed-data "* Headline1
  1432. :PROPERTIES:
  1433. :ID: aaaa
  1434. :END:
  1435. * Headline 2
  1436. \[[id:aaaa]]"
  1437. (should
  1438. (org-export-resolve-id-link
  1439. (org-element-map tree 'link 'identity info t) info)))
  1440. ;; 4. Test for external id target.
  1441. (org-test-with-parsed-data "[[id:aaaa]]"
  1442. (should
  1443. (org-export-resolve-id-link
  1444. (org-element-map tree 'link 'identity info t)
  1445. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1446. (ert-deftest test-org-export/resolve-radio-link ()
  1447. "Test `org-export-resolve-radio-link' specifications."
  1448. ;; Standard test.
  1449. (should
  1450. (org-test-with-temp-text "<<<radio>>> radio"
  1451. (org-update-radio-target-regexp)
  1452. (let* ((tree (org-element-parse-buffer))
  1453. (info `(:parse-tree ,tree)))
  1454. (org-export-resolve-radio-link
  1455. (org-element-map tree 'link 'identity info t)
  1456. info))))
  1457. ;; Radio targets are case-insensitive.
  1458. (should
  1459. (org-test-with-temp-text "<<<RADIO>>> radio"
  1460. (org-update-radio-target-regexp)
  1461. (let* ((tree (org-element-parse-buffer))
  1462. (info `(:parse-tree ,tree)))
  1463. (org-export-resolve-radio-link
  1464. (org-element-map tree 'link 'identity info t)
  1465. info))))
  1466. ;; Radio target with objects.
  1467. (should
  1468. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1469. (org-update-radio-target-regexp)
  1470. (let* ((tree (org-element-parse-buffer))
  1471. (info `(:parse-tree ,tree)))
  1472. (org-export-resolve-radio-link
  1473. (org-element-map tree 'link 'identity info t)
  1474. info)))))
  1475. ;;; Src-block and example-block
  1476. (ert-deftest test-org-export/unravel-code ()
  1477. "Test `org-export-unravel-code' function."
  1478. (let ((org-coderef-label-format "(ref:%s)"))
  1479. ;; 1. Code without reference.
  1480. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1481. (should (equal (org-export-unravel-code (org-element-at-point))
  1482. '("(+ 1 1)\n"))))
  1483. ;; 2. Code with reference.
  1484. (org-test-with-temp-text
  1485. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1486. (should (equal (org-export-unravel-code (org-element-at-point))
  1487. '("(+ 1 1)\n" (1 . "test")))))
  1488. ;; 3. Code with user-defined reference.
  1489. (org-test-with-temp-text
  1490. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1491. (should (equal (org-export-unravel-code (org-element-at-point))
  1492. '("(+ 1 1)\n" (1 . "test")))))
  1493. ;; 4. Code references keys are relative to the current block.
  1494. (org-test-with-temp-text "
  1495. #+BEGIN_EXAMPLE -n
  1496. \(+ 1 1)
  1497. #+END_EXAMPLE
  1498. #+BEGIN_EXAMPLE +n
  1499. \(+ 2 2)
  1500. \(+ 3 3) (ref:one)
  1501. #+END_EXAMPLE"
  1502. (goto-line 5)
  1503. (should (equal (org-export-unravel-code (org-element-at-point))
  1504. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1505. (ert-deftest test-org-export/format-code-default ()
  1506. "Test `org-export-format-code-default' specifications."
  1507. ;; Return the empty string when code is empty.
  1508. (should
  1509. (equal ""
  1510. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  1511. (org-export-format-code-default
  1512. (org-element-map tree 'src-block 'identity info t) info))))
  1513. ;; Number lines, two whitespace characters before the actual loc.
  1514. (should
  1515. (equal "1 a\n2 b\n"
  1516. (org-test-with-parsed-data
  1517. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  1518. (org-export-format-code-default
  1519. (org-element-map tree 'src-block 'identity info t) info))))
  1520. ;; Put references 6 whitespace characters after the widest line,
  1521. ;; wrapped within parenthesis.
  1522. (should
  1523. (equal "123 (a)\n1 (b)\n"
  1524. (let ((org-coderef-label-format "(ref:%s)"))
  1525. (org-test-with-parsed-data
  1526. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  1527. (org-export-format-code-default
  1528. (org-element-map tree 'src-block 'identity info t) info))))))
  1529. ;;; Smart Quotes
  1530. (ert-deftest test-org-export/activate-smart-quotes ()
  1531. "Test `org-export-activate-smart-quotes' specifications."
  1532. ;; Opening double quotes: standard test.
  1533. (should
  1534. (equal
  1535. '("some &ldquo;paragraph")
  1536. (let ((org-export-default-language "en"))
  1537. (org-test-with-parsed-data "some \"paragraph"
  1538. (org-element-map tree 'plain-text
  1539. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1540. info)))))
  1541. ;; Opening quotes: at the beginning of a paragraph.
  1542. (should
  1543. (equal
  1544. '("&ldquo;begin")
  1545. (let ((org-export-default-language "en"))
  1546. (org-test-with-parsed-data "\"begin"
  1547. (org-element-map tree 'plain-text
  1548. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1549. info)))))
  1550. ;; Opening quotes: after an object.
  1551. (should
  1552. (equal
  1553. '("&ldquo;begin")
  1554. (let ((org-export-default-language "en"))
  1555. (org-test-with-parsed-data "=verb= \"begin"
  1556. (org-element-map tree 'plain-text
  1557. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1558. info)))))
  1559. ;; Closing quotes: standard test.
  1560. (should
  1561. (equal
  1562. '("some&rdquo; paragraph")
  1563. (let ((org-export-default-language "en"))
  1564. (org-test-with-parsed-data "some\" paragraph"
  1565. (org-element-map tree 'plain-text
  1566. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1567. info)))))
  1568. ;; Closing quotes: at the end of a paragraph.
  1569. (should
  1570. (equal
  1571. '("end&rdquo;")
  1572. (let ((org-export-default-language "en"))
  1573. (org-test-with-parsed-data "end\""
  1574. (org-element-map tree 'plain-text
  1575. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1576. info)))))
  1577. ;; Apostrophe: standard test.
  1578. (should
  1579. (equal
  1580. '("It shouldn&rsquo;t fail")
  1581. (let ((org-export-default-language "en"))
  1582. (org-test-with-parsed-data "It shouldn't fail"
  1583. (org-element-map tree 'plain-text
  1584. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1585. info)))))
  1586. ;; Apostrophe: before an object.
  1587. (should
  1588. (equal
  1589. '("a&rsquo;")
  1590. (let ((org-export-default-language "en"))
  1591. (org-test-with-parsed-data "a'=b="
  1592. (org-element-map tree 'plain-text
  1593. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1594. info)))))
  1595. ;; Apostrophe: after an object.
  1596. (should
  1597. (equal
  1598. '("&rsquo;s")
  1599. (let ((org-export-default-language "en"))
  1600. (org-test-with-parsed-data "=code='s"
  1601. (org-element-map tree 'plain-text
  1602. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1603. info)))))
  1604. ;; Special case: isolated quotes.
  1605. (should
  1606. (equal '("&ldquo;" "&rdquo;")
  1607. (let ((org-export-default-language "en"))
  1608. (org-test-with-parsed-data "\"$x$\""
  1609. (org-element-map tree 'plain-text
  1610. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1611. info)))))
  1612. ;; Smart quotes in secondary strings.
  1613. (should
  1614. (equal '("&ldquo;" "&rdquo;")
  1615. (let ((org-export-default-language "en"))
  1616. (org-test-with-parsed-data "* \"$x$\""
  1617. (org-element-map tree 'plain-text
  1618. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1619. info)))))
  1620. ;; Smart quotes in document keywords.
  1621. (should
  1622. (equal '("&ldquo;" "&rdquo;")
  1623. (let ((org-export-default-language "en"))
  1624. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1625. (org-element-map (plist-get info :title) 'plain-text
  1626. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1627. info)))))
  1628. ;; Smart quotes in parsed affiliated keywords.
  1629. (should
  1630. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1631. (let ((org-export-default-language "en"))
  1632. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1633. (org-element-map tree 'plain-text
  1634. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1635. info nil nil t))))))
  1636. ;;; Tables
  1637. (ert-deftest test-org-export/special-column ()
  1638. "Test if the table's special column is properly recognized."
  1639. ;; 1. First column is special if it contains only a special marking
  1640. ;; characters or empty cells.
  1641. (org-test-with-temp-text "
  1642. | ! | 1 |
  1643. | | 2 |"
  1644. (should
  1645. (org-export-table-has-special-column-p
  1646. (org-element-map
  1647. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1648. ;; 2. If the column contains anything else, it isn't special.
  1649. (org-test-with-temp-text "
  1650. | ! | 1 |
  1651. | b | 2 |"
  1652. (should-not
  1653. (org-export-table-has-special-column-p
  1654. (org-element-map
  1655. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1656. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1657. ;; and "!".
  1658. (org-test-with-temp-text "
  1659. | # | 1 |
  1660. | ^ | 2 |
  1661. | * | 3 |
  1662. | _ | 4 |
  1663. | / | 5 |
  1664. | $ | 6 |
  1665. | ! | 7 |"
  1666. (should
  1667. (org-export-table-has-special-column-p
  1668. (org-element-map
  1669. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1670. ;; 4. A first column with only empty cells isn't considered as
  1671. ;; special.
  1672. (org-test-with-temp-text "
  1673. | | 1 |
  1674. | | 2 |"
  1675. (should-not
  1676. (org-export-table-has-special-column-p
  1677. (org-element-map
  1678. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1679. (ert-deftest test-org-export/table-row-is-special-p ()
  1680. "Test `org-export-table-row-is-special-p' specifications."
  1681. ;; 1. A row is special if it has a special marking character in the
  1682. ;; special column.
  1683. (org-test-with-parsed-data "| ! | 1 |"
  1684. (should
  1685. (org-export-table-row-is-special-p
  1686. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1687. ;; 2. A row is special when its first field is "/"
  1688. (org-test-with-parsed-data "
  1689. | / | 1 |
  1690. | a | b |"
  1691. (should
  1692. (org-export-table-row-is-special-p
  1693. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1694. ;; 3. A row only containing alignment cookies is also considered as
  1695. ;; special.
  1696. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1697. (should
  1698. (org-export-table-row-is-special-p
  1699. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1700. ;; 4. Everything else isn't considered as special.
  1701. (org-test-with-parsed-data "| \alpha | | c |"
  1702. (should-not
  1703. (org-export-table-row-is-special-p
  1704. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1705. ;; 5. Table's rules are never considered as special rows.
  1706. (org-test-with-parsed-data "|---+---|"
  1707. (should-not
  1708. (org-export-table-row-is-special-p
  1709. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1710. (ert-deftest test-org-export/has-header-p ()
  1711. "Test `org-export-table-has-header-p' specifications."
  1712. ;; 1. With an header.
  1713. (org-test-with-parsed-data "
  1714. | a | b |
  1715. |---+---|
  1716. | c | d |"
  1717. (should
  1718. (org-export-table-has-header-p
  1719. (org-element-map tree 'table 'identity info 'first-match)
  1720. info)))
  1721. ;; 2. Without an header.
  1722. (org-test-with-parsed-data "
  1723. | a | b |
  1724. | c | d |"
  1725. (should-not
  1726. (org-export-table-has-header-p
  1727. (org-element-map tree 'table 'identity info 'first-match)
  1728. info)))
  1729. ;; 3. Don't get fooled with starting and ending rules.
  1730. (org-test-with-parsed-data "
  1731. |---+---|
  1732. | a | b |
  1733. | c | d |
  1734. |---+---|"
  1735. (should-not
  1736. (org-export-table-has-header-p
  1737. (org-element-map tree 'table 'identity info 'first-match)
  1738. info))))
  1739. (ert-deftest test-org-export/table-row-group ()
  1740. "Test `org-export-table-row-group' specifications."
  1741. ;; 1. A rule creates a new group.
  1742. (org-test-with-parsed-data "
  1743. | a | b |
  1744. |---+---|
  1745. | 1 | 2 |"
  1746. (should
  1747. (equal
  1748. '(1 nil 2)
  1749. (mapcar (lambda (row) (org-export-table-row-group row info))
  1750. (org-element-map tree 'table-row 'identity)))))
  1751. ;; 2. Special rows are ignored in count.
  1752. (org-test-with-parsed-data "
  1753. | / | < | > |
  1754. |---|---+---|
  1755. | | 1 | 2 |"
  1756. (should
  1757. (equal
  1758. '(nil nil 1)
  1759. (mapcar (lambda (row) (org-export-table-row-group row info))
  1760. (org-element-map tree 'table-row 'identity)))))
  1761. ;; 3. Double rules also are ignored in count.
  1762. (org-test-with-parsed-data "
  1763. | a | b |
  1764. |---+---|
  1765. |---+---|
  1766. | 1 | 2 |"
  1767. (should
  1768. (equal
  1769. '(1 nil nil 2)
  1770. (mapcar (lambda (row) (org-export-table-row-group row info))
  1771. (org-element-map tree 'table-row 'identity))))))
  1772. (ert-deftest test-org-export/table-cell-width ()
  1773. "Test `org-export-table-cell-width' specifications."
  1774. ;; 1. Width is primarily determined by width cookies. If no cookie
  1775. ;; is found, cell's width is nil.
  1776. (org-test-with-parsed-data "
  1777. | / | <l> | <6> | <l7> |
  1778. | | a | b | c |"
  1779. (should
  1780. (equal
  1781. '(nil 6 7)
  1782. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1783. (org-element-map tree 'table-cell 'identity info)))))
  1784. ;; 2. The last width cookie has precedence.
  1785. (org-test-with-parsed-data "
  1786. | <6> |
  1787. | <7> |
  1788. | a |"
  1789. (should
  1790. (equal
  1791. '(7)
  1792. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1793. (org-element-map tree 'table-cell 'identity info)))))
  1794. ;; 3. Valid width cookies must have a specific row.
  1795. (org-test-with-parsed-data "| <6> | cell |"
  1796. (should
  1797. (equal
  1798. '(nil nil)
  1799. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1800. (org-element-map tree 'table-cell 'identity))))))
  1801. (ert-deftest test-org-export/table-cell-alignment ()
  1802. "Test `org-export-table-cell-alignment' specifications."
  1803. (let ((org-table-number-fraction 0.5)
  1804. (org-table-number-regexp "^[0-9]+$"))
  1805. ;; 1. Alignment is primarily determined by alignment cookies.
  1806. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1807. (let* ((tree (org-element-parse-buffer))
  1808. (info `(:parse-tree ,tree)))
  1809. (should
  1810. (equal
  1811. '(left center right)
  1812. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1813. (org-element-map tree 'table-cell 'identity))))))
  1814. ;; 2. The last alignment cookie has precedence.
  1815. (org-test-with-parsed-data "
  1816. | <l8> |
  1817. | cell |
  1818. | <r9> |"
  1819. (should
  1820. (equal
  1821. '(right right right)
  1822. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1823. (org-element-map tree 'table-cell 'identity)))))
  1824. ;; 3. If there's no cookie, cell's contents determine alignment.
  1825. ;; A column mostly made of cells containing numbers will align
  1826. ;; its cells to the right.
  1827. (org-test-with-parsed-data "
  1828. | 123 |
  1829. | some text |
  1830. | 12345 |"
  1831. (should
  1832. (equal
  1833. '(right right right)
  1834. (mapcar (lambda (cell)
  1835. (org-export-table-cell-alignment cell info))
  1836. (org-element-map tree 'table-cell 'identity)))))
  1837. ;; 4. Otherwise, they will be aligned to the left.
  1838. (org-test-with-parsed-data "
  1839. | text |
  1840. | some text |
  1841. | \alpha |"
  1842. (should
  1843. (equal
  1844. '(left left left)
  1845. (mapcar (lambda (cell)
  1846. (org-export-table-cell-alignment cell info))
  1847. (org-element-map tree 'table-cell 'identity)))))))
  1848. (ert-deftest test-org-export/table-cell-borders ()
  1849. "Test `org-export-table-cell-borders' specifications."
  1850. ;; 1. Recognize various column groups indicators.
  1851. (org-test-with-parsed-data "| / | < | > | <> |"
  1852. (should
  1853. (equal
  1854. '((right bottom top) (left bottom top) (right bottom top)
  1855. (right left bottom top))
  1856. (mapcar (lambda (cell)
  1857. (org-export-table-cell-borders cell info))
  1858. (org-element-map tree 'table-cell 'identity)))))
  1859. ;; 2. Accept shortcuts to define column groups.
  1860. (org-test-with-parsed-data "| / | < | < |"
  1861. (should
  1862. (equal
  1863. '((right bottom top) (right left bottom top) (left bottom top))
  1864. (mapcar (lambda (cell)
  1865. (org-export-table-cell-borders cell info))
  1866. (org-element-map tree 'table-cell 'identity)))))
  1867. ;; 3. A valid column groups row must start with a "/".
  1868. (org-test-with-parsed-data "
  1869. | | < |
  1870. | a | b |"
  1871. (should
  1872. (equal '((top) (top) (bottom) (bottom))
  1873. (mapcar (lambda (cell)
  1874. (org-export-table-cell-borders cell info))
  1875. (org-element-map tree 'table-cell 'identity)))))
  1876. ;; 4. Take table rules into consideration.
  1877. (org-test-with-parsed-data "
  1878. | 1 |
  1879. |---|
  1880. | 2 |"
  1881. (should
  1882. (equal '((below top) (bottom above))
  1883. (mapcar (lambda (cell)
  1884. (org-export-table-cell-borders cell info))
  1885. (org-element-map tree 'table-cell 'identity)))))
  1886. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1887. ;; (resp. `bottom' and `below') borders. Any special row is
  1888. ;; ignored.
  1889. (org-test-with-parsed-data "
  1890. |---+----|
  1891. | / | |
  1892. | | 1 |
  1893. |---+----|"
  1894. (should
  1895. (equal '((bottom below top above))
  1896. (last
  1897. (mapcar (lambda (cell)
  1898. (org-export-table-cell-borders cell info))
  1899. (org-element-map tree 'table-cell 'identity)))))))
  1900. (ert-deftest test-org-export/table-dimensions ()
  1901. "Test `org-export-table-dimensions' specifications."
  1902. ;; 1. Standard test.
  1903. (org-test-with-parsed-data "
  1904. | 1 | 2 | 3 |
  1905. | 4 | 5 | 6 |"
  1906. (should
  1907. (equal '(2 . 3)
  1908. (org-export-table-dimensions
  1909. (org-element-map tree 'table 'identity info 'first-match) info))))
  1910. ;; 2. Ignore horizontal rules and special columns.
  1911. (org-test-with-parsed-data "
  1912. | / | < | > |
  1913. | 1 | 2 | 3 |
  1914. |---+---+---|
  1915. | 4 | 5 | 6 |"
  1916. (should
  1917. (equal '(2 . 3)
  1918. (org-export-table-dimensions
  1919. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1920. (ert-deftest test-org-export/table-cell-address ()
  1921. "Test `org-export-table-cell-address' specifications."
  1922. ;; 1. Standard test: index is 0-based.
  1923. (org-test-with-parsed-data "| a | b |"
  1924. (should
  1925. (equal '((0 . 0) (0 . 1))
  1926. (org-element-map tree 'table-cell
  1927. (lambda (cell) (org-export-table-cell-address cell info))
  1928. info))))
  1929. ;; 2. Special column isn't counted, nor are special rows.
  1930. (org-test-with-parsed-data "
  1931. | / | <> |
  1932. | | c |"
  1933. (should
  1934. (equal '(0 . 0)
  1935. (org-export-table-cell-address
  1936. (car (last (org-element-map tree 'table-cell 'identity info)))
  1937. info))))
  1938. ;; 3. Tables rules do not count either.
  1939. (org-test-with-parsed-data "
  1940. | a |
  1941. |---|
  1942. | b |
  1943. |---|
  1944. | c |"
  1945. (should
  1946. (equal '(2 . 0)
  1947. (org-export-table-cell-address
  1948. (car (last (org-element-map tree 'table-cell 'identity info)))
  1949. info))))
  1950. ;; 4. Return nil for special cells.
  1951. (org-test-with-parsed-data "| / | a |"
  1952. (should-not
  1953. (org-export-table-cell-address
  1954. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1955. info))))
  1956. (ert-deftest test-org-export/get-table-cell-at ()
  1957. "Test `org-export-get-table-cell-at' specifications."
  1958. ;; 1. Address ignores special columns, special rows and rules.
  1959. (org-test-with-parsed-data "
  1960. | / | <> |
  1961. | | a |
  1962. |---+----|
  1963. | | b |"
  1964. (should
  1965. (equal '("b")
  1966. (org-element-contents
  1967. (org-export-get-table-cell-at
  1968. '(1 . 0)
  1969. (org-element-map tree 'table 'identity info 'first-match)
  1970. info)))))
  1971. ;; 2. Return value for a non-existent address is nil.
  1972. (org-test-with-parsed-data "| a |"
  1973. (should-not
  1974. (org-export-get-table-cell-at
  1975. '(2 . 2)
  1976. (org-element-map tree 'table 'identity info 'first-match)
  1977. info)))
  1978. (org-test-with-parsed-data "| / |"
  1979. (should-not
  1980. (org-export-get-table-cell-at
  1981. '(0 . 0)
  1982. (org-element-map tree 'table 'identity info 'first-match)
  1983. info))))
  1984. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1985. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1986. ;; 1. A cell at a beginning of a row always starts a column group.
  1987. (org-test-with-parsed-data "| a |"
  1988. (should
  1989. (org-export-table-cell-starts-colgroup-p
  1990. (org-element-map tree 'table-cell 'identity info 'first-match)
  1991. info)))
  1992. ;; 2. Special column should be ignored when determining the
  1993. ;; beginning of the row.
  1994. (org-test-with-parsed-data "
  1995. | / | |
  1996. | | a |"
  1997. (should
  1998. (org-export-table-cell-starts-colgroup-p
  1999. (org-element-map tree 'table-cell 'identity info 'first-match)
  2000. info)))
  2001. ;; 2. Explicit column groups.
  2002. (org-test-with-parsed-data "
  2003. | / | | < |
  2004. | a | b | c |"
  2005. (should
  2006. (equal
  2007. '(yes no yes)
  2008. (org-element-map tree 'table-cell
  2009. (lambda (cell)
  2010. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  2011. info)))))
  2012. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  2013. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  2014. ;; 1. A cell at the end of a row always ends a column group.
  2015. (org-test-with-parsed-data "| a |"
  2016. (should
  2017. (org-export-table-cell-ends-colgroup-p
  2018. (org-element-map tree 'table-cell 'identity info 'first-match)
  2019. info)))
  2020. ;; 2. Special column should be ignored when determining the
  2021. ;; beginning of the row.
  2022. (org-test-with-parsed-data "
  2023. | / | |
  2024. | | a |"
  2025. (should
  2026. (org-export-table-cell-ends-colgroup-p
  2027. (org-element-map tree 'table-cell 'identity info 'first-match)
  2028. info)))
  2029. ;; 3. Explicit column groups.
  2030. (org-test-with-parsed-data "
  2031. | / | < | |
  2032. | a | b | c |"
  2033. (should
  2034. (equal
  2035. '(yes no yes)
  2036. (org-element-map tree 'table-cell
  2037. (lambda (cell)
  2038. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  2039. info)))))
  2040. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  2041. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  2042. ;; 1. A row at the beginning of a table always starts a row group.
  2043. ;; So does a row following a table rule.
  2044. (org-test-with-parsed-data "
  2045. | a |
  2046. |---|
  2047. | b |"
  2048. (should
  2049. (equal
  2050. '(yes no yes)
  2051. (org-element-map tree 'table-row
  2052. (lambda (row)
  2053. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2054. info))))
  2055. ;; 2. Special rows should be ignored when determining the beginning
  2056. ;; of the row.
  2057. (org-test-with-parsed-data "
  2058. | / | < |
  2059. | | a |
  2060. |---+---|
  2061. | / | < |
  2062. | | b |"
  2063. (should
  2064. (equal
  2065. '(yes no yes)
  2066. (org-element-map tree 'table-row
  2067. (lambda (row)
  2068. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2069. info)))))
  2070. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  2071. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  2072. ;; 1. A row at the end of a table always ends a row group. So does
  2073. ;; a row preceding a table rule.
  2074. (org-test-with-parsed-data "
  2075. | a |
  2076. |---|
  2077. | b |"
  2078. (should
  2079. (equal
  2080. '(yes no yes)
  2081. (org-element-map tree 'table-row
  2082. (lambda (row)
  2083. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2084. info))))
  2085. ;; 2. Special rows should be ignored when determining the beginning
  2086. ;; of the row.
  2087. (org-test-with-parsed-data "
  2088. | | a |
  2089. | / | < |
  2090. |---+---|
  2091. | | b |
  2092. | / | < |"
  2093. (should
  2094. (equal
  2095. '(yes no yes)
  2096. (org-element-map tree 'table-row
  2097. (lambda (row)
  2098. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2099. info)))))
  2100. (ert-deftest test-org-export/table-row-starts-header-p ()
  2101. "Test `org-export-table-row-starts-header-p' specifications."
  2102. ;; 1. Only the row starting the first row group starts the table
  2103. ;; header.
  2104. (org-test-with-parsed-data "
  2105. | a |
  2106. | b |
  2107. |---|
  2108. | c |"
  2109. (should
  2110. (equal
  2111. '(yes no no no)
  2112. (org-element-map tree 'table-row
  2113. (lambda (row)
  2114. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  2115. info))))
  2116. ;; 2. A row cannot start an header if there's no header in the
  2117. ;; table.
  2118. (org-test-with-parsed-data "
  2119. | a |
  2120. |---|"
  2121. (should-not
  2122. (org-export-table-row-starts-header-p
  2123. (org-element-map tree 'table-row 'identity info 'first-match)
  2124. info))))
  2125. (ert-deftest test-org-export/table-row-ends-header-p ()
  2126. "Test `org-export-table-row-ends-header-p' specifications."
  2127. ;; 1. Only the row starting the first row group starts the table
  2128. ;; header.
  2129. (org-test-with-parsed-data "
  2130. | a |
  2131. | b |
  2132. |---|
  2133. | c |"
  2134. (should
  2135. (equal
  2136. '(no yes no no)
  2137. (org-element-map tree 'table-row
  2138. (lambda (row)
  2139. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  2140. info))))
  2141. ;; 2. A row cannot start an header if there's no header in the
  2142. ;; table.
  2143. (org-test-with-parsed-data "
  2144. | a |
  2145. |---|"
  2146. (should-not
  2147. (org-export-table-row-ends-header-p
  2148. (org-element-map tree 'table-row 'identity info 'first-match)
  2149. info))))
  2150. ;;; Templates
  2151. (ert-deftest test-org-export/inner-template ()
  2152. "Test `inner-template' translator specifications."
  2153. (should
  2154. (equal "Success!"
  2155. (let (org-export-registered-backends)
  2156. (org-export-define-backend 'test
  2157. '((inner-template . (lambda (contents info) "Success!"))
  2158. (headline . (lambda (h c i) "Headline"))))
  2159. (org-test-with-temp-text "* Headline"
  2160. (org-export-as 'test)))))
  2161. ;; Inner template is applied even in a "body-only" export.
  2162. (should
  2163. (equal "Success!"
  2164. (let (org-export-registered-backends)
  2165. (org-export-define-backend 'test
  2166. '((inner-template . (lambda (contents info) "Success!"))
  2167. (headline . (lambda (h c i) "Headline"))))
  2168. (org-test-with-temp-text "* Headline"
  2169. (org-export-as 'test nil nil 'body-only))))))
  2170. (ert-deftest test-org-export/template ()
  2171. "Test `template' translator specifications."
  2172. (should
  2173. (equal "Success!"
  2174. (let (org-export-registered-backends)
  2175. (org-export-define-backend 'test
  2176. '((template . (lambda (contents info) "Success!"))
  2177. (headline . (lambda (h c i) "Headline"))))
  2178. (org-test-with-temp-text "* Headline"
  2179. (org-export-as 'test)))))
  2180. ;; Template is not applied in a "body-only" export.
  2181. (should-not
  2182. (equal "Success!"
  2183. (let (org-export-registered-backends)
  2184. (org-export-define-backend 'test
  2185. '((template . (lambda (contents info) "Success!"))
  2186. (headline . (lambda (h c i) "Headline"))))
  2187. (org-test-with-temp-text "* Headline"
  2188. (org-export-as 'test nil nil 'body-only))))))
  2189. ;;; Topology
  2190. (ert-deftest test-org-export/get-next-element ()
  2191. "Test `org-export-get-next-element' specifications."
  2192. ;; Standard test.
  2193. (should
  2194. (equal "b"
  2195. (org-test-with-parsed-data "* Headline\n*a* b"
  2196. (org-export-get-next-element
  2197. (org-element-map tree 'bold 'identity info t) info))))
  2198. ;; Return nil when no previous element.
  2199. (should-not
  2200. (org-test-with-parsed-data "* Headline\na *b*"
  2201. (org-export-get-next-element
  2202. (org-element-map tree 'bold 'identity info t) info)))
  2203. ;; Non-exportable elements are ignored.
  2204. (should-not
  2205. (let ((org-export-with-timestamps nil))
  2206. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2207. (org-export-get-next-element
  2208. (org-element-map tree 'entity 'identity info t) info))))
  2209. ;; Find next element in secondary strings.
  2210. (should
  2211. (eq 'verbatim
  2212. (org-test-with-parsed-data "* a =verb="
  2213. (org-element-type
  2214. (org-export-get-next-element
  2215. (org-element-map tree 'plain-text 'identity info t) info)))))
  2216. ;; Find next element in document keywords.
  2217. (should
  2218. (eq 'verbatim
  2219. (org-test-with-parsed-data "#+TITLE: a =verb="
  2220. (org-element-type
  2221. (org-export-get-next-element
  2222. (org-element-map
  2223. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2224. ;; Find next element in parsed affiliated keywords.
  2225. (should
  2226. (eq 'verbatim
  2227. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2228. (org-element-type
  2229. (org-export-get-next-element
  2230. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2231. ;; With optional argument N, return a list containing all the
  2232. ;; following elements.
  2233. (should
  2234. (equal
  2235. '(bold code underline)
  2236. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2237. (mapcar 'car
  2238. (org-export-get-next-element
  2239. (org-element-map tree 'italic 'identity info t) info t)))))
  2240. ;; When N is a positive integer, return a list containing up to
  2241. ;; N following elements.
  2242. (should
  2243. (equal
  2244. '(bold code)
  2245. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2246. (mapcar 'car
  2247. (org-export-get-next-element
  2248. (org-element-map tree 'italic 'identity info t) info 2))))))
  2249. (ert-deftest test-org-export/get-previous-element ()
  2250. "Test `org-export-get-previous-element' specifications."
  2251. ;; Standard test.
  2252. (should
  2253. (equal "a "
  2254. (org-test-with-parsed-data "* Headline\na *b*"
  2255. (org-export-get-previous-element
  2256. (org-element-map tree 'bold 'identity info t) info))))
  2257. ;; Return nil when no previous element.
  2258. (should-not
  2259. (org-test-with-parsed-data "* Headline\n*a* b"
  2260. (org-export-get-previous-element
  2261. (org-element-map tree 'bold 'identity info t) info)))
  2262. ;; Non-exportable elements are ignored.
  2263. (should-not
  2264. (let ((org-export-with-timestamps nil))
  2265. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  2266. (org-export-get-previous-element
  2267. (org-element-map tree 'entity 'identity info t) info))))
  2268. ;; Find previous element in secondary strings.
  2269. (should
  2270. (eq 'verbatim
  2271. (org-test-with-parsed-data "* =verb= a"
  2272. (org-element-type
  2273. (org-export-get-previous-element
  2274. (org-element-map tree 'plain-text 'identity info t) info)))))
  2275. ;; Find previous element in document keywords.
  2276. (should
  2277. (eq 'verbatim
  2278. (org-test-with-parsed-data "#+TITLE: =verb= a"
  2279. (org-element-type
  2280. (org-export-get-previous-element
  2281. (org-element-map
  2282. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2283. ;; Find previous element in parsed affiliated keywords.
  2284. (should
  2285. (eq 'verbatim
  2286. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  2287. (org-element-type
  2288. (org-export-get-previous-element
  2289. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2290. ;; With optional argument N, return a list containing up to
  2291. ;; N previous elements.
  2292. (should
  2293. (equal '(underline italic bold)
  2294. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2295. (mapcar 'car
  2296. (org-export-get-previous-element
  2297. (org-element-map tree 'code 'identity info t) info t)))))
  2298. ;; When N is a positive integer, return a list containing up to
  2299. ;; N previous elements.
  2300. (should
  2301. (equal '(italic bold)
  2302. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2303. (mapcar 'car
  2304. (org-export-get-previous-element
  2305. (org-element-map tree 'code 'identity info t) info 2))))))
  2306. (provide 'test-ox)
  2307. ;;; test-org-export.el end here