test-org-export.el 68 KB

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