test-ox.el 119 KB

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