test-ox.el 116 KB

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