test-org.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. ;;; test-org.el
  2. ;; Copyright (c) ߚ David Maus
  3. ;; Authors: David Maus
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  6. ;;;; Comments:
  7. ;; Template test file for Org-mode tests
  8. ;;; Code:
  9. (ert-deftest test-org/org-link-escape-ascii-character ()
  10. "Escape an ascii character."
  11. (should
  12. (string=
  13. "%5B"
  14. (org-link-escape "["))))
  15. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  16. "Escape an ascii control character."
  17. (should
  18. (string=
  19. "%09"
  20. (org-link-escape "\t"))))
  21. (ert-deftest test-org/org-link-escape-multibyte-character ()
  22. "Escape an unicode multibyte character."
  23. (should
  24. (string=
  25. "%E2%82%AC"
  26. (org-link-escape "€"))))
  27. (ert-deftest test-org/org-link-escape-custom-table ()
  28. "Escape string with custom character table."
  29. (should
  30. (string=
  31. "Foo%3A%42ar%0A"
  32. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  33. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  34. "Escape string with custom table merged with default table."
  35. (should
  36. (string=
  37. "%5BF%6F%6F%3A%42ar%0A%5D"
  38. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  39. (ert-deftest test-org/org-link-unescape-ascii-character ()
  40. "Unescape an ascii character."
  41. (should
  42. (string=
  43. "["
  44. (org-link-unescape "%5B"))))
  45. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  46. "Unescpae an ascii control character."
  47. (should
  48. (string=
  49. "\n"
  50. (org-link-unescape "%0A"))))
  51. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  52. "Unescape unicode multibyte character."
  53. (should
  54. (string=
  55. "€"
  56. (org-link-unescape "%E2%82%AC"))))
  57. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  58. "Unescape old style percent escaped character."
  59. (should
  60. (string=
  61. "àâçèéêîôùû"
  62. (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  63. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  64. "Escape and unscape a URL that includes an escaped char.
  65. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  66. (should
  67. (string=
  68. "http://some.host.com/form?&id=blah%2Bblah25"
  69. (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  70. (ert-deftest test-org/accumulated-properties-in-drawers ()
  71. "Ensure properties accumulate in subtree drawers."
  72. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  73. (org-babel-next-src-block)
  74. (should (equal '(2 1) (org-babel-execute-src-block)))))
  75. ;;; Links
  76. ;;;; Fuzzy links
  77. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  78. ;; a target keyword (aka an invisible target: #+TARGET: text), to
  79. ;; a named element (#+name: text) and to headlines (* Text).
  80. (ert-deftest test-org/fuzzy-links ()
  81. "Test fuzzy links specifications."
  82. ;; 1. Fuzzy link goes in priority to a matching target.
  83. (org-test-with-temp-text
  84. "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  85. (goto-line 6)
  86. (org-open-at-point)
  87. (should (looking-at "<<Test>>")))
  88. ;; 2. Fuzzy link should then go to a matching target keyword.
  89. (org-test-with-temp-text
  90. "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
  91. (goto-line 5)
  92. (org-open-at-point)
  93. (should (looking-at "#\\+TARGET: Test")))
  94. ;; 3. Then fuzzy link points to an element with a given name.
  95. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  96. (goto-line 5)
  97. (org-open-at-point)
  98. (should (looking-at "#\\+NAME: Test")))
  99. ;; 4. A target still lead to a matching headline otherwise.
  100. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  101. (goto-line 4)
  102. (org-open-at-point)
  103. (should (looking-at "\\* Head2")))
  104. ;; 5. With a leading star in link, enforce heading match.
  105. (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
  106. (goto-line 4)
  107. (org-open-at-point)
  108. (should (looking-at "\\* Test"))))
  109. ;;; Filling
  110. (ert-deftest test-org/fill-paragraph ()
  111. "Test `org-fill-paragraph' specifications."
  112. ;; At an Org table, align it.
  113. (org-test-with-temp-text "|a|"
  114. (org-fill-paragraph)
  115. (should (equal (buffer-string) "| a |\n")))
  116. ;; At a paragraph, preserve line breaks.
  117. (org-test-with-temp-text "some \\\\\nlong\ntext"
  118. (let ((fill-column 20))
  119. (org-fill-paragraph)
  120. (should (equal (buffer-string) "some \\\\\nlong text"))))
  121. ;; Correctly fill a paragraph when point is at its very end.
  122. (should
  123. (equal "A B"
  124. (org-test-with-temp-text "A\nB"
  125. (let ((fill-column 20))
  126. (goto-char (point-max))
  127. (org-fill-paragraph)
  128. (buffer-string)))))
  129. ;; Correctly fill the last paragraph of a greater element.
  130. (should
  131. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  132. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  133. (let ((fill-column 8))
  134. (forward-line)
  135. (end-of-line)
  136. (org-fill-paragraph)
  137. (buffer-string)))))
  138. ;; Special case: Fill first paragraph when point is at an item or
  139. ;; a plain-list or a footnote reference.
  140. (should
  141. (equal "- A B"
  142. (org-test-with-temp-text "- A\n B"
  143. (let ((fill-column 20))
  144. (org-fill-paragraph)
  145. (buffer-string)))))
  146. (should
  147. (equal "[fn:1] A B"
  148. (org-test-with-temp-text "[fn:1] A\nB"
  149. (let ((fill-column 20))
  150. (org-fill-paragraph)
  151. (buffer-string)))))
  152. ;; At a verse block, fill paragraph at point, also preserving line
  153. ;; breaks. Though, do nothing when point is at the block
  154. ;; boundaries.
  155. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  156. (forward-line)
  157. (let ((fill-column 20))
  158. (org-fill-paragraph)
  159. (should (equal (buffer-string)
  160. "#+BEGIN_VERSE\nSome \\\\\nlong text\n#+END_VERSE"))))
  161. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  162. (let ((fill-column 20))
  163. (org-fill-paragraph)
  164. (should (equal (buffer-string)
  165. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  166. ;; Fill contents of `comment-block' elements.
  167. (should
  168. (equal
  169. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  170. (let ((fill-column 20))
  171. (forward-line)
  172. (org-fill-paragraph)
  173. (buffer-string)))
  174. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  175. ;; Fill `comment' elements.
  176. (should
  177. (equal " # A B"
  178. (org-test-with-temp-text " # A\n # B"
  179. (let ((fill-column 20))
  180. (org-fill-paragraph)
  181. (buffer-string)))))
  182. ;; Do nothing at affiliated keywords.
  183. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  184. (let ((fill-column 20))
  185. (org-fill-paragraph)
  186. (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
  187. (ert-deftest test-org/auto-fill-function ()
  188. "Test auto-filling features."
  189. ;; Auto fill paragraph.
  190. (should
  191. (equal "12345\n7890"
  192. (org-test-with-temp-text "12345 7890"
  193. (let ((fill-column 5))
  194. (end-of-line)
  195. (org-auto-fill-function)
  196. (buffer-string)))))
  197. ;; Auto fill first paragraph in an item.
  198. (should
  199. (equal "- 12345\n 7890"
  200. (org-test-with-temp-text "- 12345 7890"
  201. (let ((fill-column 7))
  202. (end-of-line)
  203. (org-auto-fill-function)
  204. (buffer-string)))))
  205. ;; Auto fill comments.
  206. (should
  207. (equal " # 12345\n # 7890"
  208. (org-test-with-temp-text " # 12345 7890"
  209. (let ((fill-column 10))
  210. (end-of-line)
  211. (org-auto-fill-function)
  212. (buffer-string)))))
  213. ;; Verse and comment block: auto fill contents.
  214. (should
  215. (equal "#+BEGIN_VERSE\n12345\n7890\n#+END_VERSE"
  216. (org-test-with-temp-text "#+BEGIN_VERSE\n12345 7890\n#+END_VERSE"
  217. (let ((fill-column 5))
  218. (forward-line)
  219. (end-of-line)
  220. (org-auto-fill-function)
  221. (buffer-string)))))
  222. (should
  223. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  224. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  225. (let ((fill-column 5))
  226. (forward-line)
  227. (end-of-line)
  228. (org-auto-fill-function)
  229. (buffer-string)))))
  230. ;; Do not fill if a new item could be created.
  231. (should-not
  232. (equal "12345\n- 90"
  233. (org-test-with-temp-text "12345 - 90"
  234. (let ((fill-column 5))
  235. (end-of-line)
  236. (org-auto-fill-function)
  237. (buffer-string)))))
  238. ;; Do not fill if a line break could be introduced.
  239. (should-not
  240. (equal "123\\\\\n7890"
  241. (org-test-with-temp-text "123\\\\ 7890"
  242. (let ((fill-column 6))
  243. (end-of-line)
  244. (org-auto-fill-function)
  245. (buffer-string)))))
  246. ;; Do not fill affiliated keywords.
  247. (should-not
  248. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  249. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  250. (let ((fill-column 20))
  251. (end-of-line)
  252. (org-auto-fill-function)
  253. (buffer-string))))))
  254. ;;; Comments
  255. (ert-deftest test-org/comment-dwim ()
  256. "Test `comment-dwim' behaviour in an Org buffer."
  257. ;; No region selected, no comment on current line and line not
  258. ;; empty: insert comment on line above.
  259. (should
  260. (equal "# \nComment"
  261. (org-test-with-temp-text "Comment"
  262. (progn (call-interactively 'comment-dwim)
  263. (buffer-string)))))
  264. ;; No region selected, no comment on current line and line empty:
  265. ;; insert comment on this line.
  266. (should
  267. (equal "# \nParagraph"
  268. (org-test-with-temp-text "\nParagraph"
  269. (progn (call-interactively 'comment-dwim)
  270. (buffer-string)))))
  271. ;; No region selected, and a comment on this line: indent it.
  272. (should
  273. (equal "* Headline\n # Comment"
  274. (org-test-with-temp-text "* Headline\n# Comment"
  275. (progn (forward-line)
  276. (let ((org-adapt-indentation t))
  277. (call-interactively 'comment-dwim))
  278. (buffer-string)))))
  279. ;; Also recognize single # at column 0 as comments.
  280. (should
  281. (equal "# Comment"
  282. (org-test-with-temp-text "# Comment"
  283. (progn (forward-line)
  284. (call-interactively 'comment-dwim)
  285. (buffer-string)))))
  286. ;; Region selected and only comments and blank lines within it:
  287. ;; un-comment all commented lines.
  288. (should
  289. (equal "Comment 1\n\nComment 2"
  290. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  291. (progn
  292. (transient-mark-mode 1)
  293. (push-mark (point) t t)
  294. (goto-char (point-max))
  295. (call-interactively 'comment-dwim)
  296. (buffer-string)))))
  297. ;; Region selected without comments: comment all non-blank lines.
  298. (should
  299. (equal "# Comment 1\n\n# Comment 2"
  300. (org-test-with-temp-text "Comment 1\n\nComment 2"
  301. (progn
  302. (transient-mark-mode 1)
  303. (push-mark (point) t t)
  304. (goto-char (point-max))
  305. (call-interactively 'comment-dwim)
  306. (buffer-string))))))
  307. ;;; Mark region
  308. (ert-deftest test-org/mark-subtree ()
  309. "Test `org-mark-subtree' specifications."
  310. ;; Error when point is before first headline.
  311. (should-error
  312. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  313. (progn (transient-mark-mode 1)
  314. (org-mark-subtree))))
  315. ;; Without argument, mark current subtree.
  316. (should
  317. (equal
  318. '(12 32)
  319. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  320. (progn (transient-mark-mode 1)
  321. (forward-line 2)
  322. (org-mark-subtree)
  323. (list (region-beginning) (region-end))))))
  324. ;; With an argument, move ARG up.
  325. (should
  326. (equal
  327. '(1 32)
  328. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  329. (progn (transient-mark-mode 1)
  330. (forward-line 2)
  331. (org-mark-subtree 1)
  332. (list (region-beginning) (region-end))))))
  333. ;; Do not get fooled with inlinetasks.
  334. (when (featurep 'org-inlinetask)
  335. (should
  336. (= 1
  337. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  338. (progn (transient-mark-mode 1)
  339. (forward-line 1)
  340. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  341. (region-beginning)))))))
  342. ;; Navigation
  343. (ert-deftest test-org/forward-element ()
  344. "Test `org-forward-element' specifications."
  345. ;; 1. At EOB: should error.
  346. (org-test-with-temp-text "Some text\n"
  347. (goto-char (point-max))
  348. (should-error (org-forward-element)))
  349. ;; 2. Standard move: expected to ignore blank lines.
  350. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  351. (org-forward-element)
  352. (should (looking-at "Second paragraph.")))
  353. ;; 3. Headline tests.
  354. (org-test-with-temp-text "
  355. * Head 1
  356. ** Head 1.1
  357. *** Head 1.1.1
  358. ** Head 1.2"
  359. ;; 3.1. At an headline beginning: move to next headline at the
  360. ;; same level.
  361. (goto-line 3)
  362. (org-forward-element)
  363. (should (looking-at "** Head 1.2"))
  364. ;; 3.2. At an headline beginning: move to parent headline if no
  365. ;; headline at the same level.
  366. (goto-line 3)
  367. (org-forward-element)
  368. (should (looking-at "** Head 1.2")))
  369. ;; 4. Greater element tests.
  370. (org-test-with-temp-text
  371. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  372. ;; 4.1. At a greater element: expected to skip contents.
  373. (org-forward-element)
  374. (should (looking-at "Outside."))
  375. ;; 4.2. At the end of greater element contents: expected to skip
  376. ;; to the end of the greater element.
  377. (goto-line 2)
  378. (org-forward-element)
  379. (should (looking-at "Outside.")))
  380. ;; 5. List tests.
  381. (org-test-with-temp-text "
  382. - item1
  383. - sub1
  384. - sub2
  385. - sub3
  386. Inner paragraph.
  387. - item2
  388. Outside."
  389. ;; 5.1. At list top point: expected to move to the element after
  390. ;; the list.
  391. (goto-line 2)
  392. (org-forward-element)
  393. (should (looking-at "Outside."))
  394. ;; 5.2. Special case: at the first line of a sub-list, but not at
  395. ;; beginning of line, move to next item.
  396. (goto-line 2)
  397. (forward-char)
  398. (org-forward-element)
  399. (should (looking-at "- item2"))
  400. (goto-line 4)
  401. (forward-char)
  402. (org-forward-element)
  403. (should (looking-at " - sub2"))
  404. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  405. (goto-line 4)
  406. (org-forward-element)
  407. (should (looking-at " Inner paragraph."))
  408. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  409. (goto-line 8)
  410. (org-forward-element)
  411. (should (looking-at " Inner paragraph."))
  412. ;; 5.5. At an item: expected to move to next item, if any.
  413. (goto-line 6)
  414. (org-forward-element)
  415. (should (looking-at " - sub3"))))
  416. (ert-deftest test-org/backward-element ()
  417. "Test `org-backward-element' specifications."
  418. ;; 1. At BOB (modulo some white spaces): should error.
  419. (org-test-with-temp-text " \nParagraph."
  420. (org-skip-whitespace)
  421. (should-error (org-backward-element)))
  422. ;; 2. Not at the beginning of an element: move at its beginning.
  423. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  424. (goto-line 3)
  425. (end-of-line)
  426. (org-backward-element)
  427. (should (looking-at "Paragraph2.")))
  428. ;; 3. Headline tests.
  429. (org-test-with-temp-text "
  430. * Head 1
  431. ** Head 1.1
  432. *** Head 1.1.1
  433. ** Head 1.2"
  434. ;; 3.1. At an headline beginning: move to previous headline at the
  435. ;; same level.
  436. (goto-line 5)
  437. (org-backward-element)
  438. (should (looking-at "** Head 1.1"))
  439. ;; 3.2. At an headline beginning: move to parent headline if no
  440. ;; headline at the same level.
  441. (goto-line 3)
  442. (org-backward-element)
  443. (should (looking-at "* Head 1"))
  444. ;; 3.3. At the first top-level headline: should error.
  445. (goto-line 2)
  446. (should-error (org-backward-element)))
  447. ;; 4. At beginning of first element inside a greater element:
  448. ;; expected to move to greater element's beginning.
  449. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER."
  450. (goto-line 3)
  451. (org-backward-element)
  452. (should (looking-at "#\\+BEGIN_CENTER")))
  453. ;; 5. List tests.
  454. (org-test-with-temp-text "
  455. - item1
  456. - sub1
  457. - sub2
  458. - sub3
  459. Inner paragraph.
  460. - item2
  461. Outside."
  462. ;; 5.1. At beginning of sub-list: expected to move to the
  463. ;; paragraph before it.
  464. (goto-line 4)
  465. (org-backward-element)
  466. (should (looking-at "item1"))
  467. ;; 5.2. At an item in a list: expected to move at previous item.
  468. (goto-line 8)
  469. (org-backward-element)
  470. (should (looking-at " - sub2"))
  471. (goto-line 12)
  472. (org-backward-element)
  473. (should (looking-at "- item1"))
  474. ;; 5.3. At end of list/sub-list: expected to move to list/sub-list
  475. ;; beginning.
  476. (goto-line 10)
  477. (org-backward-element)
  478. (should (looking-at " - sub1"))
  479. (goto-line 15)
  480. (org-backward-element)
  481. (should (looking-at "- item1"))
  482. ;; 5.4. At blank-lines before list end: expected to move to top
  483. ;; item.
  484. (goto-line 14)
  485. (org-backward-element)
  486. (should (looking-at "- item1"))))
  487. (ert-deftest test-org/up-element ()
  488. "Test `org-up-element' specifications."
  489. ;; 1. At BOB or with no surrounding element: should error.
  490. (org-test-with-temp-text "Paragraph."
  491. (should-error (org-up-element)))
  492. (org-test-with-temp-text "* Head1\n* Head2"
  493. (goto-line 2)
  494. (should-error (org-up-element)))
  495. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  496. (goto-line 3)
  497. (should-error (org-up-element)))
  498. ;; 2. At an headline: move to parent headline.
  499. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  500. (goto-line 3)
  501. (org-up-element)
  502. (should (looking-at "\\* Head1")))
  503. ;; 3. Inside a greater element: move to greater element beginning.
  504. (org-test-with-temp-text
  505. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  506. (goto-line 3)
  507. (org-up-element)
  508. (should (looking-at "#\\+BEGIN_CENTER")))
  509. ;; 4. List tests.
  510. (org-test-with-temp-text "* Top
  511. - item1
  512. - sub1
  513. - sub2
  514. Paragraph within sub2.
  515. - item2"
  516. ;; 4.1. Within an item: move to the item beginning.
  517. (goto-line 8)
  518. (org-up-element)
  519. (should (looking-at " - sub2"))
  520. ;; 4.2. At an item in a sub-list: move to parent item.
  521. (goto-line 4)
  522. (org-up-element)
  523. (should (looking-at "- item1"))
  524. ;; 4.3. At an item in top list: move to beginning of whole list.
  525. (goto-line 10)
  526. (org-up-element)
  527. (should (looking-at "- item1"))
  528. ;; 4.4. Special case. At very top point: should move to parent of
  529. ;; list.
  530. (goto-line 2)
  531. (org-up-element)
  532. (should (looking-at "\\* Top"))))
  533. (ert-deftest test-org/down-element ()
  534. "Test `org-down-element' specifications."
  535. ;; Error when the element hasn't got a recursive type.
  536. (org-test-with-temp-text "Paragraph."
  537. (should-error (org-down-element)))
  538. ;; Error when the element has no contents
  539. (org-test-with-temp-text "* Headline"
  540. (should-error (org-down-element)))
  541. ;; When at a plain-list, move to first item.
  542. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  543. (goto-line 2)
  544. (org-down-element)
  545. (should (looking-at " - Item 1.1")))
  546. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  547. (org-down-element)
  548. (should (looking-at " Item 1")))
  549. ;; When at a table, move to first row
  550. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  551. (org-down-element)
  552. (should (looking-at " a | b |")))
  553. ;; Otherwise, move inside the greater element.
  554. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  555. (org-down-element)
  556. (should (looking-at "Paragraph"))))
  557. (ert-deftest test-org/drag-element-backward ()
  558. "Test `org-drag-element-backward' specifications."
  559. ;; 1. Error when trying to move first element of buffer.
  560. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  561. (should-error (org-drag-element-backward)))
  562. ;; 2. Error when trying to swap nested elements.
  563. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  564. (forward-line)
  565. (should-error (org-drag-element-backward)))
  566. ;; 3. Error when trying to swap an headline element and
  567. ;; a non-headline element.
  568. (org-test-with-temp-text "Test.\n* Head 1"
  569. (forward-line)
  570. (should-error (org-drag-element-backward)))
  571. ;; 4. Otherwise, swap elements, preserving column and blank lines
  572. ;; between elements.
  573. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  574. (search-forward "graph")
  575. (org-drag-element-backward)
  576. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  577. (should (looking-at " 2")))
  578. ;; 5. Preserve visibility of elements and their contents.
  579. (org-test-with-temp-text "
  580. #+BEGIN_CENTER
  581. Text.
  582. #+END_CENTER
  583. - item 1
  584. #+BEGIN_QUOTE
  585. Text.
  586. #+END_QUOTE"
  587. (while (search-forward "BEGIN_" nil t) (org-cycle))
  588. (search-backward "- item 1")
  589. (org-drag-element-backward)
  590. (should
  591. (equal
  592. '((63 . 82) (26 . 48))
  593. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  594. (overlays-in (point-min) (point-max)))))))
  595. (ert-deftest test-org/drag-element-forward ()
  596. "Test `org-drag-element-forward' specifications."
  597. ;; 1. Error when trying to move first element of buffer.
  598. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  599. (goto-line 3)
  600. (should-error (org-drag-element-forward)))
  601. ;; 2. Error when trying to swap nested elements.
  602. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  603. (forward-line)
  604. (should-error (org-drag-element-forward)))
  605. ;; 3. Error when trying to swap a non-headline element and an
  606. ;; headline.
  607. (org-test-with-temp-text "Test.\n* Head 1"
  608. (should-error (org-drag-element-forward)))
  609. ;; 4. Otherwise, swap elements, preserving column and blank lines
  610. ;; between elements.
  611. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  612. (search-forward "graph")
  613. (org-drag-element-forward)
  614. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  615. (should (looking-at " 1")))
  616. ;; 5. Preserve visibility of elements and their contents.
  617. (org-test-with-temp-text "
  618. #+BEGIN_CENTER
  619. Text.
  620. #+END_CENTER
  621. - item 1
  622. #+BEGIN_QUOTE
  623. Text.
  624. #+END_QUOTE"
  625. (while (search-forward "BEGIN_" nil t) (org-cycle))
  626. (search-backward "#+BEGIN_CENTER")
  627. (org-drag-element-forward)
  628. (should
  629. (equal
  630. '((63 . 82) (26 . 48))
  631. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  632. (overlays-in (point-min) (point-max)))))))
  633. (provide 'test-org)
  634. ;;; test-org.el ends here