test-ox.el 104 KB

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