test-org.el 22 KB

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