test-ox.el 91 KB

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