test-ox.el 102 KB

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