test-ox.el 120 KB

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