test-ox.el 125 KB

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