test-ox.el 93 KB

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