test-ox.el 121 KB

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