test-ox.el 94 KB

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