test-ox.el 76 KB

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