test-ox.el 118 KB

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