test-org.el 28 KB

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