test-ox.el 141 KB

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