test-org.el 33 KB

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