test-ox.el 106 KB

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