test-ox.el 94 KB

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