test-ox.el 99 KB

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