test-ox.el 100 KB

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