test-org.el 33 KB

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