test-org.el 26 KB

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