test-org.el 24 KB

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