test-org.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. ;; A hash within a line isn't a comment.
  228. (should-not
  229. (equal "12345 # 7890\n# 1"
  230. (org-test-with-temp-text "12345 # 7890 1"
  231. (let ((fill-column 12))
  232. (end-of-line)
  233. (org-auto-fill-function)
  234. (buffer-string)))))
  235. ;; Correctly interpret empty prefix.
  236. (should-not
  237. (equal "# a\n# b\nRegular\n# paragraph"
  238. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  239. (let ((fill-column 12))
  240. (end-of-line 3)
  241. (org-auto-fill-function)
  242. (buffer-string)))))
  243. ;; Comment block: auto fill contents.
  244. (should
  245. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  246. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  247. (let ((fill-column 5))
  248. (forward-line)
  249. (end-of-line)
  250. (org-auto-fill-function)
  251. (buffer-string)))))
  252. (should
  253. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  254. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  255. (let ((fill-column 5))
  256. (forward-line)
  257. (end-of-line)
  258. (org-auto-fill-function)
  259. (buffer-string)))))
  260. ;; Do not fill if a new item could be created.
  261. (should-not
  262. (equal "12345\n- 90"
  263. (org-test-with-temp-text "12345 - 90"
  264. (let ((fill-column 5))
  265. (end-of-line)
  266. (org-auto-fill-function)
  267. (buffer-string)))))
  268. ;; Do not fill if a line break could be introduced.
  269. (should-not
  270. (equal "123\\\\\n7890"
  271. (org-test-with-temp-text "123\\\\ 7890"
  272. (let ((fill-column 6))
  273. (end-of-line)
  274. (org-auto-fill-function)
  275. (buffer-string)))))
  276. ;; Do not fill affiliated keywords.
  277. (should-not
  278. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  279. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  280. (let ((fill-column 20))
  281. (end-of-line)
  282. (org-auto-fill-function)
  283. (buffer-string))))))
  284. ;;; Comments
  285. (ert-deftest test-org/comment-dwim ()
  286. "Test `comment-dwim' behaviour in an Org buffer."
  287. ;; No region selected, no comment on current line and line not
  288. ;; empty: insert comment on line above.
  289. (should
  290. (equal "# \nComment"
  291. (org-test-with-temp-text "Comment"
  292. (progn (call-interactively 'comment-dwim)
  293. (buffer-string)))))
  294. ;; No region selected, no comment on current line and line empty:
  295. ;; insert comment on this line.
  296. (should
  297. (equal "# \nParagraph"
  298. (org-test-with-temp-text "\nParagraph"
  299. (progn (call-interactively 'comment-dwim)
  300. (buffer-string)))))
  301. ;; No region selected, and a comment on this line: indent it.
  302. (should
  303. (equal "* Headline\n # Comment"
  304. (org-test-with-temp-text "* Headline\n# Comment"
  305. (progn (forward-line)
  306. (let ((org-adapt-indentation t))
  307. (call-interactively 'comment-dwim))
  308. (buffer-string)))))
  309. ;; Also recognize single # at column 0 as comments.
  310. (should
  311. (equal "# Comment"
  312. (org-test-with-temp-text "# Comment"
  313. (progn (forward-line)
  314. (call-interactively 'comment-dwim)
  315. (buffer-string)))))
  316. ;; Region selected and only comments and blank lines within it:
  317. ;; un-comment all commented lines.
  318. (should
  319. (equal "Comment 1\n\nComment 2"
  320. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  321. (progn
  322. (transient-mark-mode 1)
  323. (push-mark (point) t t)
  324. (goto-char (point-max))
  325. (call-interactively 'comment-dwim)
  326. (buffer-string)))))
  327. ;; Region selected without comments: comment all non-blank lines.
  328. (should
  329. (equal "# Comment 1\n\n# Comment 2"
  330. (org-test-with-temp-text "Comment 1\n\nComment 2"
  331. (progn
  332. (transient-mark-mode 1)
  333. (push-mark (point) t t)
  334. (goto-char (point-max))
  335. (call-interactively 'comment-dwim)
  336. (buffer-string)))))
  337. ;; In front of a keyword without region, insert a new comment.
  338. (should
  339. (equal "# \n#+KEYWORD: value"
  340. (org-test-with-temp-text "#+KEYWORD: value"
  341. (progn (call-interactively 'comment-dwim)
  342. (buffer-string))))))
  343. ;;; Mark region
  344. (ert-deftest test-org/mark-subtree ()
  345. "Test `org-mark-subtree' specifications."
  346. ;; Error when point is before first headline.
  347. (should-error
  348. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  349. (progn (transient-mark-mode 1)
  350. (org-mark-subtree))))
  351. ;; Without argument, mark current subtree.
  352. (should
  353. (equal
  354. '(12 32)
  355. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  356. (progn (transient-mark-mode 1)
  357. (forward-line 2)
  358. (org-mark-subtree)
  359. (list (region-beginning) (region-end))))))
  360. ;; With an argument, move ARG up.
  361. (should
  362. (equal
  363. '(1 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 1)
  368. (list (region-beginning) (region-end))))))
  369. ;; Do not get fooled with inlinetasks.
  370. (when (featurep 'org-inlinetask)
  371. (should
  372. (= 1
  373. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  374. (progn (transient-mark-mode 1)
  375. (forward-line 1)
  376. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  377. (region-beginning)))))))
  378. ;; Navigation
  379. (ert-deftest test-org/end-of-line ()
  380. "Test `org-end-of-line' specifications."
  381. ;; Standard test.
  382. (should
  383. (org-test-with-temp-text "Some text\nSome other text"
  384. (progn (org-end-of-line) (eolp))))
  385. ;; At an headline with special movement.
  386. (should
  387. (org-test-with-temp-text "* Headline :tag:"
  388. (let ((org-special-ctrl-a/e t))
  389. (and (progn (org-end-of-line) (looking-at " :tag:"))
  390. (progn (org-end-of-line) (eolp))
  391. (progn (org-end-of-line) (looking-at " :tag:"))))))
  392. ;; At an headline without special movement.
  393. (should
  394. (org-test-with-temp-text "* Headline :tag:"
  395. (let ((org-special-ctrl-a/e nil))
  396. (and (progn (org-end-of-line) (eolp))
  397. (progn (org-end-of-line) (eolp))))))
  398. ;; At an headline, with reversed movement.
  399. (should
  400. (org-test-with-temp-text "* Headline :tag:"
  401. (let ((org-special-ctrl-a/e 'reversed)
  402. (this-command last-command))
  403. (and (progn (org-end-of-line) (eolp))
  404. (progn (org-end-of-line) (looking-at " :tag:"))))))
  405. ;; At a block without hidden contents.
  406. (should
  407. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  408. (progn (org-end-of-line) (eolp))))
  409. ;; At a block with hidden contents.
  410. (should-not
  411. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  412. (progn (org-hide-block-toggle)
  413. (org-end-of-line)
  414. (eolp)))))
  415. (ert-deftest test-org/forward-element ()
  416. "Test `org-forward-element' specifications."
  417. ;; 1. At EOB: should error.
  418. (org-test-with-temp-text "Some text\n"
  419. (goto-char (point-max))
  420. (should-error (org-forward-element)))
  421. ;; 2. Standard move: expected to ignore blank lines.
  422. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  423. (org-forward-element)
  424. (should (looking-at "Second paragraph.")))
  425. ;; 3. Headline tests.
  426. (org-test-with-temp-text "
  427. * Head 1
  428. ** Head 1.1
  429. *** Head 1.1.1
  430. ** Head 1.2"
  431. ;; 3.1. At an headline beginning: move to next headline at the
  432. ;; same level.
  433. (goto-line 3)
  434. (org-forward-element)
  435. (should (looking-at "** Head 1.2"))
  436. ;; 3.2. At an headline beginning: move to parent headline if no
  437. ;; headline at the same level.
  438. (goto-line 3)
  439. (org-forward-element)
  440. (should (looking-at "** Head 1.2")))
  441. ;; 4. Greater element tests.
  442. (org-test-with-temp-text
  443. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  444. ;; 4.1. At a greater element: expected to skip contents.
  445. (org-forward-element)
  446. (should (looking-at "Outside."))
  447. ;; 4.2. At the end of greater element contents: expected to skip
  448. ;; to the end of the greater element.
  449. (goto-line 2)
  450. (org-forward-element)
  451. (should (looking-at "Outside.")))
  452. ;; 5. List tests.
  453. (org-test-with-temp-text "
  454. - item1
  455. - sub1
  456. - sub2
  457. - sub3
  458. Inner paragraph.
  459. - item2
  460. Outside."
  461. ;; 5.1. At list top point: expected to move to the element after
  462. ;; the list.
  463. (goto-line 2)
  464. (org-forward-element)
  465. (should (looking-at "Outside."))
  466. ;; 5.2. Special case: at the first line of a sub-list, but not at
  467. ;; beginning of line, move to next item.
  468. (goto-line 2)
  469. (forward-char)
  470. (org-forward-element)
  471. (should (looking-at "- item2"))
  472. (goto-line 4)
  473. (forward-char)
  474. (org-forward-element)
  475. (should (looking-at " - sub2"))
  476. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  477. (goto-line 4)
  478. (org-forward-element)
  479. (should (looking-at " Inner paragraph."))
  480. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  481. (goto-line 8)
  482. (org-forward-element)
  483. (should (looking-at " Inner paragraph."))
  484. ;; 5.5. At an item: expected to move to next item, if any.
  485. (goto-line 6)
  486. (org-forward-element)
  487. (should (looking-at " - sub3"))))
  488. (ert-deftest test-org/backward-element ()
  489. "Test `org-backward-element' specifications."
  490. ;; 1. Should error at BOB.
  491. (org-test-with-temp-text " \nParagraph."
  492. (should-error (org-backward-element)))
  493. ;; 2. Should move at BOB when called on the first element in buffer.
  494. (should
  495. (org-test-with-temp-text "\n#+TITLE: test"
  496. (progn (forward-line)
  497. (org-backward-element)
  498. (bobp))))
  499. ;; 3. Not at the beginning of an element: move at its beginning.
  500. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  501. (goto-line 3)
  502. (end-of-line)
  503. (org-backward-element)
  504. (should (looking-at "Paragraph2.")))
  505. ;; 4. Headline tests.
  506. (org-test-with-temp-text "
  507. * Head 1
  508. ** Head 1.1
  509. *** Head 1.1.1
  510. ** Head 1.2"
  511. ;; 4.1. At an headline beginning: move to previous headline at the
  512. ;; same level.
  513. (goto-line 5)
  514. (org-backward-element)
  515. (should (looking-at "** Head 1.1"))
  516. ;; 4.2. At an headline beginning: move to parent headline if no
  517. ;; headline at the same level.
  518. (goto-line 3)
  519. (org-backward-element)
  520. (should (looking-at "* Head 1"))
  521. ;; 4.3. At the first top-level headline: should error.
  522. (goto-line 2)
  523. (should-error (org-backward-element)))
  524. ;; 5. At beginning of first element inside a greater element:
  525. ;; expected to move to greater element's beginning.
  526. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  527. (goto-line 3)
  528. (org-backward-element)
  529. (should (looking-at "#\\+BEGIN_CENTER")))
  530. ;; 6. At the beginning of the first element in a section: should
  531. ;; move back to headline, if any.
  532. (should
  533. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  534. (progn (goto-char (point-max))
  535. (beginning-of-line)
  536. (org-backward-element)
  537. (org-at-heading-p))))
  538. ;; 7. List tests.
  539. (org-test-with-temp-text "
  540. - item1
  541. - sub1
  542. - sub2
  543. - sub3
  544. Inner paragraph.
  545. - item2
  546. Outside."
  547. ;; 7.1. At beginning of sub-list: expected to move to the
  548. ;; paragraph before it.
  549. (goto-line 4)
  550. (org-backward-element)
  551. (should (looking-at "item1"))
  552. ;; 7.2. At an item in a list: expected to move at previous item.
  553. (goto-line 8)
  554. (org-backward-element)
  555. (should (looking-at " - sub2"))
  556. (goto-line 12)
  557. (org-backward-element)
  558. (should (looking-at "- item1"))
  559. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  560. ;; beginning.
  561. (goto-line 10)
  562. (org-backward-element)
  563. (should (looking-at " - sub1"))
  564. (goto-line 15)
  565. (org-backward-element)
  566. (should (looking-at "- item1"))
  567. ;; 7.4. At blank-lines before list end: expected to move to top
  568. ;; item.
  569. (goto-line 14)
  570. (org-backward-element)
  571. (should (looking-at "- item1"))))
  572. (ert-deftest test-org/up-element ()
  573. "Test `org-up-element' specifications."
  574. ;; 1. At BOB or with no surrounding element: should error.
  575. (org-test-with-temp-text "Paragraph."
  576. (should-error (org-up-element)))
  577. (org-test-with-temp-text "* Head1\n* Head2"
  578. (goto-line 2)
  579. (should-error (org-up-element)))
  580. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  581. (goto-line 3)
  582. (should-error (org-up-element)))
  583. ;; 2. At an headline: move to parent headline.
  584. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  585. (goto-line 3)
  586. (org-up-element)
  587. (should (looking-at "\\* Head1")))
  588. ;; 3. Inside a greater element: move to greater element beginning.
  589. (org-test-with-temp-text
  590. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  591. (goto-line 3)
  592. (org-up-element)
  593. (should (looking-at "#\\+BEGIN_CENTER")))
  594. ;; 4. List tests.
  595. (org-test-with-temp-text "* Top
  596. - item1
  597. - sub1
  598. - sub2
  599. Paragraph within sub2.
  600. - item2"
  601. ;; 4.1. Within an item: move to the item beginning.
  602. (goto-line 8)
  603. (org-up-element)
  604. (should (looking-at " - sub2"))
  605. ;; 4.2. At an item in a sub-list: move to parent item.
  606. (goto-line 4)
  607. (org-up-element)
  608. (should (looking-at "- item1"))
  609. ;; 4.3. At an item in top list: move to beginning of whole list.
  610. (goto-line 10)
  611. (org-up-element)
  612. (should (looking-at "- item1"))
  613. ;; 4.4. Special case. At very top point: should move to parent of
  614. ;; list.
  615. (goto-line 2)
  616. (org-up-element)
  617. (should (looking-at "\\* Top"))))
  618. (ert-deftest test-org/down-element ()
  619. "Test `org-down-element' specifications."
  620. ;; Error when the element hasn't got a recursive type.
  621. (org-test-with-temp-text "Paragraph."
  622. (should-error (org-down-element)))
  623. ;; Error when the element has no contents
  624. (org-test-with-temp-text "* Headline"
  625. (should-error (org-down-element)))
  626. ;; When at a plain-list, move to first item.
  627. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  628. (goto-line 2)
  629. (org-down-element)
  630. (should (looking-at " - Item 1.1")))
  631. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  632. (org-down-element)
  633. (should (looking-at " Item 1")))
  634. ;; When at a table, move to first row
  635. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  636. (org-down-element)
  637. (should (looking-at " a | b |")))
  638. ;; Otherwise, move inside the greater element.
  639. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  640. (org-down-element)
  641. (should (looking-at "Paragraph"))))
  642. (ert-deftest test-org/drag-element-backward ()
  643. "Test `org-drag-element-backward' specifications."
  644. ;; 1. Error when trying to move first element of buffer.
  645. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  646. (should-error (org-drag-element-backward)))
  647. ;; 2. Error when trying to swap nested elements.
  648. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  649. (forward-line)
  650. (should-error (org-drag-element-backward)))
  651. ;; 3. Error when trying to swap an headline element and
  652. ;; a non-headline element.
  653. (org-test-with-temp-text "Test.\n* Head 1"
  654. (forward-line)
  655. (should-error (org-drag-element-backward)))
  656. ;; 4. Otherwise, swap elements, preserving column and blank lines
  657. ;; between elements.
  658. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  659. (search-forward "graph")
  660. (org-drag-element-backward)
  661. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  662. (should (looking-at " 2")))
  663. ;; 5. Preserve visibility of elements and their contents.
  664. (org-test-with-temp-text "
  665. #+BEGIN_CENTER
  666. Text.
  667. #+END_CENTER
  668. - item 1
  669. #+BEGIN_QUOTE
  670. Text.
  671. #+END_QUOTE"
  672. (while (search-forward "BEGIN_" nil t) (org-cycle))
  673. (search-backward "- item 1")
  674. (org-drag-element-backward)
  675. (should
  676. (equal
  677. '((63 . 82) (26 . 48))
  678. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  679. (overlays-in (point-min) (point-max)))))))
  680. (ert-deftest test-org/drag-element-forward ()
  681. "Test `org-drag-element-forward' specifications."
  682. ;; 1. Error when trying to move first element of buffer.
  683. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  684. (goto-line 3)
  685. (should-error (org-drag-element-forward)))
  686. ;; 2. Error when trying to swap nested elements.
  687. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  688. (forward-line)
  689. (should-error (org-drag-element-forward)))
  690. ;; 3. Error when trying to swap a non-headline element and an
  691. ;; headline.
  692. (org-test-with-temp-text "Test.\n* Head 1"
  693. (should-error (org-drag-element-forward)))
  694. ;; 4. Otherwise, swap elements, preserving column and blank lines
  695. ;; between elements.
  696. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  697. (search-forward "graph")
  698. (org-drag-element-forward)
  699. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  700. (should (looking-at " 1")))
  701. ;; 5. Preserve visibility of elements and their contents.
  702. (org-test-with-temp-text "
  703. #+BEGIN_CENTER
  704. Text.
  705. #+END_CENTER
  706. - item 1
  707. #+BEGIN_QUOTE
  708. Text.
  709. #+END_QUOTE"
  710. (while (search-forward "BEGIN_" nil t) (org-cycle))
  711. (search-backward "#+BEGIN_CENTER")
  712. (org-drag-element-forward)
  713. (should
  714. (equal
  715. '((63 . 82) (26 . 48))
  716. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  717. (overlays-in (point-min) (point-max)))))))
  718. (provide 'test-org)
  719. ;;; test-org.el ends here