test-ox.el 93 KB

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