test-org-element.el 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. ;;; test-org-element.el --- Tests for org-element.el
  2. ;; Copyright (C) 2012 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;; Code:
  16. (unless (featurep 'org-element)
  17. (signal 'missing-test-dependency "org-element"))
  18. (defun org-test-parse-and-interpret (text)
  19. "Parse TEXT as Org syntax and interpret it.
  20. Return interpreted string."
  21. (with-temp-buffer
  22. (org-mode)
  23. (insert text)
  24. (org-element-interpret-data (org-element-parse-buffer))))
  25. ;;; Tests:
  26. ;;;; Headlines
  27. (ert-deftest test-org-element/headline-quote-keyword ()
  28. "Test QUOTE keyword recognition."
  29. ;; Reference test.
  30. (org-test-with-temp-text "* Headline"
  31. (let ((org-quote-string "QUOTE"))
  32. (should-not (org-element-property :quotedp (org-element-at-point)))))
  33. ;; Standard position.
  34. (org-test-with-temp-text "* QUOTE Headline"
  35. (let ((org-quote-string "QUOTE"))
  36. (let ((headline (org-element-at-point)))
  37. (should (org-element-property :quotedp headline))
  38. ;; Test removal from raw value.
  39. (should (equal (org-element-property :raw-value headline) "Headline"))))
  40. ;; Case sensitivity.
  41. (let ((org-quote-string "Quote"))
  42. (should-not (org-element-property :quotedp (org-element-at-point)))))
  43. ;; With another keyword.
  44. (org-test-with-temp-text "* TODO QUOTE Headline"
  45. (let ((org-quote-string "QUOTE")
  46. (org-todo-keywords '((sequence "TODO" "DONE"))))
  47. (should (org-element-property :quotedp (org-element-at-point))))))
  48. (ert-deftest test-org-element/headline-comment-keyword ()
  49. "Test COMMENT keyword recognition."
  50. ;; Reference test.
  51. (org-test-with-temp-text "* Headline"
  52. (let ((org-comment-string "COMMENT"))
  53. (should-not (org-element-property :commentedp (org-element-at-point)))))
  54. ;; Standard position.
  55. (org-test-with-temp-text "* COMMENT Headline"
  56. (let ((org-comment-string "COMMENT"))
  57. (let ((headline (org-element-at-point)))
  58. (should (org-element-property :commentedp headline))
  59. ;; Test removal from raw value.
  60. (should (equal (org-element-property :raw-value headline) "Headline"))))
  61. ;; Case sensitivity.
  62. (let ((org-comment-string "Comment"))
  63. (should-not (org-element-property :commentedp (org-element-at-point)))))
  64. ;; With another keyword.
  65. (org-test-with-temp-text "* TODO COMMENT Headline"
  66. (let ((org-comment-string "COMMENT")
  67. (org-todo-keywords '((sequence "TODO" "DONE"))))
  68. (should (org-element-property :commentedp (org-element-at-point))))))
  69. (ert-deftest test-org-element/headline-archive-tag ()
  70. "Test ARCHIVE tag recognition."
  71. ;; Reference test.
  72. (org-test-with-temp-text "* Headline"
  73. (let ((org-archive-tag "ARCHIVE"))
  74. (should-not (org-element-property :archivedp (org-element-at-point)))))
  75. ;; Single tag.
  76. (org-test-with-temp-text "* Headline :ARCHIVE:"
  77. (let ((org-archive-tag "ARCHIVE"))
  78. (let ((headline (org-element-at-point)))
  79. (should (org-element-property :archivedp headline))
  80. ;; Test tag removal.
  81. (should-not (org-element-property :tags headline))))
  82. (let ((org-archive-tag "Archive"))
  83. (should-not (org-element-property :archivedp (org-element-at-point)))))
  84. ;; Multiple tags.
  85. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  86. (let ((org-archive-tag "ARCHIVE"))
  87. (let ((headline (org-element-at-point)))
  88. (should (org-element-property :archivedp headline))
  89. ;; Test tag removal.
  90. (should (equal (org-element-property :tags headline) ":test:"))))))
  91. ;;;; Example-blocks and Src-blocks
  92. (ert-deftest test-org-element/block-switches ()
  93. "Test `example-block' and `src-block' switches parsing."
  94. (let ((org-coderef-label-format "(ref:%s)"))
  95. ;; 1. Test "-i" switch.
  96. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  97. (let ((element (org-element-current-element)))
  98. (should-not (org-element-property :preserve-indent element))))
  99. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  100. (let ((element (org-element-current-element)))
  101. (should (org-element-property :preserve-indent element))))
  102. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  103. (let ((element (org-element-current-element)))
  104. (should-not (org-element-property :preserve-indent element))))
  105. (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
  106. (let ((element (org-element-current-element)))
  107. (should (org-element-property :preserve-indent element))))
  108. ;; 2. "-n -r -k" combination should number lines, retain labels but
  109. ;; not use them in coderefs.
  110. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\N#+END_EXAMPLE"
  111. (let ((element (org-element-current-element)))
  112. (should (and (org-element-property :number-lines element)
  113. (org-element-property :retain-labels element)
  114. (not (org-element-property :use-labels element))))))
  115. (org-test-with-temp-text
  116. "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
  117. (let ((element (org-element-current-element)))
  118. (should (and (org-element-property :number-lines element)
  119. (org-element-property :retain-labels element)
  120. (not (org-element-property :use-labels element))))))
  121. ;; 3. "-n -r" combination should number-lines remove labels and not
  122. ;; use them in coderefs.
  123. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
  124. (let ((element (org-element-current-element)))
  125. (should (and (org-element-property :number-lines element)
  126. (not (org-element-property :retain-labels element))
  127. (not (org-element-property :use-labels element))))))
  128. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
  129. (let ((element (org-element-current-element)))
  130. (should (and (org-element-property :number-lines element)
  131. (not (org-element-property :retain-labels element))
  132. (not (org-element-property :use-labels element))))))
  133. ;; 4. "-n" or "+n" should number lines, retain labels and use them
  134. ;; in coderefs.
  135. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
  136. (let ((element (org-element-current-element)))
  137. (should (and (org-element-property :number-lines element)
  138. (org-element-property :retain-labels element)
  139. (org-element-property :use-labels element)))))
  140. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
  141. (let ((element (org-element-current-element)))
  142. (should (and (org-element-property :number-lines element)
  143. (org-element-property :retain-labels element)
  144. (org-element-property :use-labels element)))))
  145. (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
  146. (let ((element (org-element-current-element)))
  147. (should (and (org-element-property :number-lines element)
  148. (org-element-property :retain-labels element)
  149. (org-element-property :use-labels element)))))
  150. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
  151. (let ((element (org-element-current-element)))
  152. (should (and (org-element-property :number-lines element)
  153. (org-element-property :retain-labels element)
  154. (org-element-property :use-labels element)))))
  155. ;; 5. No switch should not number lines, but retain labels and use
  156. ;; them in coderefs.
  157. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  158. (let ((element (org-element-current-element)))
  159. (should (and (not (org-element-property :number-lines element))
  160. (org-element-property :retain-labels element)
  161. (org-element-property :use-labels element)))))
  162. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  163. (let ((element (org-element-current-element)))
  164. (should (and (not (org-element-property :number-lines element))
  165. (org-element-property :retain-labels element)
  166. (org-element-property :use-labels element)))))
  167. ;; 6. "-r" switch only: do not number lines, remove labels, and
  168. ;; don't use labels in coderefs.
  169. (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
  170. (let ((element (org-element-current-element)))
  171. (should (and (not (org-element-property :number-lines element))
  172. (not (org-element-property :retain-labels element))
  173. (not (org-element-property :use-labels element))))))
  174. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
  175. (let ((element (org-element-current-element)))
  176. (should (and (not (org-element-property :number-lines element))
  177. (not (org-element-property :retain-labels element))
  178. (not (org-element-property :use-labels element))))))
  179. ;; 7. Recognize coderefs with user-defined syntax.
  180. (org-test-with-temp-text
  181. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
  182. (let ((element (org-element-current-element)))
  183. (should
  184. (equal (org-element-property :label-fmt element) "[ref:%s]"))))
  185. (org-test-with-temp-text
  186. "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
  187. (let ((element (org-element-current-element)))
  188. (should
  189. (equal (org-element-property :label-fmt element) "[ref:%s]"))))))
  190. ;;;; Footnotes references and definitions
  191. (ert-deftest test-org-element/footnote-reference ()
  192. "Test footnote-reference parsing."
  193. ;; 1. Parse a standard reference.
  194. (org-test-with-temp-text "[fn:label]"
  195. (should (equal (org-element-footnote-reference-parser)
  196. '(footnote-reference
  197. (:label "fn:label" :type standard :inline-definition nil
  198. :begin 1 :end 11 :post-blank 0)))))
  199. ;; 2. Parse a normalized reference.
  200. (org-test-with-temp-text "[1]"
  201. (should (equal (org-element-footnote-reference-parser)
  202. '(footnote-reference
  203. (:label "1" :type standard :inline-definition nil
  204. :begin 1 :end 4 :post-blank 0)))))
  205. ;; 3. Parse an inline reference.
  206. (org-test-with-temp-text "[fn:test:def]"
  207. (should (equal (org-element-footnote-reference-parser)
  208. '(footnote-reference
  209. (:label "fn:test" :type inline :inline-definition ("def")
  210. :begin 1 :end 14 :post-blank 0)))))
  211. ;; 4. Parse an anonymous reference.
  212. (org-test-with-temp-text "[fn::def]"
  213. (should (equal (org-element-footnote-reference-parser)
  214. '(footnote-reference
  215. (:label nil :type inline :inline-definition ("def")
  216. :begin 1 :end 10 :post-blank 0)))))
  217. ;; 5. Parse nested footnotes.
  218. (org-test-with-temp-text "[fn::def [fn:label]]"
  219. (should
  220. (equal
  221. (org-element-footnote-reference-parser)
  222. '(footnote-reference
  223. (:label nil :type inline
  224. :inline-definition
  225. ("def "
  226. (footnote-reference
  227. (:label "fn:label" :type standard :inline-definition nil
  228. :begin 5 :end 15 :post-blank 0)))
  229. :begin 1 :end 21 :post-blank 0)))))
  230. ;; 6. Parse adjacent footnotes.
  231. (org-test-with-temp-text "[fn:label1][fn:label2]"
  232. (should
  233. (equal
  234. (org-element-footnote-reference-parser)
  235. '(footnote-reference
  236. (:label "fn:label1" :type standard :inline-definition nil :begin 1
  237. :end 12 :post-blank 0)))))
  238. ;; 7. Only properly closed footnotes are recognized as such.
  239. (org-test-with-temp-text "Text [fn:label"
  240. (should-not
  241. (org-element-map
  242. (org-element-parse-buffer) 'footnote-reference 'identity))))
  243. ;;;; Verse blocks
  244. (ert-deftest test-org-element/verse-block ()
  245. "Test verse block parsing."
  246. ;; Standard test.
  247. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
  248. (should
  249. (equal
  250. (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)
  251. '(verse-block
  252. (:begin 1 :end 38 :contents-begin 15 :contents-end 27 :hiddenp nil
  253. :post-blank 0)
  254. "Verse block\n"))))
  255. ;; Ignore case.
  256. (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
  257. (should
  258. (equal
  259. (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)
  260. '(verse-block
  261. (:begin 1 :end 38 :contents-begin 15 :contents-end 27 :hiddenp nil
  262. :post-blank 0)
  263. "Verse block\n"))))
  264. ;; Parse folding.
  265. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
  266. (org-hide-block-all)
  267. (should
  268. (equal
  269. (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)
  270. '(verse-block
  271. (:begin 1 :end 38 :contents-begin 15 :contents-end 27
  272. :hiddenp org-hide-block :post-blank 0)
  273. "Verse block\n"))))
  274. ;; Parse objects in verse blocks.
  275. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
  276. (should (org-element-map (org-element-parse-buffer) 'entity 'identity))))
  277. ;;;; Granularity
  278. (ert-deftest test-org-element/granularity ()
  279. "Test granularity impact on buffer parsing."
  280. (org-test-with-temp-text "
  281. * Head 1
  282. ** Head 2
  283. #+BEGIN_CENTER
  284. Centered paragraph.
  285. #+END_CENTER
  286. Paragraph \\alpha."
  287. ;; 1.1. Granularity set to `headline' should parse every headline
  288. ;; in buffer, and only them.
  289. (let ((tree (org-element-parse-buffer 'headline)))
  290. (should (= 2 (length (org-element-map tree 'headline 'identity))))
  291. (should-not (org-element-map tree 'paragraph 'identity)))
  292. ;; 1.2. Granularity set to `greater-element' should not enter
  293. ;; greater elements excepted headlines and sections.
  294. (let ((tree (org-element-parse-buffer 'greater-element)))
  295. (should (= 1 (length (org-element-map tree 'center-block 'identity))))
  296. (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
  297. (should-not (org-element-map tree 'entity 'identity)))
  298. ;; 1.3. Granularity set to `element' should enter every
  299. ;; greater-element.
  300. (let ((tree (org-element-parse-buffer 'element)))
  301. (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
  302. (should-not (org-element-map tree 'entity 'identity)))
  303. ;; 1.4. Granularity set to `object' can see everything.
  304. (let ((tree (org-element-parse-buffer 'object)))
  305. (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
  306. (ert-deftest test-org-element/secondary-string-parsing ()
  307. "Test if granularity correctly toggles secondary strings parsing."
  308. ;; 1. With a granularity bigger than `object', no secondary string
  309. ;; should be parsed.
  310. ;;
  311. ;; 1.1. Test with `headline' type.
  312. (org-test-with-temp-text "* Headline"
  313. (let ((headline
  314. (org-element-map (org-element-parse-buffer 'headline) 'headline
  315. 'identity
  316. nil
  317. 'first-match)))
  318. (should (stringp (org-element-property :title headline)))))
  319. ;; 1.2. Test with `item' type.
  320. (org-test-with-temp-text "* Headline\n- tag :: item"
  321. (let ((item (org-element-map (org-element-parse-buffer 'element)
  322. 'item
  323. 'identity
  324. nil
  325. 'first-match)))
  326. (should (stringp (org-element-property :tag item)))))
  327. ;; 1.3. Test with `inlinetask' type, if avalaible.
  328. (when (featurep 'org-inlinetask)
  329. (let ((org-inlinetask-min-level 15))
  330. (org-test-with-temp-text "*************** Inlinetask"
  331. (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
  332. 'inlinetask
  333. 'identity
  334. nil
  335. 'first-match)))
  336. (should (stringp (org-element-property :title inlinetask)))))))
  337. ;; 2. With a default granularity, secondary strings should be
  338. ;; parsed.
  339. (org-test-with-temp-text "* Headline"
  340. (let ((headline
  341. (org-element-map (org-element-parse-buffer) 'headline
  342. 'identity
  343. nil
  344. 'first-match)))
  345. (should (listp (org-element-property :title headline)))))
  346. ;; 3. `org-element-at-point' should never parse a secondary string.
  347. (org-test-with-temp-text "* Headline"
  348. (should (stringp (org-element-property :title (org-element-at-point))))))
  349. ;;;; Interpretation.
  350. (ert-deftest test-org-element/interpret-affiliated-keywords ()
  351. "Test if affiliated keywords are correctly interpreted."
  352. ;; Interpret simple keywords.
  353. (should
  354. (equal
  355. (org-element-interpret-data
  356. '(org-data nil (paragraph (:name "para") "Paragraph")))
  357. "#+NAME: para\nParagraph\n"))
  358. ;; Interpret multiple keywords.
  359. (should
  360. (equal
  361. (org-element-interpret-data
  362. '(org-data nil (paragraph (:attr_ascii ("line1" "line2")) "Paragraph")))
  363. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
  364. ;; Interpret parsed keywords.
  365. (should
  366. (equal
  367. (org-element-interpret-data
  368. '(org-data nil (paragraph (:caption ("caption")) "Paragraph")))
  369. "#+CAPTION: caption\nParagraph\n"))
  370. ;; Interpret dual keywords.
  371. (should
  372. (equal
  373. (org-element-interpret-data
  374. '(org-data nil (paragraph (:caption (("long") "short")) "Paragraph")))
  375. "#+CAPTION[short]: long\nParagraph\n")))
  376. (ert-deftest test-org-element/center-block-interpreter ()
  377. "Test center block interpreter."
  378. (should
  379. (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
  380. "#+BEGIN_CENTER\nTest\n#+END_CENTER\n")))
  381. (ert-deftest test-org-element/drawer-interpreter ()
  382. "Test drawer interpreter."
  383. (should
  384. (equal (let ((org-drawers '("TEST")))
  385. (org-test-parse-and-interpret ":TEST:\nTest\n:END:"))
  386. ":TEST:\nTest\n:END:\n")))
  387. (ert-deftest test-org-element/dynamic-block-interpreter ()
  388. "Test dynamic block interpreter."
  389. (should
  390. (equal (org-test-parse-and-interpret
  391. "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
  392. "#+BEGIN: myblock :parameter value1\nTest\n#+END:\n")))
  393. (ert-deftest test-org-element/footnote-definition-interpreter ()
  394. "Test footnote definition interpreter."
  395. (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n")))
  396. (ert-deftest test-org-element/headline-interpreter ()
  397. "Test headline and section interpreters."
  398. ;; 1. Standard test.
  399. (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
  400. ;; 2. With TODO keywords.
  401. (should
  402. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  403. (org-test-parse-and-interpret "* TODO Headline"))
  404. "* TODO Headline\n"))
  405. ;; 3. With tags...
  406. ;;
  407. ;; 3.1. ... and a positive `org-tags-column' value.
  408. (should
  409. (equal (let ((org-tags-column 20))
  410. (org-test-parse-and-interpret "* Headline :tag:"))
  411. "* Headline :tag:\n"))
  412. ;; 3.2. ... and a negative `org-tags-column' value.
  413. (should
  414. (equal (let ((org-tags-column -20))
  415. (org-test-parse-and-interpret "* Headline :tag:"))
  416. "* Headline :tag:\n"))
  417. ;; 3.3. ... and a null `org-tags-column' value.
  418. (should
  419. (equal (let ((org-tags-column 0))
  420. (org-test-parse-and-interpret "* Headline :tag:"))
  421. "* Headline :tag:\n"))
  422. ;; 4. With priority cookie.
  423. (should
  424. (equal (org-test-parse-and-interpret "* [#B] Headline")
  425. "* [#B] Headline\n"))
  426. ;; 5. With comment keyword.
  427. (should
  428. (equal (let ((org-comment-string "COMMENT"))
  429. (org-test-parse-and-interpret "* COMMENT Headline"))
  430. "* COMMENT Headline\n"))
  431. ;; 6. With quote section.
  432. (should
  433. (equal (let ((org-quote-string "QUOTE"))
  434. (org-test-parse-and-interpret "* QUOTE Headline"))
  435. "* QUOTE Headline\n"))
  436. ;; 7. Keep same number of blank lines before body.
  437. (should
  438. (equal (org-test-parse-and-interpret
  439. "* Headline\n\n\nText after two blank lines.")
  440. "* Headline\n\n\nText after two blank lines.\n")))
  441. (ert-deftest test-org-element/inlinetask-interpreter ()
  442. "Test inlinetask interpretation."
  443. (when (featurep 'org-inlinetask)
  444. (let ((org-inlinetask-min-level 15))
  445. ;; 1. Regular inlinetask.
  446. (should (equal (org-test-parse-and-interpret
  447. "*************** Task\nTest\n*************** END")
  448. "*************** Task\nTest\n*************** END\n"))
  449. ;; 2. Degenerate inlinetask.
  450. (should (equal (org-test-parse-and-interpret "*************** Task")
  451. "*************** Task\n"))
  452. ;; 3. Prefer degenerate form when there are no contents.
  453. (should (equal (org-test-parse-and-interpret
  454. "*************** Task\n*************** END")
  455. "*************** Task\n"))
  456. ;; 4. With TODO keywords.
  457. (should
  458. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  459. (org-test-parse-and-interpret "*************** TODO Task"))
  460. "*************** TODO Task\n"))
  461. ;; 5. With tags...
  462. ;;
  463. ;; 5.1. ... and a positive `org-tags-column' value.
  464. (should
  465. (equal (let ((org-tags-column 30))
  466. (org-test-parse-and-interpret "*************** Task :tag:"))
  467. "*************** Task :tag:\n"))
  468. ;; 5.2. ... and a negative `org-tags-column' value.
  469. (should
  470. (equal (let ((org-tags-column -30))
  471. (org-test-parse-and-interpret "*************** Task :tag:"))
  472. "*************** Task :tag:\n"))
  473. ;; 5.3. ... and a null `org-tags-column' value.
  474. (should
  475. (equal (let ((org-tags-column 0))
  476. (org-test-parse-and-interpret "*************** Task :tag:"))
  477. "*************** Task :tag:\n"))
  478. ;; 6. With priority cookie.
  479. (should
  480. (equal (org-test-parse-and-interpret "*************** [#B] Task")
  481. "*************** [#B] Task\n")))))
  482. (ert-deftest test-org-element/plain-list-interpreter ()
  483. "Test plain-list and item interpreters."
  484. ;; 1. Unordered list.
  485. (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
  486. ;; 2. Description list.
  487. (should
  488. (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
  489. ;; 3. Ordered list.
  490. (should
  491. (equal (let ((org-plain-list-ordered-item-terminator t))
  492. (org-test-parse-and-interpret "1. Item"))
  493. "1. Item\n"))
  494. ;; 4. Ordered list with counter.
  495. (should
  496. (equal (let ((org-plain-list-ordered-item-terminator t))
  497. (org-test-parse-and-interpret "1. [@5] Item"))
  498. "5. [@5] Item\n"))
  499. ;; 5. List with check-boxes.
  500. (should
  501. (equal (org-test-parse-and-interpret
  502. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
  503. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n")))
  504. (ert-deftest test-org-element/quote-block-interpreter ()
  505. "Test quote block interpreter."
  506. (should (equal (org-test-parse-and-interpret
  507. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
  508. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE\n")))
  509. (ert-deftest test-org-element/special-block-interpreter ()
  510. "Test special block interpreter."
  511. (should (equal (org-test-parse-and-interpret
  512. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
  513. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL\n")))
  514. (ert-deftest test-org-element/babel-call-interpreter ()
  515. "Test babel call interpreter."
  516. ;; 1. Without argument.
  517. (should (equal (org-test-parse-and-interpret "#+CALL: test()")
  518. "#+CALL: test()\n"))
  519. ;; 2. With argument.
  520. (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
  521. "#+CALL: test(x=2)\n"))
  522. ;; 3. With header arguments.
  523. (should (equal (org-test-parse-and-interpret
  524. "#+CALL: test[:results output]()[:results html]")
  525. "#+CALL: test[:results output]()[:results html]\n")))
  526. (ert-deftest test-org-element/comment-interpreter ()
  527. "Test comment interpreter."
  528. ;; Regular comment.
  529. (should (equal (org-test-parse-and-interpret "#Comment") "#Comment\n"))
  530. ;; Inline comment.
  531. (should (equal (org-test-parse-and-interpret " #+ Comment")
  532. " #+ Comment\n")))
  533. (ert-deftest test-org-element/comment-block-interpreter ()
  534. "Test comment block interpreter."
  535. (should (equal (org-test-parse-and-interpret
  536. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
  537. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n")))
  538. (ert-deftest test-org-element/example-block-interpreter ()
  539. "Test example block interpreter."
  540. (should (equal (org-test-parse-and-interpret
  541. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
  542. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n")))
  543. (ert-deftest test-org-element/export-block-interpreter ()
  544. "Test export block interpreter."
  545. (should (equal (org-test-parse-and-interpret
  546. "#+BEGIN_HTML\nTest\n#+END_HTML")
  547. "#+BEGIN_HTML\nTest\n#+END_HTML\n")))
  548. (ert-deftest test-org-element/fixed-width-interpreter ()
  549. "Test fixed width interpreter."
  550. (should (equal (org-test-parse-and-interpret ": Test") ": Test\n")))
  551. (ert-deftest test-org-element/horizontal-rule-interpreter ()
  552. "Test horizontal rule interpreter."
  553. (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
  554. (ert-deftest test-org-element/keyword-interpreter ()
  555. "Test keyword interpreter."
  556. (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
  557. "#+KEYWORD: value\n")))
  558. (ert-deftest test-org-element/latex-environment-interpreter ()
  559. "Test latex environment interpreter."
  560. (should (equal (org-test-parse-and-interpret
  561. "\begin{equation}\n1+1=2\n\end{equation}")
  562. "\begin{equation}\n1+1=2\n\end{equation}\n")))
  563. (ert-deftest test-org-element/property-drawer-interpreter ()
  564. "Test property drawer interpreter."
  565. (should (equal (let ((org-property-format "%-10s %s"))
  566. (org-test-parse-and-interpret
  567. ":PROPERTIES:\n:prop: value\n:END:"))
  568. ":PROPERTIES:\n:prop: value\n:END:\n")))
  569. (ert-deftest test-org-element/src-block-interpreter ()
  570. "Test src block interpreter."
  571. (should
  572. (equal (org-test-parse-and-interpret
  573. "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC")
  574. "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC\n")))
  575. (ert-deftest test-org-element/table-interpreter ()
  576. "Test table, table-row and table-cell interpreters."
  577. ;; 1. Simple table.
  578. (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
  579. "| a | b |\n| c | d |\n"))
  580. ;; 2. Table with horizontal rules.
  581. (should (equal (org-test-parse-and-interpret
  582. "| a | b |\n|---+---|\n| c | d |")
  583. "| a | b |\n|---+---|\n| c | d |\n"))
  584. ;; 3. Table with meta-data.
  585. (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
  586. "| / | < | > |\n| * | 1 | 2 |\n")))
  587. (ert-deftest test-org-element/verse-block-interpreter ()
  588. "Test verse block interpretation."
  589. (should
  590. (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
  591. "#+BEGIN_VERSE\nTest\n#+END_VERSE\n")))
  592. (ert-deftest test-org-element/bold-interpreter ()
  593. "Test bold interpreter."
  594. (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
  595. (ert-deftest test-org-element/code-interpreter ()
  596. "Test code interpreter."
  597. (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
  598. (ert-deftest test-org-element/entity-interpreter ()
  599. "Test entity interpreter."
  600. ;; 1. Without brackets.
  601. (should
  602. (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
  603. ;; 2. With brackets.
  604. (should
  605. (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
  606. (ert-deftest test-org-element/export-snippet-interpreter ()
  607. "Test export snippet interpreter."
  608. (should (equal (org-test-parse-and-interpret "@back-end{test}")
  609. "@back-end{test}\n")))
  610. (ert-deftest test-org-element/footnote-reference-interpreter ()
  611. "Test footnote reference interpreter."
  612. ;; 1. Regular reference.
  613. (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
  614. ;; 2. Normalized reference.
  615. (should (equal (org-test-parse-and-interpret "Text[1]") "Text[1]\n"))
  616. ;; 3. Named reference.
  617. (should (equal (org-test-parse-and-interpret "Text[fn:label]")
  618. "Text[fn:label]\n"))
  619. ;; 4. Inline reference.
  620. (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
  621. "Text[fn:label:def]\n"))
  622. ;; 5. Anonymous reference.
  623. (should (equal (org-test-parse-and-interpret "Text[fn::def]")
  624. "Text[fn::def]\n")))
  625. (ert-deftest test-org-element/inline-babel-call-interpreter ()
  626. "Test inline babel call interpreter."
  627. ;; 1. Without arguments.
  628. (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
  629. ;; 2. With arguments.
  630. (should (equal (org-test-parse-and-interpret "call_test(x=2)")
  631. "call_test(x=2)\n"))
  632. ;; 3. With header arguments.
  633. (should (equal (org-test-parse-and-interpret
  634. "call_test[:results output]()[:results html]")
  635. "call_test[:results output]()[:results html]\n")))
  636. (ert-deftest test-org-element/inline-src-block-interpreter ()
  637. "Test inline src block interpreter."
  638. ;; 1. Without header argument.
  639. (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
  640. "src_emacs-lisp{(+ 1 1)}\n"))
  641. ;; 2. With header arguments.
  642. (should (equal (org-test-parse-and-interpret
  643. "src_emacs-lisp[:results silent]{(+ 1 1)}")
  644. "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
  645. (ert-deftest test-org-element/italic-interpreter ()
  646. "Test italic interpreter."
  647. (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
  648. (ert-deftest test-org-element/latex-fragment-interpreter ()
  649. "Test latex fragment interpreter."
  650. (let ((org-latex-regexps
  651. '(("begin" "^[ ]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^�]+?\\\\end{\\2}\\)" 1 t)
  652. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \n,;.$]\\$\\)\\([- .,?;:'\")�]\\|$\\)" 2 nil)
  653. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \n,;.$][^$\n ]*?\\(\n[^$\n ]*?\\)\\{0,2\\}[^ \n,.$]\\)\\$\\)\\)\\([- .,?;:'\")�]\\|$\\)" 2 nil)
  654. ("\\(" "\\\\([^�]*?\\\\)" 0 nil)
  655. ("\\[" "\\\\\\[[^�]*?\\\\\\]" 0 nil)
  656. ("$$" "\\$\\$[^�]*?\\$\\$" 0 nil))))
  657. (should (equal (org-test-parse-and-interpret "\\command{}")
  658. "\\command{}\n"))
  659. (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
  660. (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
  661. (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
  662. (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
  663. (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n"))))
  664. (ert-deftest test-org-element/line-break-interpreter ()
  665. "Test line break interpreter."
  666. (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
  667. "First line \\\\\nSecond line\n")))
  668. (ert-deftest test-org-element/link-interpreter ()
  669. "Test link interpreter."
  670. ;; 1. Links targeted from a radio target.
  671. (should (equal (let ((org-target-link-regexp "radio-target"))
  672. (org-test-parse-and-interpret "a radio-target"))
  673. "a radio-target\n"))
  674. ;; 2. Regular links.
  675. ;;
  676. ;; 2.1. Without description.
  677. (should (equal (org-test-parse-and-interpret "[[http://orgmode.org]]")
  678. "[[http://orgmode.org]]\n"))
  679. ;; 2.2. With a description.
  680. (should (equal (org-test-parse-and-interpret
  681. "[[http://orgmode.org][Org mode]]")
  682. "[[http://orgmode.org][Org mode]]\n"))
  683. ;; 2.3. Id links.
  684. (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
  685. ;; 2.4. Custom-id links.
  686. (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
  687. ;; 2.5 Code-ref links.
  688. (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
  689. ;; 3. Normalize plain links.
  690. (should (equal (org-test-parse-and-interpret "http://orgmode.org")
  691. "[[http://orgmode.org]]\n"))
  692. ;; 4. Normalize angular links.
  693. (should (equal (org-test-parse-and-interpret "<http://orgmode.org>")
  694. "[[http://orgmode.org]]\n")))
  695. (ert-deftest test-org-element/macro-interpreter ()
  696. "Test macro interpreter."
  697. ;; 1. Without argument.
  698. (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
  699. ;; 2. With arguments.
  700. (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
  701. "{{{test(arg1,arg2)}}}\n")))
  702. (ert-deftest test-org-element/radio-target-interpreter ()
  703. "Test radio target interpreter."
  704. (should (equal (org-test-parse-and-interpret "<<<some text>>>")
  705. "<<<some text>>>\n")))
  706. (ert-deftest test-org-element/statistics-cookie-interpreter ()
  707. "Test statistics cookie interpreter."
  708. ;; 1. Without percent
  709. (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
  710. ;; 2. With percent.
  711. (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
  712. (ert-deftest test-org-element/strike-through-interpreter ()
  713. "Test strike through interpreter."
  714. (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
  715. (ert-deftest test-org-element/subscript-interpreter ()
  716. "Test subscript interpreter."
  717. ;; 1. Without brackets.
  718. (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
  719. ;; 2. With brackets.
  720. (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
  721. (ert-deftest test-org-element/superscript-interpreter ()
  722. "Test superscript interpreter."
  723. ;; 1. Without brackets.
  724. (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
  725. ;; 2. With brackets.
  726. (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
  727. (ert-deftest test-org-element/target-interpreter ()
  728. "Test target interpreter."
  729. (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
  730. (ert-deftest test-org-element/underline-interpreter ()
  731. "Test underline interpreter."
  732. (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
  733. (ert-deftest test-org-element/verbatim-interpreter ()
  734. "Test verbatim interpreter."
  735. (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
  736. ;;;; Normalize contents
  737. (ert-deftest test-org-element/normalize-contents ()
  738. "Test `org-element-normalize-contents' specifications."
  739. ;; 1. Remove maximum common indentation from element's contents.
  740. (should
  741. (equal
  742. (org-element-normalize-contents
  743. '(paragraph nil " Two spaces\n Three spaces"))
  744. '(paragraph nil "Two spaces\n Three spaces")))
  745. ;; 2. Ignore objects within contents when computing maximum common
  746. ;; indentation.
  747. (should
  748. (equal
  749. (org-element-normalize-contents
  750. '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
  751. '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
  752. ;; 3. Ignore blank lines.
  753. (should
  754. (equal
  755. (org-element-normalize-contents
  756. '(paragraph nil " Two spaces\n\n \n Two spaces"))
  757. '(paragraph nil "Two spaces\n\n \nTwo spaces")))
  758. ;; 4. Recursively enter objects in order to compute common
  759. ;; indentation.
  760. (should
  761. (equal
  762. (org-element-normalize-contents
  763. '(paragraph nil " Two spaces " (bold nil " and\n One space")))
  764. '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
  765. ;; 5. When optional argument is provided, ignore first line
  766. ;; indentation.
  767. (should
  768. (equal
  769. (org-element-normalize-contents
  770. '(paragraph nil "No space\n Two spaces\n Three spaces") t)
  771. '(paragraph nil "No space\nTwo spaces\n Three spaces"))))
  772. ;;;; Navigation tools.
  773. (ert-deftest test-org-element/forward-element ()
  774. "Test `org-element-forward' specifications."
  775. ;; 1. At EOB: should error.
  776. (org-test-with-temp-text "Some text\n"
  777. (goto-char (point-max))
  778. (should-error (org-element-forward)))
  779. ;; 2. Standard move: expected to ignore blank lines.
  780. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  781. (org-element-forward)
  782. (should (looking-at "Second paragraph.")))
  783. ;; 3. Headline tests.
  784. (org-test-with-temp-text "
  785. * Head 1
  786. ** Head 1.1
  787. *** Head 1.1.1
  788. ** Head 1.2"
  789. ;; 3.1. At an headline beginning: move to next headline at the
  790. ;; same level.
  791. (goto-line 3)
  792. (org-element-forward)
  793. (should (looking-at "** Head 1.2"))
  794. ;; 3.2. At an headline beginning: move to parent headline if no
  795. ;; headline at the same level.
  796. (goto-line 3)
  797. (org-element-forward)
  798. (should (looking-at "** Head 1.2")))
  799. ;; 4. Greater element tests.
  800. (org-test-with-temp-text
  801. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  802. ;; 4.1. At a greater element: expected to skip contents.
  803. (org-element-forward)
  804. (should (looking-at "Outside."))
  805. ;; 4.2. At the end of greater element contents: expected to skip
  806. ;; to the end of the greater element.
  807. (goto-line 2)
  808. (org-element-forward)
  809. (should (looking-at "Outside.")))
  810. ;; 5. List tests.
  811. (org-test-with-temp-text "
  812. - item1
  813. - sub1
  814. - sub2
  815. - sub3
  816. Inner paragraph.
  817. - item2
  818. Outside."
  819. ;; 5.1. At list top point: expected to move to the element after
  820. ;; the list.
  821. (goto-line 2)
  822. (org-element-forward)
  823. (should (looking-at "Outside."))
  824. ;; 5.2. Special case: at the first line of a sub-list, but not at
  825. ;; beginning of line, move to next item.
  826. (goto-line 2)
  827. (forward-char)
  828. (org-element-forward)
  829. (should (looking-at "- item2"))
  830. (goto-line 4)
  831. (forward-char)
  832. (org-element-forward)
  833. (should (looking-at " - sub2"))
  834. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  835. (goto-line 4)
  836. (org-element-forward)
  837. (should (looking-at " Inner paragraph."))
  838. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  839. (goto-line 8)
  840. (org-element-forward)
  841. (should (looking-at " Inner paragraph."))
  842. ;; 5.5. At an item: expected to move to next item, if any.
  843. (goto-line 6)
  844. (org-element-forward)
  845. (should (looking-at " - sub3"))))
  846. (ert-deftest test-org-element/backward-element ()
  847. "Test `org-element-backward' specifications."
  848. ;; 1. At BOB (modulo some white spaces): should error.
  849. (org-test-with-temp-text " \nParagraph."
  850. (org-skip-whitespace)
  851. (should-error (org-element-backward)))
  852. ;; 2. Not at the beginning of an element: move at its beginning.
  853. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  854. (goto-line 3)
  855. (end-of-line)
  856. (org-element-backward)
  857. (should (looking-at "Paragraph2.")))
  858. ;; 3. Headline tests.
  859. (org-test-with-temp-text "
  860. * Head 1
  861. ** Head 1.1
  862. *** Head 1.1.1
  863. ** Head 1.2"
  864. ;; 3.1. At an headline beginning: move to previous headline at the
  865. ;; same level.
  866. (goto-line 5)
  867. (org-element-backward)
  868. (should (looking-at "** Head 1.1"))
  869. ;; 3.2. At an headline beginning: move to parent headline if no
  870. ;; headline at the same level.
  871. (goto-line 3)
  872. (org-element-backward)
  873. (should (looking-at "* Head 1"))
  874. ;; 3.3. At the first top-level headline: should error.
  875. (goto-line 2)
  876. (should-error (org-element-backward)))
  877. ;; 4. At beginning of first element inside a greater element:
  878. ;; expected to move to greater element's beginning.
  879. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
  880. (goto-line 3)
  881. (org-element-backward)
  882. (should (looking-at "#\\+BEGIN_CENTER")))
  883. ;; 5. List tests.
  884. (org-test-with-temp-text "
  885. - item1
  886. - sub1
  887. - sub2
  888. - sub3
  889. Inner paragraph.
  890. - item2
  891. Outside."
  892. ;; 5.1. At beginning of sub-list: expected to move to the
  893. ;; paragraph before it.
  894. (goto-line 4)
  895. (org-element-backward)
  896. (should (looking-at "item1"))
  897. ;; 5.2. At an item in a list: expected to move at previous item.
  898. (goto-line 8)
  899. (org-element-backward)
  900. (should (looking-at " - sub2"))
  901. (goto-line 12)
  902. (org-element-backward)
  903. (should (looking-at "- item1"))
  904. ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
  905. ;; beginning.
  906. (goto-line 10)
  907. (org-element-backward)
  908. (should (looking-at " - sub1"))
  909. (goto-line 15)
  910. (org-element-backward)
  911. (should (looking-at "- item1"))
  912. ;; 5.4. At blank-lines before list end: expected to move to top
  913. ;; item.
  914. (goto-line 14)
  915. (org-element-backward)
  916. (should (looking-at "- item1"))))
  917. (ert-deftest test-org-element/up-element ()
  918. "Test `org-element-up' specifications."
  919. ;; 1. At BOB or with no surrounding element: should error.
  920. (org-test-with-temp-text "Paragraph."
  921. (should-error (org-element-up)))
  922. (org-test-with-temp-text "* Head1\n* Head2"
  923. (goto-line 2)
  924. (should-error (org-element-up)))
  925. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  926. (goto-line 3)
  927. (should-error (org-element-up)))
  928. ;; 2. At an headline: move to parent headline.
  929. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  930. (goto-line 3)
  931. (org-element-up)
  932. (should (looking-at "\\* Head1")))
  933. ;; 3. Inside a greater element: move to greater element beginning.
  934. (org-test-with-temp-text
  935. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  936. (goto-line 3)
  937. (org-element-up)
  938. (should (looking-at "#\\+BEGIN_CENTER")))
  939. ;; 4. List tests.
  940. (org-test-with-temp-text "* Top
  941. - item1
  942. - sub1
  943. - sub2
  944. Paragraph within sub2.
  945. - item2"
  946. ;; 4.1. Within an item: move to the item beginning.
  947. (goto-line 8)
  948. (org-element-up)
  949. (should (looking-at " - sub2"))
  950. ;; 4.2. At an item in a sub-list: move to parent item.
  951. (goto-line 4)
  952. (org-element-up)
  953. (should (looking-at "- item1"))
  954. ;; 4.3. At an item in top list: move to beginning of whole list.
  955. (goto-line 10)
  956. (org-element-up)
  957. (should (looking-at "- item1"))
  958. ;; 4.4. Special case. At very top point: should move to parent of
  959. ;; list.
  960. (goto-line 2)
  961. (org-element-up)
  962. (should (looking-at "\\* Top"))))
  963. (ert-deftest test-org-element/down-element ()
  964. "Test `org-element-down' specifications."
  965. ;; 1. Error when the element hasn't got a recursive type.
  966. (org-test-with-temp-text "Paragraph."
  967. (should-error (org-element-down)))
  968. ;; 2. When at a plain-list, move to first item.
  969. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  970. (goto-line 2)
  971. (org-element-down)
  972. (should (looking-at " - Item 1.1")))
  973. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  974. (org-element-down)
  975. (should (looking-at " Item 1")))
  976. ;; 3. When at a table, move to first row
  977. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  978. (org-element-down)
  979. (should (looking-at " a | b |")))
  980. ;; 4. Otherwise, move inside the greater element.
  981. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  982. (org-element-down)
  983. (should (looking-at "Paragraph"))))
  984. (ert-deftest test-org-element/drag-backward ()
  985. "Test `org-element-drag-backward' specifications."
  986. ;; 1. Error when trying to move first element of buffer.
  987. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  988. (should-error (org-element-drag-backward)))
  989. ;; 2. Error when trying to swap nested elements.
  990. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  991. (forward-line)
  992. (should-error (org-element-drag-backward)))
  993. ;; 3. Error when trying to swap an headline element and
  994. ;; a non-headline element.
  995. (org-test-with-temp-text "Test.\n* Head 1"
  996. (forward-line)
  997. (should-error (org-element-drag-backward)))
  998. ;; 4. Otherwise, swap elements, preserving column and blank lines
  999. ;; between elements.
  1000. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  1001. (search-forward "graph")
  1002. (org-element-drag-backward)
  1003. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  1004. (should (looking-at " 2"))))
  1005. (ert-deftest test-org-element/drag-forward ()
  1006. "Test `org-element-drag-forward' specifications."
  1007. ;; 1. Error when trying to move first element of buffer.
  1008. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  1009. (goto-line 3)
  1010. (should-error (org-element-drag-forward)))
  1011. ;; 2. Error when trying to swap nested elements.
  1012. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  1013. (forward-line)
  1014. (should-error (org-element-drag-forward)))
  1015. ;; 3. Error when trying to swap a non-headline element and an
  1016. ;; headline.
  1017. (org-test-with-temp-text "Test.\n* Head 1"
  1018. (should-error (org-element-drag-forward)))
  1019. ;; 4. Otherwise, swap elements, preserving column and blank lines
  1020. ;; between elements.
  1021. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  1022. (search-forward "graph")
  1023. (org-element-drag-forward)
  1024. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  1025. (should (looking-at " 1"))))
  1026. (provide 'test-org-element)
  1027. ;;; test-org-element.el ends here