test-ox.el 93 KB

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