test-ox.el 102 KB

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