test-ox.el 119 KB

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