test-ox.el 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340
  1. ;;; test-ox.el --- Tests for ox.el
  2. ;; Copyright (C) 2012, 2013 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (unless (featurep 'ox)
  17. (signal 'missing-test-dependency "org-export"))
  18. (defmacro org-test-with-backend (backend &rest body)
  19. "Execute body with an export back-end defined.
  20. BACKEND is the name of the back-end. BODY is the body to
  21. execute. The defined back-end simply returns parsed data as Org
  22. syntax."
  23. (declare (debug (form body)) (indent 1))
  24. `(let ((org-export-registered-backends
  25. ',(list
  26. (list backend
  27. :translate-alist
  28. (let (transcode-table)
  29. (dolist (type (append org-element-all-elements
  30. org-element-all-objects)
  31. transcode-table)
  32. (push
  33. (cons type
  34. (lambda (obj contents info)
  35. (funcall
  36. (intern (format "org-element-%s-interpreter"
  37. type))
  38. obj contents)))
  39. transcode-table)))))))
  40. (progn ,@body)))
  41. (defmacro org-test-with-parsed-data (data &rest body)
  42. "Execute body with parsed data available.
  43. DATA is a string containing the data to be parsed. BODY is the
  44. body to execute. Parse tree is available under the `tree'
  45. variable, and communication channel under `info'.
  46. This function calls `org-export-collect-tree-properties'. As
  47. such, `:ignore-list' (for `org-element-map') and
  48. `:parse-tree' (for `org-export-get-genealogy') properties are
  49. already filled in `info'."
  50. (declare (debug (form body)) (indent 1))
  51. `(org-test-with-temp-text ,data
  52. (let* ((tree (org-element-parse-buffer))
  53. (info (org-export-collect-tree-properties
  54. tree (org-export-get-environment))))
  55. ,@body)))
  56. ;;; Internal Tests
  57. (ert-deftest test-org-export/bind-keyword ()
  58. "Test reading #+BIND: keywords."
  59. ;; Test with `org-export-allow-bind-keywords' set to t.
  60. (should
  61. (org-test-with-temp-text "#+BIND: variable value"
  62. (let ((org-export-allow-bind-keywords t))
  63. (org-export--install-letbind-maybe)
  64. (eq variable 'value))))
  65. ;; Test with `org-export-allow-bind-keywords' set to nil.
  66. (should-not
  67. (org-test-with-temp-text "#+BIND: variable value"
  68. (let ((org-export-allow-bind-keywords nil))
  69. (org-export--install-letbind-maybe)
  70. (boundp 'variable))))
  71. ;; BIND keywords are case-insensitive.
  72. (should
  73. (org-test-with-temp-text "#+bind: variable value"
  74. (let ((org-export-allow-bind-keywords t))
  75. (org-export--install-letbind-maybe)
  76. (eq variable 'value)))))
  77. (ert-deftest test-org-export/parse-option-keyword ()
  78. "Test reading all standard #+OPTIONS: items."
  79. (should
  80. (equal
  81. (org-export--parse-option-keyword
  82. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  83. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  84. stat:t")
  85. '(:headline-levels
  86. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  87. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  88. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  89. :with-footnotes t :with-inlinetasks nil :with-priority t
  90. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  91. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  92. :with-todo-keywords t)))
  93. ;; Test some special values.
  94. (should
  95. (equal
  96. (org-export--parse-option-keyword
  97. "arch:headline creator:comment d:(\"TEST\")
  98. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  99. '( :section-numbers
  100. 2
  101. :with-archived-trees headline :with-creator comment
  102. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  103. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  104. (ert-deftest test-org-export/get-inbuffer-options ()
  105. "Test reading all standard export keywords."
  106. (should
  107. (equal
  108. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  109. #+CREATOR: Idem
  110. #+DATE: Today
  111. #+DESCRIPTION: Testing
  112. #+DESCRIPTION: with two lines
  113. #+EMAIL: some@email.org
  114. #+EXCLUDE_TAGS: noexport invisible
  115. #+KEYWORDS: test
  116. #+LANGUAGE: en
  117. #+SELECT_TAGS: export
  118. #+TITLE: Some title
  119. #+TITLE: with spaces"
  120. (org-export--get-inbuffer-options))
  121. '(:author
  122. ("Me, Myself and I") :creator "Idem" :date ("Today")
  123. :description "Testing\nwith two lines" :email "some@email.org"
  124. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  125. :select-tags ("export") :title ("Some title with spaces")))))
  126. (ert-deftest test-org-export/get-subtree-options ()
  127. "Test setting options from headline's properties."
  128. ;; EXPORT_TITLE.
  129. (org-test-with-temp-text "#+TITLE: Title
  130. * Headline
  131. :PROPERTIES:
  132. :EXPORT_TITLE: Subtree Title
  133. :END:
  134. Paragraph"
  135. (forward-line)
  136. (should (equal (plist-get (org-export-get-environment nil t) :title)
  137. '("Subtree Title"))))
  138. :title
  139. '("subtree-title")
  140. ;; EXPORT_OPTIONS.
  141. (org-test-with-temp-text "#+OPTIONS: H:1
  142. * Headline
  143. :PROPERTIES:
  144. :EXPORT_OPTIONS: H:2
  145. :END:
  146. Paragraph"
  147. (forward-line)
  148. (should
  149. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  150. ;; EXPORT_DATE.
  151. (org-test-with-temp-text "#+DATE: today
  152. * Headline
  153. :PROPERTIES:
  154. :EXPORT_DATE: 29-03-2012
  155. :END:
  156. Paragraph"
  157. (forward-line)
  158. (should (equal (plist-get (org-export-get-environment nil t) :date)
  159. '("29-03-2012"))))
  160. ;; Properties with `split' behaviour are stored as a list of
  161. ;; strings.
  162. (should
  163. (equal '("a" "b")
  164. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  165. * Headline
  166. :PROPERTIES:
  167. :EXPORT_EXCLUDE_TAGS: a b
  168. :END:
  169. Paragraph"
  170. (progn
  171. (forward-line)
  172. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  173. ;; Handle :PROPERTY+: syntax.
  174. (should
  175. (equal '("a" "b")
  176. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  177. * Headline
  178. :PROPERTIES:
  179. :EXPORT_EXCLUDE_TAGS: a
  180. :EXPORT_EXCLUDE_TAGS+: b
  181. :END:
  182. Paragraph"
  183. (progn
  184. (forward-line)
  185. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  186. ;; Export properties are case-insensitive.
  187. (org-test-with-temp-text "* Headline
  188. :PROPERTIES:
  189. :EXPORT_Date: 29-03-2012
  190. :END:
  191. Paragraph"
  192. (should (equal (plist-get (org-export-get-environment nil t) :date)
  193. '("29-03-2012"))))
  194. ;; Still grab correct options when section above is empty.
  195. (should
  196. (equal '("H1")
  197. (org-test-with-temp-text "* H1\n** H11\n** H12"
  198. (progn (forward-line 2)
  199. (plist-get (org-export-get-environment nil t) :title))))))
  200. (ert-deftest test-org-export/handle-options ()
  201. "Test if export options have an impact on output."
  202. ;; Test exclude tags.
  203. (org-test-with-temp-text "* Head1 :noexport:"
  204. (org-test-with-backend test
  205. (should
  206. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  207. ""))))
  208. ;; Test include tags.
  209. (org-test-with-temp-text "
  210. * Head1
  211. * Head2
  212. ** Sub-Head2.1 :export:
  213. *** Sub-Head2.1.1
  214. * Head2"
  215. (org-test-with-backend test
  216. (should
  217. (equal
  218. "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
  219. (let ((org-tags-column 0))
  220. (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
  221. ;; Test mixing include tags and exclude tags.
  222. (org-test-with-temp-text "
  223. * Head1 :export:
  224. ** Sub-Head1 :noexport:
  225. ** Sub-Head2
  226. * Head2 :noexport:
  227. ** Sub-Head1 :export:"
  228. (org-test-with-backend test
  229. (should
  230. (string-match
  231. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  232. (org-export-as
  233. 'test nil nil nil
  234. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  235. ;; Ignore tasks.
  236. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  237. (org-test-with-temp-text "* TODO Head1"
  238. (org-test-with-backend test
  239. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  240. "")))))
  241. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  242. (org-test-with-temp-text "* TODO Head1"
  243. (org-test-with-backend test
  244. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  245. "* TODO Head1\n")))))
  246. ;; Archived tree.
  247. (org-test-with-temp-text "* Head1 :archive:"
  248. (let ((org-archive-tag "archive"))
  249. (org-test-with-backend test
  250. (should
  251. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  252. "")))))
  253. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  254. (let ((org-archive-tag "archive"))
  255. (org-test-with-backend test
  256. (should
  257. (string-match
  258. "\\* Head1[ \t]+:archive:"
  259. (org-export-as 'test nil nil nil
  260. '(:with-archived-trees headline)))))))
  261. (org-test-with-temp-text "* Head1 :archive:"
  262. (let ((org-archive-tag "archive"))
  263. (org-test-with-backend test
  264. (should
  265. (string-match
  266. "\\`\\* Head1[ \t]+:archive:\n\\'"
  267. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  268. ;; Clocks.
  269. (let ((org-clock-string "CLOCK:"))
  270. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  271. (org-test-with-backend test
  272. (should
  273. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  274. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  275. (should
  276. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  277. ;; Drawers.
  278. (let ((org-drawers '("TEST")))
  279. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  280. (org-test-with-backend test
  281. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  282. ""))
  283. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  284. ":TEST:\ncontents\n:END:\n")))))
  285. (let ((org-drawers '("FOO" "BAR")))
  286. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  287. (org-test-with-backend test
  288. (should
  289. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  290. ":FOO:\nkeep\n:END:\n")))))
  291. (let ((org-drawers '("FOO" "BAR")))
  292. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  293. (org-test-with-backend test
  294. (should
  295. (equal (org-export-as 'test nil nil nil '(:with-drawers (not "BAR")))
  296. ":FOO:\nkeep\n:END:\n")))))
  297. ;; Inlinetasks.
  298. (when (featurep 'org-inlinetask)
  299. (should
  300. (equal
  301. (let ((org-inlinetask-min-level 15))
  302. (org-test-with-temp-text "*************** Task"
  303. (org-test-with-backend test
  304. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  305. ""))
  306. (should
  307. (equal
  308. (let ((org-inlinetask-min-level 15))
  309. (org-test-with-temp-text
  310. "*************** Task\nContents\n*************** END"
  311. (org-test-with-backend test
  312. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  313. "")))
  314. ;; Plannings.
  315. (let ((org-closed-string "CLOSED:"))
  316. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  317. (org-test-with-backend test
  318. (should
  319. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  320. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  321. (should
  322. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  323. "")))))
  324. ;; Statistics cookies.
  325. (should
  326. (equal ""
  327. (org-test-with-temp-text "[0/0]"
  328. (org-test-with-backend test
  329. (org-export-as
  330. 'test nil nil nil '(:with-statistics-cookies nil))))))
  331. ;; Timestamps.
  332. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  333. (org-test-with-backend test
  334. (should
  335. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  336. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  337. (should
  338. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  339. (should
  340. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  341. "<2012-04-29 sun. 10:45>\n"))
  342. (should
  343. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  344. "[2012-04-29 sun. 10:45]\n")))))
  345. (ert-deftest test-org-export/comment-tree ()
  346. "Test if export process ignores commented trees."
  347. (let ((org-comment-string "COMMENT"))
  348. (org-test-with-temp-text "* COMMENT Head1"
  349. (org-test-with-backend test
  350. (should (equal (org-export-as 'test) ""))))))
  351. (ert-deftest test-org-export/export-scope ()
  352. "Test all export scopes."
  353. (org-test-with-temp-text "
  354. * Head1
  355. ** Head2
  356. text
  357. *** Head3"
  358. (org-test-with-backend test
  359. ;; Subtree.
  360. (forward-line 3)
  361. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  362. ;; Visible.
  363. (goto-char (point-min))
  364. (forward-line)
  365. (org-cycle)
  366. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  367. ;; Region.
  368. (goto-char (point-min))
  369. (forward-line 3)
  370. (transient-mark-mode 1)
  371. (push-mark (point) t t)
  372. (goto-char (point-at-eol))
  373. (should (equal (org-export-as 'test) "text\n"))))
  374. ;; Subtree with a code block calling another block outside.
  375. (should
  376. (equal ": 3\n"
  377. (org-test-with-temp-text "
  378. * Head1
  379. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  380. <<test>>
  381. #+END_SRC
  382. * Head2
  383. #+NAME: test
  384. #+BEGIN_SRC emacs-lisp
  385. \(+ 1 2)
  386. #+END_SRC"
  387. (org-test-with-backend test
  388. (forward-line 1)
  389. (org-export-as 'test 'subtree)))))
  390. ;; Body only.
  391. (org-test-with-temp-text "Text"
  392. (org-test-with-backend test
  393. (plist-put
  394. (cdr (assq 'test org-export-registered-backends))
  395. :translate-alist
  396. (cons (cons 'template (lambda (body info) (format "BEGIN\n%sEND" body)))
  397. (org-export-backend-translate-table 'test)))
  398. (should (equal (org-export-as 'test nil nil 'body-only) "Text\n"))
  399. (should (equal (org-export-as 'test) "BEGIN\nText\nEND")))))
  400. (ert-deftest test-org-export/output-file-name ()
  401. "Test `org-export-output-file-name' specifications."
  402. ;; Export from a file: name is built from original file name.
  403. (should
  404. (org-test-with-temp-text-in-file "Test"
  405. (equal (concat (file-name-as-directory ".")
  406. (file-name-nondirectory
  407. (file-name-sans-extension (buffer-file-name))))
  408. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  409. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  410. (should
  411. (org-test-with-temp-text-in-file
  412. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  413. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  414. ;; From a buffer not associated to a file, too.
  415. (should
  416. (org-test-with-temp-text
  417. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  418. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  419. ;; When provided name is absolute, preserve it.
  420. (should
  421. (org-test-with-temp-text
  422. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  423. (expand-file-name "test"))
  424. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  425. ;; When PUB-DIR argument is provided, use it.
  426. (should
  427. (org-test-with-temp-text-in-file "Test"
  428. (equal (file-name-directory
  429. (org-export-output-file-name ".ext" nil "dir/"))
  430. "dir/")))
  431. ;; When returned name would overwrite original file, add EXTENSION
  432. ;; another time.
  433. (should
  434. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  435. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  436. (ert-deftest test-org-export/expand-include ()
  437. "Test file inclusion in an Org buffer."
  438. ;; Error when file isn't specified.
  439. (should-error
  440. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  441. (org-export-expand-include-keyword)))
  442. ;; Full insertion with recursive inclusion.
  443. (org-test-with-temp-text
  444. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  445. (org-export-expand-include-keyword)
  446. (should (equal (buffer-string)
  447. "Small Org file with an include keyword.
  448. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  449. Success!
  450. * Heading
  451. body\n")))
  452. ;; Localized insertion.
  453. (org-test-with-temp-text
  454. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  455. org-test-dir)
  456. (org-export-expand-include-keyword)
  457. (should (equal (buffer-string)
  458. "Small Org file with an include keyword.\n")))
  459. ;; Insertion with constraints on headlines level.
  460. (org-test-with-temp-text
  461. (format
  462. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  463. org-test-dir)
  464. (org-export-expand-include-keyword)
  465. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  466. ;; Inclusion within an example block.
  467. (org-test-with-temp-text
  468. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  469. org-test-dir)
  470. (org-export-expand-include-keyword)
  471. (should
  472. (equal
  473. (buffer-string)
  474. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  475. ;; Inclusion within a src-block.
  476. (org-test-with-temp-text
  477. (format
  478. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  479. org-test-dir)
  480. (org-export-expand-include-keyword)
  481. (should (equal (buffer-string)
  482. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  483. (ert-deftest test-org-export/expand-macro ()
  484. "Test macro expansion in an Org buffer."
  485. ;; Standard macro expansion.
  486. (should
  487. (equal "#+MACRO: macro1 value\nvalue\n"
  488. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  489. (org-test-with-backend test (org-export-as 'test)))))
  490. ;; Expand specific macros.
  491. (should
  492. (equal "me 2012-03-29 me@here Title\n"
  493. (org-test-with-temp-text
  494. "
  495. #+TITLE: Title
  496. #+DATE: 2012-03-29
  497. #+AUTHOR: me
  498. #+EMAIL: me@here
  499. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  500. (let ((output (org-test-with-backend test (org-export-as 'test))))
  501. (substring output (string-match ".*\n\\'" output))))))
  502. ;; Expand specific macros when property contained a regular macro
  503. ;; already.
  504. (should
  505. (equal "value\n"
  506. (org-test-with-temp-text "
  507. #+MACRO: macro1 value
  508. #+TITLE: {{{macro1}}}
  509. {{{title}}}"
  510. (let ((output (org-test-with-backend test (org-export-as 'test))))
  511. (substring output (string-match ".*\n\\'" output))))))
  512. ;; Expand macros with templates in included files.
  513. (should
  514. (equal "success\n"
  515. (org-test-with-temp-text
  516. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  517. {{{included-macro}}}" org-test-dir)
  518. (let ((output (org-test-with-backend test (org-export-as 'test))))
  519. (substring output (string-match ".*\n\\'" output)))))))
  520. (ert-deftest test-org-export/user-ignore-list ()
  521. "Test if `:ignore-list' accepts user input."
  522. (org-test-with-backend test
  523. (flet ((skip-note-head
  524. (data backend info)
  525. ;; Ignore headlines with the word "note" in their title.
  526. (org-element-map
  527. data 'headline
  528. (lambda (headline)
  529. (when (string-match "\\<note\\>"
  530. (org-element-property :raw-value headline))
  531. (org-export-ignore-element headline info)))
  532. info)
  533. data))
  534. ;; Install function in parse tree filters.
  535. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  536. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  537. (should (equal (org-export-as 'test) "* Head1\n")))))))
  538. (ert-deftest test-org-export/before-processing-hook ()
  539. "Test `org-export-before-processing-hook'."
  540. (should
  541. (equal
  542. "#+MACRO: mac val\nTest\n"
  543. (org-test-with-backend test
  544. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  545. (let ((org-export-before-processing-hook
  546. '((lambda (backend)
  547. (while (re-search-forward "{{{" nil t)
  548. (let ((object (org-element-context)))
  549. (when (eq (org-element-type object) 'macro)
  550. (delete-region
  551. (org-element-property :begin object)
  552. (org-element-property :end object)))))))))
  553. (org-export-as 'test)))))))
  554. (ert-deftest test-org-export/before-parsing-hook ()
  555. "Test `org-export-before-parsing-hook'."
  556. (should
  557. (equal "Body 1\nBody 2\n"
  558. (org-test-with-backend test
  559. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  560. (let ((org-export-before-parsing-hook
  561. '((lambda (backend)
  562. (goto-char (point-min))
  563. (while (re-search-forward org-outline-regexp-bol nil t)
  564. (delete-region
  565. (point-at-bol) (progn (forward-line) (point))))))))
  566. (org-export-as 'test)))))))
  567. ;;; Affiliated Keywords
  568. (ert-deftest test-org-export/read-attribute ()
  569. "Test `org-export-read-attribute' specifications."
  570. ;; Standard test.
  571. (should
  572. (equal
  573. (org-export-read-attribute
  574. :attr_html
  575. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  576. (org-element-at-point)))
  577. '(:a 1 :b 2)))
  578. ;; Return nil on empty attribute.
  579. (should-not
  580. (org-export-read-attribute
  581. :attr_html
  582. (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
  583. (ert-deftest test-org-export/get-caption ()
  584. "Test `org-export-get-caption' specifications."
  585. ;; Without optional argument, return long caption
  586. (should
  587. (equal
  588. '("l")
  589. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  590. (org-export-get-caption (org-element-at-point)))))
  591. ;; With optional argument, return short caption.
  592. (should
  593. (equal
  594. '("s")
  595. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  596. (org-export-get-caption (org-element-at-point) t))))
  597. ;; Multiple lines are separated by white spaces.
  598. (should
  599. (equal
  600. '("a" " " "b")
  601. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  602. (org-export-get-caption (org-element-at-point))))))
  603. ;;; Back-End Tools
  604. (ert-deftest test-org-export/define-backend ()
  605. "Test back-end definition and accessors."
  606. ;; Translate table.
  607. (should
  608. (equal '((headline . my-headline-test))
  609. (let (org-export-registered-backends)
  610. (org-export-define-backend test ((headline . my-headline-test)))
  611. (org-export-backend-translate-table 'test))))
  612. ;; Filters.
  613. (should
  614. (equal '((:filter-headline . my-filter))
  615. (let (org-export-registered-backends)
  616. (org-export-define-backend test
  617. ((headline . my-headline-test))
  618. :filters-alist ((:filter-headline . my-filter)))
  619. (org-export-backend-filters 'test))))
  620. ;; Options.
  621. (should
  622. (equal '((:prop value))
  623. (let (org-export-registered-backends)
  624. (org-export-define-backend test
  625. ((headline . my-headline-test))
  626. :options-alist ((:prop value)))
  627. (org-export-backend-options 'test))))
  628. ;; Menu.
  629. (should
  630. (equal '(?k "Test Export" test)
  631. (let (org-export-registered-backends)
  632. (org-export-define-backend test
  633. ((headline . my-headline-test))
  634. :menu-entry (?k "Test Export" test))
  635. (org-export-backend-menu 'test))))
  636. ;; Export Blocks.
  637. (should
  638. (equal '(("TEST" . org-element-export-block-parser))
  639. (let (org-export-registered-backends org-element-block-name-alist)
  640. (org-export-define-backend test
  641. ((headline . my-headline-test))
  642. :export-block ("test"))
  643. org-element-block-name-alist))))
  644. (ert-deftest test-org-export/define-derived-backend ()
  645. "Test `org-export-define-derived-backend' specifications."
  646. ;; Error when parent back-end is not defined.
  647. (should-error
  648. (let (org-export-registered-backends)
  649. (org-export-define-derived-backend test parent)))
  650. ;; Append translation table to parent's.
  651. (should
  652. (equal '((:headline . test) (:headline . parent))
  653. (let (org-export-registered-backends)
  654. (org-export-define-backend parent ((:headline . parent)))
  655. (org-export-define-derived-backend test parent
  656. :translate-alist ((:headline . test)))
  657. (org-export-backend-translate-table 'test))))
  658. ;; Options defined in the new back have priority over those defined
  659. ;; in parent.
  660. (should
  661. (eq 'test
  662. (let (org-export-registered-backends)
  663. (org-export-define-backend parent
  664. ((:headline . parent))
  665. :options-alist ((:a nil nil 'parent)))
  666. (org-export-define-derived-backend test parent
  667. :options-alist ((:a nil nil 'test)))
  668. (plist-get (org-export--get-global-options 'test) :a)))))
  669. (ert-deftest test-org-export/derived-backend-p ()
  670. "Test `org-export-derived-backend-p' specifications."
  671. ;; Non-nil with direct match.
  672. (should
  673. (let (org-export-registered-backends)
  674. (org-export-define-backend test ((headline . test)))
  675. (org-export-derived-backend-p 'test 'test)))
  676. (should
  677. (let (org-export-registered-backends)
  678. (org-export-define-backend test ((headline . test)))
  679. (org-export-define-derived-backend test2 test)
  680. (org-export-derived-backend-p 'test2 'test2)))
  681. ;; Non-nil with a direct parent.
  682. (should
  683. (let (org-export-registered-backends)
  684. (org-export-define-backend test ((headline . test)))
  685. (org-export-define-derived-backend test2 test)
  686. (org-export-derived-backend-p 'test2 'test)))
  687. ;; Non-nil with an indirect parent.
  688. (should
  689. (let (org-export-registered-backends)
  690. (org-export-define-backend test ((headline . test)))
  691. (org-export-define-derived-backend test2 test)
  692. (org-export-define-derived-backend test3 test2)
  693. (org-export-derived-backend-p 'test3 'test)))
  694. ;; Nil otherwise.
  695. (should-not
  696. (let (org-export-registered-backends)
  697. (org-export-define-backend test ((headline . test)))
  698. (org-export-define-backend test2 ((headline . test2)))
  699. (org-export-derived-backend-p 'test2 'test)))
  700. (should-not
  701. (let (org-export-registered-backends)
  702. (org-export-define-backend test ((headline . test)))
  703. (org-export-define-backend test2 ((headline . test2)))
  704. (org-export-define-derived-backend test3 test2)
  705. (org-export-derived-backend-p 'test3 'test))))
  706. (ert-deftest test-org-export/with-backend ()
  707. "Test `org-export-with-backend' definition."
  708. ;; Error when calling an undefined back-end
  709. (should-error
  710. (let (org-export-registered-backends)
  711. (org-export-with-backend 'test "Test")))
  712. ;; Error when called back-end doesn't have an appropriate
  713. ;; transcoder.
  714. (should-error
  715. (let (org-export-registered-backends)
  716. (org-export-define-backend test ((headline . ignore)))
  717. (org-export-with-backend 'test "Test")))
  718. ;; Otherwise, export using correct transcoder
  719. (should
  720. (equal "Success"
  721. (let (org-export-registered-backends)
  722. (org-export-define-backend test
  723. ((plain-text . (lambda (text contents info) "Failure"))))
  724. (org-export-define-backend test2
  725. ((plain-text . (lambda (text contents info) "Success"))))
  726. (org-export-with-backend 'test2 "Test")))))
  727. (ert-deftest test-org-export/data-with-translations ()
  728. "Test `org-export-data-with-translations' specifications."
  729. (should
  730. (equal
  731. "Success!"
  732. (org-export-data-with-translations
  733. '(bold nil "Test")
  734. '((plain-text . (lambda (text info) "Success"))
  735. (bold . (lambda (bold contents info) (concat contents "!"))))
  736. '(:with-emphasize t)))))
  737. (ert-deftest test-org-export/data-with-backend ()
  738. "Test `org-export-data-with-backend' specifications."
  739. ;; Error when calling an undefined back-end.
  740. (should-error
  741. (let (org-export-registered-backends)
  742. (org-export-data-with-backend 'test "Test" nil)))
  743. ;; Otherwise, export data recursively, using correct back-end.
  744. (should
  745. (equal
  746. "Success!"
  747. (let (org-export-registered-backends)
  748. (org-export-define-backend test
  749. ((plain-text . (lambda (text info) "Success"))
  750. (bold . (lambda (bold contents info) (concat contents "!")))))
  751. (org-export-data-with-backend
  752. '(bold nil "Test") 'test '(:with-emphasize t))))))
  753. ;;; Export Snippets
  754. (ert-deftest test-org-export/export-snippet ()
  755. "Test export snippets transcoding."
  756. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  757. (org-test-with-backend test
  758. (plist-put
  759. (cdr (assq 'test org-export-registered-backends))
  760. :translate-alist
  761. (cons (cons 'export-snippet
  762. (lambda (snippet contents info)
  763. (when (eq (org-export-snippet-backend snippet) 'test)
  764. (org-element-property :value snippet))))
  765. (org-export-backend-translate-table 'test)))
  766. (let ((org-export-snippet-translation-alist nil))
  767. (should (equal (org-export-as 'test) "A\n")))
  768. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  769. (should (equal (org-export-as 'test) "AB\n"))))))
  770. ;;; Footnotes
  771. (ert-deftest test-org-export/footnotes ()
  772. "Test footnotes specifications."
  773. (let ((org-footnote-section nil)
  774. (org-export-with-footnotes t))
  775. ;; 1. Read every type of footnote.
  776. (should
  777. (equal
  778. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  779. (org-test-with-parsed-data
  780. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  781. (org-element-map
  782. tree 'footnote-reference
  783. (lambda (ref)
  784. (let ((def (org-export-get-footnote-definition ref info)))
  785. (cons (org-export-get-footnote-number ref info)
  786. (if (eq (org-element-property :type ref) 'inline) (car def)
  787. (car (org-element-contents
  788. (car (org-element-contents def))))))))
  789. info))))
  790. ;; 2. Test nested footnotes order.
  791. (org-test-with-parsed-data
  792. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  793. (should
  794. (equal
  795. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  796. (org-element-map
  797. tree 'footnote-reference
  798. (lambda (ref)
  799. (when (org-export-footnote-first-reference-p ref info)
  800. (cons (org-export-get-footnote-number ref info)
  801. (org-element-property :label ref))))
  802. info))))
  803. ;; 3. Test nested footnote in invisible definitions.
  804. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  805. ;; Hide definitions.
  806. (narrow-to-region (point) (point-at-eol))
  807. (let* ((tree (org-element-parse-buffer))
  808. (info (org-combine-plists
  809. `(:parse-tree ,tree)
  810. (org-export-collect-tree-properties
  811. tree (org-export-get-environment)))))
  812. ;; Both footnotes should be seen.
  813. (should
  814. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  815. ;; 4. Test footnotes definitions collection.
  816. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  817. \[fn:2] B [fn:3] [fn::D].
  818. \[fn:3] C."
  819. (should (= (length (org-export-collect-footnote-definitions tree info))
  820. 4)))
  821. ;; 5. Test export of footnotes defined outside parsing scope.
  822. (org-test-with-temp-text "[fn:1] Out of scope
  823. * Title
  824. Paragraph[fn:1]"
  825. (org-test-with-backend test
  826. (plist-put
  827. (cdr (assq 'test org-export-registered-backends))
  828. :translate-alist
  829. (cons (cons 'footnote-reference
  830. (lambda (fn contents info)
  831. (org-element-interpret-data
  832. (org-export-get-footnote-definition fn info))))
  833. (org-export-backend-translate-table 'test)))
  834. (forward-line)
  835. (should (equal "ParagraphOut of scope\n"
  836. (org-export-as 'test 'subtree)))))
  837. ;; 6. Footnotes without a definition should be provided a fallback
  838. ;; definition.
  839. (should
  840. (org-test-with-parsed-data "[fn:1]"
  841. (org-export-get-footnote-definition
  842. (org-element-map tree 'footnote-reference 'identity info t) info)))
  843. ;; 7. Footnote section should be ignored in TOC and in headlines
  844. ;; numbering.
  845. (should
  846. (= 1 (let ((org-footnote-section "Footnotes"))
  847. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  848. (org-export-collect-headlines info))))))
  849. (should
  850. (equal '(2)
  851. (let ((org-footnote-section "Footnotes"))
  852. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  853. (org-element-map tree 'headline
  854. (lambda (hl)
  855. (when (equal (org-element-property :raw-value hl) "H2")
  856. (org-export-get-headline-number hl info)))
  857. info t)))))))
  858. ;;; Headlines and Inlinetasks
  859. (ert-deftest test-org-export/get-relative-level ()
  860. "Test `org-export-get-relative-level' specifications."
  861. ;; Standard test.
  862. (should
  863. (equal '(1 2)
  864. (let ((org-odd-levels-only nil))
  865. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  866. (org-element-map
  867. tree 'headline
  868. (lambda (h) (org-export-get-relative-level h info))
  869. info)))))
  870. ;; Missing levels
  871. (should
  872. (equal '(1 3)
  873. (let ((org-odd-levels-only nil))
  874. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  875. (org-element-map
  876. tree 'headline
  877. (lambda (h) (org-export-get-relative-level h info))
  878. info))))))
  879. (ert-deftest test-org-export/low-level-p ()
  880. "Test `org-export-low-level-p' specifications."
  881. (should
  882. (equal
  883. '(no yes)
  884. (let ((org-odd-levels-only nil))
  885. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  886. (org-element-map
  887. tree 'headline
  888. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  889. (plist-put info :headline-levels 1)))))))
  890. (ert-deftest test-org-export/get-headline-number ()
  891. "Test `org-export-get-headline-number' specifications."
  892. ;; Standard test.
  893. (should
  894. (equal
  895. '((1) (1 1))
  896. (let ((org-odd-levels-only nil))
  897. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  898. (org-element-map
  899. tree 'headline
  900. (lambda (h) (org-export-get-headline-number h info))
  901. info)))))
  902. ;; Missing levels are replaced with 0.
  903. (should
  904. (equal
  905. '((1) (1 0 1))
  906. (let ((org-odd-levels-only nil))
  907. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  908. (org-element-map
  909. tree 'headline
  910. (lambda (h) (org-export-get-headline-number h info))
  911. info))))))
  912. (ert-deftest test-org-export/numbered-headline-p ()
  913. "Test `org-export-numbered-headline-p' specifications."
  914. ;; If `:section-numbers' is nil, never number headlines.
  915. (should-not
  916. (org-test-with-parsed-data "* Headline"
  917. (org-element-map
  918. tree 'headline
  919. (lambda (h) (org-export-numbered-headline-p h info))
  920. (plist-put info :section-numbers nil))))
  921. ;; If `:section-numbers' is a number, only number headlines with
  922. ;; a level greater that it.
  923. (should
  924. (equal
  925. '(yes no)
  926. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  927. (org-element-map
  928. tree 'headline
  929. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  930. (plist-put info :section-numbers 1)))))
  931. ;; Otherwise, headlines are always numbered.
  932. (should
  933. (org-test-with-parsed-data "* Headline"
  934. (org-element-map
  935. tree 'headline
  936. (lambda (h) (org-export-numbered-headline-p h info))
  937. (plist-put info :section-numbers t)))))
  938. (ert-deftest test-org-export/number-to-roman ()
  939. "Test `org-export-number-to-roman' specifications."
  940. ;; If number is negative, return it as a string.
  941. (should (equal (org-export-number-to-roman -1) "-1"))
  942. ;; Otherwise, return it as a roman number.
  943. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  944. (ert-deftest test-org-export/get-tags ()
  945. "Test `org-export-get-tags' specifications."
  946. (let ((org-export-exclude-tags '("noexport"))
  947. (org-export-select-tags '("export")))
  948. ;; Standard test: tags which are not a select tag, an exclude tag,
  949. ;; or specified as optional argument shouldn't be ignored.
  950. (should
  951. (org-test-with-parsed-data "* Headline :tag:"
  952. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  953. info)))
  954. ;; Exclude tags are removed.
  955. (should-not
  956. (org-test-with-parsed-data "* Headline :noexport:"
  957. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  958. info)))
  959. ;; Select tags are removed.
  960. (should-not
  961. (org-test-with-parsed-data "* Headline :export:"
  962. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  963. info)))
  964. (should
  965. (equal
  966. '("tag")
  967. (org-test-with-parsed-data "* Headline :tag:export:"
  968. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  969. info))))
  970. ;; Tags provided in the optional argument are also ignored.
  971. (should-not
  972. (org-test-with-parsed-data "* Headline :ignore:"
  973. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  974. info '("ignore"))))
  975. ;; Allow tag inheritance.
  976. (should
  977. (equal
  978. '(("tag") ("tag"))
  979. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  980. (org-element-map
  981. tree 'headline
  982. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  983. ;; Tag inheritance checks FILETAGS keywords.
  984. (should
  985. (equal
  986. '(("a" "b" "tag"))
  987. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  988. (org-element-map
  989. tree 'headline
  990. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  991. (ert-deftest test-org-export/get-node-property ()
  992. "Test`org-export-get-node-property' specifications."
  993. ;; Standard test.
  994. (should
  995. (equal "value"
  996. (org-test-with-parsed-data "* Headline
  997. :PROPERTIES:
  998. :prop: value
  999. :END:"
  1000. (org-export-get-node-property
  1001. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1002. ;; Test inheritance.
  1003. (should
  1004. (equal "value"
  1005. (org-test-with-parsed-data "* Parent
  1006. :PROPERTIES:
  1007. :prop: value
  1008. :END:
  1009. ** Headline
  1010. Paragraph"
  1011. (org-export-get-node-property
  1012. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1013. ;; Cannot return a value before the first headline.
  1014. (should-not
  1015. (org-test-with-parsed-data "Paragraph
  1016. * Headline
  1017. :PROPERTIES:
  1018. :prop: value
  1019. :END:"
  1020. (org-export-get-node-property
  1021. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1022. (ert-deftest test-org-export/get-category ()
  1023. "Test `org-export-get-category' specifications."
  1024. ;; Standard test.
  1025. (should
  1026. (equal "value"
  1027. (org-test-with-parsed-data "* Headline
  1028. :PROPERTIES:
  1029. :CATEGORY: value
  1030. :END:"
  1031. (org-export-get-category
  1032. (org-element-map tree 'headline 'identity nil t) info))))
  1033. ;; Test inheritance from a parent headline.
  1034. (should
  1035. (equal '("value" "value")
  1036. (org-test-with-parsed-data "* Headline1
  1037. :PROPERTIES:
  1038. :CATEGORY: value
  1039. :END:
  1040. ** Headline2"
  1041. (org-element-map
  1042. tree 'headline
  1043. (lambda (hl) (org-export-get-category hl info)) info))))
  1044. ;; Test inheritance from #+CATEGORY keyword
  1045. (should
  1046. (equal "value"
  1047. (org-test-with-parsed-data "#+CATEGORY: value
  1048. * Headline"
  1049. (org-export-get-category
  1050. (org-element-map tree 'headline 'identity nil t) info))))
  1051. ;; Test inheritance from file name.
  1052. (should
  1053. (equal "test"
  1054. (org-test-with-parsed-data "* Headline"
  1055. (let ((info (plist-put info :input-file "~/test.org")))
  1056. (org-export-get-category
  1057. (org-element-map tree 'headline 'identity nil t) info)))))
  1058. ;; Fall-back value.
  1059. (should
  1060. (equal "???"
  1061. (org-test-with-parsed-data "* Headline"
  1062. (org-export-get-category
  1063. (org-element-map tree 'headline 'identity nil t) info)))))
  1064. (ert-deftest test-org-export/first-sibling-p ()
  1065. "Test `org-export-first-sibling-p' specifications."
  1066. ;; Standard test.
  1067. (should
  1068. (equal
  1069. '(yes yes no)
  1070. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1071. (org-element-map
  1072. tree 'headline
  1073. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1074. info))))
  1075. ;; Ignore headlines not exported.
  1076. (should
  1077. (equal
  1078. '(yes)
  1079. (let ((org-export-exclude-tags '("ignore")))
  1080. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1081. (org-element-map
  1082. tree 'headline
  1083. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1084. info))))))
  1085. (ert-deftest test-org-export/last-sibling-p ()
  1086. "Test `org-export-last-sibling-p' specifications."
  1087. ;; Standard test.
  1088. (should
  1089. (equal
  1090. '(yes no yes)
  1091. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1092. (org-element-map
  1093. tree 'headline
  1094. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1095. info))))
  1096. ;; Ignore headlines not exported.
  1097. (should
  1098. (equal
  1099. '(yes)
  1100. (let ((org-export-exclude-tags '("ignore")))
  1101. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1102. (org-element-map
  1103. tree 'headline
  1104. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1105. info))))))
  1106. ;;; Links
  1107. (ert-deftest test-org-export/get-coderef-format ()
  1108. "Test `org-export-get-coderef-format' specifications."
  1109. ;; A link without description returns "%s"
  1110. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1111. "%s"))
  1112. ;; Return "%s" when path is matched within description.
  1113. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1114. "desc %s"))
  1115. ;; Otherwise return description.
  1116. (should (equal (org-export-get-coderef-format "path" "desc")
  1117. "desc")))
  1118. (ert-deftest test-org-export/inline-image-p ()
  1119. "Test `org-export-inline-image-p' specifications."
  1120. (should
  1121. (org-export-inline-image-p
  1122. (org-test-with-temp-text "[[#id]]"
  1123. (org-element-map
  1124. (org-element-parse-buffer) 'link 'identity nil t))
  1125. '(("custom-id" . "id")))))
  1126. (ert-deftest test-org-export/fuzzy-link ()
  1127. "Test fuzzy links specifications."
  1128. ;; 1. Links to invisible (keyword) targets should be ignored.
  1129. (org-test-with-parsed-data
  1130. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  1131. (should-not
  1132. (org-element-map
  1133. tree 'link
  1134. (lambda (link)
  1135. (org-export-get-ordinal
  1136. (org-export-resolve-fuzzy-link link info) info)) info)))
  1137. ;; 2. Link to an headline should return headline's number.
  1138. (org-test-with-parsed-data
  1139. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1140. (should
  1141. ;; Note: Headline's number is in fact a list of numbers.
  1142. (equal '(2)
  1143. (org-element-map
  1144. tree 'link
  1145. (lambda (link)
  1146. (org-export-get-ordinal
  1147. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1148. ;; 3. Link to a target in an item should return item's number.
  1149. (org-test-with-parsed-data
  1150. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1151. (should
  1152. ;; Note: Item's number is in fact a list of numbers.
  1153. (equal '(1 2)
  1154. (org-element-map
  1155. tree 'link
  1156. (lambda (link)
  1157. (org-export-get-ordinal
  1158. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1159. ;; 4. Link to a target in a footnote should return footnote's
  1160. ;; number.
  1161. (org-test-with-parsed-data "
  1162. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1163. (should
  1164. (equal '(2 3)
  1165. (org-element-map
  1166. tree 'link
  1167. (lambda (link)
  1168. (org-export-get-ordinal
  1169. (org-export-resolve-fuzzy-link link info) info)) info))))
  1170. ;; 5. Link to a named element should return sequence number of that
  1171. ;; element.
  1172. (org-test-with-parsed-data
  1173. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1174. (should
  1175. (= 2
  1176. (org-element-map
  1177. tree 'link
  1178. (lambda (link)
  1179. (org-export-get-ordinal
  1180. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1181. ;; 6. Link to a target not within an item, a table, a footnote
  1182. ;; reference or definition should return section number.
  1183. (org-test-with-parsed-data
  1184. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1185. (should
  1186. (equal '(2)
  1187. (org-element-map
  1188. tree 'link
  1189. (lambda (link)
  1190. (org-export-get-ordinal
  1191. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1192. ;; 7. Space are not significant when matching a fuzzy link.
  1193. (should
  1194. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  1195. (org-element-map tree 'link
  1196. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1197. info t)))
  1198. ;; 8. Statistics cookies are ignored for headline match.
  1199. (should
  1200. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  1201. (org-element-map tree 'link
  1202. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1203. info t)))
  1204. (should
  1205. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  1206. (org-element-map tree 'link
  1207. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1208. info t))))
  1209. (ert-deftest test-org-export/resolve-coderef ()
  1210. "Test `org-export-resolve-coderef' specifications."
  1211. (let ((org-coderef-label-format "(ref:%s)"))
  1212. ;; 1. A link to a "-n -k -r" block returns line number.
  1213. (org-test-with-parsed-data
  1214. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1215. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1216. (org-test-with-parsed-data
  1217. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1218. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1219. ;; 2. A link to a "-n -r" block returns line number.
  1220. (org-test-with-parsed-data
  1221. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1222. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1223. (org-test-with-parsed-data
  1224. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1225. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1226. ;; 3. A link to a "-n" block returns coderef.
  1227. (org-test-with-parsed-data
  1228. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1229. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1230. (org-test-with-parsed-data
  1231. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1232. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1233. ;; 4. A link to a "-r" block returns line number.
  1234. (org-test-with-parsed-data
  1235. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1236. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1237. (org-test-with-parsed-data
  1238. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1239. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1240. ;; 5. A link to a block without a switch returns coderef.
  1241. (org-test-with-parsed-data
  1242. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1243. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1244. (org-test-with-parsed-data
  1245. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1246. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1247. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1248. ;; should resume numbering from previous block with numbered
  1249. ;; lines, ignoring blocks not numbering lines in the process.
  1250. ;; A "-n" switch resets count.
  1251. (org-test-with-parsed-data "
  1252. #+BEGIN_EXAMPLE -n
  1253. Text.
  1254. #+END_EXAMPLE
  1255. #+BEGIN_SRC emacs-lisp
  1256. \(- 1 1)
  1257. #+END_SRC
  1258. #+BEGIN_SRC emacs-lisp +n -r
  1259. \(+ 1 1) (ref:addition)
  1260. #+END_SRC
  1261. #+BEGIN_EXAMPLE -n -r
  1262. Another text. (ref:text)
  1263. #+END_EXAMPLE"
  1264. (should (= (org-export-resolve-coderef "addition" info) 2))
  1265. (should (= (org-export-resolve-coderef "text" info) 1)))
  1266. ;; 7. Recognize coderef with user-specified syntax.
  1267. (org-test-with-parsed-data
  1268. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1269. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1270. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1271. "Test `org-export-resolve-fuzzy-link' specifications."
  1272. ;; 1. Match target objects.
  1273. (org-test-with-parsed-data "<<target>> [[target]]"
  1274. (should
  1275. (org-export-resolve-fuzzy-link
  1276. (org-element-map tree 'link 'identity info t) info)))
  1277. ;; 2. Match target elements.
  1278. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  1279. (should
  1280. (org-export-resolve-fuzzy-link
  1281. (org-element-map tree 'link 'identity info t) info)))
  1282. ;; 3. Match named elements.
  1283. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1284. (should
  1285. (org-export-resolve-fuzzy-link
  1286. (org-element-map tree 'link 'identity info t) info)))
  1287. ;; 4. Match exact headline's name.
  1288. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1289. (should
  1290. (org-export-resolve-fuzzy-link
  1291. (org-element-map tree 'link 'identity info t) info)))
  1292. ;; 5. Targets objects have priority over named elements and headline
  1293. ;; titles.
  1294. (org-test-with-parsed-data
  1295. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1296. (should
  1297. (eq 'target
  1298. (org-element-type
  1299. (org-export-resolve-fuzzy-link
  1300. (org-element-map tree 'link 'identity info t) info)))))
  1301. ;; 6. Named elements have priority over headline titles.
  1302. (org-test-with-parsed-data
  1303. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1304. (should
  1305. (eq 'paragraph
  1306. (org-element-type
  1307. (org-export-resolve-fuzzy-link
  1308. (org-element-map tree 'link 'identity info t) info)))))
  1309. ;; 7. If link's path starts with a "*", only match headline titles,
  1310. ;; though.
  1311. (org-test-with-parsed-data
  1312. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1313. (should
  1314. (eq 'headline
  1315. (org-element-type
  1316. (org-export-resolve-fuzzy-link
  1317. (org-element-map tree 'link 'identity info t) info)))))
  1318. ;; 8. Return nil if no match.
  1319. (org-test-with-parsed-data "[[target]]"
  1320. (should-not
  1321. (org-export-resolve-fuzzy-link
  1322. (org-element-map tree 'link 'identity info t) info))))
  1323. (ert-deftest test-org-export/resolve-id-link ()
  1324. "Test `org-export-resolve-id-link' specifications."
  1325. ;; 1. Regular test for custom-id link.
  1326. (org-test-with-parsed-data "* Headline1
  1327. :PROPERTIES:
  1328. :CUSTOM_ID: test
  1329. :END:
  1330. * Headline 2
  1331. \[[#test]]"
  1332. (should
  1333. (org-export-resolve-id-link
  1334. (org-element-map tree 'link 'identity info t) info)))
  1335. ;; 2. Failing test for custom-id link.
  1336. (org-test-with-parsed-data "* Headline1
  1337. :PROPERTIES:
  1338. :CUSTOM_ID: test
  1339. :END:
  1340. * Headline 2
  1341. \[[#no-match]]"
  1342. (should-not
  1343. (org-export-resolve-id-link
  1344. (org-element-map tree 'link 'identity info t) info)))
  1345. ;; 3. Test for internal id target.
  1346. (org-test-with-parsed-data "* Headline1
  1347. :PROPERTIES:
  1348. :ID: aaaa
  1349. :END:
  1350. * Headline 2
  1351. \[[id:aaaa]]"
  1352. (should
  1353. (org-export-resolve-id-link
  1354. (org-element-map tree 'link 'identity info t) info)))
  1355. ;; 4. Test for external id target.
  1356. (org-test-with-parsed-data "[[id:aaaa]]"
  1357. (should
  1358. (org-export-resolve-id-link
  1359. (org-element-map tree 'link 'identity info t)
  1360. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1361. (ert-deftest test-org-export/resolve-radio-link ()
  1362. "Test `org-export-resolve-radio-link' specifications."
  1363. ;; Standard test.
  1364. (should
  1365. (org-test-with-temp-text "<<<radio>>> radio"
  1366. (org-update-radio-target-regexp)
  1367. (let* ((tree (org-element-parse-buffer))
  1368. (info `(:parse-tree ,tree)))
  1369. (org-export-resolve-radio-link
  1370. (org-element-map tree 'link 'identity info t)
  1371. info))))
  1372. ;; Radio targets are case-insensitive.
  1373. (should
  1374. (org-test-with-temp-text "<<<RADIO>>> radio"
  1375. (org-update-radio-target-regexp)
  1376. (let* ((tree (org-element-parse-buffer))
  1377. (info `(:parse-tree ,tree)))
  1378. (org-export-resolve-radio-link
  1379. (org-element-map tree 'link 'identity info t)
  1380. info))))
  1381. ;; Radio target with objects.
  1382. (should
  1383. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1384. (org-update-radio-target-regexp)
  1385. (let* ((tree (org-element-parse-buffer))
  1386. (info `(:parse-tree ,tree)))
  1387. (org-export-resolve-radio-link
  1388. (org-element-map tree 'link 'identity info t)
  1389. info)))))
  1390. ;;; Src-block and example-block
  1391. (ert-deftest test-org-export/unravel-code ()
  1392. "Test `org-export-unravel-code' function."
  1393. (let ((org-coderef-label-format "(ref:%s)"))
  1394. ;; 1. Code without reference.
  1395. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1396. (should (equal (org-export-unravel-code (org-element-at-point))
  1397. '("(+ 1 1)\n"))))
  1398. ;; 2. Code with reference.
  1399. (org-test-with-temp-text
  1400. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1401. (should (equal (org-export-unravel-code (org-element-at-point))
  1402. '("(+ 1 1)\n" (1 . "test")))))
  1403. ;; 3. Code with user-defined reference.
  1404. (org-test-with-temp-text
  1405. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1406. (should (equal (org-export-unravel-code (org-element-at-point))
  1407. '("(+ 1 1)\n" (1 . "test")))))
  1408. ;; 4. Code references keys are relative to the current block.
  1409. (org-test-with-temp-text "
  1410. #+BEGIN_EXAMPLE -n
  1411. \(+ 1 1)
  1412. #+END_EXAMPLE
  1413. #+BEGIN_EXAMPLE +n
  1414. \(+ 2 2)
  1415. \(+ 3 3) (ref:one)
  1416. #+END_EXAMPLE"
  1417. (goto-line 5)
  1418. (should (equal (org-export-unravel-code (org-element-at-point))
  1419. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1420. ;;; Smart Quotes
  1421. (ert-deftest test-org-export/activate-smart-quotes ()
  1422. "Test `org-export-activate-smart-quotes' specifications."
  1423. ;; Opening double quotes: standard test.
  1424. (should
  1425. (equal
  1426. '("some &ldquo;paragraph")
  1427. (let ((org-export-default-language "en"))
  1428. (org-test-with-parsed-data "some \"paragraph"
  1429. (org-element-map
  1430. tree 'plain-text
  1431. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1432. info)))))
  1433. ;; Opening quotes: at the beginning of a paragraph.
  1434. (should
  1435. (equal
  1436. '("&ldquo;begin")
  1437. (let ((org-export-default-language "en"))
  1438. (org-test-with-parsed-data "\"begin"
  1439. (org-element-map
  1440. tree 'plain-text
  1441. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1442. info)))))
  1443. ;; Opening quotes: after an object.
  1444. (should
  1445. (equal
  1446. '("&ldquo;begin")
  1447. (let ((org-export-default-language "en"))
  1448. (org-test-with-parsed-data "=verb= \"begin"
  1449. (org-element-map
  1450. tree 'plain-text
  1451. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1452. info)))))
  1453. ;; Closing quotes: standard test.
  1454. (should
  1455. (equal
  1456. '("some&rdquo; paragraph")
  1457. (let ((org-export-default-language "en"))
  1458. (org-test-with-parsed-data "some\" paragraph"
  1459. (org-element-map
  1460. tree 'plain-text
  1461. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1462. info)))))
  1463. ;; Closing quotes: at the end of a paragraph.
  1464. (should
  1465. (equal
  1466. '("end&rdquo;")
  1467. (let ((org-export-default-language "en"))
  1468. (org-test-with-parsed-data "end\""
  1469. (org-element-map
  1470. tree 'plain-text
  1471. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1472. info)))))
  1473. ;; Apostrophe: standard test.
  1474. (should
  1475. (equal
  1476. '("It shouldn&rsquo;t fail")
  1477. (let ((org-export-default-language "en"))
  1478. (org-test-with-parsed-data "It shouldn't fail"
  1479. (org-element-map
  1480. tree 'plain-text
  1481. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1482. info)))))
  1483. ;; Apostrophe: before an object.
  1484. (should
  1485. (equal
  1486. '("a&rsquo;")
  1487. (let ((org-export-default-language "en"))
  1488. (org-test-with-parsed-data "a'=b="
  1489. (org-element-map
  1490. tree 'plain-text
  1491. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1492. info)))))
  1493. ;; Apostrophe: after an object.
  1494. (should
  1495. (equal
  1496. '("&rsquo;s")
  1497. (let ((org-export-default-language "en"))
  1498. (org-test-with-parsed-data "=code='s"
  1499. (org-element-map
  1500. tree 'plain-text
  1501. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1502. info)))))
  1503. ;; Special case: isolated quotes.
  1504. (should
  1505. (equal '("&ldquo;" "&rdquo;")
  1506. (let ((org-export-default-language "en"))
  1507. (org-test-with-parsed-data "\"$x$\""
  1508. (org-element-map
  1509. tree 'plain-text
  1510. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1511. info)))))
  1512. ;; Smart quotes in secondary strings.
  1513. (should
  1514. (equal '("&ldquo;" "&rdquo;")
  1515. (let ((org-export-default-language "en"))
  1516. (org-test-with-parsed-data "* \"$x$\""
  1517. (org-element-map
  1518. tree 'plain-text
  1519. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1520. info)))))
  1521. ;; Smart quotes in document keywords.
  1522. (should
  1523. (equal '("&ldquo;" "&rdquo;")
  1524. (let ((org-export-default-language "en"))
  1525. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1526. (org-element-map
  1527. (plist-get info :title) 'plain-text
  1528. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1529. info)))))
  1530. ;; Smart quotes in parsed affiliated keywords.
  1531. (should
  1532. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1533. (let ((org-export-default-language "en"))
  1534. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1535. (org-element-map
  1536. tree 'plain-text
  1537. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1538. info nil nil t))))))
  1539. ;;; Tables
  1540. (ert-deftest test-org-export/special-column ()
  1541. "Test if the table's special column is properly recognized."
  1542. ;; 1. First column is special if it contains only a special marking
  1543. ;; characters or empty cells.
  1544. (org-test-with-temp-text "
  1545. | ! | 1 |
  1546. | | 2 |"
  1547. (should
  1548. (org-export-table-has-special-column-p
  1549. (org-element-map
  1550. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1551. ;; 2. If the column contains anything else, it isn't special.
  1552. (org-test-with-temp-text "
  1553. | ! | 1 |
  1554. | b | 2 |"
  1555. (should-not
  1556. (org-export-table-has-special-column-p
  1557. (org-element-map
  1558. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1559. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1560. ;; and "!".
  1561. (org-test-with-temp-text "
  1562. | # | 1 |
  1563. | ^ | 2 |
  1564. | * | 3 |
  1565. | _ | 4 |
  1566. | / | 5 |
  1567. | $ | 6 |
  1568. | ! | 7 |"
  1569. (should
  1570. (org-export-table-has-special-column-p
  1571. (org-element-map
  1572. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1573. ;; 4. A first column with only empty cells isn't considered as
  1574. ;; special.
  1575. (org-test-with-temp-text "
  1576. | | 1 |
  1577. | | 2 |"
  1578. (should-not
  1579. (org-export-table-has-special-column-p
  1580. (org-element-map
  1581. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1582. (ert-deftest test-org-export/table-row-is-special-p ()
  1583. "Test `org-export-table-row-is-special-p' specifications."
  1584. ;; 1. A row is special if it has a special marking character in the
  1585. ;; special column.
  1586. (org-test-with-parsed-data "| ! | 1 |"
  1587. (should
  1588. (org-export-table-row-is-special-p
  1589. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1590. ;; 2. A row is special when its first field is "/"
  1591. (org-test-with-parsed-data "
  1592. | / | 1 |
  1593. | a | b |"
  1594. (should
  1595. (org-export-table-row-is-special-p
  1596. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1597. ;; 3. A row only containing alignment cookies is also considered as
  1598. ;; special.
  1599. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1600. (should
  1601. (org-export-table-row-is-special-p
  1602. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1603. ;; 4. Everything else isn't considered as special.
  1604. (org-test-with-parsed-data "| \alpha | | c |"
  1605. (should-not
  1606. (org-export-table-row-is-special-p
  1607. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1608. ;; 5. Table's rules are never considered as special rows.
  1609. (org-test-with-parsed-data "|---+---|"
  1610. (should-not
  1611. (org-export-table-row-is-special-p
  1612. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1613. (ert-deftest test-org-export/has-header-p ()
  1614. "Test `org-export-table-has-header-p' specifications."
  1615. ;; 1. With an header.
  1616. (org-test-with-parsed-data "
  1617. | a | b |
  1618. |---+---|
  1619. | c | d |"
  1620. (should
  1621. (org-export-table-has-header-p
  1622. (org-element-map tree 'table 'identity info 'first-match)
  1623. info)))
  1624. ;; 2. Without an header.
  1625. (org-test-with-parsed-data "
  1626. | a | b |
  1627. | c | d |"
  1628. (should-not
  1629. (org-export-table-has-header-p
  1630. (org-element-map tree 'table 'identity info 'first-match)
  1631. info)))
  1632. ;; 3. Don't get fooled with starting and ending rules.
  1633. (org-test-with-parsed-data "
  1634. |---+---|
  1635. | a | b |
  1636. | c | d |
  1637. |---+---|"
  1638. (should-not
  1639. (org-export-table-has-header-p
  1640. (org-element-map tree 'table 'identity info 'first-match)
  1641. info))))
  1642. (ert-deftest test-org-export/table-row-group ()
  1643. "Test `org-export-table-row-group' specifications."
  1644. ;; 1. A rule creates a new group.
  1645. (org-test-with-parsed-data "
  1646. | a | b |
  1647. |---+---|
  1648. | 1 | 2 |"
  1649. (should
  1650. (equal
  1651. '(1 nil 2)
  1652. (mapcar (lambda (row) (org-export-table-row-group row info))
  1653. (org-element-map tree 'table-row 'identity)))))
  1654. ;; 2. Special rows are ignored in count.
  1655. (org-test-with-parsed-data "
  1656. | / | < | > |
  1657. |---|---+---|
  1658. | | 1 | 2 |"
  1659. (should
  1660. (equal
  1661. '(nil nil 1)
  1662. (mapcar (lambda (row) (org-export-table-row-group row info))
  1663. (org-element-map tree 'table-row 'identity)))))
  1664. ;; 3. Double rules also are ignored in count.
  1665. (org-test-with-parsed-data "
  1666. | a | b |
  1667. |---+---|
  1668. |---+---|
  1669. | 1 | 2 |"
  1670. (should
  1671. (equal
  1672. '(1 nil nil 2)
  1673. (mapcar (lambda (row) (org-export-table-row-group row info))
  1674. (org-element-map tree 'table-row 'identity))))))
  1675. (ert-deftest test-org-export/table-cell-width ()
  1676. "Test `org-export-table-cell-width' specifications."
  1677. ;; 1. Width is primarily determined by width cookies. If no cookie
  1678. ;; is found, cell's width is nil.
  1679. (org-test-with-parsed-data "
  1680. | / | <l> | <6> | <l7> |
  1681. | | a | b | c |"
  1682. (should
  1683. (equal
  1684. '(nil 6 7)
  1685. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1686. (org-element-map tree 'table-cell 'identity info)))))
  1687. ;; 2. The last width cookie has precedence.
  1688. (org-test-with-parsed-data "
  1689. | <6> |
  1690. | <7> |
  1691. | a |"
  1692. (should
  1693. (equal
  1694. '(7)
  1695. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1696. (org-element-map tree 'table-cell 'identity info)))))
  1697. ;; 3. Valid width cookies must have a specific row.
  1698. (org-test-with-parsed-data "| <6> | cell |"
  1699. (should
  1700. (equal
  1701. '(nil nil)
  1702. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1703. (org-element-map tree 'table-cell 'identity))))))
  1704. (ert-deftest test-org-export/table-cell-alignment ()
  1705. "Test `org-export-table-cell-alignment' specifications."
  1706. (let ((org-table-number-fraction 0.5)
  1707. (org-table-number-regexp "^[0-9]+$"))
  1708. ;; 1. Alignment is primarily determined by alignment cookies.
  1709. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1710. (let* ((tree (org-element-parse-buffer))
  1711. (info `(:parse-tree ,tree)))
  1712. (should
  1713. (equal
  1714. '(left center right)
  1715. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1716. (org-element-map tree 'table-cell 'identity))))))
  1717. ;; 2. The last alignment cookie has precedence.
  1718. (org-test-with-parsed-data "
  1719. | <l8> |
  1720. | cell |
  1721. | <r9> |"
  1722. (should
  1723. (equal
  1724. '(right right right)
  1725. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1726. (org-element-map tree 'table-cell 'identity)))))
  1727. ;; 3. If there's no cookie, cell's contents determine alignment.
  1728. ;; A column mostly made of cells containing numbers will align
  1729. ;; its cells to the right.
  1730. (org-test-with-parsed-data "
  1731. | 123 |
  1732. | some text |
  1733. | 12345 |"
  1734. (should
  1735. (equal
  1736. '(right right right)
  1737. (mapcar (lambda (cell)
  1738. (org-export-table-cell-alignment cell info))
  1739. (org-element-map tree 'table-cell 'identity)))))
  1740. ;; 4. Otherwise, they will be aligned to the left.
  1741. (org-test-with-parsed-data "
  1742. | text |
  1743. | some text |
  1744. | \alpha |"
  1745. (should
  1746. (equal
  1747. '(left left left)
  1748. (mapcar (lambda (cell)
  1749. (org-export-table-cell-alignment cell info))
  1750. (org-element-map tree 'table-cell 'identity)))))))
  1751. (ert-deftest test-org-export/table-cell-borders ()
  1752. "Test `org-export-table-cell-borders' specifications."
  1753. ;; 1. Recognize various column groups indicators.
  1754. (org-test-with-parsed-data "| / | < | > | <> |"
  1755. (should
  1756. (equal
  1757. '((right bottom top) (left bottom top) (right bottom top)
  1758. (right left bottom top))
  1759. (mapcar (lambda (cell)
  1760. (org-export-table-cell-borders cell info))
  1761. (org-element-map tree 'table-cell 'identity)))))
  1762. ;; 2. Accept shortcuts to define column groups.
  1763. (org-test-with-parsed-data "| / | < | < |"
  1764. (should
  1765. (equal
  1766. '((right bottom top) (right left bottom top) (left bottom top))
  1767. (mapcar (lambda (cell)
  1768. (org-export-table-cell-borders cell info))
  1769. (org-element-map tree 'table-cell 'identity)))))
  1770. ;; 3. A valid column groups row must start with a "/".
  1771. (org-test-with-parsed-data "
  1772. | | < |
  1773. | a | b |"
  1774. (should
  1775. (equal '((top) (top) (bottom) (bottom))
  1776. (mapcar (lambda (cell)
  1777. (org-export-table-cell-borders cell info))
  1778. (org-element-map tree 'table-cell 'identity)))))
  1779. ;; 4. Take table rules into consideration.
  1780. (org-test-with-parsed-data "
  1781. | 1 |
  1782. |---|
  1783. | 2 |"
  1784. (should
  1785. (equal '((below top) (bottom above))
  1786. (mapcar (lambda (cell)
  1787. (org-export-table-cell-borders cell info))
  1788. (org-element-map tree 'table-cell 'identity)))))
  1789. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1790. ;; (resp. `bottom' and `below') borders. Any special row is
  1791. ;; ignored.
  1792. (org-test-with-parsed-data "
  1793. |---+----|
  1794. | / | |
  1795. | | 1 |
  1796. |---+----|"
  1797. (should
  1798. (equal '((bottom below top above))
  1799. (last
  1800. (mapcar (lambda (cell)
  1801. (org-export-table-cell-borders cell info))
  1802. (org-element-map tree 'table-cell 'identity)))))))
  1803. (ert-deftest test-org-export/table-dimensions ()
  1804. "Test `org-export-table-dimensions' specifications."
  1805. ;; 1. Standard test.
  1806. (org-test-with-parsed-data "
  1807. | 1 | 2 | 3 |
  1808. | 4 | 5 | 6 |"
  1809. (should
  1810. (equal '(2 . 3)
  1811. (org-export-table-dimensions
  1812. (org-element-map tree 'table 'identity info 'first-match) info))))
  1813. ;; 2. Ignore horizontal rules and special columns.
  1814. (org-test-with-parsed-data "
  1815. | / | < | > |
  1816. | 1 | 2 | 3 |
  1817. |---+---+---|
  1818. | 4 | 5 | 6 |"
  1819. (should
  1820. (equal '(2 . 3)
  1821. (org-export-table-dimensions
  1822. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1823. (ert-deftest test-org-export/table-cell-address ()
  1824. "Test `org-export-table-cell-address' specifications."
  1825. ;; 1. Standard test: index is 0-based.
  1826. (org-test-with-parsed-data "| a | b |"
  1827. (should
  1828. (equal '((0 . 0) (0 . 1))
  1829. (org-element-map
  1830. tree 'table-cell
  1831. (lambda (cell) (org-export-table-cell-address cell info))
  1832. info))))
  1833. ;; 2. Special column isn't counted, nor are special rows.
  1834. (org-test-with-parsed-data "
  1835. | / | <> |
  1836. | | c |"
  1837. (should
  1838. (equal '(0 . 0)
  1839. (org-export-table-cell-address
  1840. (car (last (org-element-map tree 'table-cell 'identity info)))
  1841. info))))
  1842. ;; 3. Tables rules do not count either.
  1843. (org-test-with-parsed-data "
  1844. | a |
  1845. |---|
  1846. | b |
  1847. |---|
  1848. | c |"
  1849. (should
  1850. (equal '(2 . 0)
  1851. (org-export-table-cell-address
  1852. (car (last (org-element-map tree 'table-cell 'identity info)))
  1853. info))))
  1854. ;; 4. Return nil for special cells.
  1855. (org-test-with-parsed-data "| / | a |"
  1856. (should-not
  1857. (org-export-table-cell-address
  1858. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1859. info))))
  1860. (ert-deftest test-org-export/get-table-cell-at ()
  1861. "Test `org-export-get-table-cell-at' specifications."
  1862. ;; 1. Address ignores special columns, special rows and rules.
  1863. (org-test-with-parsed-data "
  1864. | / | <> |
  1865. | | a |
  1866. |---+----|
  1867. | | b |"
  1868. (should
  1869. (equal '("b")
  1870. (org-element-contents
  1871. (org-export-get-table-cell-at
  1872. '(1 . 0)
  1873. (org-element-map tree 'table 'identity info 'first-match)
  1874. info)))))
  1875. ;; 2. Return value for a non-existent address is nil.
  1876. (org-test-with-parsed-data "| a |"
  1877. (should-not
  1878. (org-export-get-table-cell-at
  1879. '(2 . 2)
  1880. (org-element-map tree 'table 'identity info 'first-match)
  1881. info)))
  1882. (org-test-with-parsed-data "| / |"
  1883. (should-not
  1884. (org-export-get-table-cell-at
  1885. '(0 . 0)
  1886. (org-element-map tree 'table 'identity info 'first-match)
  1887. info))))
  1888. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1889. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1890. ;; 1. A cell at a beginning of a row always starts a column group.
  1891. (org-test-with-parsed-data "| a |"
  1892. (should
  1893. (org-export-table-cell-starts-colgroup-p
  1894. (org-element-map tree 'table-cell 'identity info 'first-match)
  1895. info)))
  1896. ;; 2. Special column should be ignored when determining the
  1897. ;; beginning of the row.
  1898. (org-test-with-parsed-data "
  1899. | / | |
  1900. | | a |"
  1901. (should
  1902. (org-export-table-cell-starts-colgroup-p
  1903. (org-element-map tree 'table-cell 'identity info 'first-match)
  1904. info)))
  1905. ;; 2. Explicit column groups.
  1906. (org-test-with-parsed-data "
  1907. | / | | < |
  1908. | a | b | c |"
  1909. (should
  1910. (equal
  1911. '(yes no yes)
  1912. (org-element-map
  1913. tree 'table-cell
  1914. (lambda (cell)
  1915. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1916. info)))))
  1917. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1918. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1919. ;; 1. A cell at the end of a row always ends a column group.
  1920. (org-test-with-parsed-data "| a |"
  1921. (should
  1922. (org-export-table-cell-ends-colgroup-p
  1923. (org-element-map tree 'table-cell 'identity info 'first-match)
  1924. info)))
  1925. ;; 2. Special column should be ignored when determining the
  1926. ;; beginning of the row.
  1927. (org-test-with-parsed-data "
  1928. | / | |
  1929. | | a |"
  1930. (should
  1931. (org-export-table-cell-ends-colgroup-p
  1932. (org-element-map tree 'table-cell 'identity info 'first-match)
  1933. info)))
  1934. ;; 3. Explicit column groups.
  1935. (org-test-with-parsed-data "
  1936. | / | < | |
  1937. | a | b | c |"
  1938. (should
  1939. (equal
  1940. '(yes no yes)
  1941. (org-element-map
  1942. tree 'table-cell
  1943. (lambda (cell)
  1944. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1945. info)))))
  1946. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1947. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1948. ;; 1. A row at the beginning of a table always starts a row group.
  1949. ;; So does a row following a table rule.
  1950. (org-test-with-parsed-data "
  1951. | a |
  1952. |---|
  1953. | b |"
  1954. (should
  1955. (equal
  1956. '(yes no yes)
  1957. (org-element-map
  1958. tree 'table-row
  1959. (lambda (row)
  1960. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1961. info))))
  1962. ;; 2. Special rows should be ignored when determining the beginning
  1963. ;; of the row.
  1964. (org-test-with-parsed-data "
  1965. | / | < |
  1966. | | a |
  1967. |---+---|
  1968. | / | < |
  1969. | | b |"
  1970. (should
  1971. (equal
  1972. '(yes no yes)
  1973. (org-element-map
  1974. tree 'table-row
  1975. (lambda (row)
  1976. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1977. info)))))
  1978. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1979. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1980. ;; 1. A row at the end of a table always ends a row group. So does
  1981. ;; a row preceding a table rule.
  1982. (org-test-with-parsed-data "
  1983. | a |
  1984. |---|
  1985. | b |"
  1986. (should
  1987. (equal
  1988. '(yes no yes)
  1989. (org-element-map
  1990. tree 'table-row
  1991. (lambda (row)
  1992. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1993. info))))
  1994. ;; 2. Special rows should be ignored when determining the beginning
  1995. ;; of the row.
  1996. (org-test-with-parsed-data "
  1997. | | a |
  1998. | / | < |
  1999. |---+---|
  2000. | | b |
  2001. | / | < |"
  2002. (should
  2003. (equal
  2004. '(yes no yes)
  2005. (org-element-map
  2006. tree 'table-row
  2007. (lambda (row)
  2008. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2009. info)))))
  2010. (ert-deftest test-org-export/table-row-starts-header-p ()
  2011. "Test `org-export-table-row-starts-header-p' specifications."
  2012. ;; 1. Only the row starting the first row group starts the table
  2013. ;; header.
  2014. (org-test-with-parsed-data "
  2015. | a |
  2016. | b |
  2017. |---|
  2018. | c |"
  2019. (should
  2020. (equal
  2021. '(yes no no no)
  2022. (org-element-map
  2023. tree 'table-row
  2024. (lambda (row)
  2025. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  2026. info))))
  2027. ;; 2. A row cannot start an header if there's no header in the
  2028. ;; table.
  2029. (org-test-with-parsed-data "
  2030. | a |
  2031. |---|"
  2032. (should-not
  2033. (org-export-table-row-starts-header-p
  2034. (org-element-map tree 'table-row 'identity info 'first-match)
  2035. info))))
  2036. (ert-deftest test-org-export/table-row-ends-header-p ()
  2037. "Test `org-export-table-row-ends-header-p' specifications."
  2038. ;; 1. Only the row starting the first row group starts the table
  2039. ;; header.
  2040. (org-test-with-parsed-data "
  2041. | a |
  2042. | b |
  2043. |---|
  2044. | c |"
  2045. (should
  2046. (equal
  2047. '(no yes no no)
  2048. (org-element-map
  2049. tree 'table-row
  2050. (lambda (row)
  2051. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  2052. info))))
  2053. ;; 2. A row cannot start an header if there's no header in the
  2054. ;; table.
  2055. (org-test-with-parsed-data "
  2056. | a |
  2057. |---|"
  2058. (should-not
  2059. (org-export-table-row-ends-header-p
  2060. (org-element-map tree 'table-row 'identity info 'first-match)
  2061. info))))
  2062. ;;; Templates
  2063. (ert-deftest test-org-export/inner-template ()
  2064. "Test `inner-template' translator specifications."
  2065. (should
  2066. (equal "Success!"
  2067. (let (org-export-registered-backends)
  2068. (org-export-define-backend test
  2069. ((inner-template . (lambda (contents info) "Success!"))
  2070. (headline . (lambda (h c i) "Headline"))))
  2071. (org-test-with-temp-text "* Headline"
  2072. (org-export-as 'test)))))
  2073. ;; Inner template is applied even in a "body-only" export.
  2074. (should
  2075. (equal "Success!"
  2076. (let (org-export-registered-backends)
  2077. (org-export-define-backend test
  2078. ((inner-template . (lambda (contents info) "Success!"))
  2079. (headline . (lambda (h c i) "Headline"))))
  2080. (org-test-with-temp-text "* Headline"
  2081. (org-export-as 'test nil nil 'body-only))))))
  2082. (ert-deftest test-org-export/template ()
  2083. "Test `template' translator specifications."
  2084. (should
  2085. (equal "Success!"
  2086. (let (org-export-registered-backends)
  2087. (org-export-define-backend test
  2088. ((template . (lambda (contents info) "Success!"))
  2089. (headline . (lambda (h c i) "Headline"))))
  2090. (org-test-with-temp-text "* Headline"
  2091. (org-export-as 'test)))))
  2092. ;; Template is not applied in a "body-only" export.
  2093. (should-not
  2094. (equal "Success!"
  2095. (let (org-export-registered-backends)
  2096. (org-export-define-backend test
  2097. ((template . (lambda (contents info) "Success!"))
  2098. (headline . (lambda (h c i) "Headline"))))
  2099. (org-test-with-temp-text "* Headline"
  2100. (org-export-as 'test nil nil 'body-only))))))
  2101. ;;; Topology
  2102. (ert-deftest test-org-export/get-next-element ()
  2103. "Test `org-export-get-next-element' specifications."
  2104. ;; Standard test.
  2105. (should
  2106. (equal "b"
  2107. (org-test-with-parsed-data "* Headline\n*a* b"
  2108. (org-export-get-next-element
  2109. (org-element-map tree 'bold 'identity info t) info))))
  2110. ;; Return nil when no previous element.
  2111. (should-not
  2112. (org-test-with-parsed-data "* Headline\na *b*"
  2113. (org-export-get-next-element
  2114. (org-element-map tree 'bold 'identity info t) info)))
  2115. ;; Non-exportable elements are ignored.
  2116. (should-not
  2117. (let ((org-export-with-timestamps nil))
  2118. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2119. (org-export-get-next-element
  2120. (org-element-map tree 'entity 'identity info t) info))))
  2121. ;; Find next element in secondary strings.
  2122. (should
  2123. (eq 'verbatim
  2124. (org-test-with-parsed-data "* a =verb="
  2125. (org-element-type
  2126. (org-export-get-next-element
  2127. (org-element-map tree 'plain-text 'identity info t) info)))))
  2128. ;; Find next element in document keywords.
  2129. (should
  2130. (eq 'verbatim
  2131. (org-test-with-parsed-data "#+TITLE: a =verb="
  2132. (org-element-type
  2133. (org-export-get-next-element
  2134. (org-element-map
  2135. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2136. ;; Find next element in parsed affiliated keywords.
  2137. (should
  2138. (eq 'verbatim
  2139. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2140. (org-element-type
  2141. (org-export-get-next-element
  2142. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2143. ;; With optional argument N, return a list containing all the
  2144. ;; following elements.
  2145. (should
  2146. (equal
  2147. '(bold code underline)
  2148. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2149. (mapcar 'car
  2150. (org-export-get-next-element
  2151. (org-element-map tree 'italic 'identity info t) info t)))))
  2152. ;; When N is a positive integer, return a list containing up to
  2153. ;; N following elements.
  2154. (should
  2155. (equal
  2156. '(bold code)
  2157. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2158. (mapcar 'car
  2159. (org-export-get-next-element
  2160. (org-element-map tree 'italic 'identity info t) info 2))))))
  2161. (ert-deftest test-org-export/get-previous-element ()
  2162. "Test `org-export-get-previous-element' specifications."
  2163. ;; Standard test.
  2164. (should
  2165. (equal "a "
  2166. (org-test-with-parsed-data "* Headline\na *b*"
  2167. (org-export-get-previous-element
  2168. (org-element-map tree 'bold 'identity info t) info))))
  2169. ;; Return nil when no previous element.
  2170. (should-not
  2171. (org-test-with-parsed-data "* Headline\n*a* b"
  2172. (org-export-get-previous-element
  2173. (org-element-map tree 'bold 'identity info t) info)))
  2174. ;; Non-exportable elements are ignored.
  2175. (should-not
  2176. (let ((org-export-with-timestamps nil))
  2177. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  2178. (org-export-get-previous-element
  2179. (org-element-map tree 'entity 'identity info t) info))))
  2180. ;; Find previous element in secondary strings.
  2181. (should
  2182. (eq 'verbatim
  2183. (org-test-with-parsed-data "* =verb= a"
  2184. (org-element-type
  2185. (org-export-get-previous-element
  2186. (org-element-map tree 'plain-text 'identity info t) info)))))
  2187. ;; Find previous element in document keywords.
  2188. (should
  2189. (eq 'verbatim
  2190. (org-test-with-parsed-data "#+TITLE: =verb= a"
  2191. (org-element-type
  2192. (org-export-get-previous-element
  2193. (org-element-map
  2194. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2195. ;; Find previous element in parsed affiliated keywords.
  2196. (should
  2197. (eq 'verbatim
  2198. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  2199. (org-element-type
  2200. (org-export-get-previous-element
  2201. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2202. ;; With optional argument N, return a list containing up to
  2203. ;; N previous elements.
  2204. (should
  2205. (equal '(bold italic underline)
  2206. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2207. (mapcar 'car
  2208. (org-export-get-previous-element
  2209. (org-element-map tree 'code 'identity info t) info t)))))
  2210. ;; When N is a positive integer, return a list containing up to
  2211. ;; N previous elements.
  2212. (should
  2213. (equal '(bold italic)
  2214. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2215. (mapcar 'car
  2216. (org-export-get-previous-element
  2217. (org-element-map tree 'code 'identity info t) info 2))))))
  2218. (provide 'test-ox)
  2219. ;;; test-org-export.el end here