test-org-export.el 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. ;;; test-org-export.el --- Tests for org-export.el
  2. ;; Copyright (C) 2012 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  6. ;;;; Comments
  7. ;;; Code:
  8. (unless (featurep 'org-export)
  9. (signal 'missing-test-dependency "org-export"))
  10. (defmacro org-test-with-backend (backend &rest body)
  11. "Execute body with an export back-end defined.
  12. BACKEND is the name of the back-end. BODY is the body to
  13. execute. The defined back-end simply returns parsed data as Org
  14. syntax."
  15. (declare (debug (form body)) (indent 1))
  16. `(let ((,(intern (format "org-%s-translate-alist" backend))
  17. ',(let (transcode-table)
  18. (dolist (type (append org-element-all-elements
  19. org-element-all-objects)
  20. transcode-table)
  21. (push
  22. (cons type
  23. (lambda (obj contents info)
  24. (funcall
  25. (intern (format "org-element-%s-interpreter" type))
  26. obj contents)))
  27. transcode-table)))))
  28. (progn ,@body)))
  29. (defmacro org-test-with-parsed-data (data &rest body)
  30. "Execute body with parsed data available.
  31. DATA is a string containing the data to be parsed. BODY is the
  32. body to execute. Parse tree is available under the `tree'
  33. variable, and communication channel under `info'.
  34. This function calls `org-export-collect-tree-properties'. As
  35. such, `:ignore-list' (for `org-element-map') and
  36. `:parse-tree' (for `org-export-get-genealogy') properties are
  37. already filled in `info'."
  38. (declare (debug (form body)) (indent 1))
  39. `(org-test-with-temp-text ,data
  40. (let* ((tree (org-element-parse-buffer))
  41. (info (org-export-collect-tree-properties
  42. tree (org-export-get-environment))))
  43. ,@body)))
  44. ;;; Internal Tests
  45. (ert-deftest test-org-export/bind-keyword ()
  46. "Test reading #+BIND: keywords."
  47. ;; Test with `org-export-all-BIND' set to t.
  48. (should
  49. (org-test-with-temp-text "#+BIND: variable value"
  50. (let ((org-export-allow-BIND t))
  51. (org-export--install-letbind-maybe)
  52. (eq variable 'value))))
  53. ;; Test with `org-export-all-BIND' set to nil.
  54. (should-not
  55. (org-test-with-temp-text "#+BIND: variable value"
  56. (let ((org-export-allow-BIND nil))
  57. (org-export--install-letbind-maybe)
  58. (boundp 'variable))))
  59. ;; Test with `org-export-all-BIND' set to 'confirm and
  60. ;; `org-export--allow-BIND-local' to t .
  61. (should
  62. (org-test-with-temp-text "#+BIND: variable value"
  63. (let ((org-export-allow-BIND 'confirm))
  64. (org-set-local 'org-export--allow-BIND-local t)
  65. (org-export--install-letbind-maybe)
  66. (eq variable 'value))))
  67. ;; Test with `org-export-all-BIND' set to 'confirm and
  68. ;; `org-export--allow-BIND-local' to nil.
  69. (should-not
  70. (org-test-with-temp-text "#+BIND: variable value"
  71. (let ((org-export-allow-BIND 'confirm))
  72. (org-set-local 'org-export--allow-BIND-local nil)
  73. (org-export--install-letbind-maybe)
  74. (boundp 'variable))))
  75. ;; BIND keywords are case-insensitive.
  76. (should
  77. (org-test-with-temp-text "#+bind: variable value"
  78. (let ((org-export-allow-BIND t))
  79. (org-export--install-letbind-maybe)
  80. (eq variable 'value)))))
  81. (ert-deftest test-org-export/parse-option-keyword ()
  82. "Test reading all standard #+OPTIONS: items."
  83. (should
  84. (equal
  85. (org-export--parse-option-keyword
  86. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  87. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  88. stat:t")
  89. '(:headline-levels
  90. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  91. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  92. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  93. :with-footnotes t :with-inlinetasks nil :with-priority t
  94. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  95. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  96. :with-todo-keywords t)))
  97. ;; Test some special values.
  98. (should
  99. (equal
  100. (org-export--parse-option-keyword
  101. "arch:headline creator:comment d:(\"TEST\")
  102. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  103. '( :section-numbers
  104. 2
  105. :with-archived-trees headline :with-creator comment
  106. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  107. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  108. (ert-deftest test-org-export/get-inbuffer-options ()
  109. "Test reading all standard export keywords."
  110. (should
  111. (equal
  112. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  113. #+CREATOR: Idem
  114. #+DATE: Today
  115. #+DESCRIPTION: Testing
  116. #+DESCRIPTION: with two lines
  117. #+EMAIL: some@email.org
  118. #+EXCLUDE_TAGS: noexport invisible
  119. #+KEYWORDS: test
  120. #+LANGUAGE: en
  121. #+SELECT_TAGS: export
  122. #+TITLE: Some title
  123. #+TITLE: with spaces"
  124. (org-export--get-inbuffer-options))
  125. '(:author
  126. ("Me, Myself and I") :creator "Idem" :date ("Today")
  127. :description "Testing\nwith two lines" :email "some@email.org"
  128. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  129. :select-tags ("export") :title ("Some title with spaces")))))
  130. (ert-deftest test-org-export/get-subtree-options ()
  131. "Test setting options from headline's properties."
  132. ;; EXPORT_TITLE.
  133. (org-test-with-temp-text "#+TITLE: Title
  134. * Headline
  135. :PROPERTIES:
  136. :EXPORT_TITLE: Subtree Title
  137. :END:
  138. Paragraph"
  139. (forward-line)
  140. (should (equal (plist-get (org-export-get-environment nil t) :title)
  141. '("Subtree Title"))))
  142. :title
  143. '("subtree-title")
  144. ;; EXPORT_OPTIONS.
  145. (org-test-with-temp-text "#+OPTIONS: H:1
  146. * Headline
  147. :PROPERTIES:
  148. :EXPORT_OPTIONS: H:2
  149. :END:
  150. Paragraph"
  151. (forward-line)
  152. (should
  153. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  154. ;; EXPORT_DATE.
  155. (org-test-with-temp-text "#+DATE: today
  156. * Headline
  157. :PROPERTIES:
  158. :EXPORT_DATE: 29-03-2012
  159. :END:
  160. Paragraph"
  161. (forward-line)
  162. (should (equal (plist-get (org-export-get-environment nil t) :date)
  163. '("29-03-2012"))))
  164. ;; Export properties are case-insensitive.
  165. (org-test-with-temp-text "* Headline
  166. :PROPERTIES:
  167. :EXPORT_Date: 29-03-2012
  168. :END:
  169. Paragraph"
  170. (should (equal (plist-get (org-export-get-environment nil t) :date)
  171. '("29-03-2012")))))
  172. (ert-deftest test-org-export/handle-options ()
  173. "Test if export options have an impact on output."
  174. ;; Test exclude tags.
  175. (org-test-with-temp-text "* Head1 :noexport:"
  176. (org-test-with-backend test
  177. (should
  178. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  179. ""))))
  180. ;; Test include tags.
  181. (org-test-with-temp-text "
  182. * Head1
  183. * Head2
  184. ** Sub-Head2.1 :export:
  185. *** Sub-Head2.1.1
  186. * Head2"
  187. (org-test-with-backend test
  188. (should
  189. (equal
  190. "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
  191. (let ((org-tags-column 0))
  192. (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
  193. ;; Test mixing include tags and exclude tags.
  194. (org-test-with-temp-text "
  195. * Head1 :export:
  196. ** Sub-Head1 :noexport:
  197. ** Sub-Head2
  198. * Head2 :noexport:
  199. ** Sub-Head1 :export:"
  200. (org-test-with-backend test
  201. (should
  202. (string-match
  203. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  204. (org-export-as
  205. 'test nil nil nil
  206. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  207. ;; Ignore tasks.
  208. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  209. (org-test-with-temp-text "* TODO Head1"
  210. (org-test-with-backend test
  211. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  212. "")))))
  213. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  214. (org-test-with-temp-text "* TODO Head1"
  215. (org-test-with-backend test
  216. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  217. "* TODO Head1\n")))))
  218. ;; Archived tree.
  219. (org-test-with-temp-text "* Head1 :archive:"
  220. (let ((org-archive-tag "archive"))
  221. (org-test-with-backend test
  222. (should
  223. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  224. "")))))
  225. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  226. (let ((org-archive-tag "archive"))
  227. (org-test-with-backend test
  228. (should
  229. (string-match
  230. "\\* Head1[ \t]+:archive:"
  231. (org-export-as 'test nil nil nil
  232. '(:with-archived-trees headline)))))))
  233. (org-test-with-temp-text "* Head1 :archive:"
  234. (let ((org-archive-tag "archive"))
  235. (org-test-with-backend test
  236. (should
  237. (string-match
  238. "\\`\\* Head1[ \t]+:archive:\n\\'"
  239. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  240. ;; Drawers.
  241. (let ((org-drawers '("TEST")))
  242. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  243. (org-test-with-backend test
  244. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  245. ""))
  246. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  247. ":TEST:\ncontents\n:END:\n")))))
  248. (let ((org-drawers '("FOO" "BAR")))
  249. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  250. (org-test-with-backend test
  251. (should
  252. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  253. ":FOO:\nkeep\n:END:\n")))))
  254. ;; Timestamps.
  255. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  256. (org-test-with-backend test
  257. (should
  258. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  259. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  260. (should
  261. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  262. (should
  263. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  264. "<2012-04-29 sun. 10:45>\n"))
  265. (should
  266. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  267. "[2012-04-29 sun. 10:45]\n"))))
  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. ;; Plannings.
  278. (let ((org-closed-string "CLOSED:"))
  279. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  280. (org-test-with-backend test
  281. (should
  282. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  283. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  284. (should
  285. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  286. "")))))
  287. ;; Inlinetasks.
  288. (when (featurep 'org-inlinetask)
  289. (should
  290. (equal
  291. (let ((org-inlinetask-min-level 15))
  292. (org-test-with-temp-text "*************** Task"
  293. (org-test-with-backend test
  294. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  295. ""))
  296. (should
  297. (equal
  298. (let ((org-inlinetask-min-level 15))
  299. (org-test-with-temp-text
  300. "*************** Task\nContents\n*************** END"
  301. (org-test-with-backend test
  302. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  303. "")))
  304. ;; Statistics cookies.
  305. (should
  306. (equal ""
  307. (org-test-with-temp-text "[0/0]"
  308. (org-test-with-backend test
  309. (org-export-as
  310. 'test nil nil nil '(:with-statistics-cookies nil)))))))
  311. (ert-deftest test-org-export/comment-tree ()
  312. "Test if export process ignores commented trees."
  313. (let ((org-comment-string "COMMENT"))
  314. (org-test-with-temp-text "* COMMENT Head1"
  315. (org-test-with-backend test
  316. (should (equal (org-export-as 'test) ""))))))
  317. (ert-deftest test-org-export/export-scope ()
  318. "Test all export scopes."
  319. (org-test-with-temp-text "
  320. * Head1
  321. ** Head2
  322. text
  323. *** Head3"
  324. (org-test-with-backend test
  325. ;; Subtree.
  326. (forward-line 3)
  327. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  328. ;; Visible.
  329. (goto-char (point-min))
  330. (forward-line)
  331. (org-cycle)
  332. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  333. ;; Body only.
  334. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  335. (push '(template . org-test-template) org-test-translate-alist)
  336. (should (equal (org-export-as 'test nil nil 'body-only)
  337. "* Head1\n** Head2\ntext\n*** Head3\n"))
  338. (should (equal (org-export-as 'test)
  339. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  340. ;; Region.
  341. (goto-char (point-min))
  342. (forward-line 3)
  343. (transient-mark-mode 1)
  344. (push-mark (point) t t)
  345. (goto-char (point-at-eol))
  346. (should (equal (org-export-as 'test) "text\n"))))
  347. ;; Subtree with a code block calling another block outside.
  348. (should
  349. (equal ": 3\n"
  350. (org-test-with-temp-text "
  351. * Head1
  352. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  353. <<test>>
  354. #+END_SRC
  355. * Head2
  356. #+NAME: test
  357. #+BEGIN_SRC emacs-lisp
  358. \(+ 1 2)
  359. #+END_SRC"
  360. (org-test-with-backend test
  361. (forward-line 1)
  362. (org-export-as 'test 'subtree))))))
  363. (ert-deftest test-org-export/expand-include ()
  364. "Test file inclusion in an Org buffer."
  365. ;; Error when file isn't specified.
  366. (should-error
  367. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  368. (org-export-expand-include-keyword)))
  369. ;; Full insertion with recursive inclusion.
  370. (org-test-with-temp-text
  371. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  372. (org-export-expand-include-keyword)
  373. (should (equal (buffer-string)
  374. "Small Org file with an include keyword.
  375. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  376. Success!
  377. * Heading
  378. body\n")))
  379. ;; Localized insertion.
  380. (org-test-with-temp-text
  381. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  382. org-test-dir)
  383. (org-export-expand-include-keyword)
  384. (should (equal (buffer-string)
  385. "Small Org file with an include keyword.\n")))
  386. ;; Insertion with constraints on headlines level.
  387. (org-test-with-temp-text
  388. (format
  389. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  390. org-test-dir)
  391. (org-export-expand-include-keyword)
  392. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  393. ;; Inclusion within an example block.
  394. (org-test-with-temp-text
  395. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  396. org-test-dir)
  397. (org-export-expand-include-keyword)
  398. (should
  399. (equal
  400. (buffer-string)
  401. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  402. ;; Inclusion within a src-block.
  403. (org-test-with-temp-text
  404. (format
  405. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  406. org-test-dir)
  407. (org-export-expand-include-keyword)
  408. (should (equal (buffer-string)
  409. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  410. (ert-deftest test-org-export/expand-macro ()
  411. "Test macro expansion in an Org buffer."
  412. ;; Standard macro expansion.
  413. (should
  414. (equal "#+MACRO: macro1 value\nvalue\n"
  415. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  416. (org-test-with-backend test (org-export-as 'test)))))
  417. ;; Expand specific macros.
  418. (should
  419. (equal "me 2012-03-29 me@here Title\n"
  420. (org-test-with-temp-text
  421. "
  422. #+TITLE: Title
  423. #+DATE: 2012-03-29
  424. #+AUTHOR: me
  425. #+EMAIL: me@here
  426. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  427. (let ((output (org-test-with-backend test (org-export-as 'test))))
  428. (substring output (string-match ".*\n\\'" output))))))
  429. ;; Expand specific macros when property contained a regular macro
  430. ;; already.
  431. (should
  432. (equal "value\n"
  433. (org-test-with-temp-text "
  434. #+MACRO: macro1 value
  435. #+TITLE: {{{macro1}}}
  436. {{{title}}}"
  437. (let ((output (org-test-with-backend test (org-export-as 'test))))
  438. (substring output (string-match ".*\n\\'" output))))))
  439. ;; Expand macros with templates in included files.
  440. (should
  441. (equal "success\n"
  442. (org-test-with-temp-text
  443. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  444. {{{included-macro}}}" org-test-dir)
  445. (let ((output (org-test-with-backend test (org-export-as 'test))))
  446. (substring output (string-match ".*\n\\'" output)))))))
  447. (ert-deftest test-org-export/user-ignore-list ()
  448. "Test if `:ignore-list' accepts user input."
  449. (org-test-with-backend test
  450. (flet ((skip-note-head
  451. (data backend info)
  452. ;; Ignore headlines with the word "note" in their title.
  453. (org-element-map
  454. data 'headline
  455. (lambda (headline)
  456. (when (string-match "\\<note\\>"
  457. (org-element-property :raw-value headline))
  458. (org-export-ignore-element headline info)))
  459. info)
  460. data))
  461. ;; Install function in parse tree filters.
  462. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  463. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  464. (should (equal (org-export-as 'test) "* Head1\n")))))))
  465. (ert-deftest test-org-export/before-parsing-hook ()
  466. "Test `org-export-before-parsing-hook'."
  467. (org-test-with-backend test
  468. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  469. (let ((org-export-before-parsing-hook
  470. '((lambda (backend)
  471. (org-map-entries
  472. (lambda ()
  473. (delete-region (point) (progn (forward-line) (point)))))))))
  474. (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
  475. ;;; Affiliated Keywords
  476. (ert-deftest test-org-export/read-attribute ()
  477. "Test `org-export-read-attribute' specifications."
  478. ;; Standard test.
  479. (should
  480. (equal
  481. (org-export-read-attribute
  482. :attr_html
  483. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  484. (org-element-at-point)))
  485. '(:a 1 :b 2)))
  486. ;; Return nil on empty attribute.
  487. (should-not
  488. (org-export-read-attribute
  489. :attr_html
  490. (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
  491. (ert-deftest test-org-export/get-caption ()
  492. "Test `org-export-get-caption' specifications."
  493. ;; Without optional argument, return long caption
  494. (should
  495. (equal
  496. '("l")
  497. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  498. (org-export-get-caption (org-element-at-point)))))
  499. ;; With optional argument, return short caption.
  500. (should
  501. (equal
  502. '("s")
  503. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  504. (org-export-get-caption (org-element-at-point) t))))
  505. ;; Multiple lines are separated by white spaces.
  506. (should
  507. (equal
  508. '("a" " " "b")
  509. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  510. (org-export-get-caption (org-element-at-point))))))
  511. ;;; Export Snippets
  512. (ert-deftest test-org-export/export-snippet ()
  513. "Test export snippets transcoding."
  514. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  515. (org-test-with-backend test
  516. (let ((org-test-translate-alist
  517. (cons (cons 'export-snippet
  518. (lambda (snippet contents info)
  519. (when (eq (org-export-snippet-backend snippet) 'test)
  520. (org-element-property :value snippet))))
  521. org-test-translate-alist)))
  522. (let ((org-export-snippet-translation-alist nil))
  523. (should (equal (org-export-as 'test) "A\n")))
  524. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  525. (should (equal (org-export-as 'test) "AB\n")))))))
  526. ;;; Footnotes
  527. (ert-deftest test-org-export/footnotes ()
  528. "Test footnotes specifications."
  529. (let ((org-footnote-section nil)
  530. (org-export-with-footnotes t))
  531. ;; 1. Read every type of footnote.
  532. (should
  533. (equal
  534. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  535. (org-test-with-parsed-data
  536. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  537. (org-element-map
  538. tree 'footnote-reference
  539. (lambda (ref)
  540. (let ((def (org-export-get-footnote-definition ref info)))
  541. (cons (org-export-get-footnote-number ref info)
  542. (if (eq (org-element-property :type ref) 'inline) (car def)
  543. (car (org-element-contents
  544. (car (org-element-contents def))))))))
  545. info))))
  546. ;; 2. Test nested footnotes order.
  547. (org-test-with-parsed-data
  548. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  549. (should
  550. (equal
  551. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  552. (org-element-map
  553. tree 'footnote-reference
  554. (lambda (ref)
  555. (when (org-export-footnote-first-reference-p ref info)
  556. (cons (org-export-get-footnote-number ref info)
  557. (org-element-property :label ref))))
  558. info))))
  559. ;; 3. Test nested footnote in invisible definitions.
  560. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  561. ;; Hide definitions.
  562. (narrow-to-region (point) (point-at-eol))
  563. (let* ((tree (org-element-parse-buffer))
  564. (info (org-combine-plists
  565. `(:parse-tree ,tree)
  566. (org-export-collect-tree-properties
  567. tree (org-export-get-environment)))))
  568. ;; Both footnotes should be seen.
  569. (should
  570. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  571. ;; 4. Test footnotes definitions collection.
  572. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  573. \[fn:2] B [fn:3] [fn::D].
  574. \[fn:3] C."
  575. (should (= (length (org-export-collect-footnote-definitions tree info))
  576. 4)))
  577. ;; 5. Test export of footnotes defined outside parsing scope.
  578. (org-test-with-temp-text "[fn:1] Out of scope
  579. * Title
  580. Paragraph[fn:1]"
  581. (org-test-with-backend test
  582. (let ((org-test-translate-alist
  583. (cons (cons 'footnote-reference
  584. (lambda (fn contents info)
  585. (org-element-interpret-data
  586. (org-export-get-footnote-definition fn info))))
  587. org-test-translate-alist)))
  588. (forward-line)
  589. (should (equal "ParagraphOut of scope\n"
  590. (org-export-as 'test 'subtree))))))))
  591. ;;; Headlines and Inlinetasks
  592. (ert-deftest test-org-export/get-relative-level ()
  593. "Test `org-export-get-relative-level' specifications."
  594. ;; Standard test.
  595. (should
  596. (equal '(1 2)
  597. (let ((org-odd-levels-only nil))
  598. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  599. (org-element-map
  600. tree 'headline
  601. (lambda (h) (org-export-get-relative-level h info))
  602. info)))))
  603. ;; Missing levels
  604. (should
  605. (equal '(1 3)
  606. (let ((org-odd-levels-only nil))
  607. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  608. (org-element-map
  609. tree 'headline
  610. (lambda (h) (org-export-get-relative-level h info))
  611. info))))))
  612. (ert-deftest test-org-export/low-level-p ()
  613. "Test `org-export-low-level-p' specifications."
  614. (should
  615. (equal
  616. '(no yes)
  617. (let ((org-odd-levels-only nil))
  618. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  619. (org-element-map
  620. tree 'headline
  621. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  622. (plist-put info :headline-levels 1)))))))
  623. (ert-deftest test-org-export/get-headline-number ()
  624. "Test `org-export-get-headline-number' specifications."
  625. ;; Standard test.
  626. (should
  627. (equal
  628. '((1) (1 1))
  629. (let ((org-odd-levels-only nil))
  630. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  631. (org-element-map
  632. tree 'headline
  633. (lambda (h) (org-export-get-headline-number h info))
  634. info)))))
  635. ;; Missing levels are replaced with 0.
  636. (should
  637. (equal
  638. '((1) (1 0 1))
  639. (let ((org-odd-levels-only nil))
  640. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  641. (org-element-map
  642. tree 'headline
  643. (lambda (h) (org-export-get-headline-number h info))
  644. info))))))
  645. (ert-deftest test-org-export/numbered-headline-p ()
  646. "Test `org-export-numbered-headline-p' specifications."
  647. ;; If `:section-numbers' is nil, never number headlines.
  648. (should-not
  649. (org-test-with-parsed-data "* Headline"
  650. (org-element-map
  651. tree 'headline
  652. (lambda (h) (org-export-numbered-headline-p h info))
  653. (plist-put info :section-numbers nil))))
  654. ;; If `:section-numbers' is a number, only number headlines with
  655. ;; a level greater that it.
  656. (should
  657. (equal
  658. '(yes no)
  659. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  660. (org-element-map
  661. tree 'headline
  662. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  663. (plist-put info :section-numbers 1)))))
  664. ;; Otherwise, headlines are always numbered.
  665. (should
  666. (org-test-with-parsed-data "* Headline"
  667. (org-element-map
  668. tree 'headline
  669. (lambda (h) (org-export-numbered-headline-p h info))
  670. (plist-put info :section-numbers t)))))
  671. (ert-deftest test-org-export/number-to-roman ()
  672. "Test `org-export-number-to-roman' specifications."
  673. ;; If number is negative, return it as a string.
  674. (should (equal (org-export-number-to-roman -1) "-1"))
  675. ;; Otherwise, return it as a roman number.
  676. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  677. (ert-deftest test-org-export/get-tags ()
  678. "Test `org-export-get-tags' specifications."
  679. (let ((org-export-exclude-tags '("noexport"))
  680. (org-export-select-tags '("export")))
  681. ;; Standard test: tags which are not a select tag, an exclude tag,
  682. ;; or specified as optional argument shouldn't be ignored.
  683. (should
  684. (org-test-with-parsed-data "* Headline :tag:"
  685. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  686. info)))
  687. ;; Exclude tags are removed.
  688. (should-not
  689. (org-test-with-parsed-data "* Headline :noexport:"
  690. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  691. info)))
  692. ;; Select tags are removed.
  693. (should-not
  694. (org-test-with-parsed-data "* Headline :export:"
  695. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  696. info)))
  697. (should
  698. (equal
  699. '("tag")
  700. (org-test-with-parsed-data "* Headline :tag:export:"
  701. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  702. info))))
  703. ;; Tags provided in the optional argument are also ignored.
  704. (should-not
  705. (org-test-with-parsed-data "* Headline :ignore:"
  706. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  707. info '("ignore"))))
  708. ;; Allow tag inheritance.
  709. (should
  710. (equal
  711. '(("tag") ("tag"))
  712. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  713. (org-element-map
  714. tree 'headline
  715. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  716. ;; Tag inheritance checks FILETAGS keywords.
  717. (should
  718. (equal
  719. '(("a" "b" "tag"))
  720. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  721. (org-element-map
  722. tree 'headline
  723. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  724. (ert-deftest test-org-export/get-node-property ()
  725. "Test`org-export-get-node-property' specifications."
  726. ;; Standard test.
  727. (should
  728. (equal "value"
  729. (org-test-with-parsed-data "* Headline
  730. :PROPERTIES:
  731. :prop: value
  732. :END:"
  733. (org-export-get-node-property
  734. :prop (org-element-map tree 'headline 'identity nil t)))))
  735. ;; Test inheritance.
  736. (should
  737. (equal "value"
  738. (org-test-with-parsed-data "* Parent
  739. :PROPERTIES:
  740. :prop: value
  741. :END:
  742. ** Headline
  743. Paragraph"
  744. (org-export-get-node-property
  745. :prop (org-element-map tree 'paragraph 'identity nil t) t))))
  746. ;; Cannot return a value before the first headline.
  747. (should-not
  748. (org-test-with-parsed-data "Paragraph
  749. * Headline
  750. :PROPERTIES:
  751. :prop: value
  752. :END:"
  753. (org-export-get-node-property
  754. :prop (org-element-map tree 'paragraph 'identity nil t)))))
  755. (ert-deftest test-org-export/first-sibling-p ()
  756. "Test `org-export-first-sibling-p' specifications."
  757. ;; Standard test.
  758. (should
  759. (equal
  760. '(yes yes no)
  761. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  762. (org-element-map
  763. tree 'headline
  764. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  765. info))))
  766. ;; Ignore headlines not exported.
  767. (should
  768. (equal
  769. '(yes)
  770. (let ((org-export-exclude-tags '("ignore")))
  771. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  772. (org-element-map
  773. tree 'headline
  774. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  775. info))))))
  776. (ert-deftest test-org-export/last-sibling-p ()
  777. "Test `org-export-last-sibling-p' specifications."
  778. ;; Standard test.
  779. (should
  780. (equal
  781. '(yes no yes)
  782. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  783. (org-element-map
  784. tree 'headline
  785. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  786. info))))
  787. ;; Ignore headlines not exported.
  788. (should
  789. (equal
  790. '(yes)
  791. (let ((org-export-exclude-tags '("ignore")))
  792. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  793. (org-element-map
  794. tree 'headline
  795. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  796. info))))))
  797. ;;; Links
  798. (ert-deftest test-org-export/get-coderef-format ()
  799. "Test `org-export-get-coderef-format' specifications."
  800. ;; A link without description returns "%s"
  801. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  802. "%s"))
  803. ;; Return "%s" when path is matched within description.
  804. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  805. "desc %s"))
  806. ;; Otherwise return description.
  807. (should (equal (org-export-get-coderef-format "path" "desc")
  808. "desc")))
  809. (ert-deftest test-org-export/inline-image-p ()
  810. "Test `org-export-inline-image-p' specifications."
  811. (should
  812. (org-export-inline-image-p
  813. (org-test-with-temp-text "[[#id]]"
  814. (org-element-map
  815. (org-element-parse-buffer) 'link 'identity nil t))
  816. '(("custom-id" . "id")))))
  817. (ert-deftest test-org-export/fuzzy-link ()
  818. "Test fuzzy links specifications."
  819. ;; 1. Links to invisible (keyword) targets should be ignored.
  820. (org-test-with-parsed-data
  821. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  822. (should-not
  823. (org-element-map
  824. tree 'link
  825. (lambda (link)
  826. (org-export-get-ordinal
  827. (org-export-resolve-fuzzy-link link info) info)) info)))
  828. ;; 2. Link to an headline should return headline's number.
  829. (org-test-with-parsed-data
  830. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  831. (should
  832. ;; Note: Headline's number is in fact a list of numbers.
  833. (equal '(2)
  834. (org-element-map
  835. tree 'link
  836. (lambda (link)
  837. (org-export-get-ordinal
  838. (org-export-resolve-fuzzy-link link info) info)) info t))))
  839. ;; 3. Link to a target in an item should return item's number.
  840. (org-test-with-parsed-data
  841. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  842. (should
  843. ;; Note: Item's number is in fact a list of numbers.
  844. (equal '(1 2)
  845. (org-element-map
  846. tree 'link
  847. (lambda (link)
  848. (org-export-get-ordinal
  849. (org-export-resolve-fuzzy-link link info) info)) info t))))
  850. ;; 4. Link to a target in a footnote should return footnote's
  851. ;; number.
  852. (org-test-with-parsed-data "
  853. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  854. (should
  855. (equal '(2 3)
  856. (org-element-map
  857. tree 'link
  858. (lambda (link)
  859. (org-export-get-ordinal
  860. (org-export-resolve-fuzzy-link link info) info)) info))))
  861. ;; 5. Link to a named element should return sequence number of that
  862. ;; element.
  863. (org-test-with-parsed-data
  864. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  865. (should
  866. (= 2
  867. (org-element-map
  868. tree 'link
  869. (lambda (link)
  870. (org-export-get-ordinal
  871. (org-export-resolve-fuzzy-link link info) info)) info t))))
  872. ;; 6. Link to a target not within an item, a table, a footnote
  873. ;; reference or definition should return section number.
  874. (org-test-with-parsed-data
  875. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  876. (should
  877. (equal '(2)
  878. (org-element-map
  879. tree 'link
  880. (lambda (link)
  881. (org-export-get-ordinal
  882. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  883. (ert-deftest test-org-export/resolve-coderef ()
  884. "Test `org-export-resolve-coderef' specifications."
  885. (let ((org-coderef-label-format "(ref:%s)"))
  886. ;; 1. A link to a "-n -k -r" block returns line number.
  887. (org-test-with-parsed-data
  888. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  889. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  890. (org-test-with-parsed-data
  891. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  892. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  893. ;; 2. A link to a "-n -r" block returns line number.
  894. (org-test-with-parsed-data
  895. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  896. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  897. (org-test-with-parsed-data
  898. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  899. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  900. ;; 3. A link to a "-n" block returns coderef.
  901. (org-test-with-parsed-data
  902. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  903. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  904. (org-test-with-parsed-data
  905. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  906. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  907. ;; 4. A link to a "-r" block returns line number.
  908. (org-test-with-parsed-data
  909. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  910. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  911. (org-test-with-parsed-data
  912. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  913. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  914. ;; 5. A link to a block without a switch returns coderef.
  915. (org-test-with-parsed-data
  916. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  917. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  918. (org-test-with-parsed-data
  919. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  920. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  921. ;; 6. Correctly handle continued line numbers. A "+n" switch
  922. ;; should resume numbering from previous block with numbered
  923. ;; lines, ignoring blocks not numbering lines in the process.
  924. ;; A "-n" switch resets count.
  925. (org-test-with-parsed-data "
  926. #+BEGIN_EXAMPLE -n
  927. Text.
  928. #+END_EXAMPLE
  929. #+BEGIN_SRC emacs-lisp
  930. \(- 1 1)
  931. #+END_SRC
  932. #+BEGIN_SRC emacs-lisp +n -r
  933. \(+ 1 1) (ref:addition)
  934. #+END_SRC
  935. #+BEGIN_EXAMPLE -n -r
  936. Another text. (ref:text)
  937. #+END_EXAMPLE"
  938. (should (= (org-export-resolve-coderef "addition" info) 2))
  939. (should (= (org-export-resolve-coderef "text" info) 1)))
  940. ;; 7. Recognize coderef with user-specified syntax.
  941. (org-test-with-parsed-data
  942. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  943. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  944. (ert-deftest test-org-export/resolve-fuzzy-link ()
  945. "Test `org-export-resolve-fuzzy-link' specifications."
  946. ;; 1. Match target objects.
  947. (org-test-with-parsed-data "<<target>> [[target]]"
  948. (should
  949. (org-export-resolve-fuzzy-link
  950. (org-element-map tree 'link 'identity info t) info)))
  951. ;; 2. Match target elements.
  952. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  953. (should
  954. (org-export-resolve-fuzzy-link
  955. (org-element-map tree 'link 'identity info t) info)))
  956. ;; 3. Match named elements.
  957. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  958. (should
  959. (org-export-resolve-fuzzy-link
  960. (org-element-map tree 'link 'identity info t) info)))
  961. ;; 4. Match exact headline's name.
  962. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  963. (should
  964. (org-export-resolve-fuzzy-link
  965. (org-element-map tree 'link 'identity info t) info)))
  966. ;; 5. Targets objects have priority over named elements and headline
  967. ;; titles.
  968. (org-test-with-parsed-data
  969. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  970. (should
  971. (eq 'target
  972. (org-element-type
  973. (org-export-resolve-fuzzy-link
  974. (org-element-map tree 'link 'identity info t) info)))))
  975. ;; 6. Named elements have priority over headline titles.
  976. (org-test-with-parsed-data
  977. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  978. (should
  979. (eq 'paragraph
  980. (org-element-type
  981. (org-export-resolve-fuzzy-link
  982. (org-element-map tree 'link 'identity info t) info)))))
  983. ;; 7. If link's path starts with a "*", only match headline titles,
  984. ;; though.
  985. (org-test-with-parsed-data
  986. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  987. (should
  988. (eq 'headline
  989. (org-element-type
  990. (org-export-resolve-fuzzy-link
  991. (org-element-map tree 'link 'identity info t) info)))))
  992. ;; 8. Return nil if no match.
  993. (org-test-with-parsed-data "[[target]]"
  994. (should-not
  995. (org-export-resolve-fuzzy-link
  996. (org-element-map tree 'link 'identity info t) info))))
  997. (ert-deftest test-org-export/resolve-id-link ()
  998. "Test `org-export-resolve-id-link' specifications."
  999. ;; 1. Regular test for custom-id link.
  1000. (org-test-with-parsed-data "* Headline1
  1001. :PROPERTIES:
  1002. :CUSTOM-ID: test
  1003. :END:
  1004. * Headline 2
  1005. \[[#test]]"
  1006. (should
  1007. (org-export-resolve-id-link
  1008. (org-element-map tree 'link 'identity info t) info)))
  1009. ;; 2. Failing test for custom-id link.
  1010. (org-test-with-parsed-data "* Headline1
  1011. :PROPERTIES:
  1012. :CUSTOM-ID: test
  1013. :END:
  1014. * Headline 2
  1015. \[[#no-match]]"
  1016. (should-not
  1017. (org-export-resolve-id-link
  1018. (org-element-map tree 'link 'identity info t) info)))
  1019. ;; 3. Test for internal id target.
  1020. (org-test-with-parsed-data "* Headline1
  1021. :PROPERTIES:
  1022. :ID: aaaa
  1023. :END:
  1024. * Headline 2
  1025. \[[id:aaaa]]"
  1026. (should
  1027. (org-export-resolve-id-link
  1028. (org-element-map tree 'link 'identity info t) info)))
  1029. ;; 4. Test for external id target.
  1030. (org-test-with-parsed-data "[[id:aaaa]]"
  1031. (should
  1032. (org-export-resolve-id-link
  1033. (org-element-map tree 'link 'identity info t)
  1034. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1035. (ert-deftest test-org-export/resolve-radio-link ()
  1036. "Test `org-export-resolve-radio-link' specifications."
  1037. ;; Standard test.
  1038. (org-test-with-temp-text "<<<radio>>> radio"
  1039. (org-update-radio-target-regexp)
  1040. (should
  1041. (let* ((tree (org-element-parse-buffer))
  1042. (info `(:parse-tree ,tree)))
  1043. (org-export-resolve-radio-link
  1044. (org-element-map tree 'link 'identity info t)
  1045. info))))
  1046. ;; Radio target with objects.
  1047. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1048. (org-update-radio-target-regexp)
  1049. (should
  1050. (let* ((tree (org-element-parse-buffer))
  1051. (info `(:parse-tree ,tree)))
  1052. (org-export-resolve-radio-link
  1053. (org-element-map tree 'link 'identity info t)
  1054. info)))))
  1055. ;;; Src-block and example-block
  1056. (ert-deftest test-org-export/unravel-code ()
  1057. "Test `org-export-unravel-code' function."
  1058. (let ((org-coderef-label-format "(ref:%s)"))
  1059. ;; 1. Code without reference.
  1060. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1061. (should (equal (org-export-unravel-code (org-element-at-point))
  1062. '("(+ 1 1)\n"))))
  1063. ;; 2. Code with reference.
  1064. (org-test-with-temp-text
  1065. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1066. (should (equal (org-export-unravel-code (org-element-at-point))
  1067. '("(+ 1 1)\n" (1 . "test")))))
  1068. ;; 3. Code with user-defined reference.
  1069. (org-test-with-temp-text
  1070. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1071. (should (equal (org-export-unravel-code (org-element-at-point))
  1072. '("(+ 1 1)\n" (1 . "test")))))
  1073. ;; 4. Code references keys are relative to the current block.
  1074. (org-test-with-temp-text "
  1075. #+BEGIN_EXAMPLE -n
  1076. \(+ 1 1)
  1077. #+END_EXAMPLE
  1078. #+BEGIN_EXAMPLE +n
  1079. \(+ 2 2)
  1080. \(+ 3 3) (ref:one)
  1081. #+END_EXAMPLE"
  1082. (goto-line 5)
  1083. (should (equal (org-export-unravel-code (org-element-at-point))
  1084. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1085. ;;; Smart Quotes
  1086. (ert-deftest test-org-export/activate-smart-quotes ()
  1087. "Test `org-export-activate-smart-quotes' specifications."
  1088. ;; Opening double quotes: standard test.
  1089. (should
  1090. (equal
  1091. '("some &ldquo;paragraph")
  1092. (let ((org-export-default-language "en"))
  1093. (org-test-with-parsed-data "some \"paragraph"
  1094. (org-element-map
  1095. tree 'plain-text
  1096. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1097. info)))))
  1098. ;; Opening quotes: at the beginning of a paragraph.
  1099. (should
  1100. (equal
  1101. '("&ldquo;begin")
  1102. (let ((org-export-default-language "en"))
  1103. (org-test-with-parsed-data "\"begin"
  1104. (org-element-map
  1105. tree 'plain-text
  1106. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1107. info)))))
  1108. ;; Opening quotes: after an object.
  1109. (should
  1110. (equal
  1111. '("&ldquo;begin")
  1112. (let ((org-export-default-language "en"))
  1113. (org-test-with-parsed-data "=verb= \"begin"
  1114. (org-element-map
  1115. tree 'plain-text
  1116. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1117. info)))))
  1118. ;; Closing quotes: standard test.
  1119. (should
  1120. (equal
  1121. '("some&rdquo; paragraph")
  1122. (let ((org-export-default-language "en"))
  1123. (org-test-with-parsed-data "some\" paragraph"
  1124. (org-element-map
  1125. tree 'plain-text
  1126. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1127. info)))))
  1128. ;; Closing quotes: at the end of a paragraph.
  1129. (should
  1130. (equal
  1131. '("end&rdquo;")
  1132. (let ((org-export-default-language "en"))
  1133. (org-test-with-parsed-data "end\""
  1134. (org-element-map
  1135. tree 'plain-text
  1136. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1137. info)))))
  1138. ;; Apostrophe: standard test.
  1139. (should
  1140. (equal
  1141. '("It shouldn&rsquo;t fail")
  1142. (let ((org-export-default-language "en"))
  1143. (org-test-with-parsed-data "It shouldn't fail"
  1144. (org-element-map
  1145. tree 'plain-text
  1146. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1147. info)))))
  1148. ;; Apostrophe: before an object.
  1149. (should
  1150. (equal
  1151. '("a&rsquo;")
  1152. (let ((org-export-default-language "en"))
  1153. (org-test-with-parsed-data "a'=b="
  1154. (org-element-map
  1155. tree 'plain-text
  1156. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1157. info)))))
  1158. ;; Apostrophe: after an object.
  1159. (should
  1160. (equal
  1161. '("&rsquo;s")
  1162. (let ((org-export-default-language "en"))
  1163. (org-test-with-parsed-data "=code='s"
  1164. (org-element-map
  1165. tree 'plain-text
  1166. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1167. info)))))
  1168. ;; Special case: isolated quotes.
  1169. (should
  1170. (equal '("&ldquo;" "&rdquo;")
  1171. (let ((org-export-default-language "en"))
  1172. (org-test-with-parsed-data "\"$x$\""
  1173. (org-element-map
  1174. tree 'plain-text
  1175. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1176. info)))))
  1177. ;; Smart quotes in secondary strings.
  1178. (should
  1179. (equal '("&ldquo;" "&rdquo;")
  1180. (let ((org-export-default-language "en"))
  1181. (org-test-with-parsed-data "* \"$x$\""
  1182. (org-element-map
  1183. tree 'plain-text
  1184. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1185. info)))))
  1186. ;; Smart quotes in document keywords.
  1187. (should
  1188. (equal '("&ldquo;" "&rdquo;")
  1189. (let ((org-export-default-language "en"))
  1190. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1191. (org-element-map
  1192. (plist-get info :title) 'plain-text
  1193. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1194. info)))))
  1195. ;; Smart quotes in parsed affiliated keywords.
  1196. (should
  1197. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1198. (let ((org-export-default-language "en"))
  1199. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1200. (org-element-map
  1201. tree 'plain-text
  1202. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1203. info nil nil t))))))
  1204. ;;; Tables
  1205. (ert-deftest test-org-export/special-column ()
  1206. "Test if the table's special column is properly recognized."
  1207. ;; 1. First column is special if it contains only a special marking
  1208. ;; characters or empty cells.
  1209. (org-test-with-temp-text "
  1210. | ! | 1 |
  1211. | | 2 |"
  1212. (should
  1213. (org-export-table-has-special-column-p
  1214. (org-element-map
  1215. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1216. ;; 2. If the column contains anything else, it isn't special.
  1217. (org-test-with-temp-text "
  1218. | ! | 1 |
  1219. | b | 2 |"
  1220. (should-not
  1221. (org-export-table-has-special-column-p
  1222. (org-element-map
  1223. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1224. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1225. ;; and "!".
  1226. (org-test-with-temp-text "
  1227. | # | 1 |
  1228. | ^ | 2 |
  1229. | * | 3 |
  1230. | _ | 4 |
  1231. | / | 5 |
  1232. | $ | 6 |
  1233. | ! | 7 |"
  1234. (should
  1235. (org-export-table-has-special-column-p
  1236. (org-element-map
  1237. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1238. ;; 4. A first column with only empty cells isn't considered as
  1239. ;; special.
  1240. (org-test-with-temp-text "
  1241. | | 1 |
  1242. | | 2 |"
  1243. (should-not
  1244. (org-export-table-has-special-column-p
  1245. (org-element-map
  1246. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1247. (ert-deftest test-org-export/table-row-is-special-p ()
  1248. "Test `org-export-table-row-is-special-p' specifications."
  1249. ;; 1. A row is special if it has a special marking character in the
  1250. ;; special column.
  1251. (org-test-with-parsed-data "| ! | 1 |"
  1252. (should
  1253. (org-export-table-row-is-special-p
  1254. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1255. ;; 2. A row is special when its first field is "/"
  1256. (org-test-with-parsed-data "
  1257. | / | 1 |
  1258. | a | b |"
  1259. (should
  1260. (org-export-table-row-is-special-p
  1261. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1262. ;; 3. A row only containing alignment cookies is also considered as
  1263. ;; special.
  1264. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1265. (should
  1266. (org-export-table-row-is-special-p
  1267. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1268. ;; 4. Everything else isn't considered as special.
  1269. (org-test-with-parsed-data "| \alpha | | c |"
  1270. (should-not
  1271. (org-export-table-row-is-special-p
  1272. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1273. ;; 5. Table's rules are never considered as special rows.
  1274. (org-test-with-parsed-data "|---+---|"
  1275. (should-not
  1276. (org-export-table-row-is-special-p
  1277. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1278. (ert-deftest test-org-export/has-header-p ()
  1279. "Test `org-export-table-has-header-p' specifications."
  1280. ;; 1. With an header.
  1281. (org-test-with-parsed-data "
  1282. | a | b |
  1283. |---+---|
  1284. | c | d |"
  1285. (should
  1286. (org-export-table-has-header-p
  1287. (org-element-map tree 'table 'identity info 'first-match)
  1288. info)))
  1289. ;; 2. Without an header.
  1290. (org-test-with-parsed-data "
  1291. | a | b |
  1292. | c | d |"
  1293. (should-not
  1294. (org-export-table-has-header-p
  1295. (org-element-map tree 'table 'identity info 'first-match)
  1296. info)))
  1297. ;; 3. Don't get fooled with starting and ending rules.
  1298. (org-test-with-parsed-data "
  1299. |---+---|
  1300. | a | b |
  1301. | c | d |
  1302. |---+---|"
  1303. (should-not
  1304. (org-export-table-has-header-p
  1305. (org-element-map tree 'table 'identity info 'first-match)
  1306. info))))
  1307. (ert-deftest test-org-export/table-row-group ()
  1308. "Test `org-export-table-row-group' specifications."
  1309. ;; 1. A rule creates a new group.
  1310. (org-test-with-parsed-data "
  1311. | a | b |
  1312. |---+---|
  1313. | 1 | 2 |"
  1314. (should
  1315. (equal
  1316. '(1 nil 2)
  1317. (mapcar (lambda (row) (org-export-table-row-group row info))
  1318. (org-element-map tree 'table-row 'identity)))))
  1319. ;; 2. Special rows are ignored in count.
  1320. (org-test-with-parsed-data "
  1321. | / | < | > |
  1322. |---|---+---|
  1323. | | 1 | 2 |"
  1324. (should
  1325. (equal
  1326. '(nil nil 1)
  1327. (mapcar (lambda (row) (org-export-table-row-group row info))
  1328. (org-element-map tree 'table-row 'identity)))))
  1329. ;; 3. Double rules also are ignored in count.
  1330. (org-test-with-parsed-data "
  1331. | a | b |
  1332. |---+---|
  1333. |---+---|
  1334. | 1 | 2 |"
  1335. (should
  1336. (equal
  1337. '(1 nil nil 2)
  1338. (mapcar (lambda (row) (org-export-table-row-group row info))
  1339. (org-element-map tree 'table-row 'identity))))))
  1340. (ert-deftest test-org-export/table-cell-width ()
  1341. "Test `org-export-table-cell-width' specifications."
  1342. ;; 1. Width is primarily determined by width cookies. If no cookie
  1343. ;; is found, cell's width is nil.
  1344. (org-test-with-parsed-data "
  1345. | / | <l> | <6> | <l7> |
  1346. | | a | b | c |"
  1347. (should
  1348. (equal
  1349. '(nil 6 7)
  1350. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1351. (org-element-map tree 'table-cell 'identity info)))))
  1352. ;; 2. The last width cookie has precedence.
  1353. (org-test-with-parsed-data "
  1354. | <6> |
  1355. | <7> |
  1356. | a |"
  1357. (should
  1358. (equal
  1359. '(7)
  1360. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1361. (org-element-map tree 'table-cell 'identity info)))))
  1362. ;; 3. Valid width cookies must have a specific row.
  1363. (org-test-with-parsed-data "| <6> | cell |"
  1364. (should
  1365. (equal
  1366. '(nil nil)
  1367. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1368. (org-element-map tree 'table-cell 'identity))))))
  1369. (ert-deftest test-org-export/table-cell-alignment ()
  1370. "Test `org-export-table-cell-alignment' specifications."
  1371. (let ((org-table-number-fraction 0.5)
  1372. (org-table-number-regexp "^[0-9]+$"))
  1373. ;; 1. Alignment is primarily determined by alignment cookies.
  1374. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1375. (let* ((tree (org-element-parse-buffer))
  1376. (info `(:parse-tree ,tree)))
  1377. (should
  1378. (equal
  1379. '(left center right)
  1380. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1381. (org-element-map tree 'table-cell 'identity))))))
  1382. ;; 2. The last alignment cookie has precedence.
  1383. (org-test-with-parsed-data "
  1384. | <l8> |
  1385. | cell |
  1386. | <r9> |"
  1387. (should
  1388. (equal
  1389. '(right right right)
  1390. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1391. (org-element-map tree 'table-cell 'identity)))))
  1392. ;; 3. If there's no cookie, cell's contents determine alignment.
  1393. ;; A column mostly made of cells containing numbers will align
  1394. ;; its cells to the right.
  1395. (org-test-with-parsed-data "
  1396. | 123 |
  1397. | some text |
  1398. | 12345 |"
  1399. (should
  1400. (equal
  1401. '(right right right)
  1402. (mapcar (lambda (cell)
  1403. (org-export-table-cell-alignment cell info))
  1404. (org-element-map tree 'table-cell 'identity)))))
  1405. ;; 4. Otherwise, they will be aligned to the left.
  1406. (org-test-with-parsed-data "
  1407. | text |
  1408. | some text |
  1409. | \alpha |"
  1410. (should
  1411. (equal
  1412. '(left left left)
  1413. (mapcar (lambda (cell)
  1414. (org-export-table-cell-alignment cell info))
  1415. (org-element-map tree 'table-cell 'identity)))))))
  1416. (ert-deftest test-org-export/table-cell-borders ()
  1417. "Test `org-export-table-cell-borders' specifications."
  1418. ;; 1. Recognize various column groups indicators.
  1419. (org-test-with-parsed-data "| / | < | > | <> |"
  1420. (should
  1421. (equal
  1422. '((right bottom top) (left bottom top) (right bottom top)
  1423. (right left bottom top))
  1424. (mapcar (lambda (cell)
  1425. (org-export-table-cell-borders cell info))
  1426. (org-element-map tree 'table-cell 'identity)))))
  1427. ;; 2. Accept shortcuts to define column groups.
  1428. (org-test-with-parsed-data "| / | < | < |"
  1429. (should
  1430. (equal
  1431. '((right bottom top) (right left bottom top) (left bottom top))
  1432. (mapcar (lambda (cell)
  1433. (org-export-table-cell-borders cell info))
  1434. (org-element-map tree 'table-cell 'identity)))))
  1435. ;; 3. A valid column groups row must start with a "/".
  1436. (org-test-with-parsed-data "
  1437. | | < |
  1438. | a | b |"
  1439. (should
  1440. (equal '((top) (top) (bottom) (bottom))
  1441. (mapcar (lambda (cell)
  1442. (org-export-table-cell-borders cell info))
  1443. (org-element-map tree 'table-cell 'identity)))))
  1444. ;; 4. Take table rules into consideration.
  1445. (org-test-with-parsed-data "
  1446. | 1 |
  1447. |---|
  1448. | 2 |"
  1449. (should
  1450. (equal '((below top) (bottom above))
  1451. (mapcar (lambda (cell)
  1452. (org-export-table-cell-borders cell info))
  1453. (org-element-map tree 'table-cell 'identity)))))
  1454. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1455. ;; (resp. `bottom' and `below') borders. Any special row is
  1456. ;; ignored.
  1457. (org-test-with-parsed-data "
  1458. |---+----|
  1459. | / | |
  1460. | | 1 |
  1461. |---+----|"
  1462. (should
  1463. (equal '((bottom below top above))
  1464. (last
  1465. (mapcar (lambda (cell)
  1466. (org-export-table-cell-borders cell info))
  1467. (org-element-map tree 'table-cell 'identity)))))))
  1468. (ert-deftest test-org-export/table-dimensions ()
  1469. "Test `org-export-table-dimensions' specifications."
  1470. ;; 1. Standard test.
  1471. (org-test-with-parsed-data "
  1472. | 1 | 2 | 3 |
  1473. | 4 | 5 | 6 |"
  1474. (should
  1475. (equal '(2 . 3)
  1476. (org-export-table-dimensions
  1477. (org-element-map tree 'table 'identity info 'first-match) info))))
  1478. ;; 2. Ignore horizontal rules and special columns.
  1479. (org-test-with-parsed-data "
  1480. | / | < | > |
  1481. | 1 | 2 | 3 |
  1482. |---+---+---|
  1483. | 4 | 5 | 6 |"
  1484. (should
  1485. (equal '(2 . 3)
  1486. (org-export-table-dimensions
  1487. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1488. (ert-deftest test-org-export/table-cell-address ()
  1489. "Test `org-export-table-cell-address' specifications."
  1490. ;; 1. Standard test: index is 0-based.
  1491. (org-test-with-parsed-data "| a | b |"
  1492. (should
  1493. (equal '((0 . 0) (0 . 1))
  1494. (org-element-map
  1495. tree 'table-cell
  1496. (lambda (cell) (org-export-table-cell-address cell info))
  1497. info))))
  1498. ;; 2. Special column isn't counted, nor are special rows.
  1499. (org-test-with-parsed-data "
  1500. | / | <> |
  1501. | | c |"
  1502. (should
  1503. (equal '(0 . 0)
  1504. (org-export-table-cell-address
  1505. (car (last (org-element-map tree 'table-cell 'identity info)))
  1506. info))))
  1507. ;; 3. Tables rules do not count either.
  1508. (org-test-with-parsed-data "
  1509. | a |
  1510. |---|
  1511. | b |
  1512. |---|
  1513. | c |"
  1514. (should
  1515. (equal '(2 . 0)
  1516. (org-export-table-cell-address
  1517. (car (last (org-element-map tree 'table-cell 'identity info)))
  1518. info))))
  1519. ;; 4. Return nil for special cells.
  1520. (org-test-with-parsed-data "| / | a |"
  1521. (should-not
  1522. (org-export-table-cell-address
  1523. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1524. info))))
  1525. (ert-deftest test-org-export/get-table-cell-at ()
  1526. "Test `org-export-get-table-cell-at' specifications."
  1527. ;; 1. Address ignores special columns, special rows and rules.
  1528. (org-test-with-parsed-data "
  1529. | / | <> |
  1530. | | a |
  1531. |---+----|
  1532. | | b |"
  1533. (should
  1534. (equal '("b")
  1535. (org-element-contents
  1536. (org-export-get-table-cell-at
  1537. '(1 . 0)
  1538. (org-element-map tree 'table 'identity info 'first-match)
  1539. info)))))
  1540. ;; 2. Return value for a non-existent address is nil.
  1541. (org-test-with-parsed-data "| a |"
  1542. (should-not
  1543. (org-export-get-table-cell-at
  1544. '(2 . 2)
  1545. (org-element-map tree 'table 'identity info 'first-match)
  1546. info)))
  1547. (org-test-with-parsed-data "| / |"
  1548. (should-not
  1549. (org-export-get-table-cell-at
  1550. '(0 . 0)
  1551. (org-element-map tree 'table 'identity info 'first-match)
  1552. info))))
  1553. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1554. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1555. ;; 1. A cell at a beginning of a row always starts a column group.
  1556. (org-test-with-parsed-data "| a |"
  1557. (should
  1558. (org-export-table-cell-starts-colgroup-p
  1559. (org-element-map tree 'table-cell 'identity info 'first-match)
  1560. info)))
  1561. ;; 2. Special column should be ignored when determining the
  1562. ;; beginning of the row.
  1563. (org-test-with-parsed-data "
  1564. | / | |
  1565. | | a |"
  1566. (should
  1567. (org-export-table-cell-starts-colgroup-p
  1568. (org-element-map tree 'table-cell 'identity info 'first-match)
  1569. info)))
  1570. ;; 2. Explicit column groups.
  1571. (org-test-with-parsed-data "
  1572. | / | | < |
  1573. | a | b | c |"
  1574. (should
  1575. (equal
  1576. '(yes no yes)
  1577. (org-element-map
  1578. tree 'table-cell
  1579. (lambda (cell)
  1580. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1581. info)))))
  1582. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1583. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1584. ;; 1. A cell at the end of a row always ends a column group.
  1585. (org-test-with-parsed-data "| a |"
  1586. (should
  1587. (org-export-table-cell-ends-colgroup-p
  1588. (org-element-map tree 'table-cell 'identity info 'first-match)
  1589. info)))
  1590. ;; 2. Special column should be ignored when determining the
  1591. ;; beginning of the row.
  1592. (org-test-with-parsed-data "
  1593. | / | |
  1594. | | a |"
  1595. (should
  1596. (org-export-table-cell-ends-colgroup-p
  1597. (org-element-map tree 'table-cell 'identity info 'first-match)
  1598. info)))
  1599. ;; 3. Explicit column groups.
  1600. (org-test-with-parsed-data "
  1601. | / | < | |
  1602. | a | b | c |"
  1603. (should
  1604. (equal
  1605. '(yes no yes)
  1606. (org-element-map
  1607. tree 'table-cell
  1608. (lambda (cell)
  1609. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1610. info)))))
  1611. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1612. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1613. ;; 1. A row at the beginning of a table always starts a row group.
  1614. ;; So does a row following a table rule.
  1615. (org-test-with-parsed-data "
  1616. | a |
  1617. |---|
  1618. | b |"
  1619. (should
  1620. (equal
  1621. '(yes no yes)
  1622. (org-element-map
  1623. tree 'table-row
  1624. (lambda (row)
  1625. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1626. info))))
  1627. ;; 2. Special rows should be ignored when determining the beginning
  1628. ;; of the row.
  1629. (org-test-with-parsed-data "
  1630. | / | < |
  1631. | | a |
  1632. |---+---|
  1633. | / | < |
  1634. | | b |"
  1635. (should
  1636. (equal
  1637. '(yes no yes)
  1638. (org-element-map
  1639. tree 'table-row
  1640. (lambda (row)
  1641. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1642. info)))))
  1643. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1644. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1645. ;; 1. A row at the end of a table always ends a row group. So does
  1646. ;; a row preceding a table rule.
  1647. (org-test-with-parsed-data "
  1648. | a |
  1649. |---|
  1650. | b |"
  1651. (should
  1652. (equal
  1653. '(yes no yes)
  1654. (org-element-map
  1655. tree 'table-row
  1656. (lambda (row)
  1657. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1658. info))))
  1659. ;; 2. Special rows should be ignored when determining the beginning
  1660. ;; of the row.
  1661. (org-test-with-parsed-data "
  1662. | | a |
  1663. | / | < |
  1664. |---+---|
  1665. | | b |
  1666. | / | < |"
  1667. (should
  1668. (equal
  1669. '(yes no yes)
  1670. (org-element-map
  1671. tree 'table-row
  1672. (lambda (row)
  1673. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1674. info)))))
  1675. (ert-deftest test-org-export/table-row-starts-header-p ()
  1676. "Test `org-export-table-row-starts-header-p' specifications."
  1677. ;; 1. Only the row starting the first row group starts the table
  1678. ;; header.
  1679. (org-test-with-parsed-data "
  1680. | a |
  1681. | b |
  1682. |---|
  1683. | c |"
  1684. (should
  1685. (equal
  1686. '(yes no no no)
  1687. (org-element-map
  1688. tree 'table-row
  1689. (lambda (row)
  1690. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1691. info))))
  1692. ;; 2. A row cannot start an header if there's no header in the
  1693. ;; table.
  1694. (org-test-with-parsed-data "
  1695. | a |
  1696. |---|"
  1697. (should-not
  1698. (org-export-table-row-starts-header-p
  1699. (org-element-map tree 'table-row 'identity info 'first-match)
  1700. info))))
  1701. (ert-deftest test-org-export/table-row-ends-header-p ()
  1702. "Test `org-export-table-row-ends-header-p' specifications."
  1703. ;; 1. Only the row starting the first row group starts the table
  1704. ;; header.
  1705. (org-test-with-parsed-data "
  1706. | a |
  1707. | b |
  1708. |---|
  1709. | c |"
  1710. (should
  1711. (equal
  1712. '(no yes no no)
  1713. (org-element-map
  1714. tree 'table-row
  1715. (lambda (row)
  1716. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1717. info))))
  1718. ;; 2. A row cannot start an header if there's no header in the
  1719. ;; table.
  1720. (org-test-with-parsed-data "
  1721. | a |
  1722. |---|"
  1723. (should-not
  1724. (org-export-table-row-ends-header-p
  1725. (org-element-map tree 'table-row 'identity info 'first-match)
  1726. info))))
  1727. ;;; Topology
  1728. (ert-deftest test-org-export/get-next-element ()
  1729. "Test `org-export-get-next-element' specifications."
  1730. ;; Standard test.
  1731. (should
  1732. (equal "b"
  1733. (org-test-with-parsed-data "* Headline\n*a* b"
  1734. (org-export-get-next-element
  1735. (org-element-map tree 'bold 'identity info t) info))))
  1736. ;; Return nil when no previous element.
  1737. (should-not
  1738. (org-test-with-parsed-data "* Headline\na *b*"
  1739. (org-export-get-next-element
  1740. (org-element-map tree 'bold 'identity info t) info)))
  1741. ;; Non-exportable elements are ignored.
  1742. (should-not
  1743. (let ((org-export-with-timestamps nil))
  1744. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  1745. (org-export-get-next-element
  1746. (org-element-map tree 'entity 'identity info t) info))))
  1747. ;; Find next element in secondary strings.
  1748. (should
  1749. (eq 'verbatim
  1750. (org-test-with-parsed-data "* a =verb="
  1751. (org-element-type
  1752. (org-export-get-next-element
  1753. (org-element-map tree 'plain-text 'identity info t) info)))))
  1754. ;; Find next element in document keywords.
  1755. (should
  1756. (eq 'verbatim
  1757. (org-test-with-parsed-data "#+TITLE: a =verb="
  1758. (org-element-type
  1759. (org-export-get-next-element
  1760. (org-element-map
  1761. (plist-get info :title) 'plain-text 'identity info t) info)))))
  1762. ;; Find next element in parsed affiliated keywords.
  1763. (should
  1764. (eq 'verbatim
  1765. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  1766. (org-element-type
  1767. (org-export-get-next-element
  1768. (org-element-map tree 'plain-text 'identity info t nil t) info))))))
  1769. (ert-deftest test-org-export/get-previous-element ()
  1770. "Test `org-export-get-previous-element' specifications."
  1771. ;; Standard test.
  1772. (should
  1773. (equal "a "
  1774. (org-test-with-parsed-data "* Headline\na *b*"
  1775. (org-export-get-previous-element
  1776. (org-element-map tree 'bold 'identity info t) info))))
  1777. ;; Return nil when no previous element.
  1778. (should-not
  1779. (org-test-with-parsed-data "* Headline\n*a* b"
  1780. (org-export-get-previous-element
  1781. (org-element-map tree 'bold 'identity info t) info)))
  1782. ;; Non-exportable elements are ignored.
  1783. (should-not
  1784. (let ((org-export-with-timestamps nil))
  1785. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  1786. (org-export-get-previous-element
  1787. (org-element-map tree 'entity 'identity info t) info))))
  1788. ;; Find previous element in secondary strings.
  1789. (should
  1790. (eq 'verbatim
  1791. (org-test-with-parsed-data "* =verb= a"
  1792. (org-element-type
  1793. (org-export-get-previous-element
  1794. (org-element-map tree 'plain-text 'identity info t) info)))))
  1795. ;; Find previous element in document keywords.
  1796. (should
  1797. (eq 'verbatim
  1798. (org-test-with-parsed-data "#+TITLE: =verb= a"
  1799. (org-element-type
  1800. (org-export-get-previous-element
  1801. (org-element-map
  1802. (plist-get info :title) 'plain-text 'identity info t) info)))))
  1803. ;; Find previous element in parsed affiliated keywords.
  1804. (should
  1805. (eq 'verbatim
  1806. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  1807. (org-element-type
  1808. (org-export-get-previous-element
  1809. (org-element-map tree 'plain-text 'identity info t nil t) info))))))
  1810. (provide 'test-org-export)
  1811. ;;; test-org-export.el end here