test-org.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org-mode tests
  17. ;;; Code:
  18. (ert-deftest test-org/org-link-escape-ascii-character ()
  19. "Escape an ascii character."
  20. (should
  21. (string=
  22. "%5B"
  23. (org-link-escape "["))))
  24. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  25. "Escape an ascii control character."
  26. (should
  27. (string=
  28. "%09"
  29. (org-link-escape "\t"))))
  30. (ert-deftest test-org/org-link-escape-multibyte-character ()
  31. "Escape an unicode multibyte character."
  32. (should
  33. (string=
  34. "%E2%82%AC"
  35. (org-link-escape "€"))))
  36. (ert-deftest test-org/org-link-escape-custom-table ()
  37. "Escape string with custom character table."
  38. (should
  39. (string=
  40. "Foo%3A%42ar%0A"
  41. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  42. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  43. "Escape string with custom table merged with default table."
  44. (should
  45. (string=
  46. "%5BF%6F%6F%3A%42ar%0A%5D"
  47. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  48. (ert-deftest test-org/org-link-unescape-ascii-character ()
  49. "Unescape an ascii character."
  50. (should
  51. (string=
  52. "["
  53. (org-link-unescape "%5B"))))
  54. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  55. "Unescpae an ascii control character."
  56. (should
  57. (string=
  58. "\n"
  59. (org-link-unescape "%0A"))))
  60. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  61. "Unescape unicode multibyte character."
  62. (should
  63. (string=
  64. "€"
  65. (org-link-unescape "%E2%82%AC"))))
  66. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  67. "Unescape old style percent escaped character."
  68. (should
  69. (string=
  70. "àâçèéêîôùû"
  71. (decode-coding-string (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  72. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  73. "Escape and unscape a URL that includes an escaped char.
  74. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  75. (should
  76. (string=
  77. "http://some.host.com/form?&id=blah%2Bblah25"
  78. (org-link-unescape (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  79. (ert-deftest test-org/accumulated-properties-in-drawers ()
  80. "Ensure properties accumulate in subtree drawers."
  81. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  82. (org-babel-next-src-block)
  83. (should (equal '(2 1) (org-babel-execute-src-block)))))
  84. ;;; Date Analysis
  85. (ert-deftest test-org/org-read-date ()
  86. "Test `org-read-date' specifications."
  87. ;; Parse ISO date with abbreviated year and month.
  88. (should (equal "2012-03-29 16:40"
  89. (let ((org-time-was-given t))
  90. (org-read-date t nil "12-3-29 16:40"))))
  91. ;; Parse Europeans dates.
  92. (should (equal "2012-03-29 16:40"
  93. (let ((org-time-was-given t))
  94. (org-read-date t nil "29.03.2012 16:40"))))
  95. ;; Parse Europeans dates without year.
  96. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  97. (let ((org-time-was-given t))
  98. (org-read-date t nil "29.03. 16:40")))))
  99. ;;; Links
  100. ;;;; Fuzzy links
  101. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  102. ;; a target keyword (aka an invisible target: #+TARGET: text), to
  103. ;; a named element (#+name: text) and to headlines (* Text).
  104. (ert-deftest test-org/fuzzy-links ()
  105. "Test fuzzy links specifications."
  106. ;; 1. Fuzzy link goes in priority to a matching target.
  107. (org-test-with-temp-text
  108. "#+TARGET: Test\n#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n[[Test]]"
  109. (goto-line 6)
  110. (org-open-at-point)
  111. (should (looking-at "<<Test>>")))
  112. ;; 2. Fuzzy link should then go to a matching target keyword.
  113. (org-test-with-temp-text
  114. "#+NAME: Test\n|a|b|\n#+TARGET: Test\n* Test\n[[Test]]"
  115. (goto-line 5)
  116. (org-open-at-point)
  117. (should (looking-at "#\\+TARGET: Test")))
  118. ;; 3. Then fuzzy link points to an element with a given name.
  119. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n[[Test]]"
  120. (goto-line 5)
  121. (org-open-at-point)
  122. (should (looking-at "#\\+NAME: Test")))
  123. ;; 4. A target still lead to a matching headline otherwise.
  124. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n[[Head2]]"
  125. (goto-line 4)
  126. (org-open-at-point)
  127. (should (looking-at "\\* Head2")))
  128. ;; 5. With a leading star in link, enforce heading match.
  129. (org-test-with-temp-text "#+TARGET: Test\n* Test\n<<Test>>\n[[*Test]]"
  130. (goto-line 4)
  131. (org-open-at-point)
  132. (should (looking-at "\\* Test"))))
  133. ;;; Filling
  134. (ert-deftest test-org/fill-paragraph ()
  135. "Test `org-fill-paragraph' specifications."
  136. ;; At an Org table, align it.
  137. (org-test-with-temp-text "|a|"
  138. (org-fill-paragraph)
  139. (should (equal (buffer-string) "| a |\n")))
  140. ;; At a paragraph, preserve line breaks.
  141. (org-test-with-temp-text "some \\\\\nlong\ntext"
  142. (let ((fill-column 20))
  143. (org-fill-paragraph)
  144. (should (equal (buffer-string) "some \\\\\nlong text"))))
  145. ;; Correctly fill a paragraph when point is at its very end.
  146. (should
  147. (equal "A B"
  148. (org-test-with-temp-text "A\nB"
  149. (let ((fill-column 20))
  150. (goto-char (point-max))
  151. (org-fill-paragraph)
  152. (buffer-string)))))
  153. ;; Correctly fill the last paragraph of a greater element.
  154. (should
  155. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  156. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  157. (let ((fill-column 8))
  158. (forward-line)
  159. (end-of-line)
  160. (org-fill-paragraph)
  161. (buffer-string)))))
  162. ;; Correctly fill an element in a narrowed buffer.
  163. (should
  164. (equal "01234\n6"
  165. (org-test-with-temp-text "01234 6789"
  166. (let ((fill-column 5))
  167. (narrow-to-region 1 8)
  168. (org-fill-paragraph)
  169. (buffer-string)))))
  170. ;; Special case: Fill first paragraph when point is at an item or
  171. ;; a plain-list or a footnote reference.
  172. (should
  173. (equal "- A B"
  174. (org-test-with-temp-text "- A\n B"
  175. (let ((fill-column 20))
  176. (org-fill-paragraph)
  177. (buffer-string)))))
  178. (should
  179. (equal "[fn:1] A B"
  180. (org-test-with-temp-text "[fn:1] A\nB"
  181. (let ((fill-column 20))
  182. (org-fill-paragraph)
  183. (buffer-string)))))
  184. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  185. (let ((fill-column 20))
  186. (org-fill-paragraph)
  187. (should (equal (buffer-string)
  188. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  189. ;; Fill contents of `comment-block' elements.
  190. (should
  191. (equal
  192. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  193. (let ((fill-column 20))
  194. (forward-line)
  195. (org-fill-paragraph)
  196. (buffer-string)))
  197. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  198. ;; Fill `comment' elements.
  199. (should
  200. (equal " # A B"
  201. (org-test-with-temp-text " # A\n # B"
  202. (let ((fill-column 20))
  203. (org-fill-paragraph)
  204. (buffer-string)))))
  205. ;; Do nothing at affiliated keywords.
  206. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  207. (let ((fill-column 20))
  208. (org-fill-paragraph)
  209. (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))))
  210. (ert-deftest test-org/auto-fill-function ()
  211. "Test auto-filling features."
  212. ;; Auto fill paragraph.
  213. (should
  214. (equal "12345\n7890"
  215. (org-test-with-temp-text "12345 7890"
  216. (let ((fill-column 5))
  217. (end-of-line)
  218. (org-auto-fill-function)
  219. (buffer-string)))))
  220. ;; Auto fill first paragraph in an item.
  221. (should
  222. (equal "- 12345\n 7890"
  223. (org-test-with-temp-text "- 12345 7890"
  224. (let ((fill-column 7))
  225. (end-of-line)
  226. (org-auto-fill-function)
  227. (buffer-string)))))
  228. ;; Auto fill comments.
  229. (should
  230. (equal " # 12345\n # 7890"
  231. (org-test-with-temp-text " # 12345 7890"
  232. (let ((fill-column 10))
  233. (end-of-line)
  234. (org-auto-fill-function)
  235. (buffer-string)))))
  236. ;; A hash within a line isn't a comment.
  237. (should-not
  238. (equal "12345 # 7890\n# 1"
  239. (org-test-with-temp-text "12345 # 7890 1"
  240. (let ((fill-column 12))
  241. (end-of-line)
  242. (org-auto-fill-function)
  243. (buffer-string)))))
  244. ;; Correctly interpret empty prefix.
  245. (should-not
  246. (equal "# a\n# b\nRegular\n# paragraph"
  247. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  248. (let ((fill-column 12))
  249. (end-of-line 3)
  250. (org-auto-fill-function)
  251. (buffer-string)))))
  252. ;; Comment block: auto fill contents.
  253. (should
  254. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  255. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  256. (let ((fill-column 5))
  257. (forward-line)
  258. (end-of-line)
  259. (org-auto-fill-function)
  260. (buffer-string)))))
  261. (should
  262. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  263. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  264. (let ((fill-column 5))
  265. (forward-line)
  266. (end-of-line)
  267. (org-auto-fill-function)
  268. (buffer-string)))))
  269. ;; Do not fill if a new item could be created.
  270. (should-not
  271. (equal "12345\n- 90"
  272. (org-test-with-temp-text "12345 - 90"
  273. (let ((fill-column 5))
  274. (end-of-line)
  275. (org-auto-fill-function)
  276. (buffer-string)))))
  277. ;; Do not fill if a line break could be introduced.
  278. (should-not
  279. (equal "123\\\\\n7890"
  280. (org-test-with-temp-text "123\\\\ 7890"
  281. (let ((fill-column 6))
  282. (end-of-line)
  283. (org-auto-fill-function)
  284. (buffer-string)))))
  285. ;; Do not fill affiliated keywords.
  286. (should-not
  287. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  288. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  289. (let ((fill-column 20))
  290. (end-of-line)
  291. (org-auto-fill-function)
  292. (buffer-string))))))
  293. ;;; Comments
  294. (ert-deftest test-org/comment-dwim ()
  295. "Test `comment-dwim' behaviour in an Org buffer."
  296. ;; No region selected, no comment on current line and line not
  297. ;; empty: insert comment on line above.
  298. (should
  299. (equal "# \nComment"
  300. (org-test-with-temp-text "Comment"
  301. (progn (call-interactively 'comment-dwim)
  302. (buffer-string)))))
  303. ;; No region selected, no comment on current line and line empty:
  304. ;; insert comment on this line.
  305. (should
  306. (equal "# \nParagraph"
  307. (org-test-with-temp-text "\nParagraph"
  308. (progn (call-interactively 'comment-dwim)
  309. (buffer-string)))))
  310. ;; No region selected, and a comment on this line: indent it.
  311. (should
  312. (equal "* Headline\n # Comment"
  313. (org-test-with-temp-text "* Headline\n# Comment"
  314. (progn (forward-line)
  315. (let ((org-adapt-indentation t))
  316. (call-interactively 'comment-dwim))
  317. (buffer-string)))))
  318. ;; Also recognize single # at column 0 as comments.
  319. (should
  320. (equal "# Comment"
  321. (org-test-with-temp-text "# Comment"
  322. (progn (forward-line)
  323. (call-interactively 'comment-dwim)
  324. (buffer-string)))))
  325. ;; Region selected and only comments and blank lines within it:
  326. ;; un-comment all commented lines.
  327. (should
  328. (equal "Comment 1\n\nComment 2"
  329. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  330. (progn
  331. (transient-mark-mode 1)
  332. (push-mark (point) t t)
  333. (goto-char (point-max))
  334. (call-interactively 'comment-dwim)
  335. (buffer-string)))))
  336. ;; Region selected without comments: comment all non-blank lines.
  337. (should
  338. (equal "# Comment 1\n\n# Comment 2"
  339. (org-test-with-temp-text "Comment 1\n\nComment 2"
  340. (progn
  341. (transient-mark-mode 1)
  342. (push-mark (point) t t)
  343. (goto-char (point-max))
  344. (call-interactively 'comment-dwim)
  345. (buffer-string)))))
  346. ;; In front of a keyword without region, insert a new comment.
  347. (should
  348. (equal "# \n#+KEYWORD: value"
  349. (org-test-with-temp-text "#+KEYWORD: value"
  350. (progn (call-interactively 'comment-dwim)
  351. (buffer-string))))))
  352. ;;; Mark region
  353. (ert-deftest test-org/mark-subtree ()
  354. "Test `org-mark-subtree' specifications."
  355. ;; Error when point is before first headline.
  356. (should-error
  357. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  358. (progn (transient-mark-mode 1)
  359. (org-mark-subtree))))
  360. ;; Without argument, mark current subtree.
  361. (should
  362. (equal
  363. '(12 32)
  364. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  365. (progn (transient-mark-mode 1)
  366. (forward-line 2)
  367. (org-mark-subtree)
  368. (list (region-beginning) (region-end))))))
  369. ;; With an argument, move ARG up.
  370. (should
  371. (equal
  372. '(1 32)
  373. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  374. (progn (transient-mark-mode 1)
  375. (forward-line 2)
  376. (org-mark-subtree 1)
  377. (list (region-beginning) (region-end))))))
  378. ;; Do not get fooled with inlinetasks.
  379. (when (featurep 'org-inlinetask)
  380. (should
  381. (= 1
  382. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  383. (progn (transient-mark-mode 1)
  384. (forward-line 1)
  385. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  386. (region-beginning)))))))
  387. ;; Navigation
  388. (ert-deftest test-org/beginning-of-line ()
  389. "Test `org-beginning-of-line' specifications."
  390. ;; Standard test.
  391. (should
  392. (org-test-with-temp-text "Some text\nSome other text"
  393. (progn (org-beginning-of-line) (bolp))))
  394. ;; Standard test with `visual-line-mode'.
  395. (should-not
  396. (org-test-with-temp-text "A long line of text\nSome other text"
  397. (progn (visual-line-mode)
  398. (forward-char 2)
  399. (dotimes (i 1000) (insert "very "))
  400. (org-beginning-of-line)
  401. (bolp))))
  402. ;; At an headline with special movement.
  403. (should
  404. (org-test-with-temp-text "* TODO Headline"
  405. (let ((org-special-ctrl-a/e t))
  406. (org-end-of-line)
  407. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  408. (progn (org-beginning-of-line) (bolp))
  409. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  410. (ert-deftest test-org/end-of-line ()
  411. "Test `org-end-of-line' specifications."
  412. ;; Standard test.
  413. (should
  414. (org-test-with-temp-text "Some text\nSome other text"
  415. (progn (org-end-of-line) (eolp))))
  416. ;; Standard test with `visual-line-mode'.
  417. (should-not
  418. (org-test-with-temp-text "A long line of text\nSome other text"
  419. (progn (visual-line-mode)
  420. (forward-char 2)
  421. (dotimes (i 1000) (insert "very "))
  422. (goto-char (point-min))
  423. (org-end-of-line)
  424. (eolp))))
  425. ;; At an headline with special movement.
  426. (should
  427. (org-test-with-temp-text "* Headline1 :tag:\n"
  428. (let ((org-special-ctrl-a/e t))
  429. (and (progn (org-end-of-line) (looking-at " :tag:"))
  430. (progn (org-end-of-line) (eolp))
  431. (progn (org-end-of-line) (looking-at " :tag:"))))))
  432. ;; At an headline without special movement.
  433. (should
  434. (org-test-with-temp-text "* Headline2 :tag:\n"
  435. (let ((org-special-ctrl-a/e nil))
  436. (and (progn (org-end-of-line) (eolp))
  437. (progn (org-end-of-line) (eolp))))))
  438. ;; At an headline, with reversed movement.
  439. (should
  440. (org-test-with-temp-text "* Headline3 :tag:\n"
  441. (let ((org-special-ctrl-a/e 'reversed)
  442. (this-command last-command))
  443. (and (progn (org-end-of-line) (eolp))
  444. (progn (org-end-of-line) (looking-at " :tag:"))))))
  445. ;; At a block without hidden contents.
  446. (should
  447. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  448. (progn (org-end-of-line) (eolp))))
  449. ;; At a block with hidden contents.
  450. (should-not
  451. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  452. (let ((org-special-ctrl-a/e t))
  453. (org-hide-block-toggle)
  454. (org-end-of-line)
  455. (eobp)))))
  456. (ert-deftest test-org/forward-element ()
  457. "Test `org-forward-element' specifications."
  458. ;; 1. At EOB: should error.
  459. (org-test-with-temp-text "Some text\n"
  460. (goto-char (point-max))
  461. (should-error (org-forward-element)))
  462. ;; 2. Standard move: expected to ignore blank lines.
  463. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  464. (org-forward-element)
  465. (should (looking-at "Second paragraph.")))
  466. ;; 3. Headline tests.
  467. (org-test-with-temp-text "
  468. * Head 1
  469. ** Head 1.1
  470. *** Head 1.1.1
  471. ** Head 1.2"
  472. ;; 3.1. At an headline beginning: move to next headline at the
  473. ;; same level.
  474. (goto-line 3)
  475. (org-forward-element)
  476. (should (looking-at "** Head 1.2"))
  477. ;; 3.2. At an headline beginning: move to parent headline if no
  478. ;; headline at the same level.
  479. (goto-line 3)
  480. (org-forward-element)
  481. (should (looking-at "** Head 1.2")))
  482. ;; 4. Greater element tests.
  483. (org-test-with-temp-text
  484. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  485. ;; 4.1. At a greater element: expected to skip contents.
  486. (org-forward-element)
  487. (should (looking-at "Outside."))
  488. ;; 4.2. At the end of greater element contents: expected to skip
  489. ;; to the end of the greater element.
  490. (goto-line 2)
  491. (org-forward-element)
  492. (should (looking-at "Outside.")))
  493. ;; 5. List tests.
  494. (org-test-with-temp-text "
  495. - item1
  496. - sub1
  497. - sub2
  498. - sub3
  499. Inner paragraph.
  500. - item2
  501. Outside."
  502. ;; 5.1. At list top point: expected to move to the element after
  503. ;; the list.
  504. (goto-line 2)
  505. (org-forward-element)
  506. (should (looking-at "Outside."))
  507. ;; 5.2. Special case: at the first line of a sub-list, but not at
  508. ;; beginning of line, move to next item.
  509. (goto-line 2)
  510. (forward-char)
  511. (org-forward-element)
  512. (should (looking-at "- item2"))
  513. (goto-line 4)
  514. (forward-char)
  515. (org-forward-element)
  516. (should (looking-at " - sub2"))
  517. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  518. (goto-line 4)
  519. (org-forward-element)
  520. (should (looking-at " Inner paragraph."))
  521. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  522. (goto-line 8)
  523. (org-forward-element)
  524. (should (looking-at " Inner paragraph."))
  525. ;; 5.5. At an item: expected to move to next item, if any.
  526. (goto-line 6)
  527. (org-forward-element)
  528. (should (looking-at " - sub3"))))
  529. (ert-deftest test-org/backward-element ()
  530. "Test `org-backward-element' specifications."
  531. ;; 1. Should error at BOB.
  532. (org-test-with-temp-text " \nParagraph."
  533. (should-error (org-backward-element)))
  534. ;; 2. Should move at BOB when called on the first element in buffer.
  535. (should
  536. (org-test-with-temp-text "\n#+TITLE: test"
  537. (progn (forward-line)
  538. (org-backward-element)
  539. (bobp))))
  540. ;; 3. Not at the beginning of an element: move at its beginning.
  541. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  542. (goto-line 3)
  543. (end-of-line)
  544. (org-backward-element)
  545. (should (looking-at "Paragraph2.")))
  546. ;; 4. Headline tests.
  547. (org-test-with-temp-text "
  548. * Head 1
  549. ** Head 1.1
  550. *** Head 1.1.1
  551. ** Head 1.2"
  552. ;; 4.1. At an headline beginning: move to previous headline at the
  553. ;; same level.
  554. (goto-line 5)
  555. (org-backward-element)
  556. (should (looking-at "** Head 1.1"))
  557. ;; 4.2. At an headline beginning: move to parent headline if no
  558. ;; headline at the same level.
  559. (goto-line 3)
  560. (org-backward-element)
  561. (should (looking-at "* Head 1"))
  562. ;; 4.3. At the first top-level headline: should error.
  563. (goto-line 2)
  564. (should-error (org-backward-element)))
  565. ;; 5. At beginning of first element inside a greater element:
  566. ;; expected to move to greater element's beginning.
  567. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  568. (goto-line 3)
  569. (org-backward-element)
  570. (should (looking-at "#\\+BEGIN_CENTER")))
  571. ;; 6. At the beginning of the first element in a section: should
  572. ;; move back to headline, if any.
  573. (should
  574. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  575. (progn (goto-char (point-max))
  576. (beginning-of-line)
  577. (org-backward-element)
  578. (org-at-heading-p))))
  579. ;; 7. List tests.
  580. (org-test-with-temp-text "
  581. - item1
  582. - sub1
  583. - sub2
  584. - sub3
  585. Inner paragraph.
  586. - item2
  587. Outside."
  588. ;; 7.1. At beginning of sub-list: expected to move to the
  589. ;; paragraph before it.
  590. (goto-line 4)
  591. (org-backward-element)
  592. (should (looking-at "item1"))
  593. ;; 7.2. At an item in a list: expected to move at previous item.
  594. (goto-line 8)
  595. (org-backward-element)
  596. (should (looking-at " - sub2"))
  597. (goto-line 12)
  598. (org-backward-element)
  599. (should (looking-at "- item1"))
  600. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  601. ;; beginning.
  602. (goto-line 10)
  603. (org-backward-element)
  604. (should (looking-at " - sub1"))
  605. (goto-line 15)
  606. (org-backward-element)
  607. (should (looking-at "- item1"))
  608. ;; 7.4. At blank-lines before list end: expected to move to top
  609. ;; item.
  610. (goto-line 14)
  611. (org-backward-element)
  612. (should (looking-at "- item1"))))
  613. (ert-deftest test-org/up-element ()
  614. "Test `org-up-element' specifications."
  615. ;; 1. At BOB or with no surrounding element: should error.
  616. (org-test-with-temp-text "Paragraph."
  617. (should-error (org-up-element)))
  618. (org-test-with-temp-text "* Head1\n* Head2"
  619. (goto-line 2)
  620. (should-error (org-up-element)))
  621. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  622. (goto-line 3)
  623. (should-error (org-up-element)))
  624. ;; 2. At an headline: move to parent headline.
  625. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  626. (goto-line 3)
  627. (org-up-element)
  628. (should (looking-at "\\* Head1")))
  629. ;; 3. Inside a greater element: move to greater element beginning.
  630. (org-test-with-temp-text
  631. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  632. (goto-line 3)
  633. (org-up-element)
  634. (should (looking-at "#\\+BEGIN_CENTER")))
  635. ;; 4. List tests.
  636. (org-test-with-temp-text "* Top
  637. - item1
  638. - sub1
  639. - sub2
  640. Paragraph within sub2.
  641. - item2"
  642. ;; 4.1. Within an item: move to the item beginning.
  643. (goto-line 8)
  644. (org-up-element)
  645. (should (looking-at " - sub2"))
  646. ;; 4.2. At an item in a sub-list: move to parent item.
  647. (goto-line 4)
  648. (org-up-element)
  649. (should (looking-at "- item1"))
  650. ;; 4.3. At an item in top list: move to beginning of whole list.
  651. (goto-line 10)
  652. (org-up-element)
  653. (should (looking-at "- item1"))
  654. ;; 4.4. Special case. At very top point: should move to parent of
  655. ;; list.
  656. (goto-line 2)
  657. (org-up-element)
  658. (should (looking-at "\\* Top"))))
  659. (ert-deftest test-org/down-element ()
  660. "Test `org-down-element' specifications."
  661. ;; Error when the element hasn't got a recursive type.
  662. (org-test-with-temp-text "Paragraph."
  663. (should-error (org-down-element)))
  664. ;; Error when the element has no contents
  665. (org-test-with-temp-text "* Headline"
  666. (should-error (org-down-element)))
  667. ;; When at a plain-list, move to first item.
  668. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  669. (goto-line 2)
  670. (org-down-element)
  671. (should (looking-at " - Item 1.1")))
  672. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  673. (org-down-element)
  674. (should (looking-at " Item 1")))
  675. ;; When at a table, move to first row
  676. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  677. (org-down-element)
  678. (should (looking-at " a | b |")))
  679. ;; Otherwise, move inside the greater element.
  680. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  681. (org-down-element)
  682. (should (looking-at "Paragraph"))))
  683. (ert-deftest test-org/drag-element-backward ()
  684. "Test `org-drag-element-backward' specifications."
  685. ;; 1. Error when trying to move first element of buffer.
  686. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  687. (should-error (org-drag-element-backward)))
  688. ;; 2. Error when trying to swap nested elements.
  689. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  690. (forward-line)
  691. (should-error (org-drag-element-backward)))
  692. ;; 3. Error when trying to swap an headline element and
  693. ;; a non-headline element.
  694. (org-test-with-temp-text "Test.\n* Head 1"
  695. (forward-line)
  696. (should-error (org-drag-element-backward)))
  697. ;; 4. Otherwise, swap elements, preserving column and blank lines
  698. ;; between elements.
  699. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  700. (search-forward "graph")
  701. (org-drag-element-backward)
  702. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  703. (should (looking-at " 2")))
  704. ;; 5. Preserve visibility of elements and their contents.
  705. (org-test-with-temp-text "
  706. #+BEGIN_CENTER
  707. Text.
  708. #+END_CENTER
  709. - item 1
  710. #+BEGIN_QUOTE
  711. Text.
  712. #+END_QUOTE"
  713. (while (search-forward "BEGIN_" nil t) (org-cycle))
  714. (search-backward "- item 1")
  715. (org-drag-element-backward)
  716. (should
  717. (equal
  718. '((63 . 82) (26 . 48))
  719. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  720. (overlays-in (point-min) (point-max)))))))
  721. (ert-deftest test-org/drag-element-forward ()
  722. "Test `org-drag-element-forward' specifications."
  723. ;; 1. Error when trying to move first element of buffer.
  724. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  725. (goto-line 3)
  726. (should-error (org-drag-element-forward)))
  727. ;; 2. Error when trying to swap nested elements.
  728. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  729. (forward-line)
  730. (should-error (org-drag-element-forward)))
  731. ;; 3. Error when trying to swap a non-headline element and an
  732. ;; headline.
  733. (org-test-with-temp-text "Test.\n* Head 1"
  734. (should-error (org-drag-element-forward)))
  735. ;; 4. Otherwise, swap elements, preserving column and blank lines
  736. ;; between elements.
  737. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  738. (search-forward "graph")
  739. (org-drag-element-forward)
  740. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  741. (should (looking-at " 1")))
  742. ;; 5. Preserve visibility of elements and their contents.
  743. (org-test-with-temp-text "
  744. #+BEGIN_CENTER
  745. Text.
  746. #+END_CENTER
  747. - item 1
  748. #+BEGIN_QUOTE
  749. Text.
  750. #+END_QUOTE"
  751. (while (search-forward "BEGIN_" nil t) (org-cycle))
  752. (search-backward "#+BEGIN_CENTER")
  753. (org-drag-element-forward)
  754. (should
  755. (equal
  756. '((63 . 82) (26 . 48))
  757. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  758. (overlays-in (point-min) (point-max)))))))
  759. (provide 'test-org)
  760. ;;; test-org.el ends here