test-org-element.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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 `inlinetask' type, if avalaible.
  287. (when (featurep 'org-inlinetask)
  288. (let ((org-inlinetask-min-level 15))
  289. (org-test-with-temp-text "*************** Inlinetask"
  290. (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
  291. 'inlinetask
  292. 'identity
  293. nil
  294. 'first-match)))
  295. (should (stringp (org-element-property :title inlinetask)))))))
  296. ;; 2. With a default granularity, secondary strings should be
  297. ;; parsed.
  298. (org-test-with-temp-text "* Headline"
  299. (let ((headline
  300. (org-element-map (org-element-parse-buffer) 'headline
  301. 'identity
  302. nil
  303. 'first-match)))
  304. (should (listp (org-element-property :title headline)))))
  305. ;; 3. `org-element-at-point' should never parse a secondary string.
  306. (org-test-with-temp-text "* Headline"
  307. (should (stringp (org-element-property :title (org-element-at-point))))))
  308. ;;;; Interpretation.
  309. (ert-deftest test-org-element/interpret-affiliated-keywords ()
  310. "Test if affiliated keywords are correctly interpreted."
  311. ;; Interpret simple keywords.
  312. (should
  313. (equal
  314. (org-element-interpret-data
  315. '(org-data nil (paragraph (:name "para") "Paragraph")))
  316. "#+NAME: para\nParagraph\n"))
  317. ;; Interpret multiple keywords.
  318. (should
  319. (equal
  320. (org-element-interpret-data
  321. '(org-data nil (paragraph (:attr_ascii ("line1" "line2")) "Paragraph")))
  322. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
  323. ;; Interpret parsed keywords.
  324. (should
  325. (equal
  326. (org-element-interpret-data
  327. '(org-data nil (paragraph (:caption ("caption")) "Paragraph")))
  328. "#+CAPTION: caption\nParagraph\n"))
  329. ;; Interpret dual keywords.
  330. (should
  331. (equal
  332. (org-element-interpret-data
  333. '(org-data nil (paragraph (:caption (("long") "short")) "Paragraph")))
  334. "#+CAPTION[short]: long\nParagraph\n")))
  335. ;;;; Normalize contents
  336. (ert-deftest test-org-element/normalize-contents ()
  337. "Test `org-element-normalize-contents' specifications."
  338. ;; 1. Remove maximum common indentation from element's contents.
  339. (should
  340. (equal
  341. (org-element-normalize-contents
  342. '(paragraph nil " Two spaces\n Three spaces"))
  343. '(paragraph nil "Two spaces\n Three spaces")))
  344. ;; 2. Ignore objects within contents when computing maximum common
  345. ;; indentation.
  346. (should
  347. (equal
  348. (org-element-normalize-contents
  349. '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
  350. '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
  351. ;; 3. Ignore blank lines.
  352. (should
  353. (equal
  354. (org-element-normalize-contents
  355. '(paragraph nil " Two spaces\n\n \n Two spaces"))
  356. '(paragraph nil "Two spaces\n\n \nTwo spaces")))
  357. ;; 4. Recursively enter objects in order to compute common
  358. ;; indentation.
  359. (should
  360. (equal
  361. (org-element-normalize-contents
  362. '(paragraph nil " Two spaces " (emphasis nil " and\n One space")))
  363. '(paragraph nil " Two spaces " (emphasis nil " and\nOne space"))))
  364. ;; 5. When optional argument is provided, ignore first line
  365. ;; indentation.
  366. (should
  367. (equal
  368. (org-element-normalize-contents
  369. '(paragraph nil "No space\n Two spaces\n Three spaces") t)
  370. '(paragraph nil "No space\nTwo spaces\n Three spaces"))))
  371. ;;;; Navigation tools.
  372. (ert-deftest test-org-element/forward-element ()
  373. "Test `org-element-forward' specifications."
  374. ;; 1. At EOB: should error.
  375. (org-test-with-temp-text "Some text\n"
  376. (goto-char (point-max))
  377. (should-error (org-element-forward)))
  378. ;; 2. Standard move: expected to ignore blank lines.
  379. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  380. (org-element-forward)
  381. (should (looking-at "Second paragraph.")))
  382. ;; 3. Headline tests.
  383. (org-test-with-temp-text "
  384. * Head 1
  385. ** Head 1.1
  386. *** Head 1.1.1
  387. ** Head 1.2"
  388. ;; 3.1. At an headline beginning: move to next headline at the
  389. ;; same level.
  390. (goto-line 3)
  391. (org-element-forward)
  392. (should (looking-at "** Head 1.2"))
  393. ;; 3.2. At an headline beginning: move to parent headline if no
  394. ;; headline at the same level.
  395. (goto-line 3)
  396. (org-element-forward)
  397. (should (looking-at "** Head 1.2")))
  398. ;; 4. Greater element tests.
  399. (org-test-with-temp-text
  400. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  401. ;; 4.1. At a greater element: expected to skip contents.
  402. (org-element-forward)
  403. (should (looking-at "Outside."))
  404. ;; 4.2. At the end of greater element contents: expected to skip
  405. ;; to the end of the greater element.
  406. (goto-line 2)
  407. (org-element-forward)
  408. (should (looking-at "Outside.")))
  409. ;; 5. List tests.
  410. (org-test-with-temp-text "
  411. - item1
  412. - sub1
  413. - sub2
  414. - sub3
  415. Inner paragraph.
  416. - item2
  417. Outside."
  418. ;; 5.1. At list top point: expected to move to the element after
  419. ;; the list.
  420. (goto-line 2)
  421. (org-element-forward)
  422. (should (looking-at "Outside."))
  423. ;; 5.2. Special case: at the first line of a sub-list, but not at
  424. ;; beginning of line, move to next item.
  425. (goto-line 2)
  426. (forward-char)
  427. (org-element-forward)
  428. (should (looking-at "- item2"))
  429. (goto-line 4)
  430. (forward-char)
  431. (org-element-forward)
  432. (should (looking-at " - sub2"))
  433. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  434. (goto-line 4)
  435. (org-element-forward)
  436. (should (looking-at " Inner paragraph."))
  437. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  438. (goto-line 8)
  439. (org-element-forward)
  440. (should (looking-at " Inner paragraph."))
  441. ;; 5.5. At an item: expected to move to next item, if any.
  442. (goto-line 6)
  443. (org-element-forward)
  444. (should (looking-at " - sub3"))))
  445. (ert-deftest test-org-element/backward-element ()
  446. "Test `org-element-backward' specifications."
  447. ;; 1. At BOB (modulo some white spaces): should error.
  448. (org-test-with-temp-text " \nParagraph."
  449. (org-skip-whitespace)
  450. (should-error (org-element-backward)))
  451. ;; 2. Not at the beginning of an element: move at its beginning.
  452. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  453. (goto-line 3)
  454. (end-of-line)
  455. (org-element-backward)
  456. (should (looking-at "Paragraph2.")))
  457. ;; 3. Headline tests.
  458. (org-test-with-temp-text "
  459. * Head 1
  460. ** Head 1.1
  461. *** Head 1.1.1
  462. ** Head 1.2"
  463. ;; 3.1. At an headline beginning: move to previous headline at the
  464. ;; same level.
  465. (goto-line 5)
  466. (org-element-backward)
  467. (should (looking-at "** Head 1.1"))
  468. ;; 3.2. At an headline beginning: move to parent headline if no
  469. ;; headline at the same level.
  470. (goto-line 3)
  471. (org-element-backward)
  472. (should (looking-at "* Head 1"))
  473. ;; 3.3. At the first top-level headline: should error.
  474. (goto-line 2)
  475. (should-error (org-element-backward)))
  476. ;; 4. At beginning of first element inside a greater element:
  477. ;; expected to move to greater element's beginning.
  478. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
  479. (goto-line 3)
  480. (org-element-backward)
  481. (should (looking-at "#\\+BEGIN_CENTER")))
  482. ;; 5. List tests.
  483. (org-test-with-temp-text "
  484. - item1
  485. - sub1
  486. - sub2
  487. - sub3
  488. Inner paragraph.
  489. - item2
  490. Outside."
  491. ;; 5.1. At beginning of sub-list: expected to move to the
  492. ;; paragraph before it.
  493. (goto-line 4)
  494. (org-element-backward)
  495. (should (looking-at "item1"))
  496. ;; 5.2. At an item in a list: expected to move at previous item.
  497. (goto-line 8)
  498. (org-element-backward)
  499. (should (looking-at " - sub2"))
  500. (goto-line 12)
  501. (org-element-backward)
  502. (should (looking-at "- item1"))
  503. ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
  504. ;; beginning.
  505. (goto-line 10)
  506. (org-element-backward)
  507. (should (looking-at " - sub1"))
  508. (goto-line 15)
  509. (org-element-backward)
  510. (should (looking-at "- item1"))
  511. ;; 5.4. At blank-lines before list end: expected to move to top
  512. ;; item.
  513. (goto-line 14)
  514. (org-element-backward)
  515. (should (looking-at "- item1"))))
  516. (ert-deftest test-org-element/up-element ()
  517. "Test `org-element-up' specifications."
  518. ;; 1. At BOB or with no surrounding element: should error.
  519. (org-test-with-temp-text "Paragraph."
  520. (should-error (org-element-up)))
  521. (org-test-with-temp-text "* Head1\n* Head2"
  522. (goto-line 2)
  523. (should-error (org-element-up)))
  524. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  525. (goto-line 3)
  526. (should-error (org-element-up)))
  527. ;; 2. At an headline: move to parent headline.
  528. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  529. (goto-line 3)
  530. (org-element-up)
  531. (should (looking-at "\\* Head1")))
  532. ;; 3. Inside a greater element: move to greater element beginning.
  533. (org-test-with-temp-text
  534. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  535. (goto-line 3)
  536. (org-element-up)
  537. (should (looking-at "#\\+BEGIN_CENTER")))
  538. ;; 4. List tests.
  539. (org-test-with-temp-text "* Top
  540. - item1
  541. - sub1
  542. - sub2
  543. Paragraph within sub2.
  544. - item2"
  545. ;; 4.1. Within an item: move to the item beginning.
  546. (goto-line 8)
  547. (org-element-up)
  548. (should (looking-at " - sub2"))
  549. ;; 4.2. At an item in a sub-list: move to parent item.
  550. (goto-line 4)
  551. (org-element-up)
  552. (should (looking-at "- item1"))
  553. ;; 4.3. At an item in top list: move to beginning of whole list.
  554. (goto-line 10)
  555. (org-element-up)
  556. (should (looking-at "- item1"))
  557. ;; 4.4. Special case. At very top point: should move to parent of
  558. ;; list.
  559. (goto-line 2)
  560. (org-element-up)
  561. (should (looking-at "\\* Top"))))
  562. (ert-deftest test-org-element/down-element ()
  563. "Test `org-element-down' specifications."
  564. ;; 1. Error when the element hasn't got a recursive type.
  565. (org-test-with-temp-text "Paragraph."
  566. (should-error (org-element-down)))
  567. ;; 2. When at a plain-list, move to first item.
  568. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  569. (goto-line 2)
  570. (org-element-down)
  571. (should (looking-at " - Item 1.1")))
  572. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  573. (org-element-down)
  574. (should (looking-at " Item 1")))
  575. ;; 3. When at a table, move to first row
  576. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  577. (org-element-down)
  578. (should (looking-at " a | b |")))
  579. ;; 4. Otherwise, move inside the greater element.
  580. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  581. (org-element-down)
  582. (should (looking-at "Paragraph"))))
  583. (ert-deftest test-org-element/drag-backward ()
  584. "Test `org-element-drag-backward' specifications."
  585. ;; 1. Error when trying to move first element of buffer.
  586. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  587. (should-error (org-element-drag-backward)))
  588. ;; 2. Error when trying to swap nested elements.
  589. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  590. (forward-line)
  591. (should-error (org-element-drag-backward)))
  592. ;; 3. Error when trying to swap an headline element and
  593. ;; a non-headline element.
  594. (org-test-with-temp-text "Test.\n* Head 1"
  595. (forward-line)
  596. (should-error (org-element-drag-backward)))
  597. ;; 4. Otherwise, swap elements, preserving column and blank lines
  598. ;; between elements.
  599. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  600. (search-forward "graph")
  601. (org-element-drag-backward)
  602. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  603. (should (looking-at " 2"))))
  604. (ert-deftest test-org-element/drag-forward ()
  605. "Test `org-element-drag-forward' specifications."
  606. ;; 1. Error when trying to move first element of buffer.
  607. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  608. (goto-line 3)
  609. (should-error (org-element-drag-forward)))
  610. ;; 2. Error when trying to swap nested elements.
  611. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  612. (forward-line)
  613. (should-error (org-element-drag-forward)))
  614. ;; 3. Error when trying to swap a non-headline element and an
  615. ;; headline.
  616. (org-test-with-temp-text "Test.\n* Head 1"
  617. (should-error (org-element-drag-forward)))
  618. ;; 4. Otherwise, swap elements, preserving column and blank lines
  619. ;; between elements.
  620. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  621. (search-forward "graph")
  622. (org-element-drag-forward)
  623. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  624. (should (looking-at " 1"))))
  625. (provide 'test-org-element)
  626. ;;; test-org-element.el ends here