test-ox.el 125 KB

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