test-ox.el 104 KB

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