test-org-element.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. ;;; Tests:
  19. ;;;; Headlines
  20. (ert-deftest test-org-element/headline-quote-keyword ()
  21. "Test QUOTE keyword recognition."
  22. ;; Reference test.
  23. (org-test-with-temp-text "* Headline"
  24. (let ((org-quote-string "QUOTE"))
  25. (should-not (org-element-property :quotedp (org-element-at-point)))))
  26. ;; Standard position.
  27. (org-test-with-temp-text "* QUOTE Headline"
  28. (let ((org-quote-string "QUOTE"))
  29. (let ((headline (org-element-at-point)))
  30. (should (org-element-property :quotedp headline))
  31. ;; Test removal from raw value.
  32. (should (equal (org-element-property :raw-value headline) "Headline"))))
  33. ;; Case sensitivity.
  34. (let ((org-quote-string "Quote"))
  35. (should-not (org-element-property :quotedp (org-element-at-point)))))
  36. ;; With another keyword.
  37. (org-test-with-temp-text "* TODO QUOTE Headline"
  38. (let ((org-quote-string "QUOTE")
  39. (org-todo-keywords '((sequence "TODO" "DONE"))))
  40. (should (org-element-property :quotedp (org-element-at-point))))))
  41. (ert-deftest test-org-element/headline-comment-keyword ()
  42. "Test COMMENT keyword recognition."
  43. ;; Reference test.
  44. (org-test-with-temp-text "* Headline"
  45. (let ((org-comment-string "COMMENT"))
  46. (should-not (org-element-property :commentedp (org-element-at-point)))))
  47. ;; Standard position.
  48. (org-test-with-temp-text "* COMMENT Headline"
  49. (let ((org-comment-string "COMMENT"))
  50. (let ((headline (org-element-at-point)))
  51. (should (org-element-property :commentedp headline))
  52. ;; Test removal from raw value.
  53. (should (equal (org-element-property :raw-value headline) "Headline"))))
  54. ;; Case sensitivity.
  55. (let ((org-comment-string "Comment"))
  56. (should-not (org-element-property :commentedp (org-element-at-point)))))
  57. ;; With another keyword.
  58. (org-test-with-temp-text "* TODO COMMENT Headline"
  59. (let ((org-comment-string "COMMENT")
  60. (org-todo-keywords '((sequence "TODO" "DONE"))))
  61. (should (org-element-property :commentedp (org-element-at-point))))))
  62. (ert-deftest test-org-element/headline-archive-tag ()
  63. "Test ARCHIVE tag recognition."
  64. ;; Reference test.
  65. (org-test-with-temp-text "* Headline"
  66. (let ((org-archive-tag "ARCHIVE"))
  67. (should-not (org-element-property :archivedp (org-element-at-point)))))
  68. ;; Single tag.
  69. (org-test-with-temp-text "* Headline :ARCHIVE:"
  70. (let ((org-archive-tag "ARCHIVE"))
  71. (let ((headline (org-element-at-point)))
  72. (should (org-element-property :archivedp headline))
  73. ;; Test tag removal.
  74. (should-not (org-element-property :tags headline))))
  75. (let ((org-archive-tag "Archive"))
  76. (should-not (org-element-property :archivedp (org-element-at-point)))))
  77. ;; Multiple tags.
  78. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  79. (let ((org-archive-tag "ARCHIVE"))
  80. (let ((headline (org-element-at-point)))
  81. (should (org-element-property :archivedp headline))
  82. ;; Test tag removal.
  83. (should (equal (org-element-property :tags headline) ":test:"))))))
  84. ;;;; Example-blocks and Src-blocks
  85. (ert-deftest test-org-element/block-switches ()
  86. "Test `example-block' and `src-block' switches parsing."
  87. (let ((org-coderef-label-format "(ref:%s)"))
  88. ;; 1. Test "-i" switch.
  89. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  90. (let ((element (org-element-current-element)))
  91. (should-not (org-element-property :preserve-indent element))))
  92. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  93. (let ((element (org-element-current-element)))
  94. (should (org-element-property :preserve-indent element))))
  95. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  96. (let ((element (org-element-current-element)))
  97. (should-not (org-element-property :preserve-indent element))))
  98. (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
  99. (let ((element (org-element-current-element)))
  100. (should (org-element-property :preserve-indent element))))
  101. ;; 2. "-n -r -k" combination should number lines, retain labels but
  102. ;; not use them in coderefs.
  103. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\N#+END_EXAMPLE"
  104. (let ((element (org-element-current-element)))
  105. (should (and (org-element-property :number-lines element)
  106. (org-element-property :retain-labels element)
  107. (not (org-element-property :use-labels element))))))
  108. (org-test-with-temp-text
  109. "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
  110. (let ((element (org-element-current-element)))
  111. (should (and (org-element-property :number-lines element)
  112. (org-element-property :retain-labels element)
  113. (not (org-element-property :use-labels element))))))
  114. ;; 3. "-n -r" combination should number-lines remove labels and not
  115. ;; use them in coderefs.
  116. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
  117. (let ((element (org-element-current-element)))
  118. (should (and (org-element-property :number-lines element)
  119. (not (org-element-property :retain-labels element))
  120. (not (org-element-property :use-labels element))))))
  121. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
  122. (let ((element (org-element-current-element)))
  123. (should (and (org-element-property :number-lines element)
  124. (not (org-element-property :retain-labels element))
  125. (not (org-element-property :use-labels element))))))
  126. ;; 4. "-n" or "+n" should number lines, retain labels and use them
  127. ;; in coderefs.
  128. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
  129. (let ((element (org-element-current-element)))
  130. (should (and (org-element-property :number-lines element)
  131. (org-element-property :retain-labels element)
  132. (org-element-property :use-labels element)))))
  133. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
  134. (let ((element (org-element-current-element)))
  135. (should (and (org-element-property :number-lines element)
  136. (org-element-property :retain-labels element)
  137. (org-element-property :use-labels element)))))
  138. (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
  139. (let ((element (org-element-current-element)))
  140. (should (and (org-element-property :number-lines element)
  141. (org-element-property :retain-labels element)
  142. (org-element-property :use-labels element)))))
  143. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
  144. (let ((element (org-element-current-element)))
  145. (should (and (org-element-property :number-lines element)
  146. (org-element-property :retain-labels element)
  147. (org-element-property :use-labels element)))))
  148. ;; 5. No switch should not number lines, but retain labels and use
  149. ;; them in coderefs.
  150. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  151. (let ((element (org-element-current-element)))
  152. (should (and (not (org-element-property :number-lines element))
  153. (org-element-property :retain-labels element)
  154. (org-element-property :use-labels element)))))
  155. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  156. (let ((element (org-element-current-element)))
  157. (should (and (not (org-element-property :number-lines element))
  158. (org-element-property :retain-labels element)
  159. (org-element-property :use-labels element)))))
  160. ;; 6. "-r" switch only: do not number lines, remove labels, and
  161. ;; don't use labels in coderefs.
  162. (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
  163. (let ((element (org-element-current-element)))
  164. (should (and (not (org-element-property :number-lines element))
  165. (not (org-element-property :retain-labels element))
  166. (not (org-element-property :use-labels element))))))
  167. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
  168. (let ((element (org-element-current-element)))
  169. (should (and (not (org-element-property :number-lines element))
  170. (not (org-element-property :retain-labels element))
  171. (not (org-element-property :use-labels element))))))
  172. ;; 7. Recognize coderefs with user-defined syntax.
  173. (org-test-with-temp-text
  174. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
  175. (let ((element (org-element-current-element)))
  176. (should
  177. (equal (org-element-property :label-fmt element) "[ref:%s]"))))
  178. (org-test-with-temp-text
  179. "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
  180. (let ((element (org-element-current-element)))
  181. (should
  182. (equal (org-element-property :label-fmt element) "[ref:%s]"))))))
  183. ;;;; Footnotes references and definitions
  184. (ert-deftest test-org-element/footnote-reference ()
  185. "Test footnote-reference parsing."
  186. ;; 1. Parse a standard reference.
  187. (org-test-with-temp-text "[fn:label]"
  188. (should (equal (org-element-footnote-reference-parser)
  189. '(footnote-reference
  190. (:label "fn:label" :type standard :inline-definition nil
  191. :begin 1 :end 11 :post-blank 0)))))
  192. ;; 2. Parse a normalized reference.
  193. (org-test-with-temp-text "[1]"
  194. (should (equal (org-element-footnote-reference-parser)
  195. '(footnote-reference
  196. (:label "1" :type standard :inline-definition nil
  197. :begin 1 :end 4 :post-blank 0)))))
  198. ;; 3. Parse an inline reference.
  199. (org-test-with-temp-text "[fn:test:def]"
  200. (should (equal (org-element-footnote-reference-parser)
  201. '(footnote-reference
  202. (:label "fn:test" :type inline :inline-definition ("def")
  203. :begin 1 :end 14 :post-blank 0)))))
  204. ;; 4. Parse an anonymous reference.
  205. (org-test-with-temp-text "[fn::def]"
  206. (should (equal (org-element-footnote-reference-parser)
  207. '(footnote-reference
  208. (:label nil :type inline :inline-definition ("def")
  209. :begin 1 :end 10 :post-blank 0)))))
  210. ;; 5. Parse nested footnotes.
  211. (org-test-with-temp-text "[fn::def [fn:label]]"
  212. (should
  213. (equal
  214. (org-element-footnote-reference-parser)
  215. '(footnote-reference
  216. (:label nil :type inline
  217. :inline-definition
  218. ("def "
  219. (footnote-reference
  220. (:label "fn:label" :type standard :inline-definition nil
  221. :begin 5 :end 15 :post-blank 0)))
  222. :begin 1 :end 21 :post-blank 0)))))
  223. ;; 6. Parse adjacent footnotes.
  224. (org-test-with-temp-text "[fn:label1][fn:label2]"
  225. (should
  226. (equal
  227. (org-element-footnote-reference-parser)
  228. '(footnote-reference
  229. (:label "fn:label1" :type standard :inline-definition nil :begin 1
  230. :end 12 :post-blank 0)))))
  231. ;; 7. Only properly closed footnotes are recognized as such.
  232. (org-test-with-temp-text "Text [fn:label"
  233. (should-not
  234. (org-element-map
  235. (org-element-parse-buffer) 'footnote-reference 'identity))))
  236. ;;;; Granularity
  237. (ert-deftest test-org-element/granularity ()
  238. "Test granularity impact on buffer parsing."
  239. (org-test-with-temp-text "
  240. * Head 1
  241. ** Head 2
  242. #+BEGIN_CENTER
  243. Centered paragraph.
  244. #+END_CENTER
  245. Paragraph \\alpha."
  246. ;; 1.1. Granularity set to `headline' should parse every headline
  247. ;; in buffer, and only them.
  248. (let ((tree (org-element-parse-buffer 'headline)))
  249. (should (= 2 (length (org-element-map tree 'headline 'identity))))
  250. (should-not (org-element-map tree 'paragraph 'identity)))
  251. ;; 1.2. Granularity set to `greater-element' should not enter
  252. ;; greater elements excepted headlines and sections.
  253. (let ((tree (org-element-parse-buffer 'greater-element)))
  254. (should (= 1 (length (org-element-map tree 'center-block 'identity))))
  255. (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
  256. (should-not (org-element-map tree 'entity 'identity)))
  257. ;; 1.3. Granularity set to `element' should enter every
  258. ;; greater-element.
  259. (let ((tree (org-element-parse-buffer 'element)))
  260. (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
  261. (should-not (org-element-map tree 'entity 'identity)))
  262. ;; 1.4. Granularity set to `object' can see everything.
  263. (let ((tree (org-element-parse-buffer 'object)))
  264. (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
  265. (ert-deftest test-org-element/secondary-string-parsing ()
  266. "Test if granularity correctly toggles secondary strings parsing."
  267. ;; 1. With a granularity bigger than `object', no secondary string
  268. ;; should be parsed.
  269. ;;
  270. ;; 1.1. Test with `headline' type.
  271. (org-test-with-temp-text "* Headline"
  272. (let ((headline
  273. (org-element-map (org-element-parse-buffer 'headline) 'headline
  274. 'identity
  275. nil
  276. 'first-match)))
  277. (should (stringp (org-element-property :title headline)))))
  278. ;; 1.2. Test with `item' type.
  279. (org-test-with-temp-text "* Headline\n- tag :: item"
  280. (let ((item (org-element-map (org-element-parse-buffer 'element)
  281. 'item
  282. 'identity
  283. nil
  284. 'first-match)))
  285. (should (stringp (org-element-property :tag item)))))
  286. ;; 1.3. Test with `verse-block' type.
  287. (org-test-with-temp-text "#+BEGIN_VERSE\nTest\n#+END_VERSE"
  288. (let ((verse-block (org-element-map (org-element-parse-buffer 'element)
  289. 'verse-block
  290. 'identity
  291. nil
  292. 'first-match)))
  293. (should (stringp (org-element-property :value verse-block)))))
  294. ;; 1.4. Test with `inlinetask' type, if avalaible.
  295. (when (featurep 'org-inlinetask)
  296. (let ((org-inlinetask-min-level 15))
  297. (org-test-with-temp-text "*************** Inlinetask"
  298. (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
  299. 'inlinetask
  300. 'identity
  301. nil
  302. 'first-match)))
  303. (should (stringp (org-element-property :title inlinetask)))))))
  304. ;; 2. With a default granularity, secondary strings should be
  305. ;; parsed.
  306. (org-test-with-temp-text "* Headline"
  307. (let ((headline
  308. (org-element-map (org-element-parse-buffer) 'headline
  309. 'identity
  310. nil
  311. 'first-match)))
  312. (should (listp (org-element-property :title headline)))))
  313. ;; 3. `org-element-at-point' should never parse a secondary string.
  314. (org-test-with-temp-text "* Headline"
  315. (should (stringp (org-element-property :title (org-element-at-point))))))
  316. ;;;; Interpretation.
  317. (ert-deftest test-org-element/interpret-affiliated-keywords ()
  318. "Test if affiliated keywords are correctly interpreted."
  319. ;; Interpret simple keywords.
  320. (should
  321. (equal
  322. (org-element-interpret-data
  323. '(org-data nil (paragraph (:name "para") "Paragraph")))
  324. "#+NAME: para\nParagraph\n"))
  325. ;; Interpret multiple keywords.
  326. (should
  327. (equal
  328. (org-element-interpret-data
  329. '(org-data nil (paragraph (:attr_ascii ("line1" "line2")) "Paragraph")))
  330. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
  331. ;; Interpret parsed keywords.
  332. (should
  333. (equal
  334. (org-element-interpret-data
  335. '(org-data nil (paragraph (:caption ("caption")) "Paragraph")))
  336. "#+CAPTION: caption\nParagraph\n"))
  337. ;; Interpret dual keywords.
  338. (should
  339. (equal
  340. (org-element-interpret-data
  341. '(org-data nil (paragraph (:caption (("long") "short")) "Paragraph")))
  342. "#+CAPTION[short]: long\nParagraph\n")))
  343. ;;;; Navigation tools.
  344. (ert-deftest test-org-element/forward-element ()
  345. "Test `org-element-forward' specifications."
  346. ;; 1. At EOB: should error.
  347. (org-test-with-temp-text "Some text\n"
  348. (goto-char (point-max))
  349. (should-error (org-element-forward)))
  350. ;; 2. Standard move: expected to ignore blank lines.
  351. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  352. (org-element-forward)
  353. (should (looking-at "Second paragraph.")))
  354. ;; 3. Headline tests.
  355. (org-test-with-temp-text "
  356. * Head 1
  357. ** Head 1.1
  358. *** Head 1.1.1
  359. ** Head 1.2"
  360. ;; 3.1. At an headline beginning: move to next headline at the
  361. ;; same level.
  362. (goto-line 3)
  363. (org-element-forward)
  364. (should (looking-at "** Head 1.2"))
  365. ;; 3.2. At an headline beginning: move to parent headline if no
  366. ;; headline at the same level.
  367. (goto-line 3)
  368. (org-element-forward)
  369. (should (looking-at "** Head 1.2")))
  370. ;; 4. Greater element tests.
  371. (org-test-with-temp-text
  372. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  373. ;; 4.1. At a greater element: expected to skip contents.
  374. (org-element-forward)
  375. (should (looking-at "Outside."))
  376. ;; 4.2. At the end of greater element contents: expected to skip
  377. ;; to the end of the greater element.
  378. (goto-line 2)
  379. (org-element-forward)
  380. (should (looking-at "Outside.")))
  381. ;; 5. List tests.
  382. (org-test-with-temp-text "
  383. - item1
  384. - sub1
  385. - sub2
  386. - sub3
  387. Inner paragraph.
  388. - item2
  389. Outside."
  390. ;; 5.1. At list top point: expected to move to the element after
  391. ;; the list.
  392. (goto-line 2)
  393. (org-element-forward)
  394. (should (looking-at "Outside."))
  395. ;; 5.2. Special case: at the first line of a sub-list, but not at
  396. ;; beginning of line, move to next item.
  397. (goto-line 2)
  398. (forward-char)
  399. (org-element-forward)
  400. (should (looking-at "- item2"))
  401. (goto-line 4)
  402. (forward-char)
  403. (org-element-forward)
  404. (should (looking-at " - sub2"))
  405. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  406. (goto-line 4)
  407. (org-element-forward)
  408. (should (looking-at " Inner paragraph."))
  409. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  410. (goto-line 8)
  411. (org-element-forward)
  412. (should (looking-at " Inner paragraph."))
  413. ;; 5.5. At an item: expected to move to next item, if any.
  414. (goto-line 6)
  415. (org-element-forward)
  416. (should (looking-at " - sub3"))))
  417. (ert-deftest test-org-element/backward-element ()
  418. "Test `org-element-backward' specifications."
  419. ;; 1. At BOB (modulo some white spaces): should error.
  420. (org-test-with-temp-text " \nParagraph."
  421. (org-skip-whitespace)
  422. (should-error (org-element-backward)))
  423. ;; 2. Not at the beginning of an element: move at its beginning.
  424. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  425. (goto-line 3)
  426. (end-of-line)
  427. (org-element-backward)
  428. (should (looking-at "Paragraph2.")))
  429. ;; 3. Headline tests.
  430. (org-test-with-temp-text "
  431. * Head 1
  432. ** Head 1.1
  433. *** Head 1.1.1
  434. ** Head 1.2"
  435. ;; 3.1. At an headline beginning: move to previous headline at the
  436. ;; same level.
  437. (goto-line 5)
  438. (org-element-backward)
  439. (should (looking-at "** Head 1.1"))
  440. ;; 3.2. At an headline beginning: move to parent headline if no
  441. ;; headline at the same level.
  442. (goto-line 3)
  443. (org-element-backward)
  444. (should (looking-at "* Head 1"))
  445. ;; 3.3. At the first top-level headline: should error.
  446. (goto-line 2)
  447. (should-error (org-element-backward)))
  448. ;; 4. At beginning of first element inside a greater element:
  449. ;; expected to move to greater element's beginning.
  450. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
  451. (goto-line 3)
  452. (org-element-backward)
  453. (should (looking-at "#\\+BEGIN_CENTER")))
  454. ;; 5. List tests.
  455. (org-test-with-temp-text "
  456. - item1
  457. - sub1
  458. - sub2
  459. - sub3
  460. Inner paragraph.
  461. - item2
  462. Outside."
  463. ;; 5.1. At beginning of sub-list: expected to move to the
  464. ;; paragraph before it.
  465. (goto-line 4)
  466. (org-element-backward)
  467. (should (looking-at "item1"))
  468. ;; 5.2. At an item in a list: expected to move at previous item.
  469. (goto-line 8)
  470. (org-element-backward)
  471. (should (looking-at " - sub2"))
  472. (goto-line 12)
  473. (org-element-backward)
  474. (should (looking-at "- item1"))
  475. ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
  476. ;; beginning.
  477. (goto-line 10)
  478. (org-element-backward)
  479. (should (looking-at " - sub1"))
  480. (goto-line 15)
  481. (org-element-backward)
  482. (should (looking-at "- item1"))
  483. ;; 5.4. At blank-lines before list end: expected to move to top
  484. ;; item.
  485. (goto-line 14)
  486. (org-element-backward)
  487. (should (looking-at "- item1"))))
  488. (ert-deftest test-org-element/up-element ()
  489. "Test `org-element-up' specifications."
  490. ;; 1. At BOB or with no surrounding element: should error.
  491. (org-test-with-temp-text "Paragraph."
  492. (should-error (org-element-up)))
  493. (org-test-with-temp-text "* Head1\n* Head2"
  494. (goto-line 2)
  495. (should-error (org-element-up)))
  496. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  497. (goto-line 3)
  498. (should-error (org-element-up)))
  499. ;; 2. At an headline: move to parent headline.
  500. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  501. (goto-line 3)
  502. (org-element-up)
  503. (should (looking-at "\\* Head1")))
  504. ;; 3. Inside a greater element: move to greater element beginning.
  505. (org-test-with-temp-text
  506. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  507. (goto-line 3)
  508. (org-element-up)
  509. (should (looking-at "#\\+BEGIN_CENTER")))
  510. ;; 4. List tests.
  511. (org-test-with-temp-text "* Top
  512. - item1
  513. - sub1
  514. - sub2
  515. Paragraph within sub2.
  516. - item2"
  517. ;; 4.1. Within an item: move to the item beginning.
  518. (goto-line 8)
  519. (org-element-up)
  520. (should (looking-at " - sub2"))
  521. ;; 4.2. At an item in a sub-list: move to parent item.
  522. (goto-line 4)
  523. (org-element-up)
  524. (should (looking-at "- item1"))
  525. ;; 4.3. At an item in top list: move to beginning of whole list.
  526. (goto-line 10)
  527. (org-element-up)
  528. (should (looking-at "- item1"))
  529. ;; 4.4. Special case. At very top point: should move to parent of
  530. ;; list.
  531. (goto-line 2)
  532. (org-element-up)
  533. (should (looking-at "\\* Top"))))
  534. (ert-deftest test-org-element/down-element ()
  535. "Test `org-element-down' specifications."
  536. ;; 1. Error when the element hasn't got a recursive type.
  537. (org-test-with-temp-text "Paragraph."
  538. (should-error (org-element-down)))
  539. ;; 2. When at a plain-list, move to first item.
  540. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  541. (goto-line 2)
  542. (org-element-down)
  543. (should (looking-at " - Item 1.1")))
  544. ;; 3. Otherwise, move inside the greater element.
  545. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  546. (org-element-down)
  547. (should (looking-at "Paragraph"))))
  548. (ert-deftest test-org-element/drag-backward ()
  549. "Test `org-element-drag-backward' specifications."
  550. ;; 1. Error when trying to move first element of buffer.
  551. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  552. (should-error (org-element-drag-backward)))
  553. ;; 2. Error when trying to swap nested elements.
  554. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  555. (forward-line)
  556. (should-error (org-element-drag-backward)))
  557. ;; 3. Error when trying to swap an headline element and
  558. ;; a non-headline element.
  559. (org-test-with-temp-text "Test.\n* Head 1"
  560. (forward-line)
  561. (should-error (org-element-drag-backward)))
  562. ;; 4. Otherwise, swap elements, preserving column and blank lines
  563. ;; between elements.
  564. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  565. (search-forward "graph")
  566. (org-element-drag-backward)
  567. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  568. (should (looking-at " 2"))))
  569. (ert-deftest test-org-element/drag-forward ()
  570. "Test `org-element-drag-forward' specifications."
  571. ;; 1. Error when trying to move first element of buffer.
  572. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  573. (goto-line 3)
  574. (should-error (org-element-drag-forward)))
  575. ;; 2. Error when trying to swap nested elements.
  576. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  577. (forward-line)
  578. (should-error (org-element-drag-forward)))
  579. ;; 3. Error when trying to swap a non-headline element and an
  580. ;; headline.
  581. (org-test-with-temp-text "Test.\n* Head 1"
  582. (should-error (org-element-drag-forward)))
  583. ;; 4. Otherwise, swap elements, preserving column and blank lines
  584. ;; between elements.
  585. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  586. (search-forward "graph")
  587. (org-element-drag-forward)
  588. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  589. (should (looking-at " 1"))))
  590. (provide 'test-org-element)
  591. ;;; test-org-element.el ends here