test-org.el 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. ;; Correctly fill an element in a narrowed buffer.
  139. (should
  140. (equal "01234\n6"
  141. (org-test-with-temp-text "01234 6789"
  142. (let ((fill-column 5))
  143. (narrow-to-region 1 8)
  144. (org-fill-paragraph)
  145. (buffer-string)))))
  146. ;; Special case: Fill first paragraph when point is at an item or
  147. ;; a plain-list or a footnote reference.
  148. (should
  149. (equal "- A B"
  150. (org-test-with-temp-text "- A\n B"
  151. (let ((fill-column 20))
  152. (org-fill-paragraph)
  153. (buffer-string)))))
  154. (should
  155. (equal "[fn:1] A B"
  156. (org-test-with-temp-text "[fn:1] A\nB"
  157. (let ((fill-column 20))
  158. (org-fill-paragraph)
  159. (buffer-string)))))
  160. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  161. (let ((fill-column 20))
  162. (org-fill-paragraph)
  163. (should (equal (buffer-string)
  164. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  165. ;; Fill contents of `comment-block' elements.
  166. (should
  167. (equal
  168. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  169. (let ((fill-column 20))
  170. (forward-line)
  171. (org-fill-paragraph)
  172. (buffer-string)))
  173. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  174. ;; Fill `comment' elements.
  175. (should
  176. (equal " # A B"
  177. (org-test-with-temp-text " # A\n # B"
  178. (let ((fill-column 20))
  179. (org-fill-paragraph)
  180. (buffer-string)))))
  181. ;; Do nothing at affiliated keywords.
  182. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  183. (let ((fill-column 20))
  184. (org-fill-paragraph)
  185. (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
  186. (ert-deftest test-org/auto-fill-function ()
  187. "Test auto-filling features."
  188. ;; Auto fill paragraph.
  189. (should
  190. (equal "12345\n7890"
  191. (org-test-with-temp-text "12345 7890"
  192. (let ((fill-column 5))
  193. (end-of-line)
  194. (org-auto-fill-function)
  195. (buffer-string)))))
  196. ;; Auto fill first paragraph in an item.
  197. (should
  198. (equal "- 12345\n 7890"
  199. (org-test-with-temp-text "- 12345 7890"
  200. (let ((fill-column 7))
  201. (end-of-line)
  202. (org-auto-fill-function)
  203. (buffer-string)))))
  204. ;; Auto fill comments.
  205. (should
  206. (equal " # 12345\n # 7890"
  207. (org-test-with-temp-text " # 12345 7890"
  208. (let ((fill-column 10))
  209. (end-of-line)
  210. (org-auto-fill-function)
  211. (buffer-string)))))
  212. ;; Comment block: auto fill contents.
  213. (should
  214. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  215. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  216. (let ((fill-column 5))
  217. (forward-line)
  218. (end-of-line)
  219. (org-auto-fill-function)
  220. (buffer-string)))))
  221. (should
  222. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  223. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  224. (let ((fill-column 5))
  225. (forward-line)
  226. (end-of-line)
  227. (org-auto-fill-function)
  228. (buffer-string)))))
  229. ;; Do not fill if a new item could be created.
  230. (should-not
  231. (equal "12345\n- 90"
  232. (org-test-with-temp-text "12345 - 90"
  233. (let ((fill-column 5))
  234. (end-of-line)
  235. (org-auto-fill-function)
  236. (buffer-string)))))
  237. ;; Do not fill if a line break could be introduced.
  238. (should-not
  239. (equal "123\\\\\n7890"
  240. (org-test-with-temp-text "123\\\\ 7890"
  241. (let ((fill-column 6))
  242. (end-of-line)
  243. (org-auto-fill-function)
  244. (buffer-string)))))
  245. ;; Do not fill affiliated keywords.
  246. (should-not
  247. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  248. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  249. (let ((fill-column 20))
  250. (end-of-line)
  251. (org-auto-fill-function)
  252. (buffer-string))))))
  253. ;;; Comments
  254. (ert-deftest test-org/comment-dwim ()
  255. "Test `comment-dwim' behaviour in an Org buffer."
  256. ;; No region selected, no comment on current line and line not
  257. ;; empty: insert comment on line above.
  258. (should
  259. (equal "# \nComment"
  260. (org-test-with-temp-text "Comment"
  261. (progn (call-interactively 'comment-dwim)
  262. (buffer-string)))))
  263. ;; No region selected, no comment on current line and line empty:
  264. ;; insert comment on this line.
  265. (should
  266. (equal "# \nParagraph"
  267. (org-test-with-temp-text "\nParagraph"
  268. (progn (call-interactively 'comment-dwim)
  269. (buffer-string)))))
  270. ;; No region selected, and a comment on this line: indent it.
  271. (should
  272. (equal "* Headline\n # Comment"
  273. (org-test-with-temp-text "* Headline\n# Comment"
  274. (progn (forward-line)
  275. (let ((org-adapt-indentation t))
  276. (call-interactively 'comment-dwim))
  277. (buffer-string)))))
  278. ;; Also recognize single # at column 0 as comments.
  279. (should
  280. (equal "# Comment"
  281. (org-test-with-temp-text "# Comment"
  282. (progn (forward-line)
  283. (call-interactively 'comment-dwim)
  284. (buffer-string)))))
  285. ;; Region selected and only comments and blank lines within it:
  286. ;; un-comment all commented lines.
  287. (should
  288. (equal "Comment 1\n\nComment 2"
  289. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  290. (progn
  291. (transient-mark-mode 1)
  292. (push-mark (point) t t)
  293. (goto-char (point-max))
  294. (call-interactively 'comment-dwim)
  295. (buffer-string)))))
  296. ;; Region selected without comments: comment all non-blank lines.
  297. (should
  298. (equal "# Comment 1\n\n# Comment 2"
  299. (org-test-with-temp-text "Comment 1\n\nComment 2"
  300. (progn
  301. (transient-mark-mode 1)
  302. (push-mark (point) t t)
  303. (goto-char (point-max))
  304. (call-interactively 'comment-dwim)
  305. (buffer-string)))))
  306. ;; In front of a keyword without region, insert a new comment.
  307. (should
  308. (equal "# \n#+KEYWORD: value"
  309. (org-test-with-temp-text "#+KEYWORD: value"
  310. (progn (call-interactively 'comment-dwim)
  311. (buffer-string))))))
  312. ;;; Mark region
  313. (ert-deftest test-org/mark-subtree ()
  314. "Test `org-mark-subtree' specifications."
  315. ;; Error when point is before first headline.
  316. (should-error
  317. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  318. (progn (transient-mark-mode 1)
  319. (org-mark-subtree))))
  320. ;; Without argument, mark current subtree.
  321. (should
  322. (equal
  323. '(12 32)
  324. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  325. (progn (transient-mark-mode 1)
  326. (forward-line 2)
  327. (org-mark-subtree)
  328. (list (region-beginning) (region-end))))))
  329. ;; With an argument, move ARG up.
  330. (should
  331. (equal
  332. '(1 32)
  333. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  334. (progn (transient-mark-mode 1)
  335. (forward-line 2)
  336. (org-mark-subtree 1)
  337. (list (region-beginning) (region-end))))))
  338. ;; Do not get fooled with inlinetasks.
  339. (when (featurep 'org-inlinetask)
  340. (should
  341. (= 1
  342. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  343. (progn (transient-mark-mode 1)
  344. (forward-line 1)
  345. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  346. (region-beginning)))))))
  347. ;; Navigation
  348. (ert-deftest test-org/forward-element ()
  349. "Test `org-forward-element' specifications."
  350. ;; 1. At EOB: should error.
  351. (org-test-with-temp-text "Some text\n"
  352. (goto-char (point-max))
  353. (should-error (org-forward-element)))
  354. ;; 2. Standard move: expected to ignore blank lines.
  355. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  356. (org-forward-element)
  357. (should (looking-at "Second paragraph.")))
  358. ;; 3. Headline tests.
  359. (org-test-with-temp-text "
  360. * Head 1
  361. ** Head 1.1
  362. *** Head 1.1.1
  363. ** Head 1.2"
  364. ;; 3.1. At an headline beginning: move to next headline at the
  365. ;; same level.
  366. (goto-line 3)
  367. (org-forward-element)
  368. (should (looking-at "** Head 1.2"))
  369. ;; 3.2. At an headline beginning: move to parent headline if no
  370. ;; headline at the same level.
  371. (goto-line 3)
  372. (org-forward-element)
  373. (should (looking-at "** Head 1.2")))
  374. ;; 4. Greater element tests.
  375. (org-test-with-temp-text
  376. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  377. ;; 4.1. At a greater element: expected to skip contents.
  378. (org-forward-element)
  379. (should (looking-at "Outside."))
  380. ;; 4.2. At the end of greater element contents: expected to skip
  381. ;; to the end of the greater element.
  382. (goto-line 2)
  383. (org-forward-element)
  384. (should (looking-at "Outside.")))
  385. ;; 5. List tests.
  386. (org-test-with-temp-text "
  387. - item1
  388. - sub1
  389. - sub2
  390. - sub3
  391. Inner paragraph.
  392. - item2
  393. Outside."
  394. ;; 5.1. At list top point: expected to move to the element after
  395. ;; the list.
  396. (goto-line 2)
  397. (org-forward-element)
  398. (should (looking-at "Outside."))
  399. ;; 5.2. Special case: at the first line of a sub-list, but not at
  400. ;; beginning of line, move to next item.
  401. (goto-line 2)
  402. (forward-char)
  403. (org-forward-element)
  404. (should (looking-at "- item2"))
  405. (goto-line 4)
  406. (forward-char)
  407. (org-forward-element)
  408. (should (looking-at " - sub2"))
  409. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  410. (goto-line 4)
  411. (org-forward-element)
  412. (should (looking-at " Inner paragraph."))
  413. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  414. (goto-line 8)
  415. (org-forward-element)
  416. (should (looking-at " Inner paragraph."))
  417. ;; 5.5. At an item: expected to move to next item, if any.
  418. (goto-line 6)
  419. (org-forward-element)
  420. (should (looking-at " - sub3"))))
  421. (ert-deftest test-org/backward-element ()
  422. "Test `org-backward-element' specifications."
  423. ;; 1. Should error at BOB.
  424. (org-test-with-temp-text " \nParagraph."
  425. (should-error (org-backward-element)))
  426. ;; 2. Should move at BOB when called on the first element in buffer.
  427. (should
  428. (org-test-with-temp-text "\n#+TITLE: test"
  429. (progn (forward-line)
  430. (org-backward-element)
  431. (bobp))))
  432. ;; 3. Not at the beginning of an element: move at its beginning.
  433. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  434. (goto-line 3)
  435. (end-of-line)
  436. (org-backward-element)
  437. (should (looking-at "Paragraph2.")))
  438. ;; 4. Headline tests.
  439. (org-test-with-temp-text "
  440. * Head 1
  441. ** Head 1.1
  442. *** Head 1.1.1
  443. ** Head 1.2"
  444. ;; 4.1. At an headline beginning: move to previous headline at the
  445. ;; same level.
  446. (goto-line 5)
  447. (org-backward-element)
  448. (should (looking-at "** Head 1.1"))
  449. ;; 4.2. At an headline beginning: move to parent headline if no
  450. ;; headline at the same level.
  451. (goto-line 3)
  452. (org-backward-element)
  453. (should (looking-at "* Head 1"))
  454. ;; 4.3. At the first top-level headline: should error.
  455. (goto-line 2)
  456. (should-error (org-backward-element)))
  457. ;; 5. At beginning of first element inside a greater element:
  458. ;; expected to move to greater element's beginning.
  459. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  460. (goto-line 3)
  461. (org-backward-element)
  462. (should (looking-at "#\\+BEGIN_CENTER")))
  463. ;; 6. At the beginning of the first element in a section: should
  464. ;; move back to headline, if any.
  465. (should
  466. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  467. (progn (goto-char (point-max))
  468. (beginning-of-line)
  469. (org-backward-element)
  470. (org-at-heading-p))))
  471. ;; 7. List tests.
  472. (org-test-with-temp-text "
  473. - item1
  474. - sub1
  475. - sub2
  476. - sub3
  477. Inner paragraph.
  478. - item2
  479. Outside."
  480. ;; 7.1. At beginning of sub-list: expected to move to the
  481. ;; paragraph before it.
  482. (goto-line 4)
  483. (org-backward-element)
  484. (should (looking-at "item1"))
  485. ;; 7.2. At an item in a list: expected to move at previous item.
  486. (goto-line 8)
  487. (org-backward-element)
  488. (should (looking-at " - sub2"))
  489. (goto-line 12)
  490. (org-backward-element)
  491. (should (looking-at "- item1"))
  492. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  493. ;; beginning.
  494. (goto-line 10)
  495. (org-backward-element)
  496. (should (looking-at " - sub1"))
  497. (goto-line 15)
  498. (org-backward-element)
  499. (should (looking-at "- item1"))
  500. ;; 7.4. At blank-lines before list end: expected to move to top
  501. ;; item.
  502. (goto-line 14)
  503. (org-backward-element)
  504. (should (looking-at "- item1"))))
  505. (ert-deftest test-org/up-element ()
  506. "Test `org-up-element' specifications."
  507. ;; 1. At BOB or with no surrounding element: should error.
  508. (org-test-with-temp-text "Paragraph."
  509. (should-error (org-up-element)))
  510. (org-test-with-temp-text "* Head1\n* Head2"
  511. (goto-line 2)
  512. (should-error (org-up-element)))
  513. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  514. (goto-line 3)
  515. (should-error (org-up-element)))
  516. ;; 2. At an headline: move to parent headline.
  517. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  518. (goto-line 3)
  519. (org-up-element)
  520. (should (looking-at "\\* Head1")))
  521. ;; 3. Inside a greater element: move to greater element beginning.
  522. (org-test-with-temp-text
  523. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  524. (goto-line 3)
  525. (org-up-element)
  526. (should (looking-at "#\\+BEGIN_CENTER")))
  527. ;; 4. List tests.
  528. (org-test-with-temp-text "* Top
  529. - item1
  530. - sub1
  531. - sub2
  532. Paragraph within sub2.
  533. - item2"
  534. ;; 4.1. Within an item: move to the item beginning.
  535. (goto-line 8)
  536. (org-up-element)
  537. (should (looking-at " - sub2"))
  538. ;; 4.2. At an item in a sub-list: move to parent item.
  539. (goto-line 4)
  540. (org-up-element)
  541. (should (looking-at "- item1"))
  542. ;; 4.3. At an item in top list: move to beginning of whole list.
  543. (goto-line 10)
  544. (org-up-element)
  545. (should (looking-at "- item1"))
  546. ;; 4.4. Special case. At very top point: should move to parent of
  547. ;; list.
  548. (goto-line 2)
  549. (org-up-element)
  550. (should (looking-at "\\* Top"))))
  551. (ert-deftest test-org/down-element ()
  552. "Test `org-down-element' specifications."
  553. ;; Error when the element hasn't got a recursive type.
  554. (org-test-with-temp-text "Paragraph."
  555. (should-error (org-down-element)))
  556. ;; Error when the element has no contents
  557. (org-test-with-temp-text "* Headline"
  558. (should-error (org-down-element)))
  559. ;; When at a plain-list, move to first item.
  560. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  561. (goto-line 2)
  562. (org-down-element)
  563. (should (looking-at " - Item 1.1")))
  564. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  565. (org-down-element)
  566. (should (looking-at " Item 1")))
  567. ;; When at a table, move to first row
  568. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  569. (org-down-element)
  570. (should (looking-at " a | b |")))
  571. ;; Otherwise, move inside the greater element.
  572. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  573. (org-down-element)
  574. (should (looking-at "Paragraph"))))
  575. (ert-deftest test-org/drag-element-backward ()
  576. "Test `org-drag-element-backward' specifications."
  577. ;; 1. Error when trying to move first element of buffer.
  578. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  579. (should-error (org-drag-element-backward)))
  580. ;; 2. Error when trying to swap nested elements.
  581. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  582. (forward-line)
  583. (should-error (org-drag-element-backward)))
  584. ;; 3. Error when trying to swap an headline element and
  585. ;; a non-headline element.
  586. (org-test-with-temp-text "Test.\n* Head 1"
  587. (forward-line)
  588. (should-error (org-drag-element-backward)))
  589. ;; 4. Otherwise, swap elements, preserving column and blank lines
  590. ;; between elements.
  591. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  592. (search-forward "graph")
  593. (org-drag-element-backward)
  594. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  595. (should (looking-at " 2")))
  596. ;; 5. Preserve visibility of elements and their contents.
  597. (org-test-with-temp-text "
  598. #+BEGIN_CENTER
  599. Text.
  600. #+END_CENTER
  601. - item 1
  602. #+BEGIN_QUOTE
  603. Text.
  604. #+END_QUOTE"
  605. (while (search-forward "BEGIN_" nil t) (org-cycle))
  606. (search-backward "- item 1")
  607. (org-drag-element-backward)
  608. (should
  609. (equal
  610. '((63 . 82) (26 . 48))
  611. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  612. (overlays-in (point-min) (point-max)))))))
  613. (ert-deftest test-org/drag-element-forward ()
  614. "Test `org-drag-element-forward' specifications."
  615. ;; 1. Error when trying to move first element of buffer.
  616. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  617. (goto-line 3)
  618. (should-error (org-drag-element-forward)))
  619. ;; 2. Error when trying to swap nested elements.
  620. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  621. (forward-line)
  622. (should-error (org-drag-element-forward)))
  623. ;; 3. Error when trying to swap a non-headline element and an
  624. ;; headline.
  625. (org-test-with-temp-text "Test.\n* Head 1"
  626. (should-error (org-drag-element-forward)))
  627. ;; 4. Otherwise, swap elements, preserving column and blank lines
  628. ;; between elements.
  629. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  630. (search-forward "graph")
  631. (org-drag-element-forward)
  632. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  633. (should (looking-at " 1")))
  634. ;; 5. Preserve visibility of elements and their contents.
  635. (org-test-with-temp-text "
  636. #+BEGIN_CENTER
  637. Text.
  638. #+END_CENTER
  639. - item 1
  640. #+BEGIN_QUOTE
  641. Text.
  642. #+END_QUOTE"
  643. (while (search-forward "BEGIN_" nil t) (org-cycle))
  644. (search-backward "#+BEGIN_CENTER")
  645. (org-drag-element-forward)
  646. (should
  647. (equal
  648. '((63 . 82) (26 . 48))
  649. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  650. (overlays-in (point-min) (point-max)))))))
  651. (provide 'test-org)
  652. ;;; test-org.el ends here