test-org.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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/end-of-line ()
  364. "Test `org-end-of-line' specifications."
  365. ;; Standard test.
  366. (should
  367. (org-test-with-temp-text "Some text\nSome other text"
  368. (progn (org-end-of-line) (eolp))))
  369. ;; At an headline with special movement.
  370. (should
  371. (org-test-with-temp-text "* Headline :tag:"
  372. (let ((org-special-ctrl-a/e t))
  373. (and (progn (org-end-of-line) (looking-at " :tag:"))
  374. (progn (org-end-of-line) (eolp))
  375. (progn (org-end-of-line) (looking-at " :tag:"))))))
  376. ;; At an headline without special movement.
  377. (should
  378. (org-test-with-temp-text "* Headline :tag:"
  379. (let ((org-special-ctrl-a/e nil))
  380. (and (progn (org-end-of-line) (eolp))
  381. (progn (org-end-of-line) (eolp))))))
  382. ;; At an headline, with reversed movement.
  383. (should
  384. (org-test-with-temp-text "* Headline :tag:"
  385. (let ((org-special-ctrl-a/e 'reversed)
  386. (this-command last-command))
  387. (and (progn (org-end-of-line) (eolp))
  388. (progn (org-end-of-line) (looking-at " :tag:"))))))
  389. ;; At a block without hidden contents.
  390. (should
  391. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  392. (progn (org-end-of-line) (eolp))))
  393. ;; At a block with hidden contents.
  394. (should-not
  395. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  396. (progn (org-hide-block-toggle)
  397. (org-end-of-line)
  398. (eolp)))))
  399. (ert-deftest test-org/forward-element ()
  400. "Test `org-forward-element' specifications."
  401. ;; 1. At EOB: should error.
  402. (org-test-with-temp-text "Some text\n"
  403. (goto-char (point-max))
  404. (should-error (org-forward-element)))
  405. ;; 2. Standard move: expected to ignore blank lines.
  406. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  407. (org-forward-element)
  408. (should (looking-at "Second paragraph.")))
  409. ;; 3. Headline tests.
  410. (org-test-with-temp-text "
  411. * Head 1
  412. ** Head 1.1
  413. *** Head 1.1.1
  414. ** Head 1.2"
  415. ;; 3.1. At an headline beginning: move to next headline at the
  416. ;; same level.
  417. (goto-line 3)
  418. (org-forward-element)
  419. (should (looking-at "** Head 1.2"))
  420. ;; 3.2. At an headline beginning: move to parent headline if no
  421. ;; headline at the same level.
  422. (goto-line 3)
  423. (org-forward-element)
  424. (should (looking-at "** Head 1.2")))
  425. ;; 4. Greater element tests.
  426. (org-test-with-temp-text
  427. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  428. ;; 4.1. At a greater element: expected to skip contents.
  429. (org-forward-element)
  430. (should (looking-at "Outside."))
  431. ;; 4.2. At the end of greater element contents: expected to skip
  432. ;; to the end of the greater element.
  433. (goto-line 2)
  434. (org-forward-element)
  435. (should (looking-at "Outside.")))
  436. ;; 5. List tests.
  437. (org-test-with-temp-text "
  438. - item1
  439. - sub1
  440. - sub2
  441. - sub3
  442. Inner paragraph.
  443. - item2
  444. Outside."
  445. ;; 5.1. At list top point: expected to move to the element after
  446. ;; the list.
  447. (goto-line 2)
  448. (org-forward-element)
  449. (should (looking-at "Outside."))
  450. ;; 5.2. Special case: at the first line of a sub-list, but not at
  451. ;; beginning of line, move to next item.
  452. (goto-line 2)
  453. (forward-char)
  454. (org-forward-element)
  455. (should (looking-at "- item2"))
  456. (goto-line 4)
  457. (forward-char)
  458. (org-forward-element)
  459. (should (looking-at " - sub2"))
  460. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  461. (goto-line 4)
  462. (org-forward-element)
  463. (should (looking-at " Inner paragraph."))
  464. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  465. (goto-line 8)
  466. (org-forward-element)
  467. (should (looking-at " Inner paragraph."))
  468. ;; 5.5. At an item: expected to move to next item, if any.
  469. (goto-line 6)
  470. (org-forward-element)
  471. (should (looking-at " - sub3"))))
  472. (ert-deftest test-org/backward-element ()
  473. "Test `org-backward-element' specifications."
  474. ;; 1. Should error at BOB.
  475. (org-test-with-temp-text " \nParagraph."
  476. (should-error (org-backward-element)))
  477. ;; 2. Should move at BOB when called on the first element in buffer.
  478. (should
  479. (org-test-with-temp-text "\n#+TITLE: test"
  480. (progn (forward-line)
  481. (org-backward-element)
  482. (bobp))))
  483. ;; 3. Not at the beginning of an element: move at its beginning.
  484. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  485. (goto-line 3)
  486. (end-of-line)
  487. (org-backward-element)
  488. (should (looking-at "Paragraph2.")))
  489. ;; 4. Headline tests.
  490. (org-test-with-temp-text "
  491. * Head 1
  492. ** Head 1.1
  493. *** Head 1.1.1
  494. ** Head 1.2"
  495. ;; 4.1. At an headline beginning: move to previous headline at the
  496. ;; same level.
  497. (goto-line 5)
  498. (org-backward-element)
  499. (should (looking-at "** Head 1.1"))
  500. ;; 4.2. At an headline beginning: move to parent headline if no
  501. ;; headline at the same level.
  502. (goto-line 3)
  503. (org-backward-element)
  504. (should (looking-at "* Head 1"))
  505. ;; 4.3. At the first top-level headline: should error.
  506. (goto-line 2)
  507. (should-error (org-backward-element)))
  508. ;; 5. At beginning of first element inside a greater element:
  509. ;; expected to move to greater element's beginning.
  510. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  511. (goto-line 3)
  512. (org-backward-element)
  513. (should (looking-at "#\\+BEGIN_CENTER")))
  514. ;; 6. At the beginning of the first element in a section: should
  515. ;; move back to headline, if any.
  516. (should
  517. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  518. (progn (goto-char (point-max))
  519. (beginning-of-line)
  520. (org-backward-element)
  521. (org-at-heading-p))))
  522. ;; 7. List tests.
  523. (org-test-with-temp-text "
  524. - item1
  525. - sub1
  526. - sub2
  527. - sub3
  528. Inner paragraph.
  529. - item2
  530. Outside."
  531. ;; 7.1. At beginning of sub-list: expected to move to the
  532. ;; paragraph before it.
  533. (goto-line 4)
  534. (org-backward-element)
  535. (should (looking-at "item1"))
  536. ;; 7.2. At an item in a list: expected to move at previous item.
  537. (goto-line 8)
  538. (org-backward-element)
  539. (should (looking-at " - sub2"))
  540. (goto-line 12)
  541. (org-backward-element)
  542. (should (looking-at "- item1"))
  543. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  544. ;; beginning.
  545. (goto-line 10)
  546. (org-backward-element)
  547. (should (looking-at " - sub1"))
  548. (goto-line 15)
  549. (org-backward-element)
  550. (should (looking-at "- item1"))
  551. ;; 7.4. At blank-lines before list end: expected to move to top
  552. ;; item.
  553. (goto-line 14)
  554. (org-backward-element)
  555. (should (looking-at "- item1"))))
  556. (ert-deftest test-org/up-element ()
  557. "Test `org-up-element' specifications."
  558. ;; 1. At BOB or with no surrounding element: should error.
  559. (org-test-with-temp-text "Paragraph."
  560. (should-error (org-up-element)))
  561. (org-test-with-temp-text "* Head1\n* Head2"
  562. (goto-line 2)
  563. (should-error (org-up-element)))
  564. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  565. (goto-line 3)
  566. (should-error (org-up-element)))
  567. ;; 2. At an headline: move to parent headline.
  568. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  569. (goto-line 3)
  570. (org-up-element)
  571. (should (looking-at "\\* Head1")))
  572. ;; 3. Inside a greater element: move to greater element beginning.
  573. (org-test-with-temp-text
  574. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  575. (goto-line 3)
  576. (org-up-element)
  577. (should (looking-at "#\\+BEGIN_CENTER")))
  578. ;; 4. List tests.
  579. (org-test-with-temp-text "* Top
  580. - item1
  581. - sub1
  582. - sub2
  583. Paragraph within sub2.
  584. - item2"
  585. ;; 4.1. Within an item: move to the item beginning.
  586. (goto-line 8)
  587. (org-up-element)
  588. (should (looking-at " - sub2"))
  589. ;; 4.2. At an item in a sub-list: move to parent item.
  590. (goto-line 4)
  591. (org-up-element)
  592. (should (looking-at "- item1"))
  593. ;; 4.3. At an item in top list: move to beginning of whole list.
  594. (goto-line 10)
  595. (org-up-element)
  596. (should (looking-at "- item1"))
  597. ;; 4.4. Special case. At very top point: should move to parent of
  598. ;; list.
  599. (goto-line 2)
  600. (org-up-element)
  601. (should (looking-at "\\* Top"))))
  602. (ert-deftest test-org/down-element ()
  603. "Test `org-down-element' specifications."
  604. ;; Error when the element hasn't got a recursive type.
  605. (org-test-with-temp-text "Paragraph."
  606. (should-error (org-down-element)))
  607. ;; Error when the element has no contents
  608. (org-test-with-temp-text "* Headline"
  609. (should-error (org-down-element)))
  610. ;; When at a plain-list, move to first item.
  611. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  612. (goto-line 2)
  613. (org-down-element)
  614. (should (looking-at " - Item 1.1")))
  615. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  616. (org-down-element)
  617. (should (looking-at " Item 1")))
  618. ;; When at a table, move to first row
  619. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  620. (org-down-element)
  621. (should (looking-at " a | b |")))
  622. ;; Otherwise, move inside the greater element.
  623. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  624. (org-down-element)
  625. (should (looking-at "Paragraph"))))
  626. (ert-deftest test-org/drag-element-backward ()
  627. "Test `org-drag-element-backward' specifications."
  628. ;; 1. Error when trying to move first element of buffer.
  629. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  630. (should-error (org-drag-element-backward)))
  631. ;; 2. Error when trying to swap nested elements.
  632. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  633. (forward-line)
  634. (should-error (org-drag-element-backward)))
  635. ;; 3. Error when trying to swap an headline element and
  636. ;; a non-headline element.
  637. (org-test-with-temp-text "Test.\n* Head 1"
  638. (forward-line)
  639. (should-error (org-drag-element-backward)))
  640. ;; 4. Otherwise, swap elements, preserving column and blank lines
  641. ;; between elements.
  642. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  643. (search-forward "graph")
  644. (org-drag-element-backward)
  645. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  646. (should (looking-at " 2")))
  647. ;; 5. Preserve visibility of elements and their contents.
  648. (org-test-with-temp-text "
  649. #+BEGIN_CENTER
  650. Text.
  651. #+END_CENTER
  652. - item 1
  653. #+BEGIN_QUOTE
  654. Text.
  655. #+END_QUOTE"
  656. (while (search-forward "BEGIN_" nil t) (org-cycle))
  657. (search-backward "- item 1")
  658. (org-drag-element-backward)
  659. (should
  660. (equal
  661. '((63 . 82) (26 . 48))
  662. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  663. (overlays-in (point-min) (point-max)))))))
  664. (ert-deftest test-org/drag-element-forward ()
  665. "Test `org-drag-element-forward' specifications."
  666. ;; 1. Error when trying to move first element of buffer.
  667. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  668. (goto-line 3)
  669. (should-error (org-drag-element-forward)))
  670. ;; 2. Error when trying to swap nested elements.
  671. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  672. (forward-line)
  673. (should-error (org-drag-element-forward)))
  674. ;; 3. Error when trying to swap a non-headline element and an
  675. ;; headline.
  676. (org-test-with-temp-text "Test.\n* Head 1"
  677. (should-error (org-drag-element-forward)))
  678. ;; 4. Otherwise, swap elements, preserving column and blank lines
  679. ;; between elements.
  680. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  681. (search-forward "graph")
  682. (org-drag-element-forward)
  683. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  684. (should (looking-at " 1")))
  685. ;; 5. Preserve visibility of elements and their contents.
  686. (org-test-with-temp-text "
  687. #+BEGIN_CENTER
  688. Text.
  689. #+END_CENTER
  690. - item 1
  691. #+BEGIN_QUOTE
  692. Text.
  693. #+END_QUOTE"
  694. (while (search-forward "BEGIN_" nil t) (org-cycle))
  695. (search-backward "#+BEGIN_CENTER")
  696. (org-drag-element-forward)
  697. (should
  698. (equal
  699. '((63 . 82) (26 . 48))
  700. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  701. (overlays-in (point-min) (point-max)))))))
  702. (provide 'test-org)
  703. ;;; test-org.el ends here