test-org.el 29 KB

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