test-org.el 32 KB

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