test-org-export.el 63 KB

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