test-ox.el 83 KB

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