test-ox.el 130 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949
  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. (should
  758. (equal "a_entity\n"
  759. (org-test-with-temp-text "a_\\alpha"
  760. (org-export-as
  761. (org-export-create-backend
  762. :transcoders '((entity . (lambda (e c i) "entity"))
  763. (subscript . (lambda (s c i) "dummy"))
  764. (paragraph . (lambda (p c i) c))
  765. (section . (lambda (s c i) c))))
  766. nil nil nil '(:with-sub-superscript nil)))))
  767. ;; Also handle uninterpreted objects in title.
  768. (should
  769. (equal "a_b"
  770. (org-test-with-temp-text "#+TITLE: a_b"
  771. (org-export-as
  772. (org-export-create-backend
  773. :transcoders
  774. '((subscript . (lambda (s c i) "dummy"))
  775. (template . (lambda (c i) (org-export-data
  776. (plist-get i :title) i)))
  777. (section . (lambda (s c i) c))))
  778. nil nil nil '(:with-sub-superscript nil)))))
  779. ;; Handle uninterpreted objects in captions.
  780. (should
  781. (equal "adummy\n"
  782. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  783. (org-export-as
  784. (org-export-create-backend
  785. :transcoders
  786. '((subscript . (lambda (s c i) "dummy"))
  787. (paragraph . (lambda (p c i)
  788. (org-export-data (org-export-get-caption p) i)))
  789. (section . (lambda (s c i) c))))
  790. nil nil nil '(:with-sub-superscript t)))))
  791. (should
  792. (equal "a_b\n"
  793. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  794. (org-export-as
  795. (org-export-create-backend
  796. :transcoders
  797. '((subscript . (lambda (s c i) "dummy"))
  798. (paragraph . (lambda (p c i)
  799. (org-export-data (org-export-get-caption p) i)))
  800. (section . (lambda (s c i) c))))
  801. nil nil nil '(:with-sub-superscript nil)))))
  802. ;; Special case: multiples uninterpreted objects in a row.
  803. (should
  804. (equal "a_b_c_d\n"
  805. (org-test-with-temp-text "a_b_c_d"
  806. (org-export-as
  807. (org-export-create-backend
  808. :transcoders '((subscript . (lambda (s c i) "dummy"))
  809. (paragraph . (lambda (p c i) c))
  810. (section . (lambda (s c i) c))))
  811. nil nil nil '(:with-sub-superscript {}))))))
  812. (ert-deftest test-org-export/export-scope ()
  813. "Test all export scopes."
  814. (org-test-with-temp-text "
  815. * Head1
  816. ** Head2
  817. text
  818. *** Head3"
  819. ;; Subtree.
  820. (forward-line 3)
  821. (should (equal (org-export-as (org-test-default-backend) 'subtree)
  822. "text\n*** Head3\n"))
  823. ;; Visible.
  824. (goto-char (point-min))
  825. (forward-line)
  826. (org-cycle)
  827. (should (equal (org-export-as (org-test-default-backend) nil 'visible)
  828. "* Head1\n"))
  829. ;; Region.
  830. (goto-char (point-min))
  831. (forward-line 3)
  832. (transient-mark-mode 1)
  833. (push-mark (point) t t)
  834. (goto-char (point-at-eol))
  835. (should (equal (org-export-as (org-test-default-backend)) "text\n")))
  836. ;; Subtree with a code block calling another block outside.
  837. (let ((org-export-babel-evaluate t))
  838. (should
  839. (equal ": 3\n"
  840. (org-test-with-temp-text "
  841. * Head1
  842. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  843. <<test>>
  844. #+END_SRC
  845. * Head2
  846. #+NAME: test
  847. #+BEGIN_SRC emacs-lisp
  848. \(+ 1 2)
  849. #+END_SRC"
  850. (forward-line 1)
  851. (org-export-as (org-test-default-backend) 'subtree)))))
  852. ;; Body only.
  853. (let ((backend (org-test-default-backend)))
  854. (setf (org-export-backend-transcoders backend)
  855. (cons '(template . (lambda (body i)
  856. (format "BEGIN\n%sEND" body)))
  857. (org-export-backend-transcoders backend)))
  858. (org-test-with-temp-text "Text"
  859. (should (equal (org-export-as backend nil nil 'body-only)
  860. "Text\n"))
  861. (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
  862. (ert-deftest test-org-export/output-file-name ()
  863. "Test `org-export-output-file-name' specifications."
  864. ;; Export from a file: name is built from original file name.
  865. (should
  866. (org-test-with-temp-text-in-file "Test"
  867. (equal (concat (file-name-as-directory ".")
  868. (file-name-nondirectory
  869. (file-name-sans-extension (buffer-file-name))))
  870. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  871. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  872. (should
  873. (org-test-with-temp-text-in-file
  874. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  875. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  876. ;; From a buffer not associated to a file, too.
  877. (should
  878. (org-test-with-temp-text
  879. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  880. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  881. ;; When provided name is absolute, preserve it.
  882. (should
  883. (org-test-with-temp-text
  884. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  885. (expand-file-name "test"))
  886. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  887. ;; When PUB-DIR argument is provided, use it.
  888. (should
  889. (org-test-with-temp-text-in-file "Test"
  890. (equal (file-name-directory
  891. (org-export-output-file-name ".ext" nil "dir/"))
  892. "dir/")))
  893. ;; When returned name would overwrite original file, add EXTENSION
  894. ;; another time.
  895. (should
  896. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  897. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  898. (ert-deftest test-org-export/expand-include ()
  899. "Test file inclusion in an Org buffer."
  900. ;; Error when file isn't specified.
  901. (should-error
  902. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  903. (org-export-expand-include-keyword)))
  904. ;; Full insertion with recursive inclusion.
  905. (should
  906. (equal
  907. (with-temp-buffer
  908. (insert-file
  909. (expand-file-name "examples/include.org" org-test-dir))
  910. (replace-regexp-in-string
  911. (regexp-quote "#+INCLUDE: \"include2.org\"")
  912. "Success!" (buffer-string)))
  913. (org-test-with-temp-text
  914. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  915. (org-export-expand-include-keyword)
  916. (buffer-string))))
  917. ;; Localized insertion.
  918. (org-test-with-temp-text
  919. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  920. org-test-dir)
  921. (org-export-expand-include-keyword)
  922. (should (equal (buffer-string)
  923. "Small Org file with an include keyword.\n")))
  924. ;; Insertion with constraints on headlines level.
  925. (should
  926. (equal
  927. "* Top heading\n** Heading\nbody\n"
  928. (org-test-with-temp-text
  929. (format
  930. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
  931. org-test-dir)
  932. (org-export-expand-include-keyword)
  933. (buffer-string))))
  934. (should
  935. (equal
  936. "* Top heading\n* Heading\nbody\n"
  937. (org-test-with-temp-text
  938. (format
  939. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
  940. org-test-dir)
  941. (org-export-expand-include-keyword)
  942. (buffer-string))))
  943. ;; Inclusion within an example block.
  944. (should
  945. (equal
  946. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  947. (org-test-with-temp-text
  948. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  949. org-test-dir)
  950. (org-export-expand-include-keyword)
  951. (buffer-string))))
  952. ;; Inclusion within a src-block.
  953. (should
  954. (equal
  955. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  956. (org-test-with-temp-text
  957. (format
  958. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  959. org-test-dir)
  960. (org-export-expand-include-keyword)
  961. (buffer-string))))
  962. ;; Inclusion within an html export-block.
  963. (should
  964. (equal
  965. "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
  966. (org-test-with-temp-text
  967. (format
  968. "#+INCLUDE: \"%s/examples/include.html\" HTML"
  969. org-test-dir)
  970. (org-export-expand-include-keyword)
  971. (buffer-string))))
  972. ;; Inclusion within an center paragraph
  973. (should
  974. (equal
  975. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  976. (org-test-with-temp-text
  977. (format
  978. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  979. org-test-dir)
  980. (org-export-expand-include-keyword)
  981. (buffer-string))))
  982. ;; Footnotes labels are local to each included file.
  983. (should
  984. (= 6
  985. (length
  986. (delete-dups
  987. (let ((contents "
  988. Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:anonymous footnote].
  989. \[fn:1] Footnote 1
  990. \[fn:test] Footnote \"test\""))
  991. (org-test-with-temp-text-in-file contents
  992. (let ((file1 (buffer-file-name)))
  993. (org-test-with-temp-text-in-file contents
  994. (let ((file2 (buffer-file-name)))
  995. (org-test-with-temp-text
  996. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  997. file1 file2)
  998. (org-export-expand-include-keyword)
  999. (org-element-map (org-element-parse-buffer)
  1000. 'footnote-reference
  1001. (lambda (r) (org-element-property :label r)))))))))))))
  1002. ;; Footnotes labels are not local to each include keyword.
  1003. (should
  1004. (= 4
  1005. (length
  1006. (delete-dups
  1007. (let ((contents "
  1008. Footnotes[fn:1], [fn:test], [2] and [fn:inline:anonymous footnote].
  1009. \[fn:1] Footnote 1
  1010. \[2] Footnote 2
  1011. \[fn:test] Footnote \"test\""))
  1012. (org-test-with-temp-text-in-file contents
  1013. (let ((file (buffer-file-name)))
  1014. (org-test-with-temp-text
  1015. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  1016. (org-export-expand-include-keyword)
  1017. (org-element-map (org-element-parse-buffer)
  1018. 'footnote-reference
  1019. (lambda (ref) (org-element-property :label ref)))))))))))
  1020. ;; Footnotes are supported by :lines-like elements and unnecessary
  1021. ;; footnotes are dropped.
  1022. (should
  1023. (= 4
  1024. (length
  1025. (delete-dups
  1026. (let ((contents "
  1027. * foo
  1028. Footnotes[fn:1]
  1029. * bar
  1030. Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote]
  1031. \[fn:1] Footnote 1
  1032. \[fn:2] Footnote 1
  1033. * Footnotes
  1034. \[fn:test] Footnote \"test\"
  1035. \[3] Footnote 3
  1036. "))
  1037. (org-test-with-temp-text-in-file contents
  1038. (let ((file (buffer-file-name)))
  1039. (org-test-with-temp-text
  1040. (format "#+INCLUDE: \"%s::*bar\"
  1041. " file)
  1042. (org-export-expand-include-keyword)
  1043. (org-element-map (org-element-parse-buffer)
  1044. 'footnote-definition
  1045. (lambda (ref) (org-element-property :label ref)))))))))))
  1046. ;; If only-contents is non-nil only include contents of element.
  1047. (should
  1048. (equal
  1049. "body\n"
  1050. (org-test-with-temp-text
  1051. (concat
  1052. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
  1053. ":only-contents t")
  1054. (org-export-expand-include-keyword)
  1055. (buffer-string))))
  1056. ;; Headings can be included via CUSTOM_ID.
  1057. (should
  1058. (org-test-with-temp-text
  1059. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1060. (org-export-expand-include-keyword)
  1061. (goto-char (point-min))
  1062. (looking-at "* Another heading")))
  1063. ;; Named objects can be included.
  1064. (should
  1065. (equal
  1066. "| 1 |\n"
  1067. (org-test-with-temp-text
  1068. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
  1069. (org-export-expand-include-keyword)
  1070. (buffer-string))))
  1071. ;; Including non-existing elements should result in an error.
  1072. (should-error
  1073. (org-test-with-temp-text
  1074. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
  1075. (org-export-expand-include-keyword)))
  1076. ;; Lines work relatively to an included element.
  1077. (should
  1078. (equal
  1079. "2\n3\n"
  1080. (org-test-with-temp-text
  1081. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
  1082. (org-export-expand-include-keyword)
  1083. (buffer-string))))
  1084. ;; Properties should be dropped from headlines.
  1085. (should
  1086. (equal
  1087. (org-test-with-temp-text
  1088. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
  1089. (org-export-expand-include-keyword)
  1090. (buffer-string))
  1091. (org-test-with-temp-text
  1092. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  1093. (org-export-expand-include-keyword)
  1094. (buffer-string))))
  1095. ;; Properties should be dropped, drawers should not be.
  1096. (should
  1097. (equal
  1098. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  1099. (org-test-with-temp-text
  1100. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
  1101. (org-export-expand-include-keyword)
  1102. (buffer-string))))
  1103. ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  1104. (should
  1105. (org-every (lambda (level) (zerop (1- level)))
  1106. (org-test-with-temp-text
  1107. (concat
  1108. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir)
  1109. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir))
  1110. (org-export-expand-include-keyword)
  1111. (org-element-map (org-element-parse-buffer) 'headline
  1112. (lambda (head) (org-element-property :level head))))))
  1113. ;; INCLUDE does not insert induced :minlevel for src-blocks.
  1114. (should-not
  1115. (equal
  1116. (org-test-with-temp-text
  1117. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir)
  1118. (org-export-expand-include-keyword)
  1119. (buffer-string))
  1120. (org-test-with-temp-text
  1121. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir)
  1122. (org-export-expand-include-keyword)
  1123. (buffer-string))))
  1124. ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  1125. (should
  1126. (org-test-with-temp-text-in-file
  1127. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1128. (narrow-to-region (point) (point-max))
  1129. (org-export-expand-include-keyword)
  1130. (eq 1 (org-current-level))))
  1131. ;; If :minlevel is present do not alter it.
  1132. (should
  1133. (org-test-with-temp-text
  1134. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3" org-test-dir)
  1135. (narrow-to-region (point) (point-max))
  1136. (org-export-expand-include-keyword)
  1137. (eq 3 (org-current-level)))))
  1138. (ert-deftest test-org-export/expand-macro ()
  1139. "Test macro expansion in an Org buffer."
  1140. ;; Standard macro expansion.
  1141. (should
  1142. (equal "#+MACRO: macro1 value\nvalue\n"
  1143. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1144. (org-export-as (org-test-default-backend)))))
  1145. ;; Allow macro in parsed keywords and associated properties.
  1146. ;; Standard macro expansion.
  1147. (should
  1148. (string-match
  1149. "#\\+K: value"
  1150. (let ((backend (org-export-create-backend
  1151. :parent 'org
  1152. :options '((:k "K" nil nil parse)))))
  1153. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1154. (org-export-as backend)))))
  1155. (should
  1156. (string-match
  1157. ":EXPORT_K: v"
  1158. (let ((backend (org-export-create-backend
  1159. :parent 'org
  1160. :options '((:k "K" nil nil parse)))))
  1161. (org-test-with-temp-text
  1162. "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1163. (org-export-as backend nil nil nil '(:with-properties t))))))
  1164. ;; Expand specific macros.
  1165. (should
  1166. (equal "me 2012-03-29 me@here Title\n"
  1167. (org-test-with-temp-text
  1168. "
  1169. #+TITLE: Title
  1170. #+DATE: 2012-03-29
  1171. #+AUTHOR: me
  1172. #+EMAIL: me@here
  1173. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1174. (let ((output (org-export-as (org-test-default-backend))))
  1175. (substring output (string-match ".*\n\\'" output))))))
  1176. ;; Expand specific macros when property contained a regular macro
  1177. ;; already.
  1178. (should
  1179. (equal "value\n"
  1180. (org-test-with-temp-text "
  1181. #+MACRO: macro1 value
  1182. #+TITLE: {{{macro1}}}
  1183. {{{title}}}"
  1184. (let ((output (org-export-as (org-test-default-backend))))
  1185. (substring output (string-match ".*\n\\'" output))))))
  1186. ;; Expand macros with templates in included files.
  1187. (should
  1188. (equal "success\n"
  1189. (org-test-with-temp-text
  1190. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1191. {{{included-macro}}}" org-test-dir)
  1192. (let ((output (org-export-as (org-test-default-backend))))
  1193. (substring output (string-match ".*\n\\'" output))))))
  1194. ;; Date macro takes a optional formatting argument
  1195. (should
  1196. (equal "09-02-15\n"
  1197. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1198. (org-export-as (org-test-default-backend)))))
  1199. ;; Only single timestamps are formatted
  1200. (should
  1201. (equal "<2015-02x-09>\n"
  1202. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1203. (org-export-as (org-test-default-backend)))))
  1204. ;; Throw an error when a macro definition is missing.
  1205. (should-error
  1206. (org-test-with-temp-text "{{{missing}}}"
  1207. (org-export-as (org-test-default-backend))))
  1208. ;; Macros defined in commented subtrees are ignored.
  1209. (should-error
  1210. (org-test-with-temp-text
  1211. "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
  1212. (org-export-as (org-test-default-backend))))
  1213. (should-error
  1214. (org-test-with-temp-text
  1215. "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
  1216. (org-export-as (org-test-default-backend)))))
  1217. (ert-deftest test-org-export/before-processing-hook ()
  1218. "Test `org-export-before-processing-hook'."
  1219. (should
  1220. (equal
  1221. "#+MACRO: mac val\nTest\n"
  1222. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1223. (let ((org-export-before-processing-hook
  1224. '((lambda (backend)
  1225. (while (re-search-forward "{{{" nil t)
  1226. (let ((object (org-element-context)))
  1227. (when (eq (org-element-type object) 'macro)
  1228. (delete-region
  1229. (org-element-property :begin object)
  1230. (org-element-property :end object)))))))))
  1231. (org-export-as (org-test-default-backend)))))))
  1232. (ert-deftest test-org-export/before-parsing-hook ()
  1233. "Test `org-export-before-parsing-hook'."
  1234. (should
  1235. (equal "Body 1\nBody 2\n"
  1236. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1237. (let ((org-export-before-parsing-hook
  1238. '((lambda (backend)
  1239. (goto-char (point-min))
  1240. (while (re-search-forward org-outline-regexp-bol nil t)
  1241. (delete-region
  1242. (point-at-bol) (progn (forward-line) (point))))))))
  1243. (org-export-as (org-test-default-backend)))))))
  1244. ;;; Affiliated Keywords
  1245. (ert-deftest test-org-export/read-attribute ()
  1246. "Test `org-export-read-attribute' specifications."
  1247. ;; Standard test.
  1248. (should
  1249. (equal
  1250. (org-export-read-attribute
  1251. :attr_html
  1252. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1253. (org-element-at-point)))
  1254. '(:a "1" :b "2")))
  1255. ;; Return nil on empty attribute.
  1256. (should-not
  1257. (org-export-read-attribute
  1258. :attr_html
  1259. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1260. ;; Return nil on "nil" string.
  1261. (should
  1262. (equal '(:a nil :b nil)
  1263. (org-export-read-attribute
  1264. :attr_html
  1265. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1266. (org-element-at-point)))))
  1267. ;; Return nil on empty string.
  1268. (should
  1269. (equal '(:a nil :b nil)
  1270. (org-export-read-attribute
  1271. :attr_html
  1272. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1273. (org-element-at-point)))))
  1274. ;; Return empty string when value is "".
  1275. (should
  1276. (equal '(:a "")
  1277. (org-export-read-attribute
  1278. :attr_html
  1279. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1280. (org-element-at-point)))))
  1281. ;; Return \"\" when value is """".
  1282. (should
  1283. (equal '(:a "\"\"")
  1284. (org-export-read-attribute
  1285. :attr_html
  1286. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1287. (org-element-at-point)))))
  1288. ;; Ignore text before first property.
  1289. (should-not
  1290. (member "ignore"
  1291. (org-export-read-attribute
  1292. :attr_html
  1293. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1294. (org-element-at-point))))))
  1295. (ert-deftest test-org-export/get-caption ()
  1296. "Test `org-export-get-caption' specifications."
  1297. ;; Without optional argument, return long caption
  1298. (should
  1299. (equal
  1300. '("l")
  1301. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1302. (org-export-get-caption (org-element-at-point)))))
  1303. ;; With optional argument, return short caption.
  1304. (should
  1305. (equal
  1306. '("s")
  1307. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1308. (org-export-get-caption (org-element-at-point) t))))
  1309. ;; Multiple lines are separated by white spaces.
  1310. (should
  1311. (equal
  1312. '("a" " " "b")
  1313. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1314. (org-export-get-caption (org-element-at-point))))))
  1315. ;;; Back-End Tools
  1316. (ert-deftest test-org-export/define-backend ()
  1317. "Test back-end definition and accessors."
  1318. ;; Translate table.
  1319. (should
  1320. (equal '((headline . my-headline-test))
  1321. (let (org-export-registered-backends)
  1322. (org-export-define-backend 'test '((headline . my-headline-test)))
  1323. (org-export-get-all-transcoders 'test))))
  1324. ;; Filters.
  1325. (should
  1326. (equal '((:filter-headline . my-filter))
  1327. (let (org-export-registered-backends)
  1328. (org-export-define-backend 'test
  1329. '((headline . my-headline-test))
  1330. :filters-alist '((:filter-headline . my-filter)))
  1331. (org-export-backend-filters (org-export-get-backend 'test)))))
  1332. ;; Options.
  1333. (should
  1334. (equal '((:prop value))
  1335. (let (org-export-registered-backends)
  1336. (org-export-define-backend 'test
  1337. '((headline . my-headline-test))
  1338. :options-alist '((:prop value)))
  1339. (org-export-backend-options (org-export-get-backend 'test)))))
  1340. ;; Menu.
  1341. (should
  1342. (equal '(?k "Test Export" test)
  1343. (let (org-export-registered-backends)
  1344. (org-export-define-backend 'test
  1345. '((headline . my-headline-test))
  1346. :menu-entry '(?k "Test Export" test))
  1347. (org-export-backend-menu (org-export-get-backend 'test)))))
  1348. ;; Export Blocks.
  1349. (should
  1350. (equal '(("TEST" . org-element-export-block-parser))
  1351. (let (org-export-registered-backends org-element-block-name-alist)
  1352. (org-export-define-backend 'test
  1353. '((headline . my-headline-test))
  1354. :export-block '("test"))
  1355. org-element-block-name-alist))))
  1356. (ert-deftest test-org-export/define-derived-backend ()
  1357. "Test `org-export-define-derived-backend' specifications."
  1358. ;; Error when parent back-end is not defined.
  1359. (should-error
  1360. (let (org-export-registered-backends)
  1361. (org-export-define-derived-backend 'test 'parent)))
  1362. ;; Append translation table to parent's.
  1363. (should
  1364. (equal '((:headline . test) (:headline . parent))
  1365. (let (org-export-registered-backends)
  1366. (org-export-define-backend 'parent '((:headline . parent)))
  1367. (org-export-define-derived-backend 'test 'parent
  1368. :translate-alist '((:headline . test)))
  1369. (org-export-get-all-transcoders 'test))))
  1370. ;; Options defined in the new back have priority over those defined
  1371. ;; in parent.
  1372. (should
  1373. (eq 'test
  1374. (let (org-export-registered-backends)
  1375. (org-export-define-backend 'parent
  1376. '((:headline . parent))
  1377. :options-alist '((:a nil nil 'parent)))
  1378. (org-export-define-derived-backend 'test 'parent
  1379. :options-alist '((:a nil nil 'test)))
  1380. (plist-get (org-export--get-global-options
  1381. (org-export-get-backend 'test))
  1382. :a)))))
  1383. (ert-deftest test-org-export/derived-backend-p ()
  1384. "Test `org-export-derived-backend-p' specifications."
  1385. ;; Non-nil with direct match.
  1386. (should
  1387. (let (org-export-registered-backends)
  1388. (org-export-define-backend 'test '((headline . test)))
  1389. (org-export-derived-backend-p 'test 'test)))
  1390. (should
  1391. (let (org-export-registered-backends)
  1392. (org-export-define-backend 'test '((headline . test)))
  1393. (org-export-define-derived-backend 'test2 'test)
  1394. (org-export-derived-backend-p 'test2 'test2)))
  1395. ;; Non-nil with a direct parent.
  1396. (should
  1397. (let (org-export-registered-backends)
  1398. (org-export-define-backend 'test '((headline . test)))
  1399. (org-export-define-derived-backend 'test2 'test)
  1400. (org-export-derived-backend-p 'test2 'test)))
  1401. ;; Non-nil with an indirect parent.
  1402. (should
  1403. (let (org-export-registered-backends)
  1404. (org-export-define-backend 'test '((headline . test)))
  1405. (org-export-define-derived-backend 'test2 'test)
  1406. (org-export-define-derived-backend 'test3 'test2)
  1407. (org-export-derived-backend-p 'test3 'test)))
  1408. ;; Nil otherwise.
  1409. (should-not
  1410. (let (org-export-registered-backends)
  1411. (org-export-define-backend 'test '((headline . test)))
  1412. (org-export-define-backend 'test2 '((headline . test2)))
  1413. (org-export-derived-backend-p 'test2 'test)))
  1414. (should-not
  1415. (let (org-export-registered-backends)
  1416. (org-export-define-backend 'test '((headline . test)))
  1417. (org-export-define-backend 'test2 '((headline . test2)))
  1418. (org-export-define-derived-backend 'test3 'test2)
  1419. (org-export-derived-backend-p 'test3 'test))))
  1420. (ert-deftest test-org-export/get-all-transcoders ()
  1421. "Test `org-export-get-all-transcoders' specifications."
  1422. ;; Return nil when back-end cannot be found.
  1423. (should-not (org-export-get-all-transcoders nil))
  1424. ;; Same as `org-export-transcoders' if no parent.
  1425. (should
  1426. (equal '((headline . ignore))
  1427. (org-export-get-all-transcoders
  1428. (org-export-create-backend
  1429. :transcoders '((headline . ignore))))))
  1430. ;; But inherit from all ancestors whenever possible.
  1431. (should
  1432. (equal '((section . ignore) (headline . ignore))
  1433. (let (org-export-registered-backends)
  1434. (org-export-define-backend 'b1 '((headline . ignore)))
  1435. (org-export-get-all-transcoders
  1436. (org-export-create-backend
  1437. :parent 'b1 :transcoders '((section . ignore)))))))
  1438. (should
  1439. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1440. (let (org-export-registered-backends)
  1441. (org-export-define-backend 'b1 '((headline . ignore)))
  1442. (org-export-define-derived-backend 'b2 'b1
  1443. :translate-alist '((section . ignore)))
  1444. (org-export-get-all-transcoders
  1445. (org-export-create-backend
  1446. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1447. ;; Back-end transcoders overrule inherited ones.
  1448. (should
  1449. (eq 'b
  1450. (let (org-export-registered-backends)
  1451. (org-export-define-backend 'b1 '((headline . a)))
  1452. (cdr (assq 'headline
  1453. (org-export-get-all-transcoders
  1454. (org-export-create-backend
  1455. :parent 'b1 :transcoders '((headline . b))))))))))
  1456. (ert-deftest test-org-export/get-all-options ()
  1457. "Test `org-export-get-all-options' specifications."
  1458. ;; Return nil when back-end cannot be found.
  1459. (should-not (org-export-get-all-options nil))
  1460. ;; Same as `org-export-options' if no parent.
  1461. (should
  1462. (equal '((headline . ignore))
  1463. (org-export-get-all-options
  1464. (org-export-create-backend
  1465. :options '((headline . ignore))))))
  1466. ;; But inherit from all ancestors whenever possible.
  1467. (should
  1468. (equal '((:key2 value2) (:key1 value1))
  1469. (let (org-export-registered-backends)
  1470. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1471. (org-export-get-all-options
  1472. (org-export-create-backend
  1473. :parent 'b1 :options '((:key2 value2)))))))
  1474. (should
  1475. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1476. (let (org-export-registered-backends)
  1477. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1478. (org-export-define-derived-backend 'b2 'b1
  1479. :options-alist '((:key2 value2)))
  1480. (org-export-get-all-options
  1481. (org-export-create-backend
  1482. :parent 'b2 :options '((:key3 value3)))))))
  1483. ;; Back-end options overrule inherited ones.
  1484. (should
  1485. (eq 'b
  1486. (let (org-export-registered-backends)
  1487. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1488. (cdr (assq :key1
  1489. (org-export-get-all-options
  1490. (org-export-create-backend
  1491. :parent 'b1 :options '((:key1 . b))))))))))
  1492. (ert-deftest test-org-export/get-all-filters ()
  1493. "Test `org-export-get-all-filters' specifications."
  1494. ;; Return nil when back-end cannot be found.
  1495. (should-not (org-export-get-all-filters nil))
  1496. ;; Same as `org-export-filters' if no parent.
  1497. (should
  1498. (equal '((:filter-headline . ignore))
  1499. (org-export-get-all-filters
  1500. (org-export-create-backend
  1501. :filters '((:filter-headline . ignore))))))
  1502. ;; But inherit from all ancestors whenever possible.
  1503. (should
  1504. (equal '((:filter-section . ignore) (: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-get-all-filters
  1509. (org-export-create-backend
  1510. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1511. (should
  1512. (equal '((:filter-paragraph . ignore)
  1513. (:filter-section . ignore)
  1514. (:filter-headline . ignore))
  1515. (let (org-export-registered-backends)
  1516. (org-export-define-backend 'b1
  1517. nil :filters-alist '((:filter-headline . ignore)))
  1518. (org-export-define-derived-backend 'b2 'b1
  1519. :filters-alist '((:filter-section . ignore)))
  1520. (org-export-get-all-filters
  1521. (org-export-create-backend
  1522. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1523. ;; Back-end filters overrule inherited ones.
  1524. (should
  1525. (eq 'b
  1526. (let (org-export-registered-backends)
  1527. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1528. (cdr (assq :filter-headline
  1529. (org-export-get-all-filters
  1530. (org-export-create-backend
  1531. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1532. (ert-deftest test-org-export/with-backend ()
  1533. "Test `org-export-with-backend' definition."
  1534. ;; Error when calling an undefined back-end
  1535. (should-error (org-export-with-backend nil "Test"))
  1536. ;; Error when called back-end doesn't have an appropriate
  1537. ;; transcoder.
  1538. (should-error
  1539. (org-export-with-backend
  1540. (org-export-create-backend :transcoders '((headline . ignore)))
  1541. "Test"))
  1542. ;; Otherwise, export using correct transcoder
  1543. (should
  1544. (equal "Success"
  1545. (let (org-export-registered-backends)
  1546. (org-export-define-backend 'test
  1547. '((plain-text . (lambda (text contents info) "Failure"))))
  1548. (org-export-define-backend 'test2
  1549. '((plain-text . (lambda (text contents info) "Success"))))
  1550. (org-export-with-backend 'test2 "Test"))))
  1551. ;; Provide correct back-end if transcoder needs to use recursive
  1552. ;; calls anyway.
  1553. (should
  1554. (equal "Success\n"
  1555. (let ((test-back-end
  1556. (org-export-create-backend
  1557. :transcoders
  1558. '((headline . (lambda (headline contents info)
  1559. (org-export-data
  1560. (org-element-property :title headline)
  1561. info)))
  1562. (plain-text . (lambda (text info) "Success"))))))
  1563. (org-export-string-as
  1564. "* Test"
  1565. (org-export-create-backend
  1566. :transcoders
  1567. '((headline . (lambda (headline contents info)
  1568. (org-export-with-backend
  1569. test-back-end headline contents info))))))))))
  1570. (ert-deftest test-org-export/data-with-backend ()
  1571. "Test `org-export-data-with-backend' specifications."
  1572. ;; Error when calling an undefined back-end.
  1573. (should-error (org-export-data-with-backend nil "nil" nil))
  1574. ;; Otherwise, export data recursively, using correct back-end.
  1575. (should
  1576. (equal
  1577. "Success!"
  1578. (org-export-data-with-backend
  1579. '(bold nil "Test")
  1580. (org-export-create-backend
  1581. :transcoders
  1582. '((plain-text . (lambda (text info) "Success"))
  1583. (bold . (lambda (bold contents info) (concat contents "!")))))
  1584. '(:with-emphasize t)))))
  1585. ;;; Comments
  1586. (ert-deftest test-org-export/comments ()
  1587. "Test comments handling during export.
  1588. In particular, structure of the document mustn't be altered after
  1589. comments removal."
  1590. (should
  1591. (equal (org-test-with-temp-text "
  1592. Para1
  1593. # Comment
  1594. # Comment
  1595. Para2"
  1596. (org-export-as (org-test-default-backend)))
  1597. "Para1\n\nPara2\n"))
  1598. (should
  1599. (equal (org-test-with-temp-text "
  1600. Para1
  1601. # Comment
  1602. Para2"
  1603. (org-export-as (org-test-default-backend)))
  1604. "Para1\n\nPara2\n"))
  1605. (should
  1606. (equal (org-test-with-temp-text "
  1607. \[fn:1] Para1
  1608. # Inside definition
  1609. # Outside definition
  1610. Para2"
  1611. (org-export-as (org-test-default-backend)))
  1612. "[fn:1] Para1\n\n\nPara2\n"))
  1613. (should
  1614. (equal (org-test-with-temp-text "
  1615. \[fn:1] Para1
  1616. # Inside definition
  1617. # Inside definition
  1618. Para2"
  1619. (org-export-as (org-test-default-backend)))
  1620. "[fn:1] Para1\n\nPara2\n"))
  1621. (should
  1622. (equal (org-test-with-temp-text "
  1623. \[fn:1] Para1
  1624. # Inside definition
  1625. Para2"
  1626. (org-export-as (org-test-default-backend)))
  1627. "[fn:1] Para1\n\nPara2\n"))
  1628. (should
  1629. (equal (org-test-with-temp-text "
  1630. \[fn:1] Para1
  1631. # Inside definition
  1632. Para2"
  1633. (org-export-as (org-test-default-backend)))
  1634. "[fn:1] Para1\n\nPara2\n")))
  1635. ;;; Export Snippets
  1636. (ert-deftest test-org-export/export-snippet ()
  1637. "Test export snippets transcoding."
  1638. ;; Standard test.
  1639. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1640. (let ((backend (org-test-default-backend)))
  1641. (setf (org-export-backend-name backend) 'test)
  1642. (setf (org-export-backend-transcoders backend)
  1643. (cons (cons 'export-snippet
  1644. (lambda (snippet contents info)
  1645. (when (eq (org-export-snippet-backend snippet) 'test)
  1646. (org-element-property :value snippet))))
  1647. (org-export-backend-transcoders backend)))
  1648. (let ((org-export-snippet-translation-alist nil))
  1649. (should (equal (org-export-as backend) "A\n")))
  1650. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1651. (should (equal (org-export-as backend) "AB\n")))))
  1652. ;; Ignored export snippets do not remove any blank.
  1653. (should
  1654. (equal "begin end\n"
  1655. (org-test-with-parsed-data "begin @@test:A@@ end"
  1656. (org-export-data-with-backend
  1657. tree
  1658. (org-export-create-backend
  1659. :transcoders
  1660. '((paragraph . (lambda (paragraph contents info) contents))
  1661. (section . (lambda (section contents info) contents))))
  1662. info)))))
  1663. ;;; Footnotes
  1664. (ert-deftest test-org-export/footnote-first-reference-p ()
  1665. "Test `org-export-footnote-first-reference-p' specifications."
  1666. (should
  1667. (equal
  1668. '(t nil)
  1669. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1670. (let (result)
  1671. (org-export-as
  1672. (org-export-create-backend
  1673. :transcoders
  1674. `(,(cons 'footnote-reference
  1675. (lambda (f c i)
  1676. (push (org-export-footnote-first-reference-p f info)
  1677. result)
  1678. ""))
  1679. (section . (lambda (s c i) c))
  1680. (paragraph . (lambda (p c i) c))))
  1681. nil nil nil '(:with-footnotes t))
  1682. (nreverse result)))))
  1683. ;; Limit check to DATA, when non-nil.
  1684. (should
  1685. (equal
  1686. '(nil t)
  1687. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1688. (let (result)
  1689. (org-element-map tree 'footnote-reference
  1690. (lambda (ref)
  1691. (push
  1692. (org-export-footnote-first-reference-p
  1693. ref info (org-element-map tree 'headline #'identity info t))
  1694. result))
  1695. info)
  1696. (nreverse result)))))
  1697. (should
  1698. (equal
  1699. '(t nil)
  1700. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1701. (let (result)
  1702. (org-element-map tree 'footnote-reference
  1703. (lambda (ref)
  1704. (push (org-export-footnote-first-reference-p ref info) result))
  1705. info)
  1706. (nreverse result)))))
  1707. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1708. ;; in the main body of the document. Otherwise, enter footnote
  1709. ;; definitions when they are encountered.
  1710. (should
  1711. (equal
  1712. '(t nil)
  1713. (org-test-with-temp-text
  1714. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1715. (let (result)
  1716. (org-export-as
  1717. (org-export-create-backend
  1718. :transcoders
  1719. `(,(cons 'footnote-reference
  1720. (lambda (f c i)
  1721. (when (org-element-lineage f '(drawer))
  1722. (push (org-export-footnote-first-reference-p f info nil)
  1723. result))
  1724. ""))
  1725. (drawer . (lambda (d c i) c))
  1726. (footnote-definition . (lambda (d c i) c))
  1727. (section . (lambda (s c i) c))
  1728. (paragraph . (lambda (p c i) c))))
  1729. nil nil nil '(:with-footnotes t))
  1730. (nreverse result)))))
  1731. (should
  1732. (equal
  1733. '(t t)
  1734. (org-test-with-temp-text
  1735. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1736. (let (result)
  1737. (org-export-as
  1738. (org-export-create-backend
  1739. :transcoders
  1740. `(,(cons 'footnote-reference
  1741. (lambda (f c i)
  1742. (when (org-element-lineage f '(drawer))
  1743. (push (org-export-footnote-first-reference-p f info nil t)
  1744. result))
  1745. ""))
  1746. (drawer . (lambda (d c i) c))
  1747. (footnote-definition . (lambda (d c i) c))
  1748. (section . (lambda (s c i) c))
  1749. (paragraph . (lambda (p c i) c))))
  1750. nil nil nil '(:with-footnotes t))
  1751. (nreverse result))))))
  1752. (ert-deftest test-org-export/get-footnote-number ()
  1753. "Test `org-export-get-footnote-number' specifications."
  1754. (should
  1755. (equal '(1 2 1)
  1756. (org-test-with-parsed-data
  1757. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  1758. (org-element-map tree 'footnote-reference
  1759. (lambda (ref) (org-export-get-footnote-number ref info))
  1760. info))))
  1761. ;; Anonymous footnotes all get a new number.
  1762. (should
  1763. (equal '(1 2)
  1764. (org-test-with-parsed-data
  1765. "Text[fn::anon1][fn::anon2]"
  1766. (org-element-map tree 'footnote-reference
  1767. (lambda (ref) (org-export-get-footnote-number ref info))
  1768. info))))
  1769. ;; Test nested footnotes order.
  1770. (should
  1771. (equal
  1772. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (3 . "fn:3") (4))
  1773. (org-test-with-parsed-data
  1774. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1775. (org-element-map tree 'footnote-reference
  1776. (lambda (ref)
  1777. (cons (org-export-get-footnote-number ref info)
  1778. (org-element-property :label ref)))
  1779. info))))
  1780. ;; Limit number to provided DATA, when non-nil.
  1781. (should
  1782. (equal
  1783. '(1)
  1784. (org-test-with-parsed-data
  1785. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1786. (org-element-map tree 'footnote-reference
  1787. (lambda (ref)
  1788. (org-export-get-footnote-number
  1789. ref info (org-element-map tree 'headline #'identity info t)))
  1790. info))))
  1791. (should
  1792. (equal
  1793. '(1 2)
  1794. (org-test-with-parsed-data
  1795. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  1796. (org-element-map tree 'footnote-reference
  1797. (lambda (ref) (org-export-get-footnote-number ref info))
  1798. info))))
  1799. ;; With a non-nil BODY-FIRST optional argument, first check body,
  1800. ;; then footnote definitions.
  1801. (should
  1802. (equal
  1803. '(("fn:1" . 1) ("fn:2" . 2) ("fn:3" . 3) ("fn:3" . 3))
  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 t)))
  1810. info))))
  1811. (should
  1812. (equal
  1813. '(("fn:1" . 1) ("fn:2" . 3) ("fn:3" . 2) ("fn:3" . 2))
  1814. (org-test-with-parsed-data
  1815. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1816. (org-element-map tree 'footnote-reference
  1817. (lambda (ref)
  1818. (cons (org-element-property :label ref)
  1819. (org-export-get-footnote-number ref info nil)))
  1820. info)))))
  1821. (ert-deftest test-org-export/get-footnote-definition ()
  1822. "Test `org-export-get-footnote-definition' specifications."
  1823. ;; Standard test.
  1824. (should
  1825. (equal "A\n"
  1826. (org-element-interpret-data
  1827. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
  1828. (org-export-get-footnote-definition
  1829. (org-element-map tree 'footnote-reference #'identity nil t)
  1830. info)))))
  1831. ;; Raise an error if no definition is found.
  1832. (should-error
  1833. (org-test-with-parsed-data "Text[fn:1]"
  1834. (org-export-get-footnote-definition
  1835. (org-element-map tree 'footnote-reference #'identity nil t)
  1836. info)))
  1837. ;; Find inline definitions.
  1838. (should
  1839. (equal "A"
  1840. (org-element-interpret-data
  1841. (org-test-with-parsed-data "Text[fn:1:A]"
  1842. (org-export-get-footnote-definition
  1843. (org-element-map tree 'footnote-reference #'identity nil t)
  1844. info)))))
  1845. ;; Find anonymous definitions.
  1846. (should
  1847. (equal "A"
  1848. (org-element-interpret-data
  1849. (org-test-with-parsed-data "Text[fn::A]"
  1850. (org-export-get-footnote-definition
  1851. (org-element-map tree 'footnote-reference #'identity nil t)
  1852. info)))))
  1853. ;; Find empty definitions.
  1854. (should
  1855. (equal ""
  1856. (org-element-interpret-data
  1857. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
  1858. (org-export-get-footnote-definition
  1859. (org-element-map tree 'footnote-reference #'identity nil t)
  1860. info)))))
  1861. (should
  1862. (equal ""
  1863. (org-element-interpret-data
  1864. (org-test-with-parsed-data "Text[fn:1:]"
  1865. (org-export-get-footnote-definition
  1866. (org-element-map tree 'footnote-reference #'identity nil t)
  1867. info)))))
  1868. (should
  1869. (equal ""
  1870. (org-element-interpret-data
  1871. (org-test-with-parsed-data "Text[fn::]"
  1872. (org-export-get-footnote-definition
  1873. (org-element-map tree 'footnote-reference #'identity nil t)
  1874. info))))))
  1875. (ert-deftest test-org-export/collect-footnote-definitions ()
  1876. "Test `org-export-collect-footnote-definitions' specifications."
  1877. (should
  1878. (= 4
  1879. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1880. \[fn:2] B [fn:3] [fn::D].
  1881. \[fn:3] C."
  1882. (length (org-export-collect-footnote-definitions info)))))
  1883. ;; Limit number to provided DATA, when non-nil.
  1884. (should
  1885. (equal
  1886. '((1 "fn:2"))
  1887. (org-test-with-parsed-data
  1888. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1889. (mapcar #'butlast
  1890. (org-export-collect-footnote-definitions
  1891. info (org-element-map tree 'headline #'identity info t))))))
  1892. (should
  1893. (equal
  1894. '((1 "fn:1") (2 "fn:2"))
  1895. (org-test-with-parsed-data
  1896. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1897. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  1898. ;; With optional argument BODY-FIRST, first check body, then
  1899. ;; footnote definitions.
  1900. (should
  1901. (equal '("fn:1" "fn:3" "fn:2" nil)
  1902. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1903. \[fn:2] B [fn:3] [fn::D].
  1904. \[fn:3] C."
  1905. (mapcar (lambda (e) (nth 1 e))
  1906. (org-export-collect-footnote-definitions info nil t)))))
  1907. (should-not
  1908. (equal '("fn:1" "fn:3" "fn:2" nil)
  1909. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1910. \[fn:2] B [fn:3] [fn::D].
  1911. \[fn:3] C."
  1912. (mapcar (lambda (e) (nth 1 e))
  1913. (org-export-collect-footnote-definitions info))))))
  1914. (ert-deftest test-org-export/footnotes ()
  1915. "Miscellaneous tests on footnotes."
  1916. (let ((org-footnote-section nil)
  1917. (org-export-with-footnotes t))
  1918. ;; Read every type of footnote.
  1919. (should
  1920. (equal
  1921. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  1922. (org-test-with-parsed-data
  1923. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  1924. (org-element-map tree 'footnote-reference
  1925. (lambda (ref)
  1926. (let ((def (org-export-get-footnote-definition ref info)))
  1927. (cons (org-export-get-footnote-number ref info)
  1928. (if (eq (org-element-property :type ref) 'inline) (car def)
  1929. (car (org-element-contents
  1930. (car (org-element-contents def))))))))
  1931. info))))
  1932. ;; Export nested footnote in invisible definitions.
  1933. (should
  1934. (= 2
  1935. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  1936. (narrow-to-region (point) (line-end-position))
  1937. (catch 'exit
  1938. (org-export-as
  1939. (org-export-create-backend
  1940. :transcoders
  1941. '((section
  1942. .
  1943. (lambda (s c i)
  1944. (throw 'exit (length
  1945. (org-export-collect-footnote-definitions
  1946. i))))))))))))
  1947. ;; Export footnotes defined outside parsing scope.
  1948. (should
  1949. (string-match
  1950. "Out of scope"
  1951. (org-test-with-temp-text "[fn:1] Out of scope
  1952. * Title
  1953. <point>Paragraph[fn:1]"
  1954. (org-export-as (org-test-default-backend) 'subtree))))
  1955. (should
  1956. (string-match
  1957. "Out of scope"
  1958. (org-test-with-temp-text "[fn:1] Out of scope
  1959. * Title
  1960. <point>Paragraph[fn:1]"
  1961. (narrow-to-region (point) (point-max))
  1962. (org-export-as (org-test-default-backend)))))
  1963. ;; Export nested footnotes defined outside parsing scope.
  1964. (should
  1965. (string-match
  1966. "Very out of scope"
  1967. (org-test-with-temp-text "
  1968. \[fn:1] Out of scope[fn:2]
  1969. \[fn:2] Very out of scope
  1970. * Title
  1971. <point>Paragraph[fn:1]"
  1972. (org-export-as (org-test-default-backend) 'subtree))))
  1973. (should
  1974. (string-match
  1975. "Very out of scope"
  1976. (org-test-with-temp-text "
  1977. \[fn:1] Out of scope[fn:2]
  1978. \[fn:2] Very out of scope
  1979. * Title
  1980. <point>Paragraph[fn:1]"
  1981. (narrow-to-region (point) (point-max))
  1982. (org-export-as (org-test-default-backend)))))
  1983. (should
  1984. (string-match
  1985. "D2"
  1986. (org-test-with-temp-text "
  1987. \[fn:1] Out of scope[fn:2:D2]
  1988. * Title
  1989. <point>Paragraph[fn:1]"
  1990. (narrow-to-region (point) (point-max))
  1991. (org-export-as (org-test-default-backend)))))
  1992. ;; Export footnotes in pruned parts of tree.
  1993. (should
  1994. (string-match
  1995. "Definition"
  1996. (let ((org-export-exclude-tags '("noexport")))
  1997. (org-test-with-temp-text
  1998. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
  1999. (org-export-as (org-test-default-backend))))))
  2000. (should
  2001. (string-match
  2002. "Definition"
  2003. (let ((org-export-select-tags '("export")))
  2004. (org-test-with-temp-text
  2005. "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
  2006. (org-export-as (org-test-default-backend))))))
  2007. ;; Export nested footnotes in pruned parts of tree.
  2008. (should
  2009. (string-match
  2010. "D2"
  2011. (let ((org-export-exclude-tags '("noexport")))
  2012. (org-test-with-temp-text
  2013. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2014. (org-export-as (org-test-default-backend))))))
  2015. (should
  2016. (string-match
  2017. "D2"
  2018. (let ((org-export-select-tags '("export")))
  2019. (org-test-with-temp-text
  2020. "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2021. (org-export-as (org-test-default-backend))))))
  2022. ;; Handle uninterpreted data in pruned footnote definitions.
  2023. (should-not
  2024. (string-match
  2025. "|"
  2026. (let ((org-export-with-tables nil))
  2027. (org-test-with-temp-text
  2028. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
  2029. (org-export-as (org-test-default-backend))))))
  2030. ;; Footnotes without a definition should throw an error.
  2031. (should-error
  2032. (org-test-with-parsed-data "Text[fn:1]"
  2033. (org-export-get-footnote-definition
  2034. (org-element-map tree 'footnote-reference #'identity info t) info)))
  2035. ;; Footnote section should be ignored in TOC and in headlines
  2036. ;; numbering.
  2037. (should
  2038. (= 1 (let ((org-footnote-section "Footnotes"))
  2039. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  2040. (org-export-collect-headlines info))))))
  2041. (should
  2042. (equal '(2)
  2043. (let ((org-footnote-section "Footnotes"))
  2044. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  2045. (org-element-map tree 'headline
  2046. (lambda (hl)
  2047. (when (equal (org-element-property :raw-value hl) "H2")
  2048. (org-export-get-headline-number hl info)))
  2049. info t)))))))
  2050. ;;; Headlines and Inlinetasks
  2051. (ert-deftest test-org-export/get-relative-level ()
  2052. "Test `org-export-get-relative-level' specifications."
  2053. ;; Standard test.
  2054. (should
  2055. (equal '(1 2)
  2056. (let ((org-odd-levels-only nil))
  2057. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2058. (org-element-map tree 'headline
  2059. (lambda (h) (org-export-get-relative-level h info))
  2060. info)))))
  2061. ;; Missing levels
  2062. (should
  2063. (equal '(1 3)
  2064. (let ((org-odd-levels-only nil))
  2065. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  2066. (org-element-map tree 'headline
  2067. (lambda (h) (org-export-get-relative-level h info))
  2068. info))))))
  2069. (ert-deftest test-org-export/low-level-p ()
  2070. "Test `org-export-low-level-p' specifications."
  2071. (should
  2072. (equal
  2073. '(no yes)
  2074. (let ((org-odd-levels-only nil))
  2075. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2076. (org-element-map tree 'headline
  2077. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  2078. (plist-put info :headline-levels 1)))))))
  2079. (ert-deftest test-org-export/get-headline-number ()
  2080. "Test `org-export-get-headline-number' specifications."
  2081. ;; Standard test.
  2082. (should
  2083. (equal
  2084. '((1) (1 1))
  2085. (let ((org-odd-levels-only nil))
  2086. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2087. (org-element-map tree 'headline
  2088. (lambda (h) (org-export-get-headline-number h info))
  2089. info)))))
  2090. ;; Missing levels are replaced with 0.
  2091. (should
  2092. (equal
  2093. '((1) (1 0 1))
  2094. (let ((org-odd-levels-only nil))
  2095. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  2096. (org-element-map tree 'headline
  2097. (lambda (h) (org-export-get-headline-number h info))
  2098. info))))))
  2099. (ert-deftest test-org-export/numbered-headline-p ()
  2100. "Test `org-export-numbered-headline-p' specifications."
  2101. ;; If `:section-numbers' is nil, never number headlines.
  2102. (should-not
  2103. (org-test-with-parsed-data "* Headline"
  2104. (org-element-map tree 'headline
  2105. (lambda (h) (org-export-numbered-headline-p h info))
  2106. (plist-put info :section-numbers nil))))
  2107. ;; If `:section-numbers' is a number, only number headlines with
  2108. ;; a level greater that it.
  2109. (should
  2110. (equal
  2111. '(yes no)
  2112. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2113. (org-element-map tree 'headline
  2114. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  2115. (plist-put info :section-numbers 1)))))
  2116. ;; Otherwise, headlines are always numbered.
  2117. (should
  2118. (org-test-with-parsed-data "* Headline"
  2119. (org-element-map tree 'headline
  2120. (lambda (h) (org-export-numbered-headline-p h info))
  2121. (plist-put info :section-numbers t))))
  2122. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  2123. (should-not
  2124. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  2125. (org-export-numbered-headline-p
  2126. (org-element-map tree 'headline 'identity info t)
  2127. info)))
  2128. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  2129. (should-not
  2130. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  2131. (org-export-numbered-headline-p
  2132. (org-element-map tree 'headline #'identity info t)
  2133. info)))
  2134. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  2135. ;; ancestors disables numbering.
  2136. (should
  2137. (org-test-with-parsed-data
  2138. "* H
  2139. :PROPERTIES:
  2140. :UNNUMBERED: t
  2141. :END:
  2142. ** H2
  2143. :PROPERTIES:
  2144. :UNNUMBERED: nil
  2145. :END:
  2146. *** H3"
  2147. (org-every
  2148. (lambda (h) (not (org-export-numbered-headline-p h info)))
  2149. (org-element-map tree 'headline #'identity info)))))
  2150. (ert-deftest test-org-export/number-to-roman ()
  2151. "Test `org-export-number-to-roman' specifications."
  2152. ;; If number is negative, return it as a string.
  2153. (should (equal (org-export-number-to-roman -1) "-1"))
  2154. ;; Otherwise, return it as a roman number.
  2155. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  2156. (ert-deftest test-org-export/get-optional-title ()
  2157. "Test `org-export-get-alt-title' specifications."
  2158. ;; If ALT_TITLE property is defined, use it.
  2159. (should
  2160. (equal '("opt")
  2161. (org-test-with-parsed-data
  2162. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  2163. (org-export-get-alt-title
  2164. (org-element-map tree 'headline 'identity info t)
  2165. info))))
  2166. ;; Otherwise, fall-back to regular title.
  2167. (should
  2168. (equal '("Headline")
  2169. (org-test-with-parsed-data "* Headline"
  2170. (org-export-get-alt-title
  2171. (org-element-map tree 'headline 'identity info t)
  2172. info)))))
  2173. (ert-deftest test-org-export/get-tags ()
  2174. "Test `org-export-get-tags' specifications."
  2175. (let ((org-export-exclude-tags '("noexport"))
  2176. (org-export-select-tags '("export")))
  2177. ;; Standard test: tags which are not a select tag, an exclude tag,
  2178. ;; or specified as optional argument shouldn't be ignored.
  2179. (should
  2180. (org-test-with-parsed-data "* Headline :tag:"
  2181. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2182. info)))
  2183. ;; Exclude tags are removed.
  2184. (should-not
  2185. (org-test-with-parsed-data "* Headline :noexport:"
  2186. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2187. info)))
  2188. ;; Select tags are removed.
  2189. (should-not
  2190. (org-test-with-parsed-data "* Headline :export:"
  2191. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2192. info)))
  2193. (should
  2194. (equal
  2195. '("tag")
  2196. (org-test-with-parsed-data "* Headline :tag:export:"
  2197. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2198. info))))
  2199. ;; Tags provided in the optional argument are also ignored.
  2200. (should-not
  2201. (org-test-with-parsed-data "* Headline :ignore:"
  2202. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2203. info '("ignore"))))
  2204. ;; Allow tag inheritance.
  2205. (should
  2206. (equal
  2207. '(("tag") ("tag"))
  2208. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  2209. (org-element-map tree 'headline
  2210. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  2211. ;; Tag inheritance checks FILETAGS keywords.
  2212. (should
  2213. (equal
  2214. '(("a" "b" "tag"))
  2215. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  2216. (org-element-map tree 'headline
  2217. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  2218. (ert-deftest test-org-export/get-node-property ()
  2219. "Test`org-export-get-node-property' specifications."
  2220. ;; Standard test.
  2221. (should
  2222. (equal "value"
  2223. (org-test-with-parsed-data "* Headline
  2224. :PROPERTIES:
  2225. :prop: value
  2226. :END:"
  2227. (org-export-get-node-property
  2228. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2229. ;; Test inheritance.
  2230. (should
  2231. (equal "value"
  2232. (org-test-with-parsed-data "* Parent
  2233. :PROPERTIES:
  2234. :prop: value
  2235. :END:
  2236. ** Headline
  2237. Paragraph"
  2238. (org-export-get-node-property
  2239. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2240. ;; Cannot return a value before the first headline.
  2241. (should-not
  2242. (org-test-with-parsed-data "Paragraph
  2243. * Headline
  2244. :PROPERTIES:
  2245. :prop: value
  2246. :END:"
  2247. (org-export-get-node-property
  2248. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2249. (ert-deftest test-org-export/get-category ()
  2250. "Test `org-export-get-category' specifications."
  2251. ;; Standard test.
  2252. (should
  2253. (equal "value"
  2254. (org-test-with-parsed-data "* Headline
  2255. :PROPERTIES:
  2256. :CATEGORY: value
  2257. :END:"
  2258. (org-export-get-category
  2259. (org-element-map tree 'headline 'identity nil t) info))))
  2260. ;; Test inheritance from a parent headline.
  2261. (should
  2262. (equal '("value" "value")
  2263. (org-test-with-parsed-data "* Headline1
  2264. :PROPERTIES:
  2265. :CATEGORY: value
  2266. :END:
  2267. ** Headline2"
  2268. (org-element-map tree 'headline
  2269. (lambda (hl) (org-export-get-category hl info)) info))))
  2270. ;; Test inheritance from #+CATEGORY keyword
  2271. (should
  2272. (equal "value"
  2273. (org-test-with-parsed-data "#+CATEGORY: value
  2274. * Headline"
  2275. (org-export-get-category
  2276. (org-element-map tree 'headline 'identity nil t) info))))
  2277. ;; Test inheritance from file name.
  2278. (should
  2279. (equal "test"
  2280. (org-test-with-parsed-data "* Headline"
  2281. (let ((info (plist-put info :input-file "~/test.org")))
  2282. (org-export-get-category
  2283. (org-element-map tree 'headline 'identity nil t) info)))))
  2284. ;; Fall-back value.
  2285. (should
  2286. (equal "???"
  2287. (org-test-with-parsed-data "* Headline"
  2288. (org-export-get-category
  2289. (org-element-map tree 'headline 'identity nil t) info)))))
  2290. (ert-deftest test-org-export/first-sibling-p ()
  2291. "Test `org-export-first-sibling-p' specifications."
  2292. ;; Standard test.
  2293. (should
  2294. (equal
  2295. '(yes yes no)
  2296. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2297. (org-element-map tree 'headline
  2298. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2299. info))))
  2300. (should
  2301. (equal '(yes no)
  2302. (org-test-with-parsed-data "- item\n\n para"
  2303. (org-element-map tree 'paragraph
  2304. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2305. info))))
  2306. ;; Ignore sections for headlines.
  2307. (should
  2308. (equal '(yes yes)
  2309. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2310. (org-element-map tree 'headline
  2311. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2312. info))))
  2313. ;; Ignore headlines not exported.
  2314. (should
  2315. (equal
  2316. '(yes)
  2317. (let ((org-export-exclude-tags '("ignore")))
  2318. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2319. (org-element-map tree 'headline
  2320. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2321. info))))))
  2322. (ert-deftest test-org-export/last-sibling-p ()
  2323. "Test `org-export-last-sibling-p' specifications."
  2324. ;; Standard test.
  2325. (should
  2326. (equal
  2327. '(yes no yes)
  2328. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2329. (org-element-map tree 'headline
  2330. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2331. info))))
  2332. (should
  2333. (equal '(no yes)
  2334. (org-test-with-parsed-data "- item\n\n para"
  2335. (org-element-map tree 'paragraph
  2336. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2337. info))))
  2338. ;; Ignore headlines not exported.
  2339. (should
  2340. (equal
  2341. '(yes)
  2342. (let ((org-export-exclude-tags '("ignore")))
  2343. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2344. (org-element-map tree 'headline
  2345. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2346. info))))))
  2347. (ert-deftest test-org-export/handle-inlinetasks ()
  2348. "Test inlinetask export."
  2349. ;; Inlinetask with an exclude tag.
  2350. (when (featurep 'org-inlinetask)
  2351. (should
  2352. (equal
  2353. ""
  2354. (let ((org-inlinetask-min-level 3)
  2355. org-export-filter-body-functions
  2356. org-export-filter-final-output-functions)
  2357. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2358. (org-export-as (org-test-default-backend)
  2359. nil nil nil '(:exclude-tags ("noexp")))))))
  2360. ;; Inlinetask with an include tag.
  2361. (should
  2362. (equal
  2363. "* H2\n*** Inline :exp:\n"
  2364. (let ((org-inlinetask-min-level 3)
  2365. (org-tags-column 0))
  2366. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2367. (org-export-as (org-test-default-backend)
  2368. nil nil nil '(:select-tags ("exp")))))))
  2369. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2370. (should
  2371. (equal ""
  2372. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2373. (org-inlinetask-min-level 3)
  2374. org-export-filter-body-functions
  2375. org-export-filter-final-output-functions)
  2376. (org-test-with-temp-text "*** TODO Inline"
  2377. (org-export-as (org-test-default-backend)
  2378. nil nil nil '(:with-tasks nil))))))))
  2379. ;;; Keywords
  2380. (ert-deftest test-org-export/get-date ()
  2381. "Test `org-export-get-date' specifications."
  2382. ;; Return a properly formatted string when
  2383. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2384. ;; consists in a single timestamp.
  2385. (should
  2386. (equal "29 03 2012"
  2387. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2388. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2389. (org-export-get-date info)))))
  2390. ;; Return a secondary string otherwise.
  2391. (should-not
  2392. (stringp
  2393. (let ((org-export-date-timestamp-format nil))
  2394. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2395. (org-export-get-date info)))))
  2396. (should
  2397. (equal '("Date")
  2398. (org-test-with-parsed-data "#+DATE: Date"
  2399. (org-export-get-date info))))
  2400. ;; Optional argument has precedence over
  2401. ;; `org-export-date-timestamp-format'.
  2402. (should
  2403. (equal "29 03"
  2404. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2405. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2406. (org-export-get-date info "%d %m"))))))
  2407. ;;; Links
  2408. (ert-deftest test-org-export/custom-protocol-maybe ()
  2409. "Test `org-export-custom-protocol-maybe' specifications."
  2410. (should
  2411. (string-match
  2412. "success"
  2413. (let ((org-link-types (copy-sequence org-link-types)))
  2414. (org-add-link-type "foo" nil (lambda (p d f) "success"))
  2415. (org-export-string-as
  2416. "[[foo:path]]"
  2417. (org-export-create-backend
  2418. :name 'test
  2419. :transcoders
  2420. '((section . (lambda (s c i) c))
  2421. (paragraph . (lambda (p c i) c))
  2422. (link . (lambda (l c i)
  2423. (or (org-export-custom-protocol-maybe l c 'test)
  2424. "failure")))))))))
  2425. (should-not
  2426. (string-match
  2427. "success"
  2428. (let ((org-link-types (copy-sequence org-link-types)))
  2429. (org-add-link-type
  2430. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2431. (org-export-string-as
  2432. "[[foo:path]]"
  2433. (org-export-create-backend
  2434. :name 'no-test
  2435. :transcoders
  2436. '((section . (lambda (s c i) c))
  2437. (paragraph . (lambda (p c i) c))
  2438. (link . (lambda (l c i)
  2439. (or (org-export-custom-protocol-maybe l c 'no-test)
  2440. "failure")))))))))
  2441. ;; Ignore anonymous back-ends.
  2442. (should-not
  2443. (string-match
  2444. "success"
  2445. (let ((org-link-types (copy-sequence org-link-types)))
  2446. (org-add-link-type
  2447. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2448. (org-export-string-as
  2449. "[[foo:path]]"
  2450. (org-export-create-backend
  2451. :transcoders
  2452. '((section . (lambda (s c i) c))
  2453. (paragraph . (lambda (p c i) c))
  2454. (link . (lambda (l c i)
  2455. (or (org-export-custom-protocol-maybe l c nil)
  2456. "failure"))))))))))
  2457. (ert-deftest test-org-export/get-coderef-format ()
  2458. "Test `org-export-get-coderef-format' specifications."
  2459. ;; A link without description returns "%s"
  2460. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2461. "%s"))
  2462. ;; Return "%s" when path is matched within description.
  2463. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2464. "desc %s"))
  2465. ;; Otherwise return description.
  2466. (should (equal (org-export-get-coderef-format "path" "desc")
  2467. "desc")))
  2468. (ert-deftest test-org-export/inline-image-p ()
  2469. "Test `org-export-inline-image-p' specifications."
  2470. (should
  2471. (org-export-inline-image-p
  2472. (org-test-with-temp-text "[[#id]]"
  2473. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2474. '(("custom-id" . "id")))))
  2475. (ert-deftest test-org-export/fuzzy-link ()
  2476. "Test fuzzy links specifications."
  2477. ;; Link to an headline should return headline's number.
  2478. (org-test-with-parsed-data
  2479. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2480. (should
  2481. ;; Note: Headline's number is in fact a list of numbers.
  2482. (equal '(2)
  2483. (org-element-map tree 'link
  2484. (lambda (link)
  2485. (org-export-get-ordinal
  2486. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2487. ;; Link to a target in an item should return item's number.
  2488. (org-test-with-parsed-data
  2489. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2490. (should
  2491. ;; Note: Item's number is in fact a list of numbers.
  2492. (equal '(1 2)
  2493. (org-element-map tree 'link
  2494. (lambda (link)
  2495. (org-export-get-ordinal
  2496. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2497. ;; Link to a target in a footnote should return footnote's number.
  2498. (org-test-with-parsed-data "
  2499. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  2500. (should
  2501. (equal '(2 3)
  2502. (org-element-map tree 'link
  2503. (lambda (link)
  2504. (org-export-get-ordinal
  2505. (org-export-resolve-fuzzy-link link info) info)) info))))
  2506. ;; Link to a named element should return sequence number of that
  2507. ;; element.
  2508. (org-test-with-parsed-data
  2509. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2510. (should
  2511. (= 2
  2512. (org-element-map tree 'link
  2513. (lambda (link)
  2514. (org-export-get-ordinal
  2515. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2516. ;; Link to a target not within an item, a table, a footnote
  2517. ;; reference or definition should return section number.
  2518. (org-test-with-parsed-data
  2519. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2520. (should
  2521. (equal '(2)
  2522. (org-element-map tree 'link
  2523. (lambda (link)
  2524. (org-export-get-ordinal
  2525. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2526. ;; Space are not significant when matching a fuzzy link.
  2527. (should
  2528. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2529. (org-element-map tree 'link
  2530. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2531. info t)))
  2532. ;; Statistics cookies are ignored for headline match.
  2533. (should
  2534. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2535. (org-element-map tree 'link
  2536. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2537. info t)))
  2538. (should
  2539. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2540. (org-element-map tree 'link
  2541. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2542. info t))))
  2543. (ert-deftest test-org-export/resolve-coderef ()
  2544. "Test `org-export-resolve-coderef' specifications."
  2545. (let ((org-coderef-label-format "(ref:%s)"))
  2546. ;; A link to a "-n -k -r" block returns line number.
  2547. (should
  2548. (= 1
  2549. (org-test-with-parsed-data
  2550. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2551. (org-export-resolve-coderef "coderef" info))))
  2552. (should
  2553. (= 1
  2554. (org-test-with-parsed-data
  2555. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2556. (org-export-resolve-coderef "coderef" info))))
  2557. ;; A link to a "-n -r" block returns line number.
  2558. (should
  2559. (= 1
  2560. (org-test-with-parsed-data
  2561. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2562. (org-export-resolve-coderef "coderef" info))))
  2563. (should
  2564. (= 1
  2565. (org-test-with-parsed-data
  2566. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2567. (org-export-resolve-coderef "coderef" info))))
  2568. ;; A link to a "-n" block returns coderef.
  2569. (should
  2570. (equal "coderef"
  2571. (org-test-with-parsed-data
  2572. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2573. (org-export-resolve-coderef "coderef" info))))
  2574. (should
  2575. (equal "coderef"
  2576. (org-test-with-parsed-data
  2577. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2578. (org-export-resolve-coderef "coderef" info))))
  2579. ;; A link to a "-r" block returns line number.
  2580. (should
  2581. (= 1
  2582. (org-test-with-parsed-data
  2583. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2584. (org-export-resolve-coderef "coderef" info))))
  2585. (should
  2586. (= 1
  2587. (org-test-with-parsed-data
  2588. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2589. (org-export-resolve-coderef "coderef" info))))
  2590. ;; A link to a block without a switch returns coderef.
  2591. (should
  2592. (equal "coderef"
  2593. (org-test-with-parsed-data
  2594. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2595. (org-export-resolve-coderef "coderef" info))))
  2596. (org-test-with-parsed-data
  2597. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2598. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2599. ;; Correctly handle continued line numbers. A "+n" switch should
  2600. ;; resume numbering from previous block with numbered lines,
  2601. ;; ignoring blocks not numbering lines in the process. A "-n"
  2602. ;; switch resets count.
  2603. (should
  2604. (equal '(2 1)
  2605. (org-test-with-parsed-data "
  2606. #+BEGIN_EXAMPLE -n
  2607. Text.
  2608. #+END_EXAMPLE
  2609. #+BEGIN_SRC emacs-lisp
  2610. \(- 1 1)
  2611. #+END_SRC
  2612. #+BEGIN_SRC emacs-lisp +n -r
  2613. \(+ 1 1) (ref:addition)
  2614. #+END_SRC
  2615. #+BEGIN_EXAMPLE -n -r
  2616. Another text. (ref:text)
  2617. #+END_EXAMPLE"
  2618. (list (org-export-resolve-coderef "addition" info)
  2619. (org-export-resolve-coderef "text" info)))))
  2620. ;; Recognize coderef with user-specified syntax.
  2621. (should
  2622. (equal "text"
  2623. (org-test-with-parsed-data
  2624. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2625. (org-export-resolve-coderef "text" info))))
  2626. ;; Unresolved coderefs throw an error.
  2627. (should-error
  2628. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2629. (org-export-resolve-coderef "unknown" info)))))
  2630. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2631. "Test `org-export-resolve-fuzzy-link' specifications."
  2632. ;; Match target objects.
  2633. (should
  2634. (org-test-with-parsed-data "<<target>> [[target]]"
  2635. (org-export-resolve-fuzzy-link
  2636. (org-element-map tree 'link 'identity info t) info)))
  2637. ;; Match named elements.
  2638. (should
  2639. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2640. (org-export-resolve-fuzzy-link
  2641. (org-element-map tree 'link 'identity info t) info)))
  2642. ;; Match exact headline's name.
  2643. (should
  2644. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2645. (org-export-resolve-fuzzy-link
  2646. (org-element-map tree 'link 'identity info t) info)))
  2647. ;; Targets objects have priority over named elements and headline
  2648. ;; titles.
  2649. (should
  2650. (eq 'target
  2651. (org-test-with-parsed-data
  2652. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  2653. (org-element-type
  2654. (org-export-resolve-fuzzy-link
  2655. (org-element-map tree 'link 'identity info t) info)))))
  2656. ;; Named elements have priority over headline titles.
  2657. (should
  2658. (eq 'paragraph
  2659. (org-test-with-parsed-data
  2660. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2661. (org-element-type
  2662. (org-export-resolve-fuzzy-link
  2663. (org-element-map tree 'link 'identity info t) info)))))
  2664. ;; If link's path starts with a "*", only match headline titles,
  2665. ;; though.
  2666. (should
  2667. (eq 'headline
  2668. (org-test-with-parsed-data
  2669. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2670. (org-element-type
  2671. (org-export-resolve-fuzzy-link
  2672. (org-element-map tree 'link 'identity info t) info)))))
  2673. ;; Error if no match.
  2674. (should-error
  2675. (org-test-with-parsed-data "[[target]]"
  2676. (org-export-resolve-fuzzy-link
  2677. (org-element-map tree 'link 'identity info t) info)))
  2678. ;; Match fuzzy link even when before first headline.
  2679. (should
  2680. (eq 'headline
  2681. (org-test-with-parsed-data "[[hl]]\n* hl"
  2682. (org-element-type
  2683. (org-export-resolve-fuzzy-link
  2684. (org-element-map tree 'link 'identity info t) info)))))
  2685. ;; Handle url-encoded fuzzy links.
  2686. (should
  2687. (org-test-with-parsed-data "* A B\n[[A%20B]]"
  2688. (org-export-resolve-fuzzy-link
  2689. (org-element-map tree 'link #'identity info t) info))))
  2690. (ert-deftest test-org-export/resolve-id-link ()
  2691. "Test `org-export-resolve-id-link' specifications."
  2692. ;; Regular test for custom-id link.
  2693. (should
  2694. (equal '("Headline1")
  2695. (org-test-with-parsed-data "* Headline1
  2696. :PROPERTIES:
  2697. :CUSTOM_ID: test
  2698. :END:
  2699. * Headline 2
  2700. \[[#test]]"
  2701. (org-element-property
  2702. :title
  2703. (org-export-resolve-id-link
  2704. (org-element-map tree 'link 'identity info t) info)))))
  2705. ;; Throw an error on failing searches.
  2706. (should-error
  2707. (org-test-with-parsed-data "* Headline1
  2708. :PROPERTIES:
  2709. :CUSTOM_ID: test
  2710. :END:
  2711. * Headline 2
  2712. \[[#no-match]]"
  2713. (org-export-resolve-id-link
  2714. (org-element-map tree 'link 'identity info t) info)))
  2715. ;; Test for internal id target.
  2716. (should
  2717. (equal '("Headline1")
  2718. (org-test-with-parsed-data "* Headline1
  2719. :PROPERTIES:
  2720. :ID: aaaa
  2721. :END:
  2722. * Headline 2
  2723. \[[id:aaaa]]"
  2724. (org-element-property
  2725. :title
  2726. (org-export-resolve-id-link
  2727. (org-element-map tree 'link 'identity info t) info)))))
  2728. ;; Test for external id target.
  2729. (should
  2730. (equal
  2731. "external-file"
  2732. (org-test-with-parsed-data "[[id:aaaa]]"
  2733. (org-export-resolve-id-link
  2734. (org-element-map tree 'link 'identity info t)
  2735. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  2736. (ert-deftest test-org-export/resolve-radio-link ()
  2737. "Test `org-export-resolve-radio-link' specifications."
  2738. ;; Standard test.
  2739. (should
  2740. (org-test-with-temp-text "<<<radio>>> radio"
  2741. (org-update-radio-target-regexp)
  2742. (let* ((tree (org-element-parse-buffer))
  2743. (info `(:parse-tree ,tree)))
  2744. (org-export-resolve-radio-link
  2745. (org-element-map tree 'link 'identity info t)
  2746. info))))
  2747. ;; Radio targets are case-insensitive.
  2748. (should
  2749. (org-test-with-temp-text "<<<RADIO>>> radio"
  2750. (org-update-radio-target-regexp)
  2751. (let* ((tree (org-element-parse-buffer))
  2752. (info `(:parse-tree ,tree)))
  2753. (org-export-resolve-radio-link
  2754. (org-element-map tree 'link 'identity info t)
  2755. info))))
  2756. ;; Radio target with objects.
  2757. (should
  2758. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2759. (org-update-radio-target-regexp)
  2760. (let* ((tree (org-element-parse-buffer))
  2761. (info `(:parse-tree ,tree)))
  2762. (org-export-resolve-radio-link
  2763. (org-element-map tree 'link 'identity info t)
  2764. info))))
  2765. ;; Radio target with objects at its beginning.
  2766. (should
  2767. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2768. (org-update-radio-target-regexp)
  2769. (let* ((tree (org-element-parse-buffer))
  2770. (info `(:parse-tree ,tree)))
  2771. (org-export-resolve-radio-link
  2772. (org-element-map tree 'link 'identity info t)
  2773. info))))
  2774. ;; Radio link next to an apostrophe.
  2775. (should
  2776. (org-test-with-temp-text "<<<radio>>> radio's"
  2777. (org-update-radio-target-regexp)
  2778. (let* ((tree (org-element-parse-buffer))
  2779. (info `(:parse-tree ,tree)))
  2780. (org-export-resolve-radio-link
  2781. (org-element-map tree 'link 'identity info t)
  2782. info))))
  2783. ;; Multiple radio targets.
  2784. (should
  2785. (equal '("radio1" "radio2")
  2786. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  2787. (org-update-radio-target-regexp)
  2788. (let* ((tree (org-element-parse-buffer))
  2789. (info `(:parse-tree ,tree)))
  2790. (org-element-map tree 'link
  2791. (lambda (link)
  2792. (org-element-property
  2793. :value (org-export-resolve-radio-link link info)))
  2794. info)))))
  2795. ;; Radio target is whitespace insensitive.
  2796. (should
  2797. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  2798. (org-update-radio-target-regexp)
  2799. (let* ((tree (org-element-parse-buffer))
  2800. (info `(:parse-tree ,tree)))
  2801. (org-element-map tree 'link
  2802. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  2803. (ert-deftest test-org-export/file-uri ()
  2804. "Test `org-export-file-uri' specifications."
  2805. ;; Preserve relative filenames.
  2806. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  2807. ;; Local files start with "file:///"
  2808. (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
  2809. ;; Remote files start with "file://"
  2810. (should (equal "file://myself@some.where:papers/last.pdf"
  2811. (org-export-file-uri "/myself@some.where:papers/last.pdf")))
  2812. (should (equal "file://localhost/etc/fstab"
  2813. (org-export-file-uri "//localhost/etc/fstab")))
  2814. ;; Expand filename starting with "~".
  2815. (should (equal (org-export-file-uri "~/file.org")
  2816. (concat "file://" (expand-file-name "~/file.org")))))
  2817. ;;; Src-block and example-block
  2818. (ert-deftest test-org-export/unravel-code ()
  2819. "Test `org-export-unravel-code' function."
  2820. ;; Code without reference.
  2821. (should
  2822. (equal '("(+ 1 1)\n")
  2823. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2824. (org-export-unravel-code (org-element-at-point)))))
  2825. ;; Code with reference.
  2826. (should
  2827. (equal '("(+ 1 1)\n" (1 . "test"))
  2828. (org-test-with-temp-text
  2829. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  2830. (let ((org-coderef-label-format "(ref:%s)"))
  2831. (org-export-unravel-code (org-element-at-point))))))
  2832. ;; Code with user-defined reference.
  2833. (should
  2834. (equal
  2835. '("(+ 1 1)\n" (1 . "test"))
  2836. (org-test-with-temp-text
  2837. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  2838. (let ((org-coderef-label-format "(ref:%s)"))
  2839. (org-export-unravel-code (org-element-at-point))))))
  2840. ;; Code references keys are relative to the current block.
  2841. (should
  2842. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  2843. (org-test-with-temp-text "
  2844. #+BEGIN_EXAMPLE -n
  2845. \(+ 1 1)
  2846. #+END_EXAMPLE
  2847. #+BEGIN_EXAMPLE +n
  2848. \(+ 2 2)
  2849. \(+ 3 3) (ref:one)
  2850. #+END_EXAMPLE"
  2851. (goto-line 5)
  2852. (let ((org-coderef-label-format "(ref:%s)"))
  2853. (org-export-unravel-code (org-element-at-point)))))))
  2854. (ert-deftest test-org-export/format-code-default ()
  2855. "Test `org-export-format-code-default' specifications."
  2856. ;; Return the empty string when code is empty.
  2857. (should
  2858. (equal ""
  2859. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  2860. (org-export-format-code-default
  2861. (org-element-map tree 'src-block 'identity info t) info))))
  2862. ;; Number lines, two whitespace characters before the actual loc.
  2863. (should
  2864. (equal "1 a\n2 b\n"
  2865. (org-test-with-parsed-data
  2866. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  2867. (org-export-format-code-default
  2868. (org-element-map tree 'src-block 'identity info t) info))))
  2869. ;; Put references 6 whitespace characters after the widest line,
  2870. ;; wrapped within parenthesis.
  2871. (should
  2872. (equal "123 (a)\n1 (b)\n"
  2873. (let ((org-coderef-label-format "(ref:%s)"))
  2874. (org-test-with-parsed-data
  2875. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  2876. (org-export-format-code-default
  2877. (org-element-map tree 'src-block 'identity info t) info))))))
  2878. ;;; Smart Quotes
  2879. (ert-deftest test-org-export/activate-smart-quotes ()
  2880. "Test `org-export-activate-smart-quotes' specifications."
  2881. ;; Double quotes: standard test.
  2882. (should
  2883. (equal
  2884. '("some &ldquo;quoted&rdquo; text")
  2885. (let ((org-export-default-language "en"))
  2886. (org-test-with-parsed-data "some \"quoted\" text"
  2887. (org-element-map tree 'plain-text
  2888. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2889. info)))))
  2890. ;; Opening quotes: at the beginning of a paragraph.
  2891. (should
  2892. (equal
  2893. '("&ldquo;begin")
  2894. (let ((org-export-default-language "en"))
  2895. (org-test-with-parsed-data "\"begin"
  2896. (org-element-map tree 'plain-text
  2897. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2898. info)))))
  2899. ;; Opening quotes: after an object.
  2900. (should
  2901. (equal
  2902. '("&ldquo;quoted&rdquo; text")
  2903. (let ((org-export-default-language "en"))
  2904. (org-test-with-parsed-data "=verb= \"quoted\" text"
  2905. (org-element-map tree 'plain-text
  2906. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2907. info)))))
  2908. ;; Closing quotes: at the end of a paragraph.
  2909. (should
  2910. (equal
  2911. '("Quoted &ldquo;text&rdquo;")
  2912. (let ((org-export-default-language "en"))
  2913. (org-test-with-parsed-data "Quoted \"text\""
  2914. (org-element-map tree 'plain-text
  2915. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2916. info)))))
  2917. ;; Inner quotes: standard test.
  2918. (should
  2919. (equal '("« outer « inner » outer »")
  2920. (let ((org-export-default-language "fr"))
  2921. (org-test-with-parsed-data "\"outer 'inner' outer\""
  2922. (org-element-map tree 'plain-text
  2923. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2924. info)))))
  2925. ;; Inner quotes: close to special symbols.
  2926. (should
  2927. (equal '("« outer (« inner ») outer »")
  2928. (let ((org-export-default-language "fr"))
  2929. (org-test-with-parsed-data "\"outer ('inner') outer\""
  2930. (org-element-map tree 'plain-text
  2931. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2932. info)))))
  2933. (should
  2934. (equal '("« « inner » »")
  2935. (let ((org-export-default-language "fr"))
  2936. (org-test-with-parsed-data "\"'inner'\""
  2937. (org-element-map tree 'plain-text
  2938. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2939. info)))))
  2940. ;; Apostrophe: standard test.
  2941. (should
  2942. (equal '("It « shouldn’t » fail")
  2943. (let ((org-export-default-language "fr"))
  2944. (org-test-with-parsed-data "It \"shouldn't\" fail"
  2945. (org-element-map tree 'plain-text
  2946. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2947. info)))))
  2948. (should
  2949. (equal '("It shouldn’t fail")
  2950. (let ((org-export-default-language "fr"))
  2951. (org-test-with-parsed-data "It shouldn't fail"
  2952. (org-element-map tree 'plain-text
  2953. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2954. info)))))
  2955. ;; Apostrophe: before an object.
  2956. (should
  2957. (equal
  2958. '("« a’" " »")
  2959. (let ((org-export-default-language "fr"))
  2960. (org-test-with-parsed-data "\"a'=b=\""
  2961. (org-element-map tree 'plain-text
  2962. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2963. info)))))
  2964. ;; Apostrophe: after an object.
  2965. (should
  2966. (equal '("« " "’s »")
  2967. (let ((org-export-default-language "fr"))
  2968. (org-test-with-parsed-data "\"=code='s\""
  2969. (org-element-map tree 'plain-text
  2970. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2971. info)))))
  2972. ;; Special case: isolated quotes.
  2973. (should
  2974. (equal '("&ldquo;" "&rdquo;")
  2975. (let ((org-export-default-language "en"))
  2976. (org-test-with-parsed-data "\"$x$\""
  2977. (org-element-map tree 'plain-text
  2978. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2979. info)))))
  2980. ;; Smart quotes in secondary strings.
  2981. (should
  2982. (equal '("&ldquo;" "&rdquo;")
  2983. (let ((org-export-default-language "en"))
  2984. (org-test-with-parsed-data "* \"$x$\""
  2985. (org-element-map tree 'plain-text
  2986. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2987. info)))))
  2988. ;; Smart quotes in document keywords.
  2989. (should
  2990. (equal '("&ldquo;" "&rdquo;")
  2991. (let ((org-export-default-language "en"))
  2992. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  2993. (org-element-map (plist-get info :title) 'plain-text
  2994. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2995. info)))))
  2996. ;; Smart quotes in parsed affiliated keywords.
  2997. (should
  2998. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  2999. (let ((org-export-default-language "en"))
  3000. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  3001. (org-element-map tree 'plain-text
  3002. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3003. info nil nil t)))))
  3004. ;; Smart quotes within objects.
  3005. (should
  3006. (equal '("&ldquo;foo&rdquo;")
  3007. (let ((org-export-default-language "en"))
  3008. (org-test-with-parsed-data "| \"foo\" |"
  3009. (org-element-map tree 'plain-text
  3010. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3011. info nil nil t)))))
  3012. ;; FIXME: Test failing non-interactively.
  3013. ;;
  3014. ;; (should
  3015. ;; (equal '("&ldquo;foo&rdquo;")
  3016. ;; (let ((org-export-default-language "en"))
  3017. ;; (org-test-with-parsed-data "*\"foo\"*"
  3018. ;; (org-element-map tree 'plain-text
  3019. ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
  3020. ;; info nil nil t)))))
  3021. )
  3022. ;;; Tables
  3023. (ert-deftest test-org-export/special-column ()
  3024. "Test if the table's special column is properly recognized."
  3025. ;; 1. First column is special if it contains only a special marking
  3026. ;; characters or empty cells.
  3027. (org-test-with-temp-text "
  3028. | ! | 1 |
  3029. | | 2 |"
  3030. (should
  3031. (org-export-table-has-special-column-p
  3032. (org-element-map
  3033. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3034. ;; 2. If the column contains anything else, it isn't special.
  3035. (org-test-with-temp-text "
  3036. | ! | 1 |
  3037. | b | 2 |"
  3038. (should-not
  3039. (org-export-table-has-special-column-p
  3040. (org-element-map
  3041. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3042. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  3043. ;; and "!".
  3044. (org-test-with-temp-text "
  3045. | # | 1 |
  3046. | ^ | 2 |
  3047. | * | 3 |
  3048. | _ | 4 |
  3049. | / | 5 |
  3050. | $ | 6 |
  3051. | ! | 7 |"
  3052. (should
  3053. (org-export-table-has-special-column-p
  3054. (org-element-map
  3055. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3056. ;; 4. A first column with only empty cells isn't considered as
  3057. ;; special.
  3058. (org-test-with-temp-text "
  3059. | | 1 |
  3060. | | 2 |"
  3061. (should-not
  3062. (org-export-table-has-special-column-p
  3063. (org-element-map
  3064. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  3065. (ert-deftest test-org-export/table-row-is-special-p ()
  3066. "Test `org-export-table-row-is-special-p' specifications."
  3067. ;; 1. A row is special if it has a special marking character in the
  3068. ;; special column.
  3069. (org-test-with-parsed-data "| ! | 1 |"
  3070. (should
  3071. (org-export-table-row-is-special-p
  3072. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3073. ;; 2. A row is special when its first field is "/"
  3074. (org-test-with-parsed-data "
  3075. | / | 1 |
  3076. | a | b |"
  3077. (should
  3078. (org-export-table-row-is-special-p
  3079. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3080. ;; 3. A row only containing alignment cookies is also considered as
  3081. ;; special.
  3082. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  3083. (should
  3084. (org-export-table-row-is-special-p
  3085. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3086. ;; 4. Everything else isn't considered as special.
  3087. (org-test-with-parsed-data "| \alpha | | c |"
  3088. (should-not
  3089. (org-export-table-row-is-special-p
  3090. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3091. ;; 5. Table's rules are never considered as special rows.
  3092. (org-test-with-parsed-data "|---+---|"
  3093. (should-not
  3094. (org-export-table-row-is-special-p
  3095. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  3096. (ert-deftest test-org-export/has-header-p ()
  3097. "Test `org-export-table-has-header-p' specifications."
  3098. ;; 1. With an header.
  3099. (org-test-with-parsed-data "
  3100. | a | b |
  3101. |---+---|
  3102. | c | d |"
  3103. (should
  3104. (org-export-table-has-header-p
  3105. (org-element-map tree 'table 'identity info 'first-match)
  3106. info)))
  3107. ;; 2. Without an header.
  3108. (org-test-with-parsed-data "
  3109. | a | b |
  3110. | c | d |"
  3111. (should-not
  3112. (org-export-table-has-header-p
  3113. (org-element-map tree 'table 'identity info 'first-match)
  3114. info)))
  3115. ;; 3. Don't get fooled with starting and ending rules.
  3116. (org-test-with-parsed-data "
  3117. |---+---|
  3118. | a | b |
  3119. | c | d |
  3120. |---+---|"
  3121. (should-not
  3122. (org-export-table-has-header-p
  3123. (org-element-map tree 'table 'identity info 'first-match)
  3124. info))))
  3125. (ert-deftest test-org-export/table-row-group ()
  3126. "Test `org-export-table-row-group' specifications."
  3127. ;; 1. A rule creates a new group.
  3128. (should
  3129. (equal '(1 rule 2)
  3130. (org-test-with-parsed-data "
  3131. | a | b |
  3132. |---+---|
  3133. | 1 | 2 |"
  3134. (org-element-map tree 'table-row
  3135. (lambda (row)
  3136. (if (eq (org-element-property :type row) 'rule) 'rule
  3137. (org-export-table-row-group row info)))))))
  3138. ;; 2. Special rows are ignored in count.
  3139. (should
  3140. (equal
  3141. '(rule 1)
  3142. (org-test-with-parsed-data "
  3143. | / | < | > |
  3144. |---|---+---|
  3145. | | 1 | 2 |"
  3146. (org-element-map tree 'table-row
  3147. (lambda (row)
  3148. (if (eq (org-element-property :type row) 'rule) 'rule
  3149. (org-export-table-row-group row info)))
  3150. info))))
  3151. ;; 3. Double rules also are ignored in count.
  3152. (should
  3153. (equal '(1 rule rule 2)
  3154. (org-test-with-parsed-data "
  3155. | a | b |
  3156. |---+---|
  3157. |---+---|
  3158. | 1 | 2 |"
  3159. (org-element-map tree 'table-row
  3160. (lambda (row)
  3161. (if (eq (org-element-property :type row) 'rule) 'rule
  3162. (org-export-table-row-group row info))))))))
  3163. (ert-deftest test-org-export/table-row-number ()
  3164. "Test `org-export-table-row-number' specifications."
  3165. ;; Standard test. Number is 0-indexed.
  3166. (should
  3167. (equal '(0 1)
  3168. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  3169. (org-element-map tree 'table-row
  3170. (lambda (row) (org-export-table-row-number row info)) info))))
  3171. ;; Number ignores separators.
  3172. (should
  3173. (equal '(0 1)
  3174. (org-test-with-parsed-data "
  3175. | a | b | c |
  3176. |---+---+---|
  3177. | d | e | f |"
  3178. (org-element-map tree 'table-row
  3179. (lambda (row) (org-export-table-row-number row info)) info))))
  3180. ;; Number ignores special rows.
  3181. (should
  3182. (equal '(0 1)
  3183. (org-test-with-parsed-data "
  3184. | / | < | > |
  3185. | | b | c |
  3186. |---+-----+-----|
  3187. | | <c> | <c> |
  3188. | | e | f |"
  3189. (org-element-map tree 'table-row
  3190. (lambda (row) (org-export-table-row-number row info)) info)))))
  3191. (ert-deftest test-org-export/table-cell-width ()
  3192. "Test `org-export-table-cell-width' specifications."
  3193. ;; 1. Width is primarily determined by width cookies. If no cookie
  3194. ;; is found, cell's width is nil.
  3195. (org-test-with-parsed-data "
  3196. | / | <l> | <6> | <l7> |
  3197. | | a | b | c |"
  3198. (should
  3199. (equal
  3200. '(nil 6 7)
  3201. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3202. (org-element-map tree 'table-cell 'identity info)))))
  3203. ;; 2. The last width cookie has precedence.
  3204. (org-test-with-parsed-data "
  3205. | <6> |
  3206. | <7> |
  3207. | a |"
  3208. (should
  3209. (equal
  3210. '(7)
  3211. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3212. (org-element-map tree 'table-cell 'identity info)))))
  3213. ;; 3. Valid width cookies must have a specific row.
  3214. (org-test-with-parsed-data "| <6> | cell |"
  3215. (should
  3216. (equal
  3217. '(nil nil)
  3218. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3219. (org-element-map tree 'table-cell 'identity))))))
  3220. (ert-deftest test-org-export/table-cell-alignment ()
  3221. "Test `org-export-table-cell-alignment' specifications."
  3222. ;; 1. Alignment is primarily determined by alignment cookies.
  3223. (should
  3224. (equal '(left center right)
  3225. (let ((org-table-number-fraction 0.5)
  3226. (org-table-number-regexp "^[0-9]+$"))
  3227. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  3228. (mapcar (lambda (cell)
  3229. (org-export-table-cell-alignment cell info))
  3230. (org-element-map tree 'table-cell 'identity))))))
  3231. ;; 2. The last alignment cookie has precedence.
  3232. (should
  3233. (equal '(right right right)
  3234. (org-test-with-parsed-data "
  3235. | <l8> |
  3236. | cell |
  3237. | <r9> |"
  3238. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  3239. (org-element-map tree 'table-cell 'identity)))))
  3240. ;; 3. If there's no cookie, cell's contents determine alignment.
  3241. ;; A column mostly made of cells containing numbers will align
  3242. ;; its cells to the right.
  3243. (should
  3244. (equal '(right right right)
  3245. (let ((org-table-number-fraction 0.5)
  3246. (org-table-number-regexp "^[0-9]+$"))
  3247. (org-test-with-parsed-data "
  3248. | 123 |
  3249. | some text |
  3250. | 12345 |"
  3251. (mapcar (lambda (cell)
  3252. (org-export-table-cell-alignment cell info))
  3253. (org-element-map tree 'table-cell 'identity))))))
  3254. ;; 4. Otherwise, they will be aligned to the left.
  3255. (should
  3256. (equal '(left left left)
  3257. (org-test-with-parsed-data "
  3258. | text |
  3259. | some text |
  3260. | \alpha |"
  3261. (mapcar (lambda (cell)
  3262. (org-export-table-cell-alignment cell info))
  3263. (org-element-map tree 'table-cell 'identity info))))))
  3264. (ert-deftest test-org-export/table-cell-borders ()
  3265. "Test `org-export-table-cell-borders' specifications."
  3266. ;; 1. Recognize various column groups indicators.
  3267. (org-test-with-parsed-data "| / | < | > | <> |"
  3268. (should
  3269. (equal
  3270. '((right bottom top) (left bottom top) (right bottom top)
  3271. (right left bottom top))
  3272. (mapcar (lambda (cell)
  3273. (org-export-table-cell-borders cell info))
  3274. (org-element-map tree 'table-cell 'identity)))))
  3275. ;; 2. Accept shortcuts to define column groups.
  3276. (org-test-with-parsed-data "| / | < | < |"
  3277. (should
  3278. (equal
  3279. '((right bottom top) (right left bottom top) (left bottom top))
  3280. (mapcar (lambda (cell)
  3281. (org-export-table-cell-borders cell info))
  3282. (org-element-map tree 'table-cell 'identity)))))
  3283. ;; 3. A valid column groups row must start with a "/".
  3284. (org-test-with-parsed-data "
  3285. | | < |
  3286. | a | b |"
  3287. (should
  3288. (equal '((top) (top) (bottom) (bottom))
  3289. (mapcar (lambda (cell)
  3290. (org-export-table-cell-borders cell info))
  3291. (org-element-map tree 'table-cell 'identity)))))
  3292. ;; 4. Take table rules into consideration.
  3293. (org-test-with-parsed-data "
  3294. | 1 |
  3295. |---|
  3296. | 2 |"
  3297. (should
  3298. (equal '((below top) (bottom above))
  3299. (mapcar (lambda (cell)
  3300. (org-export-table-cell-borders cell info))
  3301. (org-element-map tree 'table-cell 'identity)))))
  3302. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  3303. ;; (resp. `bottom' and `below') borders. Any special row is
  3304. ;; ignored.
  3305. (org-test-with-parsed-data "
  3306. |---+----|
  3307. | / | |
  3308. | | 1 |
  3309. |---+----|"
  3310. (should
  3311. (equal '((bottom below top above))
  3312. (last
  3313. (mapcar (lambda (cell)
  3314. (org-export-table-cell-borders cell info))
  3315. (org-element-map tree 'table-cell 'identity)))))))
  3316. (ert-deftest test-org-export/table-dimensions ()
  3317. "Test `org-export-table-dimensions' specifications."
  3318. ;; 1. Standard test.
  3319. (org-test-with-parsed-data "
  3320. | 1 | 2 | 3 |
  3321. | 4 | 5 | 6 |"
  3322. (should
  3323. (equal '(2 . 3)
  3324. (org-export-table-dimensions
  3325. (org-element-map tree 'table 'identity info 'first-match) info))))
  3326. ;; 2. Ignore horizontal rules and special columns.
  3327. (org-test-with-parsed-data "
  3328. | / | < | > |
  3329. | 1 | 2 | 3 |
  3330. |---+---+---|
  3331. | 4 | 5 | 6 |"
  3332. (should
  3333. (equal '(2 . 3)
  3334. (org-export-table-dimensions
  3335. (org-element-map tree 'table 'identity info 'first-match) info)))))
  3336. (ert-deftest test-org-export/table-cell-address ()
  3337. "Test `org-export-table-cell-address' specifications."
  3338. ;; 1. Standard test: index is 0-based.
  3339. (org-test-with-parsed-data "| a | b |"
  3340. (should
  3341. (equal '((0 . 0) (0 . 1))
  3342. (org-element-map tree 'table-cell
  3343. (lambda (cell) (org-export-table-cell-address cell info))
  3344. info))))
  3345. ;; 2. Special column isn't counted, nor are special rows.
  3346. (org-test-with-parsed-data "
  3347. | / | <> |
  3348. | | c |"
  3349. (should
  3350. (equal '(0 . 0)
  3351. (org-export-table-cell-address
  3352. (car (last (org-element-map tree 'table-cell 'identity info)))
  3353. info))))
  3354. ;; 3. Tables rules do not count either.
  3355. (org-test-with-parsed-data "
  3356. | a |
  3357. |---|
  3358. | b |
  3359. |---|
  3360. | c |"
  3361. (should
  3362. (equal '(2 . 0)
  3363. (org-export-table-cell-address
  3364. (car (last (org-element-map tree 'table-cell 'identity info)))
  3365. info))))
  3366. ;; 4. Return nil for special cells.
  3367. (org-test-with-parsed-data "| / | a |"
  3368. (should-not
  3369. (org-export-table-cell-address
  3370. (org-element-map tree 'table-cell 'identity nil 'first-match)
  3371. info))))
  3372. (ert-deftest test-org-export/get-table-cell-at ()
  3373. "Test `org-export-get-table-cell-at' specifications."
  3374. ;; 1. Address ignores special columns, special rows and rules.
  3375. (org-test-with-parsed-data "
  3376. | / | <> |
  3377. | | a |
  3378. |---+----|
  3379. | | b |"
  3380. (should
  3381. (equal '("b")
  3382. (org-element-contents
  3383. (org-export-get-table-cell-at
  3384. '(1 . 0)
  3385. (org-element-map tree 'table 'identity info 'first-match)
  3386. info)))))
  3387. ;; 2. Return value for a non-existent address is nil.
  3388. (org-test-with-parsed-data "| a |"
  3389. (should-not
  3390. (org-export-get-table-cell-at
  3391. '(2 . 2)
  3392. (org-element-map tree 'table 'identity info 'first-match)
  3393. info)))
  3394. (org-test-with-parsed-data "| / |"
  3395. (should-not
  3396. (org-export-get-table-cell-at
  3397. '(0 . 0)
  3398. (org-element-map tree 'table 'identity info 'first-match)
  3399. info))))
  3400. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  3401. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  3402. ;; 1. A cell at a beginning of a row always starts a column group.
  3403. (org-test-with-parsed-data "| a |"
  3404. (should
  3405. (org-export-table-cell-starts-colgroup-p
  3406. (org-element-map tree 'table-cell 'identity info 'first-match)
  3407. info)))
  3408. ;; 2. Special column should be ignored when determining the
  3409. ;; beginning of the row.
  3410. (org-test-with-parsed-data "
  3411. | / | |
  3412. | | a |"
  3413. (should
  3414. (org-export-table-cell-starts-colgroup-p
  3415. (org-element-map tree 'table-cell 'identity info 'first-match)
  3416. info)))
  3417. ;; 2. Explicit column groups.
  3418. (org-test-with-parsed-data "
  3419. | / | | < |
  3420. | a | b | c |"
  3421. (should
  3422. (equal
  3423. '(yes no yes)
  3424. (org-element-map tree 'table-cell
  3425. (lambda (cell)
  3426. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  3427. info)))))
  3428. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  3429. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  3430. ;; 1. A cell at the end of a row always ends a column group.
  3431. (org-test-with-parsed-data "| a |"
  3432. (should
  3433. (org-export-table-cell-ends-colgroup-p
  3434. (org-element-map tree 'table-cell 'identity info 'first-match)
  3435. info)))
  3436. ;; 2. Special column should be ignored when determining the
  3437. ;; beginning of the row.
  3438. (org-test-with-parsed-data "
  3439. | / | |
  3440. | | a |"
  3441. (should
  3442. (org-export-table-cell-ends-colgroup-p
  3443. (org-element-map tree 'table-cell 'identity info 'first-match)
  3444. info)))
  3445. ;; 3. Explicit column groups.
  3446. (org-test-with-parsed-data "
  3447. | / | < | |
  3448. | a | b | c |"
  3449. (should
  3450. (equal
  3451. '(yes no yes)
  3452. (org-element-map tree 'table-cell
  3453. (lambda (cell)
  3454. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  3455. info)))))
  3456. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  3457. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  3458. ;; 1. A row at the beginning of a table always starts a row group.
  3459. ;; So does a row following a table rule.
  3460. (org-test-with-parsed-data "
  3461. | a |
  3462. |---|
  3463. | b |"
  3464. (should
  3465. (equal
  3466. '(yes no yes)
  3467. (org-element-map tree 'table-row
  3468. (lambda (row)
  3469. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3470. info))))
  3471. ;; 2. Special rows should be ignored when determining the beginning
  3472. ;; of the row.
  3473. (org-test-with-parsed-data "
  3474. | / | < |
  3475. | | a |
  3476. |---+---|
  3477. | / | < |
  3478. | | b |"
  3479. (should
  3480. (equal
  3481. '(yes no yes)
  3482. (org-element-map tree 'table-row
  3483. (lambda (row)
  3484. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3485. info)))))
  3486. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  3487. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  3488. ;; 1. A row at the end of a table always ends a row group. So does
  3489. ;; a row preceding a table rule.
  3490. (org-test-with-parsed-data "
  3491. | a |
  3492. |---|
  3493. | b |"
  3494. (should
  3495. (equal
  3496. '(yes no yes)
  3497. (org-element-map tree 'table-row
  3498. (lambda (row)
  3499. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3500. info))))
  3501. ;; 2. Special rows should be ignored when determining the beginning
  3502. ;; of the row.
  3503. (org-test-with-parsed-data "
  3504. | | a |
  3505. | / | < |
  3506. |---+---|
  3507. | | b |
  3508. | / | < |"
  3509. (should
  3510. (equal
  3511. '(yes no yes)
  3512. (org-element-map tree 'table-row
  3513. (lambda (row)
  3514. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3515. info)))))
  3516. (ert-deftest test-org-export/table-row-in-header-p ()
  3517. "Test `org-export-table-row-in-header-p' specifications."
  3518. ;; Standard test. Separators are always nil.
  3519. (should
  3520. (equal
  3521. '(yes no no)
  3522. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  3523. (org-element-map tree 'table-row
  3524. (lambda (row)
  3525. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  3526. ;; Nil when there is no header.
  3527. (should
  3528. (equal
  3529. '(no no)
  3530. (org-test-with-parsed-data "| a |\n| b |"
  3531. (org-element-map tree 'table-row
  3532. (lambda (row)
  3533. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  3534. (ert-deftest test-org-export/table-row-starts-header-p ()
  3535. "Test `org-export-table-row-starts-header-p' specifications."
  3536. ;; 1. Only the row starting the first row group starts the table
  3537. ;; header.
  3538. (org-test-with-parsed-data "
  3539. | a |
  3540. | b |
  3541. |---|
  3542. | c |"
  3543. (should
  3544. (equal
  3545. '(yes no no no)
  3546. (org-element-map tree 'table-row
  3547. (lambda (row)
  3548. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  3549. info))))
  3550. ;; 2. A row cannot start an header if there's no header in the
  3551. ;; table.
  3552. (org-test-with-parsed-data "
  3553. | a |
  3554. |---|"
  3555. (should-not
  3556. (org-export-table-row-starts-header-p
  3557. (org-element-map tree 'table-row 'identity info 'first-match)
  3558. info))))
  3559. (ert-deftest test-org-export/table-row-ends-header-p ()
  3560. "Test `org-export-table-row-ends-header-p' specifications."
  3561. ;; 1. Only the row starting the first row group starts the table
  3562. ;; header.
  3563. (org-test-with-parsed-data "
  3564. | a |
  3565. | b |
  3566. |---|
  3567. | c |"
  3568. (should
  3569. (equal
  3570. '(no yes no no)
  3571. (org-element-map tree 'table-row
  3572. (lambda (row)
  3573. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  3574. info))))
  3575. ;; 2. A row cannot start an header if there's no header in the
  3576. ;; table.
  3577. (org-test-with-parsed-data "
  3578. | a |
  3579. |---|"
  3580. (should-not
  3581. (org-export-table-row-ends-header-p
  3582. (org-element-map tree 'table-row 'identity info 'first-match)
  3583. info))))
  3584. ;;; Tables of Contents
  3585. (ert-deftest test-org-export/collect-headlines ()
  3586. "Test `org-export-collect-headlines' specifications."
  3587. ;; Standard test.
  3588. (should
  3589. (= 2
  3590. (length
  3591. (org-test-with-parsed-data "* H1\n** H2"
  3592. (org-export-collect-headlines info)))))
  3593. ;; Do not collect headlines below optional argument.
  3594. (should
  3595. (= 1
  3596. (length
  3597. (org-test-with-parsed-data "* H1\n** H2"
  3598. (org-export-collect-headlines info 1)))))
  3599. ;; Never collect headlines below maximum headline level.
  3600. (should
  3601. (= 1
  3602. (length
  3603. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3604. (org-export-collect-headlines info)))))
  3605. (should
  3606. (= 1
  3607. (length
  3608. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3609. (org-export-collect-headlines info 2)))))
  3610. ;; Collect headlines locally.
  3611. (should
  3612. (= 2
  3613. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  3614. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3615. (length (org-export-collect-headlines info nil scope))))))
  3616. ;; When collecting locally, optional level is relative.
  3617. (should
  3618. (= 1
  3619. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  3620. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3621. (length (org-export-collect-headlines info 1 scope)))))))
  3622. ;;; Templates
  3623. (ert-deftest test-org-export/inner-template ()
  3624. "Test `inner-template' translator specifications."
  3625. (should
  3626. (equal "Success!"
  3627. (org-test-with-temp-text "* Headline"
  3628. (org-export-as
  3629. (org-export-create-backend
  3630. :transcoders
  3631. '((inner-template . (lambda (contents info) "Success!"))
  3632. (headline . (lambda (h c i) "Headline"))))))))
  3633. ;; Inner template is applied even in a "body-only" export.
  3634. (should
  3635. (equal "Success!"
  3636. (org-test-with-temp-text "* Headline"
  3637. (org-export-as
  3638. (org-export-create-backend
  3639. :transcoders '((inner-template . (lambda (c i) "Success!"))
  3640. (headline . (lambda (h c i) "Headline"))))
  3641. nil nil 'body-only)))))
  3642. (ert-deftest test-org-export/template ()
  3643. "Test `template' translator specifications."
  3644. (should
  3645. (equal "Success!"
  3646. (org-test-with-temp-text "* Headline"
  3647. (org-export-as
  3648. (org-export-create-backend
  3649. :transcoders '((template . (lambda (contents info) "Success!"))
  3650. (headline . (lambda (h c i) "Headline"))))))))
  3651. ;; Template is not applied in a "body-only" export.
  3652. (should-not
  3653. (equal "Success!"
  3654. (org-test-with-temp-text "* Headline"
  3655. (org-export-as
  3656. (org-export-create-backend
  3657. :transcoders '((template . (lambda (contents info) "Success!"))
  3658. (headline . (lambda (h c i) "Headline"))))
  3659. nil nil 'body-only)))))
  3660. ;;; Topology
  3661. (ert-deftest test-org-export/get-next-element ()
  3662. "Test `org-export-get-next-element' specifications."
  3663. ;; Standard test.
  3664. (should
  3665. (equal "b"
  3666. (org-test-with-parsed-data "* Headline\n*a* b"
  3667. (org-export-get-next-element
  3668. (org-element-map tree 'bold 'identity info t) info))))
  3669. ;; Return nil when no previous element.
  3670. (should-not
  3671. (org-test-with-parsed-data "* Headline\na *b*"
  3672. (org-export-get-next-element
  3673. (org-element-map tree 'bold 'identity info t) info)))
  3674. ;; Non-exportable elements are ignored.
  3675. (should-not
  3676. (let ((org-export-with-timestamps nil))
  3677. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  3678. (org-export-get-next-element
  3679. (org-element-map tree 'entity 'identity info t) info))))
  3680. ;; Find next element in secondary strings.
  3681. (should
  3682. (eq 'verbatim
  3683. (org-test-with-parsed-data "* a =verb="
  3684. (org-element-type
  3685. (org-export-get-next-element
  3686. (org-element-map tree 'plain-text 'identity info t) info)))))
  3687. (should
  3688. (eq 'verbatim
  3689. (org-test-with-parsed-data "* /italic/ =verb="
  3690. (org-element-type
  3691. (org-export-get-next-element
  3692. (org-element-map tree 'italic 'identity info t) info)))))
  3693. ;; Find next element in document keywords.
  3694. (should
  3695. (eq 'verbatim
  3696. (org-test-with-parsed-data "#+TITLE: a =verb="
  3697. (org-element-type
  3698. (org-export-get-next-element
  3699. (org-element-map
  3700. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3701. ;; Find next element in parsed affiliated keywords.
  3702. (should
  3703. (eq 'verbatim
  3704. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  3705. (org-element-type
  3706. (org-export-get-next-element
  3707. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3708. ;; With optional argument N, return a list containing all the
  3709. ;; following elements.
  3710. (should
  3711. (equal
  3712. '(bold code underline)
  3713. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3714. (mapcar 'car
  3715. (org-export-get-next-element
  3716. (org-element-map tree 'italic 'identity info t) info t)))))
  3717. ;; When N is a positive integer, return a list containing up to
  3718. ;; N following elements.
  3719. (should
  3720. (equal
  3721. '(bold code)
  3722. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3723. (mapcar 'car
  3724. (org-export-get-next-element
  3725. (org-element-map tree 'italic 'identity info t) info 2))))))
  3726. (ert-deftest test-org-export/get-previous-element ()
  3727. "Test `org-export-get-previous-element' specifications."
  3728. ;; Standard test.
  3729. (should
  3730. (equal "a "
  3731. (org-test-with-parsed-data "* Headline\na *b*"
  3732. (org-export-get-previous-element
  3733. (org-element-map tree 'bold 'identity info t) info))))
  3734. ;; Return nil when no previous element.
  3735. (should-not
  3736. (org-test-with-parsed-data "* Headline\n*a* b"
  3737. (org-export-get-previous-element
  3738. (org-element-map tree 'bold 'identity info t) info)))
  3739. ;; Non-exportable elements are ignored.
  3740. (should-not
  3741. (let ((org-export-with-timestamps nil))
  3742. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  3743. (org-export-get-previous-element
  3744. (org-element-map tree 'entity 'identity info t) info))))
  3745. ;; Find previous element in secondary strings.
  3746. (should
  3747. (eq 'verbatim
  3748. (org-test-with-parsed-data "* =verb= a"
  3749. (org-element-type
  3750. (org-export-get-previous-element
  3751. (org-element-map tree 'plain-text 'identity info t) info)))))
  3752. (should
  3753. (eq 'verbatim
  3754. (org-test-with-parsed-data "* =verb= /italic/"
  3755. (org-element-type
  3756. (org-export-get-previous-element
  3757. (org-element-map tree 'italic 'identity info t) info)))))
  3758. ;; Find previous element in document keywords.
  3759. (should
  3760. (eq 'verbatim
  3761. (org-test-with-parsed-data "#+TITLE: =verb= a"
  3762. (org-element-type
  3763. (org-export-get-previous-element
  3764. (org-element-map
  3765. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3766. ;; Find previous element in parsed affiliated keywords.
  3767. (should
  3768. (eq 'verbatim
  3769. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  3770. (org-element-type
  3771. (org-export-get-previous-element
  3772. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3773. ;; With optional argument N, return a list containing up to
  3774. ;; N previous elements.
  3775. (should
  3776. (equal '(underline italic bold)
  3777. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3778. (mapcar 'car
  3779. (org-export-get-previous-element
  3780. (org-element-map tree 'code 'identity info t) info t)))))
  3781. ;; When N is a positive integer, return a list containing up to
  3782. ;; N previous elements.
  3783. (should
  3784. (equal '(italic bold)
  3785. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3786. (mapcar 'car
  3787. (org-export-get-previous-element
  3788. (org-element-map tree 'code 'identity info t) info 2))))))
  3789. (provide 'test-ox)
  3790. ;;; test-org-export.el end here