test-org.el 26 KB

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