test-ox.el 87 KB

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