test-org-export.el 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  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. ;;; Tests
  45. (ert-deftest test-org-export/parse-option-keyword ()
  46. "Test reading all standard #+OPTIONS: items."
  47. (should
  48. (equal
  49. (org-export--parse-option-keyword
  50. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  51. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
  52. '(:headline-levels
  53. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  54. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  55. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  56. :with-footnotes t :with-priority t :with-special-strings t
  57. :with-sub-superscript t :with-toc t :with-tables t :with-tags t
  58. :with-tasks t :with-timestamps t :with-todo-keywords t)))
  59. ;; Test some special values.
  60. (should
  61. (equal
  62. (org-export--parse-option-keyword
  63. "arch:headline creator:comment d:(\"TEST\")
  64. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  65. '( :section-numbers
  66. 2
  67. :with-archived-trees headline :with-creator comment
  68. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  69. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  70. (ert-deftest test-org-export/get-inbuffer-options ()
  71. "Test reading all standard export keywords."
  72. (should
  73. (equal
  74. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  75. #+CREATOR: Idem
  76. #+DATE: Today
  77. #+DESCRIPTION: Testing
  78. #+DESCRIPTION: with two lines
  79. #+EMAIL: some@email.org
  80. #+EXCLUDE_TAGS: noexport invisible
  81. #+KEYWORDS: test
  82. #+LANGUAGE: en
  83. #+SELECT_TAGS: export
  84. #+TITLE: Some title
  85. #+TITLE: with spaces"
  86. (org-export--get-inbuffer-options))
  87. '(:author
  88. ("Me, Myself and I") :creator "Idem" :date ("Today")
  89. :description "Testing\nwith two lines" :email "some@email.org"
  90. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  91. :select-tags ("export") :title ("Some title with spaces")))))
  92. (ert-deftest test-org-export/get-subtree-options ()
  93. "Test setting options from headline's properties."
  94. ;; EXPORT_TITLE.
  95. (org-test-with-temp-text "#+TITLE: Title
  96. * Headline
  97. :PROPERTIES:
  98. :EXPORT_TITLE: Subtree Title
  99. :END:
  100. Paragraph"
  101. (forward-line)
  102. (should (equal (plist-get (org-export-get-environment nil t) :title)
  103. '("Subtree Title"))))
  104. :title
  105. '("subtree-title")
  106. ;; EXPORT_OPTIONS.
  107. (org-test-with-temp-text "#+OPTIONS: H:1
  108. * Headline
  109. :PROPERTIES:
  110. :EXPORT_OPTIONS: H:2
  111. :END:
  112. Paragraph"
  113. (forward-line)
  114. (should
  115. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  116. ;; EXPORT DATE.
  117. (org-test-with-temp-text "#+DATE: today
  118. * Headline
  119. :PROPERTIES:
  120. :EXPORT_DATE: 29-03-2012
  121. :END:
  122. Paragraph"
  123. (forward-line)
  124. (should (equal (plist-get (org-export-get-environment nil t) :date)
  125. '("29-03-2012")))))
  126. (ert-deftest test-org-export/handle-options ()
  127. "Test if export options have an impact on output."
  128. ;; Test exclude tags.
  129. (org-test-with-temp-text "* Head1 :noexport:"
  130. (org-test-with-backend test
  131. (should
  132. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  133. ""))))
  134. ;; Test include tags.
  135. (org-test-with-temp-text "
  136. * Head1
  137. * Head2
  138. ** Sub-Head2.1 :export:
  139. *** Sub-Head2.1.1
  140. * Head2"
  141. (org-test-with-backend test
  142. (should
  143. (equal
  144. "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
  145. (let ((org-tags-column 0))
  146. (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
  147. ;; Test mixing include tags and exclude tags.
  148. (org-test-with-temp-text "
  149. * Head1 :export:
  150. ** Sub-Head1 :noexport:
  151. ** Sub-Head2
  152. * Head2 :noexport:
  153. ** Sub-Head1 :export:"
  154. (org-test-with-backend test
  155. (should
  156. (string-match
  157. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  158. (org-export-as
  159. 'test nil nil nil
  160. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  161. ;; Ignore tasks.
  162. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  163. (org-test-with-temp-text "* TODO Head1"
  164. (org-test-with-backend test
  165. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  166. "")))))
  167. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  168. (org-test-with-temp-text "* TODO Head1"
  169. (org-test-with-backend test
  170. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  171. "* TODO Head1\n")))))
  172. ;; Archived tree.
  173. (org-test-with-temp-text "* Head1 :archive:"
  174. (let ((org-archive-tag "archive"))
  175. (org-test-with-backend test
  176. (should
  177. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  178. "")))))
  179. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  180. (let ((org-archive-tag "archive"))
  181. (org-test-with-backend test
  182. (should
  183. (string-match
  184. "\\* Head1[ \t]+:archive:"
  185. (org-export-as 'test nil nil nil
  186. '(:with-archived-trees headline)))))))
  187. (org-test-with-temp-text "* Head1 :archive:"
  188. (let ((org-archive-tag "archive"))
  189. (org-test-with-backend test
  190. (should
  191. (string-match
  192. "\\`\\* Head1[ \t]+:archive:\n\\'"
  193. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  194. ;; Drawers.
  195. (let ((org-drawers '("TEST")))
  196. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  197. (org-test-with-backend test
  198. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  199. ""))
  200. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  201. ":TEST:\ncontents\n:END:\n")))))
  202. (let ((org-drawers '("FOO" "BAR")))
  203. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  204. (org-test-with-backend test
  205. (should
  206. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  207. ":FOO:\nkeep\n:END:\n")))))
  208. ;; Timestamps.
  209. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  210. (org-test-with-backend test
  211. (should
  212. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  213. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  214. (should
  215. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  216. (should
  217. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  218. "<2012-04-29 sun. 10:45>\n"))
  219. (should
  220. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  221. "[2012-04-29 sun. 10:45]\n"))))
  222. ;; Clocks.
  223. (let ((org-clock-string "CLOCK:"))
  224. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  225. (org-test-with-backend test
  226. (should
  227. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  228. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  229. (should
  230. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  231. ;; Plannings.
  232. (let ((org-closed-string "CLOSED:"))
  233. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  234. (org-test-with-backend test
  235. (should
  236. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  237. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  238. (should
  239. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  240. ""))))))
  241. (ert-deftest test-org-export/comment-tree ()
  242. "Test if export process ignores commented trees."
  243. (let ((org-comment-string "COMMENT"))
  244. (org-test-with-temp-text "* COMMENT Head1"
  245. (org-test-with-backend test
  246. (should (equal (org-export-as 'test) ""))))))
  247. (ert-deftest test-org-export/export-scope ()
  248. "Test all export scopes."
  249. (org-test-with-temp-text "
  250. * Head1
  251. ** Head2
  252. text
  253. *** Head3"
  254. (org-test-with-backend test
  255. ;; Subtree.
  256. (forward-line 3)
  257. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  258. ;; Visible.
  259. (goto-char (point-min))
  260. (forward-line)
  261. (org-cycle)
  262. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  263. ;; Body only.
  264. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  265. (push '(template . org-test-template) org-test-translate-alist)
  266. (should (equal (org-export-as 'test nil nil 'body-only)
  267. "* Head1\n** Head2\ntext\n*** Head3\n"))
  268. (should (equal (org-export-as 'test)
  269. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  270. ;; Region.
  271. (goto-char (point-min))
  272. (forward-line 3)
  273. (transient-mark-mode 1)
  274. (push-mark (point) t t)
  275. (goto-char (point-at-eol))
  276. (should (equal (org-export-as 'test) "text\n"))))
  277. ;; Subtree with a code block calling another block outside.
  278. (org-test-with-temp-text "
  279. * Head1
  280. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  281. <<test>>
  282. #+END_SRC
  283. * Head2
  284. #+NAME: test
  285. #+BEGIN_SRC emacs-lisp
  286. \(+ 1 2)
  287. #+END_SRC"
  288. (org-test-with-backend test
  289. (forward-line 1)
  290. (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
  291. (ert-deftest test-org-export/expand-include ()
  292. "Test file inclusion in an Org buffer."
  293. ;; Full insertion with recursive inclusion.
  294. (org-test-with-temp-text
  295. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  296. (org-export-expand-include-keyword)
  297. (should (equal (buffer-string)
  298. "Small Org file with an include keyword.
  299. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  300. Success!
  301. * Heading
  302. body\n")))
  303. ;; Localized insertion.
  304. (org-test-with-temp-text
  305. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  306. org-test-dir)
  307. (org-export-expand-include-keyword)
  308. (should (equal (buffer-string)
  309. "Small Org file with an include keyword.\n")))
  310. ;; Insertion with constraints on headlines level.
  311. (org-test-with-temp-text
  312. (format
  313. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  314. org-test-dir)
  315. (org-export-expand-include-keyword)
  316. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  317. ;; Inclusion within an example block.
  318. (org-test-with-temp-text
  319. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  320. org-test-dir)
  321. (org-export-expand-include-keyword)
  322. (should
  323. (equal
  324. (buffer-string)
  325. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  326. ;; Inclusion within a src-block.
  327. (org-test-with-temp-text
  328. (format
  329. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  330. org-test-dir)
  331. (org-export-expand-include-keyword)
  332. (should (equal (buffer-string)
  333. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  334. (ert-deftest test-org-export/user-ignore-list ()
  335. "Test if `:ignore-list' accepts user input."
  336. (org-test-with-backend test
  337. (flet ((skip-note-head
  338. (data backend info)
  339. ;; Ignore headlines with the word "note" in their title.
  340. (org-element-map
  341. data 'headline
  342. (lambda (headline)
  343. (when (string-match "\\<note\\>"
  344. (org-element-property :raw-value headline))
  345. (org-export-ignore-element headline info)))
  346. info)
  347. data))
  348. ;; Install function in parse tree filters.
  349. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  350. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  351. (should (equal (org-export-as 'test) "* Head1\n")))))))
  352. (ert-deftest test-org-export/before-parsing-hook ()
  353. "Test `org-export-before-parsing-hook'."
  354. (org-test-with-backend test
  355. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  356. (let ((org-export-before-parsing-hook
  357. '((lambda ()
  358. (org-map-entries
  359. (lambda ()
  360. (delete-region (point) (progn (forward-line) (point)))))))))
  361. (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
  362. ;;; Affiliated Keywords
  363. (ert-deftest test-org-export/read-attribute ()
  364. "Test `org-export-read-attribute' specifications."
  365. ;; Standard test.
  366. (should
  367. (equal
  368. (org-export-read-attribute
  369. :attr_html
  370. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  371. (org-element-at-point)))
  372. '(:a 1 :b 2)))
  373. ;; Return nil on empty attribute.
  374. (should-not
  375. (org-export-read-attribute
  376. :attr_html
  377. (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
  378. ;;; Export Snippets
  379. (ert-deftest test-org-export/export-snippet ()
  380. "Test export snippets transcoding."
  381. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  382. (org-test-with-backend test
  383. (let ((org-test-translate-alist
  384. (cons (cons 'export-snippet
  385. (lambda (snippet contents info)
  386. (when (eq (org-export-snippet-backend snippet) 'test)
  387. (org-element-property :value snippet))))
  388. org-test-translate-alist)))
  389. (let ((org-export-snippet-translation-alist nil))
  390. (should (equal (org-export-as 'test) "A\n")))
  391. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  392. (should (equal (org-export-as 'test) "AB\n")))))))
  393. ;;; Footnotes
  394. (ert-deftest test-org-export/footnotes ()
  395. "Test footnotes specifications."
  396. (let ((org-footnote-section nil)
  397. (org-export-with-footnotes t))
  398. ;; 1. Read every type of footnote.
  399. (org-test-with-parsed-data
  400. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  401. (should
  402. (equal
  403. '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
  404. (org-element-map
  405. tree 'footnote-reference
  406. (lambda (ref)
  407. (let ((def (org-export-get-footnote-definition ref info)))
  408. (cons (org-export-get-footnote-number ref info)
  409. (if (eq (org-element-property :type ref) 'inline) (car def)
  410. (car (org-element-contents
  411. (car (org-element-contents def))))))))
  412. info))))
  413. ;; 2. Test nested footnotes order.
  414. (org-test-with-parsed-data
  415. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  416. (should
  417. (equal
  418. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  419. (org-element-map
  420. tree 'footnote-reference
  421. (lambda (ref)
  422. (when (org-export-footnote-first-reference-p ref info)
  423. (cons (org-export-get-footnote-number ref info)
  424. (org-element-property :label ref))))
  425. info))))
  426. ;; 3. Test nested footnote in invisible definitions.
  427. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  428. ;; Hide definitions.
  429. (narrow-to-region (point) (point-at-eol))
  430. (let* ((tree (org-element-parse-buffer))
  431. (info (org-combine-plists
  432. `(:parse-tree ,tree)
  433. (org-export-collect-tree-properties
  434. tree (org-export-get-environment)))))
  435. ;; Both footnotes should be seen.
  436. (should
  437. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  438. ;; 4. Test footnotes definitions collection.
  439. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  440. \[fn:2] B [fn:3] [fn::D].
  441. \[fn:3] C."
  442. (should (= (length (org-export-collect-footnote-definitions tree info))
  443. 4)))
  444. ;; 5. Test export of footnotes defined outside parsing scope.
  445. (org-test-with-temp-text "[fn:1] Out of scope
  446. * Title
  447. Paragraph[fn:1]"
  448. (org-test-with-backend test
  449. (let ((org-test-translate-alist
  450. (cons (cons 'footnote-reference
  451. (lambda (fn contents info)
  452. (org-element-interpret-data
  453. (org-export-get-footnote-definition fn info))))
  454. org-test-translate-alist)))
  455. (forward-line)
  456. (should (equal "ParagraphOut of scope\n"
  457. (org-export-as 'test 'subtree))))))))
  458. ;;; Headlines and Inlinetasks
  459. (ert-deftest test-org-export/get-relative-level ()
  460. "Test `org-export-get-relative-level' specifications."
  461. ;; Standard test.
  462. (should
  463. (equal '(1 2)
  464. (let ((org-odd-levels-only nil))
  465. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  466. (org-element-map
  467. tree 'headline
  468. (lambda (h) (org-export-get-relative-level h info))
  469. info)))))
  470. ;; Missing levels
  471. (should
  472. (equal '(1 3)
  473. (let ((org-odd-levels-only nil))
  474. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  475. (org-element-map
  476. tree 'headline
  477. (lambda (h) (org-export-get-relative-level h info))
  478. info))))))
  479. (ert-deftest test-org-export/low-level-p ()
  480. "Test `org-export-low-level-p' specifications."
  481. (should
  482. (equal
  483. '(no yes)
  484. (let ((org-odd-levels-only nil))
  485. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  486. (org-element-map
  487. tree 'headline
  488. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  489. (plist-put info :headline-levels 1)))))))
  490. (ert-deftest test-org-export/get-headline-number ()
  491. "Test `org-export-get-headline-number' specifications."
  492. ;; Standard test.
  493. (should
  494. (equal
  495. '((1) (1 1))
  496. (let ((org-odd-levels-only nil))
  497. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  498. (org-element-map
  499. tree 'headline
  500. (lambda (h) (org-export-get-headline-number h info))
  501. info)))))
  502. ;; Missing levels are replaced with 0.
  503. (should
  504. (equal
  505. '((1) (1 0 1))
  506. (let ((org-odd-levels-only nil))
  507. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  508. (org-element-map
  509. tree 'headline
  510. (lambda (h) (org-export-get-headline-number h info))
  511. info))))))
  512. (ert-deftest test-org-export/numbered-headline-p ()
  513. "Test `org-export-numbered-headline-p' specifications."
  514. ;; If `:section-numbers' is nil, never number headlines.
  515. (should-not
  516. (org-test-with-parsed-data "* Headline"
  517. (org-element-map
  518. tree 'headline
  519. (lambda (h) (org-export-numbered-headline-p h info))
  520. (plist-put info :section-numbers nil))))
  521. ;; If `:section-numbers' is a number, only number headlines with
  522. ;; a level greater that it.
  523. (should
  524. (equal
  525. '(yes no)
  526. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  527. (org-element-map
  528. tree 'headline
  529. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  530. (plist-put info :section-numbers 1)))))
  531. ;; Otherwise, headlines are always numbered.
  532. (should
  533. (org-test-with-parsed-data "* Headline"
  534. (org-element-map
  535. tree 'headline
  536. (lambda (h) (org-export-numbered-headline-p h info))
  537. (plist-put info :section-numbers t)))))
  538. (ert-deftest test-org-export/number-to-roman ()
  539. "Test `org-export-number-to-roman' specifications."
  540. ;; If number is negative, return it as a string.
  541. (should (equal (org-export-number-to-roman -1) "-1"))
  542. ;; Otherwise, return it as a roman number.
  543. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  544. (ert-deftest test-org-export/get-tags ()
  545. "Test `org-export-get-tags' specifications."
  546. (let ((org-export-exclude-tags '("noexport"))
  547. (org-export-select-tags '("export")))
  548. ;; Standard test: tags which are not a select tag, an exclude tag,
  549. ;; or specified as optional argument shouldn't be ignored.
  550. (should
  551. (org-test-with-parsed-data "* Headline :tag:"
  552. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  553. info)))
  554. ;; Exclude tags are removed.
  555. (should-not
  556. (org-test-with-parsed-data "* Headline :noexport:"
  557. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  558. info)))
  559. ;; Select tags are removed.
  560. (should-not
  561. (org-test-with-parsed-data "* Headline :export:"
  562. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  563. info)))
  564. (should
  565. (equal
  566. '("tag")
  567. (org-test-with-parsed-data "* Headline :tag:export:"
  568. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  569. info))))
  570. ;; Tags provided in the optional argument are also ignored.
  571. (should-not
  572. (org-test-with-parsed-data "* Headline :ignore:"
  573. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  574. info '("ignore"))))))
  575. (ert-deftest test-org-export/first-sibling-p ()
  576. "Test `org-export-first-sibling-p' specifications."
  577. (should
  578. (equal
  579. '(yes yes no)
  580. (org-test-with-temp-text "* Headline\n** Headline 2\n** Headline 3"
  581. (org-element-map
  582. (org-element-parse-buffer) 'headline
  583. (lambda (h) (if (org-export-first-sibling-p h) 'yes 'no)))))))
  584. (ert-deftest test-org-export/last-sibling-p ()
  585. "Test `org-export-last-sibling-p' specifications."
  586. (should
  587. (equal
  588. '(yes no yes)
  589. (org-test-with-temp-text "* Headline\n** Headline 2\n** Headline 3"
  590. (org-element-map
  591. (org-element-parse-buffer) 'headline
  592. (lambda (h) (if (org-export-last-sibling-p h) 'yes 'no)))))))
  593. ;;; Links
  594. (ert-deftest test-org-export/get-coderef-format ()
  595. "Test `org-export-get-coderef-format' specifications."
  596. ;; A link without description returns "%s"
  597. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  598. "%s"))
  599. ;; Return "%s" when path is matched within description.
  600. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  601. "desc %s"))
  602. ;; Otherwise return description.
  603. (should (equal (org-export-get-coderef-format "path" "desc")
  604. "desc")))
  605. (ert-deftest test-org-export/inline-image-p ()
  606. "Test `org-export-inline-image-p' specifications."
  607. (should
  608. (org-export-inline-image-p
  609. (org-test-with-temp-text "[[#id]]"
  610. (org-element-map
  611. (org-element-parse-buffer) 'link 'identity nil t))
  612. '(("custom-id" . "id")))))
  613. (ert-deftest test-org-export/fuzzy-link ()
  614. "Test fuzzy links specifications."
  615. ;; 1. Links to invisible (keyword) targets should be ignored.
  616. (org-test-with-parsed-data
  617. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  618. (should-not
  619. (org-element-map
  620. tree 'link
  621. (lambda (link)
  622. (org-export-get-ordinal
  623. (org-export-resolve-fuzzy-link link info) info)) info)))
  624. ;; 2. Link to an headline should return headline's number.
  625. (org-test-with-parsed-data
  626. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  627. (should
  628. ;; Note: Headline's number is in fact a list of numbers.
  629. (equal '(2)
  630. (org-element-map
  631. tree 'link
  632. (lambda (link)
  633. (org-export-get-ordinal
  634. (org-export-resolve-fuzzy-link link info) info)) info t))))
  635. ;; 3. Link to a target in an item should return item's number.
  636. (org-test-with-parsed-data
  637. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  638. (should
  639. ;; Note: Item's number is in fact a list of numbers.
  640. (equal '(1 2)
  641. (org-element-map
  642. tree 'link
  643. (lambda (link)
  644. (org-export-get-ordinal
  645. (org-export-resolve-fuzzy-link link info) info)) info t))))
  646. ;; 4. Link to a target in a footnote should return footnote's
  647. ;; number.
  648. (org-test-with-parsed-data "
  649. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  650. (should
  651. (equal '(2 3)
  652. (org-element-map
  653. tree 'link
  654. (lambda (link)
  655. (org-export-get-ordinal
  656. (org-export-resolve-fuzzy-link link info) info)) info))))
  657. ;; 5. Link to a named element should return sequence number of that
  658. ;; element.
  659. (org-test-with-parsed-data
  660. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  661. (should
  662. (= 2
  663. (org-element-map
  664. tree 'link
  665. (lambda (link)
  666. (org-export-get-ordinal
  667. (org-export-resolve-fuzzy-link link info) info)) info t))))
  668. ;; 6. Link to a target not within an item, a table, a footnote
  669. ;; reference or definition should return section number.
  670. (org-test-with-parsed-data
  671. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  672. (should
  673. (equal '(2)
  674. (org-element-map
  675. tree 'link
  676. (lambda (link)
  677. (org-export-get-ordinal
  678. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  679. (ert-deftest test-org-export/resolve-coderef ()
  680. "Test `org-export-resolve-coderef' specifications."
  681. (let ((org-coderef-label-format "(ref:%s)"))
  682. ;; 1. A link to a "-n -k -r" block returns line number.
  683. (org-test-with-parsed-data
  684. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  685. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  686. (org-test-with-parsed-data
  687. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  688. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  689. ;; 2. A link to a "-n -r" block returns line number.
  690. (org-test-with-parsed-data
  691. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  692. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  693. (org-test-with-parsed-data
  694. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  695. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  696. ;; 3. A link to a "-n" block returns coderef.
  697. (org-test-with-parsed-data
  698. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  699. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  700. (org-test-with-parsed-data
  701. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  702. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  703. ;; 4. A link to a "-r" block returns line number.
  704. (org-test-with-parsed-data
  705. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  706. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  707. (org-test-with-parsed-data
  708. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  709. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  710. ;; 5. A link to a block without a switch returns coderef.
  711. (org-test-with-parsed-data
  712. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  713. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  714. (org-test-with-parsed-data
  715. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  716. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  717. ;; 6. Correctly handle continued line numbers. A "+n" switch
  718. ;; should resume numbering from previous block with numbered
  719. ;; lines, ignoring blocks not numbering lines in the process.
  720. ;; A "-n" switch resets count.
  721. (org-test-with-parsed-data "
  722. #+BEGIN_EXAMPLE -n
  723. Text.
  724. #+END_EXAMPLE
  725. #+BEGIN_SRC emacs-lisp
  726. \(- 1 1)
  727. #+END_SRC
  728. #+BEGIN_SRC emacs-lisp +n -r
  729. \(+ 1 1) (ref:addition)
  730. #+END_SRC
  731. #+BEGIN_EXAMPLE -n -r
  732. Another text. (ref:text)
  733. #+END_EXAMPLE"
  734. (should (= (org-export-resolve-coderef "addition" info) 2))
  735. (should (= (org-export-resolve-coderef "text" info) 1)))
  736. ;; 7. Recognize coderef with user-specified syntax.
  737. (org-test-with-parsed-data
  738. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  739. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  740. (ert-deftest test-org-export/resolve-fuzzy-link ()
  741. "Test `org-export-resolve-fuzzy-link' specifications."
  742. ;; 1. Match target objects.
  743. (org-test-with-parsed-data "<<target>> [[target]]"
  744. (should
  745. (org-export-resolve-fuzzy-link
  746. (org-element-map tree 'link 'identity info t) info)))
  747. ;; 2. Match target elements.
  748. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  749. (should
  750. (org-export-resolve-fuzzy-link
  751. (org-element-map tree 'link 'identity info t) info)))
  752. ;; 3. Match named elements.
  753. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  754. (should
  755. (org-export-resolve-fuzzy-link
  756. (org-element-map tree 'link 'identity info t) info)))
  757. ;; 4. Match exact headline's name.
  758. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  759. (should
  760. (org-export-resolve-fuzzy-link
  761. (org-element-map tree 'link 'identity info t) info)))
  762. ;; 5. Targets objects have priority over named elements and headline
  763. ;; titles.
  764. (org-test-with-parsed-data
  765. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  766. (should
  767. (eq 'target
  768. (org-element-type
  769. (org-export-resolve-fuzzy-link
  770. (org-element-map tree 'link 'identity info t) info)))))
  771. ;; 6. Named elements have priority over headline titles.
  772. (org-test-with-parsed-data
  773. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  774. (should
  775. (eq 'paragraph
  776. (org-element-type
  777. (org-export-resolve-fuzzy-link
  778. (org-element-map tree 'link 'identity info t) info)))))
  779. ;; 7. If link's path starts with a "*", only match headline titles,
  780. ;; though.
  781. (org-test-with-parsed-data
  782. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  783. (should
  784. (eq 'headline
  785. (org-element-type
  786. (org-export-resolve-fuzzy-link
  787. (org-element-map tree 'link 'identity info t) info)))))
  788. ;; 8. Return nil if no match.
  789. (org-test-with-parsed-data "[[target]]"
  790. (should-not
  791. (org-export-resolve-fuzzy-link
  792. (org-element-map tree 'link 'identity info t) info))))
  793. (ert-deftest test-org-export/resolve-id-link ()
  794. "Test `org-export-resolve-id-link' specifications."
  795. ;; 1. Regular test for custom-id link.
  796. (org-test-with-parsed-data "* Headline1
  797. :PROPERTIES:
  798. :CUSTOM-ID: test
  799. :END:
  800. * Headline 2
  801. \[[#test]]"
  802. (should
  803. (org-export-resolve-id-link
  804. (org-element-map tree 'link 'identity info t) info)))
  805. ;; 2. Failing test for custom-id link.
  806. (org-test-with-parsed-data "* Headline1
  807. :PROPERTIES:
  808. :CUSTOM-ID: test
  809. :END:
  810. * Headline 2
  811. \[[#no-match]]"
  812. (should-not
  813. (org-export-resolve-id-link
  814. (org-element-map tree 'link 'identity info t) info)))
  815. ;; 3. Test for internal id target.
  816. (org-test-with-parsed-data "* Headline1
  817. :PROPERTIES:
  818. :ID: aaaa
  819. :END:
  820. * Headline 2
  821. \[[id:aaaa]]"
  822. (should
  823. (org-export-resolve-id-link
  824. (org-element-map tree 'link 'identity info t) info)))
  825. ;; 4. Test for external id target.
  826. (org-test-with-parsed-data "[[id:aaaa]]"
  827. (should
  828. (org-export-resolve-id-link
  829. (org-element-map tree 'link 'identity info t)
  830. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  831. (ert-deftest test-org-export/resolve-radio-link ()
  832. "Test `org-export-resolve-radio-link' specifications."
  833. ;; Standard test.
  834. (org-test-with-temp-text "<<<radio>>> radio"
  835. (org-update-radio-target-regexp)
  836. (should
  837. (let* ((tree (org-element-parse-buffer))
  838. (info `(:parse-tree ,tree)))
  839. (org-export-resolve-radio-link
  840. (org-element-map tree 'link 'identity info t)
  841. info))))
  842. ;; Radio target with objects.
  843. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  844. (org-update-radio-target-regexp)
  845. (should
  846. (let* ((tree (org-element-parse-buffer))
  847. (info `(:parse-tree ,tree)))
  848. (org-export-resolve-radio-link
  849. (org-element-map tree 'link 'identity info t)
  850. info)))))
  851. ;;; Macro
  852. (ert-deftest test-org-export/define-macro ()
  853. "Try defining various Org macro using in-buffer #+MACRO: keyword."
  854. ;; Parsed macro.
  855. (should (equal (org-test-with-temp-text "#+MACRO: one 1"
  856. (org-export--get-inbuffer-options))
  857. '(:macro-one ("1"))))
  858. ;; Evaled macro.
  859. (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
  860. (org-export--get-inbuffer-options))
  861. '(:macro-two ("(eval (+ 1 1))"))))
  862. ;; Incomplete macro.
  863. (should-not (org-test-with-temp-text "#+MACRO: three"
  864. (org-export--get-inbuffer-options)))
  865. ;; Macro with newline character.
  866. (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
  867. (org-export--get-inbuffer-options))
  868. '(:macro-four ("a\nb"))))
  869. ;; Macro with protected newline character.
  870. (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
  871. (org-export--get-inbuffer-options))
  872. '(:macro-five ("a\\nb"))))
  873. ;; Recursive macro.
  874. (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
  875. (should
  876. (equal
  877. (org-export--get-inbuffer-options)
  878. '(:macro-six
  879. ("6")
  880. :macro-seven
  881. ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
  882. :post-blank 0 :parent nil))))))))
  883. (ert-deftest test-org-export/expand-macro ()
  884. "Test `org-export-expand-macro' specifications."
  885. ;; Standard test.
  886. (should
  887. (equal
  888. "some text"
  889. (org-test-with-parsed-data "#+MACRO: macro some text\n{{{macro}}}"
  890. (org-export-expand-macro
  891. (org-element-map tree 'macro 'identity info t) info))))
  892. ;; Macro with arguments.
  893. (should
  894. (equal
  895. "some text"
  896. (org-test-with-parsed-data "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
  897. (org-export-expand-macro
  898. (org-element-map tree 'macro 'identity info t) info))))
  899. ;; Macro with "eval"
  900. (should
  901. (equal
  902. "3"
  903. (org-test-with-parsed-data "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
  904. (org-export-expand-macro
  905. (org-element-map tree 'macro 'identity info t) info))))
  906. ;; Nested macros.
  907. (should
  908. (equal
  909. "inner outer"
  910. (org-test-with-parsed-data
  911. "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
  912. (flet ((translate-macro (macro contents info)
  913. (org-export-expand-macro macro info)))
  914. (org-export-expand-macro
  915. (org-element-map tree 'macro 'identity info t)
  916. (org-combine-plists
  917. info `(:translate-alist ((macro . translate-macro))))))))))
  918. ;;; Src-block and example-block
  919. (ert-deftest test-org-export/unravel-code ()
  920. "Test `org-export-unravel-code' function."
  921. (let ((org-coderef-label-format "(ref:%s)"))
  922. ;; 1. Code without reference.
  923. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  924. (should (equal (org-export-unravel-code (org-element-at-point))
  925. '("(+ 1 1)\n"))))
  926. ;; 2. Code with reference.
  927. (org-test-with-temp-text
  928. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  929. (should (equal (org-export-unravel-code (org-element-at-point))
  930. '("(+ 1 1)\n" (1 . "test")))))
  931. ;; 3. Code with user-defined reference.
  932. (org-test-with-temp-text
  933. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  934. (should (equal (org-export-unravel-code (org-element-at-point))
  935. '("(+ 1 1)\n" (1 . "test")))))
  936. ;; 4. Code references keys are relative to the current block.
  937. (org-test-with-temp-text "
  938. #+BEGIN_EXAMPLE -n
  939. \(+ 1 1)
  940. #+END_EXAMPLE
  941. #+BEGIN_EXAMPLE +n
  942. \(+ 2 2)
  943. \(+ 3 3) (ref:one)
  944. #+END_EXAMPLE"
  945. (goto-line 5)
  946. (should (equal (org-export-unravel-code (org-element-at-point))
  947. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
  948. ;; 5. Free up comma-protected lines.
  949. ;;
  950. ;; 5.1. In an Org source block, every line is protected.
  951. (org-test-with-temp-text
  952. "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
  953. (should (equal (org-export-unravel-code (org-element-at-point))
  954. '("* Test\n# comment\nText\n"))))
  955. ;; 5.2. In other blocks, only headlines, comments and keywords are
  956. ;; protected.
  957. (org-test-with-temp-text
  958. "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
  959. (should (equal (org-export-unravel-code (org-element-at-point))
  960. '("* Headline\n, * Not headline\n,Keep\n"))))))
  961. ;;; Tables
  962. (ert-deftest test-org-export/special-column ()
  963. "Test if the table's special column is properly recognized."
  964. ;; 1. First column is special if it contains only a special marking
  965. ;; characters or empty cells.
  966. (org-test-with-temp-text "
  967. | ! | 1 |
  968. | | 2 |"
  969. (should
  970. (org-export-table-has-special-column-p
  971. (org-element-map
  972. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  973. ;; 2. If the column contains anything else, it isn't special.
  974. (org-test-with-temp-text "
  975. | ! | 1 |
  976. | b | 2 |"
  977. (should-not
  978. (org-export-table-has-special-column-p
  979. (org-element-map
  980. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  981. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  982. ;; and "!".
  983. (org-test-with-temp-text "
  984. | # | 1 |
  985. | ^ | 2 |
  986. | * | 3 |
  987. | _ | 4 |
  988. | / | 5 |
  989. | $ | 6 |
  990. | ! | 7 |"
  991. (should
  992. (org-export-table-has-special-column-p
  993. (org-element-map
  994. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  995. ;; 4. A first column with only empty cells isn't considered as
  996. ;; special.
  997. (org-test-with-temp-text "
  998. | | 1 |
  999. | | 2 |"
  1000. (should-not
  1001. (org-export-table-has-special-column-p
  1002. (org-element-map
  1003. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1004. (ert-deftest test-org-export/table-row-is-special-p ()
  1005. "Test `org-export-table-row-is-special-p' specifications."
  1006. ;; 1. A row is special if it has a special marking character in the
  1007. ;; special column.
  1008. (org-test-with-parsed-data "| ! | 1 |"
  1009. (should
  1010. (org-export-table-row-is-special-p
  1011. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1012. ;; 2. A row is special when its first field is "/"
  1013. (org-test-with-parsed-data "
  1014. | / | 1 |
  1015. | a | b |"
  1016. (should
  1017. (org-export-table-row-is-special-p
  1018. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1019. ;; 3. A row only containing alignment cookies is also considered as
  1020. ;; special.
  1021. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1022. (should
  1023. (org-export-table-row-is-special-p
  1024. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1025. ;; 4. Everything else isn't considered as special.
  1026. (org-test-with-parsed-data "| \alpha | | c |"
  1027. (should-not
  1028. (org-export-table-row-is-special-p
  1029. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1030. ;; 5. Table's rules are never considered as special rows.
  1031. (org-test-with-parsed-data "|---+---|"
  1032. (should-not
  1033. (org-export-table-row-is-special-p
  1034. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1035. (ert-deftest test-org-export/has-header-p ()
  1036. "Test `org-export-table-has-header-p' specifications."
  1037. ;; 1. With an header.
  1038. (org-test-with-parsed-data "
  1039. | a | b |
  1040. |---+---|
  1041. | c | d |"
  1042. (should
  1043. (org-export-table-has-header-p
  1044. (org-element-map tree 'table 'identity info 'first-match)
  1045. info)))
  1046. ;; 2. Without an header.
  1047. (org-test-with-parsed-data "
  1048. | a | b |
  1049. | c | d |"
  1050. (should-not
  1051. (org-export-table-has-header-p
  1052. (org-element-map tree 'table 'identity info 'first-match)
  1053. info)))
  1054. ;; 3. Don't get fooled with starting and ending rules.
  1055. (org-test-with-parsed-data "
  1056. |---+---|
  1057. | a | b |
  1058. | c | d |
  1059. |---+---|"
  1060. (should-not
  1061. (org-export-table-has-header-p
  1062. (org-element-map tree 'table 'identity info 'first-match)
  1063. info))))
  1064. (ert-deftest test-org-export/table-row-group ()
  1065. "Test `org-export-table-row-group' specifications."
  1066. ;; 1. A rule creates a new group.
  1067. (org-test-with-parsed-data "
  1068. | a | b |
  1069. |---+---|
  1070. | 1 | 2 |"
  1071. (should
  1072. (equal
  1073. '(1 nil 2)
  1074. (mapcar (lambda (row) (org-export-table-row-group row info))
  1075. (org-element-map tree 'table-row 'identity)))))
  1076. ;; 2. Special rows are ignored in count.
  1077. (org-test-with-parsed-data "
  1078. | / | < | > |
  1079. |---|---+---|
  1080. | | 1 | 2 |"
  1081. (should
  1082. (equal
  1083. '(nil nil 1)
  1084. (mapcar (lambda (row) (org-export-table-row-group row info))
  1085. (org-element-map tree 'table-row 'identity)))))
  1086. ;; 3. Double rules also are ignored in count.
  1087. (org-test-with-parsed-data "
  1088. | a | b |
  1089. |---+---|
  1090. |---+---|
  1091. | 1 | 2 |"
  1092. (should
  1093. (equal
  1094. '(1 nil nil 2)
  1095. (mapcar (lambda (row) (org-export-table-row-group row info))
  1096. (org-element-map tree 'table-row 'identity))))))
  1097. (ert-deftest test-org-export/table-cell-width ()
  1098. "Test `org-export-table-cell-width' specifications."
  1099. ;; 1. Width is primarily determined by width cookies. If no cookie
  1100. ;; is found, cell's width is nil.
  1101. (org-test-with-parsed-data "
  1102. | / | <l> | <6> | <l7> |
  1103. | | a | b | c |"
  1104. (should
  1105. (equal
  1106. '(nil 6 7)
  1107. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1108. (org-element-map tree 'table-cell 'identity info)))))
  1109. ;; 2. The last width cookie has precedence.
  1110. (org-test-with-parsed-data "
  1111. | <6> |
  1112. | <7> |
  1113. | a |"
  1114. (should
  1115. (equal
  1116. '(7)
  1117. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1118. (org-element-map tree 'table-cell 'identity info)))))
  1119. ;; 3. Valid width cookies must have a specific row.
  1120. (org-test-with-parsed-data "| <6> | cell |"
  1121. (should
  1122. (equal
  1123. '(nil nil)
  1124. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1125. (org-element-map tree 'table-cell 'identity))))))
  1126. (ert-deftest test-org-export/table-cell-alignment ()
  1127. "Test `org-export-table-cell-alignment' specifications."
  1128. (let ((org-table-number-fraction 0.5)
  1129. (org-table-number-regexp "^[0-9]+$"))
  1130. ;; 1. Alignment is primarily determined by alignment cookies.
  1131. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1132. (let* ((tree (org-element-parse-buffer))
  1133. (info `(:parse-tree ,tree)))
  1134. (should
  1135. (equal
  1136. '(left center right)
  1137. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1138. (org-element-map tree 'table-cell 'identity))))))
  1139. ;; 2. The last alignment cookie has precedence.
  1140. (org-test-with-parsed-data "
  1141. | <l8> |
  1142. | cell |
  1143. | <r9> |"
  1144. (should
  1145. (equal
  1146. '(right right right)
  1147. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1148. (org-element-map tree 'table-cell 'identity)))))
  1149. ;; 3. If there's no cookie, cell's contents determine alignment.
  1150. ;; A column mostly made of cells containing numbers will align
  1151. ;; its cells to the right.
  1152. (org-test-with-parsed-data "
  1153. | 123 |
  1154. | some text |
  1155. | 12345 |"
  1156. (should
  1157. (equal
  1158. '(right right right)
  1159. (mapcar (lambda (cell)
  1160. (org-export-table-cell-alignment cell info))
  1161. (org-element-map tree 'table-cell 'identity)))))
  1162. ;; 4. Otherwise, they will be aligned to the left.
  1163. (org-test-with-parsed-data "
  1164. | text |
  1165. | some text |
  1166. | \alpha |"
  1167. (should
  1168. (equal
  1169. '(left left left)
  1170. (mapcar (lambda (cell)
  1171. (org-export-table-cell-alignment cell info))
  1172. (org-element-map tree 'table-cell 'identity)))))))
  1173. (ert-deftest test-org-export/table-cell-borders ()
  1174. "Test `org-export-table-cell-borders' specifications."
  1175. ;; 1. Recognize various column groups indicators.
  1176. (org-test-with-parsed-data "| / | < | > | <> |"
  1177. (should
  1178. (equal
  1179. '((right bottom top) (left bottom top) (right bottom top)
  1180. (right left bottom top))
  1181. (mapcar (lambda (cell)
  1182. (org-export-table-cell-borders cell info))
  1183. (org-element-map tree 'table-cell 'identity)))))
  1184. ;; 2. Accept shortcuts to define column groups.
  1185. (org-test-with-parsed-data "| / | < | < |"
  1186. (should
  1187. (equal
  1188. '((right bottom top) (right left bottom top) (left bottom top))
  1189. (mapcar (lambda (cell)
  1190. (org-export-table-cell-borders cell info))
  1191. (org-element-map tree 'table-cell 'identity)))))
  1192. ;; 3. A valid column groups row must start with a "/".
  1193. (org-test-with-parsed-data "
  1194. | | < |
  1195. | a | b |"
  1196. (should
  1197. (equal '((top) (top) (bottom) (bottom))
  1198. (mapcar (lambda (cell)
  1199. (org-export-table-cell-borders cell info))
  1200. (org-element-map tree 'table-cell 'identity)))))
  1201. ;; 4. Take table rules into consideration.
  1202. (org-test-with-parsed-data "
  1203. | 1 |
  1204. |---|
  1205. | 2 |"
  1206. (should
  1207. (equal '((below top) (bottom above))
  1208. (mapcar (lambda (cell)
  1209. (org-export-table-cell-borders cell info))
  1210. (org-element-map tree 'table-cell 'identity)))))
  1211. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1212. ;; (resp. `bottom' and `below') borders. Any special row is
  1213. ;; ignored.
  1214. (org-test-with-parsed-data "
  1215. |---+----|
  1216. | / | |
  1217. | | 1 |
  1218. |---+----|"
  1219. (should
  1220. (equal '((bottom below top above))
  1221. (last
  1222. (mapcar (lambda (cell)
  1223. (org-export-table-cell-borders cell info))
  1224. (org-element-map tree 'table-cell 'identity)))))))
  1225. (ert-deftest test-org-export/table-dimensions ()
  1226. "Test `org-export-table-dimensions' specifications."
  1227. ;; 1. Standard test.
  1228. (org-test-with-parsed-data "
  1229. | 1 | 2 | 3 |
  1230. | 4 | 5 | 6 |"
  1231. (should
  1232. (equal '(2 . 3)
  1233. (org-export-table-dimensions
  1234. (org-element-map tree 'table 'identity info 'first-match) info))))
  1235. ;; 2. Ignore horizontal rules and special columns.
  1236. (org-test-with-parsed-data "
  1237. | / | < | > |
  1238. | 1 | 2 | 3 |
  1239. |---+---+---|
  1240. | 4 | 5 | 6 |"
  1241. (should
  1242. (equal '(2 . 3)
  1243. (org-export-table-dimensions
  1244. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1245. (ert-deftest test-org-export/table-cell-address ()
  1246. "Test `org-export-table-cell-address' specifications."
  1247. ;; 1. Standard test: index is 0-based.
  1248. (org-test-with-parsed-data "| a | b |"
  1249. (should
  1250. (equal '((0 . 0) (0 . 1))
  1251. (org-element-map
  1252. tree 'table-cell
  1253. (lambda (cell) (org-export-table-cell-address cell info))
  1254. info))))
  1255. ;; 2. Special column isn't counted, nor are special rows.
  1256. (org-test-with-parsed-data "
  1257. | / | <> |
  1258. | | c |"
  1259. (should
  1260. (equal '(0 . 0)
  1261. (org-export-table-cell-address
  1262. (car (last (org-element-map tree 'table-cell 'identity info)))
  1263. info))))
  1264. ;; 3. Tables rules do not count either.
  1265. (org-test-with-parsed-data "
  1266. | a |
  1267. |---|
  1268. | b |
  1269. |---|
  1270. | c |"
  1271. (should
  1272. (equal '(2 . 0)
  1273. (org-export-table-cell-address
  1274. (car (last (org-element-map tree 'table-cell 'identity info)))
  1275. info))))
  1276. ;; 4. Return nil for special cells.
  1277. (org-test-with-parsed-data "| / | a |"
  1278. (should-not
  1279. (org-export-table-cell-address
  1280. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1281. info))))
  1282. (ert-deftest test-org-export/get-table-cell-at ()
  1283. "Test `org-export-get-table-cell-at' specifications."
  1284. ;; 1. Address ignores special columns, special rows and rules.
  1285. (org-test-with-parsed-data "
  1286. | / | <> |
  1287. | | a |
  1288. |---+----|
  1289. | | b |"
  1290. (should
  1291. (equal '("b")
  1292. (org-element-contents
  1293. (org-export-get-table-cell-at
  1294. '(1 . 0)
  1295. (org-element-map tree 'table 'identity info 'first-match)
  1296. info)))))
  1297. ;; 2. Return value for a non-existent address is nil.
  1298. (org-test-with-parsed-data "| a |"
  1299. (should-not
  1300. (org-export-get-table-cell-at
  1301. '(2 . 2)
  1302. (org-element-map tree 'table 'identity info 'first-match)
  1303. info)))
  1304. (org-test-with-parsed-data "| / |"
  1305. (should-not
  1306. (org-export-get-table-cell-at
  1307. '(0 . 0)
  1308. (org-element-map tree 'table 'identity info 'first-match)
  1309. info))))
  1310. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1311. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1312. ;; 1. A cell at a beginning of a row always starts a column group.
  1313. (org-test-with-parsed-data "| a |"
  1314. (should
  1315. (org-export-table-cell-starts-colgroup-p
  1316. (org-element-map tree 'table-cell 'identity info 'first-match)
  1317. info)))
  1318. ;; 2. Special column should be ignored when determining the
  1319. ;; beginning of the row.
  1320. (org-test-with-parsed-data "
  1321. | / | |
  1322. | | a |"
  1323. (should
  1324. (org-export-table-cell-starts-colgroup-p
  1325. (org-element-map tree 'table-cell 'identity info 'first-match)
  1326. info)))
  1327. ;; 2. Explicit column groups.
  1328. (org-test-with-parsed-data "
  1329. | / | | < |
  1330. | a | b | c |"
  1331. (should
  1332. (equal
  1333. '(yes no yes)
  1334. (org-element-map
  1335. tree 'table-cell
  1336. (lambda (cell)
  1337. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1338. info)))))
  1339. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1340. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1341. ;; 1. A cell at the end of a row always ends a column group.
  1342. (org-test-with-parsed-data "| a |"
  1343. (should
  1344. (org-export-table-cell-ends-colgroup-p
  1345. (org-element-map tree 'table-cell 'identity info 'first-match)
  1346. info)))
  1347. ;; 2. Special column should be ignored when determining the
  1348. ;; beginning of the row.
  1349. (org-test-with-parsed-data "
  1350. | / | |
  1351. | | a |"
  1352. (should
  1353. (org-export-table-cell-ends-colgroup-p
  1354. (org-element-map tree 'table-cell 'identity info 'first-match)
  1355. info)))
  1356. ;; 3. Explicit column groups.
  1357. (org-test-with-parsed-data "
  1358. | / | < | |
  1359. | a | b | c |"
  1360. (should
  1361. (equal
  1362. '(yes no yes)
  1363. (org-element-map
  1364. tree 'table-cell
  1365. (lambda (cell)
  1366. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1367. info)))))
  1368. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1369. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1370. ;; 1. A row at the beginning of a table always starts a row group.
  1371. ;; So does a row following a table rule.
  1372. (org-test-with-parsed-data "
  1373. | a |
  1374. |---|
  1375. | b |"
  1376. (should
  1377. (equal
  1378. '(yes no yes)
  1379. (org-element-map
  1380. tree 'table-row
  1381. (lambda (row)
  1382. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1383. info))))
  1384. ;; 2. Special rows should be ignored when determining the beginning
  1385. ;; of the row.
  1386. (org-test-with-parsed-data "
  1387. | / | < |
  1388. | | a |
  1389. |---+---|
  1390. | / | < |
  1391. | | b |"
  1392. (should
  1393. (equal
  1394. '(yes no yes)
  1395. (org-element-map
  1396. tree 'table-row
  1397. (lambda (row)
  1398. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1399. info)))))
  1400. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1401. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1402. ;; 1. A row at the end of a table always ends a row group. So does
  1403. ;; a row preceding a table rule.
  1404. (org-test-with-parsed-data "
  1405. | a |
  1406. |---|
  1407. | b |"
  1408. (should
  1409. (equal
  1410. '(yes no yes)
  1411. (org-element-map
  1412. tree 'table-row
  1413. (lambda (row)
  1414. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1415. info))))
  1416. ;; 2. Special rows should be ignored when determining the beginning
  1417. ;; of the row.
  1418. (org-test-with-parsed-data "
  1419. | | a |
  1420. | / | < |
  1421. |---+---|
  1422. | | b |
  1423. | / | < |"
  1424. (should
  1425. (equal
  1426. '(yes no yes)
  1427. (org-element-map
  1428. tree 'table-row
  1429. (lambda (row)
  1430. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1431. info)))))
  1432. (ert-deftest test-org-export/table-row-starts-header-p ()
  1433. "Test `org-export-table-row-starts-header-p' specifications."
  1434. ;; 1. Only the row starting the first row group starts the table
  1435. ;; header.
  1436. (org-test-with-parsed-data "
  1437. | a |
  1438. | b |
  1439. |---|
  1440. | c |"
  1441. (should
  1442. (equal
  1443. '(yes no no no)
  1444. (org-element-map
  1445. tree 'table-row
  1446. (lambda (row)
  1447. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1448. info))))
  1449. ;; 2. A row cannot start an header if there's no header in the
  1450. ;; table.
  1451. (org-test-with-parsed-data "
  1452. | a |
  1453. |---|"
  1454. (should-not
  1455. (org-export-table-row-starts-header-p
  1456. (org-element-map tree 'table-row 'identity info 'first-match)
  1457. info))))
  1458. (ert-deftest test-org-export/table-row-ends-header-p ()
  1459. "Test `org-export-table-row-ends-header-p' specifications."
  1460. ;; 1. Only the row starting the first row group starts the table
  1461. ;; header.
  1462. (org-test-with-parsed-data "
  1463. | a |
  1464. | b |
  1465. |---|
  1466. | c |"
  1467. (should
  1468. (equal
  1469. '(no yes no no)
  1470. (org-element-map
  1471. tree 'table-row
  1472. (lambda (row)
  1473. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1474. info))))
  1475. ;; 2. A row cannot start an header if there's no header in the
  1476. ;; table.
  1477. (org-test-with-parsed-data "
  1478. | a |
  1479. |---|"
  1480. (should-not
  1481. (org-export-table-row-ends-header-p
  1482. (org-element-map tree 'table-row 'identity info 'first-match)
  1483. info))))
  1484. ;;; Topology
  1485. (ert-deftest test-org-export/get-next-element ()
  1486. "Test `org-export-get-next-element' specifications."
  1487. ;; Standard test.
  1488. (should
  1489. (equal "b"
  1490. (org-test-with-parsed-data "* Headline\n*a* b"
  1491. (org-export-get-next-element
  1492. (org-element-map tree 'bold 'identity info t)))))
  1493. ;; Return nil when no previous element.
  1494. (should-not
  1495. (org-test-with-parsed-data "* Headline\na *b*"
  1496. (org-export-get-next-element
  1497. (org-element-map tree 'bold 'identity info t)))))
  1498. (ert-deftest test-org-export/get-previous-element ()
  1499. "Test `org-export-get-previous-element' specifications."
  1500. ;; Standard test.
  1501. (should
  1502. (equal "a "
  1503. (org-test-with-parsed-data "* Headline\na *b*"
  1504. (org-export-get-previous-element
  1505. (org-element-map tree 'bold 'identity info t)))))
  1506. ;; Return nil when no previous element.
  1507. (should-not
  1508. (org-test-with-parsed-data "* Headline\n*a* b"
  1509. (org-export-get-previous-element
  1510. (org-element-map tree 'bold 'identity info t)))))
  1511. (provide 'test-org-export)
  1512. ;;; test-org-export.el end here