test-ox.el 120 KB

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