test-org.el 23 KB

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