test-ox.el 104 KB

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