test-org.el 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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. ;;; Node Properties
  361. (ert-deftest test-org/accumulated-properties-in-drawers ()
  362. "Ensure properties accumulate in subtree drawers."
  363. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  364. (org-babel-next-src-block)
  365. (should (equal '(2 1) (org-babel-execute-src-block)))))
  366. ;;; Mark Region
  367. (ert-deftest test-org/mark-subtree ()
  368. "Test `org-mark-subtree' specifications."
  369. ;; Error when point is before first headline.
  370. (should-error
  371. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  372. (progn (transient-mark-mode 1)
  373. (org-mark-subtree))))
  374. ;; Without argument, mark current subtree.
  375. (should
  376. (equal
  377. '(12 32)
  378. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  379. (progn (transient-mark-mode 1)
  380. (forward-line 2)
  381. (org-mark-subtree)
  382. (list (region-beginning) (region-end))))))
  383. ;; With an argument, move ARG up.
  384. (should
  385. (equal
  386. '(1 32)
  387. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  388. (progn (transient-mark-mode 1)
  389. (forward-line 2)
  390. (org-mark-subtree 1)
  391. (list (region-beginning) (region-end))))))
  392. ;; Do not get fooled by inlinetasks.
  393. (when (featurep 'org-inlinetask)
  394. (should
  395. (= 1
  396. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  397. (progn (transient-mark-mode 1)
  398. (forward-line 1)
  399. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  400. (region-beginning)))))))
  401. ;;; Navigation
  402. (ert-deftest test-org/beginning-of-line ()
  403. "Test `org-beginning-of-line' specifications."
  404. ;; Standard test.
  405. (should
  406. (org-test-with-temp-text "Some text\nSome other text"
  407. (progn (org-beginning-of-line) (bolp))))
  408. ;; Standard test with `visual-line-mode'.
  409. (should-not
  410. (org-test-with-temp-text "A long line of text\nSome other text"
  411. (progn (visual-line-mode)
  412. (forward-char 2)
  413. (dotimes (i 1000) (insert "very "))
  414. (org-beginning-of-line)
  415. (bolp))))
  416. ;; At an headline with special movement.
  417. (should
  418. (org-test-with-temp-text "* TODO Headline"
  419. (let ((org-special-ctrl-a/e t))
  420. (org-end-of-line)
  421. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  422. (progn (org-beginning-of-line) (bolp))
  423. (progn (org-beginning-of-line) (looking-at "Headline")))))))
  424. (ert-deftest test-org/end-of-line ()
  425. "Test `org-end-of-line' specifications."
  426. ;; Standard test.
  427. (should
  428. (org-test-with-temp-text "Some text\nSome other text"
  429. (progn (org-end-of-line) (eolp))))
  430. ;; Standard test with `visual-line-mode'.
  431. (should-not
  432. (org-test-with-temp-text "A long line of text\nSome other text"
  433. (progn (visual-line-mode)
  434. (forward-char 2)
  435. (dotimes (i 1000) (insert "very "))
  436. (goto-char (point-min))
  437. (org-end-of-line)
  438. (eolp))))
  439. ;; At an headline with special movement.
  440. (should
  441. (org-test-with-temp-text "* Headline1 :tag:\n"
  442. (let ((org-special-ctrl-a/e t))
  443. (and (progn (org-end-of-line) (looking-at " :tag:"))
  444. (progn (org-end-of-line) (eolp))
  445. (progn (org-end-of-line) (looking-at " :tag:"))))))
  446. ;; At an headline without special movement.
  447. (should
  448. (org-test-with-temp-text "* Headline2 :tag:\n"
  449. (let ((org-special-ctrl-a/e nil))
  450. (and (progn (org-end-of-line) (eolp))
  451. (progn (org-end-of-line) (eolp))))))
  452. ;; At an headline, with reversed movement.
  453. (should
  454. (org-test-with-temp-text "* Headline3 :tag:\n"
  455. (let ((org-special-ctrl-a/e 'reversed)
  456. (this-command last-command))
  457. (and (progn (org-end-of-line) (eolp))
  458. (progn (org-end-of-line) (looking-at " :tag:"))))))
  459. ;; At a block without hidden contents.
  460. (should
  461. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  462. (progn (org-end-of-line) (eolp))))
  463. ;; At a block with hidden contents.
  464. (should-not
  465. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  466. (let ((org-special-ctrl-a/e t))
  467. (org-hide-block-toggle)
  468. (org-end-of-line)
  469. (eobp)))))
  470. (ert-deftest test-org/forward-element ()
  471. "Test `org-forward-element' specifications."
  472. ;; 1. At EOB: should error.
  473. (org-test-with-temp-text "Some text\n"
  474. (goto-char (point-max))
  475. (should-error (org-forward-element)))
  476. ;; 2. Standard move: expected to ignore blank lines.
  477. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  478. (org-forward-element)
  479. (should (looking-at (regexp-quote "Second paragraph."))))
  480. ;; 3. Headline tests.
  481. (org-test-with-temp-text "
  482. * Head 1
  483. ** Head 1.1
  484. *** Head 1.1.1
  485. ** Head 1.2"
  486. ;; 3.1. At an headline beginning: move to next headline at the
  487. ;; same level.
  488. (goto-line 3)
  489. (org-forward-element)
  490. (should (looking-at (regexp-quote "** Head 1.2")))
  491. ;; 3.2. At an headline beginning: move to parent headline if no
  492. ;; headline at the same level.
  493. (goto-line 3)
  494. (org-forward-element)
  495. (should (looking-at (regexp-quote "** Head 1.2"))))
  496. ;; 4. Greater element tests.
  497. (org-test-with-temp-text
  498. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  499. ;; 4.1. At a greater element: expected to skip contents.
  500. (org-forward-element)
  501. (should (looking-at (regexp-quote "Outside.")))
  502. ;; 4.2. At the end of greater element contents: expected to skip
  503. ;; to the end of the greater element.
  504. (goto-line 2)
  505. (org-forward-element)
  506. (should (looking-at (regexp-quote "Outside."))))
  507. ;; 5. List tests.
  508. (org-test-with-temp-text "
  509. - item1
  510. - sub1
  511. - sub2
  512. - sub3
  513. Inner paragraph.
  514. - item2
  515. Outside."
  516. ;; 5.1. At list top point: expected to move to the element after
  517. ;; the list.
  518. (goto-line 2)
  519. (org-forward-element)
  520. (should (looking-at (regexp-quote "Outside.")))
  521. ;; 5.2. Special case: at the first line of a sub-list, but not at
  522. ;; beginning of line, move to next item.
  523. (goto-line 2)
  524. (forward-char)
  525. (org-forward-element)
  526. (should (looking-at "- item2"))
  527. (goto-line 4)
  528. (forward-char)
  529. (org-forward-element)
  530. (should (looking-at " - sub2"))
  531. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  532. (goto-line 4)
  533. (org-forward-element)
  534. (should (looking-at (regexp-quote " Inner paragraph.")))
  535. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  536. (goto-line 8)
  537. (org-forward-element)
  538. (should (looking-at (regexp-quote " Inner paragraph.")))
  539. ;; 5.5. At an item: expected to move to next item, if any.
  540. (goto-line 6)
  541. (org-forward-element)
  542. (should (looking-at " - sub3"))))
  543. (ert-deftest test-org/backward-element ()
  544. "Test `org-backward-element' specifications."
  545. ;; 1. Should error at BOB.
  546. (org-test-with-temp-text " \nParagraph."
  547. (should-error (org-backward-element)))
  548. ;; 2. Should move at BOB when called on the first element in buffer.
  549. (should
  550. (org-test-with-temp-text "\n#+TITLE: test"
  551. (progn (forward-line)
  552. (org-backward-element)
  553. (bobp))))
  554. ;; 3. Not at the beginning of an element: move at its beginning.
  555. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  556. (goto-line 3)
  557. (end-of-line)
  558. (org-backward-element)
  559. (should (looking-at (regexp-quote "Paragraph2."))))
  560. ;; 4. Headline tests.
  561. (org-test-with-temp-text "
  562. * Head 1
  563. ** Head 1.1
  564. *** Head 1.1.1
  565. ** Head 1.2"
  566. ;; 4.1. At an headline beginning: move to previous headline at the
  567. ;; same level.
  568. (goto-line 5)
  569. (org-backward-element)
  570. (should (looking-at (regexp-quote "** Head 1.1")))
  571. ;; 4.2. At an headline beginning: move to parent headline if no
  572. ;; headline at the same level.
  573. (goto-line 3)
  574. (org-backward-element)
  575. (should (looking-at (regexp-quote "* Head 1")))
  576. ;; 4.3. At the first top-level headline: should error.
  577. (goto-line 2)
  578. (should-error (org-backward-element)))
  579. ;; 5. At beginning of first element inside a greater element:
  580. ;; expected to move to greater element's beginning.
  581. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  582. (goto-line 3)
  583. (org-backward-element)
  584. (should (looking-at "#\\+BEGIN_CENTER")))
  585. ;; 6. At the beginning of the first element in a section: should
  586. ;; move back to headline, if any.
  587. (should
  588. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  589. (progn (goto-char (point-max))
  590. (beginning-of-line)
  591. (org-backward-element)
  592. (org-at-heading-p))))
  593. ;; 7. List tests.
  594. (org-test-with-temp-text "
  595. - item1
  596. - sub1
  597. - sub2
  598. - sub3
  599. Inner paragraph.
  600. - item2
  601. Outside."
  602. ;; 7.1. At beginning of sub-list: expected to move to the
  603. ;; paragraph before it.
  604. (goto-line 4)
  605. (org-backward-element)
  606. (should (looking-at "item1"))
  607. ;; 7.2. At an item in a list: expected to move at previous item.
  608. (goto-line 8)
  609. (org-backward-element)
  610. (should (looking-at " - sub2"))
  611. (goto-line 12)
  612. (org-backward-element)
  613. (should (looking-at "- item1"))
  614. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  615. ;; beginning.
  616. (goto-line 10)
  617. (org-backward-element)
  618. (should (looking-at " - sub1"))
  619. (goto-line 15)
  620. (org-backward-element)
  621. (should (looking-at "- item1"))
  622. ;; 7.4. At blank-lines before list end: expected to move to top
  623. ;; item.
  624. (goto-line 14)
  625. (org-backward-element)
  626. (should (looking-at "- item1"))))
  627. (ert-deftest test-org/up-element ()
  628. "Test `org-up-element' specifications."
  629. ;; 1. At BOB or with no surrounding element: should error.
  630. (org-test-with-temp-text "Paragraph."
  631. (should-error (org-up-element)))
  632. (org-test-with-temp-text "* Head1\n* Head2"
  633. (goto-line 2)
  634. (should-error (org-up-element)))
  635. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  636. (goto-line 3)
  637. (should-error (org-up-element)))
  638. ;; 2. At an headline: move to parent headline.
  639. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  640. (goto-line 3)
  641. (org-up-element)
  642. (should (looking-at "\\* Head1")))
  643. ;; 3. Inside a greater element: move to greater element beginning.
  644. (org-test-with-temp-text
  645. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  646. (goto-line 3)
  647. (org-up-element)
  648. (should (looking-at "#\\+BEGIN_CENTER")))
  649. ;; 4. List tests.
  650. (org-test-with-temp-text "* Top
  651. - item1
  652. - sub1
  653. - sub2
  654. Paragraph within sub2.
  655. - item2"
  656. ;; 4.1. Within an item: move to the item beginning.
  657. (goto-line 8)
  658. (org-up-element)
  659. (should (looking-at " - sub2"))
  660. ;; 4.2. At an item in a sub-list: move to parent item.
  661. (goto-line 4)
  662. (org-up-element)
  663. (should (looking-at "- item1"))
  664. ;; 4.3. At an item in top list: move to beginning of whole list.
  665. (goto-line 10)
  666. (org-up-element)
  667. (should (looking-at "- item1"))
  668. ;; 4.4. Special case. At very top point: should move to parent of
  669. ;; list.
  670. (goto-line 2)
  671. (org-up-element)
  672. (should (looking-at "\\* Top"))))
  673. (ert-deftest test-org/down-element ()
  674. "Test `org-down-element' specifications."
  675. ;; Error when the element hasn't got a recursive type.
  676. (org-test-with-temp-text "Paragraph."
  677. (should-error (org-down-element)))
  678. ;; Error when the element has no contents
  679. (org-test-with-temp-text "* Headline"
  680. (should-error (org-down-element)))
  681. ;; When at a plain-list, move to first item.
  682. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  683. (goto-line 2)
  684. (org-down-element)
  685. (should (looking-at " - Item 1.1")))
  686. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  687. (org-down-element)
  688. (should (looking-at " Item 1")))
  689. ;; When at a table, move to first row
  690. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  691. (org-down-element)
  692. (should (looking-at " a | b |")))
  693. ;; Otherwise, move inside the greater element.
  694. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  695. (org-down-element)
  696. (should (looking-at "Paragraph"))))
  697. (ert-deftest test-org/drag-element-backward ()
  698. "Test `org-drag-element-backward' specifications."
  699. ;; 1. Error when trying to move first element of buffer.
  700. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  701. (should-error (org-drag-element-backward)))
  702. ;; 2. Error when trying to swap nested elements.
  703. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  704. (forward-line)
  705. (should-error (org-drag-element-backward)))
  706. ;; 3. Error when trying to swap an headline element and
  707. ;; a non-headline element.
  708. (org-test-with-temp-text "Test.\n* Head 1"
  709. (forward-line)
  710. (should-error (org-drag-element-backward)))
  711. ;; 4. Otherwise, swap elements, preserving column and blank lines
  712. ;; between elements.
  713. (org-test-with-temp-text "Para1\n\n\nParagraph 2\n\nPara3"
  714. (search-forward "graph")
  715. (org-drag-element-backward)
  716. (should (equal (buffer-string) "Paragraph 2\n\n\nPara1\n\nPara3"))
  717. (should (looking-at " 2")))
  718. ;; 5. Preserve visibility of elements and their contents.
  719. (org-test-with-temp-text "
  720. #+BEGIN_CENTER
  721. Text.
  722. #+END_CENTER
  723. - item 1
  724. #+BEGIN_QUOTE
  725. Text.
  726. #+END_QUOTE"
  727. (while (search-forward "BEGIN_" nil t) (org-cycle))
  728. (search-backward "- item 1")
  729. (org-drag-element-backward)
  730. (should
  731. (equal
  732. '((63 . 82) (26 . 48))
  733. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  734. (overlays-in (point-min) (point-max)))))))
  735. (ert-deftest test-org/drag-element-forward ()
  736. "Test `org-drag-element-forward' specifications."
  737. ;; 1. Error when trying to move first element of buffer.
  738. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  739. (goto-line 3)
  740. (should-error (org-drag-element-forward)))
  741. ;; 2. Error when trying to swap nested elements.
  742. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  743. (forward-line)
  744. (should-error (org-drag-element-forward)))
  745. ;; 3. Error when trying to swap a non-headline element and an
  746. ;; headline.
  747. (org-test-with-temp-text "Test.\n* Head 1"
  748. (should-error (org-drag-element-forward)))
  749. ;; 4. Otherwise, swap elements, preserving column and blank lines
  750. ;; between elements.
  751. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  752. (search-forward "graph")
  753. (org-drag-element-forward)
  754. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  755. (should (looking-at " 1")))
  756. ;; 5. Preserve visibility of elements and their contents.
  757. (org-test-with-temp-text "
  758. #+BEGIN_CENTER
  759. Text.
  760. #+END_CENTER
  761. - item 1
  762. #+BEGIN_QUOTE
  763. Text.
  764. #+END_QUOTE"
  765. (while (search-forward "BEGIN_" nil t) (org-cycle))
  766. (search-backward "#+BEGIN_CENTER")
  767. (org-drag-element-forward)
  768. (should
  769. (equal
  770. '((63 . 82) (26 . 48))
  771. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  772. (overlays-in (point-min) (point-max)))))))
  773. ;;; Planning
  774. (ert-deftest test-org/timestamp-has-time-p ()
  775. "Test `org-timestamp-has-time-p' specifications."
  776. ;; With time.
  777. (should
  778. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  779. (org-timestamp-has-time-p (org-element-context))))
  780. ;; Without time.
  781. (should-not
  782. (org-test-with-temp-text "<2012-03-29 Thu>"
  783. (org-timestamp-has-time-p (org-element-context)))))
  784. (ert-deftest test-org/timestamp-format ()
  785. "Test `org-timestamp-format' specifications."
  786. ;; Regular test.
  787. (should
  788. (equal
  789. "2012-03-29 16:40"
  790. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  791. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  792. ;; Range end.
  793. (should
  794. (equal
  795. "2012-03-29"
  796. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  797. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  798. (ert-deftest test-org/timestamp-split-range ()
  799. "Test `org-timestamp-split-range' specifications."
  800. ;; Extract range start (active).
  801. (should
  802. (equal '(2012 3 29)
  803. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  804. (let ((ts (org-timestamp-split-range (org-element-context))))
  805. (mapcar (lambda (p) (org-element-property p ts))
  806. '(:year-end :month-end :day-end))))))
  807. ;; Extract range start (inactive)
  808. (should
  809. (equal '(2012 3 29)
  810. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  811. (let ((ts (org-timestamp-split-range (org-element-context))))
  812. (mapcar (lambda (p) (org-element-property p ts))
  813. '(:year-end :month-end :day-end))))))
  814. ;; Extract range end (active).
  815. (should
  816. (equal '(2012 3 30)
  817. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  818. (let ((ts (org-timestamp-split-range
  819. (org-element-context) t)))
  820. (mapcar (lambda (p) (org-element-property p ts))
  821. '(:year-end :month-end :day-end))))))
  822. ;; Extract range end (inactive)
  823. (should
  824. (equal '(2012 3 30)
  825. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  826. (let ((ts (org-timestamp-split-range
  827. (org-element-context) t)))
  828. (mapcar (lambda (p) (org-element-property p ts))
  829. '(:year-end :month-end :day-end))))))
  830. ;; Return the timestamp if not a range.
  831. (should
  832. (org-test-with-temp-text "[2012-03-29 Thu]"
  833. (let* ((ts-orig (org-element-context))
  834. (ts-copy (org-timestamp-split-range ts-orig)))
  835. (eq ts-orig ts-copy))))
  836. (should
  837. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  838. (let* ((ts-orig (org-element-context))
  839. (ts-copy (org-timestamp-split-range ts-orig)))
  840. (eq ts-orig ts-copy))))
  841. ;; Check that parent is the same when a range was split.
  842. (should
  843. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  844. (let* ((ts-orig (org-element-context))
  845. (ts-copy (org-timestamp-split-range ts-orig)))
  846. (eq (org-element-property :parent ts-orig)
  847. (org-element-property :parent ts-copy))))))
  848. (ert-deftest test-org/timestamp-translate ()
  849. "Test `org-timestamp-translate' specifications."
  850. ;; Translate whole date range.
  851. (should
  852. (equal "<29>--<30>"
  853. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  854. (let ((org-display-custom-times t)
  855. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  856. (org-timestamp-translate (org-element-context))))))
  857. ;; Translate date range start.
  858. (should
  859. (equal "<29>"
  860. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  861. (let ((org-display-custom-times t)
  862. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  863. (org-timestamp-translate (org-element-context) 'start)))))
  864. ;; Translate date range end.
  865. (should
  866. (equal "<30>"
  867. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  868. (let ((org-display-custom-times t)
  869. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  870. (org-timestamp-translate (org-element-context) 'end)))))
  871. ;; Translate time range.
  872. (should
  873. (equal "<08>--<16>"
  874. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  875. (let ((org-display-custom-times t)
  876. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  877. (org-timestamp-translate (org-element-context))))))
  878. ;; Translate non-range timestamp.
  879. (should
  880. (equal "<29>"
  881. (org-test-with-temp-text "<2012-03-29 Thu>"
  882. (let ((org-display-custom-times t)
  883. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  884. (org-timestamp-translate (org-element-context))))))
  885. ;; Do not change `diary' timestamps.
  886. (should
  887. (equal "<%%(org-float t 4 2)>"
  888. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  889. (let ((org-display-custom-times t)
  890. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  891. (org-timestamp-translate (org-element-context)))))))
  892. ;;; Targets and Radio Targets
  893. (ert-deftest test-org/all-targets ()
  894. "Test `org-all-targets' specifications."
  895. ;; Without an argument.
  896. (should
  897. (equal '("radio-target" "target")
  898. (org-test-with-temp-text "<<target>> <<<radio-target>>>\n: <<verb>>"
  899. (org-all-targets))))
  900. (should
  901. (equal '("radio-target")
  902. (org-test-with-temp-text "<<<radio-target>>>!" (org-all-targets))))
  903. ;; With argument.
  904. (should
  905. (equal '("radio-target")
  906. (org-test-with-temp-text "<<target>> <<<radio-target>>>"
  907. (org-all-targets t)))))
  908. (provide 'test-org)
  909. ;;; test-org.el ends here