test-org.el 23 KB

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