test-ox.el 110 KB

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