test-ox.el 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  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. (should
  461. (equal
  462. "* Top heading\n** Heading\nbody\n"
  463. (org-test-with-temp-text
  464. (format
  465. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  466. org-test-dir)
  467. (org-export-expand-include-keyword)
  468. (buffer-string))))
  469. (should
  470. (equal
  471. "* Top heading\n* Heading\nbody\n"
  472. (org-test-with-temp-text
  473. (format
  474. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
  475. org-test-dir)
  476. (org-export-expand-include-keyword)
  477. (buffer-string))))
  478. ;; Inclusion within an example block.
  479. (org-test-with-temp-text
  480. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  481. org-test-dir)
  482. (org-export-expand-include-keyword)
  483. (should
  484. (equal
  485. (buffer-string)
  486. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  487. ;; Inclusion within a src-block.
  488. (org-test-with-temp-text
  489. (format
  490. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  491. org-test-dir)
  492. (org-export-expand-include-keyword)
  493. (should (equal (buffer-string)
  494. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  495. (ert-deftest test-org-export/expand-macro ()
  496. "Test macro expansion in an Org buffer."
  497. ;; Standard macro expansion.
  498. (should
  499. (equal "#+MACRO: macro1 value\nvalue\n"
  500. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  501. (org-test-with-backend test (org-export-as 'test)))))
  502. ;; Expand specific macros.
  503. (should
  504. (equal "me 2012-03-29 me@here Title\n"
  505. (org-test-with-temp-text
  506. "
  507. #+TITLE: Title
  508. #+DATE: 2012-03-29
  509. #+AUTHOR: me
  510. #+EMAIL: me@here
  511. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  512. (let ((output (org-test-with-backend test (org-export-as 'test))))
  513. (substring output (string-match ".*\n\\'" output))))))
  514. ;; Expand specific macros when property contained a regular macro
  515. ;; already.
  516. (should
  517. (equal "value\n"
  518. (org-test-with-temp-text "
  519. #+MACRO: macro1 value
  520. #+TITLE: {{{macro1}}}
  521. {{{title}}}"
  522. (let ((output (org-test-with-backend test (org-export-as 'test))))
  523. (substring output (string-match ".*\n\\'" output))))))
  524. ;; Expand macros with templates in included files.
  525. (should
  526. (equal "success\n"
  527. (org-test-with-temp-text
  528. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  529. {{{included-macro}}}" org-test-dir)
  530. (let ((output (org-test-with-backend test (org-export-as 'test))))
  531. (substring output (string-match ".*\n\\'" output)))))))
  532. (ert-deftest test-org-export/user-ignore-list ()
  533. "Test if `:ignore-list' accepts user input."
  534. (org-test-with-backend test
  535. (flet ((skip-note-head
  536. (data backend info)
  537. ;; Ignore headlines with the word "note" in their title.
  538. (org-element-map
  539. data 'headline
  540. (lambda (headline)
  541. (when (string-match "\\<note\\>"
  542. (org-element-property :raw-value headline))
  543. (org-export-ignore-element headline info)))
  544. info)
  545. data))
  546. ;; Install function in parse tree filters.
  547. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  548. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  549. (should (equal (org-export-as 'test) "* Head1\n")))))))
  550. (ert-deftest test-org-export/before-processing-hook ()
  551. "Test `org-export-before-processing-hook'."
  552. (should
  553. (equal
  554. "#+MACRO: mac val\nTest\n"
  555. (org-test-with-backend test
  556. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  557. (let ((org-export-before-processing-hook
  558. '((lambda (backend)
  559. (while (re-search-forward "{{{" nil t)
  560. (let ((object (org-element-context)))
  561. (when (eq (org-element-type object) 'macro)
  562. (delete-region
  563. (org-element-property :begin object)
  564. (org-element-property :end object)))))))))
  565. (org-export-as 'test)))))))
  566. (ert-deftest test-org-export/before-parsing-hook ()
  567. "Test `org-export-before-parsing-hook'."
  568. (should
  569. (equal "Body 1\nBody 2\n"
  570. (org-test-with-backend test
  571. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  572. (let ((org-export-before-parsing-hook
  573. '((lambda (backend)
  574. (goto-char (point-min))
  575. (while (re-search-forward org-outline-regexp-bol nil t)
  576. (delete-region
  577. (point-at-bol) (progn (forward-line) (point))))))))
  578. (org-export-as 'test)))))))
  579. ;;; Affiliated Keywords
  580. (ert-deftest test-org-export/read-attribute ()
  581. "Test `org-export-read-attribute' specifications."
  582. ;; Standard test.
  583. (should
  584. (equal
  585. (org-export-read-attribute
  586. :attr_html
  587. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  588. (org-element-at-point)))
  589. '(:a 1 :b 2)))
  590. ;; Return nil on empty attribute.
  591. (should-not
  592. (org-export-read-attribute
  593. :attr_html
  594. (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
  595. (ert-deftest test-org-export/get-caption ()
  596. "Test `org-export-get-caption' specifications."
  597. ;; Without optional argument, return long caption
  598. (should
  599. (equal
  600. '("l")
  601. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  602. (org-export-get-caption (org-element-at-point)))))
  603. ;; With optional argument, return short caption.
  604. (should
  605. (equal
  606. '("s")
  607. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  608. (org-export-get-caption (org-element-at-point) t))))
  609. ;; Multiple lines are separated by white spaces.
  610. (should
  611. (equal
  612. '("a" " " "b")
  613. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  614. (org-export-get-caption (org-element-at-point))))))
  615. ;;; Back-End Tools
  616. (ert-deftest test-org-export/define-backend ()
  617. "Test back-end definition and accessors."
  618. ;; Translate table.
  619. (should
  620. (equal '((headline . my-headline-test))
  621. (let (org-export-registered-backends)
  622. (org-export-define-backend test ((headline . my-headline-test)))
  623. (org-export-backend-translate-table 'test))))
  624. ;; Filters.
  625. (should
  626. (equal '((:filter-headline . my-filter))
  627. (let (org-export-registered-backends)
  628. (org-export-define-backend test
  629. ((headline . my-headline-test))
  630. :filters-alist ((:filter-headline . my-filter)))
  631. (org-export-backend-filters 'test))))
  632. ;; Options.
  633. (should
  634. (equal '((:prop value))
  635. (let (org-export-registered-backends)
  636. (org-export-define-backend test
  637. ((headline . my-headline-test))
  638. :options-alist ((:prop value)))
  639. (org-export-backend-options 'test))))
  640. ;; Menu.
  641. (should
  642. (equal '(?k "Test Export" test)
  643. (let (org-export-registered-backends)
  644. (org-export-define-backend test
  645. ((headline . my-headline-test))
  646. :menu-entry (?k "Test Export" test))
  647. (org-export-backend-menu 'test))))
  648. ;; Export Blocks.
  649. (should
  650. (equal '(("TEST" . org-element-export-block-parser))
  651. (let (org-export-registered-backends org-element-block-name-alist)
  652. (org-export-define-backend test
  653. ((headline . my-headline-test))
  654. :export-block ("test"))
  655. org-element-block-name-alist))))
  656. (ert-deftest test-org-export/define-derived-backend ()
  657. "Test `org-export-define-derived-backend' specifications."
  658. ;; Error when parent back-end is not defined.
  659. (should-error
  660. (let (org-export-registered-backends)
  661. (org-export-define-derived-backend test parent)))
  662. ;; Append translation table to parent's.
  663. (should
  664. (equal '((:headline . test) (:headline . parent))
  665. (let (org-export-registered-backends)
  666. (org-export-define-backend parent ((:headline . parent)))
  667. (org-export-define-derived-backend test parent
  668. :translate-alist ((:headline . test)))
  669. (org-export-backend-translate-table 'test))))
  670. ;; Options defined in the new back have priority over those defined
  671. ;; in parent.
  672. (should
  673. (eq 'test
  674. (let (org-export-registered-backends)
  675. (org-export-define-backend parent
  676. ((:headline . parent))
  677. :options-alist ((:a nil nil 'parent)))
  678. (org-export-define-derived-backend test parent
  679. :options-alist ((:a nil nil 'test)))
  680. (plist-get (org-export--get-global-options 'test) :a)))))
  681. (ert-deftest test-org-export/derived-backend-p ()
  682. "Test `org-export-derived-backend-p' specifications."
  683. ;; Non-nil with direct match.
  684. (should
  685. (let (org-export-registered-backends)
  686. (org-export-define-backend test ((headline . test)))
  687. (org-export-derived-backend-p 'test 'test)))
  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-derived-backend-p 'test2 'test2)))
  693. ;; Non-nil with a direct parent.
  694. (should
  695. (let (org-export-registered-backends)
  696. (org-export-define-backend test ((headline . test)))
  697. (org-export-define-derived-backend test2 test)
  698. (org-export-derived-backend-p 'test2 'test)))
  699. ;; Non-nil with an indirect parent.
  700. (should
  701. (let (org-export-registered-backends)
  702. (org-export-define-backend test ((headline . test)))
  703. (org-export-define-derived-backend test2 test)
  704. (org-export-define-derived-backend test3 test2)
  705. (org-export-derived-backend-p 'test3 'test)))
  706. ;; Nil otherwise.
  707. (should-not
  708. (let (org-export-registered-backends)
  709. (org-export-define-backend test ((headline . test)))
  710. (org-export-define-backend test2 ((headline . test2)))
  711. (org-export-derived-backend-p 'test2 'test)))
  712. (should-not
  713. (let (org-export-registered-backends)
  714. (org-export-define-backend test ((headline . test)))
  715. (org-export-define-backend test2 ((headline . test2)))
  716. (org-export-define-derived-backend test3 test2)
  717. (org-export-derived-backend-p 'test3 'test))))
  718. (ert-deftest test-org-export/with-backend ()
  719. "Test `org-export-with-backend' definition."
  720. ;; Error when calling an undefined back-end
  721. (should-error
  722. (let (org-export-registered-backends)
  723. (org-export-with-backend 'test "Test")))
  724. ;; Error when called back-end doesn't have an appropriate
  725. ;; transcoder.
  726. (should-error
  727. (let (org-export-registered-backends)
  728. (org-export-define-backend test ((headline . ignore)))
  729. (org-export-with-backend 'test "Test")))
  730. ;; Otherwise, export using correct transcoder
  731. (should
  732. (equal "Success"
  733. (let (org-export-registered-backends)
  734. (org-export-define-backend test
  735. ((plain-text . (lambda (text contents info) "Failure"))))
  736. (org-export-define-backend test2
  737. ((plain-text . (lambda (text contents info) "Success"))))
  738. (org-export-with-backend 'test2 "Test")))))
  739. (ert-deftest test-org-export/data-with-translations ()
  740. "Test `org-export-data-with-translations' specifications."
  741. (should
  742. (equal
  743. "Success!"
  744. (org-export-data-with-translations
  745. '(bold nil "Test")
  746. '((plain-text . (lambda (text info) "Success"))
  747. (bold . (lambda (bold contents info) (concat contents "!"))))
  748. '(:with-emphasize t)))))
  749. (ert-deftest test-org-export/data-with-backend ()
  750. "Test `org-export-data-with-backend' specifications."
  751. ;; Error when calling an undefined back-end.
  752. (should-error
  753. (let (org-export-registered-backends)
  754. (org-export-data-with-backend 'test "Test" nil)))
  755. ;; Otherwise, export data recursively, using correct back-end.
  756. (should
  757. (equal
  758. "Success!"
  759. (let (org-export-registered-backends)
  760. (org-export-define-backend test
  761. ((plain-text . (lambda (text info) "Success"))
  762. (bold . (lambda (bold contents info) (concat contents "!")))))
  763. (org-export-data-with-backend
  764. '(bold nil "Test") 'test '(:with-emphasize t))))))
  765. ;;; Export Snippets
  766. (ert-deftest test-org-export/export-snippet ()
  767. "Test export snippets transcoding."
  768. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  769. (org-test-with-backend test
  770. (plist-put
  771. (cdr (assq 'test org-export-registered-backends))
  772. :translate-alist
  773. (cons (cons 'export-snippet
  774. (lambda (snippet contents info)
  775. (when (eq (org-export-snippet-backend snippet) 'test)
  776. (org-element-property :value snippet))))
  777. (org-export-backend-translate-table 'test)))
  778. (let ((org-export-snippet-translation-alist nil))
  779. (should (equal (org-export-as 'test) "A\n")))
  780. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  781. (should (equal (org-export-as 'test) "AB\n"))))))
  782. ;;; Footnotes
  783. (ert-deftest test-org-export/footnotes ()
  784. "Test footnotes specifications."
  785. (let ((org-footnote-section nil)
  786. (org-export-with-footnotes t))
  787. ;; 1. Read every type of footnote.
  788. (should
  789. (equal
  790. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  791. (org-test-with-parsed-data
  792. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  793. (org-element-map
  794. tree 'footnote-reference
  795. (lambda (ref)
  796. (let ((def (org-export-get-footnote-definition ref info)))
  797. (cons (org-export-get-footnote-number ref info)
  798. (if (eq (org-element-property :type ref) 'inline) (car def)
  799. (car (org-element-contents
  800. (car (org-element-contents def))))))))
  801. info))))
  802. ;; 2. Test nested footnotes order.
  803. (org-test-with-parsed-data
  804. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  805. (should
  806. (equal
  807. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  808. (org-element-map
  809. tree 'footnote-reference
  810. (lambda (ref)
  811. (when (org-export-footnote-first-reference-p ref info)
  812. (cons (org-export-get-footnote-number ref info)
  813. (org-element-property :label ref))))
  814. info))))
  815. ;; 3. Test nested footnote in invisible definitions.
  816. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  817. ;; Hide definitions.
  818. (narrow-to-region (point) (point-at-eol))
  819. (let* ((tree (org-element-parse-buffer))
  820. (info (org-combine-plists
  821. `(:parse-tree ,tree)
  822. (org-export-collect-tree-properties
  823. tree (org-export-get-environment)))))
  824. ;; Both footnotes should be seen.
  825. (should
  826. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  827. ;; 4. Test footnotes definitions collection.
  828. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  829. \[fn:2] B [fn:3] [fn::D].
  830. \[fn:3] C."
  831. (should (= (length (org-export-collect-footnote-definitions tree info))
  832. 4)))
  833. ;; 5. Test export of footnotes defined outside parsing scope.
  834. (org-test-with-temp-text "[fn:1] Out of scope
  835. * Title
  836. Paragraph[fn:1]"
  837. (org-test-with-backend test
  838. (plist-put
  839. (cdr (assq 'test org-export-registered-backends))
  840. :translate-alist
  841. (cons (cons 'footnote-reference
  842. (lambda (fn contents info)
  843. (org-element-interpret-data
  844. (org-export-get-footnote-definition fn info))))
  845. (org-export-backend-translate-table 'test)))
  846. (forward-line)
  847. (should (equal "ParagraphOut of scope\n"
  848. (org-export-as 'test 'subtree)))))
  849. ;; 6. Footnotes without a definition should be provided a fallback
  850. ;; definition.
  851. (should
  852. (org-test-with-parsed-data "[fn:1]"
  853. (org-export-get-footnote-definition
  854. (org-element-map tree 'footnote-reference 'identity info t) info)))
  855. ;; 7. Footnote section should be ignored in TOC and in headlines
  856. ;; numbering.
  857. (should
  858. (= 1 (let ((org-footnote-section "Footnotes"))
  859. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  860. (org-export-collect-headlines info))))))
  861. (should
  862. (equal '(2)
  863. (let ((org-footnote-section "Footnotes"))
  864. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  865. (org-element-map tree 'headline
  866. (lambda (hl)
  867. (when (equal (org-element-property :raw-value hl) "H2")
  868. (org-export-get-headline-number hl info)))
  869. info t)))))))
  870. ;;; Headlines and Inlinetasks
  871. (ert-deftest test-org-export/get-relative-level ()
  872. "Test `org-export-get-relative-level' specifications."
  873. ;; Standard test.
  874. (should
  875. (equal '(1 2)
  876. (let ((org-odd-levels-only nil))
  877. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  878. (org-element-map
  879. tree 'headline
  880. (lambda (h) (org-export-get-relative-level h info))
  881. info)))))
  882. ;; Missing levels
  883. (should
  884. (equal '(1 3)
  885. (let ((org-odd-levels-only nil))
  886. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  887. (org-element-map
  888. tree 'headline
  889. (lambda (h) (org-export-get-relative-level h info))
  890. info))))))
  891. (ert-deftest test-org-export/low-level-p ()
  892. "Test `org-export-low-level-p' specifications."
  893. (should
  894. (equal
  895. '(no yes)
  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) (if (org-export-low-level-p h info) 'yes 'no))
  901. (plist-put info :headline-levels 1)))))))
  902. (ert-deftest test-org-export/get-headline-number ()
  903. "Test `org-export-get-headline-number' specifications."
  904. ;; Standard test.
  905. (should
  906. (equal
  907. '((1) (1 1))
  908. (let ((org-odd-levels-only nil))
  909. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  910. (org-element-map
  911. tree 'headline
  912. (lambda (h) (org-export-get-headline-number h info))
  913. info)))))
  914. ;; Missing levels are replaced with 0.
  915. (should
  916. (equal
  917. '((1) (1 0 1))
  918. (let ((org-odd-levels-only nil))
  919. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  920. (org-element-map
  921. tree 'headline
  922. (lambda (h) (org-export-get-headline-number h info))
  923. info))))))
  924. (ert-deftest test-org-export/numbered-headline-p ()
  925. "Test `org-export-numbered-headline-p' specifications."
  926. ;; If `:section-numbers' is nil, never number headlines.
  927. (should-not
  928. (org-test-with-parsed-data "* Headline"
  929. (org-element-map
  930. tree 'headline
  931. (lambda (h) (org-export-numbered-headline-p h info))
  932. (plist-put info :section-numbers nil))))
  933. ;; If `:section-numbers' is a number, only number headlines with
  934. ;; a level greater that it.
  935. (should
  936. (equal
  937. '(yes no)
  938. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  939. (org-element-map
  940. tree 'headline
  941. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  942. (plist-put info :section-numbers 1)))))
  943. ;; Otherwise, headlines are always numbered.
  944. (should
  945. (org-test-with-parsed-data "* Headline"
  946. (org-element-map
  947. tree 'headline
  948. (lambda (h) (org-export-numbered-headline-p h info))
  949. (plist-put info :section-numbers t)))))
  950. (ert-deftest test-org-export/number-to-roman ()
  951. "Test `org-export-number-to-roman' specifications."
  952. ;; If number is negative, return it as a string.
  953. (should (equal (org-export-number-to-roman -1) "-1"))
  954. ;; Otherwise, return it as a roman number.
  955. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  956. (ert-deftest test-org-export/get-tags ()
  957. "Test `org-export-get-tags' specifications."
  958. (let ((org-export-exclude-tags '("noexport"))
  959. (org-export-select-tags '("export")))
  960. ;; Standard test: tags which are not a select tag, an exclude tag,
  961. ;; or specified as optional argument shouldn't be ignored.
  962. (should
  963. (org-test-with-parsed-data "* Headline :tag:"
  964. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  965. info)))
  966. ;; Exclude tags are removed.
  967. (should-not
  968. (org-test-with-parsed-data "* Headline :noexport:"
  969. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  970. info)))
  971. ;; Select tags are removed.
  972. (should-not
  973. (org-test-with-parsed-data "* Headline :export:"
  974. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  975. info)))
  976. (should
  977. (equal
  978. '("tag")
  979. (org-test-with-parsed-data "* Headline :tag:export:"
  980. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  981. info))))
  982. ;; Tags provided in the optional argument are also ignored.
  983. (should-not
  984. (org-test-with-parsed-data "* Headline :ignore:"
  985. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  986. info '("ignore"))))
  987. ;; Allow tag inheritance.
  988. (should
  989. (equal
  990. '(("tag") ("tag"))
  991. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  992. (org-element-map
  993. tree 'headline
  994. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  995. ;; Tag inheritance checks FILETAGS keywords.
  996. (should
  997. (equal
  998. '(("a" "b" "tag"))
  999. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1000. (org-element-map
  1001. tree 'headline
  1002. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1003. (ert-deftest test-org-export/get-node-property ()
  1004. "Test`org-export-get-node-property' specifications."
  1005. ;; Standard test.
  1006. (should
  1007. (equal "value"
  1008. (org-test-with-parsed-data "* Headline
  1009. :PROPERTIES:
  1010. :prop: value
  1011. :END:"
  1012. (org-export-get-node-property
  1013. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1014. ;; Test inheritance.
  1015. (should
  1016. (equal "value"
  1017. (org-test-with-parsed-data "* Parent
  1018. :PROPERTIES:
  1019. :prop: value
  1020. :END:
  1021. ** Headline
  1022. Paragraph"
  1023. (org-export-get-node-property
  1024. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1025. ;; Cannot return a value before the first headline.
  1026. (should-not
  1027. (org-test-with-parsed-data "Paragraph
  1028. * Headline
  1029. :PROPERTIES:
  1030. :prop: value
  1031. :END:"
  1032. (org-export-get-node-property
  1033. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1034. (ert-deftest test-org-export/get-category ()
  1035. "Test `org-export-get-category' specifications."
  1036. ;; Standard test.
  1037. (should
  1038. (equal "value"
  1039. (org-test-with-parsed-data "* Headline
  1040. :PROPERTIES:
  1041. :CATEGORY: value
  1042. :END:"
  1043. (org-export-get-category
  1044. (org-element-map tree 'headline 'identity nil t) info))))
  1045. ;; Test inheritance from a parent headline.
  1046. (should
  1047. (equal '("value" "value")
  1048. (org-test-with-parsed-data "* Headline1
  1049. :PROPERTIES:
  1050. :CATEGORY: value
  1051. :END:
  1052. ** Headline2"
  1053. (org-element-map
  1054. tree 'headline
  1055. (lambda (hl) (org-export-get-category hl info)) info))))
  1056. ;; Test inheritance from #+CATEGORY keyword
  1057. (should
  1058. (equal "value"
  1059. (org-test-with-parsed-data "#+CATEGORY: value
  1060. * Headline"
  1061. (org-export-get-category
  1062. (org-element-map tree 'headline 'identity nil t) info))))
  1063. ;; Test inheritance from file name.
  1064. (should
  1065. (equal "test"
  1066. (org-test-with-parsed-data "* Headline"
  1067. (let ((info (plist-put info :input-file "~/test.org")))
  1068. (org-export-get-category
  1069. (org-element-map tree 'headline 'identity nil t) info)))))
  1070. ;; Fall-back value.
  1071. (should
  1072. (equal "???"
  1073. (org-test-with-parsed-data "* Headline"
  1074. (org-export-get-category
  1075. (org-element-map tree 'headline 'identity nil t) info)))))
  1076. (ert-deftest test-org-export/first-sibling-p ()
  1077. "Test `org-export-first-sibling-p' specifications."
  1078. ;; Standard test.
  1079. (should
  1080. (equal
  1081. '(yes yes no)
  1082. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1083. (org-element-map
  1084. tree 'headline
  1085. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1086. info))))
  1087. ;; Ignore headlines not exported.
  1088. (should
  1089. (equal
  1090. '(yes)
  1091. (let ((org-export-exclude-tags '("ignore")))
  1092. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1093. (org-element-map
  1094. tree 'headline
  1095. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1096. info))))))
  1097. (ert-deftest test-org-export/last-sibling-p ()
  1098. "Test `org-export-last-sibling-p' specifications."
  1099. ;; Standard test.
  1100. (should
  1101. (equal
  1102. '(yes no yes)
  1103. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1104. (org-element-map
  1105. tree 'headline
  1106. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1107. info))))
  1108. ;; Ignore headlines not exported.
  1109. (should
  1110. (equal
  1111. '(yes)
  1112. (let ((org-export-exclude-tags '("ignore")))
  1113. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1114. (org-element-map
  1115. tree 'headline
  1116. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1117. info))))))
  1118. ;;; Links
  1119. (ert-deftest test-org-export/get-coderef-format ()
  1120. "Test `org-export-get-coderef-format' specifications."
  1121. ;; A link without description returns "%s"
  1122. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1123. "%s"))
  1124. ;; Return "%s" when path is matched within description.
  1125. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1126. "desc %s"))
  1127. ;; Otherwise return description.
  1128. (should (equal (org-export-get-coderef-format "path" "desc")
  1129. "desc")))
  1130. (ert-deftest test-org-export/inline-image-p ()
  1131. "Test `org-export-inline-image-p' specifications."
  1132. (should
  1133. (org-export-inline-image-p
  1134. (org-test-with-temp-text "[[#id]]"
  1135. (org-element-map
  1136. (org-element-parse-buffer) 'link 'identity nil t))
  1137. '(("custom-id" . "id")))))
  1138. (ert-deftest test-org-export/fuzzy-link ()
  1139. "Test fuzzy links specifications."
  1140. ;; 1. Links to invisible (keyword) targets should be ignored.
  1141. (org-test-with-parsed-data
  1142. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  1143. (should-not
  1144. (org-element-map
  1145. tree 'link
  1146. (lambda (link)
  1147. (org-export-get-ordinal
  1148. (org-export-resolve-fuzzy-link link info) info)) info)))
  1149. ;; 2. Link to an headline should return headline's number.
  1150. (org-test-with-parsed-data
  1151. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1152. (should
  1153. ;; Note: Headline's number is in fact a list of numbers.
  1154. (equal '(2)
  1155. (org-element-map
  1156. tree 'link
  1157. (lambda (link)
  1158. (org-export-get-ordinal
  1159. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1160. ;; 3. Link to a target in an item should return item's number.
  1161. (org-test-with-parsed-data
  1162. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1163. (should
  1164. ;; Note: Item's number is in fact a list of numbers.
  1165. (equal '(1 2)
  1166. (org-element-map
  1167. tree 'link
  1168. (lambda (link)
  1169. (org-export-get-ordinal
  1170. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1171. ;; 4. Link to a target in a footnote should return footnote's
  1172. ;; number.
  1173. (org-test-with-parsed-data "
  1174. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1175. (should
  1176. (equal '(2 3)
  1177. (org-element-map
  1178. tree 'link
  1179. (lambda (link)
  1180. (org-export-get-ordinal
  1181. (org-export-resolve-fuzzy-link link info) info)) info))))
  1182. ;; 5. Link to a named element should return sequence number of that
  1183. ;; element.
  1184. (org-test-with-parsed-data
  1185. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1186. (should
  1187. (= 2
  1188. (org-element-map
  1189. tree 'link
  1190. (lambda (link)
  1191. (org-export-get-ordinal
  1192. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1193. ;; 6. Link to a target not within an item, a table, a footnote
  1194. ;; reference or definition should return section number.
  1195. (org-test-with-parsed-data
  1196. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1197. (should
  1198. (equal '(2)
  1199. (org-element-map
  1200. tree 'link
  1201. (lambda (link)
  1202. (org-export-get-ordinal
  1203. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1204. ;; 7. Space are not significant when matching a fuzzy link.
  1205. (should
  1206. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  1207. (org-element-map tree 'link
  1208. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1209. info t)))
  1210. ;; 8. Statistics cookies are ignored for headline match.
  1211. (should
  1212. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  1213. (org-element-map tree 'link
  1214. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1215. info t)))
  1216. (should
  1217. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  1218. (org-element-map tree 'link
  1219. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1220. info t))))
  1221. (ert-deftest test-org-export/resolve-coderef ()
  1222. "Test `org-export-resolve-coderef' specifications."
  1223. (let ((org-coderef-label-format "(ref:%s)"))
  1224. ;; 1. A link to a "-n -k -r" block returns line number.
  1225. (org-test-with-parsed-data
  1226. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1227. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1228. (org-test-with-parsed-data
  1229. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1230. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1231. ;; 2. A link to a "-n -r" block returns line number.
  1232. (org-test-with-parsed-data
  1233. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1234. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1235. (org-test-with-parsed-data
  1236. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1237. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1238. ;; 3. A link to a "-n" block returns coderef.
  1239. (org-test-with-parsed-data
  1240. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1241. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1242. (org-test-with-parsed-data
  1243. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1244. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1245. ;; 4. A link to a "-r" block returns line number.
  1246. (org-test-with-parsed-data
  1247. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1248. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1249. (org-test-with-parsed-data
  1250. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1251. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1252. ;; 5. A link to a block without a switch returns coderef.
  1253. (org-test-with-parsed-data
  1254. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1255. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1256. (org-test-with-parsed-data
  1257. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1258. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1259. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1260. ;; should resume numbering from previous block with numbered
  1261. ;; lines, ignoring blocks not numbering lines in the process.
  1262. ;; A "-n" switch resets count.
  1263. (org-test-with-parsed-data "
  1264. #+BEGIN_EXAMPLE -n
  1265. Text.
  1266. #+END_EXAMPLE
  1267. #+BEGIN_SRC emacs-lisp
  1268. \(- 1 1)
  1269. #+END_SRC
  1270. #+BEGIN_SRC emacs-lisp +n -r
  1271. \(+ 1 1) (ref:addition)
  1272. #+END_SRC
  1273. #+BEGIN_EXAMPLE -n -r
  1274. Another text. (ref:text)
  1275. #+END_EXAMPLE"
  1276. (should (= (org-export-resolve-coderef "addition" info) 2))
  1277. (should (= (org-export-resolve-coderef "text" info) 1)))
  1278. ;; 7. Recognize coderef with user-specified syntax.
  1279. (org-test-with-parsed-data
  1280. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1281. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1282. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1283. "Test `org-export-resolve-fuzzy-link' specifications."
  1284. ;; 1. Match target objects.
  1285. (org-test-with-parsed-data "<<target>> [[target]]"
  1286. (should
  1287. (org-export-resolve-fuzzy-link
  1288. (org-element-map tree 'link 'identity info t) info)))
  1289. ;; 2. Match target elements.
  1290. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  1291. (should
  1292. (org-export-resolve-fuzzy-link
  1293. (org-element-map tree 'link 'identity info t) info)))
  1294. ;; 3. Match named elements.
  1295. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1296. (should
  1297. (org-export-resolve-fuzzy-link
  1298. (org-element-map tree 'link 'identity info t) info)))
  1299. ;; 4. Match exact headline's name.
  1300. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1301. (should
  1302. (org-export-resolve-fuzzy-link
  1303. (org-element-map tree 'link 'identity info t) info)))
  1304. ;; 5. Targets objects have priority over named elements and headline
  1305. ;; titles.
  1306. (org-test-with-parsed-data
  1307. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1308. (should
  1309. (eq 'target
  1310. (org-element-type
  1311. (org-export-resolve-fuzzy-link
  1312. (org-element-map tree 'link 'identity info t) info)))))
  1313. ;; 6. Named elements have priority over headline titles.
  1314. (org-test-with-parsed-data
  1315. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1316. (should
  1317. (eq 'paragraph
  1318. (org-element-type
  1319. (org-export-resolve-fuzzy-link
  1320. (org-element-map tree 'link 'identity info t) info)))))
  1321. ;; 7. If link's path starts with a "*", only match headline titles,
  1322. ;; though.
  1323. (org-test-with-parsed-data
  1324. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1325. (should
  1326. (eq 'headline
  1327. (org-element-type
  1328. (org-export-resolve-fuzzy-link
  1329. (org-element-map tree 'link 'identity info t) info)))))
  1330. ;; 8. Return nil if no match.
  1331. (org-test-with-parsed-data "[[target]]"
  1332. (should-not
  1333. (org-export-resolve-fuzzy-link
  1334. (org-element-map tree 'link 'identity info t) info))))
  1335. (ert-deftest test-org-export/resolve-id-link ()
  1336. "Test `org-export-resolve-id-link' specifications."
  1337. ;; 1. Regular test for custom-id link.
  1338. (org-test-with-parsed-data "* Headline1
  1339. :PROPERTIES:
  1340. :CUSTOM_ID: test
  1341. :END:
  1342. * Headline 2
  1343. \[[#test]]"
  1344. (should
  1345. (org-export-resolve-id-link
  1346. (org-element-map tree 'link 'identity info t) info)))
  1347. ;; 2. Failing test for custom-id link.
  1348. (org-test-with-parsed-data "* Headline1
  1349. :PROPERTIES:
  1350. :CUSTOM_ID: test
  1351. :END:
  1352. * Headline 2
  1353. \[[#no-match]]"
  1354. (should-not
  1355. (org-export-resolve-id-link
  1356. (org-element-map tree 'link 'identity info t) info)))
  1357. ;; 3. Test for internal id target.
  1358. (org-test-with-parsed-data "* Headline1
  1359. :PROPERTIES:
  1360. :ID: aaaa
  1361. :END:
  1362. * Headline 2
  1363. \[[id:aaaa]]"
  1364. (should
  1365. (org-export-resolve-id-link
  1366. (org-element-map tree 'link 'identity info t) info)))
  1367. ;; 4. Test for external id target.
  1368. (org-test-with-parsed-data "[[id:aaaa]]"
  1369. (should
  1370. (org-export-resolve-id-link
  1371. (org-element-map tree 'link 'identity info t)
  1372. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1373. (ert-deftest test-org-export/resolve-radio-link ()
  1374. "Test `org-export-resolve-radio-link' specifications."
  1375. ;; Standard test.
  1376. (should
  1377. (org-test-with-temp-text "<<<radio>>> radio"
  1378. (org-update-radio-target-regexp)
  1379. (let* ((tree (org-element-parse-buffer))
  1380. (info `(:parse-tree ,tree)))
  1381. (org-export-resolve-radio-link
  1382. (org-element-map tree 'link 'identity info t)
  1383. info))))
  1384. ;; Radio targets are case-insensitive.
  1385. (should
  1386. (org-test-with-temp-text "<<<RADIO>>> radio"
  1387. (org-update-radio-target-regexp)
  1388. (let* ((tree (org-element-parse-buffer))
  1389. (info `(:parse-tree ,tree)))
  1390. (org-export-resolve-radio-link
  1391. (org-element-map tree 'link 'identity info t)
  1392. info))))
  1393. ;; Radio target with objects.
  1394. (should
  1395. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1396. (org-update-radio-target-regexp)
  1397. (let* ((tree (org-element-parse-buffer))
  1398. (info `(:parse-tree ,tree)))
  1399. (org-export-resolve-radio-link
  1400. (org-element-map tree 'link 'identity info t)
  1401. info)))))
  1402. ;;; Src-block and example-block
  1403. (ert-deftest test-org-export/unravel-code ()
  1404. "Test `org-export-unravel-code' function."
  1405. (let ((org-coderef-label-format "(ref:%s)"))
  1406. ;; 1. Code without reference.
  1407. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1408. (should (equal (org-export-unravel-code (org-element-at-point))
  1409. '("(+ 1 1)\n"))))
  1410. ;; 2. Code with reference.
  1411. (org-test-with-temp-text
  1412. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1413. (should (equal (org-export-unravel-code (org-element-at-point))
  1414. '("(+ 1 1)\n" (1 . "test")))))
  1415. ;; 3. Code with user-defined reference.
  1416. (org-test-with-temp-text
  1417. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1418. (should (equal (org-export-unravel-code (org-element-at-point))
  1419. '("(+ 1 1)\n" (1 . "test")))))
  1420. ;; 4. Code references keys are relative to the current block.
  1421. (org-test-with-temp-text "
  1422. #+BEGIN_EXAMPLE -n
  1423. \(+ 1 1)
  1424. #+END_EXAMPLE
  1425. #+BEGIN_EXAMPLE +n
  1426. \(+ 2 2)
  1427. \(+ 3 3) (ref:one)
  1428. #+END_EXAMPLE"
  1429. (goto-line 5)
  1430. (should (equal (org-export-unravel-code (org-element-at-point))
  1431. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1432. ;;; Smart Quotes
  1433. (ert-deftest test-org-export/activate-smart-quotes ()
  1434. "Test `org-export-activate-smart-quotes' specifications."
  1435. ;; Opening double quotes: standard test.
  1436. (should
  1437. (equal
  1438. '("some &ldquo;paragraph")
  1439. (let ((org-export-default-language "en"))
  1440. (org-test-with-parsed-data "some \"paragraph"
  1441. (org-element-map
  1442. tree 'plain-text
  1443. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1444. info)))))
  1445. ;; Opening quotes: at the beginning of a paragraph.
  1446. (should
  1447. (equal
  1448. '("&ldquo;begin")
  1449. (let ((org-export-default-language "en"))
  1450. (org-test-with-parsed-data "\"begin"
  1451. (org-element-map
  1452. tree 'plain-text
  1453. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1454. info)))))
  1455. ;; Opening quotes: after an object.
  1456. (should
  1457. (equal
  1458. '("&ldquo;begin")
  1459. (let ((org-export-default-language "en"))
  1460. (org-test-with-parsed-data "=verb= \"begin"
  1461. (org-element-map
  1462. tree 'plain-text
  1463. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1464. info)))))
  1465. ;; Closing quotes: standard test.
  1466. (should
  1467. (equal
  1468. '("some&rdquo; paragraph")
  1469. (let ((org-export-default-language "en"))
  1470. (org-test-with-parsed-data "some\" paragraph"
  1471. (org-element-map
  1472. tree 'plain-text
  1473. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1474. info)))))
  1475. ;; Closing quotes: at the end of a paragraph.
  1476. (should
  1477. (equal
  1478. '("end&rdquo;")
  1479. (let ((org-export-default-language "en"))
  1480. (org-test-with-parsed-data "end\""
  1481. (org-element-map
  1482. tree 'plain-text
  1483. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1484. info)))))
  1485. ;; Apostrophe: standard test.
  1486. (should
  1487. (equal
  1488. '("It shouldn&rsquo;t fail")
  1489. (let ((org-export-default-language "en"))
  1490. (org-test-with-parsed-data "It shouldn't fail"
  1491. (org-element-map
  1492. tree 'plain-text
  1493. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1494. info)))))
  1495. ;; Apostrophe: before an object.
  1496. (should
  1497. (equal
  1498. '("a&rsquo;")
  1499. (let ((org-export-default-language "en"))
  1500. (org-test-with-parsed-data "a'=b="
  1501. (org-element-map
  1502. tree 'plain-text
  1503. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1504. info)))))
  1505. ;; Apostrophe: after an object.
  1506. (should
  1507. (equal
  1508. '("&rsquo;s")
  1509. (let ((org-export-default-language "en"))
  1510. (org-test-with-parsed-data "=code='s"
  1511. (org-element-map
  1512. tree 'plain-text
  1513. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1514. info)))))
  1515. ;; Special case: isolated quotes.
  1516. (should
  1517. (equal '("&ldquo;" "&rdquo;")
  1518. (let ((org-export-default-language "en"))
  1519. (org-test-with-parsed-data "\"$x$\""
  1520. (org-element-map
  1521. tree 'plain-text
  1522. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1523. info)))))
  1524. ;; Smart quotes in secondary strings.
  1525. (should
  1526. (equal '("&ldquo;" "&rdquo;")
  1527. (let ((org-export-default-language "en"))
  1528. (org-test-with-parsed-data "* \"$x$\""
  1529. (org-element-map
  1530. tree 'plain-text
  1531. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1532. info)))))
  1533. ;; Smart quotes in document keywords.
  1534. (should
  1535. (equal '("&ldquo;" "&rdquo;")
  1536. (let ((org-export-default-language "en"))
  1537. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1538. (org-element-map
  1539. (plist-get info :title) 'plain-text
  1540. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1541. info)))))
  1542. ;; Smart quotes in parsed affiliated keywords.
  1543. (should
  1544. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1545. (let ((org-export-default-language "en"))
  1546. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1547. (org-element-map
  1548. tree 'plain-text
  1549. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1550. info nil nil t))))))
  1551. ;;; Tables
  1552. (ert-deftest test-org-export/special-column ()
  1553. "Test if the table's special column is properly recognized."
  1554. ;; 1. First column is special if it contains only a special marking
  1555. ;; characters or empty cells.
  1556. (org-test-with-temp-text "
  1557. | ! | 1 |
  1558. | | 2 |"
  1559. (should
  1560. (org-export-table-has-special-column-p
  1561. (org-element-map
  1562. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1563. ;; 2. If the column contains anything else, it isn't special.
  1564. (org-test-with-temp-text "
  1565. | ! | 1 |
  1566. | b | 2 |"
  1567. (should-not
  1568. (org-export-table-has-special-column-p
  1569. (org-element-map
  1570. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1571. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1572. ;; and "!".
  1573. (org-test-with-temp-text "
  1574. | # | 1 |
  1575. | ^ | 2 |
  1576. | * | 3 |
  1577. | _ | 4 |
  1578. | / | 5 |
  1579. | $ | 6 |
  1580. | ! | 7 |"
  1581. (should
  1582. (org-export-table-has-special-column-p
  1583. (org-element-map
  1584. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1585. ;; 4. A first column with only empty cells isn't considered as
  1586. ;; special.
  1587. (org-test-with-temp-text "
  1588. | | 1 |
  1589. | | 2 |"
  1590. (should-not
  1591. (org-export-table-has-special-column-p
  1592. (org-element-map
  1593. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1594. (ert-deftest test-org-export/table-row-is-special-p ()
  1595. "Test `org-export-table-row-is-special-p' specifications."
  1596. ;; 1. A row is special if it has a special marking character in the
  1597. ;; special column.
  1598. (org-test-with-parsed-data "| ! | 1 |"
  1599. (should
  1600. (org-export-table-row-is-special-p
  1601. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1602. ;; 2. A row is special when its first field is "/"
  1603. (org-test-with-parsed-data "
  1604. | / | 1 |
  1605. | a | b |"
  1606. (should
  1607. (org-export-table-row-is-special-p
  1608. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1609. ;; 3. A row only containing alignment cookies is also considered as
  1610. ;; special.
  1611. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1612. (should
  1613. (org-export-table-row-is-special-p
  1614. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1615. ;; 4. Everything else isn't considered as special.
  1616. (org-test-with-parsed-data "| \alpha | | c |"
  1617. (should-not
  1618. (org-export-table-row-is-special-p
  1619. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1620. ;; 5. Table's rules are never considered as special rows.
  1621. (org-test-with-parsed-data "|---+---|"
  1622. (should-not
  1623. (org-export-table-row-is-special-p
  1624. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1625. (ert-deftest test-org-export/has-header-p ()
  1626. "Test `org-export-table-has-header-p' specifications."
  1627. ;; 1. With an header.
  1628. (org-test-with-parsed-data "
  1629. | a | b |
  1630. |---+---|
  1631. | c | d |"
  1632. (should
  1633. (org-export-table-has-header-p
  1634. (org-element-map tree 'table 'identity info 'first-match)
  1635. info)))
  1636. ;; 2. Without an header.
  1637. (org-test-with-parsed-data "
  1638. | a | b |
  1639. | c | d |"
  1640. (should-not
  1641. (org-export-table-has-header-p
  1642. (org-element-map tree 'table 'identity info 'first-match)
  1643. info)))
  1644. ;; 3. Don't get fooled with starting and ending rules.
  1645. (org-test-with-parsed-data "
  1646. |---+---|
  1647. | a | b |
  1648. | c | d |
  1649. |---+---|"
  1650. (should-not
  1651. (org-export-table-has-header-p
  1652. (org-element-map tree 'table 'identity info 'first-match)
  1653. info))))
  1654. (ert-deftest test-org-export/table-row-group ()
  1655. "Test `org-export-table-row-group' specifications."
  1656. ;; 1. A rule creates a new group.
  1657. (org-test-with-parsed-data "
  1658. | a | b |
  1659. |---+---|
  1660. | 1 | 2 |"
  1661. (should
  1662. (equal
  1663. '(1 nil 2)
  1664. (mapcar (lambda (row) (org-export-table-row-group row info))
  1665. (org-element-map tree 'table-row 'identity)))))
  1666. ;; 2. Special rows are ignored in count.
  1667. (org-test-with-parsed-data "
  1668. | / | < | > |
  1669. |---|---+---|
  1670. | | 1 | 2 |"
  1671. (should
  1672. (equal
  1673. '(nil nil 1)
  1674. (mapcar (lambda (row) (org-export-table-row-group row info))
  1675. (org-element-map tree 'table-row 'identity)))))
  1676. ;; 3. Double rules also are ignored in count.
  1677. (org-test-with-parsed-data "
  1678. | a | b |
  1679. |---+---|
  1680. |---+---|
  1681. | 1 | 2 |"
  1682. (should
  1683. (equal
  1684. '(1 nil nil 2)
  1685. (mapcar (lambda (row) (org-export-table-row-group row info))
  1686. (org-element-map tree 'table-row 'identity))))))
  1687. (ert-deftest test-org-export/table-cell-width ()
  1688. "Test `org-export-table-cell-width' specifications."
  1689. ;; 1. Width is primarily determined by width cookies. If no cookie
  1690. ;; is found, cell's width is nil.
  1691. (org-test-with-parsed-data "
  1692. | / | <l> | <6> | <l7> |
  1693. | | a | b | c |"
  1694. (should
  1695. (equal
  1696. '(nil 6 7)
  1697. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1698. (org-element-map tree 'table-cell 'identity info)))))
  1699. ;; 2. The last width cookie has precedence.
  1700. (org-test-with-parsed-data "
  1701. | <6> |
  1702. | <7> |
  1703. | a |"
  1704. (should
  1705. (equal
  1706. '(7)
  1707. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1708. (org-element-map tree 'table-cell 'identity info)))))
  1709. ;; 3. Valid width cookies must have a specific row.
  1710. (org-test-with-parsed-data "| <6> | cell |"
  1711. (should
  1712. (equal
  1713. '(nil nil)
  1714. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1715. (org-element-map tree 'table-cell 'identity))))))
  1716. (ert-deftest test-org-export/table-cell-alignment ()
  1717. "Test `org-export-table-cell-alignment' specifications."
  1718. (let ((org-table-number-fraction 0.5)
  1719. (org-table-number-regexp "^[0-9]+$"))
  1720. ;; 1. Alignment is primarily determined by alignment cookies.
  1721. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1722. (let* ((tree (org-element-parse-buffer))
  1723. (info `(:parse-tree ,tree)))
  1724. (should
  1725. (equal
  1726. '(left center right)
  1727. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1728. (org-element-map tree 'table-cell 'identity))))))
  1729. ;; 2. The last alignment cookie has precedence.
  1730. (org-test-with-parsed-data "
  1731. | <l8> |
  1732. | cell |
  1733. | <r9> |"
  1734. (should
  1735. (equal
  1736. '(right right right)
  1737. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1738. (org-element-map tree 'table-cell 'identity)))))
  1739. ;; 3. If there's no cookie, cell's contents determine alignment.
  1740. ;; A column mostly made of cells containing numbers will align
  1741. ;; its cells to the right.
  1742. (org-test-with-parsed-data "
  1743. | 123 |
  1744. | some text |
  1745. | 12345 |"
  1746. (should
  1747. (equal
  1748. '(right right right)
  1749. (mapcar (lambda (cell)
  1750. (org-export-table-cell-alignment cell info))
  1751. (org-element-map tree 'table-cell 'identity)))))
  1752. ;; 4. Otherwise, they will be aligned to the left.
  1753. (org-test-with-parsed-data "
  1754. | text |
  1755. | some text |
  1756. | \alpha |"
  1757. (should
  1758. (equal
  1759. '(left left left)
  1760. (mapcar (lambda (cell)
  1761. (org-export-table-cell-alignment cell info))
  1762. (org-element-map tree 'table-cell 'identity)))))))
  1763. (ert-deftest test-org-export/table-cell-borders ()
  1764. "Test `org-export-table-cell-borders' specifications."
  1765. ;; 1. Recognize various column groups indicators.
  1766. (org-test-with-parsed-data "| / | < | > | <> |"
  1767. (should
  1768. (equal
  1769. '((right bottom top) (left bottom top) (right bottom top)
  1770. (right left bottom top))
  1771. (mapcar (lambda (cell)
  1772. (org-export-table-cell-borders cell info))
  1773. (org-element-map tree 'table-cell 'identity)))))
  1774. ;; 2. Accept shortcuts to define column groups.
  1775. (org-test-with-parsed-data "| / | < | < |"
  1776. (should
  1777. (equal
  1778. '((right bottom top) (right left bottom top) (left bottom top))
  1779. (mapcar (lambda (cell)
  1780. (org-export-table-cell-borders cell info))
  1781. (org-element-map tree 'table-cell 'identity)))))
  1782. ;; 3. A valid column groups row must start with a "/".
  1783. (org-test-with-parsed-data "
  1784. | | < |
  1785. | a | b |"
  1786. (should
  1787. (equal '((top) (top) (bottom) (bottom))
  1788. (mapcar (lambda (cell)
  1789. (org-export-table-cell-borders cell info))
  1790. (org-element-map tree 'table-cell 'identity)))))
  1791. ;; 4. Take table rules into consideration.
  1792. (org-test-with-parsed-data "
  1793. | 1 |
  1794. |---|
  1795. | 2 |"
  1796. (should
  1797. (equal '((below top) (bottom above))
  1798. (mapcar (lambda (cell)
  1799. (org-export-table-cell-borders cell info))
  1800. (org-element-map tree 'table-cell 'identity)))))
  1801. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1802. ;; (resp. `bottom' and `below') borders. Any special row is
  1803. ;; ignored.
  1804. (org-test-with-parsed-data "
  1805. |---+----|
  1806. | / | |
  1807. | | 1 |
  1808. |---+----|"
  1809. (should
  1810. (equal '((bottom below top above))
  1811. (last
  1812. (mapcar (lambda (cell)
  1813. (org-export-table-cell-borders cell info))
  1814. (org-element-map tree 'table-cell 'identity)))))))
  1815. (ert-deftest test-org-export/table-dimensions ()
  1816. "Test `org-export-table-dimensions' specifications."
  1817. ;; 1. Standard test.
  1818. (org-test-with-parsed-data "
  1819. | 1 | 2 | 3 |
  1820. | 4 | 5 | 6 |"
  1821. (should
  1822. (equal '(2 . 3)
  1823. (org-export-table-dimensions
  1824. (org-element-map tree 'table 'identity info 'first-match) info))))
  1825. ;; 2. Ignore horizontal rules and special columns.
  1826. (org-test-with-parsed-data "
  1827. | / | < | > |
  1828. | 1 | 2 | 3 |
  1829. |---+---+---|
  1830. | 4 | 5 | 6 |"
  1831. (should
  1832. (equal '(2 . 3)
  1833. (org-export-table-dimensions
  1834. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1835. (ert-deftest test-org-export/table-cell-address ()
  1836. "Test `org-export-table-cell-address' specifications."
  1837. ;; 1. Standard test: index is 0-based.
  1838. (org-test-with-parsed-data "| a | b |"
  1839. (should
  1840. (equal '((0 . 0) (0 . 1))
  1841. (org-element-map
  1842. tree 'table-cell
  1843. (lambda (cell) (org-export-table-cell-address cell info))
  1844. info))))
  1845. ;; 2. Special column isn't counted, nor are special rows.
  1846. (org-test-with-parsed-data "
  1847. | / | <> |
  1848. | | c |"
  1849. (should
  1850. (equal '(0 . 0)
  1851. (org-export-table-cell-address
  1852. (car (last (org-element-map tree 'table-cell 'identity info)))
  1853. info))))
  1854. ;; 3. Tables rules do not count either.
  1855. (org-test-with-parsed-data "
  1856. | a |
  1857. |---|
  1858. | b |
  1859. |---|
  1860. | c |"
  1861. (should
  1862. (equal '(2 . 0)
  1863. (org-export-table-cell-address
  1864. (car (last (org-element-map tree 'table-cell 'identity info)))
  1865. info))))
  1866. ;; 4. Return nil for special cells.
  1867. (org-test-with-parsed-data "| / | a |"
  1868. (should-not
  1869. (org-export-table-cell-address
  1870. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1871. info))))
  1872. (ert-deftest test-org-export/get-table-cell-at ()
  1873. "Test `org-export-get-table-cell-at' specifications."
  1874. ;; 1. Address ignores special columns, special rows and rules.
  1875. (org-test-with-parsed-data "
  1876. | / | <> |
  1877. | | a |
  1878. |---+----|
  1879. | | b |"
  1880. (should
  1881. (equal '("b")
  1882. (org-element-contents
  1883. (org-export-get-table-cell-at
  1884. '(1 . 0)
  1885. (org-element-map tree 'table 'identity info 'first-match)
  1886. info)))))
  1887. ;; 2. Return value for a non-existent address is nil.
  1888. (org-test-with-parsed-data "| a |"
  1889. (should-not
  1890. (org-export-get-table-cell-at
  1891. '(2 . 2)
  1892. (org-element-map tree 'table 'identity info 'first-match)
  1893. info)))
  1894. (org-test-with-parsed-data "| / |"
  1895. (should-not
  1896. (org-export-get-table-cell-at
  1897. '(0 . 0)
  1898. (org-element-map tree 'table 'identity info 'first-match)
  1899. info))))
  1900. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1901. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1902. ;; 1. A cell at a beginning of a row always starts a column group.
  1903. (org-test-with-parsed-data "| a |"
  1904. (should
  1905. (org-export-table-cell-starts-colgroup-p
  1906. (org-element-map tree 'table-cell 'identity info 'first-match)
  1907. info)))
  1908. ;; 2. Special column should be ignored when determining the
  1909. ;; beginning of the row.
  1910. (org-test-with-parsed-data "
  1911. | / | |
  1912. | | a |"
  1913. (should
  1914. (org-export-table-cell-starts-colgroup-p
  1915. (org-element-map tree 'table-cell 'identity info 'first-match)
  1916. info)))
  1917. ;; 2. Explicit column groups.
  1918. (org-test-with-parsed-data "
  1919. | / | | < |
  1920. | a | b | c |"
  1921. (should
  1922. (equal
  1923. '(yes no yes)
  1924. (org-element-map
  1925. tree 'table-cell
  1926. (lambda (cell)
  1927. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1928. info)))))
  1929. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1930. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1931. ;; 1. A cell at the end of a row always ends a column group.
  1932. (org-test-with-parsed-data "| a |"
  1933. (should
  1934. (org-export-table-cell-ends-colgroup-p
  1935. (org-element-map tree 'table-cell 'identity info 'first-match)
  1936. info)))
  1937. ;; 2. Special column should be ignored when determining the
  1938. ;; beginning of the row.
  1939. (org-test-with-parsed-data "
  1940. | / | |
  1941. | | a |"
  1942. (should
  1943. (org-export-table-cell-ends-colgroup-p
  1944. (org-element-map tree 'table-cell 'identity info 'first-match)
  1945. info)))
  1946. ;; 3. Explicit column groups.
  1947. (org-test-with-parsed-data "
  1948. | / | < | |
  1949. | a | b | c |"
  1950. (should
  1951. (equal
  1952. '(yes no yes)
  1953. (org-element-map
  1954. tree 'table-cell
  1955. (lambda (cell)
  1956. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1957. info)))))
  1958. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1959. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1960. ;; 1. A row at the beginning of a table always starts a row group.
  1961. ;; So does a row following a table rule.
  1962. (org-test-with-parsed-data "
  1963. | a |
  1964. |---|
  1965. | b |"
  1966. (should
  1967. (equal
  1968. '(yes no yes)
  1969. (org-element-map
  1970. tree 'table-row
  1971. (lambda (row)
  1972. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1973. info))))
  1974. ;; 2. Special rows should be ignored when determining the beginning
  1975. ;; of the row.
  1976. (org-test-with-parsed-data "
  1977. | / | < |
  1978. | | a |
  1979. |---+---|
  1980. | / | < |
  1981. | | b |"
  1982. (should
  1983. (equal
  1984. '(yes no yes)
  1985. (org-element-map
  1986. tree 'table-row
  1987. (lambda (row)
  1988. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1989. info)))))
  1990. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1991. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1992. ;; 1. A row at the end of a table always ends a row group. So does
  1993. ;; a row preceding a table rule.
  1994. (org-test-with-parsed-data "
  1995. | a |
  1996. |---|
  1997. | b |"
  1998. (should
  1999. (equal
  2000. '(yes no yes)
  2001. (org-element-map
  2002. tree 'table-row
  2003. (lambda (row)
  2004. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2005. info))))
  2006. ;; 2. Special rows should be ignored when determining the beginning
  2007. ;; of the row.
  2008. (org-test-with-parsed-data "
  2009. | | a |
  2010. | / | < |
  2011. |---+---|
  2012. | | b |
  2013. | / | < |"
  2014. (should
  2015. (equal
  2016. '(yes no yes)
  2017. (org-element-map
  2018. tree 'table-row
  2019. (lambda (row)
  2020. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2021. info)))))
  2022. (ert-deftest test-org-export/table-row-starts-header-p ()
  2023. "Test `org-export-table-row-starts-header-p' specifications."
  2024. ;; 1. Only the row starting the first row group starts the table
  2025. ;; header.
  2026. (org-test-with-parsed-data "
  2027. | a |
  2028. | b |
  2029. |---|
  2030. | c |"
  2031. (should
  2032. (equal
  2033. '(yes no no no)
  2034. (org-element-map
  2035. tree 'table-row
  2036. (lambda (row)
  2037. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  2038. info))))
  2039. ;; 2. A row cannot start an header if there's no header in the
  2040. ;; table.
  2041. (org-test-with-parsed-data "
  2042. | a |
  2043. |---|"
  2044. (should-not
  2045. (org-export-table-row-starts-header-p
  2046. (org-element-map tree 'table-row 'identity info 'first-match)
  2047. info))))
  2048. (ert-deftest test-org-export/table-row-ends-header-p ()
  2049. "Test `org-export-table-row-ends-header-p' specifications."
  2050. ;; 1. Only the row starting the first row group starts the table
  2051. ;; header.
  2052. (org-test-with-parsed-data "
  2053. | a |
  2054. | b |
  2055. |---|
  2056. | c |"
  2057. (should
  2058. (equal
  2059. '(no yes no no)
  2060. (org-element-map
  2061. tree 'table-row
  2062. (lambda (row)
  2063. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  2064. info))))
  2065. ;; 2. A row cannot start an header if there's no header in the
  2066. ;; table.
  2067. (org-test-with-parsed-data "
  2068. | a |
  2069. |---|"
  2070. (should-not
  2071. (org-export-table-row-ends-header-p
  2072. (org-element-map tree 'table-row 'identity info 'first-match)
  2073. info))))
  2074. ;;; Templates
  2075. (ert-deftest test-org-export/inner-template ()
  2076. "Test `inner-template' translator specifications."
  2077. (should
  2078. (equal "Success!"
  2079. (let (org-export-registered-backends)
  2080. (org-export-define-backend test
  2081. ((inner-template . (lambda (contents info) "Success!"))
  2082. (headline . (lambda (h c i) "Headline"))))
  2083. (org-test-with-temp-text "* Headline"
  2084. (org-export-as 'test)))))
  2085. ;; Inner template is applied even in a "body-only" export.
  2086. (should
  2087. (equal "Success!"
  2088. (let (org-export-registered-backends)
  2089. (org-export-define-backend test
  2090. ((inner-template . (lambda (contents info) "Success!"))
  2091. (headline . (lambda (h c i) "Headline"))))
  2092. (org-test-with-temp-text "* Headline"
  2093. (org-export-as 'test nil nil 'body-only))))))
  2094. (ert-deftest test-org-export/template ()
  2095. "Test `template' translator specifications."
  2096. (should
  2097. (equal "Success!"
  2098. (let (org-export-registered-backends)
  2099. (org-export-define-backend test
  2100. ((template . (lambda (contents info) "Success!"))
  2101. (headline . (lambda (h c i) "Headline"))))
  2102. (org-test-with-temp-text "* Headline"
  2103. (org-export-as 'test)))))
  2104. ;; Template is not applied in a "body-only" export.
  2105. (should-not
  2106. (equal "Success!"
  2107. (let (org-export-registered-backends)
  2108. (org-export-define-backend test
  2109. ((template . (lambda (contents info) "Success!"))
  2110. (headline . (lambda (h c i) "Headline"))))
  2111. (org-test-with-temp-text "* Headline"
  2112. (org-export-as 'test nil nil 'body-only))))))
  2113. ;;; Topology
  2114. (ert-deftest test-org-export/get-next-element ()
  2115. "Test `org-export-get-next-element' specifications."
  2116. ;; Standard test.
  2117. (should
  2118. (equal "b"
  2119. (org-test-with-parsed-data "* Headline\n*a* b"
  2120. (org-export-get-next-element
  2121. (org-element-map tree 'bold 'identity info t) info))))
  2122. ;; Return nil when no previous element.
  2123. (should-not
  2124. (org-test-with-parsed-data "* Headline\na *b*"
  2125. (org-export-get-next-element
  2126. (org-element-map tree 'bold 'identity info t) info)))
  2127. ;; Non-exportable elements are ignored.
  2128. (should-not
  2129. (let ((org-export-with-timestamps nil))
  2130. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2131. (org-export-get-next-element
  2132. (org-element-map tree 'entity 'identity info t) info))))
  2133. ;; Find next element in secondary strings.
  2134. (should
  2135. (eq 'verbatim
  2136. (org-test-with-parsed-data "* a =verb="
  2137. (org-element-type
  2138. (org-export-get-next-element
  2139. (org-element-map tree 'plain-text 'identity info t) info)))))
  2140. ;; Find next element in document keywords.
  2141. (should
  2142. (eq 'verbatim
  2143. (org-test-with-parsed-data "#+TITLE: a =verb="
  2144. (org-element-type
  2145. (org-export-get-next-element
  2146. (org-element-map
  2147. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2148. ;; Find next element in parsed affiliated keywords.
  2149. (should
  2150. (eq 'verbatim
  2151. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2152. (org-element-type
  2153. (org-export-get-next-element
  2154. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2155. ;; With optional argument N, return a list containing all the
  2156. ;; following elements.
  2157. (should
  2158. (equal
  2159. '(bold code underline)
  2160. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2161. (mapcar 'car
  2162. (org-export-get-next-element
  2163. (org-element-map tree 'italic 'identity info t) info t)))))
  2164. ;; When N is a positive integer, return a list containing up to
  2165. ;; N following elements.
  2166. (should
  2167. (equal
  2168. '(bold code)
  2169. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2170. (mapcar 'car
  2171. (org-export-get-next-element
  2172. (org-element-map tree 'italic 'identity info t) info 2))))))
  2173. (ert-deftest test-org-export/get-previous-element ()
  2174. "Test `org-export-get-previous-element' specifications."
  2175. ;; Standard test.
  2176. (should
  2177. (equal "a "
  2178. (org-test-with-parsed-data "* Headline\na *b*"
  2179. (org-export-get-previous-element
  2180. (org-element-map tree 'bold 'identity info t) info))))
  2181. ;; Return nil when no previous element.
  2182. (should-not
  2183. (org-test-with-parsed-data "* Headline\n*a* b"
  2184. (org-export-get-previous-element
  2185. (org-element-map tree 'bold 'identity info t) info)))
  2186. ;; Non-exportable elements are ignored.
  2187. (should-not
  2188. (let ((org-export-with-timestamps nil))
  2189. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  2190. (org-export-get-previous-element
  2191. (org-element-map tree 'entity 'identity info t) info))))
  2192. ;; Find previous element in secondary strings.
  2193. (should
  2194. (eq 'verbatim
  2195. (org-test-with-parsed-data "* =verb= a"
  2196. (org-element-type
  2197. (org-export-get-previous-element
  2198. (org-element-map tree 'plain-text 'identity info t) info)))))
  2199. ;; Find previous element in document keywords.
  2200. (should
  2201. (eq 'verbatim
  2202. (org-test-with-parsed-data "#+TITLE: =verb= a"
  2203. (org-element-type
  2204. (org-export-get-previous-element
  2205. (org-element-map
  2206. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2207. ;; Find previous element in parsed affiliated keywords.
  2208. (should
  2209. (eq 'verbatim
  2210. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  2211. (org-element-type
  2212. (org-export-get-previous-element
  2213. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2214. ;; With optional argument N, return a list containing up to
  2215. ;; N previous elements.
  2216. (should
  2217. (equal '(bold italic underline)
  2218. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2219. (mapcar 'car
  2220. (org-export-get-previous-element
  2221. (org-element-map tree 'code 'identity info t) info t)))))
  2222. ;; When N is a positive integer, return a list containing up to
  2223. ;; N previous elements.
  2224. (should
  2225. (equal '(bold italic)
  2226. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2227. (mapcar 'car
  2228. (org-export-get-previous-element
  2229. (org-element-map tree 'code 'identity info t) info 2))))))
  2230. (provide 'test-ox)
  2231. ;;; test-org-export.el end here