test-org.el 33 KB

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