test-ox.el 84 KB

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