test-ox.el 87 KB

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