test-ox.el 84 KB

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