test-ox.el 95 KB

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