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. (org-test-with-temp-text
  779. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  780. (org-export-expand-include-keyword)
  781. (should (equal (buffer-string)
  782. "Small Org file with an include keyword.
  783. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  784. Success!
  785. * Heading
  786. body\n")))
  787. ;; Localized insertion.
  788. (org-test-with-temp-text
  789. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  790. org-test-dir)
  791. (org-export-expand-include-keyword)
  792. (should (equal (buffer-string)
  793. "Small Org file with an include keyword.\n")))
  794. ;; Insertion with constraints on headlines level.
  795. (should
  796. (equal
  797. "* Top heading\n** Heading\nbody\n"
  798. (org-test-with-temp-text
  799. (format
  800. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  801. org-test-dir)
  802. (org-export-expand-include-keyword)
  803. (buffer-string))))
  804. (should
  805. (equal
  806. "* Top heading\n* Heading\nbody\n"
  807. (org-test-with-temp-text
  808. (format
  809. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
  810. org-test-dir)
  811. (org-export-expand-include-keyword)
  812. (buffer-string))))
  813. ;; Inclusion within an example block.
  814. (should
  815. (equal
  816. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  817. (org-test-with-temp-text
  818. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  819. org-test-dir)
  820. (org-export-expand-include-keyword)
  821. (buffer-string))))
  822. ;; Inclusion within a src-block.
  823. (should
  824. (equal
  825. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  826. (org-test-with-temp-text
  827. (format
  828. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  829. org-test-dir)
  830. (org-export-expand-include-keyword)
  831. (buffer-string))))
  832. ;; Inclusion within an html export-block.
  833. (should
  834. (equal
  835. "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
  836. (org-test-with-temp-text
  837. (format
  838. "#+INCLUDE: \"%s/examples/include.html\" HTML"
  839. org-test-dir)
  840. (org-export-expand-include-keyword)
  841. (buffer-string))))
  842. ;; Inclusion within an center paragraph
  843. (should
  844. (equal
  845. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  846. (org-test-with-temp-text
  847. (format
  848. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  849. org-test-dir)
  850. (org-export-expand-include-keyword)
  851. (buffer-string))))
  852. ;; Footnotes labels are local to each included file.
  853. (should
  854. (= 6
  855. (length
  856. (delete-dups
  857. (let ((contents "
  858. Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
  859. \[fn:1] Footnote 1
  860. \[fn:test] Footnote \"test\""))
  861. (org-test-with-temp-text-in-file contents
  862. (let ((file1 (buffer-file-name)))
  863. (org-test-with-temp-text-in-file contents
  864. (let ((file2 (buffer-file-name)))
  865. (org-test-with-temp-text
  866. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  867. file1 file2)
  868. (org-export-expand-include-keyword)
  869. (org-element-map (org-element-parse-buffer)
  870. 'footnote-reference
  871. (lambda (ref)
  872. (org-element-property :label ref)))))))))))))
  873. ;; Footnotes labels are not local to each include keyword.
  874. (should
  875. (= 3
  876. (length
  877. (delete-dups
  878. (let ((contents "
  879. Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
  880. \[fn:1] Footnote 1
  881. \[fn:test] Footnote \"test\""))
  882. (org-test-with-temp-text-in-file contents
  883. (let ((file (buffer-file-name)))
  884. (org-test-with-temp-text
  885. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  886. (org-export-expand-include-keyword)
  887. (org-element-map (org-element-parse-buffer)
  888. 'footnote-reference
  889. (lambda (ref) (org-element-property :label ref)))))))))))
  890. ;; If only-contents is non-nil only include contents of element.
  891. (should
  892. (equal
  893. "body\n"
  894. (org-test-with-temp-text
  895. (concat
  896. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
  897. ":only-contents t")
  898. (org-export-expand-include-keyword)
  899. (buffer-string))))
  900. ;; Headings can be included via CUSTOM_ID.
  901. (should
  902. (org-test-with-temp-text
  903. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  904. (org-export-expand-include-keyword)
  905. (goto-char (point-min))
  906. (looking-at "* Another heading")))
  907. ;; Named objects can be included.
  908. (should
  909. (equal
  910. "| 1 |\n"
  911. (org-test-with-temp-text
  912. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
  913. (org-export-expand-include-keyword)
  914. (buffer-string))))
  915. ;; Including non-existing elements should result in an error.
  916. (should-error
  917. (org-test-with-temp-text
  918. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
  919. (org-export-expand-include-keyword)))
  920. ;; Lines work relatively to an included element.
  921. (should
  922. (equal
  923. "2\n3\n"
  924. (org-test-with-temp-text
  925. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
  926. (org-export-expand-include-keyword)
  927. (buffer-string))))
  928. ;; Properties should be dropped from headlines.
  929. (should
  930. (equal
  931. (org-test-with-temp-text
  932. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
  933. (org-export-expand-include-keyword)
  934. (buffer-string))
  935. (org-test-with-temp-text
  936. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  937. (org-export-expand-include-keyword)
  938. (buffer-string))))
  939. ;; Properties should be dropped, drawers should not be.
  940. (should
  941. (equal
  942. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  943. (org-test-with-temp-text
  944. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
  945. (org-export-expand-include-keyword)
  946. (buffer-string)))))
  947. (ert-deftest test-org-export/expand-macro ()
  948. "Test macro expansion in an Org buffer."
  949. ;; Standard macro expansion.
  950. (should
  951. (equal "#+MACRO: macro1 value\nvalue\n"
  952. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  953. (org-export-as (org-test-default-backend)))))
  954. ;; Expand specific macros.
  955. (should
  956. (equal "me 2012-03-29 me@here Title\n"
  957. (org-test-with-temp-text
  958. "
  959. #+TITLE: Title
  960. #+DATE: 2012-03-29
  961. #+AUTHOR: me
  962. #+EMAIL: me@here
  963. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  964. (let ((output (org-export-as (org-test-default-backend))))
  965. (substring output (string-match ".*\n\\'" output))))))
  966. ;; Expand specific macros when property contained a regular macro
  967. ;; already.
  968. (should
  969. (equal "value\n"
  970. (org-test-with-temp-text "
  971. #+MACRO: macro1 value
  972. #+TITLE: {{{macro1}}}
  973. {{{title}}}"
  974. (let ((output (org-export-as (org-test-default-backend))))
  975. (substring output (string-match ".*\n\\'" output))))))
  976. ;; Expand macros with templates in included files.
  977. (should
  978. (equal "success\n"
  979. (org-test-with-temp-text
  980. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  981. {{{included-macro}}}" org-test-dir)
  982. (let ((output (org-export-as (org-test-default-backend))))
  983. (substring output (string-match ".*\n\\'" output)))))))
  984. (ert-deftest test-org-export/user-ignore-list ()
  985. "Test if `:ignore-list' accepts user input."
  986. (let ((backend (org-test-default-backend)))
  987. (setf (org-export-backend-transcoders backend)
  988. (cons '(template . (lambda (body i)
  989. (format "BEGIN\n%sEND" body)))
  990. (org-export-backend-transcoders backend)))
  991. (org-test-with-temp-text "Text"
  992. (should (equal (org-export-as backend nil nil 'body-only)
  993. "Text\n"))
  994. (should (equal (org-export-as backend) "BEGIN\nText\nEND"))))
  995. (should
  996. (equal
  997. "* Head1\n"
  998. (let ((org-export-filter-parse-tree-functions
  999. '((lambda (data backend info)
  1000. ;; Ignore headlines with the word "note" in their title.
  1001. (org-element-map data 'headline
  1002. (lambda (headline)
  1003. (when (string-match "\\<note\\>"
  1004. (org-element-property :raw-value
  1005. headline))
  1006. (org-export-ignore-element headline info)))
  1007. info)
  1008. data))))
  1009. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  1010. (org-export-as (org-test-default-backend)))))))
  1011. (ert-deftest test-org-export/before-processing-hook ()
  1012. "Test `org-export-before-processing-hook'."
  1013. (should
  1014. (equal
  1015. "#+MACRO: mac val\nTest\n"
  1016. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1017. (let ((org-export-before-processing-hook
  1018. '((lambda (backend)
  1019. (while (re-search-forward "{{{" nil t)
  1020. (let ((object (org-element-context)))
  1021. (when (eq (org-element-type object) 'macro)
  1022. (delete-region
  1023. (org-element-property :begin object)
  1024. (org-element-property :end object)))))))))
  1025. (org-export-as (org-test-default-backend)))))))
  1026. (ert-deftest test-org-export/before-parsing-hook ()
  1027. "Test `org-export-before-parsing-hook'."
  1028. (should
  1029. (equal "Body 1\nBody 2\n"
  1030. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1031. (let ((org-export-before-parsing-hook
  1032. '((lambda (backend)
  1033. (goto-char (point-min))
  1034. (while (re-search-forward org-outline-regexp-bol nil t)
  1035. (delete-region
  1036. (point-at-bol) (progn (forward-line) (point))))))))
  1037. (org-export-as (org-test-default-backend)))))))
  1038. ;;; Affiliated Keywords
  1039. (ert-deftest test-org-export/read-attribute ()
  1040. "Test `org-export-read-attribute' specifications."
  1041. ;; Standard test.
  1042. (should
  1043. (equal
  1044. (org-export-read-attribute
  1045. :attr_html
  1046. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1047. (org-element-at-point)))
  1048. '(:a "1" :b "2")))
  1049. ;; Return nil on empty attribute.
  1050. (should-not
  1051. (org-export-read-attribute
  1052. :attr_html
  1053. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1054. ;; Return nil on "nil" string.
  1055. (should
  1056. (equal '(:a nil :b nil)
  1057. (org-export-read-attribute
  1058. :attr_html
  1059. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1060. (org-element-at-point)))))
  1061. ;; Return nil on empty string.
  1062. (should
  1063. (equal '(:a nil :b nil)
  1064. (org-export-read-attribute
  1065. :attr_html
  1066. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1067. (org-element-at-point)))))
  1068. ;; Return empty string when value is "".
  1069. (should
  1070. (equal '(:a "")
  1071. (org-export-read-attribute
  1072. :attr_html
  1073. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1074. (org-element-at-point)))))
  1075. ;; Return \"\" when value is """".
  1076. (should
  1077. (equal '(:a "\"\"")
  1078. (org-export-read-attribute
  1079. :attr_html
  1080. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1081. (org-element-at-point)))))
  1082. ;; Ignore text before first property.
  1083. (should-not
  1084. (member "ignore"
  1085. (org-export-read-attribute
  1086. :attr_html
  1087. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1088. (org-element-at-point))))))
  1089. (ert-deftest test-org-export/get-caption ()
  1090. "Test `org-export-get-caption' specifications."
  1091. ;; Without optional argument, return long caption
  1092. (should
  1093. (equal
  1094. '("l")
  1095. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1096. (org-export-get-caption (org-element-at-point)))))
  1097. ;; With optional argument, return short caption.
  1098. (should
  1099. (equal
  1100. '("s")
  1101. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1102. (org-export-get-caption (org-element-at-point) t))))
  1103. ;; Multiple lines are separated by white spaces.
  1104. (should
  1105. (equal
  1106. '("a" " " "b")
  1107. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1108. (org-export-get-caption (org-element-at-point))))))
  1109. ;;; Back-End Tools
  1110. (ert-deftest test-org-export/define-backend ()
  1111. "Test back-end definition and accessors."
  1112. ;; Translate table.
  1113. (should
  1114. (equal '((headline . my-headline-test))
  1115. (let (org-export--registered-backends)
  1116. (org-export-define-backend 'test '((headline . my-headline-test)))
  1117. (org-export-get-all-transcoders 'test))))
  1118. ;; Filters.
  1119. (should
  1120. (equal '((:filter-headline . my-filter))
  1121. (let (org-export--registered-backends)
  1122. (org-export-define-backend 'test
  1123. '((headline . my-headline-test))
  1124. :filters-alist '((:filter-headline . my-filter)))
  1125. (org-export-backend-filters (org-export-get-backend 'test)))))
  1126. ;; Options.
  1127. (should
  1128. (equal '((:prop value))
  1129. (let (org-export--registered-backends)
  1130. (org-export-define-backend 'test
  1131. '((headline . my-headline-test))
  1132. :options-alist '((:prop value)))
  1133. (org-export-backend-options (org-export-get-backend 'test)))))
  1134. ;; Menu.
  1135. (should
  1136. (equal '(?k "Test Export" test)
  1137. (let (org-export--registered-backends)
  1138. (org-export-define-backend 'test
  1139. '((headline . my-headline-test))
  1140. :menu-entry '(?k "Test Export" test))
  1141. (org-export-backend-menu (org-export-get-backend 'test)))))
  1142. ;; Export Blocks.
  1143. (should
  1144. (equal '(("TEST" . org-element-export-block-parser))
  1145. (let (org-export--registered-backends org-element-block-name-alist)
  1146. (org-export-define-backend 'test
  1147. '((headline . my-headline-test))
  1148. :export-block '("test"))
  1149. org-element-block-name-alist))))
  1150. (ert-deftest test-org-export/define-derived-backend ()
  1151. "Test `org-export-define-derived-backend' specifications."
  1152. ;; Error when parent back-end is not defined.
  1153. (should-error
  1154. (let (org-export--registered-backends)
  1155. (org-export-define-derived-backend 'test 'parent)))
  1156. ;; Append translation table to parent's.
  1157. (should
  1158. (equal '((:headline . test) (:headline . parent))
  1159. (let (org-export--registered-backends)
  1160. (org-export-define-backend 'parent '((:headline . parent)))
  1161. (org-export-define-derived-backend 'test 'parent
  1162. :translate-alist '((:headline . test)))
  1163. (org-export-get-all-transcoders 'test))))
  1164. ;; Options defined in the new back have priority over those defined
  1165. ;; in parent.
  1166. (should
  1167. (eq 'test
  1168. (let (org-export--registered-backends)
  1169. (org-export-define-backend 'parent
  1170. '((:headline . parent))
  1171. :options-alist '((:a nil nil 'parent)))
  1172. (org-export-define-derived-backend 'test 'parent
  1173. :options-alist '((:a nil nil 'test)))
  1174. (plist-get (org-export--get-global-options
  1175. (org-export-get-backend 'test))
  1176. :a)))))
  1177. (ert-deftest test-org-export/derived-backend-p ()
  1178. "Test `org-export-derived-backend-p' specifications."
  1179. ;; Non-nil with direct match.
  1180. (should
  1181. (let (org-export--registered-backends)
  1182. (org-export-define-backend 'test '((headline . test)))
  1183. (org-export-derived-backend-p 'test 'test)))
  1184. (should
  1185. (let (org-export--registered-backends)
  1186. (org-export-define-backend 'test '((headline . test)))
  1187. (org-export-define-derived-backend 'test2 'test)
  1188. (org-export-derived-backend-p 'test2 'test2)))
  1189. ;; Non-nil with a direct parent.
  1190. (should
  1191. (let (org-export--registered-backends)
  1192. (org-export-define-backend 'test '((headline . test)))
  1193. (org-export-define-derived-backend 'test2 'test)
  1194. (org-export-derived-backend-p 'test2 'test)))
  1195. ;; Non-nil with an indirect parent.
  1196. (should
  1197. (let (org-export--registered-backends)
  1198. (org-export-define-backend 'test '((headline . test)))
  1199. (org-export-define-derived-backend 'test2 'test)
  1200. (org-export-define-derived-backend 'test3 'test2)
  1201. (org-export-derived-backend-p 'test3 'test)))
  1202. ;; Nil otherwise.
  1203. (should-not
  1204. (let (org-export--registered-backends)
  1205. (org-export-define-backend 'test '((headline . test)))
  1206. (org-export-define-backend 'test2 '((headline . test2)))
  1207. (org-export-derived-backend-p 'test2 'test)))
  1208. (should-not
  1209. (let (org-export--registered-backends)
  1210. (org-export-define-backend 'test '((headline . test)))
  1211. (org-export-define-backend 'test2 '((headline . test2)))
  1212. (org-export-define-derived-backend 'test3 'test2)
  1213. (org-export-derived-backend-p 'test3 'test))))
  1214. (ert-deftest test-org-export/get-all-transcoders ()
  1215. "Test `org-export-get-all-transcoders' specifications."
  1216. ;; Return nil when back-end cannot be found.
  1217. (should-not (org-export-get-all-transcoders nil))
  1218. ;; Same as `org-export-transcoders' if no parent.
  1219. (should
  1220. (equal '((headline . ignore))
  1221. (org-export-get-all-transcoders
  1222. (org-export-create-backend
  1223. :transcoders '((headline . ignore))))))
  1224. ;; But inherit from all ancestors whenever possible.
  1225. (should
  1226. (equal '((section . ignore) (headline . ignore))
  1227. (let (org-export--registered-backends)
  1228. (org-export-define-backend 'b1 '((headline . ignore)))
  1229. (org-export-get-all-transcoders
  1230. (org-export-create-backend
  1231. :parent 'b1 :transcoders '((section . ignore)))))))
  1232. (should
  1233. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1234. (let (org-export--registered-backends)
  1235. (org-export-define-backend 'b1 '((headline . ignore)))
  1236. (org-export-define-derived-backend 'b2 'b1
  1237. :translate-alist '((section . ignore)))
  1238. (org-export-get-all-transcoders
  1239. (org-export-create-backend
  1240. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1241. ;; Back-end transcoders overrule inherited ones.
  1242. (should
  1243. (eq 'b
  1244. (let (org-export--registered-backends)
  1245. (org-export-define-backend 'b1 '((headline . a)))
  1246. (cdr (assq 'headline
  1247. (org-export-get-all-transcoders
  1248. (org-export-create-backend
  1249. :parent 'b1 :transcoders '((headline . b))))))))))
  1250. (ert-deftest test-org-export/get-all-options ()
  1251. "Test `org-export-get-all-options' specifications."
  1252. ;; Return nil when back-end cannot be found.
  1253. (should-not (org-export-get-all-options nil))
  1254. ;; Same as `org-export-options' if no parent.
  1255. (should
  1256. (equal '((headline . ignore))
  1257. (org-export-get-all-options
  1258. (org-export-create-backend
  1259. :options '((headline . ignore))))))
  1260. ;; But inherit from all ancestors whenever possible.
  1261. (should
  1262. (equal '((:key2 value2) (:key1 value1))
  1263. (let (org-export--registered-backends)
  1264. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1265. (org-export-get-all-options
  1266. (org-export-create-backend
  1267. :parent 'b1 :options '((:key2 value2)))))))
  1268. (should
  1269. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1270. (let (org-export--registered-backends)
  1271. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1272. (org-export-define-derived-backend 'b2 'b1
  1273. :options-alist '((:key2 value2)))
  1274. (org-export-get-all-options
  1275. (org-export-create-backend
  1276. :parent 'b2 :options '((:key3 value3)))))))
  1277. ;; Back-end options overrule inherited ones.
  1278. (should
  1279. (eq 'b
  1280. (let (org-export--registered-backends)
  1281. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1282. (cdr (assq :key1
  1283. (org-export-get-all-options
  1284. (org-export-create-backend
  1285. :parent 'b1 :options '((:key1 . b))))))))))
  1286. (ert-deftest test-org-export/get-all-filters ()
  1287. "Test `org-export-get-all-filters' specifications."
  1288. ;; Return nil when back-end cannot be found.
  1289. (should-not (org-export-get-all-filters nil))
  1290. ;; Same as `org-export-filters' if no parent.
  1291. (should
  1292. (equal '((:filter-headline . ignore))
  1293. (org-export-get-all-filters
  1294. (org-export-create-backend
  1295. :filters '((:filter-headline . ignore))))))
  1296. ;; But inherit from all ancestors whenever possible.
  1297. (should
  1298. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1299. (let (org-export--registered-backends)
  1300. (org-export-define-backend 'b1
  1301. nil :filters-alist '((:filter-headline . ignore)))
  1302. (org-export-get-all-filters
  1303. (org-export-create-backend
  1304. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1305. (should
  1306. (equal '((:filter-paragraph . ignore)
  1307. (:filter-section . ignore)
  1308. (:filter-headline . ignore))
  1309. (let (org-export--registered-backends)
  1310. (org-export-define-backend 'b1
  1311. nil :filters-alist '((:filter-headline . ignore)))
  1312. (org-export-define-derived-backend 'b2 'b1
  1313. :filters-alist '((:filter-section . ignore)))
  1314. (org-export-get-all-filters
  1315. (org-export-create-backend
  1316. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1317. ;; Back-end filters overrule inherited ones.
  1318. (should
  1319. (eq 'b
  1320. (let (org-export--registered-backends)
  1321. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1322. (cdr (assq :filter-headline
  1323. (org-export-get-all-filters
  1324. (org-export-create-backend
  1325. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1326. (ert-deftest test-org-export/with-backend ()
  1327. "Test `org-export-with-backend' definition."
  1328. ;; Error when calling an undefined back-end
  1329. (should-error (org-export-with-backend nil "Test"))
  1330. ;; Error when called back-end doesn't have an appropriate
  1331. ;; transcoder.
  1332. (should-error
  1333. (org-export-with-backend
  1334. (org-export-create-backend :transcoders '((headline . ignore)))
  1335. "Test"))
  1336. ;; Otherwise, export using correct transcoder
  1337. (should
  1338. (equal "Success"
  1339. (let (org-export--registered-backends)
  1340. (org-export-define-backend 'test
  1341. '((plain-text . (lambda (text contents info) "Failure"))))
  1342. (org-export-define-backend 'test2
  1343. '((plain-text . (lambda (text contents info) "Success"))))
  1344. (org-export-with-backend 'test2 "Test"))))
  1345. ;; Provide correct back-end if transcoder needs to use recursive
  1346. ;; calls anyway.
  1347. (should
  1348. (equal "Success\n"
  1349. (let ((test-back-end
  1350. (org-export-create-backend
  1351. :transcoders
  1352. '((headline . (lambda (headline contents info)
  1353. (org-export-data
  1354. (org-element-property :title headline)
  1355. info)))
  1356. (plain-text . (lambda (text info) "Success"))))))
  1357. (org-export-string-as
  1358. "* Test"
  1359. (org-export-create-backend
  1360. :transcoders
  1361. '((headline . (lambda (headline contents info)
  1362. (org-export-with-backend
  1363. test-back-end headline contents info))))))))))
  1364. (ert-deftest test-org-export/data-with-backend ()
  1365. "Test `org-export-data-with-backend' specifications."
  1366. ;; Error when calling an undefined back-end.
  1367. (should-error (org-export-data-with-backend nil "nil" nil))
  1368. ;; Otherwise, export data recursively, using correct back-end.
  1369. (should
  1370. (equal
  1371. "Success!"
  1372. (org-export-data-with-backend
  1373. '(bold nil "Test")
  1374. (org-export-create-backend
  1375. :transcoders
  1376. '((plain-text . (lambda (text info) "Success"))
  1377. (bold . (lambda (bold contents info) (concat contents "!")))))
  1378. '(:with-emphasize t)))))
  1379. ;;; Export Snippets
  1380. (ert-deftest test-org-export/export-snippet ()
  1381. "Test export snippets transcoding."
  1382. ;; Standard test.
  1383. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1384. (let ((backend (org-test-default-backend)))
  1385. (setf (org-export-backend-name backend) 'test)
  1386. (setf (org-export-backend-transcoders backend)
  1387. (cons (cons 'export-snippet
  1388. (lambda (snippet contents info)
  1389. (when (eq (org-export-snippet-backend snippet) 'test)
  1390. (org-element-property :value snippet))))
  1391. (org-export-backend-transcoders backend)))
  1392. (let ((org-export-snippet-translation-alist nil))
  1393. (should (equal (org-export-as backend) "A\n")))
  1394. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1395. (should (equal (org-export-as backend) "AB\n")))))
  1396. ;; Ignored export snippets do not remove any blank.
  1397. (should
  1398. (equal "begin end\n"
  1399. (org-test-with-parsed-data "begin @@test:A@@ end"
  1400. (org-export-data-with-backend
  1401. tree
  1402. (org-export-create-backend
  1403. :transcoders
  1404. '((paragraph . (lambda (paragraph contents info) contents))
  1405. (section . (lambda (section contents info) contents))))
  1406. info)))))
  1407. ;;; Footnotes
  1408. (ert-deftest test-org-export/footnotes ()
  1409. "Test footnotes specifications."
  1410. (let ((org-footnote-section nil)
  1411. (org-export-with-footnotes t))
  1412. ;; 1. Read every type of footnote.
  1413. (should
  1414. (equal
  1415. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  1416. (org-test-with-parsed-data
  1417. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  1418. (org-element-map tree 'footnote-reference
  1419. (lambda (ref)
  1420. (let ((def (org-export-get-footnote-definition ref info)))
  1421. (cons (org-export-get-footnote-number ref info)
  1422. (if (eq (org-element-property :type ref) 'inline) (car def)
  1423. (car (org-element-contents
  1424. (car (org-element-contents def))))))))
  1425. info))))
  1426. ;; 2. Test nested footnotes order.
  1427. (should
  1428. (equal
  1429. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  1430. (org-test-with-parsed-data
  1431. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1432. (org-element-map tree 'footnote-reference
  1433. (lambda (ref)
  1434. (when (org-export-footnote-first-reference-p ref info)
  1435. (cons (org-export-get-footnote-number ref info)
  1436. (org-element-property :label ref))))
  1437. info))))
  1438. ;; 3. Test nested footnote in invisible definitions.
  1439. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  1440. ;; Hide definitions.
  1441. (narrow-to-region (point) (point-at-eol))
  1442. (let* ((tree (org-element-parse-buffer))
  1443. (info (org-combine-plists
  1444. `(:parse-tree ,tree)
  1445. (org-export-collect-tree-properties
  1446. tree (org-export-get-environment)))))
  1447. ;; Both footnotes should be seen.
  1448. (should
  1449. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  1450. ;; 4. Test footnotes definitions collection.
  1451. (should
  1452. (= 4
  1453. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1454. \[fn:2] B [fn:3] [fn::D].
  1455. \[fn:3] C."
  1456. (length (org-export-collect-footnote-definitions tree info)))))
  1457. ;; 5. Test export of footnotes defined outside parsing scope.
  1458. (should
  1459. (equal
  1460. "ParagraphOut of scope\n"
  1461. (org-test-with-temp-text "[fn:1] Out of scope
  1462. * Title
  1463. Paragraph[fn:1]"
  1464. (let ((backend (org-test-default-backend)))
  1465. (setf (org-export-backend-transcoders backend)
  1466. (cons (cons 'footnote-reference
  1467. (lambda (fn contents info)
  1468. (org-element-interpret-data
  1469. (org-export-get-footnote-definition fn info))))
  1470. (org-export-backend-transcoders backend)))
  1471. (forward-line)
  1472. (org-export-as backend 'subtree)))))
  1473. ;; 6. Footnotes without a definition should be provided a fallback
  1474. ;; definition.
  1475. (should
  1476. (org-test-with-parsed-data "[fn:1]"
  1477. (org-export-get-footnote-definition
  1478. (org-element-map tree 'footnote-reference 'identity info t) info)))
  1479. ;; 7. Footnote section should be ignored in TOC and in headlines
  1480. ;; numbering.
  1481. (should
  1482. (= 1 (let ((org-footnote-section "Footnotes"))
  1483. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  1484. (org-export-collect-headlines info))))))
  1485. (should
  1486. (equal '(2)
  1487. (let ((org-footnote-section "Footnotes"))
  1488. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  1489. (org-element-map tree 'headline
  1490. (lambda (hl)
  1491. (when (equal (org-element-property :raw-value hl) "H2")
  1492. (org-export-get-headline-number hl info)))
  1493. info t)))))))
  1494. ;;; Headlines and Inlinetasks
  1495. (ert-deftest test-org-export/get-relative-level ()
  1496. "Test `org-export-get-relative-level' specifications."
  1497. ;; Standard test.
  1498. (should
  1499. (equal '(1 2)
  1500. (let ((org-odd-levels-only nil))
  1501. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1502. (org-element-map tree 'headline
  1503. (lambda (h) (org-export-get-relative-level h info))
  1504. info)))))
  1505. ;; Missing levels
  1506. (should
  1507. (equal '(1 3)
  1508. (let ((org-odd-levels-only nil))
  1509. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  1510. (org-element-map tree 'headline
  1511. (lambda (h) (org-export-get-relative-level h info))
  1512. info))))))
  1513. (ert-deftest test-org-export/low-level-p ()
  1514. "Test `org-export-low-level-p' specifications."
  1515. (should
  1516. (equal
  1517. '(no yes)
  1518. (let ((org-odd-levels-only nil))
  1519. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1520. (org-element-map tree 'headline
  1521. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  1522. (plist-put info :headline-levels 1)))))))
  1523. (ert-deftest test-org-export/get-headline-number ()
  1524. "Test `org-export-get-headline-number' specifications."
  1525. ;; Standard test.
  1526. (should
  1527. (equal
  1528. '((1) (1 1))
  1529. (let ((org-odd-levels-only nil))
  1530. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1531. (org-element-map tree 'headline
  1532. (lambda (h) (org-export-get-headline-number h info))
  1533. info)))))
  1534. ;; Missing levels are replaced with 0.
  1535. (should
  1536. (equal
  1537. '((1) (1 0 1))
  1538. (let ((org-odd-levels-only nil))
  1539. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  1540. (org-element-map tree 'headline
  1541. (lambda (h) (org-export-get-headline-number h info))
  1542. info))))))
  1543. (ert-deftest test-org-export/numbered-headline-p ()
  1544. "Test `org-export-numbered-headline-p' specifications."
  1545. ;; If `:section-numbers' is nil, never number headlines.
  1546. (should-not
  1547. (org-test-with-parsed-data "* Headline"
  1548. (org-element-map tree 'headline
  1549. (lambda (h) (org-export-numbered-headline-p h info))
  1550. (plist-put info :section-numbers nil))))
  1551. ;; If `:section-numbers' is a number, only number headlines with
  1552. ;; a level greater that it.
  1553. (should
  1554. (equal
  1555. '(yes no)
  1556. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1557. (org-element-map tree 'headline
  1558. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  1559. (plist-put info :section-numbers 1)))))
  1560. ;; Otherwise, headlines are always numbered.
  1561. (should
  1562. (org-test-with-parsed-data "* Headline"
  1563. (org-element-map tree 'headline
  1564. (lambda (h) (org-export-numbered-headline-p h info))
  1565. (plist-put info :section-numbers t)))))
  1566. (ert-deftest test-org-export/number-to-roman ()
  1567. "Test `org-export-number-to-roman' specifications."
  1568. ;; If number is negative, return it as a string.
  1569. (should (equal (org-export-number-to-roman -1) "-1"))
  1570. ;; Otherwise, return it as a roman number.
  1571. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  1572. (ert-deftest test-org-export/get-optional-title ()
  1573. "Test `org-export-get-alt-title' specifications."
  1574. ;; If ALT_TITLE property is defined, use it.
  1575. (should
  1576. (equal '("opt")
  1577. (org-test-with-parsed-data
  1578. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1579. (org-export-get-alt-title
  1580. (org-element-map tree 'headline 'identity info t)
  1581. info))))
  1582. ;; Otherwise, fall-back to regular title.
  1583. (should
  1584. (equal '("Headline")
  1585. (org-test-with-parsed-data "* Headline"
  1586. (org-export-get-alt-title
  1587. (org-element-map tree 'headline 'identity info t)
  1588. info)))))
  1589. (ert-deftest test-org-export/get-tags ()
  1590. "Test `org-export-get-tags' specifications."
  1591. (let ((org-export-exclude-tags '("noexport"))
  1592. (org-export-select-tags '("export")))
  1593. ;; Standard test: tags which are not a select tag, an exclude tag,
  1594. ;; or specified as optional argument shouldn't be ignored.
  1595. (should
  1596. (org-test-with-parsed-data "* Headline :tag:"
  1597. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1598. info)))
  1599. ;; Exclude tags are removed.
  1600. (should-not
  1601. (org-test-with-parsed-data "* Headline :noexport:"
  1602. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1603. info)))
  1604. ;; Select tags are removed.
  1605. (should-not
  1606. (org-test-with-parsed-data "* Headline :export:"
  1607. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1608. info)))
  1609. (should
  1610. (equal
  1611. '("tag")
  1612. (org-test-with-parsed-data "* Headline :tag:export:"
  1613. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1614. info))))
  1615. ;; Tags provided in the optional argument are also ignored.
  1616. (should-not
  1617. (org-test-with-parsed-data "* Headline :ignore:"
  1618. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1619. info '("ignore"))))
  1620. ;; Allow tag inheritance.
  1621. (should
  1622. (equal
  1623. '(("tag") ("tag"))
  1624. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1625. (org-element-map tree 'headline
  1626. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1627. ;; Tag inheritance checks FILETAGS keywords.
  1628. (should
  1629. (equal
  1630. '(("a" "b" "tag"))
  1631. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1632. (org-element-map tree 'headline
  1633. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1634. (ert-deftest test-org-export/get-node-property ()
  1635. "Test`org-export-get-node-property' specifications."
  1636. ;; Standard test.
  1637. (should
  1638. (equal "value"
  1639. (org-test-with-parsed-data "* Headline
  1640. :PROPERTIES:
  1641. :prop: value
  1642. :END:"
  1643. (org-export-get-node-property
  1644. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1645. ;; Test inheritance.
  1646. (should
  1647. (equal "value"
  1648. (org-test-with-parsed-data "* Parent
  1649. :PROPERTIES:
  1650. :prop: value
  1651. :END:
  1652. ** Headline
  1653. Paragraph"
  1654. (org-export-get-node-property
  1655. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1656. ;; Cannot return a value before the first headline.
  1657. (should-not
  1658. (org-test-with-parsed-data "Paragraph
  1659. * Headline
  1660. :PROPERTIES:
  1661. :prop: value
  1662. :END:"
  1663. (org-export-get-node-property
  1664. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1665. (ert-deftest test-org-export/get-category ()
  1666. "Test `org-export-get-category' specifications."
  1667. ;; Standard test.
  1668. (should
  1669. (equal "value"
  1670. (org-test-with-parsed-data "* Headline
  1671. :PROPERTIES:
  1672. :CATEGORY: value
  1673. :END:"
  1674. (org-export-get-category
  1675. (org-element-map tree 'headline 'identity nil t) info))))
  1676. ;; Test inheritance from a parent headline.
  1677. (should
  1678. (equal '("value" "value")
  1679. (org-test-with-parsed-data "* Headline1
  1680. :PROPERTIES:
  1681. :CATEGORY: value
  1682. :END:
  1683. ** Headline2"
  1684. (org-element-map tree 'headline
  1685. (lambda (hl) (org-export-get-category hl info)) info))))
  1686. ;; Test inheritance from #+CATEGORY keyword
  1687. (should
  1688. (equal "value"
  1689. (org-test-with-parsed-data "#+CATEGORY: value
  1690. * Headline"
  1691. (org-export-get-category
  1692. (org-element-map tree 'headline 'identity nil t) info))))
  1693. ;; Test inheritance from file name.
  1694. (should
  1695. (equal "test"
  1696. (org-test-with-parsed-data "* Headline"
  1697. (let ((info (plist-put info :input-file "~/test.org")))
  1698. (org-export-get-category
  1699. (org-element-map tree 'headline 'identity nil t) info)))))
  1700. ;; Fall-back value.
  1701. (should
  1702. (equal "???"
  1703. (org-test-with-parsed-data "* Headline"
  1704. (org-export-get-category
  1705. (org-element-map tree 'headline 'identity nil t) info)))))
  1706. (ert-deftest test-org-export/first-sibling-p ()
  1707. "Test `org-export-first-sibling-p' specifications."
  1708. ;; Standard test.
  1709. (should
  1710. (equal
  1711. '(yes yes no)
  1712. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  1713. (org-element-map tree 'headline
  1714. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1715. info))))
  1716. (should
  1717. (equal '(yes no)
  1718. (org-test-with-parsed-data "- item\n\n para"
  1719. (org-element-map tree 'paragraph
  1720. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1721. info))))
  1722. ;; Ignore sections for headlines.
  1723. (should
  1724. (equal '(yes yes)
  1725. (org-test-with-parsed-data "* H\nSection\n** H 2"
  1726. (org-element-map tree 'headline
  1727. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1728. info))))
  1729. ;; Ignore headlines not exported.
  1730. (should
  1731. (equal
  1732. '(yes)
  1733. (let ((org-export-exclude-tags '("ignore")))
  1734. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1735. (org-element-map tree 'headline
  1736. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1737. info))))))
  1738. (ert-deftest test-org-export/last-sibling-p ()
  1739. "Test `org-export-last-sibling-p' specifications."
  1740. ;; Standard test.
  1741. (should
  1742. (equal
  1743. '(yes no yes)
  1744. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1745. (org-element-map tree 'headline
  1746. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1747. info))))
  1748. (should
  1749. (equal '(no yes)
  1750. (org-test-with-parsed-data "- item\n\n para"
  1751. (org-element-map tree 'paragraph
  1752. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1753. info))))
  1754. ;; Ignore headlines not exported.
  1755. (should
  1756. (equal
  1757. '(yes)
  1758. (let ((org-export-exclude-tags '("ignore")))
  1759. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1760. (org-element-map tree 'headline
  1761. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1762. info))))))
  1763. (ert-deftest test-org-export/handle-inlinetasks ()
  1764. "Test inlinetask export."
  1765. ;; Inlinetask with an exclude tag.
  1766. (when (featurep 'org-inlinetask)
  1767. (should
  1768. (equal
  1769. ""
  1770. (let ((org-inlinetask-min-level 3))
  1771. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  1772. (org-export-as (org-test-default-backend)
  1773. nil nil nil '(:exclude-tags ("noexp")))))))
  1774. ;; Inlinetask with an include tag.
  1775. (should
  1776. (equal
  1777. "* H2\n*** Inline :exp:\n"
  1778. (let ((org-inlinetask-min-level 3)
  1779. (org-tags-column 0))
  1780. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  1781. (org-export-as (org-test-default-backend)
  1782. nil nil nil '(:select-tags ("exp")))))))
  1783. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  1784. (should
  1785. (equal ""
  1786. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  1787. (org-inlinetask-min-level 3))
  1788. (org-test-with-temp-text "*** TODO Inline"
  1789. (org-export-as (org-test-default-backend)
  1790. nil nil nil '(:with-tasks nil))))))))
  1791. ;;; Keywords
  1792. (ert-deftest test-org-export/get-date ()
  1793. "Test `org-export-get-date' specifications."
  1794. ;; Return a properly formatted string when
  1795. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  1796. ;; consists in a single timestamp.
  1797. (should
  1798. (equal "29 03 2012"
  1799. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1800. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1801. (org-export-get-date info)))))
  1802. ;; Return a secondary string otherwise.
  1803. (should-not
  1804. (stringp
  1805. (let ((org-export-date-timestamp-format nil))
  1806. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1807. (org-export-get-date info)))))
  1808. (should
  1809. (equal '("Date")
  1810. (org-test-with-parsed-data "#+DATE: Date"
  1811. (org-export-get-date info))))
  1812. ;; Optional argument has precedence over
  1813. ;; `org-export-date-timestamp-format'.
  1814. (should
  1815. (equal "29 03"
  1816. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1817. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1818. (org-export-get-date info "%d %m"))))))
  1819. ;;; Links
  1820. (ert-deftest test-org-export/get-coderef-format ()
  1821. "Test `org-export-get-coderef-format' specifications."
  1822. ;; A link without description returns "%s"
  1823. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1824. "%s"))
  1825. ;; Return "%s" when path is matched within description.
  1826. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1827. "desc %s"))
  1828. ;; Otherwise return description.
  1829. (should (equal (org-export-get-coderef-format "path" "desc")
  1830. "desc")))
  1831. (ert-deftest test-org-export/inline-image-p ()
  1832. "Test `org-export-inline-image-p' specifications."
  1833. (should
  1834. (org-export-inline-image-p
  1835. (org-test-with-temp-text "[[#id]]"
  1836. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  1837. '(("custom-id" . "id")))))
  1838. (ert-deftest test-org-export/fuzzy-link ()
  1839. "Test fuzzy links specifications."
  1840. ;; Link to an headline should return headline's number.
  1841. (org-test-with-parsed-data
  1842. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1843. (should
  1844. ;; Note: Headline's number is in fact a list of numbers.
  1845. (equal '(2)
  1846. (org-element-map tree 'link
  1847. (lambda (link)
  1848. (org-export-get-ordinal
  1849. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1850. ;; Link to a target in an item should return item's number.
  1851. (org-test-with-parsed-data
  1852. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1853. (should
  1854. ;; Note: Item's number is in fact a list of numbers.
  1855. (equal '(1 2)
  1856. (org-element-map tree 'link
  1857. (lambda (link)
  1858. (org-export-get-ordinal
  1859. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1860. ;; Link to a target in a footnote should return footnote's number.
  1861. (org-test-with-parsed-data "
  1862. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1863. (should
  1864. (equal '(2 3)
  1865. (org-element-map tree 'link
  1866. (lambda (link)
  1867. (org-export-get-ordinal
  1868. (org-export-resolve-fuzzy-link link info) info)) info))))
  1869. ;; Link to a named element should return sequence number of that
  1870. ;; element.
  1871. (org-test-with-parsed-data
  1872. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1873. (should
  1874. (= 2
  1875. (org-element-map tree 'link
  1876. (lambda (link)
  1877. (org-export-get-ordinal
  1878. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1879. ;; Link to a target not within an item, a table, a footnote
  1880. ;; reference or definition should return section number.
  1881. (org-test-with-parsed-data
  1882. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1883. (should
  1884. (equal '(2)
  1885. (org-element-map tree 'link
  1886. (lambda (link)
  1887. (org-export-get-ordinal
  1888. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1889. ;; Space are not significant when matching a fuzzy link.
  1890. (should
  1891. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  1892. (org-element-map tree 'link
  1893. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1894. info t)))
  1895. ;; Statistics cookies are ignored for headline match.
  1896. (should
  1897. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  1898. (org-element-map tree 'link
  1899. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1900. info t)))
  1901. (should
  1902. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  1903. (org-element-map tree 'link
  1904. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1905. info t)))
  1906. ;; Headline match is position dependent.
  1907. (should-not
  1908. (apply
  1909. 'eq
  1910. (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
  1911. (org-element-map tree 'link
  1912. (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
  1913. (ert-deftest test-org-export/resolve-coderef ()
  1914. "Test `org-export-resolve-coderef' specifications."
  1915. (let ((org-coderef-label-format "(ref:%s)"))
  1916. ;; 1. A link to a "-n -k -r" block returns line number.
  1917. (org-test-with-parsed-data
  1918. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1919. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1920. (org-test-with-parsed-data
  1921. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1922. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1923. ;; 2. A link to a "-n -r" block returns line number.
  1924. (org-test-with-parsed-data
  1925. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1926. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1927. (org-test-with-parsed-data
  1928. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1929. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1930. ;; 3. A link to a "-n" block returns coderef.
  1931. (org-test-with-parsed-data
  1932. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1933. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1934. (org-test-with-parsed-data
  1935. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1936. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1937. ;; 4. A link to a "-r" block returns line number.
  1938. (org-test-with-parsed-data
  1939. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1940. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1941. (org-test-with-parsed-data
  1942. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1943. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1944. ;; 5. A link to a block without a switch returns coderef.
  1945. (org-test-with-parsed-data
  1946. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1947. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1948. (org-test-with-parsed-data
  1949. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1950. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1951. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1952. ;; should resume numbering from previous block with numbered
  1953. ;; lines, ignoring blocks not numbering lines in the process.
  1954. ;; A "-n" switch resets count.
  1955. (org-test-with-parsed-data "
  1956. #+BEGIN_EXAMPLE -n
  1957. Text.
  1958. #+END_EXAMPLE
  1959. #+BEGIN_SRC emacs-lisp
  1960. \(- 1 1)
  1961. #+END_SRC
  1962. #+BEGIN_SRC emacs-lisp +n -r
  1963. \(+ 1 1) (ref:addition)
  1964. #+END_SRC
  1965. #+BEGIN_EXAMPLE -n -r
  1966. Another text. (ref:text)
  1967. #+END_EXAMPLE"
  1968. (should (= (org-export-resolve-coderef "addition" info) 2))
  1969. (should (= (org-export-resolve-coderef "text" info) 1)))
  1970. ;; 7. Recognize coderef with user-specified syntax.
  1971. (org-test-with-parsed-data
  1972. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1973. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1974. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1975. "Test `org-export-resolve-fuzzy-link' specifications."
  1976. ;; Match target objects.
  1977. (should
  1978. (org-test-with-parsed-data "<<target>> [[target]]"
  1979. (org-export-resolve-fuzzy-link
  1980. (org-element-map tree 'link 'identity info t) info)))
  1981. ;; Match named elements.
  1982. (should
  1983. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1984. (org-export-resolve-fuzzy-link
  1985. (org-element-map tree 'link 'identity info t) info)))
  1986. ;; Match exact headline's name.
  1987. (should
  1988. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1989. (org-export-resolve-fuzzy-link
  1990. (org-element-map tree 'link 'identity info t) info)))
  1991. ;; Targets objects have priority over named elements and headline
  1992. ;; titles.
  1993. (should
  1994. (eq 'target
  1995. (org-test-with-parsed-data
  1996. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1997. (org-element-type
  1998. (org-export-resolve-fuzzy-link
  1999. (org-element-map tree 'link 'identity info t) info)))))
  2000. ;; Named elements have priority over headline titles.
  2001. (should
  2002. (eq 'paragraph
  2003. (org-test-with-parsed-data
  2004. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2005. (org-element-type
  2006. (org-export-resolve-fuzzy-link
  2007. (org-element-map tree 'link 'identity info t) info)))))
  2008. ;; If link's path starts with a "*", only match headline titles,
  2009. ;; though.
  2010. (should
  2011. (eq 'headline
  2012. (org-test-with-parsed-data
  2013. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2014. (org-element-type
  2015. (org-export-resolve-fuzzy-link
  2016. (org-element-map tree 'link 'identity info t) info)))))
  2017. ;; Return nil if no match.
  2018. (should-not
  2019. (org-test-with-parsed-data "[[target]]"
  2020. (org-export-resolve-fuzzy-link
  2021. (org-element-map tree 'link 'identity info t) info)))
  2022. ;; Match fuzzy link even when before first headline.
  2023. (should
  2024. (eq 'headline
  2025. (org-test-with-parsed-data "[[hl]]\n* hl"
  2026. (org-element-type
  2027. (org-export-resolve-fuzzy-link
  2028. (org-element-map tree 'link 'identity info t) info))))))
  2029. (ert-deftest test-org-export/resolve-id-link ()
  2030. "Test `org-export-resolve-id-link' specifications."
  2031. ;; 1. Regular test for custom-id link.
  2032. (org-test-with-parsed-data "* Headline1
  2033. :PROPERTIES:
  2034. :CUSTOM_ID: test
  2035. :END:
  2036. * Headline 2
  2037. \[[#test]]"
  2038. (should
  2039. (org-export-resolve-id-link
  2040. (org-element-map tree 'link 'identity info t) info)))
  2041. ;; 2. Failing test for custom-id link.
  2042. (org-test-with-parsed-data "* Headline1
  2043. :PROPERTIES:
  2044. :CUSTOM_ID: test
  2045. :END:
  2046. * Headline 2
  2047. \[[#no-match]]"
  2048. (should-not
  2049. (org-export-resolve-id-link
  2050. (org-element-map tree 'link 'identity info t) info)))
  2051. ;; 3. Test for internal id target.
  2052. (org-test-with-parsed-data "* Headline1
  2053. :PROPERTIES:
  2054. :ID: aaaa
  2055. :END:
  2056. * Headline 2
  2057. \[[id:aaaa]]"
  2058. (should
  2059. (org-export-resolve-id-link
  2060. (org-element-map tree 'link 'identity info t) info)))
  2061. ;; 4. Test for external id target.
  2062. (org-test-with-parsed-data "[[id:aaaa]]"
  2063. (should
  2064. (org-export-resolve-id-link
  2065. (org-element-map tree 'link 'identity info t)
  2066. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  2067. (ert-deftest test-org-export/resolve-radio-link ()
  2068. "Test `org-export-resolve-radio-link' specifications."
  2069. ;; Standard test.
  2070. (should
  2071. (org-test-with-temp-text "<<<radio>>> radio"
  2072. (org-update-radio-target-regexp)
  2073. (let* ((tree (org-element-parse-buffer))
  2074. (info `(:parse-tree ,tree)))
  2075. (org-export-resolve-radio-link
  2076. (org-element-map tree 'link 'identity info t)
  2077. info))))
  2078. ;; Radio targets are case-insensitive.
  2079. (should
  2080. (org-test-with-temp-text "<<<RADIO>>> radio"
  2081. (org-update-radio-target-regexp)
  2082. (let* ((tree (org-element-parse-buffer))
  2083. (info `(:parse-tree ,tree)))
  2084. (org-export-resolve-radio-link
  2085. (org-element-map tree 'link 'identity info t)
  2086. info))))
  2087. ;; Radio target with objects.
  2088. (should
  2089. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2090. (org-update-radio-target-regexp)
  2091. (let* ((tree (org-element-parse-buffer))
  2092. (info `(:parse-tree ,tree)))
  2093. (org-export-resolve-radio-link
  2094. (org-element-map tree 'link 'identity info t)
  2095. info))))
  2096. ;; Radio target with objects at its beginning.
  2097. (should
  2098. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2099. (org-update-radio-target-regexp)
  2100. (let* ((tree (org-element-parse-buffer))
  2101. (info `(:parse-tree ,tree)))
  2102. (org-export-resolve-radio-link
  2103. (org-element-map tree 'link 'identity info t)
  2104. info))))
  2105. ;; Radio link next to an apostrophe.
  2106. (should
  2107. (org-test-with-temp-text "<<<radio>>> radio's"
  2108. (org-update-radio-target-regexp)
  2109. (let* ((tree (org-element-parse-buffer))
  2110. (info `(:parse-tree ,tree)))
  2111. (org-export-resolve-radio-link
  2112. (org-element-map tree 'link 'identity info t)
  2113. info))))
  2114. ;; Multiple radio targets.
  2115. (should
  2116. (equal '("radio1" "radio2")
  2117. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  2118. (org-update-radio-target-regexp)
  2119. (let* ((tree (org-element-parse-buffer))
  2120. (info `(:parse-tree ,tree)))
  2121. (org-element-map tree 'link
  2122. (lambda (link)
  2123. (org-element-property
  2124. :value (org-export-resolve-radio-link link info)))
  2125. info)))))
  2126. ;; Radio target is whitespace insensitive.
  2127. (should
  2128. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  2129. (org-update-radio-target-regexp)
  2130. (let* ((tree (org-element-parse-buffer))
  2131. (info `(:parse-tree ,tree)))
  2132. (org-element-map tree 'link
  2133. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  2134. ;;; Src-block and example-block
  2135. (ert-deftest test-org-export/unravel-code ()
  2136. "Test `org-export-unravel-code' function."
  2137. ;; Code without reference.
  2138. (should
  2139. (equal '("(+ 1 1)\n")
  2140. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2141. (org-export-unravel-code (org-element-at-point)))))
  2142. ;; Code with reference.
  2143. (should
  2144. (equal '("(+ 1 1)\n" (1 . "test"))
  2145. (org-test-with-temp-text
  2146. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  2147. (let ((org-coderef-label-format "(ref:%s)"))
  2148. (org-export-unravel-code (org-element-at-point))))))
  2149. ;; Code with user-defined reference.
  2150. (should
  2151. (equal
  2152. '("(+ 1 1)\n" (1 . "test"))
  2153. (org-test-with-temp-text
  2154. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  2155. (let ((org-coderef-label-format "(ref:%s)"))
  2156. (org-export-unravel-code (org-element-at-point))))))
  2157. ;; Code references keys are relative to the current block.
  2158. (should
  2159. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  2160. (org-test-with-temp-text "
  2161. #+BEGIN_EXAMPLE -n
  2162. \(+ 1 1)
  2163. #+END_EXAMPLE
  2164. #+BEGIN_EXAMPLE +n
  2165. \(+ 2 2)
  2166. \(+ 3 3) (ref:one)
  2167. #+END_EXAMPLE"
  2168. (goto-line 5)
  2169. (let ((org-coderef-label-format "(ref:%s)"))
  2170. (org-export-unravel-code (org-element-at-point)))))))
  2171. (ert-deftest test-org-export/format-code-default ()
  2172. "Test `org-export-format-code-default' specifications."
  2173. ;; Return the empty string when code is empty.
  2174. (should
  2175. (equal ""
  2176. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  2177. (org-export-format-code-default
  2178. (org-element-map tree 'src-block 'identity info t) info))))
  2179. ;; Number lines, two whitespace characters before the actual loc.
  2180. (should
  2181. (equal "1 a\n2 b\n"
  2182. (org-test-with-parsed-data
  2183. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  2184. (org-export-format-code-default
  2185. (org-element-map tree 'src-block 'identity info t) info))))
  2186. ;; Put references 6 whitespace characters after the widest line,
  2187. ;; wrapped within parenthesis.
  2188. (should
  2189. (equal "123 (a)\n1 (b)\n"
  2190. (let ((org-coderef-label-format "(ref:%s)"))
  2191. (org-test-with-parsed-data
  2192. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  2193. (org-export-format-code-default
  2194. (org-element-map tree 'src-block 'identity info t) info))))))
  2195. ;;; Smart Quotes
  2196. (ert-deftest test-org-export/activate-smart-quotes ()
  2197. "Test `org-export-activate-smart-quotes' specifications."
  2198. ;; Opening double quotes: standard test.
  2199. (should
  2200. (equal
  2201. '("some &ldquo;paragraph")
  2202. (let ((org-export-default-language "en"))
  2203. (org-test-with-parsed-data "some \"paragraph"
  2204. (org-element-map tree 'plain-text
  2205. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2206. info)))))
  2207. ;; Opening quotes: at the beginning of a paragraph.
  2208. (should
  2209. (equal
  2210. '("&ldquo;begin")
  2211. (let ((org-export-default-language "en"))
  2212. (org-test-with-parsed-data "\"begin"
  2213. (org-element-map tree 'plain-text
  2214. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2215. info)))))
  2216. ;; Opening quotes: after an object.
  2217. (should
  2218. (equal
  2219. '("&ldquo;begin")
  2220. (let ((org-export-default-language "en"))
  2221. (org-test-with-parsed-data "=verb= \"begin"
  2222. (org-element-map tree 'plain-text
  2223. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2224. info)))))
  2225. ;; Closing quotes: standard test.
  2226. (should
  2227. (equal
  2228. '("some&rdquo; paragraph")
  2229. (let ((org-export-default-language "en"))
  2230. (org-test-with-parsed-data "some\" paragraph"
  2231. (org-element-map tree 'plain-text
  2232. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2233. info)))))
  2234. ;; Closing quotes: at the end of a paragraph.
  2235. (should
  2236. (equal
  2237. '("end&rdquo;")
  2238. (let ((org-export-default-language "en"))
  2239. (org-test-with-parsed-data "end\""
  2240. (org-element-map tree 'plain-text
  2241. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2242. info)))))
  2243. ;; Apostrophe: standard test.
  2244. (should
  2245. (equal
  2246. '("It shouldn&rsquo;t fail")
  2247. (let ((org-export-default-language "en"))
  2248. (org-test-with-parsed-data "It shouldn't fail"
  2249. (org-element-map tree 'plain-text
  2250. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2251. info)))))
  2252. ;; Apostrophe: before an object.
  2253. (should
  2254. (equal
  2255. '("a&rsquo;")
  2256. (let ((org-export-default-language "en"))
  2257. (org-test-with-parsed-data "a'=b="
  2258. (org-element-map tree 'plain-text
  2259. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2260. info)))))
  2261. ;; Apostrophe: after an object.
  2262. (should
  2263. (equal
  2264. '("&rsquo;s")
  2265. (let ((org-export-default-language "en"))
  2266. (org-test-with-parsed-data "=code='s"
  2267. (org-element-map tree 'plain-text
  2268. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2269. info)))))
  2270. ;; Special case: isolated quotes.
  2271. (should
  2272. (equal '("&ldquo;" "&rdquo;")
  2273. (let ((org-export-default-language "en"))
  2274. (org-test-with-parsed-data "\"$x$\""
  2275. (org-element-map tree 'plain-text
  2276. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2277. info)))))
  2278. ;; Smart quotes in secondary strings.
  2279. (should
  2280. (equal '("&ldquo;" "&rdquo;")
  2281. (let ((org-export-default-language "en"))
  2282. (org-test-with-parsed-data "* \"$x$\""
  2283. (org-element-map tree 'plain-text
  2284. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2285. info)))))
  2286. ;; Smart quotes in document keywords.
  2287. (should
  2288. (equal '("&ldquo;" "&rdquo;")
  2289. (let ((org-export-default-language "en"))
  2290. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  2291. (org-element-map (plist-get info :title) 'plain-text
  2292. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2293. info)))))
  2294. ;; Smart quotes in parsed affiliated keywords.
  2295. (should
  2296. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  2297. (let ((org-export-default-language "en"))
  2298. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  2299. (org-element-map tree 'plain-text
  2300. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2301. info nil nil t))))))
  2302. ;;; Tables
  2303. (ert-deftest test-org-export/special-column ()
  2304. "Test if the table's special column is properly recognized."
  2305. ;; 1. First column is special if it contains only a special marking
  2306. ;; characters or empty cells.
  2307. (org-test-with-temp-text "
  2308. | ! | 1 |
  2309. | | 2 |"
  2310. (should
  2311. (org-export-table-has-special-column-p
  2312. (org-element-map
  2313. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2314. ;; 2. If the column contains anything else, it isn't special.
  2315. (org-test-with-temp-text "
  2316. | ! | 1 |
  2317. | b | 2 |"
  2318. (should-not
  2319. (org-export-table-has-special-column-p
  2320. (org-element-map
  2321. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2322. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  2323. ;; and "!".
  2324. (org-test-with-temp-text "
  2325. | # | 1 |
  2326. | ^ | 2 |
  2327. | * | 3 |
  2328. | _ | 4 |
  2329. | / | 5 |
  2330. | $ | 6 |
  2331. | ! | 7 |"
  2332. (should
  2333. (org-export-table-has-special-column-p
  2334. (org-element-map
  2335. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2336. ;; 4. A first column with only empty cells isn't considered as
  2337. ;; special.
  2338. (org-test-with-temp-text "
  2339. | | 1 |
  2340. | | 2 |"
  2341. (should-not
  2342. (org-export-table-has-special-column-p
  2343. (org-element-map
  2344. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  2345. (ert-deftest test-org-export/table-row-is-special-p ()
  2346. "Test `org-export-table-row-is-special-p' specifications."
  2347. ;; 1. A row is special if it has a special marking character in the
  2348. ;; special column.
  2349. (org-test-with-parsed-data "| ! | 1 |"
  2350. (should
  2351. (org-export-table-row-is-special-p
  2352. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2353. ;; 2. A row is special when its first field is "/"
  2354. (org-test-with-parsed-data "
  2355. | / | 1 |
  2356. | a | b |"
  2357. (should
  2358. (org-export-table-row-is-special-p
  2359. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2360. ;; 3. A row only containing alignment cookies is also considered as
  2361. ;; special.
  2362. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  2363. (should
  2364. (org-export-table-row-is-special-p
  2365. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2366. ;; 4. Everything else isn't considered as special.
  2367. (org-test-with-parsed-data "| \alpha | | c |"
  2368. (should-not
  2369. (org-export-table-row-is-special-p
  2370. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2371. ;; 5. Table's rules are never considered as special rows.
  2372. (org-test-with-parsed-data "|---+---|"
  2373. (should-not
  2374. (org-export-table-row-is-special-p
  2375. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  2376. (ert-deftest test-org-export/has-header-p ()
  2377. "Test `org-export-table-has-header-p' specifications."
  2378. ;; 1. With an header.
  2379. (org-test-with-parsed-data "
  2380. | a | b |
  2381. |---+---|
  2382. | c | d |"
  2383. (should
  2384. (org-export-table-has-header-p
  2385. (org-element-map tree 'table 'identity info 'first-match)
  2386. info)))
  2387. ;; 2. Without an header.
  2388. (org-test-with-parsed-data "
  2389. | a | b |
  2390. | c | d |"
  2391. (should-not
  2392. (org-export-table-has-header-p
  2393. (org-element-map tree 'table 'identity info 'first-match)
  2394. info)))
  2395. ;; 3. Don't get fooled with starting and ending rules.
  2396. (org-test-with-parsed-data "
  2397. |---+---|
  2398. | a | b |
  2399. | c | d |
  2400. |---+---|"
  2401. (should-not
  2402. (org-export-table-has-header-p
  2403. (org-element-map tree 'table 'identity info 'first-match)
  2404. info))))
  2405. (ert-deftest test-org-export/table-row-group ()
  2406. "Test `org-export-table-row-group' specifications."
  2407. ;; 1. A rule creates a new group.
  2408. (should
  2409. (equal '(1 rule 2)
  2410. (org-test-with-parsed-data "
  2411. | a | b |
  2412. |---+---|
  2413. | 1 | 2 |"
  2414. (org-element-map tree 'table-row
  2415. (lambda (row)
  2416. (if (eq (org-element-property :type row) 'rule) 'rule
  2417. (org-export-table-row-group row info)))))))
  2418. ;; 2. Special rows are ignored in count.
  2419. (should
  2420. (equal
  2421. '(rule 1)
  2422. (org-test-with-parsed-data "
  2423. | / | < | > |
  2424. |---|---+---|
  2425. | | 1 | 2 |"
  2426. (org-element-map tree 'table-row
  2427. (lambda (row)
  2428. (if (eq (org-element-property :type row) 'rule) 'rule
  2429. (org-export-table-row-group row info)))
  2430. info))))
  2431. ;; 3. Double rules also are ignored in count.
  2432. (should
  2433. (equal '(1 rule rule 2)
  2434. (org-test-with-parsed-data "
  2435. | a | b |
  2436. |---+---|
  2437. |---+---|
  2438. | 1 | 2 |"
  2439. (org-element-map tree 'table-row
  2440. (lambda (row)
  2441. (if (eq (org-element-property :type row) 'rule) 'rule
  2442. (org-export-table-row-group row info))))))))
  2443. (ert-deftest test-org-export/table-row-number ()
  2444. "Test `org-export-table-row-number' specifications."
  2445. ;; Standard test. Number is 0-indexed.
  2446. (should
  2447. (equal '(0 1)
  2448. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  2449. (org-element-map tree 'table-row
  2450. (lambda (row) (org-export-table-row-number row info)) info))))
  2451. ;; Number ignores separators.
  2452. (should
  2453. (equal '(0 1)
  2454. (org-test-with-parsed-data "
  2455. | a | b | c |
  2456. |---+---+---|
  2457. | d | e | f |"
  2458. (org-element-map tree 'table-row
  2459. (lambda (row) (org-export-table-row-number row info)) info))))
  2460. ;; Number ignores special rows.
  2461. (should
  2462. (equal '(0 1)
  2463. (org-test-with-parsed-data "
  2464. | / | < | > |
  2465. | | b | c |
  2466. |---+-----+-----|
  2467. | | <c> | <c> |
  2468. | | e | f |"
  2469. (org-element-map tree 'table-row
  2470. (lambda (row) (org-export-table-row-number row info)) info)))))
  2471. (ert-deftest test-org-export/table-cell-width ()
  2472. "Test `org-export-table-cell-width' specifications."
  2473. ;; 1. Width is primarily determined by width cookies. If no cookie
  2474. ;; is found, cell's width is nil.
  2475. (org-test-with-parsed-data "
  2476. | / | <l> | <6> | <l7> |
  2477. | | a | b | c |"
  2478. (should
  2479. (equal
  2480. '(nil 6 7)
  2481. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2482. (org-element-map tree 'table-cell 'identity info)))))
  2483. ;; 2. The last width cookie has precedence.
  2484. (org-test-with-parsed-data "
  2485. | <6> |
  2486. | <7> |
  2487. | a |"
  2488. (should
  2489. (equal
  2490. '(7)
  2491. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2492. (org-element-map tree 'table-cell 'identity info)))))
  2493. ;; 3. Valid width cookies must have a specific row.
  2494. (org-test-with-parsed-data "| <6> | cell |"
  2495. (should
  2496. (equal
  2497. '(nil nil)
  2498. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2499. (org-element-map tree 'table-cell 'identity))))))
  2500. (ert-deftest test-org-export/table-cell-alignment ()
  2501. "Test `org-export-table-cell-alignment' specifications."
  2502. ;; 1. Alignment is primarily determined by alignment cookies.
  2503. (should
  2504. (equal '(left center right)
  2505. (let ((org-table-number-fraction 0.5)
  2506. (org-table-number-regexp "^[0-9]+$"))
  2507. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  2508. (mapcar (lambda (cell)
  2509. (org-export-table-cell-alignment cell info))
  2510. (org-element-map tree 'table-cell 'identity))))))
  2511. ;; 2. The last alignment cookie has precedence.
  2512. (should
  2513. (equal '(right right right)
  2514. (org-test-with-parsed-data "
  2515. | <l8> |
  2516. | cell |
  2517. | <r9> |"
  2518. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  2519. (org-element-map tree 'table-cell 'identity)))))
  2520. ;; 3. If there's no cookie, cell's contents determine alignment.
  2521. ;; A column mostly made of cells containing numbers will align
  2522. ;; its cells to the right.
  2523. (should
  2524. (equal '(right right right)
  2525. (let ((org-table-number-fraction 0.5)
  2526. (org-table-number-regexp "^[0-9]+$"))
  2527. (org-test-with-parsed-data "
  2528. | 123 |
  2529. | some text |
  2530. | 12345 |"
  2531. (mapcar (lambda (cell)
  2532. (org-export-table-cell-alignment cell info))
  2533. (org-element-map tree 'table-cell 'identity))))))
  2534. ;; 4. Otherwise, they will be aligned to the left.
  2535. (should
  2536. (equal '(left left left)
  2537. (org-test-with-parsed-data "
  2538. | text |
  2539. | some text |
  2540. | \alpha |"
  2541. (mapcar (lambda (cell)
  2542. (org-export-table-cell-alignment cell info))
  2543. (org-element-map tree 'table-cell 'identity info))))))
  2544. (ert-deftest test-org-export/table-cell-borders ()
  2545. "Test `org-export-table-cell-borders' specifications."
  2546. ;; 1. Recognize various column groups indicators.
  2547. (org-test-with-parsed-data "| / | < | > | <> |"
  2548. (should
  2549. (equal
  2550. '((right bottom top) (left bottom top) (right bottom top)
  2551. (right left bottom top))
  2552. (mapcar (lambda (cell)
  2553. (org-export-table-cell-borders cell info))
  2554. (org-element-map tree 'table-cell 'identity)))))
  2555. ;; 2. Accept shortcuts to define column groups.
  2556. (org-test-with-parsed-data "| / | < | < |"
  2557. (should
  2558. (equal
  2559. '((right bottom top) (right left bottom top) (left bottom top))
  2560. (mapcar (lambda (cell)
  2561. (org-export-table-cell-borders cell info))
  2562. (org-element-map tree 'table-cell 'identity)))))
  2563. ;; 3. A valid column groups row must start with a "/".
  2564. (org-test-with-parsed-data "
  2565. | | < |
  2566. | a | b |"
  2567. (should
  2568. (equal '((top) (top) (bottom) (bottom))
  2569. (mapcar (lambda (cell)
  2570. (org-export-table-cell-borders cell info))
  2571. (org-element-map tree 'table-cell 'identity)))))
  2572. ;; 4. Take table rules into consideration.
  2573. (org-test-with-parsed-data "
  2574. | 1 |
  2575. |---|
  2576. | 2 |"
  2577. (should
  2578. (equal '((below top) (bottom above))
  2579. (mapcar (lambda (cell)
  2580. (org-export-table-cell-borders cell info))
  2581. (org-element-map tree 'table-cell 'identity)))))
  2582. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  2583. ;; (resp. `bottom' and `below') borders. Any special row is
  2584. ;; ignored.
  2585. (org-test-with-parsed-data "
  2586. |---+----|
  2587. | / | |
  2588. | | 1 |
  2589. |---+----|"
  2590. (should
  2591. (equal '((bottom below top above))
  2592. (last
  2593. (mapcar (lambda (cell)
  2594. (org-export-table-cell-borders cell info))
  2595. (org-element-map tree 'table-cell 'identity)))))))
  2596. (ert-deftest test-org-export/table-dimensions ()
  2597. "Test `org-export-table-dimensions' specifications."
  2598. ;; 1. Standard test.
  2599. (org-test-with-parsed-data "
  2600. | 1 | 2 | 3 |
  2601. | 4 | 5 | 6 |"
  2602. (should
  2603. (equal '(2 . 3)
  2604. (org-export-table-dimensions
  2605. (org-element-map tree 'table 'identity info 'first-match) info))))
  2606. ;; 2. Ignore horizontal rules and special columns.
  2607. (org-test-with-parsed-data "
  2608. | / | < | > |
  2609. | 1 | 2 | 3 |
  2610. |---+---+---|
  2611. | 4 | 5 | 6 |"
  2612. (should
  2613. (equal '(2 . 3)
  2614. (org-export-table-dimensions
  2615. (org-element-map tree 'table 'identity info 'first-match) info)))))
  2616. (ert-deftest test-org-export/table-cell-address ()
  2617. "Test `org-export-table-cell-address' specifications."
  2618. ;; 1. Standard test: index is 0-based.
  2619. (org-test-with-parsed-data "| a | b |"
  2620. (should
  2621. (equal '((0 . 0) (0 . 1))
  2622. (org-element-map tree 'table-cell
  2623. (lambda (cell) (org-export-table-cell-address cell info))
  2624. info))))
  2625. ;; 2. Special column isn't counted, nor are special rows.
  2626. (org-test-with-parsed-data "
  2627. | / | <> |
  2628. | | c |"
  2629. (should
  2630. (equal '(0 . 0)
  2631. (org-export-table-cell-address
  2632. (car (last (org-element-map tree 'table-cell 'identity info)))
  2633. info))))
  2634. ;; 3. Tables rules do not count either.
  2635. (org-test-with-parsed-data "
  2636. | a |
  2637. |---|
  2638. | b |
  2639. |---|
  2640. | c |"
  2641. (should
  2642. (equal '(2 . 0)
  2643. (org-export-table-cell-address
  2644. (car (last (org-element-map tree 'table-cell 'identity info)))
  2645. info))))
  2646. ;; 4. Return nil for special cells.
  2647. (org-test-with-parsed-data "| / | a |"
  2648. (should-not
  2649. (org-export-table-cell-address
  2650. (org-element-map tree 'table-cell 'identity nil 'first-match)
  2651. info))))
  2652. (ert-deftest test-org-export/get-table-cell-at ()
  2653. "Test `org-export-get-table-cell-at' specifications."
  2654. ;; 1. Address ignores special columns, special rows and rules.
  2655. (org-test-with-parsed-data "
  2656. | / | <> |
  2657. | | a |
  2658. |---+----|
  2659. | | b |"
  2660. (should
  2661. (equal '("b")
  2662. (org-element-contents
  2663. (org-export-get-table-cell-at
  2664. '(1 . 0)
  2665. (org-element-map tree 'table 'identity info 'first-match)
  2666. info)))))
  2667. ;; 2. Return value for a non-existent address is nil.
  2668. (org-test-with-parsed-data "| a |"
  2669. (should-not
  2670. (org-export-get-table-cell-at
  2671. '(2 . 2)
  2672. (org-element-map tree 'table 'identity info 'first-match)
  2673. info)))
  2674. (org-test-with-parsed-data "| / |"
  2675. (should-not
  2676. (org-export-get-table-cell-at
  2677. '(0 . 0)
  2678. (org-element-map tree 'table 'identity info 'first-match)
  2679. info))))
  2680. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  2681. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  2682. ;; 1. A cell at a beginning of a row always starts a column group.
  2683. (org-test-with-parsed-data "| a |"
  2684. (should
  2685. (org-export-table-cell-starts-colgroup-p
  2686. (org-element-map tree 'table-cell 'identity info 'first-match)
  2687. info)))
  2688. ;; 2. Special column should be ignored when determining the
  2689. ;; beginning of the row.
  2690. (org-test-with-parsed-data "
  2691. | / | |
  2692. | | a |"
  2693. (should
  2694. (org-export-table-cell-starts-colgroup-p
  2695. (org-element-map tree 'table-cell 'identity info 'first-match)
  2696. info)))
  2697. ;; 2. Explicit column groups.
  2698. (org-test-with-parsed-data "
  2699. | / | | < |
  2700. | a | b | c |"
  2701. (should
  2702. (equal
  2703. '(yes no yes)
  2704. (org-element-map tree 'table-cell
  2705. (lambda (cell)
  2706. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  2707. info)))))
  2708. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  2709. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  2710. ;; 1. A cell at the end of a row always ends a column group.
  2711. (org-test-with-parsed-data "| a |"
  2712. (should
  2713. (org-export-table-cell-ends-colgroup-p
  2714. (org-element-map tree 'table-cell 'identity info 'first-match)
  2715. info)))
  2716. ;; 2. Special column should be ignored when determining the
  2717. ;; beginning of the row.
  2718. (org-test-with-parsed-data "
  2719. | / | |
  2720. | | a |"
  2721. (should
  2722. (org-export-table-cell-ends-colgroup-p
  2723. (org-element-map tree 'table-cell 'identity info 'first-match)
  2724. info)))
  2725. ;; 3. Explicit column groups.
  2726. (org-test-with-parsed-data "
  2727. | / | < | |
  2728. | a | b | c |"
  2729. (should
  2730. (equal
  2731. '(yes no yes)
  2732. (org-element-map tree 'table-cell
  2733. (lambda (cell)
  2734. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  2735. info)))))
  2736. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  2737. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  2738. ;; 1. A row at the beginning of a table always starts a row group.
  2739. ;; So does a row following a table rule.
  2740. (org-test-with-parsed-data "
  2741. | a |
  2742. |---|
  2743. | b |"
  2744. (should
  2745. (equal
  2746. '(yes no yes)
  2747. (org-element-map tree 'table-row
  2748. (lambda (row)
  2749. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2750. info))))
  2751. ;; 2. Special rows should be ignored when determining the beginning
  2752. ;; of the row.
  2753. (org-test-with-parsed-data "
  2754. | / | < |
  2755. | | a |
  2756. |---+---|
  2757. | / | < |
  2758. | | b |"
  2759. (should
  2760. (equal
  2761. '(yes no yes)
  2762. (org-element-map tree 'table-row
  2763. (lambda (row)
  2764. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2765. info)))))
  2766. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  2767. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  2768. ;; 1. A row at the end of a table always ends a row group. So does
  2769. ;; a row preceding a table rule.
  2770. (org-test-with-parsed-data "
  2771. | a |
  2772. |---|
  2773. | b |"
  2774. (should
  2775. (equal
  2776. '(yes no yes)
  2777. (org-element-map tree 'table-row
  2778. (lambda (row)
  2779. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2780. info))))
  2781. ;; 2. Special rows should be ignored when determining the beginning
  2782. ;; of the row.
  2783. (org-test-with-parsed-data "
  2784. | | a |
  2785. | / | < |
  2786. |---+---|
  2787. | | b |
  2788. | / | < |"
  2789. (should
  2790. (equal
  2791. '(yes no yes)
  2792. (org-element-map tree 'table-row
  2793. (lambda (row)
  2794. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2795. info)))))
  2796. (ert-deftest test-org-export/table-row-in-header-p ()
  2797. "Test `org-export-table-row-in-header-p' specifications."
  2798. ;; Standard test. Separators are always nil.
  2799. (should
  2800. (equal
  2801. '(yes no no)
  2802. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  2803. (org-element-map tree 'table-row
  2804. (lambda (row)
  2805. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  2806. ;; Nil when there is no header.
  2807. (should
  2808. (equal
  2809. '(no no)
  2810. (org-test-with-parsed-data "| a |\n| b |"
  2811. (org-element-map tree 'table-row
  2812. (lambda (row)
  2813. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  2814. (ert-deftest test-org-export/table-row-starts-header-p ()
  2815. "Test `org-export-table-row-starts-header-p' specifications."
  2816. ;; 1. Only the row starting the first row group starts the table
  2817. ;; header.
  2818. (org-test-with-parsed-data "
  2819. | a |
  2820. | b |
  2821. |---|
  2822. | c |"
  2823. (should
  2824. (equal
  2825. '(yes no no no)
  2826. (org-element-map tree 'table-row
  2827. (lambda (row)
  2828. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  2829. info))))
  2830. ;; 2. A row cannot start an header if there's no header in the
  2831. ;; table.
  2832. (org-test-with-parsed-data "
  2833. | a |
  2834. |---|"
  2835. (should-not
  2836. (org-export-table-row-starts-header-p
  2837. (org-element-map tree 'table-row 'identity info 'first-match)
  2838. info))))
  2839. (ert-deftest test-org-export/table-row-ends-header-p ()
  2840. "Test `org-export-table-row-ends-header-p' specifications."
  2841. ;; 1. Only the row starting the first row group starts the table
  2842. ;; header.
  2843. (org-test-with-parsed-data "
  2844. | a |
  2845. | b |
  2846. |---|
  2847. | c |"
  2848. (should
  2849. (equal
  2850. '(no yes no no)
  2851. (org-element-map tree 'table-row
  2852. (lambda (row)
  2853. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  2854. info))))
  2855. ;; 2. A row cannot start an header if there's no header in the
  2856. ;; table.
  2857. (org-test-with-parsed-data "
  2858. | a |
  2859. |---|"
  2860. (should-not
  2861. (org-export-table-row-ends-header-p
  2862. (org-element-map tree 'table-row 'identity info 'first-match)
  2863. info))))
  2864. ;;; Tables of Contents
  2865. (ert-deftest test-org-export/collect-headlines ()
  2866. "Test `org-export-collect-headlines' specifications."
  2867. ;; Standard test.
  2868. (should
  2869. (= 2
  2870. (length
  2871. (org-test-with-parsed-data "* H1\n** H2"
  2872. (org-export-collect-headlines info)))))
  2873. ;; Do not collect headlines below optional argument.
  2874. (should
  2875. (= 1
  2876. (length
  2877. (org-test-with-parsed-data "* H1\n** H2"
  2878. (org-export-collect-headlines info 1)))))
  2879. ;; Never collect headlines below maximum headline level.
  2880. (should
  2881. (= 1
  2882. (length
  2883. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  2884. (org-export-collect-headlines info)))))
  2885. (should
  2886. (= 1
  2887. (length
  2888. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  2889. (org-export-collect-headlines info 2))))))
  2890. ;;; Templates
  2891. (ert-deftest test-org-export/inner-template ()
  2892. "Test `inner-template' translator specifications."
  2893. (should
  2894. (equal "Success!"
  2895. (org-test-with-temp-text "* Headline"
  2896. (org-export-as
  2897. (org-export-create-backend
  2898. :transcoders
  2899. '((inner-template . (lambda (contents info) "Success!"))
  2900. (headline . (lambda (h c i) "Headline"))))))))
  2901. ;; Inner template is applied even in a "body-only" export.
  2902. (should
  2903. (equal "Success!"
  2904. (org-test-with-temp-text "* Headline"
  2905. (org-export-as
  2906. (org-export-create-backend
  2907. :transcoders '((inner-template . (lambda (c i) "Success!"))
  2908. (headline . (lambda (h c i) "Headline"))))
  2909. nil nil 'body-only)))))
  2910. (ert-deftest test-org-export/template ()
  2911. "Test `template' translator specifications."
  2912. (should
  2913. (equal "Success!"
  2914. (org-test-with-temp-text "* Headline"
  2915. (org-export-as
  2916. (org-export-create-backend
  2917. :transcoders '((template . (lambda (contents info) "Success!"))
  2918. (headline . (lambda (h c i) "Headline"))))))))
  2919. ;; Template is not applied in a "body-only" export.
  2920. (should-not
  2921. (equal "Success!"
  2922. (org-test-with-temp-text "* Headline"
  2923. (org-export-as
  2924. (org-export-create-backend
  2925. :transcoders '((template . (lambda (contents info) "Success!"))
  2926. (headline . (lambda (h c i) "Headline"))))
  2927. nil nil 'body-only)))))
  2928. ;;; Topology
  2929. (ert-deftest test-org-export/get-next-element ()
  2930. "Test `org-export-get-next-element' specifications."
  2931. ;; Standard test.
  2932. (should
  2933. (equal "b"
  2934. (org-test-with-parsed-data "* Headline\n*a* b"
  2935. (org-export-get-next-element
  2936. (org-element-map tree 'bold 'identity info t) info))))
  2937. ;; Return nil when no previous element.
  2938. (should-not
  2939. (org-test-with-parsed-data "* Headline\na *b*"
  2940. (org-export-get-next-element
  2941. (org-element-map tree 'bold 'identity info t) info)))
  2942. ;; Non-exportable elements are ignored.
  2943. (should-not
  2944. (let ((org-export-with-timestamps nil))
  2945. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2946. (org-export-get-next-element
  2947. (org-element-map tree 'entity 'identity info t) info))))
  2948. ;; Find next element in secondary strings.
  2949. (should
  2950. (eq 'verbatim
  2951. (org-test-with-parsed-data "* a =verb="
  2952. (org-element-type
  2953. (org-export-get-next-element
  2954. (org-element-map tree 'plain-text 'identity info t) info)))))
  2955. (should
  2956. (eq 'verbatim
  2957. (org-test-with-parsed-data "* /italic/ =verb="
  2958. (org-element-type
  2959. (org-export-get-next-element
  2960. (org-element-map tree 'italic 'identity info t) info)))))
  2961. ;; Find next element in document keywords.
  2962. (should
  2963. (eq 'verbatim
  2964. (org-test-with-parsed-data "#+TITLE: a =verb="
  2965. (org-element-type
  2966. (org-export-get-next-element
  2967. (org-element-map
  2968. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2969. ;; Find next element in parsed affiliated keywords.
  2970. (should
  2971. (eq 'verbatim
  2972. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2973. (org-element-type
  2974. (org-export-get-next-element
  2975. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2976. ;; With optional argument N, return a list containing all the
  2977. ;; following elements.
  2978. (should
  2979. (equal
  2980. '(bold code underline)
  2981. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2982. (mapcar 'car
  2983. (org-export-get-next-element
  2984. (org-element-map tree 'italic 'identity info t) info t)))))
  2985. ;; When N is a positive integer, return a list containing up to
  2986. ;; N following elements.
  2987. (should
  2988. (equal
  2989. '(bold code)
  2990. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2991. (mapcar 'car
  2992. (org-export-get-next-element
  2993. (org-element-map tree 'italic 'identity info t) info 2))))))
  2994. (ert-deftest test-org-export/get-previous-element ()
  2995. "Test `org-export-get-previous-element' specifications."
  2996. ;; Standard test.
  2997. (should
  2998. (equal "a "
  2999. (org-test-with-parsed-data "* Headline\na *b*"
  3000. (org-export-get-previous-element
  3001. (org-element-map tree 'bold 'identity info t) info))))
  3002. ;; Return nil when no previous element.
  3003. (should-not
  3004. (org-test-with-parsed-data "* Headline\n*a* b"
  3005. (org-export-get-previous-element
  3006. (org-element-map tree 'bold 'identity info t) info)))
  3007. ;; Non-exportable elements are ignored.
  3008. (should-not
  3009. (let ((org-export-with-timestamps nil))
  3010. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  3011. (org-export-get-previous-element
  3012. (org-element-map tree 'entity 'identity info t) info))))
  3013. ;; Find previous element in secondary strings.
  3014. (should
  3015. (eq 'verbatim
  3016. (org-test-with-parsed-data "* =verb= a"
  3017. (org-element-type
  3018. (org-export-get-previous-element
  3019. (org-element-map tree 'plain-text 'identity info t) info)))))
  3020. (should
  3021. (eq 'verbatim
  3022. (org-test-with-parsed-data "* =verb= /italic/"
  3023. (org-element-type
  3024. (org-export-get-previous-element
  3025. (org-element-map tree 'italic 'identity info t) info)))))
  3026. ;; Find previous element in document keywords.
  3027. (should
  3028. (eq 'verbatim
  3029. (org-test-with-parsed-data "#+TITLE: =verb= a"
  3030. (org-element-type
  3031. (org-export-get-previous-element
  3032. (org-element-map
  3033. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3034. ;; Find previous element in parsed affiliated keywords.
  3035. (should
  3036. (eq 'verbatim
  3037. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  3038. (org-element-type
  3039. (org-export-get-previous-element
  3040. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3041. ;; With optional argument N, return a list containing up to
  3042. ;; N previous elements.
  3043. (should
  3044. (equal '(underline italic bold)
  3045. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3046. (mapcar 'car
  3047. (org-export-get-previous-element
  3048. (org-element-map tree 'code 'identity info t) info t)))))
  3049. ;; When N is a positive integer, return a list containing up to
  3050. ;; N previous elements.
  3051. (should
  3052. (equal '(italic bold)
  3053. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3054. (mapcar 'car
  3055. (org-export-get-previous-element
  3056. (org-element-map tree 'code 'identity info t) info 2))))))
  3057. (provide 'test-ox)
  3058. ;;; test-org-export.el end here