test-ox.el 106 KB

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